fix buffer overflow in writing triclinic headers with MPI-I/O

This commit is contained in:
Axel Kohlmeyer
2021-05-14 22:51:56 -04:00
parent 6b80cb0169
commit 1ed75e3db0
2 changed files with 442 additions and 451 deletions

View File

@ -1,4 +1,3 @@
// clang-format off
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://lammps.sandia.gov/, Sandia National Laboratories
@ -16,16 +15,17 @@
Contributing author: Paul Coffman (IBM)
------------------------------------------------------------------------- */
#include "omp_compat.h"
#include "dump_atom_mpiio.h"
#include <cmath>
#include <cstring>
#include "domain.h"
#include "update.h"
#include "memory.h"
#include "error.h"
#include "memory.h"
#include "update.h"
#include <cmath>
#include <cstring>
#include "omp_compat.h"
#if defined(_OPENMP)
#include <omp.h>
#endif
@ -38,24 +38,23 @@ using namespace LAMMPS_NS;
/* ---------------------------------------------------------------------- */
DumpAtomMPIIO::DumpAtomMPIIO(LAMMPS *lmp, int narg, char **arg) :
DumpAtom(lmp, narg, arg) {}
DumpAtomMPIIO::DumpAtomMPIIO(LAMMPS *lmp, int narg, char **arg) : DumpAtom(lmp, narg, arg) {}
/* ---------------------------------------------------------------------- */
DumpAtomMPIIO::~DumpAtomMPIIO()
{
if (multifile == 0) MPI_File_close(&mpifh);
if (multifile == 0) MPI_File_close(&mpifh);
}
/* ---------------------------------------------------------------------- */
void DumpAtomMPIIO::openfile()
{
if (singlefile_opened) { // single file already opened, so just return after resetting filesize
if (singlefile_opened) { // single file already opened, so just return after resetting filesize
mpifo = currentFileSize;
MPI_File_set_size(mpifh,mpifo+headerSize+sumFileSize);
currentFileSize = mpifo+headerSize+sumFileSize;
MPI_File_set_size(mpifh, mpifo + headerSize + sumFileSize);
currentFileSize = mpifo + headerSize + sumFileSize;
return;
}
if (multifile == 0) singlefile_opened = 1;
@ -67,61 +66,56 @@ void DumpAtomMPIIO::openfile()
if (multifile) {
char *filestar = filecurrent;
filecurrent = new char[strlen(filestar) + 16];
char *ptr = strchr(filestar,'*');
char *ptr = strchr(filestar, '*');
*ptr = '\0';
if (padflag == 0)
sprintf(filecurrent,"%s" BIGINT_FORMAT "%s",
filestar,update->ntimestep,ptr+1);
else {
char bif[8],pad[16];
strcpy(bif,BIGINT_FORMAT);
sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]);
sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1);
if (padflag == 0) {
sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1);
} else {
char bif[8], pad[16];
strcpy(bif, BIGINT_FORMAT);
sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]);
sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1);
}
*ptr = '*';
if (maxfiles > 0) {
if (numfiles < maxfiles) {
nameslist[numfiles] = new char[strlen(filecurrent)+1];
strcpy(nameslist[numfiles],filecurrent);
nameslist[numfiles] = new char[strlen(filecurrent) + 1];
strcpy(nameslist[numfiles], filecurrent);
++numfiles;
} else {
remove(nameslist[fileidx]);
delete[] nameslist[fileidx];
nameslist[fileidx] = new char[strlen(filecurrent)+1];
strcpy(nameslist[fileidx],filecurrent);
nameslist[fileidx] = new char[strlen(filecurrent) + 1];
strcpy(nameslist[fileidx], filecurrent);
fileidx = (fileidx + 1) % maxfiles;
}
}
}
if (append_flag) { // append open
int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS) {
char str[128];
sprintf(str,"Cannot open dump file %s",filecurrent);
error->one(FLERR,str);
}
if (append_flag) { // append open
int err = MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY,
MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS)
error->one(FLERR, "Cannot open dump file {}: {}", filecurrent, utils::getsyserror());
int myrank;
MPI_Comm_rank(world,&myrank);
if (myrank == 0)
MPI_File_get_size(mpifh,&mpifo);
MPI_Comm_rank(world, &myrank);
if (myrank == 0) MPI_File_get_size(mpifh, &mpifo);
MPI_Bcast(&mpifo, 1, MPI_LMP_BIGINT, 0, world);
MPI_File_set_size(mpifh,mpifo+headerSize+sumFileSize);
currentFileSize = mpifo+headerSize+sumFileSize;
MPI_File_set_size(mpifh, mpifo + headerSize + sumFileSize);
currentFileSize = mpifo + headerSize + sumFileSize;
}
else { // replace open
} else { // replace open
int err =
MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS)
error->one(FLERR, "Cannot open dump file {}: {}", filecurrent, utils::getsyserror());
int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS) {
char str[128];
sprintf(str,"Cannot open dump file %s",filecurrent);
error->one(FLERR,str);
}
mpifo = 0;
MPI_File_set_size(mpifh,(MPI_Offset) (headerSize+sumFileSize));
currentFileSize = (headerSize+sumFileSize);
MPI_File_set_size(mpifh, (MPI_Offset)(headerSize + sumFileSize));
currentFileSize = (headerSize + sumFileSize);
}
}
@ -156,10 +150,10 @@ void DumpAtomMPIIO::write()
// nmax = max # of dump lines on any proc
bigint bnme = nme;
MPI_Allreduce(&bnme,&ntotal,1,MPI_LMP_BIGINT,MPI_SUM,world);
MPI_Allreduce(&bnme, &ntotal, 1, MPI_LMP_BIGINT, MPI_SUM, world);
int nmax;
MPI_Allreduce(&nme,&nmax,1,MPI_INT,MPI_MAX,world);
MPI_Allreduce(&nme, &nmax, 1, MPI_INT, MPI_MAX, world);
// write timestep header
// for multiproc,
@ -175,64 +169,70 @@ void DumpAtomMPIIO::write()
if (nmax > maxbuf) {
if ((bigint) nmax * size_one > MAXSMALLINT)
error->all(FLERR,"Too much per-proc info for dump");
error->all(FLERR, "Too much per-proc info for dump");
maxbuf = nmax;
memory->destroy(buf);
memory->create(buf,(maxbuf*size_one),"dump:buf");
memory->create(buf, (maxbuf * size_one), "dump:buf");
}
if (sort_flag && sortcol == 0 && nmax > maxids) {
maxids = nmax;
memory->destroy(ids);
memory->create(ids,maxids,"dump:ids");
memory->create(ids, maxids, "dump:ids");
}
if (sort_flag && sortcol == 0) pack(ids);
else pack(nullptr);
if (sort_flag && sortcol == 0)
pack(ids);
else
pack(nullptr);
if (sort_flag) sort();
// determine how much data needs to be written for setting the file size and prepocess it prior to writing
performEstimate = 1;
write_header(nheader);
write_data(nme,buf);
MPI_Bcast(&sumFileSize, 1, MPI_LMP_BIGINT, (nprocs-1), world);
write_data(nme, buf);
MPI_Bcast(&sumFileSize, 1, MPI_LMP_BIGINT, (nprocs - 1), world);
openfile();
performEstimate = 0;
write_header(nheader); // mpifo now points to end of header info
write_header(nheader); // mpifo now points to end of header info
// now actually write the data
performEstimate = 0;
write_data(nme,buf);
write_data(nme, buf);
if (multifile) MPI_File_close(&mpifh);
if (multifile) delete [] filecurrent;
if (multifile) MPI_File_close(&mpifh);
if (multifile) delete[] filecurrent;
}
/* ---------------------------------------------------------------------- */
void DumpAtomMPIIO::init_style()
{
if (image_flag == 0) size_one = 5;
else size_one = 8;
if (image_flag == 0)
size_one = 5;
else
size_one = 8;
// format = copy of default or user-specified line format
// default depends on image flags
delete [] format;
delete[] format;
if (format_line_user) {
int n = strlen(format_line_user) + 2;
format = new char[n];
strcpy(format,format_line_user);
strcat(format,"\n");
strcpy(format, format_line_user);
strcat(format, "\n");
} else {
char *str;
if (image_flag == 0) str = (char *) TAGINT_FORMAT " %d %g %g %g";
else str = (char *) TAGINT_FORMAT " %d %g %g %g %d %d %d";
if (image_flag == 0)
str = (char *) TAGINT_FORMAT " %d %g %g %g";
else
str = (char *) TAGINT_FORMAT " %d %g %g %g %d %d %d";
int n = strlen(str) + 2;
format = new char[n];
strcpy(format,str);
strcat(format,"\n");
strcpy(format, str);
strcat(format, "\n");
}
// setup boundary string
@ -274,10 +274,14 @@ void DumpAtomMPIIO::init_style()
else if (scale_flag == 0 && image_flag == 1)
pack_choice = &DumpAtomMPIIO::pack_noscale_image;
if (binary) write_choice = &DumpAtomMPIIO::write_binary;
else if (buffer_flag == 1) write_choice = &DumpAtomMPIIO::write_string;
else if (image_flag == 0) write_choice = &DumpAtomMPIIO::write_lines_noimage;
else if (image_flag == 1) write_choice = &DumpAtomMPIIO::write_lines_image;
if (binary)
write_choice = &DumpAtomMPIIO::write_binary;
else if (buffer_flag == 1)
write_choice = &DumpAtomMPIIO::write_string;
else if (image_flag == 0)
write_choice = &DumpAtomMPIIO::write_lines_noimage;
else if (image_flag == 1)
write_choice = &DumpAtomMPIIO::write_lines_image;
}
/* ---------------------------------------------------------------------- */
@ -293,48 +297,47 @@ void DumpAtomMPIIO::header_binary(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc((2*sizeof(bigint)) + (9*sizeof(int)) + (6*sizeof(double)));
headerBuffer = (char *) malloc((2 * sizeof(bigint)) + (9 * sizeof(int)) + (6 * sizeof(double)));
headerSize = 0;
memcpy(&((char*)headerBuffer)[headerSize],&update->ntimestep,sizeof(bigint));
memcpy(headerBuffer + headerSize, &update->ntimestep, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&ndump,sizeof(bigint));
memcpy(headerBuffer + headerSize, &ndump, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&domain->triclinic,sizeof(int));
memcpy(headerBuffer + headerSize, &domain->triclinic, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&domain->boundary[0][0],6*sizeof(int));
headerSize += 6*sizeof(int);
memcpy(headerBuffer + headerSize, &domain->boundary[0][0], 6 * sizeof(int));
headerSize += 6 * sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&boxxlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxylo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxylo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxyhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxyhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&size_one,sizeof(int));
memcpy(headerBuffer + headerSize, &size_one, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&nprocs,sizeof(int));
memcpy(headerBuffer + headerSize, &nprocs, sizeof(int));
headerSize += sizeof(int);
}
else { // write data
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_BYTE,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_BYTE, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
}
@ -346,59 +349,58 @@ void DumpAtomMPIIO::header_binary_triclinic(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc((2*sizeof(bigint)) + (9*sizeof(int)) + (6*sizeof(double)));
headerBuffer = (char *) malloc((2 * sizeof(bigint)) + (9 * sizeof(int)) + (9 * sizeof(double)));
headerSize = 0;
memcpy(&((char*)headerBuffer)[headerSize],&update->ntimestep,sizeof(bigint));
memcpy(headerBuffer + headerSize, &update->ntimestep, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&ndump,sizeof(bigint));
memcpy(headerBuffer + headerSize, &ndump, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&domain->triclinic,sizeof(int));
memcpy(headerBuffer + headerSize, &domain->triclinic, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&domain->boundary[0][0],6*sizeof(int));
headerSize += 6*sizeof(int);
memcpy(headerBuffer + headerSize, &domain->boundary[0][0], 6 * sizeof(int));
headerSize += 6 * sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&boxxlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxylo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxylo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxyhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxyhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxy,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxy, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxz,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxz, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxyz,sizeof(double));
memcpy(headerBuffer + headerSize, &boxyz, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&size_one,sizeof(int));
memcpy(headerBuffer + headerSize, &size_one, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&nprocs,sizeof(int));
memcpy(headerBuffer + headerSize, &nprocs, sizeof(int));
headerSize += sizeof(int);
}
else { // write data
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_BYTE,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_BYTE, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
}
@ -410,25 +412,21 @@ void DumpAtomMPIIO::header_item(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc(MAX_TEXT_HEADER_SIZE);
auto itemtxt = fmt::format("ITEM: TIMESTEP\n{}\n", update->ntimestep);
itemtxt += fmt::format("ITEM: NUMBER OF ATOMS\n{}\n", ndump);
itemtxt += fmt::format("ITEM: BOX BOUNDS {}\n", boundstr);
itemtxt += fmt::format("{} {}\n{} {}\n{} {}\n", boxxlo, boxxhi, boxylo, boxyhi, boxzlo, boxzhi);
itemtxt += fmt::format("ITEM: ATOMS {}\n", columns);
headerSize = 0;
headerSize += sprintf(((char*)&((char*)headerBuffer)[headerSize]),"ITEM: TIMESTEP\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",update->ntimestep);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: NUMBER OF ATOMS\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",ndump);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: BOX BOUNDS %s\n",boundstr);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g\n",boxxlo,boxxhi);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g\n",boxylo,boxyhi);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g\n",boxzlo,boxzhi);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: ATOMS %s\n",columns);
}
else { // write data
headerSize = itemtxt.size();
headerBuffer = utils::strdup(itemtxt);
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_CHAR,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_CHAR, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
delete[] headerBuffer;
}
}
@ -438,25 +436,22 @@ void DumpAtomMPIIO::header_item_triclinic(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc(MAX_TEXT_HEADER_SIZE);
auto itemtxt = fmt::format("ITEM: TIMESTEP\n{}\n", update->ntimestep);
itemtxt += fmt::format("ITEM: NUMBER OF ATOMS\n{}\n", ndump);
itemtxt += fmt::format("ITEM: BOX BOUNDS xy xz yz {}\n", boundstr);
itemtxt += fmt::format("{} {} {}\n{} {} {}\n{} {} {}\n", boxxlo, boxxhi, boxxy, boxylo, boxyhi,
boxxz, boxzlo, boxzhi, boxyz);
itemtxt += fmt::format("ITEM: ATOMS {}\n", columns);
headerSize = 0;
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: TIMESTEP\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",update->ntimestep);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: NUMBER OF ATOMS\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",ndump);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: BOX BOUNDS xy xz yz %s\n",boundstr);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g %g\n",boxxlo,boxxhi,boxxy);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g %g\n",boxylo,boxyhi,boxxz);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g %g\n",boxzlo,boxzhi,boxyz);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: ATOMS %s\n",columns);
}
else { // write data
headerSize = itemtxt.size();
headerBuffer = utils::strdup(itemtxt);
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_CHAR,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_CHAR, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
delete[] headerBuffer;
}
}
@ -464,8 +459,7 @@ void DumpAtomMPIIO::header_item_triclinic(bigint ndump)
void DumpAtomMPIIO::write_data(int n, double *mybuf)
{
(this->*write_choice)(n,mybuf);
(this->*write_choice)(n, mybuf);
}
/* ---------------------------------------------------------------------- */
@ -478,22 +472,21 @@ void DumpAtomMPIIO::write_binary(int n, double *mybuf)
bigint incPrefix = 0;
bigint bigintNme = (bigint) nme;
MPI_Scan(&bigintNme,&incPrefix,1,MPI_LMP_BIGINT,MPI_SUM,world);
sumFileSize = (incPrefix*size_one*sizeof(double)) + (nprocs * sizeof(int));
offsetFromHeader = ((incPrefix-bigintNme)*size_one*sizeof(double)) + (me * sizeof(int));
}
else {
int byteBufSize = (n*sizeof(double)) + sizeof(int);
MPI_Scan(&bigintNme, &incPrefix, 1, MPI_LMP_BIGINT, MPI_SUM, world);
sumFileSize = (incPrefix * size_one * sizeof(double)) + (nprocs * sizeof(int));
offsetFromHeader = ((incPrefix - bigintNme) * size_one * sizeof(double)) + (me * sizeof(int));
} else {
int byteBufSize = (n * sizeof(double)) + sizeof(int);
char *bufWithSize;
memory->create(bufWithSize,byteBufSize,"dump:bufWithSize");
memcpy(bufWithSize,(char*)(&n),sizeof(int));
memcpy(&((char*)bufWithSize)[sizeof(int)],mybuf,(n*sizeof(double)));
MPI_File_write_at_all(mpifh,mpifo+offsetFromHeader,bufWithSize,byteBufSize,MPI_BYTE,MPI_STATUS_IGNORE);
memory->create(bufWithSize, byteBufSize, "dump:bufWithSize");
memcpy(bufWithSize, (char *) (&n), sizeof(int));
memcpy(&((char *) bufWithSize)[sizeof(int)], mybuf, (n * sizeof(double)));
MPI_File_write_at_all(mpifh, mpifo + offsetFromHeader, bufWithSize, byteBufSize, MPI_BYTE,
MPI_STATUS_IGNORE);
memory->destroy(bufWithSize);
if (flush_flag)
MPI_File_sync(mpifh);
if (flush_flag) MPI_File_sync(mpifh);
}
}
@ -506,23 +499,21 @@ void DumpAtomMPIIO::write_string(int n, double *mybuf)
#if defined(_OPENMP)
int nthreads = omp_get_max_threads();
if (nthreads > 1)
nsme = convert_string_omp(n,mybuf);
nsme = convert_string_omp(n, mybuf);
else {
nsme = convert_string(n,mybuf);
nsme = convert_string(n, mybuf);
}
#else
nsme = convert_string(n,mybuf);
nsme = convert_string(n, mybuf);
#endif
bigint incPrefix = 0;
bigint bigintNsme = (bigint) nsme;
MPI_Scan(&bigintNsme,&incPrefix,1,MPI_LMP_BIGINT,MPI_SUM,world);
sumFileSize = (incPrefix*sizeof(char));
offsetFromHeader = ((incPrefix-bigintNsme)*sizeof(char));
}
else {
MPI_File_write_at_all(mpifh,mpifo+offsetFromHeader,sbuf,nsme,MPI_CHAR,MPI_STATUS_IGNORE);
if (flush_flag)
MPI_File_sync(mpifh);
MPI_Scan(&bigintNsme, &incPrefix, 1, MPI_LMP_BIGINT, MPI_SUM, world);
sumFileSize = (incPrefix * sizeof(char));
offsetFromHeader = ((incPrefix - bigintNsme) * sizeof(char));
} else {
MPI_File_write_at_all(mpifh, mpifo + offsetFromHeader, sbuf, nsme, MPI_CHAR, MPI_STATUS_IGNORE);
if (flush_flag) MPI_File_sync(mpifh);
}
}
@ -531,9 +522,9 @@ void DumpAtomMPIIO::write_string(int n, double *mybuf)
int DumpAtomMPIIO::convert_string(int n, double *mybuf)
{
if (image_flag == 0)
return convert_noimage(n,mybuf);
return convert_noimage(n, mybuf);
else
return convert_image(n,mybuf);
return convert_image(n, mybuf);
}
/* ---------------------------------------------------------------------- */
@ -543,9 +534,9 @@ int DumpAtomMPIIO::convert_string(int n, double *mybuf)
int DumpAtomMPIIO::convert_string_omp(int n, double *mybuf)
{
if (image_flag == 0)
return convert_noimage_omp(n,mybuf);
return convert_noimage_omp(n, mybuf);
else
return convert_image_omp(n,mybuf);
return convert_image_omp(n, mybuf);
}
/* ----------------------------------------------------------------------
@ -562,55 +553,63 @@ int DumpAtomMPIIO::convert_image_omp(int n, double *mybuf)
mpifhStringCount = 0;
int nthreads = omp_get_max_threads();
if (nthreads > n) { // call serial version
convert_string(n,mybuf);
if (nthreads > n) { // call serial version
convert_string(n, mybuf);
}
else {
memory->create(mpifhStringCountPerThread,nthreads,"dump:mpifhStringCountPerThread");
mpifh_buffer_line_per_thread = (char **) malloc(nthreads*sizeof(char*));
memory->create(bufOffset,nthreads,"dump:bufOffset");
memory->create(bufRange,nthreads,"dump:bufRange");
memory->create(bufLength,nthreads,"dump:bufLength");
} else {
memory->create(mpifhStringCountPerThread, nthreads, "dump:mpifhStringCountPerThread");
mpifh_buffer_line_per_thread = (char **) malloc(nthreads * sizeof(char *));
memory->create(bufOffset, nthreads, "dump:bufOffset");
memory->create(bufRange, nthreads, "dump:bufRange");
memory->create(bufLength, nthreads, "dump:bufLength");
int i=0;
for (i=0;i<(nthreads-1);i++) {
int i = 0;
for (i = 0; i < (nthreads - 1); i++) {
mpifhStringCountPerThread[i] = 0;
bufOffset[i] = (int) (i*(int)(floor((double)n/(double)nthreads))*size_one);
bufRange[i] = (int)(floor((double)n/(double)nthreads));
bufOffset[i] = (int) (i * (int) (floor((double) n / (double) nthreads)) * size_one);
bufRange[i] = (int) (floor((double) n / (double) nthreads));
bufLength[i] = DUMP_BUF_CHUNK_SIZE;
mpifh_buffer_line_per_thread[i] = (char *) malloc(DUMP_BUF_CHUNK_SIZE * sizeof(char));
mpifh_buffer_line_per_thread[i][0] = '\0';
}
mpifhStringCountPerThread[i] = 0;
bufOffset[i] = (int) (i*(int)(floor((double)n/(double)nthreads))*size_one);
bufRange[i] = n-(i*(int)(floor((double)n/(double)nthreads)));
bufOffset[i] = (int) (i * (int) (floor((double) n / (double) nthreads)) * size_one);
bufRange[i] = n - (i * (int) (floor((double) n / (double) nthreads)));
bufLength[i] = DUMP_BUF_CHUNK_SIZE;
mpifh_buffer_line_per_thread[i] = (char *) malloc(DUMP_BUF_CHUNK_SIZE * sizeof(char));
mpifh_buffer_line_per_thread[i][0] = '\0';
#pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(bufOffset, bufRange, bufLength, mpifhStringCountPerThread, mpifh_buffer_line_per_thread, mybuf)
#pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(bufOffset, bufRange, bufLength, \
mpifhStringCountPerThread, \
mpifh_buffer_line_per_thread, mybuf)
{
int tid = omp_get_thread_num();
int m=0;
int m = 0;
for (int i = 0; i < bufRange[tid]; i++) {
if ((bufLength[tid] - mpifhStringCountPerThread[tid]) < DUMP_BUF_INCREMENT_SIZE) {
mpifh_buffer_line_per_thread[tid] = (char *) realloc(mpifh_buffer_line_per_thread[tid],(mpifhStringCountPerThread[tid]+DUMP_BUF_CHUNK_SIZE) * sizeof(char));
bufLength[tid] = (mpifhStringCountPerThread[tid]+DUMP_BUF_CHUNK_SIZE) * sizeof(char);
mpifh_buffer_line_per_thread[tid] = (char *) realloc(
mpifh_buffer_line_per_thread[tid],
(mpifhStringCountPerThread[tid] + DUMP_BUF_CHUNK_SIZE) * sizeof(char));
bufLength[tid] = (mpifhStringCountPerThread[tid] + DUMP_BUF_CHUNK_SIZE) * sizeof(char);
}
mpifhStringCountPerThread[tid] += sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),format,static_cast<int> (mybuf[bufOffset[tid]+m]),static_cast<int> (mybuf[bufOffset[tid]+m+1]),mybuf[bufOffset[tid]+m+2],mybuf[bufOffset[tid]+m+3],mybuf[bufOffset[tid]+m+4],static_cast<int> (mybuf[bufOffset[tid]+m+5]),static_cast<int> (mybuf[bufOffset[tid]+m+6]),static_cast<int> (mybuf[bufOffset[tid]+m+7]));
mpifhStringCountPerThread[tid] +=
sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]), format,
static_cast<int>(mybuf[bufOffset[tid] + m]),
static_cast<int>(mybuf[bufOffset[tid] + m + 1]), mybuf[bufOffset[tid] + m + 2],
mybuf[bufOffset[tid] + m + 3], mybuf[bufOffset[tid] + m + 4],
static_cast<int>(mybuf[bufOffset[tid] + m + 5]),
static_cast<int>(mybuf[bufOffset[tid] + m + 6]),
static_cast<int>(mybuf[bufOffset[tid] + m + 7]));
m += size_one;
}
}
#pragma omp barrier
mpifhStringCount = 0;
for (i=0;i<nthreads;i++) {
mpifhStringCount += mpifhStringCountPerThread[i];
}
for (i = 0; i < nthreads; i++) { mpifhStringCount += mpifhStringCountPerThread[i]; }
memory->destroy(bufOffset);
memory->destroy(bufRange);
@ -619,21 +618,19 @@ int DumpAtomMPIIO::convert_image_omp(int n, double *mybuf)
if (mpifhStringCount > 0) {
if (mpifhStringCount > maxsbuf) {
if (mpifhStringCount > MAXSMALLINT) return -1;
maxsbuf = mpifhStringCount+1;
memory->grow(sbuf,maxsbuf,"dump:sbuf");
maxsbuf = mpifhStringCount + 1;
memory->grow(sbuf, maxsbuf, "dump:sbuf");
}
sbuf[0] = '\0';
}
for (int i=0;i<nthreads;i++) {
strcat(sbuf,mpifh_buffer_line_per_thread[i]);
for (int i = 0; i < nthreads; i++) {
strcat(sbuf, mpifh_buffer_line_per_thread[i]);
free(mpifh_buffer_line_per_thread[i]);
}
memory->destroy(mpifhStringCountPerThread);
free(mpifh_buffer_line_per_thread);
}
return mpifhStringCount;
}
@ -652,55 +649,60 @@ int DumpAtomMPIIO::convert_noimage_omp(int n, double *mybuf)
mpifhStringCount = 0;
int nthreads = omp_get_max_threads();
if (nthreads > n) { // call serial version
convert_string(n,mybuf);
if (nthreads > n) { // call serial version
convert_string(n, mybuf);
}
else {
memory->create(mpifhStringCountPerThread,nthreads,"dump:mpifhStringCountPerThread");
mpifh_buffer_line_per_thread = (char **) malloc(nthreads*sizeof(char*));
memory->create(bufOffset,nthreads,"dump:bufOffset");
memory->create(bufRange,nthreads,"dump:bufRange");
memory->create(bufLength,nthreads,"dump:bufLength");
} else {
memory->create(mpifhStringCountPerThread, nthreads, "dump:mpifhStringCountPerThread");
mpifh_buffer_line_per_thread = (char **) malloc(nthreads * sizeof(char *));
memory->create(bufOffset, nthreads, "dump:bufOffset");
memory->create(bufRange, nthreads, "dump:bufRange");
memory->create(bufLength, nthreads, "dump:bufLength");
int i=0;
for (i=0;i<(nthreads-1);i++) {
int i = 0;
for (i = 0; i < (nthreads - 1); i++) {
mpifhStringCountPerThread[i] = 0;
bufOffset[i] = (int) (i*(int)(floor((double)n/(double)nthreads))*size_one);
bufRange[i] = (int)(floor((double)n/(double)nthreads));
bufOffset[i] = (int) (i * (int) (floor((double) n / (double) nthreads)) * size_one);
bufRange[i] = (int) (floor((double) n / (double) nthreads));
bufLength[i] = DUMP_BUF_CHUNK_SIZE;
mpifh_buffer_line_per_thread[i] = (char *) malloc(DUMP_BUF_CHUNK_SIZE * sizeof(char));
mpifh_buffer_line_per_thread[i][0] = '\0';
}
mpifhStringCountPerThread[i] = 0;
bufOffset[i] = (int) (i*(int)(floor((double)n/(double)nthreads))*size_one);
bufRange[i] = n-(i*(int)(floor((double)n/(double)nthreads)));
bufOffset[i] = (int) (i * (int) (floor((double) n / (double) nthreads)) * size_one);
bufRange[i] = n - (i * (int) (floor((double) n / (double) nthreads)));
bufLength[i] = DUMP_BUF_CHUNK_SIZE;
mpifh_buffer_line_per_thread[i] = (char *) malloc(DUMP_BUF_CHUNK_SIZE * sizeof(char));
mpifh_buffer_line_per_thread[i][0] = '\0';
#pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(bufOffset, bufRange, bufLength, mpifhStringCountPerThread, mpifh_buffer_line_per_thread, mybuf)
#pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(bufOffset, bufRange, bufLength, \
mpifhStringCountPerThread, \
mpifh_buffer_line_per_thread, mybuf)
{
int tid = omp_get_thread_num();
int m=0;
int m = 0;
for (int i = 0; i < bufRange[tid]; i++) {
if ((bufLength[tid] - mpifhStringCountPerThread[tid]) < DUMP_BUF_INCREMENT_SIZE) {
mpifh_buffer_line_per_thread[tid] = (char *) realloc(mpifh_buffer_line_per_thread[tid],(mpifhStringCountPerThread[tid]+DUMP_BUF_CHUNK_SIZE) * sizeof(char));
bufLength[tid] = (mpifhStringCountPerThread[tid]+DUMP_BUF_CHUNK_SIZE) * sizeof(char);
mpifh_buffer_line_per_thread[tid] = (char *) realloc(
mpifh_buffer_line_per_thread[tid],
(mpifhStringCountPerThread[tid] + DUMP_BUF_CHUNK_SIZE) * sizeof(char));
bufLength[tid] = (mpifhStringCountPerThread[tid] + DUMP_BUF_CHUNK_SIZE) * sizeof(char);
}
mpifhStringCountPerThread[tid] += sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),format,static_cast<int> (mybuf[bufOffset[tid]+m]),static_cast<int> (mybuf[bufOffset[tid]+m+1]),mybuf[bufOffset[tid]+m+2],mybuf[bufOffset[tid]+m+3],mybuf[bufOffset[tid]+m+4]);
mpifhStringCountPerThread[tid] +=
sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]), format,
static_cast<int>(mybuf[bufOffset[tid] + m]),
static_cast<int>(mybuf[bufOffset[tid] + m + 1]), mybuf[bufOffset[tid] + m + 2],
mybuf[bufOffset[tid] + m + 3], mybuf[bufOffset[tid] + m + 4]);
m += size_one;
}
}
#pragma omp barrier
mpifhStringCount = 0;
for (i=0;i<nthreads;i++) {
mpifhStringCount += mpifhStringCountPerThread[i];
}
for (i = 0; i < nthreads; i++) { mpifhStringCount += mpifhStringCountPerThread[i]; }
memory->destroy(bufOffset);
memory->destroy(bufRange);
@ -709,21 +711,19 @@ int DumpAtomMPIIO::convert_noimage_omp(int n, double *mybuf)
if (mpifhStringCount > 0) {
if (mpifhStringCount > maxsbuf) {
if (mpifhStringCount > MAXSMALLINT) return -1;
maxsbuf = mpifhStringCount+1;
memory->grow(sbuf,maxsbuf,"dump:sbuf");
maxsbuf = mpifhStringCount + 1;
memory->grow(sbuf, maxsbuf, "dump:sbuf");
}
sbuf[0] = '\0';
}
for (int i=0;i<nthreads;i++) {
strcat(sbuf,mpifh_buffer_line_per_thread[i]);
for (int i = 0; i < nthreads; i++) {
strcat(sbuf, mpifh_buffer_line_per_thread[i]);
free(mpifh_buffer_line_per_thread[i]);
}
memory->destroy(mpifhStringCountPerThread);
free(mpifh_buffer_line_per_thread);
}
return mpifhStringCount;

View File

@ -1,4 +1,3 @@
// clang-format off
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://lammps.sandia.gov/, Sandia National Laboratories
@ -16,20 +15,21 @@
Contributing author: Paul Coffman (IBM)
------------------------------------------------------------------------- */
#include "omp_compat.h"
#include "dump_custom_mpiio.h"
#include <cmath>
#include <cstring>
#include "domain.h"
#include "input.h"
#include "variable.h"
#include "update.h"
#include "modify.h"
#include "fix.h"
#include "memory.h"
#include "error.h"
#include "fix.h"
#include "input.h"
#include "memory.h"
#include "modify.h"
#include "update.h"
#include "variable.h"
#include <cmath>
#include <cstring>
#include "omp_compat.h"
#if defined(_OPENMP)
#include <omp.h>
#endif
@ -40,21 +40,20 @@ using namespace LAMMPS_NS;
#define DUMP_BUF_CHUNK_SIZE 16384
#define DUMP_BUF_INCREMENT_SIZE 4096
enum{ID,MOL,TYPE,ELEMENT,MASS,
X,Y,Z,XS,YS,ZS,XSTRI,YSTRI,ZSTRI,XU,YU,ZU,XUTRI,YUTRI,ZUTRI,
XSU,YSU,ZSU,XSUTRI,YSUTRI,ZSUTRI,
IX,IY,IZ,
VX,VY,VZ,FX,FY,FZ,
Q,MUX,MUY,MUZ,MU,RADIUS,DIAMETER,
OMEGAX,OMEGAY,OMEGAZ,ANGMOMX,ANGMOMY,ANGMOMZ,
TQX,TQY,TQZ,SPIN,ERADIUS,ERVEL,ERFORCE,
COMPUTE,FIX,VARIABLE};
enum{LT,LE,GT,GE,EQ,NEQ};
// clang-format off
enum{ ID, MOL, TYPE, ELEMENT, MASS,
X, Y, Z, XS, YS, ZS, XSTRI, YSTRI, ZSTRI, XU, YU, ZU, XUTRI, YUTRI, ZUTRI,
XSU, YSU, ZSU, XSUTRI, YSUTRI, ZSUTRI,
IX, IY, IZ, VX, VY, VZ, FX, FY, FZ,
Q, MUX, MUY, MUZ, MU, RADIUS, DIAMETER,
OMEGAX, OMEGAY, OMEGAZ, ANGMOMX, ANGMOMY, ANGMOMZ,
TQX, TQY, TQZ, SPIN, ERADIUS, ERVEL, ERFORCE,
COMPUTE, FIX, VARIABLE };
enum{ LT, LE, GT, GE, EQ, NEQ };
// clang-format on
/* ---------------------------------------------------------------------- */
DumpCustomMPIIO::DumpCustomMPIIO(LAMMPS *lmp, int narg, char **arg) :
DumpCustom(lmp, narg, arg) {}
DumpCustomMPIIO::DumpCustomMPIIO(LAMMPS *lmp, int narg, char **arg) : DumpCustom(lmp, narg, arg) {}
/* ---------------------------------------------------------------------- */
@ -67,10 +66,10 @@ DumpCustomMPIIO::~DumpCustomMPIIO()
void DumpCustomMPIIO::openfile()
{
if (singlefile_opened) { // single file already opened, so just return after resetting filesize
if (singlefile_opened) { // single file already opened, so just return after resetting filesize
mpifo = currentFileSize;
MPI_File_set_size(mpifh,mpifo+headerSize+sumFileSize);
currentFileSize = mpifo+headerSize+sumFileSize;
MPI_File_set_size(mpifh, mpifo + headerSize + sumFileSize);
currentFileSize = mpifo + headerSize + sumFileSize;
return;
}
if (multifile == 0) singlefile_opened = 1;
@ -82,62 +81,56 @@ void DumpCustomMPIIO::openfile()
if (multifile) {
char *filestar = filecurrent;
filecurrent = new char[strlen(filestar) + 16];
char *ptr = strchr(filestar,'*');
char *ptr = strchr(filestar, '*');
*ptr = '\0';
if (padflag == 0)
sprintf(filecurrent,"%s" BIGINT_FORMAT "%s",
filestar,update->ntimestep,ptr+1);
else {
char bif[8],pad[16];
strcpy(bif,BIGINT_FORMAT);
sprintf(pad,"%%s%%0%d%s%%s",padflag,&bif[1]);
sprintf(filecurrent,pad,filestar,update->ntimestep,ptr+1);
if (padflag == 0) {
sprintf(filecurrent, "%s" BIGINT_FORMAT "%s", filestar, update->ntimestep, ptr + 1);
} else {
char bif[8], pad[16];
strcpy(bif, BIGINT_FORMAT);
sprintf(pad, "%%s%%0%d%s%%s", padflag, &bif[1]);
sprintf(filecurrent, pad, filestar, update->ntimestep, ptr + 1);
}
*ptr = '*';
if (maxfiles > 0) {
if (numfiles < maxfiles) {
nameslist[numfiles] = new char[strlen(filecurrent)+1];
strcpy(nameslist[numfiles],filecurrent);
nameslist[numfiles] = new char[strlen(filecurrent) + 1];
strcpy(nameslist[numfiles], filecurrent);
++numfiles;
} else {
remove(nameslist[fileidx]);
delete[] nameslist[fileidx];
nameslist[fileidx] = new char[strlen(filecurrent)+1];
strcpy(nameslist[fileidx],filecurrent);
nameslist[fileidx] = new char[strlen(filecurrent) + 1];
strcpy(nameslist[fileidx], filecurrent);
fileidx = (fileidx + 1) % maxfiles;
}
}
}
if (append_flag) { // append open
int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS) {
char str[128];
sprintf(str,"Cannot open dump file %s",filecurrent);
error->one(FLERR,str);
}
if (append_flag) { // append open
int err = MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_APPEND | MPI_MODE_WRONLY,
MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS)
error->one(FLERR, "Cannot open dump file {}: {}", filecurrent, utils::getsyserror());
int myrank;
MPI_Comm_rank(world,&myrank);
if (myrank == 0)
MPI_File_get_size(mpifh,&mpifo);
MPI_Comm_rank(world, &myrank);
if (myrank == 0) MPI_File_get_size(mpifh, &mpifo);
MPI_Bcast(&mpifo, 1, MPI_LMP_BIGINT, 0, world);
MPI_File_set_size(mpifh,mpifo+headerSize+sumFileSize);
currentFileSize = mpifo+headerSize+sumFileSize;
MPI_File_set_size(mpifh, mpifo + headerSize + sumFileSize);
currentFileSize = mpifo + headerSize + sumFileSize;
}
else { // replace open
} else { // replace open
int err =
MPI_File_open(world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS)
error->one(FLERR, "Cannot open dump file {}: {}", filecurrent, utils::getsyserror());
int err = MPI_File_open( world, filecurrent, MPI_MODE_CREATE | MPI_MODE_WRONLY , MPI_INFO_NULL, &mpifh);
if (err != MPI_SUCCESS) {
char str[128];
sprintf(str,"Cannot open dump file %s",filecurrent);
error->one(FLERR,str);
}
mpifo = 0;
MPI_File_set_size(mpifh,(MPI_Offset) (headerSize+sumFileSize));
currentFileSize = (headerSize+sumFileSize);
MPI_File_set_size(mpifh, (MPI_Offset)(headerSize + sumFileSize));
currentFileSize = (headerSize + sumFileSize);
}
}
@ -172,10 +165,10 @@ void DumpCustomMPIIO::write()
// nmax = max # of dump lines on any proc
bigint bnme = nme;
MPI_Allreduce(&bnme,&ntotal,1,MPI_LMP_BIGINT,MPI_SUM,world);
MPI_Allreduce(&bnme, &ntotal, 1, MPI_LMP_BIGINT, MPI_SUM, world);
int nmax;
MPI_Allreduce(&nme,&nmax,1,MPI_INT,MPI_MAX,world);
MPI_Allreduce(&nme, &nmax, 1, MPI_INT, MPI_MAX, world);
// write timestep header
// for multiproc,
@ -191,38 +184,40 @@ void DumpCustomMPIIO::write()
if (nmax > maxbuf) {
if ((bigint) nmax * size_one > MAXSMALLINT)
error->all(FLERR,"Too much per-proc info for dump");
error->all(FLERR, "Too much per-proc info for dump");
maxbuf = nmax;
memory->destroy(buf);
memory->create(buf,(maxbuf*size_one),"dump:buf");
memory->create(buf, (maxbuf * size_one), "dump:buf");
}
if (sort_flag && sortcol == 0 && nmax > maxids) {
maxids = nmax;
memory->destroy(ids);
memory->create(ids,maxids,"dump:ids");
memory->create(ids, maxids, "dump:ids");
}
if (sort_flag && sortcol == 0) pack(ids);
else pack(nullptr);
if (sort_flag && sortcol == 0)
pack(ids);
else
pack(nullptr);
if (sort_flag) sort();
// determine how much data needs to be written for setting the file size and prepocess it prior to writing
performEstimate = 1;
write_header(nheader);
write_data(nme,buf);
MPI_Bcast(&sumFileSize, 1, MPI_LMP_BIGINT, (nprocs-1), world);
write_data(nme, buf);
MPI_Bcast(&sumFileSize, 1, MPI_LMP_BIGINT, (nprocs - 1), world);
openfile();
performEstimate = 0;
write_header(nheader); // mpifo now points to end of header info
write_header(nheader); // mpifo now points to end of header info
// now actually write the data
performEstimate = 0;
write_data(nme,buf);
write_data(nme, buf);
if (multifile) MPI_File_close(&mpifh);
if (multifile) delete [] filecurrent;
if (multifile) MPI_File_close(&mpifh);
if (multifile) delete[] filecurrent;
}
/* ---------------------------------------------------------------------- */
@ -231,14 +226,16 @@ void DumpCustomMPIIO::init_style()
{
// format = copy of default or user-specified line format
delete [] format;
delete[] format;
char *str;
if (format_line_user) str = format_line_user;
else str = format_default;
if (format_line_user)
str = format_line_user;
else
str = format_default;
int n = strlen(str) + 1;
format = new char[n];
strcpy(format,str);
strcpy(format, str);
// tokenize the format string and add space at end of each format element
// if user-specified int/float format exists, use it instead
@ -247,29 +244,31 @@ void DumpCustomMPIIO::init_style()
char *ptr;
for (int i = 0; i < size_one; i++) {
if (i == 0) ptr = strtok(format," \0");
else ptr = strtok(nullptr," \0");
if (ptr == nullptr) error->all(FLERR,"Dump_modify format line is too short");
delete [] vformat[i];
if (i == 0)
ptr = strtok(format, " \0");
else
ptr = strtok(nullptr, " \0");
if (ptr == nullptr) error->all(FLERR, "Dump_modify format line is too short");
delete[] vformat[i];
if (format_column_user[i]) {
vformat[i] = new char[strlen(format_column_user[i]) + 2];
strcpy(vformat[i],format_column_user[i]);
strcpy(vformat[i], format_column_user[i]);
} else if (vtype[i] == Dump::INT && format_int_user) {
vformat[i] = new char[strlen(format_int_user) + 2];
strcpy(vformat[i],format_int_user);
strcpy(vformat[i], format_int_user);
} else if (vtype[i] == Dump::DOUBLE && format_float_user) {
vformat[i] = new char[strlen(format_float_user) + 2];
strcpy(vformat[i],format_float_user);
strcpy(vformat[i], format_float_user);
} else if (vtype[i] == Dump::BIGINT && format_bigint_user) {
vformat[i] = new char[strlen(format_bigint_user) + 2];
strcpy(vformat[i],format_bigint_user);
strcpy(vformat[i], format_bigint_user);
} else {
vformat[i] = new char[strlen(ptr) + 2];
strcpy(vformat[i],ptr);
strcpy(vformat[i], ptr);
}
vformat[i] = strcat(vformat[i]," ");
vformat[i] = strcat(vformat[i], " ");
}
// setup boundary string
@ -287,8 +286,10 @@ void DumpCustomMPIIO::init_style()
else if (!binary && domain->triclinic == 1)
header_choice = &DumpCustomMPIIO::header_item_triclinic;
if (binary) write_choice = &DumpCustomMPIIO::write_binary;
else write_choice = &DumpCustomMPIIO::write_string;
if (binary)
write_choice = &DumpCustomMPIIO::write_binary;
else
write_choice = &DumpCustomMPIIO::write_string;
// find current ptr for each compute,fix,variable
// check that fix frequency is acceptable
@ -296,24 +297,23 @@ void DumpCustomMPIIO::init_style()
int icompute;
for (int i = 0; i < ncompute; i++) {
icompute = modify->find_compute(id_compute[i]);
if (icompute < 0) error->all(FLERR,"Could not find dump custom compute ID");
if (icompute < 0) error->all(FLERR, "Could not find dump custom compute ID");
compute[i] = modify->compute[icompute];
}
int ifix;
for (int i = 0; i < nfix; i++) {
ifix = modify->find_fix(id_fix[i]);
if (ifix < 0) error->all(FLERR,"Could not find dump custom fix ID");
if (ifix < 0) error->all(FLERR, "Could not find dump custom fix ID");
fix[i] = modify->fix[ifix];
if (nevery % modify->fix[ifix]->peratom_freq)
error->all(FLERR,"Dump custom and fix not computed at compatible times");
error->all(FLERR, "Dump custom and fix not computed at compatible times");
}
int ivariable;
for (int i = 0; i < nvariable; i++) {
ivariable = input->variable->find(id_variable[i]);
if (ivariable < 0)
error->all(FLERR,"Could not find dump custom variable name");
if (ivariable < 0) error->all(FLERR, "Could not find dump custom variable name");
variable[i] = ivariable;
}
@ -321,8 +321,7 @@ void DumpCustomMPIIO::init_style()
if (iregion >= 0) {
iregion = domain->find_region(idregion);
if (iregion == -1)
error->all(FLERR,"Region ID for dump custom does not exist");
if (iregion == -1) error->all(FLERR, "Region ID for dump custom does not exist");
}
}
@ -339,48 +338,47 @@ void DumpCustomMPIIO::header_binary(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc((2*sizeof(bigint)) + (9*sizeof(int)) + (6*sizeof(double)));
headerBuffer = (char *) malloc((2 * sizeof(bigint)) + (9 * sizeof(int)) + (6 * sizeof(double)));
headerSize = 0;
memcpy(&((char*)headerBuffer)[headerSize],&update->ntimestep,sizeof(bigint));
memcpy(headerBuffer + headerSize, &update->ntimestep, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&ndump,sizeof(bigint));
memcpy(headerBuffer + headerSize, &ndump, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&domain->triclinic,sizeof(int));
memcpy(headerBuffer + headerSize, &domain->triclinic, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&domain->boundary[0][0],6*sizeof(int));
headerSize += 6*sizeof(int);
memcpy(headerBuffer + headerSize, &domain->boundary[0][0], 6 * sizeof(int));
headerSize += 6 * sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&boxxlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxylo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxylo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxyhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxyhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&size_one,sizeof(int));
memcpy(headerBuffer + headerSize, &size_one, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&nprocs,sizeof(int));
memcpy(headerBuffer + headerSize, &nprocs, sizeof(int));
headerSize += sizeof(int);
}
else { // write data
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_BYTE,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_BYTE, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
}
@ -392,59 +390,58 @@ void DumpCustomMPIIO::header_binary_triclinic(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc((2*sizeof(bigint)) + (9*sizeof(int)) + (6*sizeof(double)));
headerBuffer = (char *) malloc((2 * sizeof(bigint)) + (9 * sizeof(int)) + (9 * sizeof(double)));
headerSize = 0;
memcpy(&((char*)headerBuffer)[headerSize],&update->ntimestep,sizeof(bigint));
memcpy(headerBuffer + headerSize, &update->ntimestep, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&ndump,sizeof(bigint));
memcpy(headerBuffer + headerSize, &ndump, sizeof(bigint));
headerSize += sizeof(bigint);
memcpy(&((char*)headerBuffer)[headerSize],&domain->triclinic,sizeof(int));
memcpy(headerBuffer + headerSize, &domain->triclinic, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&domain->boundary[0][0],6*sizeof(int));
headerSize += 6*sizeof(int);
memcpy(headerBuffer + headerSize, &domain->boundary[0][0], 6 * sizeof(int));
headerSize += 6 * sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&boxxlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxylo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxylo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxyhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxyhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzlo,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzlo, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxzhi,sizeof(double));
memcpy(headerBuffer + headerSize, &boxzhi, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxy,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxy, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxxz,sizeof(double));
memcpy(headerBuffer + headerSize, &boxxz, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&boxyz,sizeof(double));
memcpy(headerBuffer + headerSize, &boxyz, sizeof(double));
headerSize += sizeof(double);
memcpy(&((char*)headerBuffer)[headerSize],&size_one,sizeof(int));
memcpy(headerBuffer + headerSize, &size_one, sizeof(int));
headerSize += sizeof(int);
memcpy(&((char*)headerBuffer)[headerSize],&nprocs,sizeof(int));
memcpy(headerBuffer + headerSize, &nprocs, sizeof(int));
headerSize += sizeof(int);
}
else { // write data
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_BYTE,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_BYTE, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
}
@ -456,25 +453,21 @@ void DumpCustomMPIIO::header_item(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc(MAX_TEXT_HEADER_SIZE);
auto itemtxt = fmt::format("ITEM: TIMESTEP\n{}\n", update->ntimestep);
itemtxt += fmt::format("ITEM: NUMBER OF ATOMS\n{}\n", ndump);
itemtxt += fmt::format("ITEM: BOX BOUNDS {}\n", boundstr);
itemtxt += fmt::format("{} {}\n{} {}\n{} {}\n", boxxlo, boxxhi, boxylo, boxyhi, boxzlo, boxzhi);
itemtxt += fmt::format("ITEM: ATOMS {}\n", columns);
headerSize = 0;
headerSize += sprintf(((char*)&((char*)headerBuffer)[headerSize]),"ITEM: TIMESTEP\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",update->ntimestep);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: NUMBER OF ATOMS\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",ndump);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: BOX BOUNDS %s\n",boundstr);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g\n",boxxlo,boxxhi);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g\n",boxylo,boxyhi);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g\n",boxzlo,boxzhi);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: ATOMS %s\n",columns);
}
else { // write data
headerSize = itemtxt.size();
headerBuffer = utils::strdup(itemtxt);
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_CHAR,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_CHAR, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
delete[] headerBuffer;
}
}
@ -484,25 +477,22 @@ void DumpCustomMPIIO::header_item_triclinic(bigint ndump)
{
if (performEstimate) {
headerBuffer = (char *) malloc(MAX_TEXT_HEADER_SIZE);
auto itemtxt = fmt::format("ITEM: TIMESTEP\n{}\n", update->ntimestep);
itemtxt += fmt::format("ITEM: NUMBER OF ATOMS\n{}\n", ndump);
itemtxt += fmt::format("ITEM: BOX BOUNDS xy xz yz {}\n", boundstr);
itemtxt += fmt::format("{} {} {}\n{} {} {}\n{} {} {}\n", boxxlo, boxxhi, boxxy, boxylo, boxyhi,
boxxz, boxzlo, boxzhi, boxyz);
itemtxt += fmt::format("ITEM: ATOMS {}\n", columns);
headerSize = 0;
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: TIMESTEP\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",update->ntimestep);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: NUMBER OF ATOMS\n");
headerSize += sprintf(&((char*)headerBuffer)[headerSize],BIGINT_FORMAT "\n",ndump);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: BOX BOUNDS xy xz yz %s\n",boundstr);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g %g\n",boxxlo,boxxhi,boxxy);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g %g\n",boxylo,boxyhi,boxxz);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"%g %g %g\n",boxzlo,boxzhi,boxyz);
headerSize += sprintf(&((char*)headerBuffer)[headerSize],"ITEM: ATOMS %s\n",columns);
}
else { // write data
headerSize = itemtxt.size();
headerBuffer = utils::strdup(itemtxt);
} else { // write data
if (me == 0)
MPI_File_write_at(mpifh,mpifo,headerBuffer,headerSize,MPI_CHAR,MPI_STATUS_IGNORE);
MPI_File_write_at(mpifh, mpifo, headerBuffer, headerSize, MPI_CHAR, MPI_STATUS_IGNORE);
mpifo += headerSize;
free(headerBuffer);
delete[] headerBuffer;
}
}
@ -510,7 +500,7 @@ void DumpCustomMPIIO::header_item_triclinic(bigint ndump)
void DumpCustomMPIIO::write_data(int n, double *mybuf)
{
(this->*write_choice)(n,mybuf);
(this->*write_choice)(n, mybuf);
}
/* ---------------------------------------------------------------------- */
@ -523,22 +513,21 @@ void DumpCustomMPIIO::write_binary(int n, double *mybuf)
bigint incPrefix = 0;
bigint bigintNme = (bigint) nme;
MPI_Scan(&bigintNme,&incPrefix,1,MPI_LMP_BIGINT,MPI_SUM,world);
sumFileSize = (incPrefix*size_one*sizeof(double)) + (nprocs * sizeof(int));
offsetFromHeader = ((incPrefix-bigintNme)*size_one*sizeof(double)) + (me * sizeof(int));
}
else {
int byteBufSize = (n*sizeof(double)) + sizeof(int);
MPI_Scan(&bigintNme, &incPrefix, 1, MPI_LMP_BIGINT, MPI_SUM, world);
sumFileSize = (incPrefix * size_one * sizeof(double)) + (nprocs * sizeof(int));
offsetFromHeader = ((incPrefix - bigintNme) * size_one * sizeof(double)) + (me * sizeof(int));
} else {
int byteBufSize = (n * sizeof(double)) + sizeof(int);
char *bufWithSize;
memory->create(bufWithSize,byteBufSize,"dump:bufWithSize");
memcpy(bufWithSize,(char*)(&n),sizeof(int));
memcpy(&((char*)bufWithSize)[sizeof(int)],mybuf,(n*sizeof(double)));
MPI_File_write_at_all(mpifh,mpifo+offsetFromHeader,bufWithSize,byteBufSize,MPI_BYTE,MPI_STATUS_IGNORE);
memory->create(bufWithSize, byteBufSize, "dump:bufWithSize");
memcpy(bufWithSize, (char *) (&n), sizeof(int));
memcpy(&((char *) bufWithSize)[sizeof(int)], mybuf, (n * sizeof(double)));
MPI_File_write_at_all(mpifh, mpifo + offsetFromHeader, bufWithSize, byteBufSize, MPI_BYTE,
MPI_STATUS_IGNORE);
memory->destroy(bufWithSize);
if (flush_flag)
MPI_File_sync(mpifh);
if (flush_flag) MPI_File_sync(mpifh);
}
}
@ -551,23 +540,20 @@ void DumpCustomMPIIO::write_string(int n, double *mybuf)
#if defined(_OPENMP)
int nthreads = omp_get_max_threads();
if ((nthreads > 1) && !(lmp->kokkos))
nsme = convert_string_omp(n,mybuf); // not (yet) compatible with Kokkos
nsme = convert_string_omp(n, mybuf); // not (yet) compatible with Kokkos
else
nsme = convert_string(n,mybuf);
nsme = convert_string(n, mybuf);
#else
nsme = convert_string(n,mybuf);
nsme = convert_string(n, mybuf);
#endif
bigint incPrefix = 0;
bigint bigintNsme = (bigint) nsme;
MPI_Scan(&bigintNsme,&incPrefix,1,MPI_LMP_BIGINT,MPI_SUM,world);
sumFileSize = (incPrefix*sizeof(char));
offsetFromHeader = ((incPrefix-bigintNsme)*sizeof(char));
}
else {
MPI_File_write_at_all(mpifh,mpifo+offsetFromHeader,sbuf,nsme,MPI_CHAR,MPI_STATUS_IGNORE);
if (flush_flag)
MPI_File_sync(mpifh);
MPI_Scan(&bigintNsme, &incPrefix, 1, MPI_LMP_BIGINT, MPI_SUM, world);
sumFileSize = (incPrefix * sizeof(char));
offsetFromHeader = ((incPrefix - bigintNsme) * sizeof(char));
} else {
MPI_File_write_at_all(mpifh, mpifo + offsetFromHeader, sbuf, nsme, MPI_CHAR, MPI_STATUS_IGNORE);
if (flush_flag) MPI_File_sync(mpifh);
}
}
@ -587,64 +573,71 @@ int DumpCustomMPIIO::convert_string_omp(int n, double *mybuf)
mpifhStringCount = 0;
int nthreads = omp_get_max_threads();
if (nthreads > n) { // call serial version
convert_string(n,mybuf);
if (nthreads > n) { // call serial version
convert_string(n, mybuf);
}
else {
memory->create(mpifhStringCountPerThread,nthreads,"dump:mpifhStringCountPerThread");
mpifh_buffer_line_per_thread = (char **) malloc(nthreads*sizeof(char*));
memory->create(bufOffset,nthreads,"dump:bufOffset");
memory->create(bufRange,nthreads,"dump:bufRange");
memory->create(bufLength,nthreads,"dump:bufLength");
} else {
memory->create(mpifhStringCountPerThread, nthreads, "dump:mpifhStringCountPerThread");
mpifh_buffer_line_per_thread = (char **) malloc(nthreads * sizeof(char *));
memory->create(bufOffset, nthreads, "dump:bufOffset");
memory->create(bufRange, nthreads, "dump:bufRange");
memory->create(bufLength, nthreads, "dump:bufLength");
int i=0;
for (i=0;i<(nthreads-1);i++) {
int i = 0;
for (i = 0; i < (nthreads - 1); i++) {
mpifhStringCountPerThread[i] = 0;
bufOffset[i] = (int) (i*(int)(floor((double)n/(double)nthreads))*size_one);
bufRange[i] = (int)(floor((double)n/(double)nthreads));
bufOffset[i] = (int) (i * (int) (floor((double) n / (double) nthreads)) * size_one);
bufRange[i] = (int) (floor((double) n / (double) nthreads));
bufLength[i] = DUMP_BUF_CHUNK_SIZE;
mpifh_buffer_line_per_thread[i] = (char *) malloc(DUMP_BUF_CHUNK_SIZE * sizeof(char));
mpifh_buffer_line_per_thread[i][0] = '\0';
}
mpifhStringCountPerThread[i] = 0;
bufOffset[i] = (int) (i*(int)(floor((double)n/(double)nthreads))*size_one);
bufRange[i] = n-(i*(int)(floor((double)n/(double)nthreads)));
bufOffset[i] = (int) (i * (int) (floor((double) n / (double) nthreads)) * size_one);
bufRange[i] = n - (i * (int) (floor((double) n / (double) nthreads)));
bufLength[i] = DUMP_BUF_CHUNK_SIZE;
mpifh_buffer_line_per_thread[i] = (char *) malloc(DUMP_BUF_CHUNK_SIZE * sizeof(char));
mpifh_buffer_line_per_thread[i][0] = '\0';
#pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(bufOffset, bufRange, bufLength, mpifhStringCountPerThread, mpifh_buffer_line_per_thread, mybuf)
#pragma omp parallel LMP_DEFAULT_NONE LMP_SHARED(bufOffset, bufRange, bufLength, \
mpifhStringCountPerThread, \
mpifh_buffer_line_per_thread, mybuf)
{
int tid = omp_get_thread_num();
int m=0;
int m = 0;
for (int i = 0; i < bufRange[tid]; i++) {
if ((bufLength[tid] - mpifhStringCountPerThread[tid]) < DUMP_BUF_INCREMENT_SIZE) {
mpifh_buffer_line_per_thread[tid] = (char *) realloc(mpifh_buffer_line_per_thread[tid],(mpifhStringCountPerThread[tid]+DUMP_BUF_CHUNK_SIZE) * sizeof(char));
bufLength[tid] = (mpifhStringCountPerThread[tid]+DUMP_BUF_CHUNK_SIZE) * sizeof(char);
mpifh_buffer_line_per_thread[tid] = (char *) realloc(
mpifh_buffer_line_per_thread[tid],
(mpifhStringCountPerThread[tid] + DUMP_BUF_CHUNK_SIZE) * sizeof(char));
bufLength[tid] = (mpifhStringCountPerThread[tid] + DUMP_BUF_CHUNK_SIZE) * sizeof(char);
}
for (int j = 0; j < size_one; j++) {
if (vtype[j] == Dump::INT)
mpifhStringCountPerThread[tid] += sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),vformat[j],static_cast<int> (mybuf[bufOffset[tid]+m]));
mpifhStringCountPerThread[tid] +=
sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),
vformat[j], static_cast<int>(mybuf[bufOffset[tid] + m]));
else if (vtype[j] == Dump::DOUBLE)
mpifhStringCountPerThread[tid] += sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),vformat[j],mybuf[bufOffset[tid]+m]);
mpifhStringCountPerThread[tid] +=
sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),
vformat[j], mybuf[bufOffset[tid] + m]);
else if (vtype[j] == Dump::STRING)
mpifhStringCountPerThread[tid] += sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),vformat[j],typenames[(int) mybuf[bufOffset[tid]+m]]);
m ++;
mpifhStringCountPerThread[tid] +=
sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),
vformat[j], typenames[(int) mybuf[bufOffset[tid] + m]]);
m++;
}
mpifhStringCountPerThread[tid] += sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]),"\n");
mpifhStringCountPerThread[tid] +=
sprintf(&(mpifh_buffer_line_per_thread[tid][mpifhStringCountPerThread[tid]]), "\n");
}
}
#pragma omp barrier
mpifhStringCount = 0;
for (i=0;i<nthreads;i++) {
mpifhStringCount += mpifhStringCountPerThread[i];
}
for (i = 0; i < nthreads; i++) { mpifhStringCount += mpifhStringCountPerThread[i]; }
memory->destroy(bufOffset);
memory->destroy(bufRange);
@ -653,21 +646,19 @@ int DumpCustomMPIIO::convert_string_omp(int n, double *mybuf)
if (mpifhStringCount > 0) {
if (mpifhStringCount > maxsbuf) {
if (mpifhStringCount > MAXSMALLINT) return -1;
maxsbuf = mpifhStringCount+1;
memory->grow(sbuf,maxsbuf,"dump:sbuf");
maxsbuf = mpifhStringCount + 1;
memory->grow(sbuf, maxsbuf, "dump:sbuf");
}
sbuf[0] = '\0';
}
for (int i=0;i<nthreads;i++) {
strcat(sbuf,mpifh_buffer_line_per_thread[i]);
for (int i = 0; i < nthreads; i++) {
strcat(sbuf, mpifh_buffer_line_per_thread[i]);
free(mpifh_buffer_line_per_thread[i]);
}
memory->destroy(mpifhStringCountPerThread);
free(mpifh_buffer_line_per_thread);
}
return mpifhStringCount;