我试图让多个进程使用 MPI 并行 io 同时将整数缓冲区写入文件。为了实现这个目标,我搜索了各种网站:
我试图学习他们的教导。为了测试它们,我在 C++ 中创建了以下简单代码:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define BUFSIZE 100
using namespace std;
int main(int argc, char *argv[])
{
int myrank, buf[BUFSIZE], rcode;
MPI_File thefile;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
for (int i=0; i<BUFSIZE; i++)
buf[i] = myrank * BUFSIZE + i;
rcode = MPI_File_open(MPI_COMM_WORLD, "testfile", MPI_MODE_CREATE | MPI_MODE_RDWR,
MPI_INFO_NULL, &thefile);
if(rcode != MPI_SUCCESS){
cerr << "Did not open file" << endl;
return EXIT_FAILURE;
}
rcode = MPI_File_set_view(thefile, myrank * (MPI_Offset)BUFSIZE * sizeof(int), MPI_INT, MPI_INT,
"native", MPI_INFO_NULL);
if(rcode != MPI_SUCCESS){
cerr << "Problem setting process view" << endl;
return EXIT_FAILURE;
}
MPI_File_write(thefile, buf, BUFSIZE, MPI_INT, MPI_STATUS_IGNORE);
if(rcode != MPI_SUCCESS){
cerr << "Problem writting file" << endl;
return EXIT_FAILURE;
}
MPI_File_close(&thefile);
MPI_Finalize();
return EXIT_SUCCESS;
}
然而,当我尝试用 Kate 读取文件时,我得到了随机垃圾:一堆正方形、矩形、!$"%&!!/ 符号,根本没有整数。
我做错了什么?