我拿了the MPI example on the wikipedia page并修改它以使用整数而不是字符。
结果是:
#include <stdio.h>
#include <mpi.h>
#include <string.h>
int main(int argc, char **argv)
{
int buf[4];
int my_rank, num_procs;
/* Initialize the infrastructure necessary for communication */
MPI_Init(&argc, &argv);
/* Identify this process */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Find out how many total processes are active */
MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
int test[4] = {0, 1, 2};
/* Until this point, all programs have been doing exactly the same.
Here, we check the rank to distinguish the roles of the programs */
if (my_rank == 0) {
int other_rank;
printf("We have %i processes.\n", num_procs);
/* Send messages to all other processes */
for (other_rank = 1; other_rank < num_procs; other_rank++)
{
memcpy(buf, test, 4 * sizeof(int));
MPI_Send(buf, sizeof(buf), MPI_INT, other_rank,
0, MPI_COMM_WORLD);
}
/* Receive messages from all other process */
for (other_rank = 1; other_rank < num_procs; other_rank++)
{
MPI_Recv(buf, sizeof(buf), MPI_INT, other_rank,
0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
printf("%d\n", buf[other_rank]);
}
} else {
/* Receive message from process #0 */
MPI_Recv(buf, sizeof(buf), MPI_INT, 0,
0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
memcpy(buf, test, 4 * sizeof(int));
MPI_Send(buf, sizeof(buf), MPI_INT, 0,
0, MPI_COMM_WORLD);
}
/* Tear down the communication infrastructure */
MPI_Finalize();
return 0;
}
显然代码应该与bug传递整数数组而不是字符串之前做同样的事情,实际上我收到这个错误:
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Illegal instruction
(signal 4) This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions
我在这里做错了吗?
我正在用这个简单的命令编译和执行:
mpicc example.c && mpiexec -n 4 ./a.out
答案 0 :(得分:2)
审核int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm)
和其他人。
count
是发送缓冲区中的元素数,而不是 size 。
代码应该是:
// MPI_Send(buf, sizeof(buf), MPI_INT, other_rank, 0, MPI_COMM_WORLD);`
MPI_Send(buf, sizeof buf /sizeof buf[0], MPI_INT, other_rank, 0, MPI_COMM_WORLD);`
检查MPI_Recv();
以及所有类似的来电。 @Gilles Gouaillardet