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