MPI通信,大型二维数组 [英] MPI communicate large two dimensional arrays

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

问题描述

与我的<一个href=\"http://stackoverflow.com/questions/22541623/fastest-way-to-copy-several-2-dimensional-arrays-into-one-1-dimensional-array-i/22542543#22542543\">$p$pvious关于2-复制到一维数组的问题,我想知道是否有一个更好的方式做全业务,我尽量做到。所以,我想传达的MPI一对夫妇大的二维数组(这是动态分配的数组的数组,但是在编译时已知大小),从主到客户端,反之亦然。该数据是应该被散射向/从客户端列方向聚集

Related to my previous question concerning copying of 2- into 1-dimensional arrays, I wondered if there was an even better way to do the whole business, I try to achieve. So, I want to MPI communicated a couple of large two dimensional arrays (which are allocated dynamically as arrays of arrays, but with known size at compile time) from the master to the clients and vice versa. The data is supposed to be scattered to/gathered from the clients column-wise.

我在做什么,此刻是2维数组(这实际上是三维的那些子阵列)映射到静态分配的一维数组,发送这些1-暗淡。通过MPI阵列,然后在接收器再次重建的二维阵列。但是,我有一种感觉,应该有什么(多)比这更有效的...

What I am doing at the moment is to map the 2-dimensional arrays (which are in fact sub-arrays of 3-dimensional ones) into statically allocated 1-dimensional arrays, send these 1-dim. arrays via MPI and then rebuild the 2-dimensional arrays again at the receiver. However, I have the feeling that there should be something (much) more efficient than that...

非常感谢!

推荐答案

C99具有动态多维数组支持:使用它们可避免在所有复制数据。例如,下面的code确实与 GCC编译-std = C99

C99 has support for dynamic multidimensional arrays: using them you can avoid to copy your data at all. For example the following code does compile with gcc -std=c99:

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

int main(int argc, char **argv) {
  int m = atoi(argv[1]);
  int n = atoi(argv[2]);
  int p = atoi(argv[3]);

  // if defined in stack... actually I'm not sure it's defined in stack
  // in this way - shouldn't its size be known at compile time?
  // float a[m][n][p];
  // in heap:
  float (*a)[n][p] = malloc(m*n*p*sizeof(float));
  for (int i=0; i<m; ++i) {
    for (int j=0; j<n; ++j) {
      for (int k=0; k<p; ++k) {
        a[i][j][k] = 100.*i + 10.*j + k;
      }
    }
  }
  for (int i=0; i<m; ++i) {
    for (int j=0; j<n; ++j) {
      for (int k=0; k<p; ++k) {
        if (k>0) printf(",");
        printf("%7.2f", a[i][j][k]);
      }
      printf("\n");
    }
    printf("\n");
  }
  free(a);
}

(执行与 ./ a.out的2 3 4 例如 - 没有错误检查......耐心等待请)。

(execute with ./a.out 2 3 4 for example - no error checking... be patient please).

如果用C89,寻找优化code,当我想你应该牺牲最好的内存布局的语法,我会写同一code为:

If using C89, when looking for optimized code I think you should sacrifice syntax for the best memory layout, and I would write the same code as:

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

#define ARR(A,i,j,k) ((A).a[(i)*A.p*A.n + (j)*A.p + (k)]) 

struct Arr3d {
  float *a;
  int m;
  int n;
  int p;
};

int main(int argc, char **argv) {
  struct Arr3d a;
  int m,n,p;
  int i,j,k;

  m = a.m = atoi(argv[1]);
  n = a.n = atoi(argv[2]);
  p = a.p = atoi(argv[3]);
  a.a = malloc(m*n*p*sizeof(float));
  for (i=0; i<m; ++i) {
    for (j=0; j<n; ++j) {
      for (k=0; k<p; ++k) {
        ARR(a,i,j,k) = 100.*i + 10.*j + k;
      }
    }
  }
  for (i=0; i<m; ++i) {
    for (j=0; j<n; ++j) {
      for (k=0; k<p; ++k) {
        if (k>0) printf(",");
        printf("%7.2f", ARR(a,i,j,k));
      }
      printf("\n");
    }
    printf("\n");
  }
  free(a.a);
}

在两种方式的阵列的位置是在存储器中连续,并且可以与单个MPI通讯发送

In both ways the arrays locations are contiguous in memory and can be sent with a single MPI communication:

MPI_Send(&a[0][0][0], m*n*p, MPI_FLOAT, ...       (c99)
MPI_Send(&ARR(a,0,0,0), m*n*p, MPI_FLOAT, ...     (c89)

或者像你问,送第i子阵:

Or as you asked, to send the i-th subarray:

MPI_Send(&a[i][0][0], n*p, MPI_FLOAT, ...         (c99)
MPI_Send(&ARR(a,i,0,0), n*p, MPI_FLOAT, ...       (c89)

这篇关于MPI通信,大型二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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