MPI数据类型为2天阵 [英] MPI datatype for 2 d array

查看:182
本文介绍了MPI数据类型为2天阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要整型数组(基本上是一个2维数组)的数组传递给来自root.I所有处理器我在C程序中使用MPI。如何申报MPI数据类型为2天array.and如何发送邮件(我应该用广播或散射)

I need to pass an array of integer arrays (basically a 2 d array )to all the processors from root.I am using MPI in C programs. How to declare MPI datatype for 2 d array.and how to send the message (should i use broadcast or scatter)

推荐答案

您需要使用<一个href="http://www.netlib.org/utk/papers/mpi-book/node96.html#SECTION00550000000000000000">Broadcast,因为你要发送相同邮件的副本每一道工序。 <一href="http://www.netlib.org/utk/papers/mpi-book/node102.html#SECTION00570000000000000000">Scatter打破了信息和销售流程之间的块。

You'll need to use Broadcast, because you want to send a copy of the same message to every process. Scatter breaks up a message and distributes the chunks between processes.

至于如何发送数据:<一href="http://www.netlib.org/utk/papers/mpi-book/node78.html#SECTION00435000000000000000">HIndexed数据类型是你。

As for how to send the data: the HIndexed datatype is for you.

假设你的二维数组的定义是这样的:

Suppose your 2d array is defined like this:

int N;            // number of arrays (first dimension)
int sizes[N];     // number of elements in each array (second dimensions)
int* arrays[N];   // pointers to the start of each array

首先,你必须计算出每个数组的起始地址的位移,相对于数据类型的起始地址,可以是第一个阵列,使事情的起始地址,方便的:

First you have to calculate the displacement of each array's starting address, relative to the starting address of the datatype, which can be the starting address of the first array to make things convenient:

MPI_Aint base;
MPI_Address(arrays[0], &base);
MPI_Aint* displacements = new int[N];
for (int i=0; i<N; ++i)
{
    MPI_Address(arrays[i], &displacements[i]);
    displacements[i] -= base;
}

那么对于你的类型的定义是:

Then the definition for your type would be:

MPI_Datatype newType;
MPI_Type_hindexed(N, sizes, displacements, MPI_INTEGER, &newType);
MPI_Type_commit(&newType);

这个定义将创建一个包含所有阵列装一个接一个数据类型。一旦做到这一点,你只要把你的数据,因为这类型的单个对象:

This definition will create a datatype that contains all your arrays packed one after the other. Once this is done, you just send your data as a single object of this type:

MPI_Bcast(arrays, 1, newType, root, comm);   // 'root' and 'comm' is whatever you need

不过,您还没有完成。接收过程需要知道你发送数组的大小:如果知识是不可用在编译的时候,你就必须先发送一个独立的消息与数据(简单的整数数组)。如果 N 尺寸阵列的定义类似于上面在接收过程中,有足够的空间分配,以填补阵列,那么所有的进程需要做的接收被定义了相同的数据类型(完全相同的code作为发送器),然后接收发送者的消息的单个实例这种类型的:

However, you're not done yet. The receiving processes will need to know the sizes of the arrays you're sending: if that knowledge isn't available at compile time, you'll have to send a separate message with that data first (simple array of ints). If N, sizes and arrays are defined similar as above on the receiving processes, with enough space allocated to fill the arrays, then all the receiving processes need to do is define the same datatype (exact same code as the sender), and then receive the sender's message as a single instance of that type:

MPI_Bcast(arrays, 1, newType, root, comm);    // 'root' and 'comm' must have the same value as in the sender's code

,瞧!所有进程现在有你的数组的一个副本。

And voilá! All processes now have a copy of your array.

当然,事情就会变得容易得多,如果你的二维数组的第二个维度是固定到某一值 M 。在这种情况下,最简单的解决方案是简单地将其存储在一个单一的 INT [N * M] 数组:C ++会保证它的所有连续的内存,这样你就可以播放它没有定义自定义数据类型,如:

Of course, things get a lot easier if the 2nd dimension of your 2d array is fixed to some value M. In that case, the easiest solution is to simply store it in a single int[N*M] array: C++ will guarantee that it's all contiguous memory, so you can broadcast it without defining a custom datatype, like this:

MPI_Bcast(arrays, N*M, MPI_INTEGER, root, comm);

请注意:你的可能的侥幸使用索引类型而不是HIndexed。所不同的是在索引中,位移阵列中给出的元素数,而在HIndexed它的字节数(H代表异构)。如果你要使用索引,然后在位移给出的值将必须由分的sizeof(int)的。不过,我不知道,如果堆在任意位置定义整型数组,保证排队在C ++整数的限制,在任何情况下,HIndexed版本有(略)​​小于code和生产同样的结果。

Note: you might get away with using the Indexed type instead of HIndexed. The difference is that in Indexed, the displacements array is given in number of elements, while in HIndexed it's the number of bytes (H stands for Heterogenous). If you were to use Indexed, then the values given in displacements would have to be divided by sizeof(int). However, I'm not sure if integer arrays defined in arbitrary positions on the heap are guaranteed to "line up" to integer limits in C++, and in any case, the HIndexed version has (marginally) less code and produces the same result.

这篇关于MPI数据类型为2天阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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