0

I get this in gdb when running my openmpi program. The program just hangs on recv and I'm not really understanding why. I've tried searching everywhere but I can't seem to find anything that is similiar.

Attaching to process 29704
[New LWP 29709]
where[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007f107c333827 in sched_yield () at ../sysdeps/unix/syscall-template.S:84
84  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) where
#0  0x00007f107c333827 in sched_yield ()
    at ../sysdeps/unix/syscall-template.S:84
#1  0x00007f106f07d955 in mca_pml_ob1_recv ()
   from /usr/lib/openmpi/lib/openmpi/mca_pml_ob1.so
#2  0x00007f107cc1061c in PMPI_Recv () from /usr/lib/libmpi.so.12
#3  0x0000000000408740 in solve_it (k=4, n=5) at MPI_pythagorean.cc:24
#4  0x000000000040893b in main (argc=2, argv=0x7ffec018c088)
    at MPI_pythagorean.cc:61

I'm compiling it with mpic++ -g MPI_pythagorean.cc and running it with mpiexec -np 5 ./a.out 500

Here is the code

#include <iostream>
#include <cstdlib>
#include <mpi.h>
#include <stdio.h>
using namespace std;

int solve_it(int k, int n) {
  int m;

  MPI_Recv(&m,1,MPI_INT,0,0,MPI_COMM_WORLD,NULL);
  int count=0;
  for (int x=1+k;x<=m;x+=n) {
    for (int y=x+1;y<=m;y++) {
      for (int z=y+1;z<=m;z++) {

    long long x1 = x;
    long long y1 = y;
    long long z1 = z;
    if (x1*x1 + y1*y1 == z1*z1) {
      count++;
    }
      }
    }
  }
  MPI_Send(&count,1,MPI_INT,0,0,MPI_COMM_WORLD);
  return 0;
}


int main(int argc, char *argv[]) {
  int m;
  int k;
  int n;
  if (argc !=2) {
    cout << "Needs at least one argument " << endl;
    exit(-1);
  } else {
    m =atoi(argv[1]);
    cout << m << endl;
  }
  cout << argc << endl;
  cout << "Hello1" << endl; 
  MPI_Init(&argc,&argv);
  cout << "Hello" << endl;
  MPI_Comm_size(MPI_COMM_WORLD,&n);
  MPI_Comm_rank(MPI_COMM_WORLD,&k);
  int res = solve_it(k,n);
  if (k==0) {
    // Send m to everyone
    for (int i=0;i<n;i++) {
      MPI_Send(&m,1,MPI_INT,i,0,MPI_COMM_WORLD);
    }
    int sum=0;
    int res=0;
    // Get the answer from everyone
    for (int i=0;i<n;i++) {
      MPI_Recv(&res,1,MPI_INT,i,0,MPI_COMM_WORLD,NULL);
      sum+=res;
    }
    cout << sum << endl;
  }
  MPI_Finalize();
}

Any help is apprecaited

6
  • 1
    Why is namespace std bad? Commented Mar 22, 2018 at 23:41
  • What do you mean? Commented Mar 22, 2018 at 23:59
  • Did you read it? Commented Mar 23, 2018 at 0:14
  • Yeah, I don't see in anyway how that could possibly be the issue... Commented Mar 23, 2018 at 0:36
  • But it is a pointer for the future. Also, SO people are more likely to answer your question if they don't think you are new to the language. Commented Mar 23, 2018 at 0:39

1 Answer 1

1

Rank 0 hangs because solve_it() MPI_Recv() but MPI_Send() has not been yet invoked by the main function.

That being said, the MPI'ish way of doing this is :

  • MPI_Bcast(&m, ...)
  • solve_it(...)
  • MPI_Reduce(&count, ...)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.