YAKL
ArrayIR.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include <array>
5 #include <iostream>
6 #include <type_traits>
7 
11 namespace array_ir {
12 
14  int constexpr MEMORY_HOST = 0;
16  int constexpr MEMORY_DEVICE = 1;
18  int constexpr MEMORY_SHARED = 2;
19 
20 
38  template <class T, int N>
39  class ArrayIR {
40  private:
41  T * my_data; // Data pointer
42  std::array<size_t,N> my_dimensions; // Dimensions (right-most varying the fastest
43  char const * my_label; // Label for the array (for debugging and things)
44  int my_memory_type; // Memory type (host, device, or shared)
45 
46  void nullify() { my_data = nullptr; my_label = nullptr; my_dimensions.fill(0); }
47 
48  void copy_from(ArrayIR const &rhs) {
49  my_data = rhs.my_data;
50  my_label = rhs.my_label;
51  my_memory_type = rhs.my_memory_type;
52  my_dimensions = rhs.my_dimensions;
53  }
54 
55  void error_message(char const *message) const {
56  std::cerr << "*** ArrayIR class: A FATAL ERROR WAS ENCOUNTERED ***" << std::endl;
57  std::cerr << message << std::endl;
58  throw std::runtime_error(message);
59  }
60 
61 
62  public:
64  typedef T exact_type;
66  typedef typename std::remove_cv<T>::type remove_cv_type;
67 
69  ArrayIR() { nullify(); }
78  ArrayIR( T * data , std::array<size_t,N> dimensions , int memory_type , char const * label="" ) {
79  my_data = data;
80  my_memory_type = memory_type;
81  my_label = label;
82  my_dimensions = dimensions;
83  }
85  ArrayIR (ArrayIR const &rhs) { copy_from(rhs); }
87  ArrayIR (ArrayIR const &&rhs) { copy_from(rhs); }
89  ArrayIR &operator=(ArrayIR const &rhs) { copy_from(rhs); }
91  ArrayIR &operator=(ArrayIR const &&rhs) { copy_from(rhs); }
92  ~ArrayIR() { nullify(); }
94  bool empty () const { return my_data == nullptr; }
96  bool is_empty() const { return my_data == nullptr; }
98  bool valid () const { return ! empty(); }
100  bool is_valid() const { return ! empty(); }
102  T * data () const { return my_data; }
104  T * get_data () const { return my_data; }
106  T * get_data_pointer() const { return my_data; }
108  size_t extent(int i) const {
109  if (i < 0 || i > N-1) error_message("extent() index out of bounds");
110  return my_dimensions[i];
111  }
113  std::array<size_t,N> shape() const { return my_dimensions; }
115  size_t dimension(int i) const { return extent(i); }
117  char const * label () const { return my_label; }
119  char const * get_label() const { return my_label; }
121  int memory_type () const { return my_memory_type; }
123  int get_memory_type() const { return my_memory_type; }
125  bool data_valid_on_host () const { return (my_memory_type == MEMORY_HOST ) || (my_memory_type == MEMORY_SHARED); }
127  bool data_valid_on_device() const { return (my_memory_type == MEMORY_DEVICE) || (my_memory_type == MEMORY_SHARED); }
128  };
129 
130 }
131 
132 
array_ir::ArrayIR::get_memory_type
int get_memory_type() const
Get the memory type for this array object (MEMORY_HOST, MEMORY_DEVICE, or MEMORY_SHARED)
Definition: ArrayIR.h:123
array_ir::MEMORY_SHARED
constexpr int MEMORY_SHARED
Declares that the data pointer is defined in both host and device memory.
Definition: ArrayIR.h:18
array_ir::ArrayIR::dimension
size_t dimension(int i) const
Get the extent of the dimension of the provided index.
Definition: ArrayIR.h:115
array_ir::ArrayIR::operator=
ArrayIR & operator=(ArrayIR const &&rhs)
Move constructor.
Definition: ArrayIR.h:91
array_ir::ArrayIR::get_label
const char * get_label() const
Get the label for this array.
Definition: ArrayIR.h:119
array_ir::ArrayIR::operator=
ArrayIR & operator=(ArrayIR const &rhs)
Copy constructor.
Definition: ArrayIR.h:89
array_ir::ArrayIR::get_data_pointer
T * get_data_pointer() const
Get the data pointer.
Definition: ArrayIR.h:106
array_ir::ArrayIR::is_valid
bool is_valid() const
Is this ArrayIR object valid (does it point to something other than nullptr)?
Definition: ArrayIR.h:100
array_ir::ArrayIR::memory_type
int memory_type() const
Get the memory type for this array object (MEMORY_HOST, MEMORY_DEVICE, or MEMORY_SHARED)
Definition: ArrayIR.h:121
array_ir::ArrayIR::exact_type
T exact_type
The exact type of this ArrayIR object with all qualifiers.
Definition: ArrayIR.h:64
array_ir::ArrayIR::valid
bool valid() const
Is this ArrayIR object valid (does it point to something other than nullptr)?
Definition: ArrayIR.h:98
array_ir::MEMORY_HOST
constexpr int MEMORY_HOST
Declares that the data pointer is defined only in host memory.
Definition: ArrayIR.h:14
array_ir::ArrayIR::ArrayIR
ArrayIR(ArrayIR const &rhs)
Copy constructor.
Definition: ArrayIR.h:85
array_ir::ArrayIR::shape
std::array< size_t, N > shape() const
Get the shape of the array as a std::array<size_t,N>
Definition: ArrayIR.h:113
array_ir::ArrayIR::empty
bool empty() const
Is this ArrayIR object invalid (does it point to nullptr)?
Definition: ArrayIR.h:94
array_ir::MEMORY_DEVICE
constexpr int MEMORY_DEVICE
Declares that the data pointer is defined only in device memory.
Definition: ArrayIR.h:16
array_ir::ArrayIR::get_data
T * get_data() const
Get the data pointer.
Definition: ArrayIR.h:104
array_ir::ArrayIR::~ArrayIR
~ArrayIR()
Definition: ArrayIR.h:92
array_ir::ArrayIR
The ArrayIR class holds library-agnostic Array metadata to make it easy to transfer array objects bet...
Definition: ArrayIR.h:39
array_ir::ArrayIR::ArrayIR
ArrayIR(T *data, std::array< size_t, N > dimensions, int memory_type, char const *label="")
Creates an ArrayIR object with the given data, dimensions, memory type, and optional label.
Definition: ArrayIR.h:78
array_ir
The ArrayIR namespace holds the ArrayIR class and memory type constants associated with ArrayIR....
Definition: ArrayIR.h:11
array_ir::ArrayIR::ArrayIR
ArrayIR(ArrayIR const &&rhs)
Move constructor.
Definition: ArrayIR.h:87
array_ir::ArrayIR::label
const char * label() const
Get the label for this array.
Definition: ArrayIR.h:117
array_ir::ArrayIR::data_valid_on_device
bool data_valid_on_device() const
Determine if the data pointer is valid on the device.
Definition: ArrayIR.h:127
array_ir::ArrayIR::remove_cv_type
std::remove_cv< T >::type remove_cv_type
The type of this ArrayIR object with cv qualifiers removed.
Definition: ArrayIR.h:66
array_ir::ArrayIR::extent
size_t extent(int i) const
Get the extent of the dimension of the provided index.
Definition: ArrayIR.h:108
array_ir::ArrayIR::data
T * data() const
Get the data pointer.
Definition: ArrayIR.h:102
array_ir::ArrayIR::ArrayIR
ArrayIR()
Creates an empty object with nullptr as the data pointer.
Definition: ArrayIR.h:69
array_ir::ArrayIR::is_empty
bool is_empty() const
Is this ArrayIR object invalid (does it point to nullptr)?
Definition: ArrayIR.h:96
array_ir::ArrayIR::data_valid_on_host
bool data_valid_on_host() const
Determine if the data pointer is valid on the host.
Definition: ArrayIR.h:125