I am new to open MPI

I have a simple program below:

#include <iostream>
#include <pthread.h>
#include "mpi.h"
using namespace std;



void* backID(void* arg)
{
 int myid;
 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
 cout << myid << " create background ID" << endl;
 int v;
 MPI_Status status;
 int m;
 int x, y;
 int count = 0;

 while(true)
 {
  MPI_Recv(&m, 1, MPI_INT, MPI_ANY_SOURCE, 222, MPI_COMM_WORLD, &status);
  cout << myid << " recv from " << status.MPI_SOURCE << " m = " << m << " with 
tag 222" << endl;
  MPI_Send(&m, 1, MPI_INT, status.MPI_SOURCE, 333, MPI_COMM_WORLD);
  cout << myid << " replies " << status.MPI_SOURCE << " m = " << m << endl;
  count++;
  if(count == 50)
  {
   pthread_exit(NULL);
  }
 };
}


void* backRecv(void* arg)
{
 int myid;
 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
 cout << myid << " create background message recv" << endl;
 int x, y;
 MPI_Status status;
 int m;
 int count = 0;


 while(true)
 {
  MPI_Recv(&m, 1, MPI_INT, MPI_ANY_SOURCE, 333, MPI_COMM_WORLD, &status);
  cout << myid << " recv from " << status.MPI_SOURCE << " m = " << m << " with 
tag 333" << endl;
  count++;
  if(count == 50)
  {
   pthread_exit(NULL);
  } 
 };
}

int main(int argc, char **argv) 
{
 int myid = 0;
 int nprocs = 0;
 pthread_t pt1 = 0;
    pthread_t pt2 = 0;;
 int pret1 = 0;
 int pret2 = 0;
 int i = 0, j = 0, t = 0;
 MPI_Status status;
 MPI_Init(&argc,&argv);
   MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
   MPI_Comm_rank(MPI_COMM_WORLD,&myid); 
  pret1 = pthread_create(&pt1, NULL, backRecv, NULL);
    if(pret1 != 0)
    {
        cout << myid << "backRecv Thread Create Failed." << endl;
        exit(1);
    }
 pret2 = pthread_create(&pt2, NULL, backID, NULL);
 if(pret2 != 0)
 {
        cout << myid << "backID Thread Create Failed." << endl;
        exit(1);
 }
 sleep(5);
 for(i=0; i<50; ++i)
 {
  t = (myid + 1) * i;
  MPI_Send(&myid, 1, MPI_INT, (myid+1)%nprocs, 222, MPI_COMM_WORLD);
   cout << myid << " sends to "<< (myid+1)%nprocs << " " << myid << endl;
 }

 pthread_join(pt1, NULL);
 pthread_join(pt2, NULL);
 MPI_Finalize();
}



I create two sub-threads in each main thread and want to see if they can work 
as a background receiver. But when I run it I got segmentation fault. I do not 
know why. If I only create one thread, it works fine. The error message I got 
is:

#0  0x00002b5d3c80a940 in mca_btl_sm_alloc () from 
/act/openmpi/gnu/lib/openmpi/mca_btl_sm.so
(gdb) bt
#0  0x00002b5d3c80a940 in mca_btl_sm_alloc () from 
/act/openmpi/gnu/lib/openmpi/mca_btl_sm.so
#1  0x00002b5d3bbb91ca in mca_pml_ob1_send_request_start_copy () from 
/act/openmpi/gnu/lib/openmpi/mca_pml_ob1.so
#2  0x00002b5d3bbadc86 in mca_pml_ob1_send () from 
/act/openmpi/gnu/lib/openmpi/mca_pml_ob1.so
#3  0x00002b5d3945571d in PMPI_Send () from /act/openmpi/gnu/lib/libmpi.so.0
#4  0x0000000000409139 in main (argc=1, argv=0x7fff718e4db8) at ptest.cpp:125
This message is from gdb. 

Any help will be appreciated. 

Thanks in advance.

vincent

_________________________________________________________________
约会说不清地方?来试试微软地图最新msn互动功能!
http://ditu.live.com/?form=TL&swm=1

Reply via email to