forked from milk-org/ImageStreamIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageStreamIO.c
2098 lines (1787 loc) · 56.8 KB
/
ImageStreamIO.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 ImageStreamIO.c
* @brief Read and Create image
*
* Read and create images and streams (shared memory)
*
*
*
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif//_GNU_SOURCE
#include <math.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h> // for open
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <semaphore.h>
#include <unistd.h> // for close
#ifdef USE_CFITSIO
#include <fitsio.h>
#endif
// shared memory and semaphores file permission
#define FILEMODE 0666
#if defined NDEBUG
#define DEBUG_TRACEPOINT_LOG(...)
#else
#define DEBUG_TRACEPOINT_LOG(...) \
do \
{ \
char msg[1000]; \
snprintf(msg, 1000, __VA_ARGS__); \
ImageStreamIO_write_process_log(msg); \
} while (0)
#endif
// Handle old fitsios
#ifndef ULONGLONG_IMG
#define ULONGLONG_IMG (80)
#endif
#include "ImageStreamIO.h"
static int INITSTATUS_ImageStreamIO = 0;
void __attribute__((constructor)) libinit_ImageStreamIO()
{
if (INITSTATUS_ImageStreamIO == 0)
{
init_ImageStreamIO();
INITSTATUS_ImageStreamIO = 1;
}
}
errno_t init_ImageStreamIO()
{
// any initialization needed ?
return IMAGESTREAMIO_SUCCESS;
}
// Forward dec'l
errno_t ImageStreamIO_printERROR_(const char *file, const char *func, int line,
errno_t code, char *errmessage);
errno_t ImageStreamIO_printWARNING(char *warnmessage);
errno_t (*internal_printError)(const char *, const char *, int, errno_t,
char *) = &ImageStreamIO_printERROR_;
errno_t ImageStreamIO_set_default_printError()
{
internal_printError = &ImageStreamIO_printERROR_;
return IMAGESTREAMIO_SUCCESS;
}
errno_t ImageStreamIO_set_printError(errno_t (*new_printError)(const char *,
const char *, int, errno_t, char *))
{
internal_printError = new_printError;
return IMAGESTREAMIO_SUCCESS;
}
#define ImageStreamIO_printERROR(code, msg) \
if (internal_printError) \
internal_printError(__FILE__, __func__, __LINE__, code, (char*)msg);
#ifdef HAVE_CUDA
void check(cudaError_t result, char const *const func, const char *const file,
int const line)
{
if (result)
{
cudaDeviceReset();
// Make sure we call CUDA Device Reset
ImageStreamIO_printERROR_(file, func, line, result, "CUDA error");
}
}
// This will output the proper CUDA error strings in the event
// that a CUDA host call returns an error
#define checkCudaErrors(val) check((val), #val, __FILE__, __LINE__)
#endif
/**
* @brief Write entry into debug log
*
*
*/
errno_t ImageStreamIO_write_process_log(
char *msg)
{
FILE *fplog;
char fname[STRINGMAXLEN_FILE_NAME];
pid_t thisPID;
thisPID = getpid();
snprintf(fname, STRINGMAXLEN_FILE_NAME, "logreport.%05d.log", thisPID);
struct tm *uttime;
time_t tvsec0;
fplog = fopen(fname, "a");
if (fplog != NULL)
{
struct timespec tnow;
// time_t now;
clock_gettime(CLOCK_ISIO, &tnow);
tvsec0 = tnow.tv_sec;
uttime = gmtime(&tvsec0);
fprintf(fplog, "%04d%02d%02dT%02d%02d%02d.%09ld %s\n",
1900 + uttime->tm_year, 1 + uttime->tm_mon, uttime->tm_mday, uttime->tm_hour,
uttime->tm_min, uttime->tm_sec, tnow.tv_nsec,
msg);
fclose(fplog);
}
return 0;
}
/**
* Print error to stderr
*
*
*/
errno_t ImageStreamIO_printERROR_(
const char *file,
const char *func,
int line,
__attribute__((unused)) errno_t code,
char *errmessage)
{
fprintf(stderr,
"%c[%d;%dmERROR [ FILE: %s FUNCTION: %s LINE: %d ] %c[%d;m\n",
(char)27, 1, 31, file, func, line, (char)27, 0);
if (errno != 0)
{
char buff[256];
// Test for which version of strerror_r we're using (XSI or GNU)
#if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && \
!defined(_GNU_SOURCE))
if (strerror_r(errno, buff, sizeof(buff)) == 0)
{
fprintf(stderr, "C Error: %s\n", buff);
}
else
{
fprintf(stderr, "Unknown C Error\n");
}
#else
// GNU strerror_r does not necessarily use buff, and uses errno to report
// errors.
int _errno = errno;
errno = 0;
char *estr = strerror_r(_errno, buff, sizeof(buff));
if (errno == 0)
fprintf(stderr, "%c[%d;%dmC Error: %s%c[%d;m\n", (char)27, 1, 31, estr,
27, 0);
else
fprintf(stderr, "%c[%d;%dmUnknown C Error%c[%d;m\n", (char)27, 1, 31, 27,
0);
errno = _errno; // restore it in case it's used later.
#endif
}
else
fprintf(stderr, "%c[%d;%dmNo C error (errno = 0)%c[%d;m\n", (char)27, 1, 31,
27, 0);
fprintf(stderr, "%c[%d;%dm %s %c[%d;m\n", (char)27, 1, 31, errmessage,
(char)27, 0);
return IMAGESTREAMIO_SUCCESS;
}
/**
* Print warning to stderr
*
*
*/
errno_t ImageStreamIO_printWARNING(
char *warnmessage)
{
int fn = fileno(stderr);
fprintf(stderr, "%c[%d;%dmWARNING %c[%d;m\n",
(char)27, 1, 35, (char)27, 0);
fprintf(stderr, "%c[%d;%dm (PID %d) %s %c[%d;m\n", (char)27, 1, 35, (int) getpid(), warnmessage,
(char)27, 0);
return IMAGESTREAMIO_SUCCESS;
}
/* ============================================================================================================================================================================================== */
/* @name 0. Utilities */
/* ============================================================================================================================================================================================== */
errno_t ImageStreamIO_readBufferAt(
const IMAGE *image,
const unsigned int slice_index,
void **buffer)
{
if ((image->md->imagetype & 0xF) != CIRCULAR_BUFFER)
{
*buffer = (void *)image->array.UI8;
return IMAGESTREAMIO_SUCCESS;
}
if (slice_index >= image->md->size[2])
{
*buffer = NULL;
return IMAGESTREAMIO_FAILURE;
}
const uint64_t frame_size = image->md->size[0] * image->md->size[1];
const int size_element = ImageStreamIO_typesize(image->md->datatype);
*buffer = (void *)(image->array.UI8 + slice_index * frame_size * size_element);
return IMAGESTREAMIO_SUCCESS;
}
errno_t ImageStreamIO_shmdirname(
char *shmdname)
{
DIR *tmpdir = NULL; // Initialize to failure (NULL dir stream)
// first, we try the env variable if it exists
char *MILK_SHM_DIR = getenv("MILK_SHM_DIR");
if (MILK_SHM_DIR != NULL)
{
snprintf(shmdname,STRINGMAXLEN_DIR_NAME, "%s", MILK_SHM_DIR);
// does this direcory exist ?
tmpdir = opendir(shmdname);
if (!tmpdir)
{ // Print warning about envvar if envvar has in valid dir
printf(" [ WARNING ] '%s' does not exist\n", MILK_SHM_DIR);
}
}
// second, we try SHAREDMEMDIR default
if (!tmpdir)
{
snprintf(shmdname, STRINGMAXLEN_DIR_NAME, "%s", SHAREDMEMDIR);
tmpdir = opendir(shmdname);
}
// if all above fails, set to /tmp
if (!tmpdir)
{
snprintf(shmdname, STRINGMAXLEN_DIR_NAME, "%s", "/tmp");
tmpdir = opendir(shmdname);
}
// Failure: no directories were found that could be opened
if (!tmpdir) { exit(EXIT_FAILURE); }
// Success: close directory stream; dirname is in shdname
closedir(tmpdir);
return IMAGESTREAMIO_SUCCESS;
}
errno_t ImageStreamIO_filename(
char *file_name,
size_t ssz,
const char *im_name)
{
static char shmdirname[STRINGMAXLEN_DIR_NAME];
static int initSHAREDMEMDIR = 0;
if (initSHAREDMEMDIR == 0)
{
ImageStreamIO_shmdirname(shmdirname);
initSHAREDMEMDIR = 1;
}
int rv = snprintf(file_name, ssz, "%s/%s.im.shm", shmdirname, im_name);
if ((rv > 0) && (rv < (int)ssz)) { return IMAGESTREAMIO_SUCCESS; }
if (rv < 0)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FAILURE, strerror(errno));
return IMAGESTREAMIO_FAILURE;
}
else
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FAILURE,
"string not large enough for file name");
return IMAGESTREAMIO_FAILURE;
}
}
int ImageStreamIO_typesize(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8: return SIZEOF_DATATYPE_UINT8;
case _DATATYPE_INT8: return SIZEOF_DATATYPE_INT8;
case _DATATYPE_UINT16: return SIZEOF_DATATYPE_UINT16;
case _DATATYPE_INT16: return SIZEOF_DATATYPE_INT16;
case _DATATYPE_UINT32: return SIZEOF_DATATYPE_UINT32;
case _DATATYPE_INT32: return SIZEOF_DATATYPE_INT32;
case _DATATYPE_UINT64: return SIZEOF_DATATYPE_UINT64;
case _DATATYPE_INT64: return SIZEOF_DATATYPE_INT64;
case _DATATYPE_HALF: return SIZEOF_DATATYPE_HALF;
case _DATATYPE_FLOAT: return SIZEOF_DATATYPE_FLOAT;
case _DATATYPE_DOUBLE: return SIZEOF_DATATYPE_DOUBLE;
case _DATATYPE_COMPLEX_FLOAT: return SIZEOF_DATATYPE_COMPLEX_FLOAT;
case _DATATYPE_COMPLEX_DOUBLE:return SIZEOF_DATATYPE_COMPLEX_DOUBLE;
default: break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG, "invalid type code");
return -1; // This is an in-band error code, so can't be > 0.
}
const char *ImageStreamIO_typename(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8: return "UINT8";
case _DATATYPE_INT8: return "INT8";
case _DATATYPE_UINT16: return "UINT16";
case _DATATYPE_INT16: return "INT16";
case _DATATYPE_UINT32: return "UINT32";
case _DATATYPE_INT32: return "INT32";
case _DATATYPE_UINT64: return "UINT64";
case _DATATYPE_INT64: return "INT64";
case _DATATYPE_HALF: return "FLT16";
case _DATATYPE_FLOAT: return "FLT32";
case _DATATYPE_DOUBLE: return "FLT64";
case _DATATYPE_COMPLEX_FLOAT: return "CPLX32";
case _DATATYPE_COMPLEX_DOUBLE: return "CPLX64";
default: break;
}
return "unknown";
}
const char *ImageStreamIO_typename_7(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8: return "UINT8 ";
case _DATATYPE_INT8: return "INT8 ";
case _DATATYPE_UINT16: return "UINT16 ";
case _DATATYPE_INT16: return "INT16 ";
case _DATATYPE_UINT32: return "UINT32 ";
case _DATATYPE_INT32: return "INT32 ";
case _DATATYPE_UINT64: return "UINT64 ";
case _DATATYPE_INT64: return "INT64 ";
case _DATATYPE_HALF: return "FLT16 ";
case _DATATYPE_FLOAT: return "FLOAT ";
case _DATATYPE_DOUBLE: return "DOUBLE ";
case _DATATYPE_COMPLEX_FLOAT: return "CFLOAT ";
case _DATATYPE_COMPLEX_DOUBLE: return "CDOUBLE";
default: break;
}
return "unknown";
}
int ImageStreamIO_checktype(uint8_t datatype, int complex_allowed)
{
switch (datatype) {
case _DATATYPE_UINT8:
case _DATATYPE_INT8:
case _DATATYPE_UINT16:
case _DATATYPE_INT16:
case _DATATYPE_UINT32:
case _DATATYPE_INT32:
case _DATATYPE_UINT64:
case _DATATYPE_INT64:
case _DATATYPE_HALF:
case _DATATYPE_FLOAT:
case _DATATYPE_DOUBLE: return 0;
case _DATATYPE_COMPLEX_FLOAT:
case _DATATYPE_COMPLEX_DOUBLE: return complex_allowed ? 0 : -1;
default: break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG, "invalid type code");
return -1; // This is an in-band error code, so can't be > 0.
}
const char *ImageStreamIO_typename_short(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8: return " UI8";
case _DATATYPE_INT8: return " I8";
case _DATATYPE_UINT16: return "UI16";
case _DATATYPE_INT16: return " I16";
case _DATATYPE_UINT32: return "UI32";
case _DATATYPE_INT32: return " I32";
case _DATATYPE_UINT64: return "UI64";
case _DATATYPE_INT64: return " I64";
case _DATATYPE_HALF: return " F16";
case _DATATYPE_FLOAT: return " FLT";
case _DATATYPE_DOUBLE: return " DBL";
case _DATATYPE_COMPLEX_FLOAT: return "CFLT";
case _DATATYPE_COMPLEX_DOUBLE: return "CDBL";
default: break;
}
return " ???";
}
int ImageStreamIO_floattype(
uint8_t datatype)
{
switch (datatype)
{
case _DATATYPE_UINT8: return _DATATYPE_FLOAT;
case _DATATYPE_INT8: return _DATATYPE_FLOAT;
case _DATATYPE_UINT16: return _DATATYPE_FLOAT;
case _DATATYPE_INT16: return _DATATYPE_FLOAT;
case _DATATYPE_UINT32: return _DATATYPE_FLOAT;
case _DATATYPE_INT32: return _DATATYPE_FLOAT;
case _DATATYPE_UINT64: return _DATATYPE_DOUBLE;
case _DATATYPE_INT64: return _DATATYPE_DOUBLE;
case _DATATYPE_HALF: return _DATATYPE_HALF;
case _DATATYPE_FLOAT: return _DATATYPE_FLOAT;
case _DATATYPE_DOUBLE: return _DATATYPE_DOUBLE;
case _DATATYPE_COMPLEX_FLOAT: return _DATATYPE_COMPLEX_FLOAT;
case _DATATYPE_COMPLEX_DOUBLE: return _DATATYPE_COMPLEX_DOUBLE;
default: break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG, "invalid type code");
return -1; // This is an in-band error code, so can't be > 0.
}
int ImageStreamIO_FITSIOdatatype(uint8_t datatype)
{
switch (datatype)
{
#ifdef USE_CFITSIO
case _DATATYPE_UINT8: return TBYTE;
case _DATATYPE_INT8: return TSBYTE;
case _DATATYPE_UINT16: return TUSHORT;
case _DATATYPE_INT16: return TSHORT;
case _DATATYPE_UINT32: return TUINT;
case _DATATYPE_INT32: return TINT;
case _DATATYPE_UINT64: return TULONG;
case _DATATYPE_INT64: return TLONG;
case _DATATYPE_FLOAT: return TFLOAT;
case _DATATYPE_DOUBLE: return TDOUBLE;
#endif
default: break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG,
"bitpix not implemented for type");
return -1; // This is an in-band error code, must be unique from valid BITPIX values.
}
int ImageStreamIO_FITSIObitpix(
uint8_t datatype)
{
switch (datatype)
{
#ifdef USE_CFITSIO
case _DATATYPE_UINT8: return BYTE_IMG;
case _DATATYPE_INT8: return SBYTE_IMG;
case _DATATYPE_UINT16: return USHORT_IMG;
case _DATATYPE_INT16: return SHORT_IMG;
case _DATATYPE_UINT32: return ULONG_IMG;
case _DATATYPE_INT32: return LONG_IMG;
case _DATATYPE_UINT64: return ULONGLONG_IMG;
case _DATATYPE_INT64: return LONGLONG_IMG;
case _DATATYPE_FLOAT: return FLOAT_IMG;
case _DATATYPE_DOUBLE: return DOUBLE_IMG;
#endif
default: break;
}
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG,
"bitpix not implemented for type");
return -1; // This is an in-band error code, must be unique from valid BITPIX values.
}
uint64_t ImageStreamIO_offset_data(
IMAGE *image,
void *map)
{
uint8_t datatype = image->md->datatype;
u_int64_t offset = 0;
// printf("datatype = %d\n", (int)datatype);
// fflush(stdout);
if (image->md->location >= 0)
{
image->array.raw = ImageStreamIO_get_image_d_ptr(image);
offset = 0;
}
else
{
image->array.raw = map;
offset = ImageStreamIO_typesize(datatype) * image->md->nelement;
}
return offset;
}
uint64_t ImageStreamIO_initialize_buffer(
IMAGE *image)
{
// void *map; // pointed cast in bytes
const size_t size_element = ImageStreamIO_typesize(image->md->datatype);
if (image->md->location == -1)
{
if (image->md->shared == 1)
{
memset(image->array.raw, 0, image->md->nelement * size_element);
// memset takes an int source value
//memset(image->array.raw, '\0', image->md->nelement * size_element);
}
else
{
image->array.raw = calloc((size_t)image->md->nelement, size_element);
if (image->array.raw == NULL)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_BADALLOC, "memory allocation failed");
fprintf(stderr, "%c[%d;%dm", (char)27, 1, 31);
fprintf(stderr, "Image name = %s\n", image->name);
fprintf(stderr, "Image size = ");
fprintf(stderr, "%ld", (long)image->md->size[0]);
int i;
for (i = 1; i < image->md->naxis; i++)
{
fprintf(stderr, "x%ld", (long)image->md->size[i]);
}
fprintf(stderr, "\n");
fprintf(stderr, "Requested memory size = %ld elements = %f Mb\n",
(long)image->md->nelement,
1.0 / 1024 / 1024 * image->md->nelement * sizeof(uint8_t));
fprintf(stderr, " %c[%d;m", (char)27, 0);
exit(EXIT_FAILURE); ///\todo Is this really an exit or should we return?
}
}
}
else if (image->md->location >= 0)
{
#ifdef HAVE_CUDA
checkCudaErrors(cudaSetDevice(image->md->location));
checkCudaErrors(
cudaMalloc(&image->array.raw, size_element * image->md->nelement));
if (image->md->shared == 1)
{
checkCudaErrors(
cudaIpcGetMemHandle(&image->md->cudaMemHandle, image->array.raw));
}
#else
ImageStreamIO_printERROR(IMAGESTREAMIO_NOTIMPL,
"unsupported location, milk needs to be compiled with -DUSE_CUDA=ON"); ///\todo should this return an error?
#endif
}
return ImageStreamIO_offset_data(image, image->array.raw);
}
/* ===============================================================================================
*/
/* ===============================================================================================
*/
/* @name 1. READ / WRITE STREAM
*
*/
/* ===============================================================================================
*/
/* ===============================================================================================
*/
errno_t ImageStreamIO_createIm(
IMAGE *image,
const char *name,
long naxis,
uint32_t *size,
uint8_t datatype,
int shared,
int NBkw,
int CBsize)
{
return ImageStreamIO_createIm_gpu(image, name, naxis, size, datatype, -1,
shared, IMAGE_NB_SEMAPHORE, NBkw,
MATH_DATA, (uint32_t)CBsize);
}
errno_t ImageStreamIO_createIm_gpu(
IMAGE *image,
const char *name,
long naxis,
uint32_t *size,
uint8_t datatype,
int8_t location, // -1: CPU RAM, 0+ : GPU
int shared,
int NBsem,
int NBkw,
uint64_t imagetype,
uint32_t CBsize // circular buffer size (if shared), 0 if not used
)
{
long nelement;
uint8_t *map;
int kw;
// Get shm directory name (only on first call to this function)
static char shmdirname[200];
static int initSHAREDMEMDIR = 0;
if (initSHAREDMEMDIR == 0)
{
ImageStreamIO_shmdirname(shmdirname);
for (unsigned int stri = 0; stri < strlen(shmdirname); stri++)
if (shmdirname[stri] == '/') // replace '/' by '.'
{
shmdirname[stri] = '.';
}
initSHAREDMEMDIR = 1;
}
int NBproctrace = IMAGE_NB_PROCTRACE;
nelement = 1;
for (long i = 0; i < naxis; i++)
{
nelement *= size[i];
}
uint64_t imdatamemsize = ImageStreamIO_typesize(datatype) * nelement;
if (((imagetype & 0xF000F) == CIRCULAR_BUFFER) &&
(naxis != 3))
{
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG,
"Error calling ImageStreamIO_createIm_gpu, "
"temporal circular buffer needs 3 dimensions");
return IMAGESTREAMIO_INVALIDARG;
}
// compute total size to be allocated
if (shared == 1)
{
char semlogname[200];
// create semlog
size_t sharedsize = 0; // shared memory size in bytes
size_t datasharedsize = 0; // shared memory size in bytes used by the data
char lclname[STRINGMAXLEN_IMAGE_NAME];
strncpy(lclname, name, STRINGMAXLEN_IMAGE_NAME);
lclname[STRINGMAXLEN_IMAGE_NAME-1] = '\0';
char shmdirnamepfx[sizeof(semlogname) - 7 - sizeof(lclname)];
strncpy(shmdirnamepfx, shmdirname, sizeof(shmdirnamepfx));
shmdirnamepfx[sizeof(shmdirnamepfx)-1] = '\0';
snprintf(semlogname, sizeof(semlogname), "%s.%s_semlog", shmdirnamepfx, lclname);
remove(semlogname);
image->semlog = NULL;
umask(0);
if ((image->semlog = sem_open(semlogname, O_CREAT, FILEMODE, 1)) == SEM_FAILED)
{
fprintf(stderr, "Semaphore log %s :", semlogname);
ImageStreamIO_printERROR(IMAGESTREAMIO_SEMINIT,
"semaphore creation / initialization");
}
else
{
sem_init(
image->semlog, 1,
SEMAPHORE_INITVAL); // SEMAPHORE_INITVAL defined in ImageStruct.h
}
sharedsize = sizeof(IMAGE_METADATA);
datasharedsize = imdatamemsize;
if (location == -1)
{
// image on CPU
// printf("shared memory space in CPU RAM = %ud bytes\n", sharedsize);
sharedsize += datasharedsize;
}
else if (location >= 0)
{
// image on GPU
// printf("shared memory space in GPU%d RAM= %ud bytes\n", location,
// sharedsize); //TEST
}
else
{
ImageStreamIO_printERROR(IMAGESTREAMIO_INVALIDARG, "Error location unknown");
return IMAGESTREAMIO_FAILURE;
}
sharedsize += sizeof(IMAGE_KEYWORD) * NBkw;
sharedsize += sizeof(SEMFILEDATA) * NBsem;
// one read PID array, one write PID array
sharedsize += 2 * NBsem * sizeof(pid_t);
// semctrl
sharedsize += sizeof(uint32_t) * NBsem;
// semstatus
sharedsize += sizeof(uint32_t) * NBsem;
sharedsize += sizeof(STREAM_PROC_TRACE) * NBproctrace;
if ((imagetype & 0xF000F) ==
(CIRCULAR_BUFFER | ZAXIS_TEMPORAL)) // Circular buffer
{
// room for atimearray, writetimearray and cntarray
sharedsize += size[2] * (2 * sizeof(struct timespec) + sizeof(uint64_t));
}
// fast circular buffer metadata
sharedsize += sizeof(CBFRAMEMD) * CBsize;
// fast circular buffer data buffer
sharedsize += datasharedsize * CBsize;
#ifdef IMAGESTRUCT_WRITEHISTORY
// write time buffer
sharedsize += sizeof(FRAMEWRITEMD) * IMAGESTRUCT_FRAMEWRITEMDSIZE;
#endif
char SM_fname[200];
ImageStreamIO_filename(SM_fname, 200, name);
struct stat buffer;
if ((stat(SM_fname, &buffer) == 0) && (location > -1))
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FILEEXISTS,
"Error creating GPU SHM buffer on an existing file");
return IMAGESTREAMIO_FILEEXISTS;
}
int SM_fd; // shared memory file descriptor
umask(0);
SM_fd = open(SM_fname, O_RDWR | O_CREAT | O_TRUNC, (mode_t)FILEMODE);
if (SM_fd == -1)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_FILEOPEN,
"Error opening file for writing");
return IMAGESTREAMIO_FILEOPEN;
}
image->shmfd = SM_fd;
image->memsize = sharedsize;
int result;
result = lseek(SM_fd, sharedsize - 1, SEEK_SET);
if (result == -1)
{
close(SM_fd);
ImageStreamIO_printERROR(IMAGESTREAMIO_FILESEEK,
"Error calling lseek() to 'stretch' the file");
return IMAGESTREAMIO_FILESEEK;
}
result = write(SM_fd, "", 1);
if (result != 1)
{
close(SM_fd);
ImageStreamIO_printERROR(IMAGESTREAMIO_FILEWRITE,
"Error writing last byte of the file");
return IMAGESTREAMIO_FILEWRITE;
}
map = (uint8_t *)mmap(0, sharedsize, PROT_READ | PROT_WRITE, MAP_SHARED,
SM_fd, 0);
if (map == MAP_FAILED)
{
close(SM_fd);
ImageStreamIO_printERROR(IMAGESTREAMIO_MMAP, "Error mmapping the file");
return IMAGESTREAMIO_MMAP;
}
image->md = (IMAGE_METADATA *)map;
image->md->shared = 1;
image->md->creatorPID = getpid();
image->md->ownerPID = 0; // default value, indicates unset
image->md->sem = NBsem;
image->md->NBproctrace = NBproctrace;
{
struct stat file_stat;
int ret;
ret = fstat(SM_fd, &file_stat);
if (ret < 0)
{
ImageStreamIO_printERROR(IMAGESTREAMIO_INODE, "Error getting inode");
return IMAGESTREAMIO_INODE;
}
image->md->inode =
file_stat.st_ino; // inode now contains inode number of the file with descriptor fd
}
map += sizeof(IMAGE_METADATA);
if (location == -1)
{
// CPU
image->array.raw = map;
map += datasharedsize;
}
else
{
// GPU
image->array.raw = NULL;
}
image->kw = (IMAGE_KEYWORD *)(map);
map += sizeof(IMAGE_KEYWORD) * NBkw;
image->semfile = (SEMFILEDATA*)(map);
map += sizeof(SEMFILEDATA) * NBsem;
image->semReadPID = (pid_t *)(map);
map += sizeof(pid_t) * NBsem;
image->semWritePID = (pid_t *)(map);
map += sizeof(pid_t) * NBsem;
image->semctrl = (uint32_t *)(map);
map += sizeof(uint32_t) * NBsem;
image->semstatus = (uint32_t *)(map);
map += sizeof(uint32_t) * NBsem;
image->streamproctrace = (STREAM_PROC_TRACE *)(map);
map += sizeof(STREAM_PROC_TRACE) * NBproctrace;
if ((imagetype & 0xF000F) ==
(CIRCULAR_BUFFER | ZAXIS_TEMPORAL)) // If main image is circular buffer
{
image->atimearray = (struct timespec *)(map);
map += sizeof(struct timespec) * size[2];
image->writetimearray = (struct timespec *)(map);
map += sizeof(struct timespec) * size[2];
image->cntarray = (uint64_t *)(map);
map += sizeof(uint64_t) * size[2];
}
image->CircBuff_md = (CBFRAMEMD *)(map);
map += sizeof(CBFRAMEMD) * CBsize;
if (CBsize > 0)
{
image->CBimdata = map;
}
else
{
image->CBimdata = NULL;
}
map += datasharedsize * CBsize;
image->md->CBsize = CBsize;
image->md->CBindex = 0;
image->md->CBcycle = 0;
#ifdef IMAGESTRUCT_WRITEHISTORY
image->writehist = (FRAMEWRITEMD *)(map);
map += sizeof(FRAMEWRITEMD) * IMAGESTRUCT_FRAMEWRITEMDSIZE;
image->md->wCBindex = 0;
image->md->wCBcycle = 0;
#endif
}
else
{
// not shared memory, local memory only
image->shmfd = 0;
image->memsize = 0;
image->md = (IMAGE_METADATA *)malloc(sizeof(IMAGE_METADATA));
if (image->md == NULL)
{
printf("Memory allocation error %s %d\n", __FILE__, __LINE__);
abort();
}
image->md->shared = 0;
image->md->inode = 0;
if (NBkw > 0)
{
image->kw = (IMAGE_KEYWORD *)malloc(sizeof(IMAGE_KEYWORD) * NBkw);
if (image->kw == NULL)
{
printf("Memory allocation error %s %d\n", __FILE__, __LINE__);
abort();
}
}
else
{
image->kw = NULL;
}
image->md->CBsize = 0;
image->md->CBindex = 0;
image->md->CBcycle = 0;
#ifdef IMAGESTRUCT_WRITEHISTORY
image->md->wCBindex = 0;
image->md->wCBcycle = 0;
#endif
}
strncpy(image->md->version, IMAGESTRUCT_VERSION, 32);
image->md->imagetype = imagetype; // Image is mathematical vector or matrix
image->md->location = location;
image->md->datatype = datatype;
image->md->naxis = naxis;
strncpy(image->name, name, STRINGMAXLEN_IMAGE_NAME); // local name
strncpy(image->md->name, name, STRINGMAXLEN_IMAGE_NAME);
// Ensure image and image metadata names are null-terminated
image->name[STRINGMAXLEN_IMAGE_NAME-1] =
image->md->name[STRINGMAXLEN_IMAGE_NAME-1] = '\0';
for (long i = 0; i < naxis; i++)
{
image->md->size[i] = size[i];
}
image->md->nelement = nelement;
image->md->imdatamemsize = imdatamemsize;
image->md->NBkw = NBkw;
ImageStreamIO_initialize_buffer(image);
clock_gettime(CLOCK_ISIO, &image->md->lastaccesstime);