我在MPI中很新穎,並試圖通過編寫一個簡單的C程序來理解它的含義。我想要做的是拆分陣列併發送塊到N處理器。因此,每個處理器將在其塊中找到本地分鐘。然後程序(在根或其他地方)找到全局最小值。在MPI中分割和傳遞數組塊
我研究了MPI_Send
,MPI_Isend
或MPI_Bcast
函數,但在使用一個而不是另一個的位置有點混淆。我需要我的程序的一般結構中的一些技巧:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define N 9 // array size
int A[N] = {0,2,1,5,4,3,7,6,8}; // this is a dummy array
int main(int argc, char *argv[]) {
int i, k = 0, size, rank, source = 0, dest = 1, count;
int tag = 1234;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
count = N/(size-1); // think size = 4 for this example
int *tempArray = malloc(count * sizeof(int));
int *localMins = malloc((size-1) * sizeof(int));
if (rank == 0) {
for(i=0; i<size; i+=count)
{
// Is it better to use MPI_Isend or MPI_Bcast here?
MPI_Send(&A[i], count, MPI_INT, dest, tag, MPI_COMM_WORLD);
printf("P0 sent a %d elements to P%d.\n", count, dest);
dest++;
}
}
else {
for(i=0; i<size; i+=count)
{
MPI_Recv(tempArray, count, MPI_INT, 0, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
localMins[k] = findMin(tempArray, count);
printf("Min for P%d is %d.\n", rank, localMins[k]);
k++;
}
}
MPI_Finalize();
int gMin = findMin(localMins, (size-1)); // where should I assign this
printf("Global min: %d\n", gMin); // and where should I print the results?
return 0;
}
可能有多個錯誤,對不起,不能在此處指定一個確切的問題。感謝您的任何建議。
http://stackoverflow.com/questions/10017301/mpi-blocking-vs-non-blocking? –
您可以使用'MPI_Scatter()'而不是分割數組並使用'MPI_Send()'到每個進程。同樣,你也可以使用'MPI_Gather()'來收集所有結果。 –
查看MAX/MIN/MAXLOC/MINLOC減少量。 – Jeff