function - 初级产业部。在函数中运行 mpi

标签 function mpi

我想使用 main 中的 mpi 运行一些函数,但我不知道它应该如何运行。看起来像:

#define MAXSIZE 100
int main (int argc, char **argv) {
  int i;
  float matrixA[MAXSIZE][MAXSIZE], matrixB[MAXSIZE][MAXSIZE], matrixC[MAXSIZE][MAXSIZE];
  for(i=0;i<10;i++){
    multiply(matrixA, matrixB, matrixC);
  }
}
void multiply(float matrixA[MAXSIZE][MAXSIZE], float matrixB[MAXSIZE][MAXSIZE], float matrixC[MAXSIZE][MAXSIZE]) {
  int rank; //process rank
  int size; //number of processes
  MPI_Init(&argc, &argv); //initialize MPI operations
  MPI_Comm_rank(MPI_COMM_WORLD, &rank); //get the rank
  MPI_Comm_size(MPI_COMM_WORLD, &size); //get number of processes

  ...someoperation...
  MPI_Finalize();
}

我知道如何在不使用其他函数的情况下运行基本的 MPI,但我需要这种构造。

最佳答案

在一个应用程序实例中,MPI 可以被初始化最多一次。因此,您提供的代码结构将不起作用。

你的程序的正确结构如下:

#define MAXSIZE 100
int main (int argc, char **argv) {
  int i;
  float matrixA[MAXSIZE][MAXSIZE], matrixB[MAXSIZE][MAXSIZE], matrixC[MAXSIZE][MAXSIZE];
  int rank; //process rank
  int size; //number of processes
  MPI_Init(&argc, &argv); //initialize MPI operations
  MPI_Comm_rank(MPI_COMM_WORLD, &rank); //get the rank
  MPI_Comm_size(MPI_COMM_WORLD, &size); //get number of processes
  for(i=0;i<10;i++){
    multiply(matrixA, matrixB, matrixC);
  }
  MPI_Finalize();
}
void multiply(float matrixA[MAXSIZE][MAXSIZE], float matrixB[MAXSIZE][MAXSIZE], float matrixC[MAXSIZE][MAXSIZE]) {    
  ...someoperation...
}

关于function - 初级产业部。在函数中运行 mpi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6635070/

相关文章:

python - 函数调用错误值

MPI sendbuf 和 recvbuf 可以是一回事吗?

c - MPI : getting number of nodes (not processes) in a communicator

mpi - 从 PVM 迁移到 MPI

jQuery — 通过 'addClass' 添加的类上的单击功能没有响应

javascript - 如何复用函数属性?

返回记录数的 Postgresql 函数

c - 从 "char"到 "char *"的转换无效

c - 解决处理笛卡尔网格的 MPI 程序中的死锁问题

io - 使用 MPI_File_write_all 的 MPI-IO 死锁