C 使用MPI-IO读取多个文件

C 使用MPI-IO读取多个文件,c,file-io,matrix,io,mpi,C,File Io,Matrix,Io,Mpi,我正在尝试使用C中的MPI-IO读取多个文件。我遵循以下示例: 然而,我在矩阵中读取的是一个双精度矩阵,而不是一串字符。以下是执行情况: /* Simple MPI-IO program that demonstrate parallel reading from a file. Compile the program with 'mpicc -O2 readfile1.c -o readfile1' */ #include <stdlib.h> #include <stdi

我正在尝试使用C中的MPI-IO读取多个文件。我遵循以下示例:

然而,我在矩阵中读取的是一个双精度矩阵,而不是一串字符。以下是执行情况:

/*
Simple MPI-IO program that demonstrate parallel reading from a file.
Compile the program with 'mpicc -O2 readfile1.c -o readfile1'
*/

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

#define FILENAME "filename.dat"

double** ArrayAllocation() {
    int i;
    double** array2D;
    array2D= (double**) malloc(num_procs*sizeof(double*));
    for(i = 0; i < num_procs; i++) {
        twoDarray[i] = (double*) malloc(column_size*sizeof(double));
    }
    return array2D;
}

int main(int argc, char* argv[]) {
  int i, np, myid;
  int bufsize, nrchar;
  double *buf;          /* Buffer for reading */
  double **matrix = ArrayAllocation();
  MPI_Offset filesize;
  MPI_File myfile;    /* Shared file */ 
  MPI_Status status;  /* Status returned from read */

  /* Initialize MPI */
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  MPI_Comm_size(MPI_COMM_WORLD, &np);

  /* Open the files */
  MPI_File_open (MPI_COMM_WORLD, FILENAME, MPI_MODE_RDONLY,
         MPI_INFO_NULL, &myfile);

  /* Get the size of the file */
  MPI_File_get_size(myfile, &filesize);
  /* Calculate how many elements that is */
  filesize = filesize/sizeof(double);
  /* Calculate how many elements each processor gets */
  bufsize = filesize/np;
  /* Allocate the buffer to read to, one extra for terminating null char */
  buf = (double *) malloc((bufsize)*sizeof(double));
  /* Set the file view */
  MPI_File_set_view(myfile, myid*bufsize*sizeof(double), MPI_DOUBLE,
             MPI_DOUBLE,"native", MPI_INFO_NULL);
  /* Read from the file */
  MPI_File_read(myfile, buf, bufsize, MPI_DOUBLE, &status);
  /* Find out how many elemyidnts were read */
  MPI_Get_count(&status, MPI_DOUBLE, &nrchar);
  /* Set terminating null char in the string */
  //buf[nrchar] = (double)0;
  printf("Process %2d read %d characters: ", myid, nrchar);

  int j;
  for (j = 0; j <bufsize;j++){
    matrix[myid][j] = buf[j];
  }

  /* Close the file */
  MPI_File_close(&myfile);

  if (myid==0) {
    printf("Done\n");
  }

  MPI_Finalize();
  exit(0);
}
/*
演示从文件并行读取的简单MPI-IO程序。
使用“mpicc-O2 readfile1.c-o readfile1”编译程序
*/
#包括
#包括
#包括“mpi.h”
#定义文件名“FILENAME.dat”
双**阵列位置(){
int i;
双**阵列2d;
array2D=(双**)malloc(num_procs*sizeof(双*);
对于(i=0;i对于(j=0;j以上
ArrayAllocation
中的代码与主程序的逻辑不太匹配。在初始化MPI之前,矩阵被分配为指向双精度向量的指针数组,因此无法将行数设置为MPI进程数

在确定文件大小之前,
列的大小也不知道

在C语言中,按行存储矩阵是一种通用的约定。违反此约定可能会使您或代码的读者感到困惑

总之,为了让这个程序工作,你需要声明

 int num_procs, column_size;
作为定义
ArrayAllocation
之前的全局变量,并将对该函数的调用下移到计算bufsize的行下方:

 ...
 /* Calculate how many elements each processor gets */
 bufsize = filesize/np;

 num_procs = np;
 column_size = bufsize;
 double **matrix = ArrayAllocation();
 ...
通过以上修改,本示例应适用于支持MPI-IO的任何MPI实现。我已使用OpenMPI 1.2.8对其进行了测试

为了生成测试文件,您可以使用以下代码:

 FILE* f = fopen(FILENAME,"w");
 double x = 0;
 for(i=0;i<100;i++){
   fwrite(&x, 1,sizeof(double), f);
   x +=0.1;
 }
 fclose(f);
FILE*f=fopen(文件名,“w”);
双x=0;
对于(i=0;i