0

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 
21
  • int mass = new int[N]; your compiler let you do that, without, a least, a warning? Commented Oct 9, 2019 at 1:00
  • yep. I don't know how to do this in other way, actually... Commented Oct 9, 2019 at 1:02
  • Sorry, I'm not so familiar with C/C++, but I found this implementation of dynamic arrays somewhere, and it works. Commented Oct 9, 2019 at 1:03
  • Ohh, I get it... There should be * before mass, of course Commented Oct 9, 2019 at 1:05
  • 1
    modern implementations of std::vector` have a data method you can call to get a pointer to the vector's data buffer. Previously you could &myvec[0]; which is technically undefined behaviour, but I've never seen avector implementation 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. Commented Oct 9, 2019 at 1:23

1 Answer 1

1

MPI typically redirects stdout to rank 0, so N and k are not correctly set on the other ranks.

Here is a working version of your program

#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; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! Now everything has become very-very clear
I hope I also someday can know programming as well as you and live and work in Japan. Thank you again!
all you needed here was an extra pair of eyes. Japan is great, and we can be singing "sweet home Alabama" all summer long :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.