如何发送一个二维数组中的MPI与变化,每个处理器 [英] How to send a 2D array in MPI with variation for each processor

查看:507
本文介绍了如何发送一个二维数组中的MPI与变化,每个处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想取一个随机生成的阵列上根0,以随机方式各不相同的,并且每个变化发送到另一处理器。这里是我的code迄今:

I'm trying to take a randomly generated array on root 0, vary it slightly and randomly, and send each variation to another processor. Here is my code so far:

#include "stdio.h"
#include "stdlib.h"
#include "mpi.h"
#include "math.h"

int main(int argc, char **argv) {

  int N = 32;
  int dim = 3;
  float a = 10.0;
  int size, rank, i, j, k, q;
  float **C;
  float rijx, rijy, rijz, rij, Vij, E=0;
  float stepsize = 0.05;

  double Start_time, End_time, Elapse_time;
  MPI_Status status;

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

  C = (float **)malloc(N * sizeof(float*)); // 32 particles

  for (i = 0; i < N; i++) {
    C[i]=(float *)malloc(dim*sizeof(float)); // x, y, z
  }

  MPI_Barrier(MPI_COMM_WORLD);

  if(rank == 0) {
    Start_time = MPI_Wtime();
  }

  if (rank == 0) {
  for(i = 0; i < N; i++) {
    for(j = 0; j < dim; j++) {
      C[i][j] = (float)rand()/(float)(RAND_MAX/a);
      //printf("%f\n",C[i][j]);
    }
  }
  }

// this is to generate some slight variations in the array

  float** randomsteps(float **matrix) {
for(i = 0; i < N; i = i+(rand()%(32/size))) {
   for (j = 0; j < dim; j++) {
      if(i%2 == 0) {
        C[i][j] = C[i][j]+stepsize;
          if(C[i][j] > 10) {
            C[i][j] = C[i][j] - 10;
          }
      } else {
        C[i][j] = C[i][j]-stepsize;
        if(C[i][j] < 0) {
          C[i][j] = C[i][j] + 10;
        }
      }
   }
}
return C;
  }


// and here I try to send the array
  if(rank == 0) {
   for(i=0; i<size; i++) {
    C = randomsteps(C);

    MPI_Send(&C, N*3, MPI_FLOAT, i, 10+i, MPI_COMM_WORLD);
   }
  }

  if(rank != 0) {
    for(i=0; i<size; i++) {
      MPI_Recv(&C, N*3, MPI_FLOAT, 0, 10+i, MPI_COMM_WORLD, &status);
    }
  }

MPI_Barrier(MPI_COMM_WORLD);

  MPI_Finalize();

  return 0;
}

与code的一个明显的问题是,产生的随机数的方法是有点幼稚(它赋予了相同的值每次运行程序的时间)。这件事情后,我可以工作。

An obvious problem with the code is that the way the random numbers are generated is somewhat naive (it gives the same values every time I run the program). That's something I can work on later.

有关,现在,我只是想知道 - 什么是错的,我发送和接收阵列的方法是什么?我有很多的麻烦缠绕它是如何最好我的头格式化数据发送时使用MPI接收。我怎么会去固定code的这一部分?

For right now, I'm just wondering - what is wrong with the way I'm sending and receiving the array? I'm having a lot of trouble wrapping my head around how it is best to format data when sending and receiving using MPI. How would I go about fixing this part of the code?

在此先感谢您的帮助!

推荐答案

这里的问题是,所有的MPI调用期望内存是连续的。你的内存是只给行内的连续的,和你指的是一个二维数组确实是一个指针数组。指针是不可移植的,所以要发送或广播指针数组到另一个进程是没有意义的,和MPI本身不支持深拷贝,所以这种做法是行不通的。

The problem here is that all MPI calls expect that memory is contiguous. Your memory is only contiguous within given row, and what you are referring to as a 2D array is really an array of pointers. Pointers are not portable, so trying to send or broadcast an array of pointers to another process makes no sense, and MPI itself doesn't support deep copy, so this approach won't work.

不过,如果你改变你的数组分配是这样的:

However, if you change your array allocation to something like this:

  float** C;
  float* C_buff;
  C = (float**)malloc(N * sizeof(float*)); // 32 particles
  C_buff = (float*)malloc(N * dim * sizeof(float)); // buffer for particles

  float* p = &C_buff[0];
  for (i = 0; i < N; i++) {
    C[i]=p;
    p+= dim*sizeof(float));
  }

[免责声明:写在浏览器中,完全未经测试,用在自己的风险]

[disclaimer: written in browser, totally untested, use at own risk]

C_buff 重新presents连续内存为你的二维数组,而 C 包含行指针在 C_buff 连续分配中的内存,那么你可以使用现有的code用于初始化,但随后做这样的事情:

so that C_buff represents the contiguous memory for your 2D array, and C contains row pointers to memory within the C_buff contiguous allocation, then you can use your existing code for initialisation, but then do something like this:

MPI_Send(&C_buff[0][0], N*DIM, MPI_FLOAT, i, 10+i, MPI_COMM_WORLD);

IE浏览器。使用 C_buff 为MPI调用,它应该工作。

ie. use C_buff for the MPI calls, and it should work.

这篇关于如何发送一个二维数组中的MPI与变化,每个处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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