i am trying to create a ring of mpi processes where each MPI process will launch a pthread and the threads will perform the ring, i used pthread so i can use the MPI processes to do another task. It seems that i can't use MPI_send or MPI_Recv inside a pthread, i have no compilation error but i do have a run time error.
i compile using this command mpicc -lpthread threaded_ring.c
this is the runtime error
a.out:28372 terminated with signal 11 at PC=2aaaaaae312d SP=2aaab0771860. Backtrace:
/usr/lib64/libpsm_infinipath.so.1(psmi_mpool_get+0xd)[0x2aaaaaae312d]
a.out:28366 terminated with signal 11 at PC=333c00c110 SP=2aaab02d9698. Backtrace:
/lib64/libpthread.so.0(pthread_spin_lock+0x0)[0x333c00c110]
/usr/lib64/libpsm_infinipath.so.1(psmi_amsh_short_request+0x180)[0x2aaaaaad31b0]
/usr/lib64/libpsm_infinipath.so.1(+0xd9f6)[0x2aaaaaad49f6]
/usr/lib64/libpsm_infinipath.so.1(psm_mq_send+0x41)[0x2aaaaaaf5d51]
/usr/local/mpi/mvapich2/intel12/1.8.1/lib/libmpich.so.3(psm_send_pkt+0xb1)[0x2aaaaae0af21]
/usr/local/mpi/mvapich2/intel12/1.8.1/lib/libmpich.so.3(psm_istartmsgv+0x130)[0x2aaaaae0a010]
/usr/local/mpi/mvapich2/intel12/1.8.1/lib/libmpich.so.3(MPIDI_CH3_iStartMsgv+0x6)[0x2aaaaaddf1e6]
/usr/local/mpi/mvapich2/intel12/1.8.1/lib/libmpich.so.3(MPIDI_CH3_EagerContigSend+0x89)[0x2aaaaada6e39]
/usr/local/mpi/mvapich2/intel12/1.8.1/lib/libmpich.so.3(MPID_Send+0x116)[0x2aaaaade3136]
/usr/local/mpi/mvapich2/intel12/1.8.1/lib/libmpich.so.3(MPI_Send+0xf8)[0x2aaaaae2a408]
./a.out[0x4022ba]
/lib64/libpthread.so.0[0x333c0077f1]
/lib64/libc.so.6(clone+0x6d)[0x333bce570d]
a.out:28373 terminated with signal 11 at PC=333bf9d428 SP=2aaab0771838. Backtrace:
a.out:28370 terminated with signal 11 at PC=2aaaaaae312d SP=2aaab0771860. Backtrace:
here is my code
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
void *ring_func(void *p)
{
int token=1;
int world_rank;
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
if (world_rank==0){
MPI_Send(&token, 1, MPI_INT, (world_rank + 1) % world_size, 0,
MPI_COMM_WORLD);
}
if (world_rank != 0) {
MPI_Recv(&token, 1, MPI_INT, world_rank - 1, 0, MPI_COMM_WORLD,
MPI_STATUS_IGNORE);
printf("Process %d received token %d from process %d\n", world_rank, token,
world_rank - 1);
}
pthread_exit(NULL);
}
int main(int argc, char** argv) {
// Initialize the MPI threaded environment
int provided;
MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE , &provided);
if (provided < MPI_THREAD_MULTIPLE)
{
printf("Error: the MPI library doesn't provide the required thread level\n");
MPI_Abort(MPI_COMM_WORLD, 0);
}
pthread_t ring ;
pthread_create (&ring, NULL, ring_func, NULL) ;
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
provided
is equal toMPI_THREAD_MULTIPLE
after the call toMPI_Init_thread
. Also, you never join the thread and there is no guarantee whatsoever that the main thread won't succeed in going through the barrier and callingMPI_Finalize()
before the second thread has even started. – Hristo Ilievpthread_join(ring, NULL);
before the barrier. – Hristo Iliev