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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26