schwz  Generated automatically from develop
gather.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 gather_hpp
35 #define gather_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 gather_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 gather_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 gather_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 gather_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 Gather : public gko::Operation {
72  Gather(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[i] = from_array[indices[i]];
89  }
90  break;
91  case add:
92 #pragma omp parallel for
93  for (auto i = 0; i < num_elems; ++i) {
94  into_array[i] = from_array[indices[i]] + into_array[i];
95  }
96  break;
97  case diff:
98 #pragma omp parallel for
99  for (auto i = 0; i < num_elems; ++i) {
100  into_array[i] = from_array[indices[i]] - into_array[i];
101  }
102  break;
103  case avg:
104 #pragma omp parallel for
105  for (auto i = 0; i < num_elems; ++i) {
106  into_array[i] = (from_array[indices[i]] + into_array[i]) / 2;
107  }
108  break;
109  default: {
110  std::cout << "Undefined gather operation" << std::endl;
111  break;
112  }
113  }
114  }
115 
116  void run(std::shared_ptr<const gko::CudaExecutor>) const override
117  {
118  switch (optype) {
119  case copy:
120  gather_values(num_elems, indices, from_array, into_array);
121  break;
122  case add:
123  gather_add_values(num_elems, indices, from_array, into_array);
124  break;
125  case diff:
126  gather_diff_values(num_elems, indices, from_array, into_array);
127  break;
128  case avg:
129  gather_avg_values(num_elems, indices, from_array, into_array);
130  break;
131  default: {
132  std::cout << "Undefined gather operation" << std::endl;
133  break;
134  }
135  }
136  }
137  const IndexType num_elems;
138  const IndexType *indices;
139  const ValueType *from_array;
140  ValueType *into_array;
141  OperationType optype;
142 };
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_GATHER(ValueType, IndexType) struct Gather<ValueType, IndexType>
152 INSTANTIATE_FOR_EACH_VALUE_AND_INDEX_TYPE(DECLARE_GATHER);
153 #undef DECLARE_GATHER
154 
155 } // namespace schwz
156 
157 
158 #endif // gather.hpp
Definition: gather.hpp:71
The Schwarz wrappers namespace.
Definition: comm_helpers.hpp:49