Fix VTK MPI-IO Writer

This commit is contained in:
Jan Eitzinger
2023-02-07 09:48:24 +01:00
parent f7e04a19ac
commit 93a796af98
5 changed files with 77 additions and 39 deletions

View File

@@ -76,6 +76,7 @@ int main(int argc, char** argv)
printf("Solution took %.2fs\n", timeStop - timeStart);
}
// testInit(&solver);
VtkOptions opts = { .grid = solver.grid, .comm = solver.comm };
vtkOpen(&opts, solver.problem);
vtkScalar(&opts, "pressure", solver.p);

View File

@@ -26,10 +26,21 @@ void testInit(Solver* s)
for (int k = 0; k < kmaxLocal + 2; k++) {
for (int j = 0; j < jmaxLocal + 2; j++) {
for (int i = 0; i < imaxLocal + 2; i++) {
G(p, i, j, k) = myrank;
G(f, i, j, k) = myrank;
G(g, i, j, k) = myrank;
G(h, i, j, k) = myrank;
G(p, i, j, k) = 10.0;
G(f, i, j, k) = myrank + 1.0;
G(g, i, j, k) = myrank + 1.0;
G(h, i, j, k) = myrank + 1.0;
}
}
}
for (int k = 1; k < kmaxLocal + 1; k++) {
for (int j = 1; j < jmaxLocal + 1; j++) {
for (int i = 1; i < imaxLocal + 1; i++) {
G(p, i, j, k) = myrank + 1.0;
G(f, i, j, k) = myrank + 1.0;
G(g, i, j, k) = myrank + 1.0;
G(h, i, j, k) = myrank + 1.0;
}
}
}

View File

@@ -14,6 +14,16 @@
#include "comm.h"
#include "vtkWriter.h"
static void resetFileview(VtkOptions* o)
{
// reset fileview for output of string header
MPI_Offset disp;
MPI_Barrier(o->comm.comm);
MPI_File_get_size(o->fh, &disp);
MPI_File_set_view(o->fh, disp, MPI_CHAR, MPI_CHAR, "native", MPI_INFO_NULL);
// printf("Rank %d disp %lld\n", o->comm.rank, disp);
}
static void writeHeader(VtkOptions* o)
{
const size_t MAX_HEADER = 200;
@@ -42,10 +52,9 @@ static void writeHeader(VtkOptions* o)
o->grid.imax * o->grid.jmax * o->grid.kmax);
if (commIsMaster(&o->comm)) {
MPI_File_write_at(o->fh, 0, header, strlen(header), MPI_CHAR, MPI_STATUS_IGNORE);
MPI_File_write(o->fh, header, strlen(header), MPI_CHAR, MPI_STATUS_IGNORE);
}
o->disp = strlen(header);
free(header);
}
@@ -57,22 +66,16 @@ void vtkOpen(VtkOptions* o, char* problem)
snprintf(filename, 50, "%s-p%d.vtk", problem, o->comm.size);
MPI_File_open(o->comm.comm,
filename,
MPI_MODE_WRONLY | MPI_MODE_CREATE,
MPI_MODE_WRONLY | MPI_MODE_CREATE | MPI_MODE_EXCL,
MPI_INFO_NULL,
&o->fh);
if (commIsMaster(&o->comm)) {
printf("Writing VTK output for %s\n", problem);
}
writeHeader(o);
}
static void resetFileview(VtkOptions* o)
{
// reset fileview for output of string header
MPI_Offset disp;
MPI_File_get_size(o->fh, &disp);
MPI_File_set_view(o->fh, disp, MPI_CHAR, MPI_CHAR, "native", MPI_INFO_NULL);
resetFileview(o);
writeHeader(o);
}
void vtkScalar(VtkOptions* o, char* name, double* s)
@@ -85,7 +88,7 @@ void vtkScalar(VtkOptions* o, char* name, double* s)
char* header = (char*)malloc(MAX_HEADER);
char* cursor = header;
cursor += sprintf(cursor, "SCALARS %s float 1\n", name);
cursor += sprintf(cursor, "SCALARS %s double 1\n", name);
cursor += sprintf(cursor, "LOOKUP_TABLE default\n");
if (commIsMaster(&o->comm)) {
@@ -100,6 +103,7 @@ void vtkScalar(VtkOptions* o, char* name, double* s)
// set global view in file
MPI_Offset disp;
MPI_Datatype fileViewType;
MPI_Barrier(o->comm.comm);
MPI_File_get_size(o->fh, &disp);
MPI_Type_create_subarray(NDIMS,
@@ -112,6 +116,18 @@ void vtkScalar(VtkOptions* o, char* name, double* s)
MPI_Type_commit(&fileViewType);
MPI_File_set_view(o->fh, disp, MPI_DOUBLE, fileViewType, "external32", MPI_INFO_NULL);
#ifdef VERBOSE
printf("Rank: %d, Disp: %lld, Size(k,j,i): %d %d %d, Offset(k,j,i): %d %d %d\n",
o->comm.rank,
disp,
o->comm.kmaxLocal,
o->comm.jmaxLocal,
o->comm.imaxLocal,
offsets[KDIM],
offsets[JDIM],
offsets[IDIM]);
#endif
// create local bulk type
MPI_Datatype bulkType;
@@ -152,7 +168,7 @@ void vtkVector(VtkOptions* o, char* name, VtkVector vec)
const size_t MAX_HEADER = 100;
char* header = (char*)malloc(MAX_HEADER);
sprintf(header, "VECTORS %s float\n", name);
sprintf(header, "VECTORS %s double\n", name);
resetFileview(o);
if (commIsMaster(&o->comm)) {
@@ -164,21 +180,25 @@ void vtkVector(VtkOptions* o, char* name, VtkVector vec)
// set global view in file
MPI_Offset disp;
MPI_Datatype fileViewType;
MPI_Datatype fileViewType, vectorType;
MPI_Barrier(o->comm.comm);
MPI_File_get_size(o->fh, &disp);
MPI_Type_create_subarray(NDIMS + 1,
(int[NDIMS + 1]) { o->grid.kmax, o->grid.jmax, o->grid.imax, NDIMS },
(int[NDIMS + 1]) { kmaxLocal, jmaxLocal, imaxLocal, NDIMS },
MPI_Type_contiguous(NDIMS, MPI_DOUBLE, &vectorType);
MPI_Type_commit(&vectorType);
MPI_Type_create_subarray(NDIMS,
(int[NDIMS]) { o->grid.kmax, o->grid.jmax, o->grid.imax },
(int[NDIMS]) { kmaxLocal, jmaxLocal, imaxLocal },
offsets,
MPI_ORDER_C,
MPI_DOUBLE,
vectorType,
&fileViewType);
MPI_Type_commit(&fileViewType);
MPI_File_set_view(o->fh, disp, MPI_DOUBLE, fileViewType, "external32", MPI_INFO_NULL);
size_t cnt = imaxLocal * jmaxLocal * kmaxLocal * NDIMS;
double* tmp = allocate(64, cnt * sizeof(double));
size_t cnt = imaxLocal * jmaxLocal * kmaxLocal;
double* tmp = allocate(64, cnt * NDIMS * sizeof(double));
int idx = 0;
for (int k = 1; k < kmaxLocal + 1; k++) {
@@ -191,8 +211,10 @@ void vtkVector(VtkOptions* o, char* name, VtkVector vec)
}
}
MPI_File_write(o->fh, tmp, cnt, MPI_DOUBLE, MPI_STATUS_IGNORE);
if (commIsMaster(&o->comm)) printf("Write %d vectors\n", (int)cnt);
MPI_File_write(o->fh, tmp, cnt, vectorType, MPI_STATUS_IGNORE);
MPI_Type_free(&fileViewType);
MPI_Type_free(&vectorType);
// Binary segment must be terminated with newline character
resetFileview(o);