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