From c8e48bab330916c5b7254c3f32de3cf739bc2b33 Mon Sep 17 00:00:00 2001
From: nadolski <nadolski@9a6e40ed-f3a0-4838-9b4a-bf418f78e88d>
Date: Sun, 30 Jun 2013 07:11:08 +0000
Subject: [PATCH] parallel fmap bug fix

---
 tracy/tools/soltracy.cc      | 70 +++++++++++++++++++-----------------
 tracy/tracy/src/soleillib.cc | 10 +++---
 2 files changed, 42 insertions(+), 38 deletions(-)

diff --git a/tracy/tools/soltracy.cc b/tracy/tools/soltracy.cc
index 33f8777..df7585d 100644
--- a/tracy/tools/soltracy.cc
+++ b/tracy/tools/soltracy.cc
@@ -474,6 +474,8 @@ int main(int argc, char *argv[]) {
     if(myid == 0){
 	
 			FILE *outf;
+      FILE *outf_loss;
+      char FmapLossFile[max_str+5]=" ";
 			 
 			// open frequency map file for final results
 			if ((outf = fopen(UserCommandFlag[i]._FmapFlag_fmap_file, "w")) == NULL){
@@ -483,8 +485,6 @@ int main(int argc, char *argv[]) {
 
 			// open frequency map loss file for final results
 			if (UserCommandFlag[i]._FmapFlag_printloss){
-				FILE *outf_loss;
-				char FmapLossFile[max_str+5]=" ";
 				strcpy(FmapLossFile, UserCommandFlag[i]._FmapFlag_fmap_file);
 				strcat(FmapLossFile, ".loss");
 				if ((outf_loss = fopen(FmapLossFile, "w")) == NULL){
@@ -505,24 +505,26 @@ int main(int argc, char *argv[]) {
 				if((fp = fopen(FmapFile, "r")) == NULL)  {
 					fprintf(stdout, "%s: error while opening file.\n", FmapFile);
 					exit_(1);
-					while(fgets(buffer, 80, fp) != NULL){
-						fputs(buffer, outf);
-						buffer[0]='\0';
-					}
-					fclose(fp);
 				}
+				// concatanation of the results,  add current j-file to the master file
+				while(fgets(buffer, 80, fp) != NULL){
+					fputs(buffer, outf);
+					buffer[0]='\0';
+				}
+				fclose(fp);
 
 				if (UserCommandFlag[i]._FmapFlag_printloss){
 					strcpy(FmapLossFile,FmapFile);
 					strcat(FmapLossFile,".loss");
 				
-					if((fp_loss = fopen(FmapFile, "r")) == NULL)  {
-						fprintf(stdout, "%s: error while opening file.\n", FmapFile);
+					if((fp_loss = fopen(FmapLossFile, "r")) == NULL)  {
+						fprintf(stdout, "%s: error while opening file.\n", FmapLossFile);
 						exit_(1);
-						while(fgets(buffer, 80, fp_loss) != NULL){
-							fputs(buffer, outf_loss);
-							buffer[0]='\0';
-						}
+					}
+					// concatanation of the results,  add current j-file to the master file
+					while(fgets(buffer, 80, fp_loss) != NULL){
+						fputs(buffer, outf_loss);
+						buffer[0]='\0';
 					}
 					fclose(fp_loss);
 				}
@@ -573,44 +575,47 @@ int main(int argc, char *argv[]) {
 			UserCommandFlag[i]._FmapdpFlag_emax, 
 			UserCommandFlag[i]._FmapdpFlag_z,
 			UserCommandFlag[i]._FmapdpFlag_diffusion,
-			UserCommandFlag[i]._FmapspFlag_printloss, numprocs, myid);
+			UserCommandFlag[i]._FmapdpFlag_printloss, numprocs, myid);
 
     //Synchronize all cores, till all cores finish fmap of different region,then all cores will proceed.
     MPI_Barrier(MPI_COMM_WORLD);
    
     //Collecting data from all files generated by cores, then write to fmap_p.out
     if(myid == 0){
-			FILE *outf;
+			FILE *outf, *outf_loss;
+			char FmapdpLossFile[max_str+5]=" ";
 			if ((outf = fopen(UserCommandFlag[i]._FmapdpFlag_fmapdp_file, "w")) == NULL){
-				fprintf(stdout, "psoltracy: error while opening file %s\n", UserCommandFlag[i]._FmapdpFlag_fmapdp_file);
+				fprintf(stdout, "psoltracy: error while opening file %s\n", 
+				        UserCommandFlag[i]._FmapdpFlag_fmapdp_file);
 				exit_(1);
 			}
 
 		  // open frequency map loss file for final results
 			if (UserCommandFlag[i]._FmapdpFlag_printloss){
-				FILE *outf_loss;
-				char FmapdpLossFile[max_str+5]=" ";
 				strcpy(FmapdpLossFile, UserCommandFlag[i]._FmapdpFlag_fmapdp_file);
 				strcat(FmapdpLossFile, ".loss");
 				if ((outf_loss = fopen(FmapdpLossFile, "w")) == NULL){
-					fprintf(stdout, "psoltracy: error while opening file %s\n", UserCommandFlag[i]._FmapdpFlag_fmap_file);
+					fprintf(stdout, "psoltracy: error while opening file %s\n", 
+					        UserCommandFlag[i]._FmapdpFlag_fmapdp_file);
 					exit_(1);
 				}
 			}
 
 			FILE *fp, *fp_loss;
 			char buffer[81];
-			char FmapFile[50];
+			char FmapdpFile[50];
+			
 			for(int j = 0; j < numprocs; j++){
-			  FmapFile[0]='\0';
-			  sprintf(FmapFile,"%d",j);
-			  strcat(FmapFile,UserCommandFlag[i]._FmapdpFlag_fmapdp_file);
+			  FmapdpFile[0]='\0';
+			  sprintf(FmapdpFile,"%d",j);
+			  strcat(FmapdpFile, UserCommandFlag[i]._FmapdpFlag_fmapdp_file);
 				
-			  if((fp = fopen(FmapFile, "r")) == NULL){
-					fprintf(stdout, "%s: error while opening file.\n", FmapFile);
+			  if((fp = fopen(FmapdpFile, "r")) == NULL){
+					fprintf(stdout, "%s: error while opening file.\n", FmapdpFile);
 					exit_(1);
 				} 
 
+				// concatanation of the results,  add current j-file to the master file
 				while(fgets(buffer, 80, fp) != NULL){
 					fputs(buffer, outf);
 					buffer[0]='\0';
@@ -621,13 +626,14 @@ int main(int argc, char *argv[]) {
 					strcpy(FmapdpLossFile, FmapdpFile);
 					strcat(FmapdpLossFile,".loss");
 				
-					if((fp_loss = fopen(FmapdpFile, "r")) == NULL)  {
-						fprintf(stdout, "%s: error while opening file.\n", FmapdpFile);
+					if((fp_loss = fopen(FmapdpLossFile, "r")) == NULL)  {
+						fprintf(stdout, "%s: error while opening file.\n", FmapdpLossFile);
 						exit_(1);
-						while(fgets(buffer, 80, fp_loss) != NULL){
-							fputs(buffer, outf_loss);
-							buffer[0]='\0';
-						}
+					}
+					// concatanation of the results,  add current j-file to the master file
+					while(fgets(buffer, 80, fp_loss) != NULL){
+						fputs(buffer, outf_loss);
+						buffer[0]='\0';
 					}
 					fclose(fp_loss);
 				} // if printloss
@@ -1004,7 +1010,7 @@ Based on parts of functions get_param( ) & ID_corr(), etc in nsls-ii_lib.cc
       cout << "N_calls = " << N_calls << "    N_steps = " << N_steps <<endl;
       cout << "Number of quadrupole families used for ID correction:   " << N_Fam << endl; 
       cout << "Quadrupoles used for ID correction: " << endl;
-      for (int k = 0; k < N_Fam; k++)
+      for (k = 0; k < N_Fam; k++)
         fprintf(stdout, "%d\n",Q_Fam[k]);
     } 
     
diff --git a/tracy/tracy/src/soleillib.cc b/tracy/tracy/src/soleillib.cc
index 705fd2f..d244847 100644
--- a/tracy/tracy/src/soleillib.cc
+++ b/tracy/tracy/src/soleillib.cc
@@ -892,12 +892,10 @@ void fmap(const char *FmapFile, long Nbx, long Nby, long Nbtour, double xmax, do
  long lastpos = 1;
  ss_vect<double> x1;
  
- if(printloss){
-	 char FmapLossFile[S_SIZE+5]=" ";
-	 strcpy(FmapLossFile,FmapFile);
-	 strcat(FmapLossFile,".loss");
- }
- 
+ char FmapLossFile[S_SIZE+5]=" ";
+ strcpy(FmapLossFile,FmapFile);
+ strcat(FmapLossFile,".loss");
+
  /* Get time and date */
  time_t aclock;
  time(&aclock);                 /* Get time in seconds */
-- 
GitLab