XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
view_arithmetic.hpp
Go to the documentation of this file.
1 #ifndef VIEW_ARITHMETIC_HPP
2 #define VIEW_ARITHMETIC_HPP
3 #include "space_settings.hpp"
4 #include "globals.hpp"
5 
6 // Functions to facilitate element-wise addition and subtraction of Views. Could try overloading views, but this works and is simple enough
7 
8 // Struct to hold raw device pointers (At some point there was an OMP bug that disallowed raw pointers sent in lambdas
9 template<typename T>
11  T* x;
12  T* y;
13 
14  ViewArithmeticPointers(T* x, T* y) : x(x), y(y){}
15 };
16 
17 // Add added_view*coeff to dest_view
18 template<typename V1, typename V2, typename T>
19 inline void host_add_ay_to_x(V1& dest_view, const V2& added_view, T coeff){
20  if (dest_view.size() != added_view.size()) exit_XGC("\nWARNING: dest_view and added_view are not the same size");
21 
22  // Access raw pointer of View so that we can loop over the whole array without worrying about dimensions
23  const ViewArithmeticPointers<T> ptrs(dest_view.data(), added_view.data());
24  Kokkos::parallel_for("host_add_ay_to_x", Kokkos::RangePolicy<HostExSpace>( 0, dest_view.size()), KOKKOS_LAMBDA(const int idx){
25  ptrs.x[idx] += coeff*ptrs.y[idx];
26  });
27 }
28 
29 // Divide x by y
30 template<typename V1, typename V2>
31 inline void host_divide_x_by_y(V1& dest_view, const V2& divisor_view){
32  if (dest_view.size() != divisor_view.size()) exit_XGC("\nWARNING: dest_view and divisor_view are not the same size");
33 
34  // Access raw pointer of View so that we can loop over the whole array without worrying about dimensions
35  const ViewArithmeticPointers<double> ptrs(dest_view.data(), divisor_view.data());
36  Kokkos::parallel_for("host_divide_x_by_y", Kokkos::RangePolicy<HostExSpace>( 0, dest_view.size()), KOKKOS_LAMBDA(const int idx){
37  ptrs.x[idx] /= ptrs.y[idx];
38  });
39 }
40 
41 // Divide x by a
42 template<typename V1, typename T>
43 inline void host_divide_x_by_a(V1& dest_view, T coeff){
44  double coeff_inv = 1.0/coeff;
45 
46  // Access raw pointer of View so that we can loop over the whole array without worrying about dimensions
47  const ViewArithmeticPointers<double> ptrs(dest_view.data(), dest_view.data());
48  Kokkos::parallel_for("host_divide_x_by_a", Kokkos::RangePolicy<HostExSpace>( 0, dest_view.size()), KOKKOS_LAMBDA(const int idx){
49  ptrs.x[idx] *= coeff_inv;
50  });
51 }
52 
53 template<typename T>
54 inline double sum_view(const T& view){
55  // Cast to 1D with an unmanaged view for a generic element-wise operation
56  View<double*,CLayout, typename T::device_type, Kokkos::MemoryTraits<Kokkos::Unmanaged>> view_1D(view.data(), view.size());
57 
58  double sum = 0.0;
59  Kokkos::parallel_reduce("sum", Kokkos::RangePolicy<typename T::execution_space>( 0,view.size() ), KOKKOS_LAMBDA( const int i, double& sum_l){
60  sum_l += view_1D(i);
61  }, sum);
62  Kokkos::fence();
63 
64  return sum;
65 }
66 
67 #endif
double sum_view(const T &view)
Definition: view_arithmetic.hpp:54
void host_divide_x_by_y(V1 &dest_view, const V2 &divisor_view)
Definition: view_arithmetic.hpp:31
void host_add_ay_to_x(V1 &dest_view, const V2 &added_view, T coeff)
Definition: view_arithmetic.hpp:19
T * y
Definition: view_arithmetic.hpp:12
ViewArithmeticPointers(T *x, T *y)
Definition: view_arithmetic.hpp:14
idx
Definition: diag_f0_df_port1.hpp:32
void exit_XGC(std::string msg)
Definition: globals.hpp:36
void parallel_for(const std::string name, int n_ptl, Function func, Option option, HostAoSoA aosoa_h, DeviceAoSoA aosoa_d)
Definition: streamed_parallel_for.hpp:252
Definition: view_arithmetic.hpp:10
T * x
Definition: view_arithmetic.hpp:11
void host_divide_x_by_a(V1 &dest_view, T coeff)
Definition: view_arithmetic.hpp:43