Debian Codegraph @

src/distributor.cc

raw
1 /*
2 * This file is part of MUSIC.
3 * Copyright (C) 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 "music/distributor.hh"
20
21 #include "music/debug.hh"
22
23 #if MUSIC_USE_MPI
24
25 // distributor.hh needs to be included first since it causes inclusion
26 // of mpi.h (in data_map.hh). mpi.h must be included before other
27 // header files on BG/L
28
29
30 #include <algorithm>
31 #include <cstring>
32
33 #include "music/event.hh"
34 #include "music/mpi_utils.hh"
35
36 namespace MUSIC {
37
38 Distributor::Interval::Interval (IndexInterval& interval)
39 {
40 setBegin (interval.begin ());
41 setLength (interval.end () - interval.begin ());
42 }
43
44
45 void
46 Distributor::configure (DataMap* dmap)
47 {
48 dataMap = dmap;
49 }
50
51
52 IntervalTree<int, MUSIC::Interval, int>*
53 Distributor::buildTree ()
54 {
55 IntervalTree<int, MUSIC::Interval, int>* tree
56 = new IntervalTree<int, MUSIC::Interval, int> ();
57
58 IndexMap* indices = dataMap->indexMap ();
59 for (IndexMap::iterator i = indices->begin ();
60 i != indices->end ();
61 ++i)
62 {
63 MUSIC_LOGR ("adding (" << i->begin () << ", " << i->end ()
64 << ", " << i->local () << ") to tree");
65 tree->add (*i, i->local ());
66 }
67
68 tree->build ();
69
70 return tree;
71 }
72
73
74 void
75 Distributor::addRoutingInterval (IndexInterval interval, FIBO* buffer)
76 {
77 BufferMap::iterator b = buffers.find (buffer);
78 if (b == buffers.end ())
79 {
80 buffers.insert (std::make_pair (buffer, Intervals ()));
81 b = buffers.find (buffer);
82 }
83 Intervals& intervals = b->second;
84 intervals.push_back (Interval (interval));
85 }
86
87
88 void
89 Distributor::IntervalCalculator::operator() (int& offset)
90 {
91 interval_.setBegin (elementSize_
92 * (interval_.begin () - offset));
93 interval_.setLength (elementSize_ * interval_.length ());
94 }
95
96
97 void
98 Distributor::initialize ()
99 {
100 IntervalTree<int, MUSIC::Interval, int>* tree = buildTree ();
101
102 for (BufferMap::iterator b = buffers.begin (); b != buffers.end (); ++b)
103 {
104 FIBO* buffer = b->first;
105 Intervals& intervals = b->second;
106 sort (intervals.begin (), intervals.end ());
107 int elementSize = mpi_get_type_size (dataMap->type ());
108 int size = 0;
109 for (Intervals::iterator i = intervals.begin ();
110 i != intervals.end ();
111 ++i)
112 {
113 IntervalCalculator calculator (*i, elementSize);
114 tree->search (i->begin (), &calculator);
115 size += i->length ();
116 }
117 buffer->configure (size);
118 }
119
120 delete tree;
121 }
122
123
124 void
125 Distributor::distribute ()
126 {
127 for (BufferMap::iterator b = buffers.begin (); b != buffers.end (); ++b)
128 {
129 FIBO* buffer = b->first;
130 Intervals& intervals = b->second;
131 ContDataT* src = static_cast<ContDataT*> (dataMap->base ());
132 ContDataT* dest = static_cast<ContDataT*> (buffer->insert ());
133 for (Intervals::iterator i = intervals.begin ();
134 i != intervals.end ();
135 ++i)
136 {
137 MUSIC_LOGR ("src = " << static_cast<void*> (src)
138 << ", begin = " << i->begin ()
139 << ", length = " << i->length ());
140 memcpy (dest, src + i->begin (), i->length ());
141 dest += i->length ();
142 }
143 }
144 }
145
146 }
147 #endif