Debian Codegraph @

src/collector.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/collector.hh"
20
21 #include "music/debug.hh"
22
23 #if MUSIC_USE_MPI
24 // collector.hh needs to be included first since it causes inclusion
25 // of mpi.h (in data_map.hh). mpi.h must be included before other
26 // header files on BG/L
27
28
29 #include <algorithm>
30 #include <cstring>
31
32 #include "music/event.hh"
33 #include "music/mpi_utils.hh"
34
35 namespace MUSIC {
36
37 Collector::Interval::Interval (IndexInterval& interval)
38 {
39 setBegin (interval.begin ());
40 setLength (interval.end () - interval.begin ());
41 }
42
43
44 void
45 Collector::configure (DataMap* dmap, int maxsize)
46 {
47 dataMap = dmap;
48 maxsize_ = maxsize;
49 }
50
51
52 IntervalTree<int, MUSIC::Interval, int>*
53 Collector::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 Collector::addRoutingInterval (IndexInterval interval, BIFO* 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 Collector::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 Collector::initialize ()
99 {
100 IntervalTree<int, MUSIC::Interval, int>* tree = buildTree ();
101
102 for (BufferMap::iterator b = buffers.begin (); b != buffers.end (); ++b)
103 {
104 BIFO* 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 MUSIC_LOGX ("searching for " << i->begin ());
115 tree->search (i->begin (), &calculator);
116 size += i->length ();
117 }
118 buffer->configure (size, maxsize_);
119 }
120 }
121
122
123 void
124 Collector::collect (ContDataT* base)
125 {
126 ContDataT* dest = static_cast<ContDataT*> (base);
127 for (BufferMap::iterator b = buffers.begin (); b != buffers.end (); ++b)
128 {
129 BIFO* buffer = b->first;
130 Intervals& intervals = b->second;
131 ContDataT* src = static_cast<ContDataT*> (buffer->next ());
132 if (src == NULL)
133 // Out of data (remote has flushed)
134 return;
135 for (Intervals::iterator i = intervals.begin ();
136 i != intervals.end ();
137 ++i)
138 {
139 MUSIC_LOGX ("collect to dest = " << static_cast<void*> (dest)
140 << ", begin = " << i->begin ()
141 << ", length = " << i->length ());
142
143 memcpy (dest + i->begin (), src, i->length ());
144 src += i->length ();
145 }
146 }
147 }
148
149 void
150 Collector::collect ()
151 {
152 collect (static_cast<ContDataT*> (dataMap->base ()));
153 }
154
155 }
156 #endif