File: math_partial_reduce.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 (147 lines) | stat: -rw-r--r-- 3,768 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
/*
 *  This file is a part of TiledArray.
 *  Copyright (C) 2014  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
 *  Department of Chemistry, Virginia Tech
 *
 *  math_partial_reduce.cpp
 *  Apr 13, 2014
 *
 */

#include "TiledArray/math/partial_reduce.h"
#include "unit_test_config.h"

using namespace TiledArray;

struct PartialReduceFixture {
  PartialReduceFixture() {
    rand_fill(x, 78);
    rand_fill(y, 27);
    rand_fill(a, 388);
  }

  ~PartialReduceFixture() {}

  template <std::size_t N>
  static void rand_fill(int (&vec)[N], const int seed) {
    GlobalFixture::world->srand(seed);
    for (std::size_t i = 0ul; i < N; ++i)
      vec[i] = GlobalFixture::world->rand() % 101;
  }

  struct Sum {
    typedef void result_type;

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

  struct MultSum {
    typedef void result_type;

    void operator()(int& result, const int left, const int right) const {
      result += left * right;
    }
  };

  static const std::size_t m = TILEDARRAY_LOOP_UNWIND * 2.5;
  static const std::size_t n = TILEDARRAY_LOOP_UNWIND * 4.5;
  int x[m];
  int y[n];
  int a[m * n];

};  // PartialReduceFixture

BOOST_FIXTURE_TEST_SUITE(math_partial_reduce_suite, PartialReduceFixture)

BOOST_AUTO_TEST_CASE(unary_row_reduce) {
  // Create a copy of the result for later use.
  int x_ref[m];
  std::copy(x, x + m, x_ref);

  // Do the partial reduction
  math::row_reduce(m, n, a, x, Sum());

  // Check the result of the partial reduction.
  for (std::size_t i = 0ul; i < m; ++i) {
    int expected = x_ref[i];
    for (std::size_t j = 0ul; j < n; ++j) {
      expected += a[i * n + j];
    }

    BOOST_CHECK_EQUAL(x[i], expected);
  }
}

BOOST_AUTO_TEST_CASE(binary_row_reduce) {
  // Create a copy of the result for later use.
  int x_ref[m];
  std::copy(x, x + m, x_ref);

  // Do the partial reduction
  math::row_reduce(m, n, a, y, x, MultSum());

  // Check the result of the partial reduction.
  for (std::size_t i = 0ul; i < m; ++i) {
    int expected = x_ref[i];
    for (std::size_t j = 0ul; j < n; ++j) {
      expected += a[i * n + j] * y[j];
    }

    BOOST_CHECK_EQUAL(x[i], expected);
  }
}

BOOST_AUTO_TEST_CASE(unary_col_reduce) {
  // Create a copy of the result for later use.
  int y_ref[n];
  std::copy(y, y + n, y_ref);

  // Do the partial reduction
  math::col_reduce(m, n, a, y, Sum());

  // Check the result of the partial reduction.
  for (std::size_t j = 0ul; j < n; ++j) {
    int expected = y_ref[j];
    for (std::size_t i = 0ul; i < m; ++i) {
      expected += a[i * n + j];
    }

    BOOST_CHECK_EQUAL(y[j], expected);
  }
}

BOOST_AUTO_TEST_CASE(binary_col_reduce) {
  // Create a copy of the result for later use.
  int y_ref[n];
  std::copy(y, y + n, y_ref);

  // Do the partial reduction
  math::col_reduce(m, n, a, x, y, MultSum());

  // Check the result of the partial reduction.
  for (std::size_t j = 0ul; j < n; ++j) {
    int expected = y_ref[j];
    for (std::size_t i = 0ul; i < m; ++i) {
      expected += a[i * n + j] * x[i];
    }

    BOOST_CHECK_EQUAL(y[j], expected);
  }
}

BOOST_AUTO_TEST_SUITE_END()