Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Pthreads 在C中的pthread内部使用MPI_发送_Pthreads_Mpi - Fatal编程技术网

Pthreads 在C中的pthread内部使用MPI_发送

Pthreads 在C中的pthread内部使用MPI_发送,pthreads,mpi,Pthreads,Mpi,我正在尝试创建一个mpi进程环,其中每个mpi进程将启动一个pthread,线程将执行该环,我使用pthread,以便可以使用mpi进程执行另一个任务。似乎我不能在pthread中使用MPI_send或MPI_Recv,我没有编译错误,但确实有运行时错误 我使用这个命令编译 mpicc-LPU螺纹环.c 这是运行时错误 a.out:28372 terminated with signal 11 at PC=2aaaaaae312d SP=2aaab0771860. Backtrace: /us

我正在尝试创建一个mpi进程环,其中每个mpi进程将启动一个pthread,线程将执行该环,我使用pthread,以便可以使用mpi进程执行另一个任务。似乎我不能在pthread中使用MPI_send或MPI_Recv,我没有编译错误,但确实有运行时错误

我使用这个命令编译 mpicc-LPU螺纹环.c

这是运行时错误

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:
这是我的密码

#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();
}
多亏了Hristo lliev,我才能够解决这个问题,问题是主线程在我的pthread之前查找,但是当我添加pthread_join时,主线程在调用MPI_Finalize之前等待pthread加入。这是新代码

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>

void *ring_func(void *p)
{
  int token;
  // Receive from the lower process and send to the higher process. Take care
  // of the special case when you are the first process to prevent deadlock.

  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_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);
  } else {
// Set the token's value if you are process
 token = -1;
  }
MPI_Send(&token, 1, MPI_INT, (world_rank + 1) % world_size, 0,
           MPI_COMM_WORLD);

 if (world_rank == 0) {
//    sleep(20);
    MPI_Recv(&token, 1, MPI_INT, world_size - 1, 0, MPI_COMM_WORLD,
             MPI_STATUS_IGNORE);
    printf("Process %d received token %d from process %d\n", world_rank, token,
           world_size - 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) ;
  pthread_join(ring,NULL);
  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Finalize();
}

已添加MPI_Init_thread&argc、&argv、MPI_thread\u MULTIPLE和提供的可能的重复项;但我还是有同样的错误!!在调用MPI_Init_THREAD之后,检查提供的值是否等于MPI_THREAD_倍数。此外,您从未加入线程,也不能保证主线程在第二个线程开始之前不会成功通过屏障并调用MPI_Finalize。嗨,利耶夫,谢谢您的帮助,我已经实际添加了提供的,并确保它等于MPI_THREAD_倍数,但它似乎并没有解决问题,我认为您在第二部分是对的,有时主线程会调用MPI_Finlize,但我不知道如何解决这个问题。您能想出一些方法来阻止主线程在pthread完成之前终止吗;在障碍物之前。