schwz  Generated automatically from develop
scatter.hpp (7c9a9f7)
1 
2 /*******************************<SCHWARZ LIB LICENSE>***********************
3 Copyright (c) 2019, the SCHWARZ LIB authors
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
10 1. Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ******************************<SCHWARZ LIB LICENSE>*************************/
33 
34 #ifndef scatter_hpp
35 #define scatter_hpp
36 
37 #include <functional>
38 
39 #include <omp.h>
40 #include <ginkgo/ginkgo.hpp>
41 
42 #include <collective_common.hpp>
43 
44 namespace schwz {
45 
46 
47 template <typename ValueType, typename IndexType>
48 extern void scatter_add_values(const IndexType num_elems,
49  const IndexType *indices,
50  const ValueType *from_array,
51  ValueType *into_array);
52 
53 template <typename ValueType, typename IndexType>
54 extern void scatter_diff_values(const IndexType num_elems,
55  const IndexType *indices,
56  const ValueType *from_array,
57  ValueType *into_array);
58 
59 template <typename ValueType, typename IndexType>
60 extern void scatter_avg_values(const IndexType num_elems,
61  const IndexType *indices,
62  const ValueType *from_array,
63  ValueType *into_array);
64 
65 template <typename ValueType, typename IndexType>
66 extern void scatter_values(const IndexType num_elems, const IndexType *indices,
67  const ValueType *from_array, ValueType *into_array);
68 
69 
70 template <typename ValueType, typename IndexType>
71 struct Scatter : public gko::Operation {
72  Scatter(const IndexType num_elems, const IndexType *indices,
73  const ValueType *from_array, ValueType *into_array,
74  OperationType optype)
75  : num_elems{num_elems},
76  indices{indices},
77  from_array{from_array},
78  into_array{into_array},
79  optype{optype}
80  {}
81 
82  void run(std::shared_ptr<const gko::OmpExecutor>) const override
83  {
84  switch (optype) {
85  case copy:
86 #pragma omp parallel for
87  for (auto i = 0; i < num_elems; ++i) {
88  into_array[indices[i]] = from_array[i];
89  }
90  break;
91  case add:
92 #pragma omp parallel for
93  for (auto i = 0; i < num_elems; ++i) {
94  into_array[indices[i]] = from_array[i] + into_array[indices[i]];
95  }
96  break;
97  case diff:
98 #pragma omp parallel for
99  for (auto i = 0; i < num_elems; ++i) {
100  into_array[indices[i]] = from_array[i] - into_array[indices[i]];
101  }
102  break;
103  case avg:
104 #pragma omp parallel for
105  for (auto i = 0; i < num_elems; ++i) {
106  into_array[indices[i]] =
107  (from_array[i] + into_array[indices[i]]) / 2;
108  }
109  break;
110  default: {
111  std::cout << "Undefined gather operation" << std::endl;
112  break;
113  }
114  }
115  }
116 
117  void run(std::shared_ptr<const gko::CudaExecutor>) const override
118  {
119  switch (optype) {
120  case copy:
121  scatter_values(num_elems, indices, from_array, into_array);
122  break;
123  case add:
124  scatter_add_values(num_elems, indices, from_array, into_array);
125  break;
126  case diff:
127  scatter_diff_values(num_elems, indices, from_array, into_array);
128  break;
129  case avg:
130  scatter_avg_values(num_elems, indices, from_array, into_array);
131  break;
132  default: {
133  std::cout << "Undefined gather operation" << std::endl;
134  break;
135  }
136  }
137  }
138  const IndexType num_elems;
139  const IndexType *indices;
140  const ValueType *from_array;
141  ValueType *into_array;
142  OperationType optype;
143 };
144 
145 #define INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(_macro) \
146  template _macro(float, gko::int32); \
147  template _macro(double, gko::int32); \
148  template _macro(float, gko::int64); \
149  template _macro(double, gko::int64);
150 
151 #define DECLARE_SCATTER(ValueType, IndexType) \
152  struct Scatter<ValueType, IndexType>
153 INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(DECLARE_SCATTER);
154 #undef DECLARE_SCATTER
155 
156 } // namespace schwz
157 
158 
159 #endif // gather_scatter.hpp
Definition: scatter.hpp:71
The Schwarz wrappers namespace.
Definition: comm_helpers.hpp:49