COMPASS  5.0.0
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-2019 COMPASS Team <https://github.com/ANR-COMPASS>
5 // All rights reserved.
6 // Distributed under GNU - LGPL
7 //
8 // COMPASS is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
9 // General Public License as published by the Free Software Foundation, either version 3 of the License,
10 // or any later version.
11 //
12 // COMPASS: End-to-end AO simulation tool using GPU acceleration
13 // The COMPASS platform was designed to meet the need of high-performance for the simulation of AO systems.
14 //
15 // The final product includes a software package for simulating all the critical subcomponents of AO,
16 // particularly in the context of the ELT and a real-time core based on several control approaches,
17 // with performances consistent with its integration into an instrument. Taking advantage of the specific
18 // hardware architecture of the GPU, the COMPASS tool allows to achieve adequate execution speeds to
19 // conduct large simulation campaigns called to the ELT.
20 //
21 // The COMPASS platform can be used to carry a wide variety of simulations to both testspecific components
22 // of AO of the E-ELT (such as wavefront analysis device with a pyramid or elongated Laser star), and
23 // various systems configurations such as multi-conjugate AO.
24 //
25 // COMPASS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
26 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 // See the GNU Lesser General Public License for more details.
28 //
29 // You should have received a copy of the GNU Lesser General Public License along with COMPASS.
30 // If not, see <https://www.gnu.org/licenses/lgpl-3.0.txt>.
31 // -----------------------------------------------------------------------------
32 
41 
42 #ifndef _CarmaHostObj_H_
43 #define _CarmaHostObj_H_
44 
45 #include <carma.h>
46 #include <carma_context.h>
47 #include <carma_streams.h>
48 #include <carma_utils.h>
49 #include <iostream>
50 #include <typeinfo> // operator typeid
51 
52 enum MemAlloc {
59 };
60 
61 #define MEMORY_ALIGNMENT 4096
62 #define ALIGN_UP(x, size) (((size_t)x + (size - 1)) & (~(size - 1)))
63 
64 template <class T_data>
65 class CarmaObj;
66 
67 template <class T_data>
68 class CarmaHostObj {
69  protected:
70  T_data *h_data;
71  T_data *data_UA;
72  long *dims_data;
73  int nb_elem;
76 
77  void init(const long *dims_data, const T_data *data, MemAlloc malloc_type,
78  int nb_streams);
79 
80  public:
81  CarmaHostObj(const long *dims_data);
85  CarmaHostObj(const long *dims_data, const T_data *data);
86  CarmaHostObj(const long *dims_data, const T_data *data,
88  CarmaHostObj(const long *dims_data, int nb_streams);
89  CarmaHostObj(const long *dims_data, MemAlloc malloc_type, int nb_streams);
90  CarmaHostObj(const CarmaHostObj<T_data> *obj, int nb_streams);
92  int nb_streams);
93  CarmaHostObj(const long *dims_data, const T_data *data, int nb_streams);
94  CarmaHostObj(const long *dims_data, const T_data *data, MemAlloc malloc_type,
95  int nb_streams);
97 
98  void get_devpntr(void **pntr_dev);
99 
101  int add_stream();
102  int add_stream(int nb);
103  int del_stream();
104  int del_stream(int nb);
105  cudaStream_t get_cuda_stream(int stream);
106  int wait_stream(int stream);
108 
109  int cpy_obj(CarmaObj<T_data> *carma_obj, cudaMemcpyKind flag);
110  int cpy_obj(CarmaObj<T_data> *carma_obj, cudaMemcpyKind flag,
111  unsigned int stream);
112 
114  operator T_data *() { return h_data; }
115  operator std::string() {
116  std::ostringstream stream;
117  stream << *this;
118  return stream.str();
119  }
120  inline char const *c_str() { return std::string(*this).c_str(); }
121  T_data &operator[](int index) { return h_data[index]; }
122  T_data *get_data() { return h_data; }
123  T_data *get_data_at(int index) { return &h_data[index]; }
124  const long *get_dims() { return dims_data; }
125  long get_dims(int i) { return dims_data[i]; }
126  int get_nb_elements() { return nb_elem; }
127 
129  int fill_from(const T_data *data);
130  int fill_into(T_data *data);
131  int fill(T_data value);
132 
133  std::string get_mem_alloc() {
134  switch (malloc_type) {
135  case MA_MALLOC:
136  return "MA_MALLOC";
137  case MA_PAGELOCK:
138  return "MA_PAGELOCK";
139  case MA_ZEROCPY:
140  return "MA_ZEROCPY";
141  case MA_PORTABLE:
142  return "MA_PORTABLE";
143  case MA_WRICOMB:
144  return "MA_WRICOMB";
145  case MA_GENEPIN:
146  return "MA_GENEPIN";
147  default:
148  return "MA_UNKNOWN";
149  }
150  }
151 };
152 
153 template <class T_data>
154 std::ostream &operator<<(std::ostream &os, CarmaHostObj<T_data> &obj) {
155  os << "-----------------------" << std::endl;
156  os << "CarmaHostObj<" << typeid(T_data).name() << "> object" << std::endl;
157  long ndims = obj.get_dims(0);
158  os << "ndims = " << ndims << std::endl;
159  for (long dim = 0; dim < ndims; dim++) {
160  os << "dim[" << dim << "] = " << obj.get_dims(dim + 1) << std::endl;
161  }
162  os << "nbElem = " << obj.get_nb_elements() << std::endl;
163  os << "sizeof(" << typeid(T_data).name() << ") = " << sizeof(T_data)
164  << std::endl;
165  os << "-----------------------" << std::endl;
166  return os;
167 }
168 
169 /*
170  extern "C" {
171 
172  }
173  */
174 
175 #endif // _CarmaHostObj_H_
MA_MALLOC
@ MA_MALLOC
Definition: carma_host_obj.h:53
CarmaHostObj::get_nb_elements
int get_nb_elements()
Definition: carma_host_obj.h:126
CarmaHostObj::get_dims
const long * get_dims()
Definition: carma_host_obj.h:124
MA_ZEROCPY
@ MA_ZEROCPY
Definition: carma_host_obj.h:55
carma_context.h
CarmaHostObj::get_devpntr
void get_devpntr(void **pntr_dev)
CarmaHostObj::get_mem_alloc
std::string get_mem_alloc()
Definition: carma_host_obj.h:133
CarmaHostObj::add_stream
int add_stream()
MA_WRICOMB
@ MA_WRICOMB
Definition: carma_host_obj.h:57
carma_utils.h
this file provides tools to CarmaObj
carma_streams.h
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data, MemAlloc malloc_type)
operator<<
std::ostream & operator<<(std::ostream &os, CarmaHostObj< T_data > &obj)
Definition: carma_host_obj.h:154
CarmaHostObj::nb_elem
int nb_elem
number of elments in the array
Definition: carma_host_obj.h:73
CarmaHostObj::get_data_at
T_data * get_data_at(int index)
Definition: carma_host_obj.h:123
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data, const T_data *data, MemAlloc malloc_type, int nb_streams)
CarmaHostObj::streams
CarmaStreams * streams
Definition: carma_host_obj.h:75
CarmaHostObj::CarmaHostObj
CarmaHostObj(const CarmaHostObj< T_data > *obj, MemAlloc malloc_type, int nb_streams)
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data, const T_data *data, int nb_streams)
CarmaHostObj::get_data
T_data * get_data()
Definition: carma_host_obj.h:122
MemAlloc
MemAlloc
Definition: carma_host_obj.h:52
CarmaHostObj::get_cuda_stream
cudaStream_t get_cuda_stream(int stream)
CarmaHostObj::add_stream
int add_stream(int nb)
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data, int nb_streams)
CarmaStreams
this class provides the stream features to CarmaObj
Definition: carma_streams.h:49
CarmaHostObj::CarmaHostObj
CarmaHostObj(const CarmaHostObj< T_data > *obj, MemAlloc malloc_type)
CarmaHostObj::get_dims
long get_dims(int i)
Definition: carma_host_obj.h:125
CarmaHostObj::data_UA
T_data * data_UA
unpadded input dara for generic pinned mem
Definition: carma_host_obj.h:71
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data)
MA_PORTABLE
@ MA_PORTABLE
Definition: carma_host_obj.h:56
CarmaHostObj::fill
int fill(T_data value)
CarmaObj
this class provides wrappers to the generic carma object
Definition: carma_host_obj.h:65
CarmaHostObj::wait_stream
int wait_stream(int stream)
CarmaHostObj::del_stream
int del_stream(int nb)
CarmaHostObj::malloc_type
MemAlloc malloc_type
type of host alloc
Definition: carma_host_obj.h:74
CarmaHostObj::cpy_obj
int cpy_obj(CarmaObj< T_data > *carma_obj, cudaMemcpyKind flag, unsigned int stream)
CarmaHostObj::~CarmaHostObj
~CarmaHostObj()
CarmaHostObj::fill_from
int fill_from(const T_data *data)
MA_PAGELOCK
@ MA_PAGELOCK
Definition: carma_host_obj.h:54
CarmaHostObj::get_nb_streams
int get_nb_streams()
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data, const T_data *data)
CarmaHostObj::CarmaHostObj
CarmaHostObj(const CarmaHostObj< T_data > *obj)
CarmaHostObj
this class provides wrappers to the generic carma host object
Definition: carma_host_obj.h:68
CarmaHostObj::dims_data
long * dims_data
dimensions of the array
Definition: carma_host_obj.h:72
CarmaHostObj::del_stream
int del_stream()
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data, const T_data *data, MemAlloc malloc_type)
MA_GENEPIN
@ MA_GENEPIN
Definition: carma_host_obj.h:58
CarmaHostObj::operator[]
T_data & operator[](int index)
Definition: carma_host_obj.h:121
CarmaHostObj::c_str
char const * c_str()
Definition: carma_host_obj.h:120
CarmaHostObj::wait_all_streams
int wait_all_streams()
CarmaHostObj::h_data
T_data * h_data
Input data.
Definition: carma_host_obj.h:70
CarmaHostObj::CarmaHostObj
CarmaHostObj(const CarmaHostObj< T_data > *obj, int nb_streams)
CarmaHostObj::cpy_obj
int cpy_obj(CarmaObj< T_data > *carma_obj, cudaMemcpyKind flag)
CarmaHostObj::init
void init(const long *dims_data, const T_data *data, MemAlloc malloc_type, int nb_streams)
CarmaHostObj::CarmaHostObj
CarmaHostObj(const long *dims_data, MemAlloc malloc_type, int nb_streams)
CarmaHostObj::fill_into
int fill_into(T_data *data)