本文介绍了与MPI线程同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的线程与MPI。此程序产生一个线程用于秩= 0,并发送和接收消息(封锁)并从线程。线程的数目是命令行输入。这code然而块上的发送/接收,就如何解决这一问题的任何想法?此外,线程级安全我收到的MPI_THREAD_SINGLE,不是我要求MPI_THREAD_MULTIPLE。犯规_SINGLE究竟意味着只能有一个是每个进程执行的线程?那么为什么有多个线程输出显示收到消息两个线程?

谢谢!

  typedef结构{
       INT ID;
} struct_t;无效的getMsg *(无效* ARG)
{
    INT排名;
    字符myStr的[10];
    MPI_Request请求;
    MPI_Status状态;
    struct_t * FD =(struct_t *)ARG;
    MPI_Comm_rank(MPI_COMM_WORLD,&安培;等级);
    的printf(等级%D在线程%d个等待我的邮件\\ n,军衔,FD-和SEQ ID);
    而(1){
            MPI_RECV(myStr中,10 MPI_CHAR,MPI_ANY_SOURCE,MPI_ANY_TAG,MPI_COMM_WORLD,&安培;状态);
            如果(status.MPI_TAG == 0){
                    的printf(上排名%d个线程%d个收到%d个\\ n NULL,FD-GT&ID,等级,status.MPI_SOURCE);
                    返回;
            }
            的printf(上排名%d个线程%d个接到排名%d个\\ N%的,FD-GT&ID,等级,myStr中,status.MPI_SOURCE);
    }
    的printf(现在我给字符串排名1 \\ n);
    MPI_SEND(myStr中,10,MPI_CHAR,1,2,MPI_COMM_WORLD);    返回(NULL);
}无效spawn_thread(INT N)
{
    INT等级,I;
    的pthread_t *线程;
    pthread_attr_t pthread_custom_attr;
    struct_t * FD;
    线程=(*的pthread_t)的malloc(N * sizeof的(线程));
    FD =(struct_t *)malloc的(的sizeof(struct_t)* N);    MPI_Comm_rank(MPI_COMM_WORLD,&安培;等级);
    对于(i = 0; I< N;我++)
    {
            FD [I] .ID = I;
     //输出(我的排名是%D和我创建的线程#%d个\\ N,职级,I);
            在pthread_create(安培;线程[I],NULL的getMsg,(无效*)(FD + I));
    }    免费(FD);
}无效的主要(INT ARGC,字符** argv的)
{
    INT N,I,提供,声称;
    INT等级,大小,犯错误;    INT为主;    MPI_Init_thread(安培; ARGC,&安培; argv的,MPI_THREAD_MULTIPLE,&安培;提供);
    MPI_Comm_rank(MPI_COMM_WORLD,&安培;等级);
    MPI_Comm_size(MPI_COMM_WORLD,&安培;大小);    字符myStr的[10];
    MPI_Status状态;    如果(排名== 0安培;&安培;提供< MPI_THREAD_MULTIPLE){
            的printf(你得到%d个级别线程安全,而不是%d \\ n,提供MPI_THREAD_MULTIPLE);
    }
    如果(的argc!= 2)
    {
            的printf(用法:%S N \\ n,其中n是没有线程的\\ n,argv的[0]);
            出口(1);
    }    N =的atoi(ARGV [1]);
    如果((N< 1)||(N> MAX_THREAD))
    {
            的printf(以下简称无螺纹应1到%d之间\\ n,MAX_THREAD);
            MPI_Abort(MPI_COMM_WORLD,-1);
 }    MPI_Request请求;
    如果(排名== 0){
            spawn_thread(N);
    }    的printf(等级%d个打招呼\\ N级);
    MPI_SEND(HELLO !!!,10,MPI_CHAR,0,1,MPI_COMM_WORLD);    的printf(等级%d被发送空\\ n级);
    如果(排名== 0)
            MPI_SEND(NULL,0,MPI_CHAR,0,0,MPI_COMM_WORLD);    MPI_RECV(myStr中,10,MPI_CHAR,0,2,MPI_COMM_WORLD,&放大器;状态);
    的printf(我是排名%D和我收到%S \\ n,军衔,myStr的);    MPI_Finalize();
}


解决方案

所提供的MPI线程级的支持是不是你问,因为您的MPI库编译时不与它安装的。所提供的支持值是你的库可以给你,而不是必要你的要求。所以这个值并不总是比所要求的值等于或greather。
例如,对于的openmpi,你必须使用选项--enable-MPI线程多配置

您可以用这个命令来检查:

 壳$ ompi_info | grep按线程
      线程支持:POSIX(MPI:是的,进度:无)

如果它说MPI:不,你没有机会拥有多线程支持

I am trying to use threads with MPI. This program spawns a thread for rank = 0 and sends and receives messages (blocking) to and from the threads. The number of threads is command line input. This code however blocks on the sends/receives, any ideas on how to fix this? Also, the thread level safety I receive is MPI_THREAD_SINGLE, not what I ask for MPI_THREAD_MULTIPLE. Doesnt _SINGLE really mean there can be only one thread that is executed per process? Why then does the output with more than one thread shows both the threads received the message?

Thanks!

typedef struct {
       int id;
} struct_t;

void *getmsg(void *arg)
{
    int rank;
    char mystr[10];
    MPI_Request request;
    MPI_Status status;
    struct_t *fd=(struct_t *)arg;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    printf("Rank %d is waiting in thread %d for my message\n", rank, fd->id);
    while(1){
            MPI_Recv(mystr, 10, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
            if(status.MPI_TAG == 0){
                    printf("Thread %d on rank %d received NULL from %d\n", fd->id, rank, status.MPI_SOURCE);
                    return;
            }
            printf("Thread %d on rank %d received %s from rank %d\n", fd->id, rank, mystr, status.MPI_SOURCE);
    }
    printf("I am now sending the string to rank 1\n");
    MPI_Send(mystr, 10, MPI_CHAR, 1, 2, MPI_COMM_WORLD);

    return (NULL);
}

void spawn_thread(int n)
{
    int rank, i;
    pthread_t *threads;
    pthread_attr_t pthread_custom_attr;
    struct_t *fd;
    threads=(pthread_t *)malloc(n*sizeof(threads));
    fd=(struct_t *)malloc(sizeof(struct_t)*n);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    for (i=0; i<n; i++)
    {
            fd[i].id=i;
     //       printf("My rank is %d and I created thread #%d\n", rank, i);
            pthread_create(&threads[i], NULL, getmsg, (void *)(fd+i));
    }

    free(fd);
}

void main(int argc, char ** argv)
{
    int n,i, provided, claimed;
    int rank, size, errs;

    int main;

    MPI_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    char mystr[10];
    MPI_Status status;

    if(rank==0 && provided<MPI_THREAD_MULTIPLE){
            printf("You get %d level thread safety, not %d\n",provided, MPI_THREAD_MULTIPLE);
    }
    if (argc != 2)
    {
            printf ("Usage: %s n\n  where n is no. of threads\n",argv[0]);
            exit(1);
    }

    n=atoi(argv[1]);
    if ((n < 1) || (n > MAX_THREAD))
    {
            printf ("The no of thread should between 1 and %d.\n",MAX_THREAD);
            MPI_Abort(MPI_COMM_WORLD,-1);
 }

    MPI_Request request;
    if(rank == 0){
            spawn_thread(n);
    }

    printf("Rank %d says hello\n",rank);
    MPI_Send("HELLO!!!", 10, MPI_CHAR, 0, 1, MPI_COMM_WORLD);

    printf("Rank %d is sending Null\n",rank);
    if(rank==0)
            MPI_Send(NULL,0,MPI_CHAR,0,0,MPI_COMM_WORLD);

    MPI_Recv(mystr, 10, MPI_CHAR, 0, 2, MPI_COMM_WORLD,&status);
    printf("I am rank %d and I received %s \n",rank, mystr);

    MPI_Finalize();
}
解决方案

The provided MPI thread level support is not the one you ask because your MPI library wasn't compiled and install with it. The provided support value is what your library can gives you and not necessary what you are asking. So this value is not always equal or greather than the required value.For example, for OpenMPI, you have to configure it with the option --enable-mpi-thread-multiple

You can check it with this command:

shell$ ompi_info | grep -i thread
      Thread support: posix (mpi: yes, progress: no)

If it says mpi: no, you have no chance to have multi thread support

这篇关于与MPI线程同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:44