File: dist_op_communicator.cpp

package info (click to toggle)
tiledarray 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,568 kB
  • sloc: cpp: 53,449; javascript: 1,599; sh: 393; ansic: 226; python: 223; xml: 195; makefile: 36
file content (337 lines) | stat: -rw-r--r-- 10,187 bytes parent folder | download
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
/*
 *  This file is a part of TiledArray.
 *  Copyright (C) 2013  Virginia Tech
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Justus Calvin
 *  Department of Chemistry, Virginia Tech
 *
 *  dist_op.cpp
 *  Oct 24, 2013
 *
 */

#include "TiledArray/external/madness.h"
#include "unit_test_config.h"

struct DistOpFixture {
  DistOpFixture()
      : group_list(),
        world_group_list(),
        group_did(GlobalFixture::world->unique_obj_id(),
                  GlobalFixture::world->rank() % 2),
        world_did(GlobalFixture::world->unique_obj_id(),
                  GlobalFixture::world->size()) {
    for (ProcessID p = GlobalFixture::world->rank() % 2;
         p < GlobalFixture::world->size(); p += 2)
      group_list.push_back(p);
    for (ProcessID p = 0; p < GlobalFixture::world->size(); ++p)
      world_group_list.push_back(p);
  }

  ~DistOpFixture() { GlobalFixture::world->gop.fence(); }

  std::vector<ProcessID> group_list;
  std::vector<ProcessID> world_group_list;
  madness::DistributedID group_did;
  madness::DistributedID world_did;

};  // DistOpFixture

struct SyncTester {
  TiledArray::Future<int> f;

  SyncTester() : f() {}
  SyncTester(const SyncTester& other) : f(other.f) {}

  void operator()() { f.set(GlobalFixture::world->size()); }
};  // struct sync_tester

template <typename T>
struct plus {
  typedef T result_type;
  typedef T argument_type;

  result_type operator()() const { return result_type(); }

  void operator()(result_type& result, const argument_type& arg) const {
    result += arg;
  }

  void operator()(result_type& result, const argument_type& arg1,
                  const argument_type& arg2) const {
    result += arg1 + arg2;
  }
};

BOOST_FIXTURE_TEST_SUITE(dist_op_suite, DistOpFixture)

BOOST_AUTO_TEST_CASE(ring_send_recv) {
  // Send messages in a ring.
  const ProcessID left_neighbor(
      (GlobalFixture::world->rank() + GlobalFixture::world->size() - 1) %
      GlobalFixture::world->size());
  const ProcessID right_neighbor(
      (GlobalFixture::world->rank() + GlobalFixture::world->size() + 1) %
      GlobalFixture::world->size());

  // Get the Future that will hold the remote data
  TiledArray::Future<int> remote_data;
  BOOST_REQUIRE_NO_THROW(
      remote_data = GlobalFixture::world->gop.recv<int>(right_neighbor, 0));

  // Send a future to the right neighbor
  TiledArray::Future<int> local_data;
  BOOST_REQUIRE_NO_THROW(
      GlobalFixture::world->gop.send(left_neighbor, 0, local_data));

  // Set the local data, which will be forwarded to the right neighbor
  local_data.set(GlobalFixture::world->rank());

  // Check that the message was received
  BOOST_CHECK_EQUAL(remote_data.get(), right_neighbor);
}

BOOST_AUTO_TEST_CASE(lazy_sync) {
  SyncTester sync_tester;
  int key = 1;

  // Start the lazy sync
  BOOST_REQUIRE_NO_THROW(GlobalFixture::world->gop.lazy_sync(key, sync_tester));

  // Test for completion
  BOOST_CHECK_EQUAL(sync_tester.f.get(), GlobalFixture::world->size());
}

BOOST_AUTO_TEST_CASE(lazy_sync_group) {
  // Create broadcast group
  madness::Group group(*GlobalFixture::world, group_list, group_did);

  SyncTester sync_tester;
  int key = 1;

  // Start the lazy sync
  BOOST_REQUIRE_NO_THROW(
      GlobalFixture::world->gop.lazy_sync(key, sync_tester, group));

  // Test for completion
  BOOST_CHECK_EQUAL(sync_tester.f.get(), GlobalFixture::world->size());

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_CASE(lazy_sync_world_group) {
  // Create broadcast group
  madness::Group group(*GlobalFixture::world, world_group_list, world_did);

  SyncTester sync_tester;
  int key = 1;

  // Start the lazy sync
  BOOST_REQUIRE_NO_THROW(
      GlobalFixture::world->gop.lazy_sync(key, sync_tester, group));

  // Test for completion
  BOOST_CHECK_EQUAL(sync_tester.f.get(), GlobalFixture::world->size());

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_CASE(bcast_world) {
  // Pick a random root
  const ProcessID root = 101 % GlobalFixture::world->size();

  // Setup the broadcast
  TiledArray::Future<int> data;
  BOOST_REQUIRE_NO_THROW(GlobalFixture::world->gop.bcast(0, data, root));

  // Set the data on the root process, which will initiate the broadcast.
  if (GlobalFixture::world->rank() == root) data.set(42);

  // Check that all processes got the same message.
  BOOST_CHECK_EQUAL(data.get(), 42);
}

BOOST_AUTO_TEST_CASE(bcast_group) {
  // Create broadcast group
  madness::Group group(*GlobalFixture::world, group_list, group_did);

  // Pick a random root
  const ProcessID root = 101 % group.size();

  // Setup the group broadcast
  TiledArray::Future<int> data;
  BOOST_REQUIRE_NO_THROW(GlobalFixture::world->gop.bcast(0, data, root, group));

  // Set the data on the root process, which will initiate the broadcast.
  if (group.rank() == root) data.set(42 + (GlobalFixture::world->rank() % 2));

  // Check that all processes in the group got the same message.
  BOOST_CHECK_EQUAL(data.get(), 42 + (GlobalFixture::world->rank() % 2));

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_CASE(bcast_world_group) {
  // Create broadcast group
  madness::Group group(*GlobalFixture::world, world_group_list, world_did);

  // Pick a random root
  const ProcessID root = 101 % group.size();

  // Setup the group broadcast
  TiledArray::Future<int> data;
  BOOST_REQUIRE_NO_THROW(GlobalFixture::world->gop.bcast(0, data, root, group));

  // Set the data which will initiate the broadcast
  if (GlobalFixture::world->rank() == root) data.set(42);

  // Check that all processes in the group got the same message.
  BOOST_CHECK_EQUAL(data.get(), 42);

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_CASE(reduce_world) {
  // Pick a random root
  const ProcessID root = 101 % GlobalFixture::world->size();

  // Setup the reduction
  TiledArray::Future<int> data;
  TiledArray::Future<int> result;
  BOOST_REQUIRE_NO_THROW(
      result = GlobalFixture::world->gop.reduce(0, data, plus<int>(), root));

  // Set the local value to be reduced
  data.set(42);

  // Check that the result has been reduced to the root process
  if (GlobalFixture::world->rank() == root)
    BOOST_CHECK_EQUAL(result.get(), GlobalFixture::world->size() * 42);
  else
    BOOST_CHECK(result.is_default_initialized());
}

BOOST_AUTO_TEST_CASE(reduce_group) {
  // Create reduction group
  madness::Group group(*GlobalFixture::world, group_list, group_did);

  // Pick a random root
  const ProcessID root = 101 % group.size();

  // Setup the reduction
  TiledArray::Future<int> data;
  TiledArray::Future<int> result;
  BOOST_REQUIRE_NO_THROW(result = GlobalFixture::world->gop.reduce(
                             0, data, plus<int>(), root, group));

  // Set the local value to be reduced
  data.set(42);

  // Check that the result has been reduced to the root process
  if (group.rank() == root)
    BOOST_CHECK_EQUAL(result.get(), group.size() * 42);
  else
    BOOST_CHECK(result.is_default_initialized());

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_CASE(reduce_world_group) {
  // Create reduction group
  madness::Group group(*GlobalFixture::world, world_group_list, world_did);

  // Pick a random root
  const ProcessID root = 101 % group.size();

  // Setup the reduction
  TiledArray::Future<int> data;
  TiledArray::Future<int> result;
  BOOST_REQUIRE_NO_THROW(result = GlobalFixture::world->gop.reduce(
                             0, data, plus<int>(), root, group));

  // Set the local value to be reduced
  data.set(42);

  // Check that the result has been reduced to the root process
  if (group.rank() == root)
    BOOST_CHECK_EQUAL(result.get(), group.size() * 42);
  else
    BOOST_CHECK(result.is_default_initialized());

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_CASE(all_reduce_world) {
  // Setup the reduction
  TiledArray::Future<int> data;
  TiledArray::Future<int> result;
  BOOST_REQUIRE_NO_THROW(
      result = GlobalFixture::world->gop.all_reduce(0, data, plus<int>()));

  // Set the local value to be reduced
  data.set(42);

  // Check that the result has been reduced to the root process
  BOOST_CHECK_EQUAL(result.get(), GlobalFixture::world->size() * 42);
}

BOOST_AUTO_TEST_CASE(all_reduce_group) {
  // Create reduction group
  madness::Group group(*GlobalFixture::world, group_list, group_did);

  // Setup the reduction
  TiledArray::Future<int> data;
  TiledArray::Future<int> result;
  BOOST_REQUIRE_NO_THROW(result = GlobalFixture::world->gop.all_reduce(
                             0, data, plus<int>(), group));

  // Set the local value to be reduced
  data.set(42);

  // Check that the result has been reduced to the root process
  BOOST_CHECK_EQUAL(result.get(), group.size() * 42);

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_CASE(all_reduce_world_group) {
  // Create reduction group
  madness::Group group(*GlobalFixture::world, world_group_list, world_did);

  // Setup the reduction
  TiledArray::Future<int> data;
  TiledArray::Future<int> result;
  BOOST_REQUIRE_NO_THROW(result = GlobalFixture::world->gop.all_reduce(
                             0, data, plus<int>(), group));

  // Set the local value to be reduced
  data.set(42);

  // Check that the result has been reduced to the root process
  BOOST_CHECK_EQUAL(result.get(), group.size() * 42);

  // Cleanup the group
  GlobalFixture::world->gop.fence();
}

BOOST_AUTO_TEST_SUITE_END()