YAKL
YAKL_intrinsics_pack.h
Go to the documentation of this file.
1 
2 #pragma once
3 // Included by YAKL_intrinsics.h
4 
6 namespace yakl {
7  namespace intrinsics {
8 
9  template <class T, int rank, int myStyle>
13  #ifdef YAKL_DEBUG
14  if (! allocated(arr)) yakl_throw("ERROR: Calling pack with unallocated array");
15  #endif
16  if (allocated(mask)) {
17  #ifdef YAKL_DEBUG
18  using yakl::componentwise::operator==;
19  using yakl::componentwise::operator&&;
20  using yakl::componentwise::operator!;
21  if ( any( !(shape(mask) == shape(arr)) ) ) yakl_throw("ERROR: arr & mask shapes do not match in pack call");
22  #endif
23 
24  if (mask.totElems() != arr.totElems()) {
25  yakl_throw("Error: pack: arr and mask have a different number of elements");
26  }
27  // count the number of true elements
28  int numTrue = count( mask );
29  Array<T,1,memHost,myStyle> ret("packReturn",numTrue);
30  int slot = 0;
31  for (int i=0; i < arr.totElems(); i++) {
32  if (mask.data()[i]) { ret.data()[slot] = arr.data()[i]; slot++; }
33  }
34  return ret;
35 
36  } else {
37 
38  Array<T,1,memHost,myStyle> ret("packReturn",arr.totElems());
39  for (int i=0; i < arr.totElems(); i++) {
40  ret.data()[i] = arr.data()[i];
41  }
42  return ret;
43 
44  }
45  }
46 
47  template <class T, int rank, int myStyle>
51  #ifdef YAKL_DEBUG
52  if (! allocated(arr)) yakl_throw("ERROR: Calling pack with unallocated array");
53  #endif
54  if constexpr (streams_enabled) fence();
55  if (allocated(mask)) {
56  #ifdef YAKL_DEBUG
57  using yakl::componentwise::operator==;
58  using yakl::componentwise::operator&&;
59  using yakl::componentwise::operator!;
60  if ( any( !(shape(mask) == shape(arr)) ) ) yakl_throw("ERROR: arr & mask shapes do not match in pack call");
61  #endif
62  return pack(arr.createHostCopy() , mask.createHostCopy()).createDeviceCopy();
63  } else {
64  return pack(arr.createHostCopy() ).createDeviceCopy();
65  }
66  }
67 
68  }
69 }
71 
yakl::intrinsics::count
int count(Array< bool, rank, memHost, myStyle > const &mask)
Definition: YAKL_intrinsics_count.h:10
yakl::intrinsics::shape
YAKL_INLINE auto shape(T const &arr)
Definition: YAKL_intrinsics_shape.h:9
yakl::intrinsics::allocated
YAKL_INLINE bool allocated(T const &arr)
Definition: YAKL_intrinsics_allocated.h:9
__YAKL_NAMESPACE_WRAPPER_END__
#define __YAKL_NAMESPACE_WRAPPER_END__
Definition: YAKL.h:20
yakl::streams_enabled
constexpr bool streams_enabled
If the CPP Macro YAKL_ENABLE_STREAMS is defined, then this bool is set to true
Definition: YAKL_streams_events.h:11
__YAKL_NAMESPACE_WRAPPER_BEGIN__
#define __YAKL_NAMESPACE_WRAPPER_BEGIN__
Definition: YAKL.h:19
yakl::fence
void fence()
Block the host code until all device code has completed.
Definition: YAKL_fence.h:16
yakl::yakl_throw
YAKL_INLINE void yakl_throw(const char *msg)
Throw an error message. Works from the host or device.
Definition: YAKL_error.h:17
yakl::intrinsics::pack
Array< T, 1, memHost, myStyle > pack(Array< T, rank, memHost, myStyle > const &arr, Array< bool, rank, memHost, myStyle > const &mask=Array< bool, rank, memHost, myStyle >())
Definition: YAKL_intrinsics_pack.h:10
yakl::Array
This declares the yakl::Array class. Please see the yakl::styleC and yakl::styleFortran template spec...
Definition: YAKL_Array.h:40
yakl
yakl::intrinsics::any
bool any(Array< T, rank, memHost, myStyle > arr)
Definition: YAKL_intrinsics_any.h:10