2011-08-23 40 views
1

我是MPI的初学者,我想以下面的方式使用MPI的线程。某些进程(甚至是特克斯)产生线程并且阻塞等待接收消息。每个proc产生的线程数是一个命令行参数。所以如果有4个特效,进程0和2会产生一个线程。现在,我希望进程0和2都将消息发送到所有线程。例如,进程0向自己发送消息并向进程2发送消息,进程2将消息发送给进程0和自己。 这是我的代码看起来像,它显然不会做任何想要的。它只是等待收到消息。我哪里错了?MPI和线程

谢谢!

typedef struct { 
    int id; 
    } struct_t; 

void *hello(void *arg) 
{ 
    int rank; 
    char mystr[10]; 
    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); 

    if(rank%2 ==0){ 
      MPI_Recv(mystr, 10, MPI_CHAR, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status); 
      printf("Thread %d on rank %d received %s\n", fd->id, rank, mystr); 
    } 

    return (NULL); 
} 

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

    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &size); 

    /* Start up thread */ 
    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], &pthread_custom_attr, hello, (void *)(fd+i)); 
    } 

    /* Synchronize the completion of each thread. */ 
    for (i=0; i<n; i++) 
    { 
      pthread_join(threads[i],NULL); 
    } 
    free(fd); 
} 
void main(int argc, char ** argv) 
{ 
    int n,i, provided, claimed; 
    int rank, size, errs; 

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

    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); 
    } 

    if(rank%2 == 0) 
      spawn_thread(n); 

    if(rank%2 == 0){ 

        printf("My rank is %d and I am sending Hello!\n", rank); 
        MPI_Send("HELLOOO", 10, MPI_CHAR, rank, 0, MPI_COMM_WORLD); 
      } 

    MPI_Finalize(); 
} 

回答

1

我不完全明白你正在尝试什么来实现的,但请并非所有的甚至排名进程的线程将阻塞接收,所以没有人会运行发送代码。你的奇数排名进程的线程只是立即开始和结束,因为他们不会做任何事情。

也许以下,如果:

if(rank%2 == 0){ 
     printf("My rank is %d and I am sending Hello!\n", rank); 
     MPI_Send("HELLOOO", 10, MPI_CHAR, rank, 0, MPI_COMM_WORLD); 
    } 

应该是这样的:

if(rank%2 != 0) 

这样,你的排名奇数流程至少会发送命令?

或者,您需要将“join”代码移到spawn_thread函数之外,并在调用send之后执行连接。

希望这会有所帮助。

+0

谢谢crisbia,但我想要偶数级别发送消息。所以它需要(排名%2 == 0)。我试图在其线程上发送和接收相同的进程消息,我不知道这是否可能。 – cnovice

+0

我明白了。这是可能的,但你必须按照我答案的最后部分的建议。基本上你需要产生线程,然后不要加入,因为他们将被阻止在接收。在进程的主线程(调用'spawn_thread'的线程,执行send(就像你已经做的那样),然后在线程上执行Join。也许你可以让spawn_threads使用'fd'和'threads'作为全局变量,这样你可以从主函数访问它们并加入线程并释放结构 – crisbia

+0

感谢crisbia,我完全删除了连接,并修改了一些代码,我只是想玩弄线程和mpi,以便更好地理解这些概念。新代码似乎阻塞,我不知道为什么。这是它:[link] http://stackoverflow.com/questions/7191907/thread-synchronization-with-mpi [/ link] – cnovice