使用mpi将矩阵写入单个txt文件
writing a matrix into a single txt file with mpi
我有一个巨大的矩阵,我把它分成一些子矩阵,并在上面进行一些计算。在这些计算之后,我必须把这个矩阵写进一个文件中进行后期处理。是否可以将结果写入单个文本文件?我如何才能做到这一点?例如,我们有一个在y方向上划分的nxny矩阵(每个进程都有一个nx秩矩阵),我们想将nx*ny矩阵写入单个文本文件。
因此,将大量数据写成文本不是一个好主意。它真的,真的,很慢,它生成了不必要的大文件,处理起来很痛苦。大量的数据应该写成二进制,只有人类的摘要数据写成文本。让计算机处理的东西对计算机来说很容易,只有你真正要坐下来阅读的东西对你来说很容易处理(例如,文本)。
无论是以文本还是二进制形式编写,都可以使用MPI-IO将输出协调到文件,以生成一个大文件。我们在这里有一个关于这个主题的小教程(使用MPI-IO、HDF5和NetCDF)。对于MPI-IO,诀窍是定义一个类型(这里是子数组),根据文件的全局布局来描述数据的本地布局,然后将其作为"视图"写入文件。每个文件只能看到自己的视图,MPI-IO库会协调输出,因此只要视图不重叠,所有内容都会显示为一个大文件。
如果我们用二进制写出来,我们只需要将MPI_Write指向我们的数据,然后就可以完成它;由于我们使用的是文本,我们必须将数据转换为字符串。我们按照通常的方式定义数组,只是它不是MPI_FLOAT,而是一种新的类型,即每个数字charspernum
个字符。
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>
float **alloc2d(int n, int m) {
float *data = malloc(n*m*sizeof(float));
float **array = malloc(n*sizeof(float *));
for (int i=0; i<n; i++)
array[i] = &(data[i*m]);
return array;
}
int main(int argc, char **argv) {
int ierr, rank, size;
MPI_Offset offset;
MPI_File file;
MPI_Status status;
MPI_Datatype num_as_string;
MPI_Datatype localarray;
const int nrows=10;
const int ncols=10;
float **data;
char *const fmt="%8.3f ";
char *const endfmt="%8.3fn";
int startrow, endrow, locnrows;
const int charspernum=9;
ierr = MPI_Init(&argc, &argv);
ierr|= MPI_Comm_size(MPI_COMM_WORLD, &size);
ierr|= MPI_Comm_rank(MPI_COMM_WORLD, &rank);
locnrows = nrows/size;
startrow = rank * locnrows;
endrow = startrow + locnrows - 1;
if (rank == size-1) {
endrow = nrows - 1;
locnrows = endrow - startrow + 1;
}
/* allocate local data */
data = alloc2d(locnrows, ncols);
/* fill local data */
for (int i=0; i<locnrows; i++)
for (int j=0; j<ncols; j++)
data[i][j] = rank;
/* each number is represented by charspernum chars */
MPI_Type_contiguous(charspernum, MPI_CHAR, &num_as_string);
MPI_Type_commit(&num_as_string);
/* convert our data into txt */
char *data_as_txt = malloc(locnrows*ncols*charspernum*sizeof(char));
int count = 0;
for (int i=0; i<locnrows; i++) {
for (int j=0; j<ncols-1; j++) {
sprintf(&data_as_txt[count*charspernum], fmt, data[i][j]);
count++;
}
sprintf(&data_as_txt[count*charspernum], endfmt, data[i][ncols-1]);
count++;
}
printf("%d: %sn", rank, data_as_txt);
/* create a type describing our piece of the array */
int globalsizes[2] = {nrows, ncols};
int localsizes [2] = {locnrows, ncols};
int starts[2] = {startrow, 0};
int order = MPI_ORDER_C;
MPI_Type_create_subarray(2, globalsizes, localsizes, starts, order, num_as_string, &localarray);
MPI_Type_commit(&localarray);
/* open the file, and set the view */
MPI_File_open(MPI_COMM_WORLD, "all-data.txt",
MPI_MODE_CREATE|MPI_MODE_WRONLY,
MPI_INFO_NULL, &file);
MPI_File_set_view(file, 0, MPI_CHAR, localarray,
"native", MPI_INFO_NULL);
MPI_File_write_all(file, data_as_txt, locnrows*ncols, num_as_string, &status);
MPI_File_close(&file);
MPI_Type_free(&localarray);
MPI_Type_free(&num_as_string);
free(data[0]);
free(data);
MPI_Finalize();
return 0;
}
运行给予:
$ mpicc -o matrixastxt matrixastxt.c -std=c99
$ mpirun -np 4 ./matrixastxt
$ more all-data.txt
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000
2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000 2.000
3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000
3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000
3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000
3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000 3.000
相关文章:
- 如何将 txt 文件中的行分隔为组件C++
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 一种在C++中读取TXT配置文件的简单方法
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 如何修复此iFile以将.txt输出到控制台
- 无法打开.txt文件
- CMakeLists.txt中的命名空间表示法
- Arduino从Txt读取整数
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何拆分文件中.txt字母并使用c ++使用数组进行扑克?
- 使用fstream对txt文件中的数字进行循环
- Visual Studio在尝试读取resource.txt文件时崩溃
- 更正 CMakeList.txt 用于谷歌测试的文件?
- 如何从txt文件中读取多个不同长度的数组?
- 从带有空间的.txt中读取完整的文本,为单个变量C
- 使用 QFtp 下载单个.txt
- 如何在不指定argc的情况下在C++中读取单个.txt文件作为输入
- 使用mpi将矩阵写入单个txt文件
- 打开c++ TXT文件并将单个数字读入int数组