展平C ++中的3D数组以与MPI一起使用 [英] Flattening a 3D array in c++ for use with MPI

查看:85
本文介绍了展平C ++中的3D数组以与MPI一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以使用MPI展平3D阵列的通用格式吗?我想我只需使用(i + xlength * j + xlength * ylength * k)就能获得一维数组,但是然后我很难使用引用数组特定单元格的方程.

Can anyone help with the general format for flattening a 3D array using MPI? I think I can get the array 1 dimensional just by using (i+xlength*j+xlength*ylength*k), but then I have trouble using equations that reference particular cells of the array.

我尝试根据我拥有的处理器数量将代码分块,但是当我需要另一个处理器具有的值时,我就很难过.有没有办法使用幻像单元格或指针变戏法使它更容易(更有效)?

I tried chunking the code into chunks based on how many processors I had, but then when I needed a value that another processor had, I had a hard time. Is there a way to make this easier (and more efficient) using ghost cells or pointer juggling?

推荐答案

您至少有两个选择.比较简单的方法是声明一个预处理器宏,该宏隐藏了索引计算的复杂性,例如:

You have two options at least. The simpler one is to declare a preprocessor macro that hides the complexity of the index calculation, e.g.:

#define ARR(A,i,j,k) A[(i)*ylength*zlength+(j)*zlength+(k)]

ARR(myarray,i,j,k) = ARR(myarray,i+1,j,k) + ARR(myarray,i,j+1,k) + ...

这很笨拙,因为该宏仅适用于固定前导尺寸的数组,例如不管x ylength x zlength.

This is clumsy since the macro will only work with arrays of fixed leading dimensions, e.g. whatever x ylength x zlength.

更好的方法是使用所谓的涂料向量.涂料向量基本上是大数组的索引.您分配一个大小为xlength * ylength * zlength的大平面块来保存实际数据,然后创建索引向量(在3D情况下实际上是一棵树).在您的情况下,索引有两个级别:

Much better way to do it is to use so-called dope vectors. Dope vectors are basically indices into the big array. You allocate one big flat chunk of size xlength * ylength * zlength to hold the actual data and then create an index vector (actually a tree in the 3D case). In your case the index has two levels:

  • 顶层,由指向
  • xlength指针组成
  • 第二级,由xlength个指针数组组成,每个数组包含指向内存中zlength个元素块的开头的ylength个指针.
  • top level, consisting of xlength pointers to the
  • second level, consisting of xlength arrays of pointers, each containing ylength pointers to the beginning of a block of zlength elements in memory.

让我们调用顶级指针数组A.然后,A[i]是指向描述第i个数据平板的指针数组的指针. A[i][j]是第i个指针数组的第j个元素,它指向data[i][j][0](如果data是3D数组).涂料矢量的构建与此类似:

Let's call the top level pointer array A. Then A[i] is a pointer to a pointer array that describes the i-th slab of data. A[i][j] is the j-th element of the i-th pointer array, which points to data[i][j][0] (if data was a 3D array). Construction of the dope vector works similar to this:

double *data = new double[xlength*ylength*zlength];
double ***A;

A = new double**[xlength];
for (int i = 0; i < xlength; i++)
{
   A[i] = new double*[ylength];
   for (int j = 0; j < ylength; j++)
      A[i][j] = data + i*ylength*zlength + j*zlength;
}

在某些特殊情况下,涂料向量与普通阵列一样易于使用.例如,A[i][j][k]将使您可以访问data的所需元素.涂料向量的一个警告是,顶层由指向其他指针表的指针组成,而不是指向数据本身的指针,因此A不能用作&A[0][0][0]的快捷方式,也不能将A[i]用作&A[i][0][0]的快捷方式>.仍然A[i][j]等效于&A[i][j][0].另一个警告是,这种形式的数组索引比普通的3D数组索引要慢,因为它涉及指针追踪.

Dope vectors are as easy to use as normal arrays with some special considerations. For example, A[i][j][k] will give you access to the desired element of data. One caveat of dope vectors is that the top level consist of pointers to other pointer tables and not of pointers to the data itself, hence A cannot be used as shortcut for &A[0][0][0], nor A[i] used as shortcut for &A[i][0][0]. Still A[i][j] is equivalent to &A[i][j][0]. Another caveat is that this form of array indexing is slower than normal 3D array indexing since it involves pointer chasing.

某些人倾向于为数据和涂料向量分配单个存储块.他们只是将索引放在分配的块的开头,然后实际的数据就会在分配的块之后.这种方法的优点是,处理数组就像删除整个内存块一样简单,而处理使用上一节中的代码创建的涂料矢量时,需要多次调用free运算符.

Some people tend to allocate a single storage block for both data and dope vectors. They simply place the index at the beginning of the allocated block and the actual data goes after that. The advantage of this method is that disposing the array is as simple as deleting the whole memory block, while disposing dope vectors, created with the code from the previous section, requires multiple invocations of the free operator.

这篇关于展平C ++中的3D数组以与MPI一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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