COMPASS  5.4.4
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-2023 COMPASS Team <https://github.com/ANR-COMPASS>
5 // All rights reserved.
6 
7 // -----------------------------------------------------------------------------
8 
16 
17 #ifndef _SUTRA_CONTROLLER_H_
18 #define _SUTRA_CONTROLLER_H_
19 
20 #include <carma_cublas.h>
21 #include <carma_host_obj.h>
22 #include <sutra_centroider.h>
23 #include <sutra_dm.h>
24 #include <sutra_utils.h>
25 #include <sutra_wfs.h>
26 #include <mutex>
27 #include <tuple>
28 #include <deque>
29 
30 using std::map;
31 using std::mutex;
32 using std::string;
33 using std::tuple;
34 
35 template <typename Tcomp, typename Tout>
36 typename std::enable_if<std::is_same<Tcomp, Tout>::value, void>::type
38  volts = comClipped;
39 };
40 
41 template <typename Tcomp, typename Tout>
42 typename std::enable_if<!std::is_same<Tcomp, Tout>::value, void>::type
44  volts = new CarmaObj<Tout>(comClipped->get_context(), comClipped->get_dims());
45 };
46 
47 template <typename Tcomp, typename Tout>
49  public:
51  int device;
52 
53  int open_loop;
54  Tcomp delay;
55  Tcomp gain;
56  float volt_min;
57  float volt_max;
58  int nactus;
59  int nslopes;
60  Tout val_max;
61  Tcomp a; // Coefficient for linear interpolation on command buffer to allow
62  // non-integer delay
63  Tcomp b; // Coefficient for linear interpolation on command buffer to allow
64  // non-integer delay
65  Tcomp c; // Coefficient for linear interpolation on command buffer to allow
66  // non-integer delay
67  vector<SutraDm *> d_dmseen;
68  CarmaObj<Tcomp> *d_centroids; // current centroids
69  CarmaObj<Tcomp> *d_centroids_padded; // current centroids
70  CarmaObj<Tcomp> *d_com; // current command
71  CarmaObj<Tcomp> *d_com_padded; // current command
72  CarmaObj<Tcomp> *d_com_clipped; // current command
73  CarmaObj<Tout> *d_voltage; // commands after perturbation and clipping
74  CarmaObj<Tcomp> *d_com1; // commands k-1
75  vector<int> centro_idx; // Centroider indices to handle
76  std::deque<CarmaObj<Tcomp> *> d_circular_coms; //Circular buffer of commands for latency
77 
78 
79  map<string, tuple<CarmaObj<Tcomp> *, int, bool>> d_perturb_map;
80  // perturbation command buffer
81 
82  cudaStream_t mainStream;
83 
84  // allocation of d_centroids and d_com
86  float delay, SutraDms *dms, int *idx_dms, int ndm, int *idx_centro, int ncentro);
87  virtual ~SutraController();
88 
89  virtual string get_type() = 0;
90 
92  virtual int comp_com() = 0;
93 
94  // It is better to have something like this (+protected d_centroids):
95  // virtual int comp_com (CarmaObj<T> *new_centroids)=0;
96  // it would imply copy, but would be much safer
97 
98  inline int nactu() { return this->nactus; }
99  inline int nslope() { return this->nslopes; }
100 
101  cublasHandle_t cublas_handle() { return current_context->get_cublas_handle(); }
102 
103  void init_voltage() {
104  init_voltage_impl<Tcomp, Tout>(this->d_voltage, this->d_com_clipped);
105  };
106 
107  int set_centroids_ref(Tcomp *centroids_ref);
108  int add_perturb_voltage(string name, float *perturb, int N);
109  int set_perturb_voltage(string name, float *perturb, int N);
110  int remove_perturb_voltage(string name);
112  int enable_perturb_voltage(string name);
113  int disable_perturb_voltage(string name);
114  int set_com(float *com, int nElem);
115  int set_open_loop(int open_loop_status, bool rst = true);
119  int set_delay(float delay);
120  int set_volt_min(float volt_min);
121  int set_volt_max(float volt_max);
122  int set_val_max(float val_max);
123  int set_gain(float gain);
124  int reset_coms();
125 
126  // int syevd_f(char meth, CarmaObj<T> *d_U,
127  // CarmaHostObj<T> *h_eingenvals);
128  // int invgen(CarmaObj<T> *d_mat, T cond, int job);
130  int add_perturb();
131 
149 
150  protected:
152 };
153 
154 template <typename Tin, typename Tout, std::enable_if_t<std::is_same<Tin, Tout>::value, bool> = true>
155 void convert_to_voltage(Tin *d_idata, Tout *d_odata, int N, float volt_min, float volt_max,
156  uint16_t val_max, CarmaDevice *device, cudaStream_t stream){};
157 
158 template <typename Tin, typename Tout, std::enable_if_t<!std::is_same<Tin, Tout>::value, bool> = true>
159 void convert_to_voltage(Tin *d_idata, Tout *d_odata, int N, float volt_min, float volt_max,
160  uint16_t val_max, CarmaDevice *device, cudaStream_t stream);
161 
162 int shift_buf(float *d_data, int offset, int N, CarmaDevice *device);
163 int fill_filtmat(float *filter, int nactu, int N, CarmaDevice *device);
164 int TT_filt(float *mat, int n, CarmaDevice *device);
165 int fill_cmat(float *cmat, float *wtt, float *Mtt, int nactu, int nslopes,
166  CarmaDevice *device);
167 int do_statmat(float *statcov, long dim, float *xpos, float *ypos, float norm,
168  CarmaDevice *device);
169 
170 template <class T>
171 int get_pupphase(T *odata, float *idata, int *indx_pup, int Nphi,
172  CarmaDevice *device);
173 
174 int compute_Hcor_gpu(float *o_data, int nrow, int ncol, float Fs, float gmin,
175  float gmax, float delay, CarmaDevice *device);
176 int absnormfft(cuFloatComplex *idata, float *odata, int N, float norm,
177  CarmaDevice *device);
178 int adjust_csr_index(int *rowind, int *NNZ, int *nact, int nact_tot,
179  int row_off, CarmaDevice *device);
180 #endif // _SUTRA_CONTROLLER_H_
this file provides the cublas features to CarmaObj
this class provides the context in which CarmaObj are created
Definition: carma_context.h:79
cublasHandle_t get_cublas_handle()
CarmaContext * get_context()
Definition: carma_obj.h:220
const long * get_dims()
Definition: carma_obj.h:217
this class provides the controller features to COMPASS
int set_delay(float delay)
CarmaObj< Tcomp > * d_com1
int enable_perturb_voltage(string name)
int add_perturb_voltage(string name, float *perturb, int N)
int set_volt_max(float volt_max)
virtual string get_type()=0
int set_val_max(float val_max)
int reset_perturb_voltage()
cublasHandle_t cublas_handle()
CarmaObj< Tout > * d_voltage
int comp_polc(CarmaObj< Tcomp > &sk, CarmaObj< Tcomp > &iMat, CarmaObj< Tcomp > &ol_meas)
Compute the open loop measurements and effective commands.
CarmaObj< Tcomp > * d_com_padded
int set_centroids_ref(Tcomp *centroids_ref)
CarmaObj< Tcomp > * d_com
int set_com(float *com, int nElem)
virtual int comp_com()=0
!!! YOU MUST set d_centroids before calling it!!!!
CarmaObj< Tcomp > * d_com_clipped
int set_open_loop(int open_loop_status, bool rst=true)
vector< SutraDm * > d_dmseen
CarmaObj< Tcomp > * d_centroids
int set_volt_min(float volt_min)
CarmaContext * current_context
CarmaObj< Tcomp > * d_centroids_padded
int remove_perturb_voltage(string name)
int set_gain(float gain)
vector< int > centro_idx
cudaStream_t mainStream
int set_perturb_voltage(string name, float *perturb, int N)
virtual ~SutraController()
SutraController(CarmaContext *context, int nslope, int nactu, float delay, SutraDms *dms, int *idx_dms, int ndm, int *idx_centro, int ncentro)
map< string, tuple< CarmaObj< Tcomp > *, int, bool > > d_perturb_map
int disable_perturb_voltage(string name)
std::deque< CarmaObj< Tcomp > * > d_circular_coms
int fill_cmat(float *cmat, float *wtt, float *Mtt, int nactu, int nslopes, CarmaDevice *device)
void convert_to_voltage(Tin *d_idata, Tout *d_odata, int N, float volt_min, float volt_max, uint16_t val_max, CarmaDevice *device, cudaStream_t stream)
int adjust_csr_index(int *rowind, int *NNZ, int *nact, int nact_tot, int row_off, CarmaDevice *device)
int do_statmat(float *statcov, long dim, float *xpos, float *ypos, float norm, CarmaDevice *device)
int fill_filtmat(float *filter, int nactu, int N, CarmaDevice *device)
int absnormfft(cuFloatComplex *idata, float *odata, int N, float norm, CarmaDevice *device)
std::enable_if< std::is_same< Tcomp, Tout >::value, void >::type init_voltage_impl(CarmaObj< Tout > *&volts, CarmaObj< Tcomp > *comClipped)
int compute_Hcor_gpu(float *o_data, int nrow, int ncol, float Fs, float gmin, float gmax, float delay, CarmaDevice *device)
int get_pupphase(T *odata, float *idata, int *indx_pup, int Nphi, CarmaDevice *device)
int shift_buf(float *d_data, int offset, int N, CarmaDevice *device)
int TT_filt(float *mat, int n, CarmaDevice *device)