MPI - 发送一个数组段 [英] MPI - Sending segments of an array

查看:1127
本文介绍了MPI - 发送一个数组段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有双打的数组。我想送,说每5双,到接收过程。所以基本上,我需要发送特定双打与它们之间的进展的方法。是否有一个函数来做到这一点,除了双打存入发送缓冲区?它会更好,使我自己的派生类型?

So I have an array of doubles. I would like to send, say every 5th double, to the receiving process. So essentially, I need a way of sending specific doubles with strides between them. Is there a function to do this, apart from storing the doubles into a send buffer? Would it be better to make my own derived type?

推荐答案

您一定要创建一个MPI数据类型;它给MPI库中的一个机会,以避免额外副本从阵列编组,并且它pretty直截了当用做的,在这种情况下,<一href=\"http://www.mcs.anl.gov/research/projects/mpi/www/www3/MPI_Type_vector.html\">MPI_Type_vector():

You should definitely create an MPI data type; it gives the MPI library an opportunity to avoid the extra copy for marshaling from the array, and it's pretty straightforward to do using, in this case, MPI_Type_vector():

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

int main(int argc, char** argv)
{
    int size, rank;
    const int bigsize=50;
    const int stride = 5;
    const int count = (bigsize + stride - 1)/stride;

    const int sender = 0;
    const int receiver = 1;
    const int mytag = 1;

    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);

    if (size < 2) {
        fprintf(stderr,"%s: Require at least two processors.\n", argv[0]);
        MPI_Finalize();
        exit(-1);
    }


    if(rank == sender)
    {
        double bigarray[bigsize];
        for (int i=0; i<bigsize; i++)
            bigarray[i] = 0.;

        for (int i=0; i<bigsize; i+=stride)
            bigarray[i] = i/stride;

        printf("[%d]: ", rank);
        for (int i=0; i<bigsize; i++)
            printf("%lf ", bigarray[i]);
        printf("\n");

        MPI_Datatype everyfifth;

        MPI_Type_vector( count, 1, stride, MPI_DOUBLE, &everyfifth);
        MPI_Type_commit(&everyfifth);

        MPI_Send(bigarray, 1, everyfifth, receiver, mytag, MPI_COMM_WORLD);

        MPI_Type_free(&everyfifth);
    }
    else if( rank == receiver )
    {
        double littlearray[count];

        MPI_Status status;

        MPI_Recv(littlearray, count, MPI_DOUBLE, sender, mytag,
                    MPI_COMM_WORLD, &status);

        printf("[%d]: ", rank);
        for (int i=0; i<count; i++)
            printf("%lf ", littlearray[i]);
        printf("\n");
    }

    MPI_Finalize();

    return 0;
}

编译和运行提供了

Compiling and running gives

$ mpicc -o vector vector.c -std=c99
$ mpirun -np 2 ./vector
[0]: 0.000000 0.000000 0.000000 0.000000 0.000000 1.000000 0.000000 0.000000 0.000000 0.000000 2.000000 0.000000 0.000000 0.000000 0.000000 3.000000 0.000000 0.000000 0.000000 0.000000 4.000000 0.000000 0.000000 0.000000 0.000000 5.000000 0.000000 0.000000 0.000000 0.000000 6.000000 0.000000 0.000000 0.000000 0.000000 7.000000 0.000000 0.000000 0.000000 0.000000 8.000000 0.000000 0.000000 0.000000 0.000000 9.000000 0.000000 0.000000 0.000000 0.000000 
[1]: 0.000000 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 

这篇关于MPI - 发送一个数组段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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