/[escript]/trunk/pythonMPI/src/ScriptMPIredirect.cpp
ViewVC logotype

Contents of /trunk/pythonMPI/src/ScriptMPIredirect.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6939 - (show annotations)
Mon Jan 20 03:37:18 2020 UTC (18 months, 1 week ago) by uqaeller
File size: 3754 byte(s)
Updated the copyright header.


1
2 /*****************************************************************************
3 *
4 * Copyright (c) 2003-2020 by The University of Queensland
5 * http://www.uq.edu.au
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Apache License, version 2.0
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 * Development 2012-2013 by School of Earth Sciences
13 * Development from 2014-2017 by Centre for Geoscience Computing (GeoComp)
14 * Development from 2019 by School of Earth and Environmental Sciences
15 **
16 *****************************************************************************/
17
18 #include <Python.h>
19 #include <iostream>
20 #include <stdexcept>
21
22 #include <escript/EsysMPI.h>
23
24 #ifdef ESYS_MPI
25
26 int main(int argc, char **argv)
27 {
28 int status = 0;
29 int provided;
30 try
31 {
32 /*
33 * Initialise MPI
34 */
35 /* status = MPI_Init(&argc, &argv); */
36 status = MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided );
37 if (status != MPI_SUCCESS) {
38 std::cerr << argv[0] << ": MPI_Init failed, exiting." << std::endl;
39 return status;
40 }
41 int rank=0;
42 if (MPI_Comm_rank(MPI_COMM_WORLD, &rank)!=MPI_SUCCESS)
43 {
44 MPI_Abort(MPI_COMM_WORLD, 1);
45 }
46
47 if( rank )
48 {
49 char fname[256];
50 sprintf( fname, "stdout_%04d.out", rank );
51 if (freopen( fname, "w+", stdout ) == NULL) {
52 std::cerr << "Warning: Unable to redirect stdout." << std::endl;
53 }
54 sprintf( fname, "stdout_%04d.err", rank );
55 if (freopen(fname, "w+", stderr) == NULL) {
56 std::cerr << "Warning: Unable to redirect stderr." << std::endl;
57 }
58 }
59
60 /*
61 * Start the python parser
62 */
63 #ifdef ESPYTHON3
64 wchar_t** wargv=new wchar_t*[argc+1];
65 for (int i=0;i<argc;++i)
66 {
67 int len=strlen(argv[i]);
68 wargv[i]=new wchar_t[len+1];
69 for (int j=0;j<len;++j)
70 {
71 wargv[i][j]=wchar_t(argv[i][j]);
72 }
73 wargv[i][len]=0;
74 }
75 wargv[argc]=0;
76 status = Py_Main(argc, wargv);
77 #else
78 status = Py_Main(argc, argv);
79 #endif
80
81 /*
82 * Close down MPI.
83 * status==1 : uncaught python exception
84 * status==2 : invalid python cmd line
85 * status>2 : supposed to be param of sys.exit()
86 * sys.exit doesn't return as it should.
87 *
88 * I have made an exception for 2 because calling MPI_Abort
89 * can display pretty ugly messages for not typing params
90 * properly.
91 * Yes that means you probably shouldn't use 2 as an exit code
92 * but we don't really recommend sys.exit anyway.
93 */
94 if ((status!=0) && (status!=2))
95 {
96 MPI_Abort(MPI_COMM_WORLD,status);
97 }
98 else
99 {
100 /*
101 * Finalise MPI for a clean exit.
102 */
103 MPI_Finalize();
104 }
105 }
106 catch (std::runtime_error &e)
107 {
108 std::cerr << "EXCEPTION: " << e.what() << std::endl;
109 MPI_Abort(MPI_COMM_WORLD,1);
110 throw;
111 }
112 catch (char *e)
113 {
114 std::cerr << "EXCEPTION: " << e << std::endl;
115 MPI_Abort(MPI_COMM_WORLD,1);
116 throw;
117 }
118 catch (...)
119 {
120 std::cerr << "EXCEPTION: " << "UNKNOWN." << std::endl;
121 MPI_Abort(MPI_COMM_WORLD,1);
122 throw;
123 }
124
125 return status;
126 }
127
128 #else
129
130 int main(int argc, char **argv)
131 {
132 std::cout << "Escript must be compiled with ESYS_MPI defined to make the MPI version available" << std::endl;
133 return 0;
134 }
135
136 #endif // ESYS_MPI
137

  ViewVC Help
Powered by ViewVC 1.1.26