c - MPI:将整数写入和读取文件

标签 c multiprocessing mpi 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);
}

不工作..请帮助!

最佳答案

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

如果你在 linux 上

首先备份你的文件

cp file file2

然后

试试这个命令把二进制转换成ascii

hexdump -v -e '7/4 "%10d "' -e '"\n"' file2

另请注意:除非您将数百万行写入文件,否则您甚至可能不需要 mpi_file 写入/加载函数

关于c - MPI:将整数写入和读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11355412/

相关文章:

c++ - clCreateBuffer() 永远不会失败

c - Openssl 中 RSA 结构的大小和公钥的大小

Python:以固定间隔连续且可取消地重复执行

c - 如何从处理器中获取 MPI_Gatherv 列,其中每个进程可能发送不同数量的列

port - 将 MPI 设置为在一系列端口上运行

c - 左移 uint64_t 将最重要的双字清零

c - 指针和数据宽度不同时的指针间接

python - 多处理进行许多快速计算

python - 告诉多个生产者的消费者没有更多结果

python - 派生进程能否与 "main"MPI 通信器通信