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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6939 - (show annotations)
Mon Jan 20 03:37:18 2020 UTC (10 months ago) by uqaeller
File size: 3101 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
19 #include <Python.h>
20 #include <iostream>
21 #include <stdexcept>
22
23 #include <escript/EsysMPI.h>
24
25 #ifdef ESYS_MPI
26
27 int main( int argc, char **argv )
28 {
29 int status = 0;
30 int provided;
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 int rank=0;
43 if (MPI_Comm_rank(MPI_COMM_WORLD, &rank)!=MPI_SUCCESS)
44 {
45 MPI_Abort(MPI_COMM_WORLD, 1);
46 }
47
48 if( rank )
49 {
50 #ifdef _WIN32
51 if ( freopen( "NUL", "w+", stdout ) == NULL ) {
52 exit(EXIT_FAILURE);
53 }
54 #else
55 if ( freopen( "/dev/null", "w+", stdout ) == NULL ) {
56 exit(EXIT_FAILURE);
57 }
58 #endif
59 }
60 /*
61 * Start the python parser
62 */
63
64 #ifdef ESPYTHON3
65 wchar_t** wargv=new wchar_t*[argc+1];
66 for (int i=0;i<argc;++i)
67 {
68 int len=strlen(argv[i]);
69 wargv[i]=new wchar_t[len+1];
70 for (int j=0;j<len;++j)
71 {
72 wargv[i][j]=wchar_t(argv[i][j]);
73 }
74 wargv[i][len]=0;
75 }
76 wargv[argc]=0;
77 status = Py_Main(argc, wargv);
78 #else
79
80
81 status = Py_Main(argc, argv);
82 #endif
83 /*
84 * Close down MPI.
85 * status==1 : uncaught python exception
86 * status==2 : invalid python cmd line
87 * status>2 : supposed to be param of sys.exit()
88 * sys.exit doesn't return as it should.
89 *
90 * I have made an exception for 2 because calling MPI_Abort
91 * can display pretty ugly messages for not typing params
92 * properly.
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 printf( "Esys must be compiled with ESYS_MPI defined to make the MPI version available\n\n" );
133 return 0;
134 }
135 #endif // ESYS_MPI
136

  ViewVC Help
Powered by ViewVC 1.1.26