diff --git a/src/parallel_io.c b/src/parallel_io.c index aafce0a4fbf39537604e652e70eb324865d61660..0076c225e1c5361287280f8a567c8062aefd914e 100644 --- a/src/parallel_io.c +++ b/src/parallel_io.c @@ -178,10 +178,10 @@ void readArrayBackEnd(hid_t grp, char* name, enum DATA_TYPE type, int N, * * Calls #error() if an error occurs. */ -void writeArrayBackEnd(hid_t grp, char* fileName, FILE* xmfFile, - char* partTypeGroupName, char* name, enum DATA_TYPE type, - int N, int dim, long long N_total, int mpi_rank, - long long offset, char* part_c, size_t partSize, +void writeArrayBackEnd(hid_t grp, char* fileName, FILE* xmfFile, + char* partTypeGroupName, char* name, enum DATA_TYPE type, + int N, int dim, long long N_total, int mpi_rank, + long long offset, char* part_c, size_t partSize, struct UnitSystem* us, enum UnitConversionFactor convFactor) { hid_t h_data = 0, h_err = 0, h_memspace = 0, h_filespace = 0, h_plist_id = 0; @@ -269,7 +269,9 @@ void writeArrayBackEnd(hid_t grp, char* fileName, FILE* xmfFile, } /* Write XMF description for this data set */ - if (mpi_rank == 0) writeXMFline(xmfFile, fileName, partTypeGroupName, name, N_total, dim, type); + if (mpi_rank == 0) + writeXMFline(xmfFile, fileName, partTypeGroupName, name, N_total, dim, + type); /* Write unit conversion factors for this data set */ conversionString(buffer, us, convFactor); @@ -328,19 +330,17 @@ void writeArrayBackEnd(hid_t grp, char* fileName, FILE* xmfFile, * @param convFactor The UnitConversionFactor for this array * */ -#define writeArray(grp, fileName, xmfFile, partTypeGroupName, name, type, N, \ - dim, part, N_total, mpi_rank, offset, field, us, \ - convFactor) \ - writeArrayBackEnd(grp, fileName, xmfFile, partTypeGroupName, name, type, N, \ - dim, N_total, mpi_rank, offset, (char*)(&(part[0]).field),\ - sizeof(part[0]),us, convFactor) +#define writeArray(grp, fileName, xmfFile, pTypeGroupName, name, type, N, dim, \ + part, N_total, mpi_rank, offset, field, us, convFactor) \ + writeArrayBackEnd(grp, fileName, xmfFile, pTypeGroupName, name, type, N, \ + dim, N_total, mpi_rank, offset, (char*)(&(part[0]).field), \ + sizeof(part[0]), us, convFactor) /* Import the right hydro definition */ #include "hydro_io.h" /* Import the right gravity definition */ #include "gravity_io.h" - /** * @brief Reads an HDF5 initial condition file (GADGET-3 type) in parallel * @@ -361,12 +361,12 @@ void writeArrayBackEnd(hid_t grp, char* fileName, FILE* xmfFile, * */ void read_ic_parallel(char* fileName, double dim[3], struct part** parts, - struct gpart** gparts, size_t* Ngas, size_t* Ngparts, - int* periodic, int mpi_rank, int mpi_size, MPI_Comm comm, - MPI_Info info) { + struct gpart** gparts, size_t* Ngas, size_t* Ngparts, + int* periodic, int mpi_rank, int mpi_size, MPI_Comm comm, + MPI_Info info) { hid_t h_file = 0, h_grp = 0; /* GADGET has only cubic boxes (in cosmological mode) */ - double boxSize[3] = {0.0, -1.0, -1.0}; + double boxSize[3] = {0.0, -1.0, -1.0}; int numParticles[NUM_PARTICLE_TYPES] = {0}; int numParticles_highWord[NUM_PARTICLE_TYPES] = {0}; size_t N[NUM_PARTICLE_TYPES] = {0}; @@ -411,8 +411,10 @@ void read_ic_parallel(char* fileName, double dim[3], struct part** parts, dim[1] = (boxSize[1] < 0) ? boxSize[0] : boxSize[1]; dim[2] = (boxSize[2] < 0) ? boxSize[0] : boxSize[2]; - /* message("Found %d particles in a %speriodic box of size [%f %f %f].", */ - /* N_total, (periodic ? "": "non-"), dim[0], dim[1], dim[2]); */ + /* message("Found %d particles in a %speriodic box of size + * [%f %f %f].", */ + /* N_total, (periodic ? "": "non-"), dim[0], + * dim[1], dim[2]); */ /* Divide the particles among the tasks. */ for (int ptype = 0; ptype < NUM_PARTICLE_TYPES; ++ptype) { @@ -423,7 +425,6 @@ void read_ic_parallel(char* fileName, double dim[3], struct part** parts, /* Close header */ H5Gclose(h_grp); - /* Allocate memory to store SPH particles */ *Ngas = N[0]; if (posix_memalign((void*)parts, part_align, (*Ngas) * sizeof(struct part)) != @@ -436,47 +437,51 @@ void read_ic_parallel(char* fileName, double dim[3], struct part** parts, *Ngparts = N[1] + N[0]; if (posix_memalign((void*)gparts, gpart_align, *Ngparts * sizeof(struct gpart)) != 0) - error("Error while allocating memory for gravity particles"); + error( + "Error while allocating memory for gravity " + "particles"); bzero(*gparts, *Ngparts * sizeof(struct gpart)); - /* message("Allocated %8.2f MB for particles.", *N * sizeof(struct part) / + /* message("Allocated %8.2f MB for particles.", *N * + * sizeof(struct part) / * (1024.*1024.)); */ /* message("BoxSize = %lf", dim[0]); */ - /* message("NumPart = [%zd, %zd] Total = %zd", *Ngas, Ndm, *Ngparts); */ + /* message("NumPart = [%zd, %zd] Total = %zd", *Ngas, Ndm, + * *Ngparts); */ /* Loop over all particle types */ for (int ptype = 0; ptype < NUM_PARTICLE_TYPES; ptype++) { - + /* Don't do anything if no particle of this kind */ if (N_total[ptype] == 0) continue; - + /* Open the particle group in the file */ char partTypeGroupName[PARTICLE_GROUP_BUFFER_SIZE]; snprintf(partTypeGroupName, PARTICLE_GROUP_BUFFER_SIZE, "/PartType%d", - ptype); + ptype); h_grp = H5Gopen(h_file, partTypeGroupName, H5P_DEFAULT); if (h_grp < 0) { error("Error while opening particle group %s.", partTypeGroupName); } - + /* Read particle fields into the particle structure */ switch (ptype) { - - case GAS: - hydro_read_particles(h_grp, N[ptype], N_total[ptype], offset[ptype], - *parts); - break; - - case DM: - darkmatter_read_particles(h_grp, N[ptype], N_total[ptype], - offset[ptype], *gparts); - break; - - default: - error("Particle Type %d not yet supported. Aborting", ptype); + + case GAS: + hydro_read_particles(h_grp, N[ptype], N_total[ptype], offset[ptype], + *parts); + break; + + case DM: + darkmatter_read_particles(h_grp, N[ptype], N_total[ptype], + offset[ptype], *gparts); + break; + + default: + error("Particle Type %d not yet supported. Aborting", ptype); } - + /* Close particle group */ H5Gclose(h_grp); } @@ -497,13 +502,17 @@ void read_ic_parallel(char* fileName, double dim[3], struct part** parts, } /** - * @brief Writes an HDF5 output file (GADGET-3 type) with its XMF descriptor + * @brief Writes an HDF5 output file (GADGET-3 type) with + *its XMF descriptor * * @param e The engine containing all the system. - * @param us The UnitSystem used for the conversion of units in the output + * @param us The UnitSystem used for the conversion of units + *in the output * - * Creates an HDF5 output file and writes the particles contained - * in the engine. If such a file already exists, it is erased and replaced + * Creates an HDF5 output file and writes the particles + *contained + * in the engine. If such a file already exists, it is + *erased and replaced * by the new one. * The companion XMF file is also updated accordingly. * @@ -549,7 +558,8 @@ void write_output_parallel(struct engine* e, struct UnitSystem* us, error("Error while opening file '%s'.", fileName); } - /* Compute offset in the file and total number of particles */ + /* Compute offset in the file and total number of + * particles */ size_t N[NUM_PARTICLE_TYPES] = {Ngas, Ndm, 0}; long long N_total[NUM_PARTICLE_TYPES] = {0}; long long offset[NUM_PARTICLE_TYPES] = {0}; @@ -557,13 +567,16 @@ void write_output_parallel(struct engine* e, struct UnitSystem* us, for (int ptype = 0; ptype < NUM_PARTICLE_TYPES; ++ptype) N_total[ptype] = offset[ptype] + N[ptype]; - /* The last rank now has the correct N_total. Let's broadcast from there */ + /* The last rank now has the correct N_total. Let's + * broadcast from there */ MPI_Bcast(&N_total, 6, MPI_LONG_LONG, mpi_size - 1, comm); - /* Now everybody konws its offset and the total number of particles of each + /* Now everybody konws its offset and the total number of + * particles of each * type */ - /* Write the part of the XMF file corresponding to this specific output */ + /* Write the part of the XMF file corresponding to this + * specific output */ if (mpi_rank == 0) writeXMFoutputheader(xmfFile, fileName, e->time); /* Open header to write simulation properties */ @@ -582,12 +595,12 @@ void write_output_parallel(struct engine* e, struct UnitSystem* us, /* message("Writing file header..."); */ h_grp = H5Gcreate(h_file, "/Header", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (h_grp < 0) error("Error while creating file header\n"); - + /* Print the relevant information and print status */ writeAttribute(h_grp, "BoxSize", DOUBLE, e->s->dim, 3); double dblTime = e->time; writeAttribute(h_grp, "Time", DOUBLE, &dblTime, 1); - + /* GADGET-2 legacy values */ /* Number of particles of each type */ unsigned int numParticles[NUM_PARTICLE_TYPES] = {0}; @@ -597,21 +610,21 @@ void write_output_parallel(struct engine* e, struct UnitSystem* us, numParticlesHighWord[ptype] = (unsigned int)(N_total[ptype] >> 32); } writeAttribute(h_grp, "NumPart_ThisFile", LONGLONG, N_total, - NUM_PARTICLE_TYPES); + NUM_PARTICLE_TYPES); writeAttribute(h_grp, "NumPart_Total", UINT, numParticles, - NUM_PARTICLE_TYPES); + NUM_PARTICLE_TYPES); writeAttribute(h_grp, "NumPart_Total_HighWord", UINT, numParticlesHighWord, - NUM_PARTICLE_TYPES); + NUM_PARTICLE_TYPES); double MassTable[6] = {0., 0., 0., 0., 0., 0.}; writeAttribute(h_grp, "MassTable", DOUBLE, MassTable, NUM_PARTICLE_TYPES); unsigned int flagEntropy[NUM_PARTICLE_TYPES] = {0}; writeAttribute(h_grp, "Flag_Entropy_ICs", UINT, flagEntropy, - NUM_PARTICLE_TYPES); + NUM_PARTICLE_TYPES); writeAttribute(h_grp, "NumFilesPerSnapshot", INT, &numFiles, 1); - + /* Close header */ H5Gclose(h_grp); - + /* Print the code version */ writeCodeDescription(h_file); @@ -624,78 +637,80 @@ void write_output_parallel(struct engine* e, struct UnitSystem* us, /* Print the system of Units */ writeUnitSystem(h_file, us); - /* Loop over all particle types */ for (int ptype = 0; ptype < NUM_PARTICLE_TYPES; ptype++) { - + /* Don't do anything if no particle of this kind */ if (N_total[ptype] == 0) continue; - - /* Add the global information for that particle type to the XMF meta-file */ + + /* Add the global information for that particle type to + * the XMF meta-file */ if (mpi_rank == 0) writeXMFgroupheader(xmfFile, fileName, N_total[ptype], ptype); - + /* Open the particle group in the file */ char partTypeGroupName[PARTICLE_GROUP_BUFFER_SIZE]; snprintf(partTypeGroupName, PARTICLE_GROUP_BUFFER_SIZE, "/PartType%d", - ptype); - h_grp = H5Gcreate(h_file, partTypeGroupName, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); + ptype); + h_grp = H5Gcreate(h_file, partTypeGroupName, H5P_DEFAULT, H5P_DEFAULT, + H5P_DEFAULT); if (h_grp < 0) { error("Error while opening particle group %s.", partTypeGroupName); } - + /* Read particle fields into the particle structure */ switch (ptype) { - - case GAS: - hydro_write_particles(h_grp, fileName, partTypeGroupName, xmfFile, - N[ptype], N_total[ptype], mpi_rank, - offset[ptype], parts, us); - - break; - - case DM: - /* Allocate temporary array */ - if (posix_memalign((void*)&dmparts, gpart_align, - Ndm * sizeof(struct gpart)) != 0) - error("Error while allocating temporart memory for DM particles"); - bzero(dmparts, Ndm * sizeof(struct gpart)); - - /* Collect the DM particles from gpart */ - collect_dm_gparts(gparts, Ntot, dmparts, Ndm); - - /* Write DM particles */ - darkmatter_write_particles(h_grp, fileName, partTypeGroupName, - xmfFile, N[ptype], N_total[ptype], - mpi_rank, offset[ptype], dmparts, us); - - /* Free temporary array */ - free(dmparts); - break; - - default: - error("Particle Type %d not yet supported. Aborting", ptype); + + case GAS: + hydro_write_particles(h_grp, fileName, partTypeGroupName, xmfFile, + N[ptype], N_total[ptype], mpi_rank, offset[ptype], + parts, us); + + break; + + case DM: + /* Allocate temporary array */ + if (posix_memalign((void*)&dmparts, gpart_align, + Ndm * sizeof(struct gpart)) != 0) + error( + "Error while allocating temporart memory for " + "DM particles"); + bzero(dmparts, Ndm * sizeof(struct gpart)); + + /* Collect the DM particles from gpart */ + collect_dm_gparts(gparts, Ntot, dmparts, Ndm); + + /* Write DM particles */ + darkmatter_write_particles(h_grp, fileName, partTypeGroupName, xmfFile, + N[ptype], N_total[ptype], mpi_rank, + offset[ptype], dmparts, us); + + /* Free temporary array */ + free(dmparts); + break; + + default: + error("Particle Type %d not yet supported. Aborting", ptype); } - + /* Close particle group */ H5Gclose(h_grp); - + /* Close this particle group in the XMF file as well */ - if (mpi_rank == 0) - writeXMFgroupfooter(xmfFile, ptype); + if (mpi_rank == 0) writeXMFgroupfooter(xmfFile, ptype); } - + /* Write LXMF file descriptor */ if (mpi_rank == 0) writeXMFoutputfooter(xmfFile, outputCount, e->time); - + /* message("Done writing particles..."); */ - + /* Close property descriptor */ H5Pclose(plist_id); - + /* Close file */ H5Fclose(h_file); - + ++outputCount; } diff --git a/src/parallel_io.h b/src/parallel_io.h index 09d6574309646d33416670e4c726cc7da0321a2e..663f0aabac44c08682b964512839b925673ea5c5 100644 --- a/src/parallel_io.h +++ b/src/parallel_io.h @@ -32,9 +32,9 @@ #if defined(HAVE_HDF5) && defined(WITH_MPI) && defined(HAVE_PARALLEL_HDF5) void read_ic_parallel(char* fileName, double dim[3], struct part** parts, - struct gpart** gparts, size_t* Ngas, size_t* Ngparts, + struct gpart** gparts, size_t* Ngas, size_t* Ngparts, int* periodic, int mpi_rank, int mpi_size, MPI_Comm comm, - MPI_Info info); + MPI_Info info); void write_output_parallel(struct engine* e, struct UnitSystem* us, int mpi_rank, int mpi_size, MPI_Comm comm, diff --git a/src/serial_io.c b/src/serial_io.c index c3878061464adb8a90aba149ab3397b03ac84772..40bd2b1c8921f4acbfa0950984d6915ebd3d241e 100644 --- a/src/serial_io.c +++ b/src/serial_io.c @@ -756,9 +756,10 @@ void write_output_serial(struct engine* e, struct UnitSystem* us, int mpi_rank, /* Don't do anything if no particle of this kind */ if (N_total[ptype] == 0) continue; - /* Add the global information for that particle type to the XMF meta-file */ - if (mpi_rank == 0) - writeXMFgroupheader(xmfFile, fileName, N_total[ptype], ptype); + /* Add the global information for that particle type to the XMF + * meta-file */ + if (mpi_rank == 0) + writeXMFgroupheader(xmfFile, fileName, N_total[ptype], ptype); /* Open the particle group in the file */ char partTypeGroupName[PARTICLE_GROUP_BUFFER_SIZE]; @@ -805,9 +806,8 @@ void write_output_serial(struct engine* e, struct UnitSystem* us, int mpi_rank, /* Close particle group */ H5Gclose(h_grp); - /* Close this particle group in the XMF file as well */ - if (mpi_rank == 0) - writeXMFgroupfooter(xmfFile, ptype); + /* Close this particle group in the XMF file as well */ + if (mpi_rank == 0) writeXMFgroupfooter(xmfFile, ptype); } /* Close file */