Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matrix 具有MPI_类型结构的MPI_散射子矩阵_Matrix_Mpi_Submatrix - Fatal编程技术网

Matrix 具有MPI_类型结构的MPI_散射子矩阵

Matrix 具有MPI_类型结构的MPI_散射子矩阵,matrix,mpi,submatrix,Matrix,Mpi,Submatrix,我目前正在进行一个MPI程序,我正在尝试将带有scatterv的矩阵块发送到所有进程 过程描述 矩阵以数组的形式给出。 首先,我使用MPI_Type_vector生成一个数据类型,以从原始数组中创建必要的块。 第二,我创建了一个MPI_类型的结构,该结构应该包含多行块 #include <math.h> #include <mpi.h> #include <stdio.h> #include <stdlib.h> #define n 16 in

我目前正在进行一个MPI程序,我正在尝试将带有scatterv的矩阵块发送到所有进程

过程描述 矩阵以数组的形式给出。 首先,我使用MPI_Type_vector生成一个数据类型,以从原始数组中创建必要的块。 第二,我创建了一个MPI_类型的结构,该结构应该包含多行块

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

#define n 16

int main(int argc, char *argv[])
{
  MPI_Init(&argc, &argv);
  MPI_Comm comm = MPI_COMM_WORLD;
  int p,r;
  MPI_Comm_size(comm, &p);
  MPI_Comm_rank(comm, &r);
  int *arr;
  arr = NULL;
  if (r == 0){
    arr = (int *) malloc(n * n * sizeof(int));
    for (int i = 0; i < n * n; i++) arr[i] = i;
    for (int i = 0; i < n; i++){
      printf("\n");
      for (int j = 0; j < n; j++)
        printf("%4d", arr[i * n + j]);
    }
  }
  printf("\n");
  int ps = sqrt(p);
  int ns = n / ps;

  if (r == 0) {
    printf("ps: %d ns: %d\n", ps, ns);
  }
  /* create datatype */
  MPI_Datatype block;
  MPI_Type_vector(ns, ns, n, MPI_INT, &block);
  int blocks[ps];
  MPI_Aint displs[ps];
  for (int i = 0; i < ps; i++) { 
    blocks[i] = 1;
    displs[i] = i  * sizeof(int);
  }
  MPI_Datatype types[ps];
  //for (int i = 0; i < ps - 1; i++) types[i] = block;
  //types[ps - 1] = MPI_UB;
  types[0] = block;
  for (int i = 1; i < ps; i++) types[i] = MPI_UB; 
  //types[0] = block;
  //types[1] = MPI_UB;
  if (r == 0) {
    printf("displs:\n");
    for(int i = 0; i < ps; i++) printf("%3ld", displs[i]);
    printf("\n");
  }

  MPI_Datatype row;
  MPI_Type_struct(ps, blocks, displs, types, &row);
  MPI_Type_commit(&row);

  /* prepare scatter */
  int sdispl[p]; int sendcounts[p];
  for (int i = 0; i < p; i++) {
    sdispl[i] = (i % ps) + (i / ps) * (ns * ps);
    sendcounts[i] = 1;
  }
  if (r == 0) {
    printf("sdispl: \n");
    for (int i = 0; i < 4; i++) printf("%3d", sdispl[i]);
    printf("\n");
  }

  int rcv[ns * ns];
  MPI_Scatterv(arr, sendcounts, sdispl, row, rcv, ns * ns, MPI_INT, 0, comm);

  int result = 1;
  if (r == result) {
    printf("result for %d:\n", result);
    for (int i = 0; i < ns * ns; i++) {
      printf("%4d", rcv[i]);
      if ((i+1) % ns == 0) printf("\n");
    }
  }

  if (arr != NULL) free(arr);
  MPI_Finalize();
  return 0;
}
我使用以下命令运行代码:

mpirun -np 16 ./main

提前谢谢你的帮助

在祖兰的暗示下,我解决了我的问题

下面的代码基于对的优秀答案

并运行它

mpirun -np 4 ./main

在祖兰的暗示下,我解决了我的问题

下面的代码基于对的优秀答案

并运行它

mpirun -np 4 ./main

你应该检查这一点,并回答一个相关的问题。我不确定你到底想用你的结构类型实现什么,我认为你把事情复杂化了。尽量避免在代码中留下各种//注释掉的语句。谢谢!我会根据你的提示来解决我的问题。你应该在相关问题上对此进行检查。我不确定你到底想用你的结构类型实现什么,我认为你把事情复杂化了。尽量避免在代码中留下各种//注释掉的语句。谢谢!我会根据你的提示来解决我的问题。
mpicc -o main main.c
mpirun -np 4 ./main