如何用C语言来调用同等级的MPI? [英] How to call same rank in MPI using C language?

查看:178
本文介绍了如何用C语言来调用同等级的MPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习编程MPI和写了下面的程序。它增加了阵列的一整行,并输出总和。在等级0(或进程0),它会调用所有的奴隶队伍做计算。我想这样做,只用两个其他的从职级/进程。每当我尝试调用同一职级的两倍所示code波纹管,我的code将只是挂在中间,也不会执行。如果我不叫同级别的两倍,code将正常工作

I am trying to learn MPI programing and have written the following program. It adds an entire row of array and outputs the sum. At rank 0 (or process 0), it will call all its slave ranks to do the calculation. I want to do this using only two other slave ranks/process. Whenever I try to invoke same rank twice as shown in the code bellow, my code would just hang in the middle and wouldn't execute. If I don't call the same rank twice, code would work correctly

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>

int main (int argc, char *argv[])
{
        MPI_Init(&argc, &argv);
        int world_rank;
        MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
        int world_size;
        MPI_Comm_size(MPI_COMM_WORLD, &world_size);
        int tag2 = 1;       
        int arr[30] = {0};

        MPI_Request request;
        MPI_Status status;

        printf ("\n--Current Rank: %d\n", world_rank);
        int index;
        int source = 0;
        int dest;
        if (world_rank == 0)
        {
            int i;

            printf("* Rank 0 excecuting\n");
            index = 0;
            dest = 1;
            for ( i = 0; i < 30; i++ )
            {
                arr[ i ] = i + 1; 
            }           
            MPI_Send(&arr[0], 30, MPI_INT, dest, tag2, MPI_COMM_WORLD); 

            index = 0;
            dest = 2;
            for ( i = 0; i < 30; i++ )
            {
                arr[ i ] = 0; 
            }
            MPI_Send(&arr[0], 30, MPI_INT, dest, tag2, MPI_COMM_WORLD);         

            index = 0;
            dest = 2; //Problem happens here when I try to call the same destination(or rank 2) twice
            //If I change this dest value to 3 and run using: mpirun -np 4 test, this code will work correctly
            for ( i = 0; i < 30; i++ )
            {
                arr[ i ] = 1; 
            }
            MPI_Send(&arr[0], 30, MPI_INT, dest, tag2, MPI_COMM_WORLD);
        }
        else 
        {
            int sum = 0;
            int i;
            MPI_Irecv(&arr[0], 30, MPI_INT, source, tag2, MPI_COMM_WORLD, &request);
            MPI_Wait (&request, &status);
            for(i = 0; i<30; i++)
            {   
                sum = arr[i]+sum;
            }
            printf("\nSum is: %d at rank: %d\n", sum, world_rank);
        }       

        MPI_Finalize();
}

当使用结果:-np的mpirun 3测试

--Current Rank: 2

Sum is: 0 at rank: 2

--Current Rank: 0
* Rank 0 excecuting

--Current Rank: 1

Sum is: 524800 at rank: 1
                  //Program hangs here and wouldn't show sum of 30 

请让我知道我怎么能叫同级别的两倍。例如,如果我只有另外两个从属进程,我可以打电话。
请用一个例子如果可能的话显示

Please let me know how can I call the same rank twice. For example, if I only had two other slave process that I can call. Please show with an example if possible

推荐答案

在MPI,每个进程执行相同的code和,为你做,你主要是通过的if / else检查等级区分不同的过程声明。秩0做3主进程发送:一个发送处理1,则需要两个发送到处理2.从处理每个只有一个接收做的,这意味着1级接收其第一条消息和等级2接收其第一条消息。当你调用第三 MPI_SEND 工艺0,没有,也不会是任何从等待该点后收到消息,作为奴隶完成执行其else块。该计划被封锁作为主等待发送的最后消息。

In MPI, each process executes the same code and, as you're doing, you differentiate the different processes primarily through checking rank in if/else statements. The master process with rank 0 is doing 3 sends: a send to process 1, then two sends to process 2. The slave processes each do only one receive, which means that rank 1 receives its first message and rank 2 receives its first message. When you call the third MPI_Send on process 0, there is not and will not be any slave waiting to receive the message after that point, as the slaves have finished executing their else block. The program gets blocked as the master waits to send the final message.

为了解决这个问题,你有2个执行两个接收,通过添加仅这一过程中循环或重复该过程只(因此,使用以确保排名的奴隶的如果(world_rank == 2)检查)code块

In order to fix that, you have to make sure the slave of rank 2 performs two receives, either by adding a loop for that process only or by repeating for that process only (so, using an if(world_rank == 2) check) the block of code

        sum = 0; //resetting sum
        MPI_Irecv(&arr[0], 1024, MPI_INT, source, tag2, MPI_COMM_WORLD, &request);
        MPI_Wait (&request, &status);
        for(i = 0; i<1024; i++)
        {   
            sum = arr[i]+sum;
        }
        printf("\nSum is: %d at rank: %d\n", sum, world_rank); 

这篇关于如何用C语言来调用同等级的MPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆