COMPASS  5.4.4
End-to-end AO simulation tool using GPU acceleration
carma_host_obj.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 // This file is part of COMPASS <https://anr-compass.github.io/compass/>
3 //
4 // Copyright (C) 2011-2023 COMPASS Team <https://github.com/ANR-COMPASS>
5 // All rights reserved.
6 // -----------------------------------------------------------------------------
7 
15 
16 #ifndef _CarmaHostObj_H_
17 #define _CarmaHostObj_H_
18 
19 #include <carma.h>
20 #include <carma_context.h>
21 #include <carma_streams.h>
22 #include <carma_utils.h>
23 
24 #include <iostream>
25 #include <typeinfo> // operator typeid
26 
27 enum MemAlloc {
34 };
35 
36 #define MEMORY_ALIGNMENT 4096
37 #define ALIGN_UP(x, size) (((size_t)x + (size - 1)) & (~(size - 1)))
38 
39 template <class T_data>
40 class CarmaObj;
41 
42 template <class T_data>
43 class CarmaHostObj {
44  protected:
45  T_data *h_data;
46  T_data *data_UA;
47  long *dims_data;
48  int nb_elem;
51 
52  void init(const long *dims_data, const T_data *data, MemAlloc malloc_type,
53  int nb_streams);
54 
55  public:
57  CarmaHostObj(const long *dims_data);
58  CarmaHostObj(const std::vector<long> &dims);
62  CarmaHostObj(const long *dims_data, const T_data *data);
63  CarmaHostObj(const long *dims_data, const T_data *data, MemAlloc malloc_type);
64  CarmaHostObj(const long *dims_data, int nb_streams);
65  CarmaHostObj(const long *dims_data, MemAlloc malloc_type, int nb_streams);
66  CarmaHostObj(const CarmaHostObj<T_data> *obj, int nb_streams);
68  int nb_streams);
69  CarmaHostObj(const long *dims_data, const T_data *data, int nb_streams);
70  CarmaHostObj(const long *dims_data, const T_data *data, MemAlloc malloc_type,
71  int nb_streams);
73 
74  void get_devpntr(void **pntr_dev);
75 
77  int add_stream();
78  int add_stream(int nb);
79  int del_stream();
80  int del_stream(int nb);
81  cudaStream_t get_cuda_stream(int stream);
82  int wait_stream(int stream);
84 
85  int cpy_obj(CarmaObj<T_data> *carma_obj, cudaMemcpyKind flag);
86  int cpy_obj(CarmaObj<T_data> *carma_obj, cudaMemcpyKind flag,
87  unsigned int stream);
88 
90  T_data &operator[](long idx) { return this->h_data[idx]; }
91  const T_data &operator[](long idx) const { return this->h_data[idx]; }
92  operator T_data *() { return h_data; }
93  operator std::string() {
94  std::ostringstream stream;
95  stream << *this;
96  return stream.str();
97  }
98  // inline char const *c_str() { return std::string(*this).c_str(); }
99  T_data *get_data() { return h_data; }
100  T_data *get_data_at(int index) { return &h_data[index]; }
101  const long *get_dims() { return dims_data; }
102  long get_dims(int i) { return dims_data[i]; }
103  int get_nb_elements() { return nb_elem; }
104 
106  int fill_from(const T_data *data);
107  int fill_into(T_data *data);
108  int fill(T_data value);
109 
110  std::string get_mem_alloc() {
111  switch (malloc_type) {
112  case MA_MALLOC:
113  return "MA_MALLOC";
114  case MA_PAGELOCK:
115  return "MA_PAGELOCK";
116  case MA_ZEROCPY:
117  return "MA_ZEROCPY";
118  case MA_PORTABLE:
119  return "MA_PORTABLE";
120  case MA_WRICOMB:
121  return "MA_WRICOMB";
122  case MA_GENEPIN:
123  return "MA_GENEPIN";
124  default:
125  return "MA_UNKNOWN";
126  }
127  }
128 };
129 
130 template <class T_data>
131 std::ostream &operator<<(std::ostream &os, CarmaHostObj<T_data> &obj) {
132  os << "-----------------------" << std::endl;
133  os << "CarmaHostObj<" << typeid(T_data).name() << "> object" << std::endl;
134  long ndims = obj.get_dims(0);
135  os << "ndims = " << ndims << std::endl;
136  for (long dim = 0; dim < ndims; dim++) {
137  os << "dim[" << dim << "] = " << obj.get_dims(dim + 1) << std::endl;
138  }
139  os << "nbElem = " << obj.get_nb_elements() << std::endl;
140  os << "sizeof(" << typeid(T_data).name() << ") = " << sizeof(T_data)
141  << std::endl;
142  os << "-----------------------" << std::endl;
143  return os;
144 }
145 
146 /*
147  extern "C" {
148 
149  }
150  */
151 
152 #endif // _CarmaHostObj_H_
std::ostream & operator<<(std::ostream &os, CarmaHostObj< T_data > &obj)
MemAlloc
@ MA_GENEPIN
@ MA_WRICOMB
@ MA_MALLOC
@ MA_PAGELOCK
@ MA_ZEROCPY
@ MA_PORTABLE
this file provides tools to CarmaObj
this class provides wrappers to the generic carma host object
const T_data & operator[](long idx) const
T_data * get_data_at(int index)
CarmaHostObj(const long *dims_data, const T_data *data)
int wait_stream(int stream)
int cpy_obj(CarmaObj< T_data > *carma_obj, cudaMemcpyKind flag, unsigned int stream)
int add_stream()
CarmaHostObj(const long *dims_data)
T_data * h_data
Input data.
CarmaStreams * streams
int del_stream(int nb)
cudaStream_t get_cuda_stream(int stream)
T_data * data_UA
unpadded input dara for generic pinned mem
CarmaHostObj(const CarmaHostObj< T_data > *obj, MemAlloc malloc_type)
void get_devpntr(void **pntr_dev)
long get_dims(int i)
int cpy_obj(CarmaObj< T_data > *carma_obj, cudaMemcpyKind flag)
long * dims_data
dimensions of the array
void init(const long *dims_data, const T_data *data, MemAlloc malloc_type, int nb_streams)
int fill_into(T_data *data)
CarmaHostObj(const long *dims_data, int nb_streams)
int fill(T_data value)
CarmaHostObj(const CarmaHostObj< T_data > *obj)
int nb_elem
number of elments in the array
CarmaHostObj(const CarmaHostObj< T_data > *obj, int nb_streams)
int add_stream(int nb)
CarmaHostObj(const long *dims_data, const T_data *data, int nb_streams)
MemAlloc malloc_type
type of host alloc
int fill_from(const T_data *data)
T_data & operator[](long idx)
int del_stream()
int get_nb_streams()
CarmaHostObj(const std::vector< long > &dims)
std::string get_mem_alloc()
CarmaHostObj(const long *dims_data, MemAlloc malloc_type)
T_data * get_data()
int wait_all_streams()
CarmaHostObj(const CarmaHostObj< T_data > *obj, MemAlloc malloc_type, int nb_streams)
int get_nb_elements()
CarmaHostObj(const long *dims_data, MemAlloc malloc_type, int nb_streams)
CarmaHostObj(const long *dims_data, const T_data *data, MemAlloc malloc_type)
CarmaHostObj(const long *dims_data, const T_data *data, MemAlloc malloc_type, int nb_streams)
const long * get_dims()
this class provides wrappers to the generic carma object
Definition: carma_obj.h:92
this class provides the stream features to CarmaObj
Definition: carma_streams.h:24