@@ -32,79 +32,51 @@ const string CSU2FileWriter::fileExt = ".csv";
3232CSU2FileWriter::CSU2FileWriter (string valFileName, CParallelDataSorter *valDataSorter) :
3333 CFileWriter(std::move(valFileName), valDataSorter, fileExt){}
3434
35-
36- CSU2FileWriter::~CSU2FileWriter (){
37-
38- }
39-
4035void CSU2FileWriter::Write_Data (){
4136
42- /* --- Local variables ---*/
43-
44- unsigned short iVar;
45- unsigned long iPoint;
46-
4737 ofstream restart_file;
48-
49- int iProcessor;
5038 const vector<string> fieldNames = dataSorter->GetFieldNames ();
5139
5240 /* --- Set a timer for the file writing. ---*/
5341
5442 startTime = SU2_MPI::Wtime ();
5543
56- /* --- Only the master node writes the header. ---*/
44+ /* --- Only the FIRST node writes the header (it does not matter if that is the master) . ---*/
5745
58- if (rank == MASTER_NODE) {
59- restart_file.open (fileName.c_str (), ios::out);
60- restart_file.precision (15 );
46+ if (rank == 0 ) {
47+ restart_file.open (fileName);
6148 restart_file << " \" PointID\" " ;
62- for (iVar = 0 ; iVar < fieldNames.size ()-1 ; iVar++)
63- restart_file << " ,\" " << fieldNames[iVar] << " \" " ;
64- restart_file << " ,\" " << fieldNames[fieldNames.size ()-1 ] << " \" " << endl;
49+ for (auto & field : fieldNames) restart_file << " ,\" " << field << " \" " ;
50+ restart_file << " \n " ;
6551 restart_file.close ();
6652 }
6753
68- #ifdef HAVE_MPI
69- SU2_MPI::Barrier (MPI_COMM_WORLD);
70- #endif
71-
72- /* --- All processors open the file. ---*/
54+ /* --- Serialize the writes to the restart file. ---*/
7355
74- restart_file.open (fileName.c_str (), ios::out | ios::app);
75- restart_file.precision (15 );
76-
77- /* --- Write the restart file in parallel, processor by processor. ---*/
78-
79- unsigned long myPoint = 0 , Global_Index;
80- for (iProcessor = 0 ; iProcessor < size; iProcessor++) {
56+ for (int iProcessor = 0 ; iProcessor < size; iProcessor++) {
8157 if (rank == iProcessor) {
82- for (iPoint = 0 ; iPoint < dataSorter->GetnPoints (); iPoint++) {
58+ restart_file.open (fileName, ios::app);
59+ restart_file.precision (15 );
8360
84- /* --- Global Index of the current point. (note outer loop over procs) --- */
61+ for ( auto iPoint = 0ul ; iPoint < dataSorter-> GetnPoints (); iPoint++) {
8562
86- Global_Index = dataSorter-> GetGlobalIndex (iPoint);
63+ /* --- Write global index of the current point. --- */
8764
88- /* --- Write global index. (note outer loop over procs) --- */
65+ restart_file << dataSorter-> GetGlobalIndex (iPoint);
8966
90- restart_file << Global_Index << " , " ;
91- myPoint++;
67+ /* --- Loop over the variables and write the values to file. ---*/
9268
93- /* --- Loop over the variables and write the values to file ---*/
94-
95- for (iVar = 0 ; iVar < fieldNames.size ()-1 ; iVar++) {
96- restart_file << scientific << dataSorter->GetData (iVar, iPoint) << " , " ;
97- }
98- restart_file << scientific << dataSorter->GetData (fieldNames.size ()-1 , iPoint) << " \n " ;
69+ for (size_t iVar = 0 ; iVar < fieldNames.size (); iVar++)
70+ restart_file << " , " << scientific << dataSorter->GetData (iVar, iPoint);
71+ restart_file << " \n " ;
9972 }
10073
74+ restart_file.close ();
10175 }
102- /* --- Flush the file and wait for all processors to arrive. ---*/
103- restart_file.flush ();
104- #ifdef HAVE_MPI
105- SU2_MPI::Barrier (MPI_COMM_WORLD);
106- #endif
10776
77+ /* --- Wait for iProcessor to finish and close the file. ---*/
78+
79+ SU2_MPI::Barrier (MPI_COMM_WORLD);
10880 }
10981
11082 /* --- Compute and store the write time. ---*/
@@ -120,8 +92,4 @@ void CSU2FileWriter::Write_Data(){
12092 /* --- Compute and store the bandwidth ---*/
12193
12294 bandwidth = fileSize/(1.0e6 )/usedTime;
123-
124- /* --- All processors close the file. ---*/
125-
126- restart_file.close ();
12795}
0 commit comments