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