Compadre  1.5.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Compadre_Utilities.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Compadre: COMpatible PArticle Discretization and REmap Toolkit
4 //
5 // Copyright 2018 NTESS and the Compadre contributors.
6 // SPDX-License-Identifier: BSD-2-Clause
7 // *****************************************************************************
8 // @HEADER
9 #ifndef _COMPADRE_UTILITIES_HPP_
10 #define _COMPADRE_UTILITIES_HPP_
11 
12 #include "Compadre_Config.h"
13 #include "Compadre_Typedefs.hpp"
14 
15 namespace Compadre {
16 
17 KOKKOS_INLINE_FUNCTION
18 void getMidpointFromCellVertices(const member_type& teamMember, scratch_vector_type midpoint_storage, scratch_matrix_right_type cell_coordinates, const int cell_num, const int dim=3) {
19 Kokkos::single(Kokkos::PerThread(teamMember), [&] () {
20  auto num_nodes = cell_coordinates.extent(1)/dim;
21  for (int j=0; j<dim; ++j) {
22  midpoint_storage(j) = 0;
23  for (size_t i=0; i<num_nodes; ++i) {
24  midpoint_storage(j) += cell_coordinates(cell_num, i*dim + j) / (double)(num_nodes);
25  }
26  }
27 });
28 }
29 
30 template <typename view_type_1, typename view_type_2>
31 KOKKOS_INLINE_FUNCTION
32 double getAreaFromVectors(const member_type& teamMember, view_type_1 v1, view_type_2 v2) {
33  if (v1.extent(0)==3) {
34  double area = 0;
35  double val = v1[1]*v2[2] - v1[2]*v2[1];
36  area += val*val;
37  val = v1[2]*v2[0] - v1[0]*v2[2];
38  area += val*val;
39  val = v1[0]*v2[1] - v1[1]*v2[0];
40  area += val*val;
41  return std::sqrt(area);
42  } else if (v1.extent(0)==2) {
43  double area = 0;
44  double val = v1[0]*v2[1] - v1[1]*v2[0];
45  area += val*val;
46  return std::sqrt(area);
47  } else {
48  compadre_kernel_assert_debug(false && "v1 in getAreaFromVectors has length != 2 or 3");
49  return 0.0;
50  }
51 }
52 
53 template <typename output_memory_space, typename view_type_input_data, typename output_array_layout = typename view_type_input_data::array_layout, typename index_type=int>
54 Kokkos::View<int*, output_array_layout, output_memory_space> // shares layout of input by default
55  filterViewByID(view_type_input_data input_data_host_or_device, index_type filtered_value) {
56 
57  // Make view on the host (does nothing if already on the host)
58  auto input_data_host = Kokkos::create_mirror_view(input_data_host_or_device);
59  Kokkos::deep_copy(input_data_host, input_data_host_or_device);
60  Kokkos::fence();
61 
62  // Count the number of elements in the input view that match the desired value
63  int num_count = 0;
64  auto this_filtered_value = filtered_value;
65 
66  // call device functor here
67 
68  for (size_t i=0; i<input_data_host.extent(0); i++) {
69  if (input_data_host(i) == this_filtered_value) {
70  num_count++;
71  }
72  }
73  Kokkos::fence();
74 
75  // Create a new view living on device
76  Kokkos::View<int*, output_array_layout, output_memory_space> filtered_view("filterd view", num_count);
77  // Gather up the indices into the new view
78  int filtered_index = 0;
79  for (size_t i=0; i<input_data_host.extent(0); i++) {
80  if (input_data_host(i) == this_filtered_value) {
81  filtered_view(filtered_index) = i;
82  filtered_index++;
83  }
84  }
85  Kokkos::fence();
86 
87  // Then copy it back out - either to host or device space based on user's request
88  typedef Kokkos::View<int*, output_array_layout, output_memory_space> output_view_type;
89  output_view_type filtered_view_output("output filtered view", num_count);
90  Kokkos::deep_copy(filtered_view_output, filtered_view);
91  Kokkos::fence();
92 
93  return filtered_view_output;
94 }
95 
96 struct Extract {
97 
98  template <typename output_memory_space, typename view_type_input_data, typename view_type_index_data,
100  ||std::is_same<typename view_type_input_data::data_type, int**>::value, int> = 0>
101  static Kokkos::View<typename view_type_input_data::data_type, typename view_type_input_data::array_layout, output_memory_space> // shares layout of input by default
102  extractViewByIndex(view_type_input_data input_data_host_or_device, view_type_index_data index_data_host_or_device) {
103 
104  typedef typename view_type_input_data::data_type output_data_type;
105  typedef typename view_type_input_data::array_layout output_array_layout;
106 
107  // Make view on the host for input data (does nothing if already on the host)
108  auto input_data_host = Kokkos::create_mirror_view(input_data_host_or_device);
109  Kokkos::deep_copy(input_data_host, input_data_host_or_device);
110  Kokkos::fence();
111 
112  // Make view on the host for index data (does nothing if already on the host)
113  auto index_data_host = Kokkos::create_mirror_view(index_data_host_or_device);
114  Kokkos::deep_copy(index_data_host, index_data_host_or_device);
115  Kokkos::fence();
116 
117  // Create a new view to extract out the rows that belong to the filtered index
118  Kokkos::View<output_data_type, output_array_layout, output_memory_space> extracted_view("extracted view",
119  index_data_host.extent(0), input_data_host.extent(1));
120 
121  // Loop through all the entries of index data
122  for (size_t i=0; i<index_data_host.extent(0); i++) {
123  for (size_t j=0; j<input_data_host.extent(1); j++) {
124  extracted_view(i, j) = input_data_host(index_data_host(i), j);
125  }
126  }
127 
128  // Then copy it back out - either to host or device space based on user's request
129  typedef Kokkos::View<output_data_type, output_array_layout, output_memory_space> output_view_type;
130  output_view_type extracted_view_output("output extracted view", extracted_view.extent(0), extracted_view.extent(1));
131  Kokkos::deep_copy(extracted_view_output, extracted_view);
132  Kokkos::fence();
133 
134  return extracted_view_output;
135  }
136 
137  template <typename output_memory_space, typename view_type_input_data, typename view_type_index_data,
139  ||std::is_same<typename view_type_input_data::data_type, int*>::value, int> = 0>
140  static Kokkos::View<double*, typename view_type_input_data::array_layout, output_memory_space> // shares layout of input by default
141  extractViewByIndex(view_type_input_data input_data_host_or_device, view_type_index_data index_data_host_or_device) {
142 
143  typedef typename view_type_input_data::data_type output_data_type;
144  typedef typename view_type_input_data::array_layout output_array_layout;
145 
146  // Make view on the host for input data (does nothing if already on the host)
147  auto input_data_host = Kokkos::create_mirror_view(input_data_host_or_device);
148  Kokkos::deep_copy(input_data_host, input_data_host_or_device);
149  Kokkos::fence();
150 
151  // Make view on the host for index data (does nothing if already on the host)
152  auto index_data_host = Kokkos::create_mirror_view(index_data_host_or_device);
153  Kokkos::deep_copy(index_data_host, index_data_host_or_device);
154  Kokkos::fence();
155 
156  // Create a new view to extract out the rows that belong to the filtered index
157  Kokkos::View<output_data_type, output_array_layout, output_memory_space> extracted_view("extracted view",
158  index_data_host.extent(0));
159 
160  // Loop through all the entries of index data
161  for (size_t i=0; i<index_data_host.extent(0); i++) {
162  extracted_view(i) = input_data_host(index_data_host(i));
163  }
164 
165  // Then copy it back out - either to host or device space based on user's request
166  typedef Kokkos::View<output_data_type, output_array_layout, output_memory_space> output_view_type;
167  output_view_type extracted_view_output("output extracted view", extracted_view.extent(0));
168  Kokkos::deep_copy(extracted_view_output, extracted_view);
169  Kokkos::fence();
170 
171  return extracted_view_output;
172  }
173 
174 };
175 
176 // template <typename output_memory_space, typename view_type_input_data, typename output_array_layout = typename view_type_input_data::array_layout, typename index_type=int>
177 // Kokkos::View<int*, output_array_layout, output_memory_space> // shares layout of input by default
178 // filterViewByID(view_type_input_data input_data_host_or_device, index_type filtered_value) {
179 //
180 // // Make view on the device (does nothing if already on the device)
181 // auto input_data_device = Kokkos::create_mirror_view(
182 // device_memory_space(), input_data_host_or_device);
183 // Kokkos::deep_copy(input_data_device, input_data_host_or_device);
184 // Kokkos::fence();
185 //
186 // // Count the number of elements in the input view that match the desired value
187 // int num_count = 0;
188 // auto this_filtered_value = filtered_value;
189 //
190 // // call device functor here
191 //
192 // for (int i=0; i<input_data_device.extent(0); i++) {
193 // input_data_device(i)++;
194 // // if (input_data_device(i) == this_filtered_value) {
195 // // if (input_data_device(i) == 1) {
196 // // num_count++;
197 // // }
198 // }
199 // Kokkos::fence();
200 //
201 // // Create a new view living on device
202 // Kokkos::View<int*, output_array_layout> filtered_view("filterd view", num_count);
203 // // // Gather up the indices into the new view
204 // // int filtered_index = 0;
205 // // for (int i=0; i<input_data_device.extent(0); i++) {
206 // // if (input_data_device(i) == filtered_value) {
207 // // filtered_view(filtered_index) = i;
208 // // filtered_index++;
209 // // }
210 // // }
211 // // Kokkos::fence();
212 //
213 // // Then copy it back out - either to host or device space based on user's request
214 // typedef Kokkos::View<int*, output_array_layout, output_memory_space> output_view_type;
215 // output_view_type filtered_view_output("output filtered view", num_count);
216 // Kokkos::deep_copy(filtered_view_output, filtered_view);
217 // Kokkos::fence();
218 //
219 // return filtered_view_output;
220 // }
221 
222 } // Compadre namespace
223 
224 #endif
KOKKOS_INLINE_FUNCTION void getMidpointFromCellVertices(const member_type &teamMember, scratch_vector_type midpoint_storage, scratch_matrix_right_type cell_coordinates, const int cell_num, const int dim=3)
team_policy::member_type member_type
KOKKOS_INLINE_FUNCTION double getAreaFromVectors(const member_type &teamMember, view_type_1 v1, view_type_2 v2)
Kokkos::View< int *, output_array_layout, output_memory_space > filterViewByID(view_type_input_data input_data_host_or_device, index_type filtered_value)
static Kokkos::View< typename view_type_input_data::data_type, typename view_type_input_data::array_layout, output_memory_space > extractViewByIndex(view_type_input_data input_data_host_or_device, view_type_index_data index_data_host_or_device)
typename std::enable_if< B, T >::type enable_if_t
Kokkos::View< double **, layout_right, Kokkos::MemoryTraits< Kokkos::Unmanaged > > scratch_matrix_right_type
Kokkos::View< double *, Kokkos::MemoryTraits< Kokkos::Unmanaged > > scratch_vector_type
static Kokkos::View< double *, typename view_type_input_data::array_layout, output_memory_space > extractViewByIndex(view_type_input_data input_data_host_or_device, view_type_index_data index_data_host_or_device)
#define compadre_kernel_assert_debug(condition)