XGC1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vertex_list.hpp
Go to the documentation of this file.
1 #ifndef VERTEX_LIST_HPP
2 #define VERTEX_LIST_HPP
3 
4 #include "space_settings.hpp"
5 
6 // A compressed way to represent a list of vertices.
7 // Note: using C/python convention so range {0, 2} means vertices 0 and 1
8 template<class Device>
9 class VertexList{
10 
11  using exspace = typename Device::execution_space;
12 
13  struct IntegerRange{
14  int start;
15  int end; // Note: using C/python convention so range {0, 2} means vertices 0 and 1
16 
17  KOKKOS_INLINE_FUNCTION bool is_in_range(int i) const{
18  return (i>=start && i<end);
19  }
20 
21  KOKKOS_INLINE_FUNCTION int size() const{
22  return (end - start);
23  }
24  };
25 
26  View<IntegerRange*,CLayout,Device> ranges;
27  View<int*,CLayout,Device> singlets;
28 
29  public:
30 
32 
33  // Initialize with single range
34  VertexList(int start, int end);
35 
36  // Initialize with a condition, e.g.:
37  // VertexList<HostType>(grid.nnode, KOKKOS_LAMBDA( const int i){return grid_h.is_in_psi_range(i)});
38  template<typename F>
39  VertexList(int n, F condition){
40  int nranges = 0;
41  int nsinglets = 0;
42  int istart = 0;
43  bool was_in_list = false;
44  for(int i=0; i<n+1; i++){
45  bool in_list = false;
46  if(i<n) in_list = condition(i);
47 
48  // Entered new range
49  if(in_list && !was_in_list){
50  istart = i;
51  }
52 
53  // Just left range
54  if(was_in_list && !in_list){
55  if(i == istart+1){
56  nsinglets += 1;
57  }else{
58  nranges += 1;
59  }
60  }
61  }
62 
63  // Allocate
64  ranges = View<IntegerRange*,CLayout,Device>(NoInit("ranges"), nranges);
65  singlets = View<int*,CLayout,Device>(NoInit("singlets"), nsinglets);
66 
67  // Now populate views
68  nranges = 0;
69  nsinglets = 0;
70  istart = 0;
71  was_in_list = false;
72  for(int i=0; i<n+1; i++){
73  bool in_list = false;
74  if(i<n) in_list = condition(i);
75 
76  // Entered new range
77  if(in_list && !was_in_list){
78  istart = i;
79  }
80 
81  // Just left range
82  if(was_in_list && !in_list){
83  if(i == istart+1){
84  singlets(nsinglets) = istart;
85  nsinglets += 1;
86  }else{
87  ranges(nranges) = {istart, i}; // Note: using C/python convention so range {0, 2} means vertices 0 and 1
88  nranges += 1;
89  }
90  }
91  }
92  }
93 
94  // Initialize the list by passing in the full uncompressed list of vertices.
95  VertexList(const View<int*,CLayout,Device>& unordered_full_list, bool one_indexed);
96 
97  // Execute a lambda function on the vertex list
98  template<typename F>
99  void parallel_for(F func) const{
100  // Loop through ranges
101  for (int is=0; is<ranges.size(); is++){
102  Kokkos::parallel_for("vertex_list_parallel_for", Kokkos::RangePolicy<exspace>( ranges(is).start, ranges(is).end), KOKKOS_LAMBDA( const int i){
103  func(i);
104  });
105  }
106 
107  // Now through singlets
108  auto s = singlets; // To avoid passing class member
109  Kokkos::parallel_for("vertex_list_parallel_for2", Kokkos::RangePolicy<exspace>(0, singlets.size()), KOKKOS_LAMBDA( const int i){
110  // Map to true index
111  func(s(i));
112  });
113 
114  Kokkos::fence();
115  }
116 
124  KOKKOS_INLINE_FUNCTION bool is_in_list(int i) const {
125  // First check ranges
126  for (int is=0; is<ranges.size(); is++){
127  if(ranges(is).is_in_range(i)) return true;
128  }
129 
130  // Otherwise, check singlets
131  for(int is=0; is<singlets.size(); is++){
132  if(singlets(is)==i) return true;
133  }
134 
135  return false;
136  }
137 
138  // Number of vertices
139  KOKKOS_INLINE_FUNCTION int size(){
140  int n = singlets.size();
141 
142  // Add ranges
143  for (int is=0; is<ranges.size(); is++){
144  n += ranges(is).size();
145  }
146 
147  return n;
148  }
149 
155  template<typename DeviceExSpace, typename F>
156  inline void for_all(const std::string label, F lambda_func) const {
157  // Loop through ranges
158  for (int is=0; is<ranges.size(); is++){
159  Kokkos::parallel_for(label, Kokkos::RangePolicy<DeviceExSpace>(ranges(is).start, ranges(is).end), lambda_func);
160  }
161 
162  // Now through singlets
163  auto singlets_c = singlets;
164  Kokkos::parallel_for(label, Kokkos::RangePolicy<DeviceExSpace>(0, singlets.size()), KOKKOS_LAMBDA( const int i){
165  lambda_func(singlets_c(i)); // Map to true index
166  });
167  }
168 
169  void pack_contiguous(const View<double*, CLayout, HostType>& input, const View<double*, CLayout, HostType>& contiguous) const;
170 
171  void unpack_contiguous(const View<double*, CLayout, HostType>& contiguous, const View<double*, CLayout, HostType>& output) const;
172 
173  VertexList intersection(const VertexList& list2) const;
174 
175  int min() const;
176 
177  bool is_contiguous() const;
178 };
179 
180 #endif
void parallel_for(F func) const
Definition: vertex_list.hpp:99
VertexList intersection(const VertexList &list2) const
VertexList()
Definition: vertex_list.hpp:31
VertexList(int n, F condition)
Definition: vertex_list.hpp:39
void unpack_contiguous(const View< double *, CLayout, HostType > &contiguous, const View< double *, CLayout, HostType > &output) const
KOKKOS_INLINE_FUNCTION int size() const
Definition: vertex_list.hpp:21
KOKKOS_INLINE_FUNCTION int size()
Definition: vertex_list.hpp:139
KOKKOS_INLINE_FUNCTION bool is_in_range(int i) const
Definition: vertex_list.hpp:17
typename Device::execution_space exspace
Use execution space where views are allocated.
Definition: vertex_list.hpp:11
int start
Definition: vertex_list.hpp:14
Definition: vertex_list.hpp:13
int end
Definition: vertex_list.hpp:15
int min() const
void pack_contiguous(const View< double *, CLayout, HostType > &input, const View< double *, CLayout, HostType > &contiguous) const
View< int *, CLayout, Device > singlets
Definition: vertex_list.hpp:27
void for_all(const std::string label, F lambda_func) const
Definition: vertex_list.hpp:156
View< IntegerRange *, CLayout, Device > ranges
Definition: vertex_list.hpp:26
Definition: vertex_list.hpp:9
KOKKOS_INLINE_FUNCTION bool is_in_list(int i) const
Definition: vertex_list.hpp:124
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
bool is_contiguous() const
Kokkos::ViewAllocateWithoutInitializing NoInit
Definition: space_settings.hpp:68