MPI:写入和读取整数到文件

Ani*_*udh 2 c mpi multiprocessing openmpi

刚开始使用Openmpi.尝试编写和读取文件的内容..编写代码:

写入文件的字符无法识别,主要是垃圾.

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

#define BUFSIZE 10
#define FIRSTCHAR 1
#define FILENAME "file1.dat"

int main(int argc, char* argv[]) {
  int i, np, me;
  int buf[BUFSIZE];     /* The buffer to write */
  MPI_File myfile;       /* Shared file */

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

  /* Initialize buf with characters. Process 0 uses 'a', process 1 'b', etc. */
  for (i=0; i<BUFSIZE; i++) {
    buf[i] = FIRSTCHAR+(me);
  }

  /* Open the file */
  MPI_File_open (MPI_COMM_WORLD, FILENAME, MPI_MODE_CREATE | MPI_MODE_WRONLY,
         MPI_INFO_NULL, &myfile);
  /* Set the file view */
  MPI_File_set_view(myfile, me*BUFSIZE*sizeof(int), MPI_INT, MPI_INT, 
            "native", MPI_INFO_NULL);
  /* Write buf to the file */
  MPI_File_write(myfile, buf, BUFSIZE*sizeof(int), MPI_INT, MPI_STATUS_IGNORE);
  /* Close the file */
  MPI_File_close(&myfile);
  MPI_Finalize();
  exit(0);
}
Run Code Online (Sandbox Code Playgroud)

不工作..请帮忙!

pyC*_*hon 6

写的文件不是垃圾,它们只是二进制文件

如果你的Linux

首先备份你的文件

cp file file2
Run Code Online (Sandbox Code Playgroud)

然后

尝试此命令将二进制转换为ascii

hexdump -v -e '7/4 "%10d "' -e '"\n"' file2
Run Code Online (Sandbox Code Playgroud)

另外还有一个注意事项:除非你写入数百万行文件,否则你甚至可能不需要mpi_file写入/加载功能