Debian Codegraph @

src/application_map.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 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
20 #include "music/application_map.hh"
21 #if MUSIC_USE_MPI
22 #include "music/mpi_utils.hh"
23 #endif
24 #include "music/ioutils.hh"
25 #include <iostream>
26 #include <fstream>
27 namespace MUSIC {
28
29 ApplicationMap::ApplicationMap ()
30 {
31 }
32
33 int
34 ApplicationMap::nProcesses ()
35 {
36 int n = 0;
37 for (ApplicationMap::iterator i = begin (); i != end (); ++i)
38 n += i->nProc ();
39 return n;
40 }
41
42
43 ApplicationInfo*
44 ApplicationMap::lookup (std::string appName)
45 {
46 for (iterator i = begin (); i != end (); ++i)
47 {
48 if (i->name () == appName)
49 return &*i;
50 }
51 return 0;
52 }
53
54
55 void
56 ApplicationMap::add (std::string name, int n, int c)
57 {
58 push_back (ApplicationInfo (name, n, c));
59 }
60
61
62 void
63 ApplicationMap::write (std::ostringstream& out)
64 {
65 out << size ();
66 for (iterator i = begin (); i != end (); ++i)
67 {
68 out << ':';
69 IOUtils::write (out, i->name ());
70 out << ':' << i->nProc ();
71 }
72 }
73
74
75 void
76 ApplicationMap::read (std::istringstream& in)
77 {
78 int nApp;
79 in >> nApp;
80
81 #ifdef MUSIC_DEBUG
82 /* for debugging */
83 std::ofstream outfile ("leaders");
84 #endif
85 for (int i = 0; i < nApp; ++i)
86 {
87 in.ignore ();
88 std::string name = IOUtils::read (in);
89 in.ignore ();
90 int np;
91 in >> np;
92 #ifdef MUSIC_DEBUG
93 /* for debugging */
94 outfile<< name << "\t" << aleader << std::endl;
95 #endif
96 add (name, np, i);
97 }
98 #ifdef MUSIC_DEBUG
99 /* for debugging */
100 outfile.close();
101 #endif
102 }
103
104
105 ApplicationInfo*
106 ApplicationMap::lookup (int color)
107 {
108 for (iterator i = begin (); i != end (); ++i)
109 {
110 if (i->color () == color)
111 return &*i;
112 }
113 return 0;
114
115 }
116
117 #if MUSIC_USE_MPI
118 std::map<int, int>
119 ApplicationMap::assignLeaders (std::string my_app_label)
120 {
121 std::map<int, int> leaders;
122
123 int size = mpi_get_comm_size (MPI_COMM_WORLD);
124 int rank = mpi_get_rank (MPI_COMM_WORLD);
125 int *colors = new int[size];
126 colors[rank] = lookup (my_app_label)->color ();
127 MPI_Allgather (MPI_IN_PLACE, 0, MPI_INT, colors, 1, MPI_INT, MPI_COMM_WORLD);
128
129 int prev_color = -1;
130 for (int i = 0; i < size; ++i)
131 {
132 if (colors[i] != prev_color)
133 {
134 ApplicationInfo* info = lookup (colors[i]);
135 info->setLeader (i);
136 leaders[info->color ()] = i;
137 prev_color = colors[i];
138 }
139 }
140
141 #ifdef MUSIC_DEBUG
142 /* block for debugging */
143 std::ofstream outfile ("ranks");
144 for(int i = 0; i < nLeaders; ++i)
145 {
146 outfile<< i <<":";
147 for(int j = 0; j < gsize; ++j)
148 if(rbuf[j] == i) outfile<< " " << j;
149 outfile<<std::endl;
150 }
151 outfile<<std::endl;
152 outfile.close();
153 /*end of block */
154 #endif // MUSIC_DEBUG
155 delete[] colors;
156 return leaders;
157 }
158 #endif
159 }
160