gpt4 book ai didi

c - MPI,我更改了一个非常简单的程序以使用 INT 而不是 CHAR 作为消息缓冲区的数据类型,但现在它失败了,这是为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 02:27:46 24 4
gpt4 key购买 nike

我拿了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;
}

显然代码应该做同样的事情,因为错误传递了一个整数数组而不是字符串,实际上我收到了这个错误:

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

最佳答案

回顾 int MPI_Send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) 等。

count 是发送缓冲区中元素的数量,而不是它的大小

代码应该是:

// 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

关于c - MPI,我更改了一个非常简单的程序以使用 INT 而不是 CHAR 作为消息缓冲区的数据类型,但现在它失败了,这是为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48599043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com