-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAOloopControl_CompModes_loop.c
221 lines (138 loc) · 5.08 KB
/
AOloopControl_CompModes_loop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @file AOloopControl_aorun.c
* @brief AO loop Control compute functions
*
* REAL TIME COMPUTING ROUTINES
*
* @author O. Guyon
* @date 2018-01-04
*
*
* @bug No known bugs.
*
*/
#define _GNU_SOURCE
// uncomment for test print statements to stdout
//#define _PRINT_TEST
#include <time.h>
#include <string.h>
#include <sched.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_blas.h>
#include <pthread.h>
#include "info/info.h"
//libraries created by O. Guyon
#include "CommandLineInterface/CLIcore.h"
#include "AOloopControl/AOloopControl.h"
#include "00CORE/00CORE.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "AOloopControl_IOtools/AOloopControl_IOtools.h"
#ifdef HAVE_CUDA
#include "cudacomp/cudacomp.h"
#endif
# ifdef _OPENMP
# include <omp.h>
#define OMP_NELEMENT_LIMIT 1000000
# endif
#define NB_AOloopcontrol 10 // max number of loops
static int AOlooploadconf_init = 0;
extern AOLOOPCONTROL_CONF *AOconf; // configuration - this can be an array
extern AOloopControl_var aoloopcontrol_var;
int_fast8_t AOloopControl_CompModes_loop(const char *ID_CM_name, const char *ID_WFSref_name, const char *ID_WFSim_name, const char *ID_WFSimtot_name, const char *ID_coeff_name)
{
#ifdef HAVE_CUDA
int *GPUsetM;
long ID_CM;
long ID_WFSref;
long ID_coeff;
long GPUcnt;
int k;
int_fast8_t GPUstatus[100];
int_fast8_t status;
long NBmodes;
uint32_t *sizearray;
long ID_WFSim;
long ID_WFSim_n;
long wfsxsize, wfsysize;
int m;
long IDcoeff0;
long ID_WFSimtot;
double totfluxave;
long ID_coefft;
double alpha = 0.1;
char imname[200];
// LOG function start
int logfunc_level = 0;
int logfunc_level_max = 1;
char commentstring[200];
sprintf(commentstring, "loop %ld", aoloopcontrol_var.LOOPNUMBER);
CORE_logFunctionCall( logfunc_level, logfunc_level_max, 0, __FILE__, __func__, __LINE__, commentstring);
if(aoloopcontrol_var.aoconfID_looptiming == -1)
{
// LOOPiteration is written in cnt1 of loop timing array
if(sprintf(imname, "aol%ld_looptiming", aoloopcontrol_var.LOOPNUMBER) < 1)
printERROR(__FILE__, __func__, __LINE__, "sprintf wrote <1 char");
aoloopcontrol_var.aoconfID_looptiming = AOloopControl_IOtools_2Dloadcreate_shmim(imname, " ", aoloopcontrol_var.AOcontrolNBtimers, 1, 0.0);
}
GPUcnt = 2;
GPUsetM = (int*) malloc(sizeof(int)*GPUcnt);
for(k=0; k<GPUcnt; k++)
GPUsetM[k] = k+5;
ID_CM = image_ID(ID_CM_name);
wfsxsize = data.image[ID_CM].md[0].size[0];
wfsysize = data.image[ID_CM].md[0].size[1];
NBmodes = data.image[ID_CM].md[0].size[2];
ID_WFSref = image_ID(ID_WFSref_name);
sizearray = (uint32_t*) malloc(sizeof(uint32_t)*2);
sizearray[0] = NBmodes;
sizearray[1] = 1;
ID_coeff = create_image_ID(ID_coeff_name, 2, sizearray, _DATATYPE_FLOAT, 1, 0);
COREMOD_MEMORY_image_set_createsem(ID_coeff_name, 10);
data.image[ID_coeff].md[0].cnt0 = 0;
ID_coefft = create_image_ID("coefftmp", 2, sizearray, _DATATYPE_FLOAT, 1, 0);
COREMOD_MEMORY_image_set_createsem("coefftmp", 10);
IDcoeff0 = create_image_ID("coeff0", 2, sizearray, _DATATYPE_FLOAT, 1, 0);
ID_WFSim_n = create_2Dimage_ID("wfsim_n", wfsxsize, wfsysize);
COREMOD_MEMORY_image_set_createsem("wfsim_n", 10);
ID_WFSim = image_ID(ID_WFSim_name);
ID_WFSimtot = image_ID(ID_WFSimtot_name);
GPU_loop_MultMat_setup(2, ID_CM_name, "wfsim_n", "coefftmp", GPUcnt, GPUsetM, 0, 1, 1, 0);
totfluxave = 1.0;
int initWFSref;
for(;;)
{
if(initWFSref==0)
{
printf("Computing reference\n");
fflush(stdout);
memcpy(data.image[ID_WFSim_n].array.F, data.image[ID_WFSref].array.F, sizeof(float)*wfsxsize*wfsysize);
GPU_loop_MultMat_execute(2, &status, &GPUstatus[0], 1.0, 0.0, 0, 0);
for(m=0; m<NBmodes; m++)
{
data.image[IDcoeff0].array.F[m] = data.image[ID_coefft].array.F[m];
}
printf("\n");
initWFSref = 1;
printf("reference computed\n");
fflush(stdout);
}
memcpy(data.image[ID_WFSim_n].array.F, data.image[ID_WFSim].array.F, sizeof(float)*wfsxsize*wfsysize);
COREMOD_MEMORY_image_set_semwait(ID_WFSim_name, 0);
GPU_loop_MultMat_execute(2, &status, &GPUstatus[0], 1.0, 0.0, 0, 0);
totfluxave = (1.0-alpha)*totfluxave + alpha*data.image[ID_WFSimtot].array.F[0];
data.image[ID_coeff].md[0].write = 1;
for(m=0; m<NBmodes; m++)
data.image[ID_coeff].array.F[m] = data.image[ID_coefft].array.F[m]/totfluxave - data.image[IDcoeff0].array.F[m];
data.image[ID_coeff].md[0].cnt0 ++;
data.image[ID_coeff].md[0].cnt1 = data.image[aoloopcontrol_var.aoconfID_looptiming].md[0].cnt1;
data.image[ID_coeff].md[0].write = 0;
}
delete_image_ID("coeff0");
free(sizearray);
free(GPUsetM);
// LOG function start
CORE_logFunctionCall( logfunc_level, logfunc_level_max, 1, __FILE__, __func__, __LINE__, commentstring);
#endif
return(0);
}