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