Openmpi program hangs

时间:2018-03-22 23:38:50

标签: parallel-processing mpi

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

1 个答案:

答案 0 :(得分:1)

排名0挂起,因为solve_it() MPI_Recv()MPI_Send()函数尚未调用main

话虽如此,MPI的方法是:

  • MPI_Bcast(&m, ...)
  • solve_it(...)
  • MPI_Reduce(&count, ...)