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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6939 - (hide 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 gross 2376
2 jfenwick 3981 /*****************************************************************************
3 gross 2376 *
4 uqaeller 6939 * Copyright (c) 2003-2020 by The University of Queensland
5 jfenwick 3981 * http://www.uq.edu.au
6 gross 2376 *
7     * Primary Business: Queensland, Australia
8 jfenwick 6112 * Licensed under the Apache License, version 2.0
9     * http://www.apache.org/licenses/LICENSE-2.0
10 gross 2376 *
11 jfenwick 3981 * Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 jfenwick 4657 * Development 2012-2013 by School of Earth Sciences
13 uqaeller 6939 * Development from 2014-2017 by Centre for Geoscience Computing (GeoComp)
14     * Development from 2019 by School of Earth and Environmental Sciences
15     **
16 jfenwick 3981 *****************************************************************************/
17 gross 2376
18     #include <Python.h>
19     #include <iostream>
20     #include <stdexcept>
21    
22 caltinay 5997 #include <escript/EsysMPI.h>
23 caltinay 3317
24 jfenwick 3259 #ifdef ESYS_MPI
25 gross 2376
26 caltinay 5997 int main(int argc, char **argv)
27 caltinay 3939 {
28     int status = 0;
29     int provided;
30     try
31 caltinay 3908 {
32 caltinay 3939 /*
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 jfenwick 4844 int rank=0;
42     if (MPI_Comm_rank(MPI_COMM_WORLD, &rank)!=MPI_SUCCESS)
43     {
44     MPI_Abort(MPI_COMM_WORLD, 1);
45     }
46 jfenwick 3892
47 jfenwick 4844 if( rank )
48 caltinay 3939 {
49     char fname[256];
50 jfenwick 4934 sprintf( fname, "stdout_%04d.out", rank );
51 caltinay 3939 if (freopen( fname, "w+", stdout ) == NULL) {
52     std::cerr << "Warning: Unable to redirect stdout." << std::endl;
53     }
54 jfenwick 4934 sprintf( fname, "stdout_%04d.err", rank );
55 caltinay 3939 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 jfenwick 3892 #ifdef ESPYTHON3
64     wchar_t** wargv=new wchar_t*[argc+1];
65 caltinay 3939 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 jfenwick 3892 wargv[i][len]=0;
74 caltinay 3939 }
75 jfenwick 3892 wargv[argc]=0;
76     status = Py_Main(argc, wargv);
77     #else
78 caltinay 3939 status = Py_Main(argc, argv);
79 jfenwick 3892 #endif
80 jfenwick 2703
81 caltinay 3939 /*
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 jfenwick 2703 {
108 caltinay 3939 std::cerr << "EXCEPTION: " << e.what() << std::endl;
109     MPI_Abort(MPI_COMM_WORLD,1);
110     throw;
111     }
112     catch (char *e)
113 jfenwick 2703 {
114 caltinay 3939 std::cerr << "EXCEPTION: " << e << std::endl;
115     MPI_Abort(MPI_COMM_WORLD,1);
116     throw;
117 jfenwick 2703 }
118 caltinay 3939 catch (...)
119     {
120     std::cerr << "EXCEPTION: " << "UNKNOWN." << std::endl;
121     MPI_Abort(MPI_COMM_WORLD,1);
122     throw;
123     }
124 gross 2376
125 caltinay 3939 return status;
126 gross 2376 }
127    
128     #else
129 caltinay 3317
130 caltinay 5997 int main(int argc, char **argv)
131 caltinay 3939 {
132     std::cout << "Escript must be compiled with ESYS_MPI defined to make the MPI version available" << std::endl;
133     return 0;
134 gross 2376 }
135 caltinay 3939
136 caltinay 3317 #endif // ESYS_MPI
137    

  ViewVC Help
Powered by ViewVC 1.1.26