【发布时间】:2015-11-27 23:55:48
【问题描述】:
我是 MPI 的新手,我试图通过编写一个简单的 C 程序来理解其中的含义。我要做的就是拆分一个数组并将块发送到 N 个处理器。因此,每个处理器都会在其块中找到本地分钟。然后程序(在根目录或其他地方)找到全局最小值。
我研究过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;
}
我的代码可能存在多个错误,很抱歉无法在此处指定确切的问题。感谢您的任何建议。
【问题讨论】:
-
您可以使用
MPI_Scatter()而不是拆分数组并将MPI_Send()用于每个进程。同样,您也可以使用MPI_Gather()收集所有结果。 -
查看 MAX/MIN/MAXLOC/MINLOC 减少量。
-
@VladFeinstein 谢谢,我已经阅读了该主题。但正如我所提到的,我需要一些关于我的练习的帮助才能更好地理解它。
-
@PoojaNilangekar 谢谢,我会研究这些功能并提供反馈:)
标签: c++ c arrays parallel-processing mpi