xref: /phasta/phSolver/common/phasta.cc (revision ee150812e03b04b642c5c2bd3124e8eb86e98922)
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 initPhCommonVars();
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   outpar.input_mode = -1; //FIXME magic value for streams
91   outpar.output_mode = 1; //FIXME magic value for syncio
92   streamio_set_gr(grs);
93 
94   initPhCommonVars();
95   /* Input data  */
96   ierr = input_fform(ctrl);
97   if(!ierr){
98     sprintf(inpfilename,"%d-procs_case/",size);
99     if( chdir( inpfilename ) ) {
100       cerr << "could not change to the problem directory "
101         << inpfilename << endl;
102       return -1;
103     }
104     MPI_Barrier(MPI_COMM_WORLD);
105     input();
106     /* now we can start the solver */
107     proces();
108   }
109   else{
110     printf("error during reading ascii input \n");
111   }
112   MPI_Barrier(MPI_COMM_WORLD);
113   if ( myrank == 0 ) {
114     printf("phasta.cc - last call before finalize!\n");
115   }
116   return timdat.lstep;
117 }
118 
119 int phasta(phSolver::Input& ctrl, RStream* rs) {
120   fprintf(stderr, "HEY! if you see this email Cameron and tell him "
121       "to implement %s(...) on line %d of %s "
122       "... returning an error\n", __func__, __LINE__, __FILE__);
123   return -1;
124 }
125 
126 int phasta(phSolver::Input& ctrl, GRStream* grs, RStream* rs) {
127   int size,ierr;
128   char inpfilename[100];
129   MPI_Comm_size (MPI_COMM_WORLD, &size);
130   MPI_Comm_rank (MPI_COMM_WORLD, &myrank);
131 
132   workfc.numpe = size;
133   workfc.myrank = myrank;
134   outpar.input_mode = -1; //FIXME magic value for streams
135   outpar.output_mode = -1; //FIXME magic value for streams
136   streamio_set_gr(grs);
137   streamio_set_r(rs);
138 
139   initPhCommonVars();
140   /* Input data  */
141   ierr = input_fform(ctrl);
142   if(!ierr){
143     sprintf(inpfilename,"%d-procs_case/",size);
144     if( chdir( inpfilename ) ) {
145       cerr << "could not change to the problem directory "
146         << inpfilename << endl;
147       return -1;
148     }
149     MPI_Barrier(MPI_COMM_WORLD);
150     input();
151     /* now we can start the solver */
152     proces();
153   }
154   else{
155     printf("error during reading ascii input \n");
156   }
157   MPI_Barrier(MPI_COMM_WORLD);
158   if ( myrank == 0 ) {
159     printf("phasta.cc - last call before finalize!\n");
160   }
161   if( cdToParent() )
162     return -1;
163   return timdat.lstep;
164 }
165 
166 int phasta( int argc, char *argv[] ) {
167     int size,ierr;
168     char inpfilename[100];
169     char* pauseDebugger = getenv("catchDebugger");
170     MPI_Comm_size (MPI_COMM_WORLD, &size);
171     MPI_Comm_rank (MPI_COMM_WORLD, &myrank);
172 
173     workfc.numpe = size;
174     workfc.myrank = myrank;
175 
176 #if (defined WIN32)
177     if(argc > 2 ){
178       catchDebugger();
179     }
180 #endif
181 #if (1) // ALWAYS ( defined LAUNCH_GDB ) && !( defined WIN32 )
182 
183     if ( pauseDebugger ) {
184 
185         int parent_pid = getpid();
186         int gdb_child = fork();
187         cout << "gdb_child" << gdb_child << endl;
188 
189         if( gdb_child == 0 ) {
190 
191             cout << "Debugger Process initiating" << endl;
192             stringstream exec_string;
193 
194 #if ( defined decalp )
195             exec_string <<"xterm -e idb "
196                         << " -pid "<< parent_pid <<" "<< argv[0] << endl;
197 #endif
198 #if ( defined LINUX )
199             exec_string <<"xterm -e gdb"
200                         << " -pid "<< parent_pid <<" "<< argv[0] << endl;
201 #endif
202 #if ( defined SUN4 )
203             exec_string <<"xterm -e dbx "
204                         << " - "<< parent_pid <<" "<< argv[0] << endl;
205 #endif
206 #if ( defined IRIX )
207             exec_string <<"xterm -e dbx "
208                         << " -p "<< parent_pid <<" "<< argv[0] << endl;
209 #endif
210             string s = exec_string.str();
211             system( s.c_str() );
212             exit(0);
213         }
214         catchDebugger();
215     }
216 
217 #endif
218 
219     /* Input data  */
220     if(argc > 1 ){
221         strcpy(inpfilename,argv[1]);
222     } else {
223         strcpy(inpfilename,"solver.inp");
224     }
225     string defaultConf = ".";
226     const char* path_to_config = getenv("PHASTA_CONFIG");
227     if(path_to_config)
228       defaultConf = path_to_config;
229     defaultConf.append("/input.config");
230     string userConf(inpfilename);
231     phSolver::Input ctrl(userConf, defaultConf);
232     initPhCommonVars();
233     ierr = input_fform(ctrl);
234     if(!ierr){
235       sprintf(inpfilename,"%d-procs_case/",size);
236       if( chdir( inpfilename ) ) {
237         cerr << "could not change to the problem directory "
238           << inpfilename << endl;
239         return -1;
240       }
241       MPI_Barrier(MPI_COMM_WORLD);
242       setIOparam();
243       outpar.input_mode = outpar.nsynciofiles; //FIXME this is awful
244       outpar.output_mode = outpar.nsynciofiles; //FIXME this is awful
245       input();
246       /* now we can start the solver */
247       proces();
248     }
249     else{
250         printf("error during reading ascii input \n");
251     }
252     MPI_Barrier(MPI_COMM_WORLD);
253     if ( myrank == 0 ) {
254       printf("phasta.cc - last call before finalize!\n");
255     }
256     if( cdToParent() )
257       return -1;
258     return timdat.lstep;
259 }
260