我试图使用MPI做一些并行工作,比如在网络图上计算三角形。为了让每个进程计算相同的次数,我使用scatterv将不同的数据分配到不同的进程中。这是我的代码:
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int i, j, k, v1, v2, vertices;
struct Graph *g;
int *n;
int rank, size;
// Build the Graph from the input file
f = fopen(argv[1], "r");
fscanf(f, "%d", &vertices);
g = createGraph(vertices);
while ((fscanf(f, "%d%d", &v1, &v2)) != EOF) {
addEdge(g, v1, v2);
}
fclose(f);
n = (int *)malloc(sizeof(int) * vertices);
for (i = 0; i < vertices; i++) {
n[i] = i;
}
createNeighborSets(g);
printf("create graph\n");
int sendcount[size];
int offset[size];
int count = 0;
int *localVertices;
int localNum;
MPI_Status status;
MPI_Request request;
if (rank == 0) {
calculateDistribution(send count, offset);
for (int i = 1; i < size; i++) {
MPI_Send(&sendcount[i], 1, MPI_INT, i, 3, MPI_COMM_WORLD);
}
localNum = sendcount[0];
} else {
MPI_Recv(&localNum, 1, MPI_INT, 0, 3, MPI_COMM_WORLD, &status);
}
localVertices = (int *)malloc(sizeof(int) * localNum);
MPI_Scatterv(n, sendcount, offset, MPI_INT, localVertices, localNum, MPI_INT, 0, MPI_COMM_WORLD);
当我运行我的代码时,错误消息是这样的:
[MacBook-Pro-7:96512] *** Process received signal ***
[MacBook-Pro-7:96512] Signal: Segmentation fault: 11 (11)
[MacBook-Pro-7:96512] Signal code: (0)
[MacBook-Pro-7:96512] Failing at address: 0x0
[MacBook-Pro-7:96512] [ 0] [MacBook-Pro-7:96513] *** Process received signal ***
[MacBook-Pro-7:96513] Signal: Segmentation fault: 11 (11)
[MacBook-Pro-7:96513] Signal code: (0)
[MacBook-Pro-7:96513] Failing at address: 0x0
[MacBook-Pro-7:96513] [ 0] 0 libsystem_platform.dylib 0x00007fff633b4f5a _sigtramp + 26
[MacBook-Pro-7:96513] [ 1] [MacBook-Pro-7:96510] *** Process received signal ***
[MacBook-Pro-7:96510] Signal: Segmentation fault: 11 (11)
[MacBook-Pro-7:96510] Signal code: (0)
[MacBook-Pro-7:96510] Failing at address: 0x0
[MacBook-Pro-7:96510] [ 0] 0 libsystem_platform.dylib 0x00007fff633b4f5a _sigtramp + 26
[MacBook-Pro-7:96510] [ 1] 0 ??? 0x0000000000000001 0x0 + 1
当我在scatterv之后输出localVertices的值时,我发现我的线程已经有了正确的成员,但是在进一步处理三角形计数之前发生了错误。