gpt4 book ai didi

c - MPI 连续类型示例给出 fatal error

转载 作者:行者123 更新时间:2023-11-30 17:37:06 30 4
gpt4 key购买 nike

我是 MPI 的初学者,我通过以下链接进行学习:https://computing.llnl.gov/tutorials/mpi/#Derived_Data_Types 。在本节的最后,有一个示例。我在这里添加了代码。区别只是第 30 行和第 33 行中的两个 printf 函数。

问题是代码不起作用。它给出了一个 fatal error ,例如:

MPI_Send(173): MPI_Send(buf=0x7fff20072940, count=1, dtype=USER<contig>, dest=4, tag=1, 
MPI_COMM_WORLD) failed
MPI_Send(98).: Invalid rank has value 4 but must be nonnegative and less than 4

我认为它尝试发送处理器 4,但它不存在。它为什么要这么做?另外,当我删除 printf 函数时,程序不会显示任何内容,我只看到命令行的光标。

我编译它:

mpicc mpi_contigous_data.cpp -o contigous_type

我运行它:

mpirun -np 4 ./contigous_type

代码在这里:

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>

#define SIZE 4

int main (int argc, char *argv[])
{
int numtasks, rank, source=0, dest, tag=1, i;
float a[SIZE][SIZE] =
{1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0};
float b[SIZE];

MPI_Status stat;
MPI_Datatype rowtype;

MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);

MPI_Type_contiguous(SIZE, MPI_FLOAT, &rowtype);
MPI_Type_commit(&rowtype);

if (numtasks == SIZE) {
if (rank == 0) {
for (i=0; i<numtasks; i++)
printf("From root, sending to %d\n",i);
MPI_Send(&a[i][0], 1, rowtype, i, tag, MPI_COMM_WORLD);
}
printf("My rank is %d,waiting message\n",rank);
MPI_Recv(b, SIZE, MPI_FLOAT, source, tag, MPI_COMM_WORLD, &stat);
printf("rank= %d b= %3.1f %3.1f %3.1f %3.1f\n",
rank,b[0],b[1],b[2],b[3]);
}
else
printf("Must specify %d processors. Terminating.\n",SIZE);

MPI_Type_free(&rowtype);
MPI_Finalize();
}

最佳答案

请记住,排名编号从 0 开始。因此,如果您像这样启动程序:

mpirun -np 4 ./contigous_type

您的排名将为 0、1、2、3。没有 4。

更具体地说,对于您的问题,您需要在第 29 行的 for 循环周围添加花括号。您将执行 print 语句 4 次,并执行一次 MPI_Send 。使用一个能够自动缩进的优秀编辑器在这里非常有帮助。

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>

#define SIZE 4

int main (int argc, char *argv[])
{
int numtasks, rank, source=0, dest, tag=1, i;
float a[SIZE][SIZE] =
{1.0, 2.0, 3.0, 4.0,
5.0, 6.0, 7.0, 8.0,
9.0, 10.0, 11.0, 12.0,
13.0, 14.0, 15.0, 16.0};
float b[SIZE];

MPI_Status stat;
MPI_Datatype rowtype;

MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);

MPI_Type_contiguous(SIZE, MPI_FLOAT, &rowtype);
MPI_Type_commit(&rowtype);

if (numtasks == SIZE) {
if (rank == 0) {
for (i=0; i<numtasks; i++) { /*<--- HERE!*/
printf("From root, sending to %d\n",i);
MPI_Send(&a[i][0], 1, rowtype, i, tag, MPI_COMM_WORLD);
} /*<--- HERE!*/
}
printf("My rank is %d,waiting message\n",rank);
MPI_Recv(b, SIZE, MPI_FLOAT, source, tag, MPI_COMM_WORLD, &stat);
printf("rank= %d b= %3.1f %3.1f %3.1f %3.1f\n",
rank,b[0],b[1],b[2],b[3]);
}
else
printf("Must specify %d processors. Terminating.\n",SIZE);

MPI_Type_free(&rowtype);
MPI_Finalize();
}

但是您的代码中还有很多其他问题。我不会在这里修复所有这些问题,因为这对您没有多大帮助。最明显的事情是您的发送和接收不匹配。每个进程都发送 4 条消息,但只接收一条。

关于c - MPI 连续类型示例给出 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22507633/

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