Debian Codegraph @

mpidep/mpidep.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 2007, 2008, 2009, 2022 INCF
4 *
5 * MUSIC is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * MUSIC is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20
21 #include <string>
22 #include <cstring>
23 #include <sstream>
24 #include <fstream>
25
26 extern "C" {
27 #include <stdlib.h>
28 }
29
30 #include "mpidep.hh"
31
32 // Implementation-dependent code
33
34 #ifndef CRAY_XE6
35 #ifdef HAVE_RTS_GET_PERSONALITY
36 #define BGL
37 #else
38 #ifdef HAVE_OMPI_COMM_FREE
39 #define OPENMPI
40 #else
41 #ifdef HAVE_MPICH2
42 #define MPICH2
43 #else
44 #define MPICH
45 #endif
46 #endif
47 #endif
48 #endif
49
50 #ifdef BGL
51 #include <rts.h>
52 #endif
53
54 #ifdef CRAY_XE6
55 extern "C" {
56 #include <sched.h>
57 }
58 #endif
59
60 /*
61 * Predict the local MPI process rank before having called MPI_Init
62 *
63 * MPI implementations typically passes meta information either through
64 * argc and argv (MPICH) or through the environment (OpenMPI).
65 *
66 * return -1 on failure
67 */
68
69 int
70 getRank (int argc, char *argv[])
71 {
72 #ifdef BGL
73 BGLPersonality p;
74 rts_get_personality (&p, sizeof (p));
75 int pid = rts_get_processor_id ();
76 unsigned int rank, np;
77 rts_rankForCoordinates (p.xCoord, p.yCoord, p.zCoord, pid, &rank, &np);
78 return rank;
79 #endif
80 #ifdef MPICH2
81 char *pmi_rank = getenv("PMI_RANK");
82 int rank;
83 if (pmi_rank==NULL)
84 return -1;
85 else {
86 rank = atoi(pmi_rank);
87 if (rank<0) return -1;
88 return rank;
89 }
90 #endif
91 #ifdef MPICH
92 int seenP4arg = 0;
93 int rank;
94 const std::string rankopt = "-p4rmrank";
95 for (int i = argc - 2; i > 0; --i)
96 {
97 if (!strncmp (argv[i], "-p4", 3))
98 seenP4arg = 1;
99 if (argv[i] == rankopt)
100 {
101 std::istringstream iss (argv[i + 1]);
102 iss >> rank;
103 return rank;
104 }
105 }
106 return seenP4arg ? 0 : -1;
107 #endif
108 #ifdef OPENMPI
109 char* vpid = getenv ("OMPI_MCA_ns_nds_vpid");
110 if (vpid == NULL)
111 vpid = getenv ("OMPI_COMM_WORLD_RANK");
112 if (vpid == NULL)
113 return -1;
114 std::istringstream iss (vpid);
115 int rank;
116 iss >> rank;
117 return rank;
118 #endif
119 #ifdef CRAY_XE6
120 std::ifstream fnid ("/proc/cray_xt/nid");
121 int nid;
122 fnid >> nid;
123 fnid.close ();
124 std::ifstream fnids (getenv ("MUSIC_NODEFILE"));
125 int n = 0;
126 int i;
127 while (fnids)
128 {
129 fnids >> i;
130 if (i == nid)
131 {
132 int core = sched_getcpu ();
133 return 24 * n + core;
134 }
135 ++n;
136 }
137 return -1;
138 #endif
139 }
140
141
142 #ifdef MPICH
143 std::string
144 getSharedDir ()
145 {
146 std::ostringstream dirname;
147 char* musicSharedDir = getenv ("MUSIC_SHARED_DIR");
148 if (musicSharedDir)
149 dirname << musicSharedDir;
150 else
151 {
152 char* home = getenv ("HOME");
153 dirname << home;
154 }
155 return dirname.str ();
156 }
157 #endif
158
159
160 std::string
161 getFirstArg (int argc, char** argv)
162 {
163 for (int i = 1; i < argc; ++i)
164 if (*argv[i] != '-') // skip options
165 return argv[i];
166 return "";
167 }
168
169
170 /*
171 * Retrieve first argument (the name of the music configuration file)
172 * given to the music utility.
173 *
174 * Be robust against rank == -1 (which occurs when getRank fails).
175 */
176
177 std::istream*
178 getConfig (int rank, int argc, char** argv)
179 {
180 #ifdef MPICH
181 std::ostringstream fname;
182 fname << getSharedDir () << "/.musicconf";
183 std::string confname;
184 if (rank == 0)
185 {
186 std::ofstream f (fname.str ().c_str ());
187 confname = getFirstArg (argc, argv);
188 f << confname;
189 }
190 else if (rank > 0)
191 {
192 std::ifstream f (fname.str ().c_str ());
193 f >> confname;
194 }
195 else // rank == -1
196 confname = getFirstArg (argc, argv);
197 return new std::ifstream (confname.c_str ());
198 #else
199 return new std::ifstream (getFirstArg (argc, argv).c_str ());
200 #endif
201 }