I have problem with MPI_Scatter. Dont know hot to use it and my current program crashes with seg fault when I launch.
I guess that the problem in parameters of MPI_Scatter, particularly in calling it with right operator (& or * or void), but I've tried almost every combination and nothing actually helped.
#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; } When I launch this code, it prints this:
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
int mass = new int[N];your compiler let you do that, without, a least, a warning?datamethod you can call to get a pointer to thevector's data buffer. Previously you could&myvec[0];which is technically undefined behaviour, but I've never seen avectorimplementation where it doesn't work (and can't imagine why anyone would write one). In your case,MPI_Scatter(mass.data(), ...);should do the deed.