00001
00002
00003 #include "cddefines.h"
00004 #include "mpi_utilities.h"
00005 #include "save.h"
00006 #include "dynamics.h"
00007 #include "grid.h"
00008
00009 #ifndef MPI_ENABLED
00010
00011 namespace MPI
00012 {
00013 t_MPI COMM_WORLD;
00014 }
00015
00016 #endif
00017
00018
00019
00020 void load_balance::init(int nJobs)
00021 {
00022 if( nJobs <= 0 )
00023 return;
00024
00025 bool lgMPI = cpu.i().lgMPI();
00026
00027 p_jobs.resize( nJobs );
00028 if( lgMPI )
00029 p_ptr = MPI::COMM_WORLD.Get_rank();
00030 else
00031 p_ptr = 0;
00032
00033
00034 if( p_ptr == 0 )
00035 {
00036 for( int i=0; i < nJobs; ++i )
00037 p_jobs[i] = i;
00038
00039 if ( lgMPI )
00040 {
00041
00042
00043 srand( unsigned( time(NULL) ) );
00044 random_shuffle( p_jobs.begin(), p_jobs.end() );
00045 }
00046 }
00047
00048 if( lgMPI )
00049 MPI::COMM_WORLD.Bcast( &p_jobs[0], nJobs, MPI::type(p_jobs[0]), 0 );
00050 }
00051
00052 STATIC void check_grid_file( const string& fnam, int j, int ipPun );
00053
00055 void process_output()
00056 {
00057 DEBUG_ENTRY( "process_output()" );
00058
00059
00060
00061 try
00062 {
00063 string main_input = save.chRedirectPrefix + ".in";
00064 string main_output = save.chRedirectPrefix + ".out";
00065
00066
00067 FILE* main_output_handle = open_data( main_output.c_str(), "a", AS_LOCAL_ONLY );
00068 for( int j=0; j < grid.totNumModels; ++j )
00069 {
00070 string Base = GridPointPrefix(j) + save.chRedirectPrefix;
00071 string in_name = Base + ".in";
00072 remove( in_name.c_str() );
00073 string out_name = Base + ".out";
00074 append_file( main_output_handle, out_name.c_str() );
00075 remove( out_name.c_str() );
00076 }
00077 fclose( main_output_handle );
00078
00079 fstream main_input_handle;
00080 open_data( main_input_handle, main_input.c_str(), mode_r, AS_LOCAL_ONLY );
00081 string line;
00082
00083 int ipPun = 0;
00084
00085 while( getline( main_input_handle, line ) )
00086 {
00087 string caps_line;
00088
00089 string::const_iterator p = line.begin();
00090 while( p != line.end() )
00091 caps_line.push_back( toupper(*p++) );
00092 if( caps_line.compare( 0, 4, "SAVE" ) == 0 || caps_line.compare( 0, 4, "PUNC" ) == 0 )
00093 {
00094 ASSERT( ipPun < save.nsave );
00095 string fnam = save.chFilenamePrefix;
00096 string::size_type p = line.find( '"' );
00097 fnam += line.substr( ++p );
00098 fnam.erase( fnam.find( '"' ) );
00099
00100 for( int j=0; j < grid.totNumModels; ++j )
00101 check_grid_file( fnam, j, ipPun );
00102
00103 FILE *dest = open_data( fnam.c_str(), "ab", AS_LOCAL_ONLY_TRY );
00104 if( dest != NULL )
00105 {
00106 if( save.lgSaveToSeparateFiles[ipPun] )
00107 {
00108
00109
00110
00111
00112 string gridnam = GridPointPrefix(0) + fnam;
00113 append_file( dest, gridnam.c_str() );
00114 fclose( dest );
00115 dest = NULL;
00116
00117 rename( fnam.c_str(), gridnam.c_str() );
00118 }
00119 else
00120 {
00121
00122 for( int j=0; j < grid.totNumModels; ++j )
00123 {
00124 string gridnam = GridPointPrefix(j) + fnam;
00125 append_file( dest, gridnam.c_str() );
00126 remove( gridnam.c_str() );
00127 }
00128 }
00129 if( caps_line.find( "XSPE", 4 ) != string::npos )
00130 {
00131
00132 ASSERT( save.FITStype[ipPun] >= 0 &&
00133 save.FITStype[ipPun] < NUM_OUTPUT_TYPES );
00134 saveFITSfile( dest, save.FITStype[ipPun] );
00135 fseek( dest, 0, SEEK_END );
00136 ASSERT( ftell(dest)%2880 == 0 );
00137 }
00138 if( dest != NULL )
00139 {
00140 fclose( dest );
00141 }
00142 }
00143 else
00144 {
00145 fprintf( ioQQQ, "PROBLEM - could not open file %s\n", fnam.c_str() );
00146 }
00147 ++ipPun;
00148 }
00149 }
00150 }
00151 catch( ... )
00152 {
00153 fprintf( ioQQQ, "PROBLEM - an internal error occurred while post-processing the grid output\n" );
00154 }
00155 }
00156
00158 STATIC void check_grid_file( const string& fnam, int j, int ipPun )
00159 {
00160 DEBUG_ENTRY( "check_grid_file()" );
00161
00162
00163 if( save.lgFITS[ipPun] )
00164 return;
00165
00166 bool lgForceNoDelimiter = false;
00167
00168 if( !save.lgHashEndIter[ipPun] || !save.lg_separate_iterations[ipPun] ||
00169 dynamics.lgTimeDependentStatic || strcmp( save.chHashString , "TIME_DEP" ) == 0 )
00170 lgForceNoDelimiter = true;
00171
00172 bool lgAppendDelimiter = true;
00173 bool lgAppendNewline = false;
00174 string gridnam = GridPointPrefix(j) + fnam;
00175 fstream str;
00176 open_data( str, gridnam.c_str(), mode_r, AS_LOCAL_ONLY_TRY );
00177 if( str.is_open() )
00178 {
00179 str.seekg( 0, ios_base::end );
00180 if( str.good() && str.tellg() > 0 )
00181 {
00182
00183 str.seekg( -1, ios_base::cur );
00184 char chr;
00185 str.get( chr );
00186 lgAppendNewline = ( chr != '\n' );
00187
00188 string line;
00189 str.seekg( 0, ios_base::beg );
00190 while( getline( str, line ) )
00191 {
00192 if( line.find( "GRID_DELIMIT" ) != string::npos )
00193 lgAppendDelimiter = false;
00194 }
00195 }
00196 str.close();
00197 }
00198 if( lgForceNoDelimiter )
00199 lgAppendDelimiter = false;
00200 if( lgAppendNewline || lgAppendDelimiter )
00201 {
00202 open_data( str, gridnam.c_str(), mode_a, AS_LOCAL_ONLY_TRY );
00203 if( str.is_open() )
00204 {
00205 if( lgAppendNewline )
00206 str << endl;
00207 if( lgAppendDelimiter )
00208 {
00209 str << save.chHashString << " GRID_DELIMIT -- grid";
00210 str << setfill( '0' ) << setw(9) << j << endl;
00211 }
00212 str.close();
00213 }
00214 }
00215 }
00216
00218 void append_file( FILE *dest, const char *source )
00219 {
00220 DEBUG_ENTRY( "append_file()" );
00221
00222 FILE *src = open_data( source, "rb", AS_LOCAL_ONLY_TRY );
00223 if( src == NULL )
00224 return;
00225 int chr = fgetc(src);
00226 while( ! feof(src) )
00227 {
00228 fputc( chr, dest );
00229 chr = fgetc(src);
00230 }
00231 fclose(src);
00232 return;
00233 }