-
Notifications
You must be signed in to change notification settings - Fork 0
/
cudacomp_MVMextractModesLoop.c
1466 lines (1051 loc) · 45.7 KB
/
cudacomp_MVMextractModesLoop.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file cudacomp_MVMextractModesLoop.c
* @brief CUDA functions wrapper
*
* Requires CUDA library
*
*/
// include sem_timedwait
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
/* =============================================================================================== */
/* =============================================================================================== */
/* HEADER FILES */
/* =============================================================================================== */
/* =============================================================================================== */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
#include <sched.h>
#include <signal.h>
#include <semaphore.h>
#include <time.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/types.h>
#ifdef HAVE_CUDA
#include <cuda_runtime_api.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <device_types.h>
#include <pthread.h>
#include <cusolverDn.h>
#endif
#include "CommandLineInterface/CLIcore.h"
#include "CommandLineInterface/timeutils.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_iofits/COREMOD_iofits.h"
#include "COREMOD_arith/COREMOD_arith.h"
#include "COREMOD_tools/COREMOD_tools.h"
//#include "cudacomp/cudacomp.h"
#include "linopt_imtools/linopt_imtools.h" // for testing
#ifdef HAVE_CUDA
// ==========================================
// Forward declaration(s)
// ==========================================
errno_t CUDACOMP_MVMextractModesLoop_FPCONF();
errno_t __attribute__((hot)) CUDACOMP_MVMextractModesLoop_RUN();
int __attribute__((hot)) CUDACOMP_MVMextractModesLoop(
const char *in_stream, // input stream
const char *intot_stream, // [optional] input normalization stream
const char *IDmodes_name, // Modes matrix
const char *IDrefin_name, // [optional] input reference - to be subtracted
const char *IDrefout_name, // [optional] output reference - to be added
const char *IDmodes_val_name, // ouput stream
int GPUindex, // GPU index
int PROCESS, // 1 if postprocessing
int TRACEMODE, // 1 if writing trace
int MODENORM, // 1 if input modes should be normalized
int insem, // input semaphore index
int axmode, // 0 for normal mode extraction, 1 for expansion
long twait, // if >0, insert time wait [us] at each iteration
int semwarn // 1 if warning when input stream semaphore >1
);
// ==========================================
// Command line interface wrapper function(s)
// ==========================================
static errno_t CUDACOMP_MVMextractModesLoop_cli()
{
// try FPS implementation
// set data.fpsname, providing default value as first arg, and set data.FPS_CMDCODE value
// default FPS name will be used if CLI process has NOT been named
// see code in function_parameter.c for detailed rules
function_parameter_getFPSargs_from_CLIfunc("cudaMVM");
if(data.FPS_CMDCODE != 0) // use FPS implementation
{
// set pointers to CONF and RUN functions
data.FPS_CONFfunc = CUDACOMP_MVMextractModesLoop_FPCONF;
data.FPS_RUNfunc = CUDACOMP_MVMextractModesLoop_RUN;
function_parameter_execFPScmd();
return RETURN_SUCCESS;
}
// non FPS implementation - all parameters specified at function launch
if(
CLI_checkarg(1, CLIARG_IMG) +
CLI_checkarg(2, CLIARG_STR) +
CLI_checkarg(3, CLIARG_IMG) +
CLI_checkarg(4, CLIARG_STR) +
CLI_checkarg(5, CLIARG_STR) +
CLI_checkarg(6, CLIARG_STR) +
CLI_checkarg(7, CLIARG_LONG) +
CLI_checkarg(8, CLIARG_LONG) +
CLI_checkarg(9, CLIARG_LONG) +
CLI_checkarg(10, CLIARG_LONG) +
CLI_checkarg(11, CLIARG_LONG) +
CLI_checkarg(12, CLIARG_LONG) +
CLI_checkarg(13, CLIARG_LONG) +
CLI_checkarg(14, CLIARG_LONG)
== 0)
{
CUDACOMP_MVMextractModesLoop(
data.cmdargtoken[1].val.string,
data.cmdargtoken[2].val.string,
data.cmdargtoken[3].val.string,
data.cmdargtoken[4].val.string,
data.cmdargtoken[5].val.string,
data.cmdargtoken[6].val.string,
data.cmdargtoken[7].val.numl,
data.cmdargtoken[8].val.numl,
data.cmdargtoken[9].val.numl,
data.cmdargtoken[10].val.numl,
data.cmdargtoken[11].val.numl,
data.cmdargtoken[12].val.numl,
data.cmdargtoken[13].val.numl,
data.cmdargtoken[14].val.numl
);
return RETURN_SUCCESS;
}
else
{
return RETURN_FAILURE;
}
}
// ==========================================
// Register CLI command(s)
// ==========================================
errno_t cudacomp_MVMextractModesLoop_addCLIcmd()
{
RegisterCLIcommand(
"cudaextrmodes",
__FILE__,
CUDACOMP_MVMextractModesLoop_cli,
"CUDA extract mode values loop. Note that intot and refout parameters can be NULL",
"<inval stream> <intot stream> <modes> <refin val> <refout_val> <outmode vals> <GPU index [long]> <PROCESS flag> <TRACEMODE flag> <MODE norm flag> <input semaphore> <axis orientation> <twait [us]> <semwarn>",
"cudaextrmodes inmap inmaptot modes imref imoutref modeval 3 1 1 1 3 0 0",
"int CUDACOMP_MVMextractModesLoop(const char *in_stream, const char *intot_stream, const char *IDmodes_name, const char *IDrefin_name, const char *IDrefout_name, const char *IDmodes_val_name, int GPUindex, int PROCESS, int TRACEMODE, int MODENORM, int insem, int axmode, long twait, int semwarn)"
);
return RETURN_SUCCESS;
}
//
// manages configuration parameters
// initializes configuration parameters structure
//
errno_t CUDACOMP_MVMextractModesLoop_FPCONF()
{
FPS_SETUP_INIT(data.FPS_name, data.FPS_CMDCODE); // sets up fps
fps_add_processinfo_entries(&fps);
// ===========================
// ALLOCATE FPS ENTRIES
// ===========================
void *pNull = NULL;
uint64_t FPFLAG;
FPFLAG = FPFLAG_DEFAULT_INPUT;
FPFLAG &= FPFLAG_WRITECONF;
FPFLAG &= ~FPFLAG_WRITERUN;
long GPUindex_default[4] = { 0, 0, 9, 0 };
//long fp_GPUindex = 0;
function_parameter_add_entry(&fps, ".GPUindex", "GPU index",
FPTYPE_INT64, FPFLAG_DEFAULT_INPUT, &GPUindex_default, NULL);
//long fp_streamname_in = 0;
function_parameter_add_entry(&fps, ".sname_in", "input stream vector",
FPTYPE_STREAMNAME, FPFLAG_DEFAULT_INPUT_STREAM, pNull, NULL);
//long fp_streamname_modes = 0;
function_parameter_add_entry(&fps, ".sname_modes", "input modes matrix",
FPTYPE_STREAMNAME, FPFLAG_DEFAULT_INPUT_STREAM, pNull, NULL);
FPFLAG = FPFLAG_DEFAULT_INPUT_STREAM;
FPFLAG &= ~FPFLAG_STREAM_RUN_REQUIRED;
//long fp_streamname_intot = 0;
function_parameter_add_entry(&fps, ".option.sname_intot", "optional input normalization stream",
FPTYPE_STREAMNAME, FPFLAG, pNull, NULL);
//long fp_streamname_refin = 0;
function_parameter_add_entry(&fps, ".option.sname_refin", "optional input reference to be subtracted stream",
FPTYPE_STREAMNAME, FPFLAG, pNull, NULL);
//long fp_streamname_refout = 0;
function_parameter_add_entry(&fps, ".option.sname_refout", "optional output reference to be subtracted stream",
FPTYPE_STREAMNAME, FPFLAG, pNull, NULL);
//long fp_stream_outmodesval = 0;
function_parameter_add_entry(&fps, ".sname_outmodesval", "output mode coefficients stream",
FPTYPE_STREAMNAME, FPFLAG, pNull, NULL);
//long fp_outinit = 0;
function_parameter_add_entry(&fps, ".outinit", "output stream init mode",
FPTYPE_ONOFF, FPFLAG, pNull, NULL);
//long fp_PROCESS = 0;
function_parameter_add_entry(&fps, ".option.PROCESS", "1 if processing",
FPTYPE_ONOFF, FPFLAG_DEFAULT_INPUT, pNull, NULL);
//long fp_TRACEMODE = 0;
function_parameter_add_entry(&fps, ".option.TRACEMODE", "1 if writing trace",
FPTYPE_ONOFF, FPFLAG_DEFAULT_INPUT, pNull, NULL);
//long fp_MODENORM = 0;
function_parameter_add_entry(&fps, ".option.MODENORM", "1 if input modes should be normalized",
FPTYPE_ONOFF, FPFLAG_DEFAULT_INPUT, pNull, NULL);
//long fp_insem = 0;
function_parameter_add_entry(&fps, ".option.insem", "input semaphore index",
FPTYPE_INT64, FPFLAG_DEFAULT_INPUT, pNull, NULL);
//long fp_axmode = 0;
function_parameter_add_entry(&fps, ".option.axmode", "0 for normal mode extraction, 1 for expansion",
FPTYPE_INT64, FPFLAG_DEFAULT_INPUT, pNull, NULL);
//long fp_twait = 0;
function_parameter_add_entry(&fps, ".option.twait", "if >0, insert time wait [us] at each iteration",
FPTYPE_INT64, FPFLAG_DEFAULT_INPUT|FPFLAG_WRITERUN, pNull, NULL);
//long fp_semwarn = 0;
function_parameter_add_entry(&fps, ".option.semwarn", "issue warning when input stream semaphore >1",
FPTYPE_ONOFF, FPFLAG_DEFAULT_INPUT, pNull, NULL);
// ==============================================
// ======== START FPS CONF LOOP =================
// ==============================================
FPS_CONFLOOP_START // macro in function_parameter.h
// ==============================================
// ======== STOP FPS CONF LOOP ==================
// ==============================================
FPS_CONFLOOP_END // macro in function_parameter.h
return RETURN_SUCCESS;
}
/**
* @brief MVM, GPU-based
*
*
* Used for AO application, single GPU
* This is meant to be used as stand-alone MVM process managed by cacao
*
*
*
*
* [axmode 0] Converting WFS image to modes
* Input is 2D (WFS)
* Output is 1D (modes)
*
* Matrix is 3D
* (size[0], size[1]) = (sizeWFS[0], sizeWFS[1])
* (size[2]) = NBmodes
*
*
*
*
*
* [axmode 1] Expanding DM vector to WFS vector
* Input is 2D vector (DM)
* Output is 2D vector (WFS)
*
* Matrix is 3D.
* (size[0], size[1]) = (sizeWFS[0], sizeWFS[1])
* (size[2]) = sizeDM[0] x sizeDM[1]
*
* Matrix internally remapped to :
* (size[0], size[1]) = (sizeDM[0], sizeDM[1])
* (size[2]) = sizeWFS[0] x sizeWFS[1]
*
*
*/
errno_t __attribute__((hot)) CUDACOMP_MVMextractModesLoop_RUN()
{
imageID IDmodes;
imageID ID;
imageID ID_modeval;
cublasHandle_t cublasH = NULL;
cublasStatus_t cublas_status = CUBLAS_STATUS_SUCCESS;
cudaError_t cudaStat = cudaSuccess;
struct cudaDeviceProp deviceProp;
int m, n;
int k;
uint32_t *arraytmp;
float *d_modes = NULL; // linear memory of GPU
float *d_in = NULL;
float *d_modeval = NULL;
float alpha = 1.0;
float beta = 0.0;
//long scnt;
int semval;
long ii, jj, kk;
long NBmodes;
float *normcoeff;
//imageID IDoutact;
uint32_t *sizearraytmp;
//imageID ID_modeval_mult;
int imOK;
char traceim_name[STRINGMAXLEN_IMGNAME];
long TRACEsize = 2000;
long TRACEindex = 0;
imageID IDtrace;
uint32_t NBaveSTEP = 10; // each step is 2x longer average than previous step
double stepcoeff;
double stepcoeff0 = 0.3;
char process_ave_name[STRINGMAXLEN_IMGNAME];
char process_rms_name[STRINGMAXLEN_IMGNAME];
imageID IDprocave;
imageID IDprocrms;
long step;
double tmpv;
float *modevalarray;
float *modevalarrayref;
int initref = 0; // 1 when reference has been processed
int BETAMODE = 0;
imageID IDrefout;
uint32_t refindex;
long twait1;
struct timespec t0;
struct timespec t00;
struct timespec t01;
struct timespec t02;
struct timespec t03;
struct timespec t04;
struct timespec t05;
struct timespec t06;
struct timespec t1;
int MODEVALCOMPUTE = 1; // 1 if compute, 0 if import
int RT_priority = 91; //any number from 0-99
FPS_CONNECT(data.FPS_name, FPSCONNECT_RUN);
// ===============================
// GET FUNCTION PARAMETER VALUES
// ===============================
char in_stream[STRINGMAXLEN_IMGNAME];
strncpy(in_stream, functionparameter_GetParamPtr_STRING(&fps, ".sname_in"), FUNCTION_PARAMETER_STRMAXLEN);
char IDmodes_name[STRINGMAXLEN_IMGNAME];
strncpy(IDmodes_name, functionparameter_GetParamPtr_STRING(&fps, ".sname_modes"), FUNCTION_PARAMETER_STRMAXLEN);
char intot_stream[STRINGMAXLEN_IMGNAME];
strncpy(intot_stream, functionparameter_GetParamPtr_STRING(&fps, ".option.sname_intot"), FUNCTION_PARAMETER_STRMAXLEN);
char IDrefin_name[STRINGMAXLEN_IMGNAME];
strncpy(IDrefin_name, functionparameter_GetParamPtr_STRING(&fps, ".option.sname_refin"), FUNCTION_PARAMETER_STRMAXLEN);
char IDrefout_name[STRINGMAXLEN_IMGNAME];
strncpy(IDrefout_name, functionparameter_GetParamPtr_STRING(&fps, ".option.sname_refout"), FUNCTION_PARAMETER_STRMAXLEN);
char IDmodes_val_name[STRINGMAXLEN_IMGNAME];
strncpy(IDmodes_val_name, functionparameter_GetParamPtr_STRING(&fps, ".sname_outmodesval"), FUNCTION_PARAMETER_STRMAXLEN);
int outinit = functionparameter_GetParamValue_ONOFF(&fps, ".outinit");
int GPUindex = functionparameter_GetParamValue_INT64(&fps, ".GPUindex");
int PROCESS = functionparameter_GetParamValue_ONOFF(&fps, ".option.PROCESS");
int TRACEMODE = functionparameter_GetParamValue_ONOFF(&fps, ".option.TRACEMODE");
int MODENORM = functionparameter_GetParamValue_ONOFF(&fps, ".option.MODENORM");
int insem = functionparameter_GetParamValue_INT64(&fps, ".option.insem");
int axmode = functionparameter_GetParamValue_INT64(&fps, ".option.axmode");
long *twait = functionparameter_GetParamPtr_INT64(&fps, ".option.twait");
int semwarn = functionparameter_GetParamValue_ONOFF(&fps, ".option.semwarn");
// ===============================
// Review input parameters
// ===============================
printf("\n");
printf("in_stream : %16s input stream\n", in_stream);
printf("intot_stream : %16s [optional] input normalization stream\n", intot_stream);
printf("IDmodes_name : %16s Modes\n", IDmodes_name);
printf("IDrefin_name : %16s [optional] input reference - to be subtracted\n", IDrefin_name);
printf("IDrefout_name : %16s [optional] output reference - to be added\n", IDrefout_name);
printf("IDmodes_val_name : %16s ouput stream\n", IDmodes_val_name);
printf("GPUindex : %16d GPU index\n", GPUindex);
printf("PROCESS : %16d 1 if postprocessing\n", PROCESS);
printf("TRACEMODE : %16d 1 if writing trace\n", TRACEMODE);
printf("MODENORM : %16d 1 if input modes should be normalized\n", MODENORM);
printf("insem : %16d input semaphore index\n", insem);
printf("axmode : %16d 0 for normal mode extraction, 1 for expansion\n", axmode);
printf("twait : %16ld if >0, insert time wait [us] at each iteration\n", *twait);
printf("semwarn : %16d 1 if warning when input stream semaphore >1\n", semwarn);
printf("\n");
// ===========================
// processinfo support
// ===========================
char pinfoname[STRINGMAXLEN_PROCESSINFO_NAME];
{
int slen = snprintf(pinfoname, STRINGMAXLEN_PROCESSINFO_NAME, "cudaMVMextract-%s", in_stream);
if(slen<1) {
PRINT_ERROR("snprintf wrote <1 char");
abort(); // can't handle this error any other way
}
if(slen >= STRINGMAXLEN_PROCESSINFO_NAME) {
PRINT_ERROR("snprintf string truncation");
abort(); // can't handle this error any other way
}
}
char pinfodescr[STRINGMAXLEN_PROCESSINFO_DESCRIPTION];
{
int slen = snprintf(pinfodescr, STRINGMAXLEN_PROCESSINFO_DESCRIPTION, "%s->%s", in_stream, IDmodes_val_name);
if(slen<1) {
PRINT_ERROR("snprintf wrote <1 char");
abort(); // can't handle this error any other way
}
if(slen >= STRINGMAXLEN_PROCESSINFO_DESCRIPTION) {
PRINT_ERROR("snprintf string truncation");
abort(); // can't handle this error any other way
}
}
char pinfomsg[STRINGMAXLEN_PROCESSINFO_STATUSMSG];
{
int slen = snprintf(pinfomsg, STRINGMAXLEN_PROCESSINFO_STATUSMSG, "Setup");
if(slen<1) {
PRINT_ERROR("snprintf wrote <1 char");
abort(); // can't handle this error any other way
}
if(slen >= STRINGMAXLEN_PROCESSINFO_STATUSMSG) {
PRINT_ERROR("snprintf string truncation");
abort(); // can't handle this error any other way
}
}
PROCESSINFO *processinfo;
processinfo = processinfo_setup(
pinfoname, // short name for the processinfo instance, no spaces, no dot, name should be human-readable
pinfodescr, // description
pinfomsg, // message on startup
__FUNCTION__, __FILE__, __LINE__
);
// OPTIONAL SETTINGS
processinfo->MeasureTiming = 1; // Measure timing
processinfo->RT_priority = RT_priority; // RT_priority, 0-99. Larger number = higher priority. If <0, ignore
int loopOK = 1;
// ===========================
// INITIALIZATIONS
// ===========================
// CONNECT TO INPUT STREAM
long IDin;
IDin = image_ID(in_stream);
// ERROR HANDLING
if(IDin == -1) {
struct timespec errtime;
struct tm *errtm;
clock_gettime(CLOCK_REALTIME, &errtime);
errtm = gmtime(&errtime.tv_sec);
fprintf(stderr,
"%02d:%02d:%02d.%09ld ERROR [%s %s %d] Input stream %s does not exist, cannot proceed\n",
errtm->tm_hour,
errtm->tm_min,
errtm->tm_sec,
errtime.tv_nsec,
__FILE__,
__FUNCTION__,
__LINE__,
in_stream);
return 1;
}
m = data.image[IDin].md[0].size[0] * data.image[IDin].md[0].size[1];
COREMOD_MEMORY_image_set_createsem(in_stream, 10);
// NORMALIZATION
// CONNECT TO TOTAL FLUX STREAM
long IDintot;
IDintot = image_ID(intot_stream);
int INNORMMODE = 0; // 1 if input normalized
if(IDintot == -1) {
INNORMMODE = 0;
create_2Dimage_ID("intot_tmp", 1, 1, &IDintot);
data.image[IDintot].array.F[0] = 1.0;
} else {
INNORMMODE = 1;
}
// CONNECT TO WFS REFERENCE STREAM
long IDref;
IDref = image_ID(IDrefin_name);
if(IDref == -1) {
create_2Dimage_ID("_tmprefin", data.image[IDin].md[0].size[0], data.image[IDin].md[0].size[1], &IDref);
for(ii = 0; ii < data.image[IDin].md[0].size[0]*data.image[IDin].md[0].size[1]; ii++) {
data.image[IDref].array.F[ii] = 0.0;
}
}
if(axmode == 0) {
//
// Extract modes.
// This is the default geometry, no need to remap
//
IDmodes = image_ID(IDmodes_name);
n = data.image[IDmodes].md[0].size[2];
NBmodes = n;
} else {
//
// Expand from DM to WFS
// Remap to new matrix tmpmodes
//
ID = image_ID(IDmodes_name);
printf("Modes: ID = %ld %s\n", ID, IDmodes_name);
fflush(stdout);
NBmodes = data.image[ID].md[0].size[0] * data.image[ID].md[0].size[1];
n = NBmodes;
printf("NBmodes = %ld\n", NBmodes);
fflush(stdout);
printf("creating _tmpmodes %ld %ld %ld\n",
(long) data.image[IDin].md[0].size[0],
(long) data.image[IDin].md[0].size[1],
NBmodes);
fflush(stdout);
create_3Dimage_ID("_tmpmodes", data.image[IDin].md[0].size[0], data.image[IDin].md[0].size[1], NBmodes, &IDmodes);
for(ii = 0; ii < data.image[IDin].md[0].size[0]; ii++)
for(jj = 0; jj < data.image[IDin].md[0].size[1]; jj++) {
for(kk = 0; kk < NBmodes; kk++) {
data.image[IDmodes].array.F[kk * data.image[IDin].md[0].size[0]*data.image[IDin].md[0].size[1] + jj * data.image[IDin].md[0].size[0] + ii] =
data.image[ID].array.F[NBmodes * (jj * data.image[IDin].md[0].size[0] + ii) + kk];
}
}
//save_fits("_tmpmodes", "_test_tmpmodes.fits");
}
normcoeff = (float *) malloc(sizeof(float) * NBmodes);
if(MODENORM == 1) {
for(k = 0; k < NBmodes; k++) {
normcoeff[k] = 0.0;
for(ii = 0; ii < m; ii++) {
normcoeff[k] += data.image[IDmodes].array.F[k * m + ii] * data.image[IDmodes].array.F[k * m + ii];
}
}
} else
for(k = 0; k < NBmodes; k++) {
normcoeff[k] = 1.0;
}
modevalarray = (float *) malloc(sizeof(float) * n);
modevalarrayref = (float *) malloc(sizeof(float) * n);
arraytmp = (uint32_t *) malloc(sizeof(uint32_t) * 2);
IDrefout = image_ID(IDrefout_name);
if(IDrefout == -1) {
arraytmp[0] = NBmodes;
arraytmp[1] = 1;
} else {
arraytmp[0] = data.image[IDrefout].md[0].size[0];
arraytmp[1] = data.image[IDrefout].md[0].size[1];
}
// CONNNECT TO OUTPUT STREAM
ID_modeval = image_ID(IDmodes_val_name);
if(ID_modeval == -1) { // CREATE IT
create_image_ID(IDmodes_val_name, 2, arraytmp, _DATATYPE_FLOAT, 1, 0, 0, &ID_modeval);
COREMOD_MEMORY_image_set_createsem(IDmodes_val_name, 10);
MODEVALCOMPUTE = 1;
} else { // USE STREAM, DO NOT COMPUTE IT
printf("======== Using pre-existing stream %s, insem = %d\n", IDmodes_val_name, insem);
fflush(stdout);
if( outinit == 0 )
MODEVALCOMPUTE = 0;
else
MODEVALCOMPUTE = 1;
// drive semaphore to zero
while(sem_trywait(data.image[ID_modeval].semptr[insem]) == 0) {
printf("WARNING %s %d : sem_trywait on ID_modeval\n", __FILE__, __LINE__);
fflush(stdout);
}
}
free(arraytmp);
printf("OUTPUT STREAM : %s ID: %ld\n", IDmodes_val_name, ID_modeval);
list_image_ID();
if(MODEVALCOMPUTE == 1) {
int deviceCount;
cudaGetDeviceCount(&deviceCount);
printf("%d devices found\n", deviceCount);
fflush(stdout);
printf("\n");
for(k = 0; k < deviceCount; k++) {
cudaGetDeviceProperties(&deviceProp, k);
printf("Device %d / %d [ %20s ] has compute capability %d.%d.\n",
k, deviceCount, deviceProp.name, deviceProp.major, deviceProp.minor);
printf(" Total amount of global memory: %.0f MBytes (%llu bytes)\n",
(float)deviceProp.totalGlobalMem / 1048576.0f, (unsigned long long) deviceProp.totalGlobalMem);
printf(" (%2d) Multiprocessors\n", deviceProp.multiProcessorCount);
printf(" GPU Clock rate: %.0f MHz (%0.2f GHz)\n",
deviceProp.clockRate * 1e-3f,
deviceProp.clockRate * 1e-6f);
printf("\n");
}
if(GPUindex < deviceCount) {
cudaSetDevice(GPUindex);
} else {
printf("Invalid Device : %d / %d\n",
GPUindex,
deviceCount);
exit(0);
}
printf("Create cublas handle ...");
fflush(stdout);
cublas_status = cublasCreate(&cublasH);
if(cublas_status != CUBLAS_STATUS_SUCCESS) {
printf("CUBLAS initialization failed\n");
return EXIT_FAILURE;
}
printf(" done\n");
fflush(stdout);
// load modes to GPU
cudaStat = cudaMalloc((void **)&d_modes, sizeof(float) * m * NBmodes);
if(cudaStat != cudaSuccess) {
printf("cudaMalloc d_modes returned error code %d, line %d\n", cudaStat, __LINE__);
exit(EXIT_FAILURE);
}
cudaStat = cudaMemcpy(d_modes, data.image[IDmodes].array.F, sizeof(float) * m * NBmodes, cudaMemcpyHostToDevice);
if(cudaStat != cudaSuccess) {
printf("cudaMemcpy returned error code %d, line %d\n", cudaStat, __LINE__);
exit(EXIT_FAILURE);
}
// create d_in
cudaStat = cudaMalloc((void **)&d_in, sizeof(float) * m);
if(cudaStat != cudaSuccess) {
printf("cudaMalloc d_in returned error code %d, line %d\n", cudaStat, __LINE__);
exit(EXIT_FAILURE);
}
// create d_modeval
cudaStat = cudaMalloc((void **)&d_modeval, sizeof(float) * NBmodes);
if(cudaStat != cudaSuccess) {
printf("cudaMalloc d_modeval returned error code %d, line %d\n", cudaStat, __LINE__);
exit(EXIT_FAILURE);
}
}
//loopcnt = 0;
if(TRACEMODE == 1) {
sizearraytmp = (uint32_t *) malloc(sizeof(uint32_t) * 2);
{
int slen = snprintf(traceim_name, STRINGMAXLEN_IMGNAME, "%s_trace", IDmodes_val_name);
if(slen<1) {
PRINT_ERROR("snprintf wrote <1 char");
abort(); // can't handle this error any other way
}
if(slen >= STRINGMAXLEN_IMGNAME) {
PRINT_ERROR("snprintf string truncation");
abort(); // can't handle this error any other way
}
}
sizearraytmp[0] = TRACEsize;
sizearraytmp[1] = NBmodes;
IDtrace = image_ID(traceim_name);
imOK = 1;
if(IDtrace == -1) {
imOK = 0;
} else {
if((data.image[IDtrace].md[0].size[0] != TRACEsize) || (data.image[IDtrace].md[0].size[1] != NBmodes)) {
imOK = 0;
delete_image_ID(traceim_name, DELETE_IMAGE_ERRMODE_WARNING);
}
}
if(imOK == 0) {
create_image_ID(traceim_name, 2, sizearraytmp, _DATATYPE_FLOAT, 1, 0, 0, &IDtrace);
}
COREMOD_MEMORY_image_set_createsem(traceim_name, 10);
free(sizearraytmp);
}
if(PROCESS == 1) {
sizearraytmp = (uint32_t *) malloc(sizeof(uint32_t) * 2);
{
int slen = snprintf(process_ave_name, STRINGMAXLEN_IMGNAME, "%s_ave", IDmodes_val_name);
if(slen<1) {
PRINT_ERROR("snprintf wrote <1 char");
abort(); // can't handle this error any other way
}
if(slen >= STRINGMAXLEN_IMGNAME) {
PRINT_ERROR("snprintf string truncation");
abort(); // can't handle this error any other way
}
}
sizearraytmp[0] = NBmodes;
sizearraytmp[1] = NBaveSTEP;
IDprocave = image_ID(process_ave_name);
imOK = 1;
if(IDprocave == -1) {
imOK = 0;
} else {
if((data.image[IDprocave].md[0].size[0] != NBmodes) || (data.image[IDprocave].md[0].size[1] != NBaveSTEP)) {
imOK = 0;
delete_image_ID(process_ave_name, DELETE_IMAGE_ERRMODE_WARNING);
}
}
if(imOK == 0) {
create_image_ID(process_ave_name, 2, sizearraytmp, _DATATYPE_FLOAT, 1, 0, 0, &IDprocave);
}
COREMOD_MEMORY_image_set_createsem(process_ave_name, 10);
free(sizearraytmp);
sizearraytmp = (uint32_t *) malloc(sizeof(uint32_t) * 2);
{
int slen = snprintf(process_rms_name, STRINGMAXLEN_IMGNAME, "%s_rms", IDmodes_val_name);
if(slen<1) {
PRINT_ERROR("snprintf wrote <1 char");
abort(); // can't handle this error any other way
}
if(slen >= STRINGMAXLEN_IMGNAME) {
PRINT_ERROR("snprintf string truncation");
abort(); // can't handle this error any other way
}
}
sizearraytmp[0] = NBmodes;
sizearraytmp[1] = NBaveSTEP;
IDprocrms = image_ID(process_rms_name);
imOK = 1;
if(IDprocrms == -1) {
imOK = 0;
} else {
if((data.image[IDprocrms].md[0].size[0] != NBmodes) || (data.image[IDprocrms].md[0].size[1] != NBaveSTEP)) {
imOK = 0;
delete_image_ID(process_rms_name, DELETE_IMAGE_ERRMODE_WARNING);
}
}
if(imOK == 0) {
create_image_ID(process_rms_name, 2, sizearraytmp, _DATATYPE_FLOAT, 1, 0, 0, &IDprocrms);
}
COREMOD_MEMORY_image_set_createsem(process_rms_name, 10);
free(sizearraytmp);
}
initref = 0;
twait1 = *twait;
printf("LOOP START MODEVALCOMPUTE = %d\n", MODEVALCOMPUTE);
fflush(stdout);
if(MODEVALCOMPUTE == 0) {
printf("\n");
printf("This function is NOT computing mode values\n");
printf("Pre-existing stream %s was detected\n", IDmodes_val_name);
printf("\n");
if(data.processinfo == 1) {
strcpy(processinfo->statusmsg, "Passing stream, no computation");
//sprintf(processinfo->description, "passthrough, no comp");
}
} else {
char msgstring[STRINGMAXLEN_PROCESSINFO_STATUSMSG];
{
int slen = snprintf(msgstring, STRINGMAXLEN_PROCESSINFO_STATUSMSG, "Running on GPU %d", GPUindex);
if(slen<1) {
PRINT_ERROR("snprintf wrote <1 char");
abort(); // can't handle this error any other way
}
if(slen >= STRINGMAXLEN_PROCESSINFO_STATUSMSG) {
PRINT_ERROR("snprintf string truncation");
abort(); // can't handle this error any other way
}
}
if(data.processinfo == 1) {