jeudi 23 juin 2016

MPI_Recv acts different when buffer is created with malloc?

Suppose I have a buffer buf into which I receive a known number of ints, denoted in size. I receive them from the main process in MPI as follows:

MPI_Recv(buf, size, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

However I am noticing a difference between the following two methods of declaring buf:

int buf[size];                       // This works fine.
int *buf = malloc(size*sizeof(int)); // This is not fine.

The issue comes in later when I try to operate upon buf. For example if I have a function void sort(int *arr, const size_t arrSize), which I call with sort(buf, sizof(buf)), which tries to reorder the members of the array, the first use of buf (buf[size]) works just dandy, but when I use malloc, when I check the contents later the ordering of members has not changed. Everything is in the exact same order.

This is only a problem for arrays received through MPI_Recv(). In any other circumstances, sort(buf, sizeof(buf)) works perfectly no matter how I've declared it. But if I use MPI_Recv() to populate an array I've malloc'd, it doesn't change anything despite the fact that I have verified the array to contain the correct elements and have verified the sorting function to work in literally any other circumstance.

Note: I have also tried calloc(size*sizeof(int), sizeof(int)), which is actually my preferred way of doing things, but the results were the same as with malloc so for the sake of simplicity I used that in my example.

Aucun commentaire:

Enregistrer un commentaire