【发布时间】:2016-02-05 20:50:08
【问题描述】:
我有一个 MPI 代码,它实现 2D 域分解以计算 PDE 的数值解。目前,我为每个进程编写了某些 2D 分布式数组(例如 array_x--> proc000x.bin)。我想将其减少为单个二进制文件。
array_0,array_1,
数组_2,数组_3,
假设上面说明了一个具有 4 个进程 (2x2) 的笛卡尔拓扑。每个二维数组都有维度 (nx + 2, nz + 2)。 +2 表示为通信目的添加到所有方面的“幽灵”层。
我想提取主数组(省略幽灵层)并将它们写入单个二进制文件,其顺序类似于,
array_0、array_1、array_2、array_3 --> output.bin
如果可能的话,最好把它写成好像我可以访问全局网格并逐行写入,即,
array_0 的第 0 行,array_1 的第 0 行,array_0 的第 1 行 array_1 的第 1 行 ....
下面的尝试尝试文件 array_test.c 中的两种输出格式中的前者
#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>
/* 2D array allocation */
float **alloc2D(int rows, int cols);
float **alloc2D(int rows, int cols) {
int i, j;
float *data = malloc(rows * cols * sizeof(float));
float **arr2D = malloc(rows * sizeof(float *));
for (i = 0; i < rows; i++) {
arr2D[i] = &(data[i * cols]);
}
/* Initialize to zero */
for (i= 0; i < rows; i++) {
for (j=0; j < cols; j++) {
arr2D[i][j] = 0.0;
}
}
return arr2D;
}
int main(void) {
/* Creates 5x5 array of floats with padding layers and
* attempts to write distributed arrays */
/* Run toy example with 4 processes */
int i, j, row, col;
int nx = 5, ny = 5, npad = 1;
int my_rank, nproc=4;
int dim[2] = {2, 2}; /* 2x2 cartesian grid */
int period[2] = {0, 0};
int coord[2];
int reorder = 1;
float **A = NULL;
MPI_Comm grid_Comm;
/* Initialize MPI */
MPI_Init(NULL, NULL);
MPI_Comm_size(MPI_COMM_WORLD, &nproc);
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Establish cartesian topology */
MPI_Cart_create(MPI_COMM_WORLD, 2, dim, period, reorder, &grid_Comm);
/* Get cartesian grid indicies of processes */
MPI_Cart_coords(grid_Comm, my_rank, 2, coord);
row = coord[1];
col = coord[0];
/* Add ghost layers */
nx += 2 * npad;
ny += 2 * npad;
A = alloc2D(nx, ny);
/* Create derived datatype for interior grid (output grid) */
MPI_Datatype grid;
int start[2] = {npad, npad};
int arrsize[2] = {nx, ny};
int gridsize[2] = {nx - 2 * npad, ny - 2 * npad};
MPI_Type_create_subarray(2, arrsize, gridsize,
start, MPI_ORDER_C, MPI_FLOAT, &grid);
MPI_Type_commit(&grid);
/* Fill interior grid */
for (i = npad; i < nx-npad; i++) {
for (j = npad; j < ny-npad; j++) {
A[i][j] = my_rank + i;
}
}
/* MPI IO */
MPI_File fh;
MPI_Status status;
char file_name[100];
int N, offset;
sprintf(file_name, "output.bin");
MPI_File_open(grid_Comm, file_name, MPI_MODE_CREATE | MPI_MODE_WRONLY,
MPI_INFO_NULL, &fh);
N = (nx - 2 * npad) * (ny - 2 *npad);
offset = (row * 2 + col) * N * sizeof(float);
MPI_File_set_view(fh, offset, MPI_FLOAT, grid, "native",
MPI_INFO_NULL);
MPI_File_write_all(fh, &A[0][0], N, MPI_FLOAT, MPI_STATUS_IGNORE);
MPI_File_close(&fh);
/* Cleanup */
free(A[0]);
free(A);
MPI_Type_free(&grid);
MPI_Finalize();
return 0;
}
用
编译mpicc -o array_test array_test.c
与
一起运行mpiexec -n 4 array_test
代码编译运行时,输出不正确。我假设在这种情况下我误解了派生数据类型和文件写入的使用。我会很感激一些帮助找出我的错误。
【问题讨论】:
标签: c multidimensional-array parallel-processing mpi hpc