我的主进程有一个字符串数组,它发送给从属进程。从属进程快速排序数组并将其发回。问题在于收回它。
在MPI_Recv之后,只填充数组的前3个索引,其余元素为空。当我在发送之前打印数组时,元素就在那里。但收到后,除了那些3,其他所有人都是空的。
发送消息代码的slave。它收到一个字符串数组,quicksotr它并将其发送回订购
// receiving number of elements
MPI_Recv(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
// allocating the array
array = (char**) malloc(sizeof(*array) * size);
array[0] = (char*) malloc(sizeof(*array[0]) * size * buf);
for(i=1; i<size; i++)
array[i] = &(array[0][i*buf]);
// receiving the array
MPI_Recv(&array[0][0], size*buf, MPI_CHAR, 0, tag, MPI_COMM_WORLD, &status);
// sorting the array
quicksort(array, 0, size-1);
// sending the ordered array back
//DEBUG printf("%d: Sending for %d -> %d...\n", rank, 0, size);
MPI_Send(&size, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
//DEBUG printf("%d: Sending for %d -> %d...\n", rank, 0, size*buf);
//printArray(array, size);
printf("%d: before sending\n", rank);
for(i=0; i<size; i++)
printf("%d: %s", rank, array[i]);
MPI_Send(&array[0][0], size*buf, MPI_CHAR, 0, tag, MPI_COMM_WORLD);
主人,谁发送数组,并应该接收它回来订购
// sending the messages
int j = 0;
for(i=1; i<procs; i++){
MPI_Send(&division[i], 1, MPI_INT, i, tag, MPI_COMM_WORLD);
MPI_Send(&array[j][0], division[i]*buf, MPI_CHAR, i, tag, MPI_COMM_WORLD);
j += division[i];
}
printf("%d: All Sent\n", rank);
// receiving the ordered arrays
printf("%d: Waiting for answers...\n", rank);
// receiving number of elements
MPI_Recv(&sizeA, 1, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &status);
// alocating an array
arrayA = (char**) malloc(sizeof(*arrayA) * sizeA);
arrayA[0] = (char*) malloc(sizeof(*arrayA[0]) * sizeA * buf);
for(i=1; i<sizeA; i++)
arrayA[i] = &(arrayA[0][i*buf]);
// receiving the array
MPI_Recv(&arrayA[0][0], sizeA*buf, MPI_CHAR, status.MPI_SOURCE, tag, MPI_COMM_WORLD, &status);
for(i=0; i<sizeA; i++)
printf("%i: %s", i, arrayA[i]);
编辑问题似乎在快速排序上。如果我评论quicksort线我收到整个阵列。虽然它在顺序版本上运行良好。这是代码。
void quicksort(char **array, int left, int right){
// array empty
if(right - left < 1)
return;
// end of quicksort, just two elements left
if(right - left == 1){
if(strcomp(array[left], array[right]) > 0)
swap(array, left, right);
return;
}
int i = left + 1; // left + 1: used to avoid the pivot, on the first position
int j = right; // right
int pivot = (left + right) / 2; // pivot position
char *key = array[pivot]; // string with the array[pivot] element
// moving the pivot to the beginning
swap(array, left, pivot);
// quicksorting
while(i < j){
// if an element is on the left of the pivot and, is <= than the pivot
// keep the element there and increase i
while(i <= right && strcomp(array[i], key) < 0){
i++;
}
// if an element is on the right of the pivot, and is > than the pivot
// keep the element there and decrease j
while(j >= left && strcomp(array[j], key) > 0){
j--;
}
// array[i] > key, and should be on the right
// array[j] <= key, and should be on the left
if(i < j){
swap(array, i, j);
}
}
// moving the pivot back to the middle
swap(array, left, j);
// left recursion
if(left < j-1)
quicksort(array, left, j-1);
// right recursion
if(j+1 < right)
quicksort(array, j+1, right);
}