0

This is the example code:

#include <mpi.h>

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]) {
  int size, rank;
  const int root = 0;

  int datasize = atoi(argv[1]);

  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);

  int nodeDest = rank + 1;
  if (nodeDest > size - 1) {
    nodeDest = 0;
  }
  int nodeFrom = rank - 1;
  if (nodeFrom < 0) {
    nodeFrom = size - 1;
  }

  MPI_Status status;
  MPI_Request req[2];

  int *data = new int[datasize];
  int *data2 = new int[datasize];
  for (int i = 0; i < datasize; i++) data[i] = rank;


  cout << "Before send" << endl;
  MPI_Isend(data, datasize, MPI_INT, nodeDest, 0, MPI_COMM_WORLD, &req[0]);
  delete[] data;
  cout << "After send" << endl;
  MPI_Recv(data2, datasize, MPI_INT, nodeFrom, 0, MPI_COMM_WORLD,&status);
  cout << "After Irecv" << endl;
  MPI_Wait(&req[0], MPI_STATUS_IGNORE);
  cout << "After wait" << endl;

  delete[] data2;

  MPI_Finalize();
  return 0;
}

I use mpich-4.1.2 and intel mpi(oneAPI 2020). When the buffer size is small, both are ok.

mpirun -np 4 ./test 100
/opt/intel/compilers_and_libraries_2020.0.139/linux/mpi/intel64/bin/mpirun -np 4 ./test 100

But when the buffer size is huge, mpich got EXIT CODE: 9

mpirun -np 4 ./test 100000
Before send
Before send
After send
Before sendBefore send
After send
After send

After send

===================================================================================
=   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
=   PID 321877 RUNNING AT 
=   EXIT CODE: 9
=   CLEANING UP REMAINING PROCESSES
=   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions

I know what caused this problem is line delete[] data; , but not clear about the details behind it.

2
  • You have a delete immediately after you Isend. That means you probably delete the buffer before it has been sent. You can not touch the buffer until after the Wait call. Commented Jun 12, 2024 at 20:21
  • "short" messages are generally sent in eager mode, which means the message is immediately sent and nothing bad will happen if the send buffer is modified (or deallocated) before MPI_Wait(). "short" can depend on the library, the interconnect and other factors at runtime. That being said, the program is incorrect w.r.t. the MPI standard, and is hence undefined behavior. Commented Jun 12, 2024 at 23:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.