1 |
|
2 |
/******************************************************* |
3 |
* |
4 |
* Copyright (c) 2003-2010 by University of Queensland |
5 |
* Earth Systems Science Computational Center (ESSCC) |
6 |
* http://www.uq.edu.au/esscc |
7 |
* |
8 |
* Primary Business: Queensland, Australia |
9 |
* Licensed under the Open Software License version 3.0 |
10 |
* http://www.opensource.org/licenses/osl-3.0.php |
11 |
* |
12 |
*******************************************************/ |
13 |
|
14 |
|
15 |
#include <Python.h> |
16 |
#include <iostream> |
17 |
#include <stdexcept> |
18 |
|
19 |
extern "C"{ |
20 |
#include "esysUtils/Esys_MPI.h" |
21 |
} |
22 |
|
23 |
#ifdef ESYS_MPI |
24 |
|
25 |
int main( int argc, char **argv ) { |
26 |
int status = 0; |
27 |
int provided; |
28 |
Esys_MPIInfo *mpi_info=NULL; |
29 |
try |
30 |
{ |
31 |
/* |
32 |
* Initialise MPI |
33 |
*/ |
34 |
/* status = MPI_Init(&argc, &argv); */ |
35 |
status = MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided ); |
36 |
if (status != MPI_SUCCESS) { |
37 |
std::cerr << argv[0] << ": MPI_Init failed, exiting." << std::endl; |
38 |
return status; |
39 |
} |
40 |
mpi_info = Esys_MPIInfo_alloc( MPI_COMM_WORLD ); |
41 |
|
42 |
if( mpi_info->rank ) |
43 |
{ |
44 |
char fname[256]; |
45 |
sprintf( fname, "stdout_%04d.out", mpi_info->rank ); |
46 |
/*FILE *fp_out =*/ freopen( fname, "w+", stdout ); |
47 |
sprintf( fname, "stdout_%04d.err", mpi_info->rank ); |
48 |
/*FILE *fp_err =*/ freopen( fname, "w+", stderr ); |
49 |
} |
50 |
/* |
51 |
* Start the python parser |
52 |
*/ |
53 |
status = Py_Main(argc, argv); |
54 |
|
55 |
/* |
56 |
* Close down MPI. |
57 |
* status==1 : uncaught python exception |
58 |
* status==2 : invalid python cmd line |
59 |
* status>2 : supposed to be param of sys.exit() |
60 |
* sys.exit doesn't return as it should. |
61 |
* |
62 |
* I have made an exception for 2 because calling MPI_Abort |
63 |
* can display pretty ugly messages for not typing params |
64 |
* properly. |
65 |
* Yes that means you probably shouldn't use 2 as an exit code |
66 |
* but we don't really recommend sys.exit anyway. |
67 |
*/ |
68 |
if ((status!=0) && (status!=2)) |
69 |
{ |
70 |
MPI_Abort(MPI_COMM_WORLD,status); |
71 |
} |
72 |
else |
73 |
{ |
74 |
/* |
75 |
* Finalise MPI for a clean exit. |
76 |
*/ |
77 |
MPI_Finalize(); |
78 |
} |
79 |
|
80 |
Esys_MPIInfo_free( mpi_info ); |
81 |
} |
82 |
catch (std::runtime_error &e) |
83 |
{ |
84 |
std::cerr << "EXCEPTION: " << e.what() << std::endl; |
85 |
MPI_Abort(MPI_COMM_WORLD,1); |
86 |
throw; |
87 |
} |
88 |
catch (char *e) |
89 |
{ |
90 |
std::cerr << "EXCEPTION: " << e << std::endl; |
91 |
MPI_Abort(MPI_COMM_WORLD,1); |
92 |
throw; |
93 |
} |
94 |
catch (...) |
95 |
{ |
96 |
std::cerr << "EXCEPTION: " << "UNKNOWN." << std::endl; |
97 |
MPI_Abort(MPI_COMM_WORLD,1); |
98 |
throw; |
99 |
} |
100 |
|
101 |
return status; |
102 |
} |
103 |
|
104 |
#else |
105 |
|
106 |
int main( int argc, char **argv ) { |
107 |
printf( "Esys must be compiled with ESYS_MPI defined to make the MPI version available\n\n" ); |
108 |
return 0; |
109 |
} |
110 |
#endif // ESYS_MPI |
111 |
|