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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4154 - (show annotations)
Tue Jan 22 09:30:23 2013 UTC (6 years, 9 months ago) by jfenwick
File size: 2991 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
17 #include <Python.h>
18 #include <iostream>
19 #include <stdexcept>
20
21 extern "C"{
22 #include "esysUtils/Esys_MPI.h"
23 }
24
25 #ifdef ESYS_MPI
26
27 int main( int argc, char **argv ) {
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 #ifdef _WIN32
47 if ( freopen( "NUL", "w+", stdout ) == NULL ) {
48 exit(EXIT_FAILURE);
49 }
50 #else
51 if ( freopen( "/dev/null", "w+", stdout ) == NULL ) {
52 exit(EXIT_FAILURE);
53 }
54 #endif
55 }
56 /*
57 * Start the python parser
58 */
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
76
77 status = Py_Main(argc, argv);
78 #endif
79 /*
80 * Close down MPI.
81 * status==1 : uncaught python exception
82 * status==2 : invalid python cmd line
83 * status>2 : supposed to be param of sys.exit()
84 * sys.exit doesn't return as it should.
85 *
86 * I have made an exception for 2 because calling MPI_Abort
87 * can display pretty ugly messages for not typing params
88 * properly.
89 */
90 if ((status!=0) && (status!=2))
91 {
92 MPI_Abort(MPI_COMM_WORLD,status);
93 }
94 else
95 {
96 /*
97 * Finalise MPI for a clean exit.
98 */
99 MPI_Finalize();
100 }
101 Esys_MPIInfo_free( mpi_info );
102 }
103 catch (std::runtime_error &e)
104 {
105 std::cerr << "EXCEPTION: " << e.what() << std::endl;
106 MPI_Abort(MPI_COMM_WORLD,1);
107 throw;
108 }
109 catch (char *e)
110 {
111 std::cerr << "EXCEPTION: " << e << std::endl;
112 MPI_Abort(MPI_COMM_WORLD,1);
113 throw;
114 }
115 catch (...)
116 {
117 std::cerr << "EXCEPTION: " << "UNKNOWN." << std::endl;
118 MPI_Abort(MPI_COMM_WORLD,1);
119 throw;
120 }
121
122 return status;
123 }
124
125 #else
126
127 int main( int argc, char **argv ) {
128 printf( "Esys must be compiled with ESYS_MPI defined to make the MPI version available\n\n" );
129 return 0;
130 }
131 #endif // ESYS_MPI
132

  ViewVC Help
Powered by ViewVC 1.1.26