-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathappmap.cpp
376 lines (278 loc) · 8.84 KB
/
appmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// Copyright (c) 2005, 2006, 2008 by CodeSourcery
// Copyright (c) 2013 Stefan Seefeld
// All rights reserved.
//
// This file is part of OpenVSIP. It is made available under the
// license contained in the accompanying LICENSE.GPL file.
#include <vsip/support.hpp>
#include <vsip/map.hpp>
#include <vsip/matrix.hpp>
#include <vsip/initfin.hpp>
#include <ovxx/length.hpp>
#include <ovxx/domain_utils.hpp>
#include <test.hpp>
using namespace ovxx;
/// Get the nth index in a domain.
// Utility to create a processor vector of given size.
// Requires:
// NUM_PROC is number of processors to place in vector.
//
// Returns:
// Vector with NUM_PROC processors.
Vector<processor_type>
create_pvec(
length_type num_proc)
{
Vector<processor_type> pvec(num_proc);
for (index_type i=0; i<num_proc; ++i)
pvec.put(i, (i+1)*10);
return pvec;
}
// Dump appmap to stream.
template <typename MapT>
void
dump_appmap(std::ostream& out,
MapT const& map,
Vector<processor_type> pvec)
{
out << "App_map:\n"
<< " Dim: (" << map.num_subblocks(0) << " x "
<< map.num_subblocks(1) << " x "
<< map.num_subblocks(2) << ")\n"
<< "-----------"
<< std::endl;
for (index_type pi=0; pi<pvec.size(); ++pi)
{
processor_type pr = pvec.get(pi);
index_type sb = map.subblock(pr);
if (sb != no_subblock)
{
for (index_type p=0; p<map.impl_num_patches(sb); ++p)
{
Domain<3> gdom = map.template impl_global_domain<3>(sb, p);
Domain<3> ldom = map.template impl_local_domain<3>(sb, p);
out << " pr=" << pr << " sb=" << sb << " patch=" << p
<< " gdom=" << gdom
<< " ldom=" << ldom
<< std::endl;
}
}
}
}
// Check that local and global indices within a patch are consistent.
template <dimension_type Dim,
typename MapT>
void
check_local_vs_global(
MapT const& map,
index_type sb,
index_type p)
{
Domain<Dim> gdom = map.template impl_global_domain<Dim>(sb, p);
Domain<Dim> ldom = map.template impl_local_domain<Dim>(sb, p);
test_assert(gdom.size() == ldom.size());
length_type dim_num_subblocks[Dim]; // number of subblocks in each dim
length_type dim_sb[Dim]; // local sb in each dim
length_type dim_num_patches[Dim]; // number of patches in each dim
length_type dim_p[Dim]; // local p in each dim
for (dimension_type d=0; d<Dim; ++d)
dim_num_subblocks[d] = map.num_subblocks(d);
vsip::impl::split_tuple(sb, Dim, dim_num_subblocks, dim_sb);
for (dimension_type d=0; d<Dim; ++d)
dim_num_patches[d] = map.impl_subblock_patches(d, dim_sb[d]);
vsip::impl::split_tuple(p, Dim, dim_num_patches, dim_p);
for (dimension_type d=0; d<Dim; ++d)
{
test_assert(gdom[d].length() == ldom[d].length());
for (index_type i=0; i<ldom[d].length(); ++i)
{
index_type gi = gdom[d].impl_nth(i);
index_type li = ldom[d].impl_nth(i);
test_assert(map.impl_local_from_global_index(d, gi) == li);
test_assert(map.impl_global_from_local_index(d, sb, li) == gi);
test_assert(map.impl_dim_subblock_from_index(d, gi) == dim_sb[d]);
test_assert(map.impl_dim_patch_from_index(d, gi) == dim_p[d]);
}
}
Length<Dim> ext = extent(gdom);
for(Index<Dim> idx; valid(ext,idx); next(ext,idx))
{
Index<Dim> g_idx = domain_nth(gdom,idx);
Index<Dim> l_idx = domain_nth(ldom,idx);
test_assert(map.impl_subblock_from_global_index(g_idx) == sb);
test_assert(map.impl_patch_from_global_index(g_idx) == p);
}
}
// Test 1-dimensional applied map.
// Checks that each index in an applied map's distributed domain is in
// one and only one subblock-patch.
template <typename Dist0>
void
tc_appmap(
length_type num_proc,
Domain<1> dom,
Dist0 dist0)
{
dimension_type const dim = 1;
typedef Map<Dist0> map_t;
Vector<processor_type> pvec = create_pvec(num_proc);
map_t map(pvec, dist0);
map.impl_apply(Domain<dim>(dom.length()));
Vector<int> data(dom.length(), 0);
for (index_type pi=0; pi<pvec.size(); ++pi)
{
processor_type pr = pvec.get(pi);
index_type sb = map.subblock(pr);
if (sb != no_subblock)
{
for (index_type p=0; p<map.impl_num_patches(sb); ++p)
{
Domain<dim> gdom = map.template impl_global_domain<dim>(sb, p);
if (gdom.size() > 0)
data(gdom) += 1;
check_local_vs_global<dim>(map, sb, p);
}
}
}
// Check that every element in vector was marked, once.
for (index_type i=0; i<data.size(); ++i)
test_assert(data.get(i) == 1);
}
// Test 2-dimensional applied Map.
// Checks that each index in an applied Map's distributed domain is in one
// and only one subblock-patch.
template <typename Dist0,
typename Dist1>
void
tc_appmap(
length_type num_proc,
Domain<2> dom,
Dist0 dist0,
Dist1 dist1)
{
dimension_type const dim = 2;
typedef Map<Dist0, Dist1> map_t;
Vector<processor_type> pvec = create_pvec(num_proc);
map_t map(pvec, dist0, dist1);
map.impl_apply(dom);
Matrix<int> data(dom[0].length(), dom[1].length(), 0);
for (index_type pi=0; pi<pvec.size(); ++pi)
{
processor_type pr = pvec.get(pi);
index_type sb = map.subblock(pr);
if (sb != no_subblock)
{
for (index_type p=0; p<map.impl_num_patches(sb); ++p)
{
Domain<dim> gdom = map.template impl_global_domain<dim>(sb, p);
data(gdom) += 1;
check_local_vs_global<dim>(map, sb, p);
}
}
}
// cout << "tc_appmap:\n" << data;
// Check that every element in vector was marked, once.
for (index_type r=0; r<data.size(0); ++r)
for (index_type c=0; c<data.size(1); ++c)
test_assert(data.get(r, c) == 1);
}
// Test various 1-dim cases.
void
test_1d_appmap()
{
tc_appmap(4, 3, Block_dist(4));
tc_appmap(4, 16, Block_dist(4));
tc_appmap(4, 17, Block_dist(4));
tc_appmap(4, 18, Block_dist(4));
tc_appmap(4, 19, Block_dist(4));
tc_appmap(4, 15, Cyclic_dist(4, 1));
tc_appmap(4, 15, Cyclic_dist(4, 2));
tc_appmap(4, 15, Cyclic_dist(4, 3));
tc_appmap(4, 15, Cyclic_dist(4, 4));
tc_appmap(4, 16, Cyclic_dist(4, 1));
tc_appmap(4, 16, Cyclic_dist(4, 2));
tc_appmap(4, 16, Cyclic_dist(4, 3));
tc_appmap(4, 16, Cyclic_dist(4, 4));
tc_appmap(4, 17, Cyclic_dist(4, 1));
tc_appmap(4, 17, Cyclic_dist(4, 2));
tc_appmap(4, 17, Cyclic_dist(4, 3));
tc_appmap(4, 17, Cyclic_dist(4, 4));
tc_appmap(4, 16, Cyclic_dist(3, 1));
tc_appmap(4, 16, Cyclic_dist(3, 2));
tc_appmap(4, 16, Cyclic_dist(3, 3));
tc_appmap(4, 16, Cyclic_dist(3, 4));
}
// Test various 2-dim cases.
void
test_2d_appmap()
{
tc_appmap(16, Domain<2>(16, 16), Block_dist(4), Block_dist(4));
tc_appmap(16, Domain<2>(17, 18), Block_dist(4), Block_dist(4));
tc_appmap(16, Domain<2>(16, 19), Block_dist(4), Block_dist(4));
for (index_type i=16; i<20; ++i)
tc_appmap(4, Domain<2>(i, 16), Block_dist(4), Block_dist(1));
for (index_type i=16; i<20; ++i)
{
tc_appmap(12, Domain<2>(i, i+1), Cyclic_dist(4, 1), Cyclic_dist(3, 4));
tc_appmap(12, Domain<2>(i, i+1), Cyclic_dist(4, 2), Cyclic_dist(3, 3));
tc_appmap(12, Domain<2>(i, i+1), Cyclic_dist(4, 3), Cyclic_dist(3, 2));
tc_appmap(12, Domain<2>(i, i+1), Cyclic_dist(4, 4), Cyclic_dist(3, 1));
}
}
void
test_appmap()
{
typedef Map<Block_dist, Block_dist> map_t;
length_type const num_proc = 16;
Vector<processor_type> pvec = create_pvec(num_proc);
map_t map(pvec, Block_dist(4), Block_dist(4));
map.impl_apply(Domain<3>(16, 16, 1));
test_assert(map.impl_num_patches(0) == 1);
test_assert(map.impl_global_domain<3>(0, 0) ==
Domain<3>(Domain<1>(0, 1, 4),
Domain<1>(0, 1, 4),
Domain<1>(0, 1, 1)));
// subblocks are row-major
test_assert(map.impl_num_patches(1) == 1);
test_assert(map.impl_global_domain<3>(1, 0) ==
Domain<3>(Domain<1>(0, 1, 4),
Domain<1>(4, 1, 4),
Domain<1>(0, 1, 1)));
test_assert(map.impl_num_patches(15) == 1);
test_assert(map.impl_global_domain<3>(15, 0) ==
Domain<3>(Domain<1>(12, 1, 4),
Domain<1>(12, 1, 4),
Domain<1>(0, 1, 1)));
}
// Test what happens when number of subblocks > elements, forcing
// multiple subblocks to be empty.
void
test_empty_subblocks()
{
typedef Map<Block_dist> map_t;
length_type const subblocks = 6;
length_type const size = 4;
Vector<processor_type> pvec = create_pvec(subblocks);
map_t map(pvec, Block_dist(subblocks));
map.impl_apply(Domain<1>(4));
length_type sum = 0;
for (index_type i=0; i<subblocks; ++i)
{
std::cout << " i = " << map.impl_subblock_domain<1>(i).size() << std::endl;
test_assert(map.impl_subblock_domain<1>(i).size() == 1 ||
map.impl_subblock_domain<1>(i).size() == 0);
sum += map.impl_subblock_domain<1>(i).size();
}
std::cout << "sum = " << sum << std::endl;
test_assert(sum == size);
}
int
main()
{
vsip::vsipl init;
test_appmap();
test_empty_subblocks();
test_1d_appmap();
test_2d_appmap();
}