COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
sutra_controller.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 _SUTRA_CONTROLLER_H_
43 #define _SUTRA_CONTROLLER_H_
44 
45 #include <carma_cublas.h>
46 #include <carma_host_obj.h>
47 #include <sutra_centroider.h>
48 #include <sutra_dm.h>
49 #include <sutra_utils.h>
50 #include <sutra_wfs.h>
51 #include <mutex>
52 #include <tuple>
53 #include <deque>
54 
55 using std::map;
56 using std::mutex;
57 using std::string;
58 using std::tuple;
59 
60 template <typename Tcomp, typename Tout>
61 typename std::enable_if<std::is_same<Tcomp, Tout>::value, void>::type
63  volts = comClipped;
64 };
65 
66 template <typename Tcomp, typename Tout>
67 typename std::enable_if<!std::is_same<Tcomp, Tout>::value, void>::type
69  volts = new CarmaObj<Tout>(comClipped->get_context(), comClipped->get_dims());
70 };
71 
72 template <typename Tcomp, typename Tout>
74  public:
76  int device;
77 
78  int open_loop;
79  Tcomp delay;
80  Tcomp gain;
81  float volt_min;
82  float volt_max;
83  int nactus;
84  int nslopes;
85  Tout val_max;
86  Tcomp a; // Coefficient for linear interpolation on command buffer to allow
87  // non-integer delay
88  Tcomp b; // Coefficient for linear interpolation on command buffer to allow
89  // non-integer delay
90  Tcomp c; // Coefficient for linear interpolation on command buffer to allow
91  // non-integer delay
92  vector<SutraDm *> d_dmseen;
93  CarmaObj<Tcomp> *d_centroids; // current centroids
94  CarmaObj<Tcomp> *d_centroids_padded; // current centroids
95  CarmaObj<Tcomp> *d_com; // current command
96  CarmaObj<Tcomp> *d_com_padded; // current command
97  CarmaObj<Tcomp> *d_com_clipped; // current command
98  CarmaObj<Tout> *d_voltage; // commands after perturbation and clipping
99  CarmaObj<Tcomp> *d_com1; // commands k-1
100  vector<int> centro_idx; // Centroider indices to handle
101  std::deque<CarmaObj<Tcomp> *> d_circular_coms; //Circular buffer of commands for latency
102 
103 
104  map<string, tuple<CarmaObj<Tcomp> *, int, bool>> d_perturb_map;
105  // perturbation command buffer
106 
108 
109  // allocation of d_centroids and d_com
110  SutraController(CarmaContext *context, int nvalid, int nslope, int nactu,
111  float delay, SutraDms *dms, int *idx_dms, int ndm, int *idx_centro, int ncentro);
112  virtual ~SutraController();
113 
114  virtual string get_type() = 0;
115 
117  virtual int comp_com() = 0;
118 
119  // It is better to have something like this (+protected d_centroids):
120  // virtual int comp_com (CarmaObj<T> *new_centroids)=0;
121  // it would imply copy, but would be much safer
122 
123  inline int nactu() { return this->nactus; }
124  inline int nslope() { return this->nslopes; }
125 
126  cublasHandle_t cublas_handle() { return current_context->get_cublas_handle(); }
127 
128  void init_voltage() {
129  init_voltage_impl<Tcomp, Tout>(this->d_voltage, this->d_com_clipped);
130  };
131 
132  int set_centroids_ref(Tcomp *centroids_ref);
133  int add_perturb_voltage(string name, float *perturb, int N);
134  int set_perturb_voltage(string name, float *perturb, int N);
135  int remove_perturb_voltage(string name);
137  int enable_perturb_voltage(string name);
138  int disable_perturb_voltage(string name);
139  int set_com(float *com, int nElem);
140  int set_open_loop(int open_loop_status, bool rst = true);
144  int set_delay(float delay);
145  int set_volt_min(float volt_min);
146  int set_volt_max(float volt_max);
147  int set_val_max(float val_max);
148  int set_gain(float gain);
149  int reset_coms();
150 
151  // int syevd_f(char meth, CarmaObj<T> *d_U,
152  // CarmaHostObj<T> *h_eingenvals);
153  // int invgen(CarmaObj<T> *d_mat, T cond, int job);
155  int add_perturb();
156 
157  protected:
159 };
160 
161 template <typename Tin, typename Tout>
162 typename std::enable_if<std::is_same<Tin, Tout>::value, void>::type
163 convert_to_voltage(Tin *d_idata, Tout *d_odata, int N, float volt_min, float volt_max,
164  uint16_t val_max, CarmaDevice *device){};
165 
166 template <typename Tin, typename Tout>
167 typename std::enable_if<!std::is_same<Tin, Tout>::value, void>::type
168 convert_to_voltage(Tin *d_idata, Tout *d_odata, int N, float volt_min, float volt_max,
169  uint16_t val_max, CarmaDevice *device);
170 
171 int shift_buf(float *d_data, int offset, int N, CarmaDevice *device);
172 int fill_filtmat(float *filter, int nactu, int N, CarmaDevice *device);
173 int TT_filt(float *mat, int n, CarmaDevice *device);
174 int fill_cmat(float *cmat, float *wtt, float *Mtt, int nactu, int nslopes,
175  CarmaDevice *device);
176 int do_statmat(float *statcov, long dim, float *xpos, float *ypos, float norm,
177  CarmaDevice *device);
178 
179 template <class T>
180 int get_pupphase(T *odata, float *idata, int *indx_pup, int Nphi,
181  CarmaDevice *device);
182 
183 int compute_Hcor_gpu(float *o_data, int nrow, int ncol, float Fs, float gmin,
184  float gmax, float delay, CarmaDevice *device);
185 int absnormfft(cuFloatComplex *idata, float *odata, int N, float norm,
186  CarmaDevice *device);
187 int adjust_csr_index(int *rowind, int *NNZ, int *nact, int nact_tot,
188  int row_off, CarmaDevice *device);
189 #endif // _SUTRA_CONTROLLER_H_
SutraController::disable_perturb_voltage
int disable_perturb_voltage(string name)
sutra_centroider.h
SutraController::add_perturb_voltage
int add_perturb_voltage(string name, float *perturb, int N)
SutraController::command_delay
int command_delay()
carma_cublas.h
this file provides the cublas features to CarmaObj
SutraController::delay
Tcomp delay
Definition: sutra_controller.h:79
CarmaObj::get_dims
const long * get_dims()
Definition: carma_obj.h:239
CarmaDevice
Definition: carma_context.h:57
do_statmat
int do_statmat(float *statcov, long dim, float *xpos, float *ypos, float norm, CarmaDevice *device)
SutraController::clip_commands
int clip_commands()
SutraController::set_open_loop
int set_open_loop(int open_loop_status, bool rst=true)
SutraController::~SutraController
virtual ~SutraController()
SutraController::set_centroids_ref
int set_centroids_ref(Tcomp *centroids_ref)
shift_buf
int shift_buf(float *d_data, int offset, int N, CarmaDevice *device)
SutraController::d_voltage
CarmaObj< Tout > * d_voltage
Definition: sutra_controller.h:98
SutraDms
Definition: sutra_dm.h:164
absnormfft
int absnormfft(cuFloatComplex *idata, float *odata, int N, float norm, CarmaDevice *device)
SutraController::enable_perturb_voltage
int enable_perturb_voltage(string name)
SutraController::centro_idx
vector< int > centro_idx
Definition: sutra_controller.h:100
SutraController::b
Tcomp b
Definition: sutra_controller.h:88
SutraController::d_perturb_map
map< string, tuple< CarmaObj< Tcomp > *, int, bool > > d_perturb_map
Definition: sutra_controller.h:104
SutraController::d_com1
CarmaObj< Tcomp > * d_com1
Definition: sutra_controller.h:99
SutraController::volt_min
float volt_min
Definition: sutra_controller.h:81
CarmaObj::get_context
CarmaContext * get_context()
Definition: carma_obj.h:242
adjust_csr_index
int adjust_csr_index(int *rowind, int *NNZ, int *nact, int nact_tot, int row_off, CarmaDevice *device)
SutraController::c
Tcomp c
Definition: sutra_controller.h:90
init_voltage_impl
std::enable_if< std::is_same< Tcomp, Tout >::value, void >::type init_voltage_impl(CarmaObj< Tout > *&volts, CarmaObj< Tcomp > *comClipped)
Definition: sutra_controller.h:62
SutraController::d_com_padded
CarmaObj< Tcomp > * d_com_padded
Definition: sutra_controller.h:96
SutraController::comp_com
virtual int comp_com()=0
!!! YOU MUST set d_centroids before calling it!!!!
SutraController::gain
Tcomp gain
Definition: sutra_controller.h:80
fill_filtmat
int fill_filtmat(float *filter, int nactu, int N, CarmaDevice *device)
SutraController::d_centroids
CarmaObj< Tcomp > * d_centroids
Definition: sutra_controller.h:93
sutra_wfs.h
SutraController::d_com_clipped
CarmaObj< Tcomp > * d_com_clipped
Definition: sutra_controller.h:97
SutraController::reset_coms
int reset_coms()
SutraController::set_volt_min
int set_volt_min(float volt_min)
get_pupphase
int get_pupphase(T *odata, float *idata, int *indx_pup, int Nphi, CarmaDevice *device)
SutraController::init_voltage
void init_voltage()
Definition: sutra_controller.h:128
CarmaStreams
this class provides the stream features to CarmaObj
Definition: carma_streams.h:49
SutraController::d_dmseen
vector< SutraDm * > d_dmseen
Definition: sutra_controller.h:92
fill_cmat
int fill_cmat(float *cmat, float *wtt, float *Mtt, int nactu, int nslopes, CarmaDevice *device)
SutraController::get_type
virtual string get_type()=0
SutraController::device
int device
Definition: sutra_controller.h:76
CarmaObj< Tout >
SutraController::set_val_max
int set_val_max(float val_max)
SutraController::comp_latency
int comp_latency()
SutraController::set_delay
int set_delay(float delay)
SutraController::d_circular_coms
std::deque< CarmaObj< Tcomp > * > d_circular_coms
Definition: sutra_controller.h:101
CarmaContext
this class provides the context in which CarmaObj are created
Definition: carma_context.h:104
SutraController::volt_max
float volt_max
Definition: sutra_controller.h:82
SutraController::d_centroids_padded
CarmaObj< Tcomp > * d_centroids_padded
Definition: sutra_controller.h:94
CarmaContext::get_cublas_handle
cublasHandle_t get_cublas_handle()
Definition: carma_context.h:168
SutraController::reset_perturb_voltage
int reset_perturb_voltage()
SutraController::nslope
int nslope()
Definition: sutra_controller.h:124
SutraController::open_loop
int open_loop
Definition: sutra_controller.h:78
compute_Hcor_gpu
int compute_Hcor_gpu(float *o_data, int nrow, int ncol, float Fs, float gmin, float gmax, float delay, CarmaDevice *device)
SutraController::val_max
Tout val_max
Definition: sutra_controller.h:85
convert_to_voltage
std::enable_if< std::is_same< Tin, Tout >::value, void >::type convert_to_voltage(Tin *d_idata, Tout *d_odata, int N, float volt_min, float volt_max, uint16_t val_max, CarmaDevice *device)
Definition: sutra_controller.h:163
sutra_utils.h
SutraController::nslopes
int nslopes
Definition: sutra_controller.h:84
SutraController::d_com
CarmaObj< Tcomp > * d_com
Definition: sutra_controller.h:95
TT_filt
int TT_filt(float *mat, int n, CarmaDevice *device)
SutraController::comp_voltage_mutex
mutex comp_voltage_mutex
Definition: sutra_controller.h:158
SutraController::cublas_handle
cublasHandle_t cublas_handle()
Definition: sutra_controller.h:126
SutraController::a
Tcomp a
Definition: sutra_controller.h:86
SutraController::add_perturb
int add_perturb()
SutraController::nactus
int nactus
Definition: sutra_controller.h:83
sutra_dm.h
SutraController::streams
CarmaStreams * streams
Definition: sutra_controller.h:107
SutraController::nactu
int nactu()
Definition: sutra_controller.h:123
SutraController::set_com
int set_com(float *com, int nElem)
SutraController::current_context
CarmaContext * current_context
Definition: sutra_controller.h:75
SutraController::set_perturb_voltage
int set_perturb_voltage(string name, float *perturb, int N)
SutraController::remove_perturb_voltage
int remove_perturb_voltage(string name)
SutraController::SutraController
SutraController(CarmaContext *context, int nvalid, int nslope, int nactu, float delay, SutraDms *dms, int *idx_dms, int ndm, int *idx_centro, int ncentro)
SutraController::comp_voltage
int comp_voltage()
SutraController::set_gain
int set_gain(float gain)
SutraController::set_volt_max
int set_volt_max(float volt_max)
SutraController
this class provides the controller features to COMPASS
Definition: sutra_controller.h:73
carma_host_obj.h