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