File: reduce_task.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 (212 lines) | stat: -rw-r--r-- 5,036 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
/*
 * 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/>.
 *
 */

#include "TiledArray/reduce_task.h"
#include <functional>
#include "unit_test_config.h"

using namespace TiledArray;
using namespace TiledArray::detail;

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

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

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

  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;
  }
};

struct ReduceTaskFixture {
  ReduceTaskFixture() : world(*GlobalFixture::world), rt(world) {}

  TiledArray::World& world;
  ReduceTask<plus<int> > rt;

};  // struct ReduceTaskFixture

struct ReduceOp {
  typedef int result_type;
  typedef int first_argument_type;
  typedef int second_argument_type;

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

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

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

  void operator()(result_type& result, const first_argument_type& first,
                  const second_argument_type& second) const {
    result += first * second;
  }

  void operator()(result_type& result, const first_argument_type& first1,
                  const second_argument_type& second1,
                  const first_argument_type& first2,
                  const second_argument_type& second2) const {
    result += first1 * second1 + first2 * second2;
  }
};  // struct ReduceOp

struct ReducePairTaskFixture {
  ReducePairTaskFixture() : world(*GlobalFixture::world), rt(world) {}

  TiledArray::World& world;
  ReducePairTask<ReduceOp> rt;

};  // struct ReducePairTaskFixture

BOOST_FIXTURE_TEST_SUITE(reduce_task_suite, ReduceTaskFixture)

BOOST_AUTO_TEST_CASE(reduce_value) {
  int sum = 0;
  for (int i = 0; i < 100; ++i) {
    sum += i;
    rt.add(i);
  }

  Future<int> result = rt.submit();

  BOOST_CHECK_EQUAL(result.get(), sum);
}

BOOST_AUTO_TEST_CASE(reduce_future) {
  std::vector<Future<int> > fut_vec;

  for (int i = 0; i < 100; ++i) {
    Future<int> f;
    fut_vec.push_back(f);
    rt.add(f);
  }

  Future<int> result = rt.submit();

  BOOST_CHECK(!(result.probe()));

  int sum = 0;
  for (int i = 0; i < 99; ++i) {
    sum += i;
    fut_vec[i].set(i);
    BOOST_CHECK(!(result.probe()));
  }

  sum += 99;
  fut_vec[99].set(99);

  world.gop.fence();

  BOOST_CHECK(result.probe());

  BOOST_CHECK_EQUAL(result.get(), sum);
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_FIXTURE_TEST_SUITE(reduce_pair_task_suite, ReducePairTaskFixture)

BOOST_AUTO_TEST_CASE(reduce_value) {
  int sum = 0;
  for (int i = 0; i < 100; ++i) {
    sum += i * i;
    BOOST_CHECK_EQUAL(rt.count(), i);
    rt.add(i, i);
    BOOST_CHECK_EQUAL(rt.count(), i + 1);
  }

  Future<int> result = rt.submit();

  BOOST_CHECK_EQUAL(result.get(), sum);
}

BOOST_AUTO_TEST_CASE(reduce_one_value) {
  BOOST_CHECK_EQUAL(rt.count(), 0);
  rt.add(2, 3);
  BOOST_CHECK_EQUAL(rt.count(), 1);

  Future<int> result = rt.submit();

  BOOST_CHECK_EQUAL(result.get(), 6);
}

BOOST_AUTO_TEST_CASE(reduce_future) {
  std::vector<Future<int> > fut1_vec;
  std::vector<Future<int> > fut2_vec;

  for (int i = 0; i < 100; ++i) {
    Future<int> f1;
    Future<int> f2;
    fut1_vec.push_back(f1);
    fut2_vec.push_back(f2);
    BOOST_CHECK_EQUAL(rt.count(), i);
    rt.add(f1, f2);
    BOOST_CHECK_EQUAL(rt.count(), i + 1);
  }

  Future<int> result = rt.submit();

  BOOST_CHECK(!(result.probe()));

  int sum = 0;
  for (int i = 0; i < 100; ++i) {
    BOOST_CHECK(!(result.probe()));
    sum += i * i;
    fut1_vec[i].set(i);
    fut2_vec[i].set(i);
  }

  BOOST_CHECK_EQUAL(result.get(), sum);
}

BOOST_AUTO_TEST_CASE(reduce_one_future) {
  Future<int> f1;
  Future<int> f2;
  rt.add(f1, f2);

  Future<int> result = rt.submit();

  BOOST_CHECK(!(result.probe()));

  BOOST_CHECK(!(result.probe()));
  f1.set(2);
  f2.set(3);

  BOOST_CHECK_EQUAL(result.get(), 6);
}

BOOST_AUTO_TEST_CASE(reduce_zero) {
  BOOST_CHECK_EQUAL(rt.count(), 0);
  Future<int> result = rt.submit();

  BOOST_CHECK_EQUAL(result.get(), 0);
}

BOOST_AUTO_TEST_SUITE_END()