gpt4 book ai didi

c++ - 使用 MPI_Scatter 时出现段错误

转载 作者:行者123 更新时间:2023-11-28 04:08:25 25 4
gpt4 key购买 nike

我对 MPI_Scatter 有疑问。不知道如何使用它,我当前的程序在启动时因段错误而崩溃。

我猜 MPI_Scatter 的参数存在问题,尤其是在使用正确的运算符(& 或 * 或 void)调用它时,但我几乎尝试了所有组合,但没有任何实际帮助。

#include <iostream>
#include <stdio.h>
#include <mpi.h>

// k = 3, N = 12, 1,2,3, 4,5,6, 7,8,9, 10,11,12

int main(int argc, char **argv) {
int N, size, myrank;
int k;
std::cin >> N;
std::cin >> k;
int *mass = new int[N];
int *recv = new int[k];

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);

if (myrank == 0) {
std::cout << "get k and n \n";

for (int i = 0; i < N; ++i) {
mass[i] = i;
std::cout << i << " written\n";
}
}

MPI_Scatter(mass, k, MPI_INT, recv, k, MPI_INT, 0, MPI_COMM_WORLD);

int sum = 0;
std::cout << "myrank" << myrank << '\n';
for (int i = 0; i < k; ++i) {
std::cout << recv[i] << '\n';
}

MPI_Finalize();

return 0;
}

当我启动这段代码时,它会打印:

N = 12
k = 3
get k and n
0 written
1 written
2 written
3 written
4 written
5 written
6 written
7 written
8 written
9 written
10 written
11 written
myrank0
0
1
2
myrank1
myrank3
myrank2
[1570583203.522390] [calc:32739:0] mpool.c:38 UCX WARN object 0x7fe1f08b2f60 was not returned to mpool mm_recv_desc
[1570583203.523214] [calc:32740:0] mpool.c:38 UCX WARN object 0x7f4643986f60 was not returned to mpool mm_recv_desc
[1570583203.524205] [calc:32741:0] mpool.c:38 UCX WARN object 0x7f22535d4f60 was not returned to mpool mm_recv_desc

最佳答案

MPI 通常会将 stdout 重定向到等级 0,因此 Nk 没有正确设置在其他等级上。

这是你的程序的工作版本

#include <iostream>
#include <cassert>
#include <stdio.h>
#include <mpi.h>

// k = 3, N = 12, 1,2,3, 4,5,6, 7,8,9, 10,11,12

int main(int argc, char **argv) {
int k, N, size, myrank;
int *mass;

MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);


if (myrank == 0) {
std::cout << "get k and n \n";
std::cin >> N;
std::cin >> k;
assert (N >= k*size);
mass = new int[N];
for (int i = 0; i < N; ++i) {
mass[i] = i;
std::cout << i << " written\n";
}
}
MPI_Bcast(&k, 1, MPI_INT, 0, MPI_COMM_WORLD);

int *recv = new int[k];

MPI_Scatter(mass, k, MPI_INT, recv, k, MPI_INT, 0, MPI_COMM_WORLD);

int sum = 0;
std::cout << "myrank" << myrank << '\n';
for (int i = 0; i < k; ++i) {
std::cout << recv[i] << '\n';
}

MPI_Finalize();

return 0;
}

关于c++ - 使用 MPI_Scatter 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58295722/

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