pthreads - Calling MPI functions from multiple threads -
i want implement following thing using mpi , pthreads facing error:
each processor have 2 threads. each processor's 1 thread sending data other processors , other thread receiving data other processors. when implementing it, giving segmentation fault error messages "current bytes -40, total bytes 0, remote id 5".
just testing purpose, when using 1 thread per processor , either sending or receiving data, errors not occur.
i found info "in general, there may problems if multiple threads make mpi calls. program may fail or behave unexpectedly. if mpi calls must made within thread, should made 1 thread." in following link: https://computing.llnl.gov/tutorials/pthreads/
i want use 2 threads per processor 1 thread use mpi_send function send data , other thread receive mpi_recv function receive data without using locking mechanism. has idea how implement or how use multiple threads call mpi functions without using mutex or locking mechanism?
here code:
int rank, size, msg_num; // thread function sending messages void *send_func_for_thread(void *arg) { int send, procnum, x; send = rank; for(x=0; x < msg_num; x++) { procnum = rand()%size; if(procnum != rank) mpi_send(&send, 1, mpi_int, procnum, 0, mpi_comm_world); } // sending special message other processors tag = 128 signal finishing of sending message (x = 0; x < size; x++) { if(x != rank) mpi_send(&send, 1, mpi_int, x, 128, mpi_comm_world); } pthread_exit((void *)null); } // thread function receiving messages void *recv_func_for_thread(void *arg) { mpi_status status; int recv, counter = 0; while(counter != size - 1) { mpi_recv(&recv, 1, mpi_int, mpi_any_source, mpi_any_tag, mpi_comm_world, &status); if(status.mpi_tag == 128) counter++; } pthread_exit((void *)null); } int main(int argc, char **argv) { void *stat; pthread_attr_t attr; pthread_t thread[2]; mpi_init(&argc, &argv); mpi_comm_rank(mpi_comm_world, &rank); // rank -> rank of processor mpi_comm_size(mpi_comm_world, &size); // size -> total number of processors srand((unsigned)time(null)); msg_num = atoi(argv[1]); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, pthread_create_joinable); // thread 0 sending messages pthread_create(&thread[0], &attr, send_func_for_thread, (void *)0); // thread 1 receiving messages pthread_create(&thread[1], &attr, recv_func_for_thread, (void *)1); pthread_attr_destroy(&attr); pthread_join(thread[0], &stat); pthread_join(thread[1], &stat); cout << "finished : proc " << rank << "\n"; mpi_finalize(); pthread_exit((void *)null); return 0; } compile: ======== module load mvapich2/gcc; mpicxx -lpthread -o demo demo.cpp run: ==== mpiexec -comm mpich2-pmi demo 10000000 ran program 3 processors , got segmentation fault.
(since haven't provided example, following speculation.)
you must initialize mpi using mpi_init_thread() instead of mpi_init(). if understand explanation correctly, "required" argument must have value mpi_thread_multiple. if mpi_init_thread() returns lower level thread support in "provided" argument, means mpi implementation doesn't support mpi_thread_multiple; in case must else. see http://www.mpi-forum.org/docs/mpi-20-html/node165.htm .
Comments
Post a Comment