刚开始使用 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/