1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <cassert> 5 6 #define OMPI_SKIP_MPICXX 1 7 #include <mpi.h> 8 9 #ifdef HAVE_PETSC 10 #include <petscsys.h> 11 #include <petscviewer.h> 12 #endif 13 14 #include <sys/types.h> 15 #include <sys/stat.h> 16 17 #if !(defined IOSTREAMH) 18 #include <iostream> 19 #include <sstream> 20 using namespace std; 21 #endif 22 23 #ifdef intel 24 #include <direct.h> 25 #define chdir _chdir 26 #else 27 #include <unistd.h> 28 #endif 29 30 #include "common_c.h" 31 #include "Input.h" 32 #include "phstream.h" 33 #include "streamio.h" 34 35 #include <FCMangle.h> 36 #define input FortranCInterface_GLOBAL_(input,INPUT) 37 #define proces FortranCInterface_GLOBAL_(proces,PROCES) 38 #define timer FortranCInterface_GLOBAL_(timer,TIMER) 39 40 extern "C" char phasta_iotype[80]; 41 char phasta_iotype[80]; 42 43 extern int SONFATH; 44 extern "C" void proces(); 45 extern "C" void input(); 46 extern int input_fform(phSolver::Input&); 47 extern void setIOparam(); // For SyncIO 48 extern "C" void initPhastaCommonVars(); 49 50 int myrank; /* made file global for ease in debugging */ 51 52 void 53 catchDebugger() { 54 while (1) { 55 int debuggerPresent=0; 56 int fakeSTOP = 1; // please stop HERE and assign as next line 57 // assign or set debuggerPresent=1 58 if(debuggerPresent) { 59 break; 60 } 61 } 62 } 63 64 // some useful debugging functions 65 66 void 67 pdarray( void* darray , int start, int end ) { 68 for( int i=start; i < end; i++ ){ 69 cout << ((double*)darray)[i] << endl; 70 } 71 } 72 73 void 74 piarray( void* iarray , int start, int end ) { 75 for( int i=start; i < end; i++ ){ 76 cout << ((int*)iarray)[i] << endl; 77 } 78 } 79 80 namespace { 81 int cdToParent() { 82 if( chdir("..") ) { 83 fprintf(stderr,"could not change to the parent directory\n"); 84 return 1; 85 } else { 86 return 0; 87 } 88 } 89 int run(phSolver::Input& ctrl) { 90 int size,ierr; 91 char inpfilename[100]; 92 MPI_Comm_size (MPI_COMM_WORLD, &size); 93 MPI_Comm_rank (MPI_COMM_WORLD, &myrank); 94 95 workfc.numpe = size; 96 workfc.myrank = myrank; 97 98 initPhastaCommonVars(); 99 /* Input data */ 100 ierr = input_fform(ctrl); 101 if(!ierr){ 102 sprintf(inpfilename,"%d-procs_case/",size); 103 if( chdir( inpfilename ) ) { 104 cerr << "could not change to the problem directory " 105 << inpfilename << endl; 106 return -1; 107 } 108 MPI_Barrier(MPI_COMM_WORLD); 109 input(); 110 /* now we can start the solver */ 111 proces(); 112 } 113 else{ 114 printf("error during reading ascii input \n"); 115 } 116 MPI_Barrier(MPI_COMM_WORLD); 117 if ( myrank == 0 ) { 118 printf("phasta.cc - last call before finalize!\n"); 119 } 120 if( cdToParent() ) 121 return -1; 122 return timdat.lstep; 123 } 124 } 125 126 int phasta(phSolver::Input& ctrl) { 127 outpar.input_mode = 0; //FIXME magic value for posix 128 outpar.output_mode = 0; //FIXME magic value for posix 129 return run(ctrl); 130 } 131 132 int phasta(phSolver::Input& ctrl, grstream grs) { 133 assert(grs); 134 outpar.input_mode = -1; //FIXME magic value for streams 135 outpar.output_mode = 1; //FIXME magic value for syncio 136 streamio_set_gr(grs); 137 return run(ctrl); 138 } 139 140 int phasta(phSolver::Input& ctrl, RStream* rs) { 141 fprintf(stderr, "HEY! if you see this email Cameron and tell him " 142 "to implement %s(...) on line %d of %s " 143 "... returning an error\n", __func__, __LINE__, __FILE__); 144 return -1; 145 } 146 147 int phasta(phSolver::Input& ctrl, GRStream* grs, RStream* rs) { 148 outpar.input_mode = -1; //FIXME magic value for streams 149 outpar.output_mode = -1; //FIXME magic value for streams 150 assert(grs); 151 assert(rs); 152 streamio_set_gr(grs); 153 streamio_set_r(rs); 154 return run(ctrl); 155 } 156 157 int phasta( int argc, char *argv[] ) { 158 int size,ierr; 159 char inpfilename[100]; 160 char* pauseDebugger = getenv("catchDebugger"); 161 MPI_Comm_size (MPI_COMM_WORLD, &size); 162 MPI_Comm_rank (MPI_COMM_WORLD, &myrank); 163 164 #ifdef HAVE_PETSC 165 PETSC_COMM_WORLD=MPI_COMM_WORLD; 166 PetscInitialize(&argc,&argv,PETSC_NULL,PETSC_NULL); 167 PetscInitializeFortran(); 168 PetscPopSignalHandler(); //Let us segfault in peace ;-) 169 // ok with Master PetscOptionsView(NULL,PETSC_VIEWER_STDOUT_WORLD); 170 // ok with 3.6x PetscOptionsView(PETSC_VIEWER_STDOUT_WORLD); 171 PetscOptionsView(PETSC_VIEWER_STDOUT_WORLD); 172 if(sizeof(PetscInt) != sizeof(long long int)) 173 { 174 //PetscInt and gcorp_t (gen_ncorp.c) 175 //must be the same size. hard-coded for now 176 //FIXME 177 if(myrank == 0) 178 { 179 printf("WARNING: PETSc Index Size Mismatch\n"); 180 printf("WARNING: Proceed at your own risk\n"); 181 } 182 } 183 MPI_Barrier(MPI_COMM_WORLD); 184 if(myrank == 0) 185 { 186 printf("PETSc Initialized\n"); 187 fflush(stdout); 188 } 189 #endif 190 workfc.numpe = size; 191 workfc.myrank = myrank; 192 193 #if (defined WIN32) 194 if(argc > 2 ){ 195 catchDebugger(); 196 } 197 #endif 198 #if (1) // ALWAYS ( defined LAUNCH_GDB ) && !( defined WIN32 ) 199 200 if ( pauseDebugger ) { 201 202 int parent_pid = getpid(); 203 int gdb_child = fork(); 204 cout << "gdb_child" << gdb_child << endl; 205 206 if( gdb_child == 0 ) { 207 208 cout << "Debugger Process initiating" << endl; 209 stringstream exec_string; 210 211 #if ( defined decalp ) 212 exec_string <<"xterm -e idb " 213 << " -pid "<< parent_pid <<" "<< argv[0] << endl; 214 #endif 215 #if ( defined LINUX ) 216 exec_string <<"xterm -e gdb" 217 << " -pid "<< parent_pid <<" "<< argv[0] << endl; 218 #endif 219 #if ( defined SUN4 ) 220 exec_string <<"xterm -e dbx " 221 << " - "<< parent_pid <<" "<< argv[0] << endl; 222 #endif 223 #if ( defined IRIX ) 224 exec_string <<"xterm -e dbx " 225 << " -p "<< parent_pid <<" "<< argv[0] << endl; 226 #endif 227 string s = exec_string.str(); 228 system( s.c_str() ); 229 exit(0); 230 } 231 catchDebugger(); 232 } 233 234 #endif 235 236 /* Input data */ 237 if(argc > 1 ){ 238 strcpy(inpfilename,argv[1]); 239 } else { 240 strcpy(inpfilename,"solver.inp"); 241 } 242 string defaultConf = "."; 243 const char* path_to_config = getenv("PHASTA_CONFIG"); 244 if(path_to_config) 245 defaultConf = path_to_config; 246 defaultConf.append("/input.config"); 247 string userConf(inpfilename); 248 phSolver::Input ctrl(userConf, defaultConf); 249 initPhastaCommonVars(); 250 ierr = input_fform(ctrl); 251 if(!ierr){ 252 sprintf(inpfilename,"%d-procs_case/",size); 253 if( chdir( inpfilename ) ) { 254 cerr << "could not change to the problem directory " 255 << inpfilename << endl; 256 return -1; 257 } 258 MPI_Barrier(MPI_COMM_WORLD); 259 setIOparam(); 260 outpar.input_mode = outpar.nsynciofiles; //FIXME this is awful 261 outpar.output_mode = outpar.nsynciofiles; //FIXME this is awful 262 input(); 263 /* now we can start the solver */ 264 proces(); 265 } 266 else{ 267 printf("error during reading ascii input \n"); 268 } 269 #ifdef HAVE_PETSC 270 PetscFinalize(); 271 #endif 272 MPI_Barrier(MPI_COMM_WORLD); 273 if ( myrank == 0 ) { 274 printf("phasta.cc - last call before finalize!\n"); 275 } 276 if( cdToParent() ) 277 return -1; 278 return timdat.lstep; 279 } 280