如何将long和/或无符号整数传递到MPI参数? [英] How can I pass long and/or unsigned integers to MPI arguments?

查看:287
本文介绍了如何将long和/或无符号整数传递到MPI参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个非常大的数组,我希望发送或接收与MPI(v1)。为了索引这个数组,我使用一个无符号长整数。

Assume that I have a very large array which I wish to send or receive with MPI (v1). In order to index this array, I use an unsigned long integer.

现在,所有的MPI函数调用我看到使用int类型的计数参数,如在此示例中:

Now, all MPI function calls I have seen use int types for their "count" arguments, such as in this example:

MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status)

但是如果在我的实现中需要能够发送/接收数组大于int可以容纳的最大数目?编译器,当我尝试将无符号整数馈送到count参数时,自然地,给我一个无效转换错误。我想了做一个演员,但是我担心这会收缩我的变量,所以我有点失去了做什么。

But what if, in my implementation, I require the ability to send/receive an array larger than the maximum number an int can hold? The compiler, naturally, gives me an "invalid conversion" error, when I try to feed an unsigned integer to the "count" argument. I thought about doing a cast, but then I am worried that this will shrink my variable, so I am kind of at a loss what to do.

推荐答案

执行转换不是解决方案,因为它将简单地截断长计数。这里有两个障碍要克服 - 一个简单的和一个困难的。

Doing a cast is not the solution as it will simply truncate the long count. There are two obstacles to overcome here - an easy one and a hard one.

容易的障碍是 int 类型为计数参数。您可以通过创建一个较小大小的连续类型,然后将数据作为新数据类型的倍数发送。示例代码如下:

The easy obstacle is the int type for the count argument. You can get past it simply by creating a contiguous type of smaller size and then send the data as multiples of the new datatype. An example code follows:

// Data to send
int data[1000];

// Create a contiguous datatype of 100 ints
MPI_Datatype dt100;
MPI_Type_contiguous(100, MPI_INT, &dt100);
MPI_Type_commit(&dt100);

// Send the data as 10 elements of the new type
MPI_Send(data, 10, dt100, ...);

由于 MPI_Type_contiguous 的count参数 int ,使用这个技术你可以发送到(2 31 -1) 2 = - 2 32 + 1)元素。如果这还不够,您可以从 dt100 数据类型创建新的连续数据类型,例如:

Since the count argument of MPI_Type_contiguous is int, with this technique you can send up to (231-1)2 = (262 - 232 + 1) elements. If this is not enough, you can create a new contiguous datatype from the dt100 datatype, e.g.:

// Create a contiguous datatype of 100 dt100's (effectively 100x100 elements)
MPI_Datatype dt10000;
MPI_Type_contiguous(100, dt100, &dt10000);
MPI_Type_commit(&dt10000);

如果您的原始数据大小不是新数据类型大小的倍数,结构数据类型,其第一个元素是连续数据类型的 int(data_size / cont_type_length)元素的数组,其第二个元素是 datasize%cont_type_length 原始数据类型的元素。示例如下:

If your original data size is not a multiple of the size of the new datatype, you could create a structure datatype whose first element is an array of int(data_size / cont_type_length) elements of the contiguous datatype and whose second element is an array of datasize % cont_type_length elements of the primitive datatype. Example follows:

// Data to send
int data[260];

// Create a structure type
MPI_Datatype dt260;

int blklens[2];
MPI_Datatype oldtypes[2];
MPI_Aint offsets[2];

blklens[0] = 2; // That's int(260 / 100)
offsets[0] = 0;
oldtypes[0] = dt100;

blklens[1] = 60; // That's 260 % 100
offsets[1] = blklens[0] * 100L * sizeof(int); // Offsets are in BYTES!
oldtypes[1] = MPI_INT;

MPI_Type_create_struct(2, blklens, offsets, oldtypes, &dt260);
MPI_Type_commit(&dt260);

// Send the data
MPI_Send(data, 1, dt260, ...);

MPI_Aint 是足够大的整数大于 int 的偏移量可以在LP64系统上表示。注意,接收器必须构造相同的数据类型,并且在 MPI_Recv 调用中类似地使用它。接收任意非整数数量的连续数据类型有点问题。

MPI_Aint is large enough integer that can hold offsets larger than what int can represent on LP64 systems. Note that the receiver must construct the same datatype and use it similarly in the MPI_Recv call. Receiving an arbitrary non-integer amount of the contiguous datatype is a bit problematic though.

这是一个容易的障碍。不那么容易的,当你的MPI实现不使用内部长计数。在这种情况下,MPI通常会崩溃或只发送部分数据或一些奇怪的可能发生。这样的MPI实现可能会崩溃,即使没有构造一个特殊的数据类型,只需发送 INT_MAX 类型 MPI_INT 消息大小将是(2 31 - 1)* 4 = 2 33 - 4.如果是这样,您唯一的转义是手动拆分消息和发送/它在一个循环。

That's the easy obstacle. The not so easy one comes when your MPI implementation does not use internally long counts. In that case MPI would usually crash or only send part of the data or something weird might happen. Such an MPI implementation could be crashed even without constructing a special datatype by simply sending INT_MAX elements of type MPI_INT as the total message size would be (231 - 1) * 4 = 233 - 4. If that is the case, your only escape is manually splitting the message and sending/receiving it in a loop.

这篇关于如何将long和/或无符号整数传递到MPI参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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