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