简体   繁体   English

使用MPI和C ++发送静态内存2D阵列

[英]Sending Static Memory 2D Array using MPI and C++

I am having some trouble sending arrays using MPI. 我在使用MPI发送阵列时遇到了一些麻烦。 I currently have an array called positions of static memory. 我目前有一个称为静态内存位置的数组。 I gradually fill the array in a recursive manner. 我以递归方式逐渐填充数组。 Eventually, I send the array to slave processors to complete the filling. 最终,我将阵列发送到从属处理器以完成填充。

const int SIZE = 8;
const int DEPTH = 3 ;

void recursion(int column, int positions[SIZE], int rank, int numtasks) {
  //other code
  positions[DEPTH] = row;
  MPI_Send(&positions, SIZE, MPI_INT, r, 1, MPI::COMM_WORLD) ;
  //other code }

int main() {
  int positions[SIZE];
  MPI::Init();
  rank = MPI::COMM_WORLD.Get_rank();
  numtasks = MPI::COMM_WORLD.Get_size();
  char name[MPI::MAX_PROCESSOR_NAME];
  memset(name,0,MPI::MAX_PROCESSOR_NAME);
  MPI::Get_processor_name(name,len);
  memset(name+len,0,MPI::MAX_PROCESSOR_NAME-len);
  if(rank == MASTER) {
    recursion(0, positions, rank, numtasks); }
  if(rank != MASTER) {
  MPI_Recv(&positions, SIZE, MPI_INT, 0, MPI::ANY_TAG, MPI::COMM_WORLD, &stat);

Using the cout function, I managed to discover that the positions array before sending is : 使用cout函数,我设法在发送之前发现positions数组是:

0 2 4 0 1055625616 32621 4252832 0 

The numbers after the second 0 are array spots that I have not filled yet. 第二个0之后的数字是我尚未填充的数组点。 But the positions array after recv is: 但是recv之后的positions数组是:

-196000928 32767 -196001336 3 -1769860728 54 1047301632 32621 

Even when I change the numbers on the array on the Master processor, the slave always receives the same array. 即使我在主处理器上更改阵列上的数字,从服务器也始终接收相同的阵列。

Can anyone tell me where I am going wrong, and how to fix these arrays? 谁能告诉我我要去哪里错了,以及如何修复这些阵列?

The error is in the line MPI_Send(&positions, SIZE, MPI_INT, r, 1, MPI::COMM_WORLD); 错误在MPI_Send(&positions, SIZE, MPI_INT, r, 1, MPI::COMM_WORLD); . You should not use &positions but positions instead. 您不应该使用&positions而是使用positions positions type is int * and so &positions type is int ** and you really are just sending garbage in this case instead of real array. positions类型是int * ,因此&positions类型是int ** ,在这种情况下,您实际上只是发送垃圾,而不是实际数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM