-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic_data_plugin.f90
429 lines (363 loc) · 17.8 KB
/
generic_data_plugin.f90
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
!
! This is free and unencumbered software released into the public domain.
!
! Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software,
! either in source code form or as a compiled binary, for any purpose, commercial or non-commercial,
! and by any means.
!
! In jurisdictions that recognize copyright laws, the author or authors of this software dedicate
! any and all copyright interest in the software to the public domain. We make this dedication for
! the benefit of the public at large and to the detriment of our heirs and successors. We intend
! this dedication to be an overt act of relinquishment in perpetuity of all present and future
! rights to this software under copyright law.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
! NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
! IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
! ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
! THE USE OR OTHER DEALINGS IN THE SOFTWARE.
!
! For more information, please refer to For more information, please refer to <http://unlicense.org/>
!
!
! Dectris Ltd., Taefernweg 1, 5405 Baden-Daettwil, Switzerland.
!
! (proof_of_concept)
!
! Interoperability with C in Fortran 2003
!
! Wrap up module to abstract the interface from
! http://cims.nyu.edu/~donev/Fortran/DLL/DLL.Forum.txt
!
module iso_c_utilities
use iso_c_binding ! intrinsic module
character(c_char), dimension(1), save, target, private :: dummy_string="?"
contains
function c_f_string(cptr) result(fptr)
! convert a null-terminated c string into a fortran character array pointer
type(c_ptr), intent(in) :: cptr ! the c address
character(kind=c_char), dimension(:), pointer :: fptr
interface ! strlen is a standard C function from <string.h>
function strlen(string) result(len) bind(C,name="strlen")
use iso_c_binding
type(c_ptr), value :: string ! a C pointer
end function
end interface
if(c_associated(cptr)) then
call c_f_pointer(fptr=fptr, cptr=cptr, shape=[strlen(cptr)])
else
! to avoid segfaults, associate fptr with a dummy target:
fptr=>dummy_string
end if
end function
end module
!
! Interoperability with C in Fortran 2003
!
! Wrap up module to abstract the interface from
! http://cims.nyu.edu/~donev/Fortran/DLL/DLL.Forum.txt
!
module dlfcn
use iso_c_binding
use iso_c_utilities
implicit none
private
public :: dlopen, dlsym, dlclose, dlerror ! dl api
! valid modes for mode in dlopen:
integer(c_int), parameter, public :: rtld_lazy=1, rtld_now=2, rtld_global=256, rtld_local=0
! obtained from the output of the previously listed c program
interface ! all we need is interfaces for the prototypes in <dlfcn.h>
function dlopen(file,mode) result(handle) bind(C,name="dlopen")
! void *dlopen(const char *file, int mode);
use iso_c_binding
character(c_char), dimension(*), intent(in) :: file
! c strings should be declared as character arrays
integer(c_int), value :: mode
type(c_ptr) :: handle
end function
function dlsym(handle,name) result(funptr) bind(C,name="dlsym")
! void *dlsym(void *handle, const char *name);
use iso_c_binding
type(c_ptr), value :: handle
character(c_char), dimension(*), intent(in) :: name
type(c_funptr) :: funptr ! a function pointer
end function
function dlclose(handle) result(status) bind(C,name="dlclose")
! int dlclose(void *handle);
use iso_c_binding
type(c_ptr), value :: handle
integer(c_int) :: status
end function
function dlerror() result(error) bind(C,name="dlerror")
! char *dlerror(void);
use iso_c_binding
type(c_ptr) :: error
end function
end interface
end module dlfcn
!
! Generic handle for share-object like structures
!
! Wrap up module to abstract the interface from
! http://cims.nyu.edu/~donev/Fortran/DLL/DLL.Forum.txt
!
module generic_data_plugin
use iso_c_binding
implicit none
character(kind=c_char,len=1024) :: dll_filename
character(kind=c_char,len=1024) :: image_data_filename
integer(c_int) :: status
type(c_ptr) :: handle=c_null_ptr
! public :: generic_open !, generic_header, generic_data, generic_clone
!
! Abstract interfaces for C mapped functions
!
!
! get_header -> dll_get_header
abstract interface
subroutine plugin_open(filename, info_array, error_flag) bind(C)
use iso_c_binding
integer(c_int) :: error_flag
character(kind=c_char) :: filename(*)
integer(c_int), dimension(1024) :: info_array
end subroutine plugin_open
subroutine plugin_close(error_flag) bind(C)
use iso_c_binding
integer (c_int) :: error_flag
end subroutine plugin_close
subroutine plugin_get_header(metadata_array, error_flag) bind(C)
use iso_c_binding
integer(c_int) :: error_flag
integer(c_int), dimension(*) :: metadata_array
end subroutine plugin_get_header
subroutine plugin_get_data(frame_number, nx, ny, data_array, data_validation_array, error_flag) bind(C)
use iso_c_binding
integer(c_int) :: nx, ny, frame_number
integer(c_int) :: error_flag
integer(c_int), dimension(nx:ny) :: data_array
integer(c_int), dimension(1024) :: data_validation_array
end subroutine plugin_get_data
end interface
! dynamically-linked procedures
procedure(plugin_open), pointer :: dll_plugin_open
procedure(plugin_get_header), pointer :: dll_plugin_get_header
procedure(plugin_get_data), pointer :: dll_plugin_get_data
procedure(plugin_close), pointer :: dll_plugin_close
contains
!
! Open the shared-object
subroutine generic_open(library, template_name, info_array, error_flag)
! Requirements:
! 'LIBRARY' input (including path, otherwise using LD_LIBRARY_PATH)
! 'TEMPLATE_NAME' input (the resource in image data masterfile)
! 'INFO' (integer array) input Array of (1024) integers:
! INFO(1) = Consumer ID (1:XDS)
! INFO(2) = Version Number of the Consumer software
! INFO(3:8) = Unused
! INFO(9:40) = 1024bit signature of the consumer software
! INFO(>41) = Unused
! 'INFO' (integer array) output Array of (1024) integers:
! INFO(1) = Vendor ID (1:Dectris)
! INFO(2) = Major Version number of the library
! INFO(3) = Minor Version number of the library
! INFO(4) = Parch Version number of the library
! INFO(5) = Linux timestamp of library creation
! INFO(6:8) = Unused
! INFO(9:40) = 1024bit signature of the library (not yet used)
! INFO(>41) = Unused
! 'ERROR_FLAG' output Return values
! 0 Success
! -1 Handle already exists
! -2 Cannot open Library
! -3 Function not found in library
! -4 Master file cannot be opened (coming from C function)
! -10 Consumer identity not supported (coming from C function)
! -11 Consumer identity could not be verified (coming from C function)
! -12 Consumer software version not supported (coming from C function)
use iso_c_binding
use iso_c_utilities
use dlfcn
implicit none
character(len=:), allocatable :: library, template_name
integer(c_int) :: error_flag
integer(c_int), dimension(1024) :: info_array
type(c_funptr) :: fun_plugin_open_ptr = c_null_funptr
type(c_funptr) :: fun_plugin_close_ptr = c_null_funptr
type(c_funptr) :: fun_plugin_get_header_ptr = c_null_funptr
type(c_funptr) :: fun_plugin_get_data_ptr = c_null_funptr
integer(c_int) :: external_error_flag
logical :: loading_error_flag = .false.
error_flag=0
write(6,*) "[generic_data_plugin] - INFO - generic_open"
write(6,*) " + library = <", library, ">"
write(6,*) " + template_name = <", template_name, ">"
if ( c_associated(handle) ) then
write(6,*) "[generic_data_plugin] - ERROR - 'handle' not null"
error_flag = -1
return
endif
dll_filename=library
error_flag = 0
write(6,*) " + dll_filename = <", trim(dll_filename)//C_NULL_CHAR, ">"
image_data_filename=trim(template_name)//C_NULL_CHAR
error_flag = 0
write(6,*) " + image_data_filename = <", trim(image_data_filename)//C_NULL_CHAR, ">"
!
! Open the DL:
! The use of IOR is not really proper...wait till Fortran 2008
handle=dlopen(trim(dll_filename)//C_NULL_CHAR, IOR(RTLD_NOW, RTLD_GLOBAL))
!
! Check if can use handle
if(.not.c_associated(handle)) then
write(6,*) "[generic_data_plugin] - ERROR - Cannot open Handle: ", c_f_string(dlerror())
error_flag = -2
return
end if
!
! Find the subroutines in the DL:
fun_plugin_get_data_ptr = DLSym(handle,"plugin_get_data")
if(.not.c_associated(fun_plugin_get_data_ptr)) then
write(6,*) "[generic_data_plugin] - ERROR in DLSym(handle,'plugin_get_data'): ", c_f_string(dlerror())
loading_error_flag = .true.
else
call c_f_procpointer(cptr=fun_plugin_get_data_ptr, fptr=dll_plugin_get_data)
endif
!
fun_plugin_get_header_ptr = DLSym(handle,"plugin_get_header")
if(.not.c_associated(fun_plugin_get_header_ptr)) then
write(6,*) "[generic_data_plugin] - ERROR in DLSym(handle,'plugin_get_header'): ",c_f_string(dlerror())
loading_error_flag = .true.
else
call c_f_procpointer(cptr=fun_plugin_get_header_ptr, fptr=dll_plugin_get_header)
endif
!
fun_plugin_open_ptr = DLSym(handle,"plugin_open")
if(.not.c_associated(fun_plugin_open_ptr)) then
write(6,*) "[generic_data_plugin] - ERROR in DLSym(handle,'plugin_open'): ", c_f_string(dlerror())
loading_error_flag = .true.
else
call c_f_procpointer(cptr=fun_plugin_open_ptr, fptr=dll_plugin_open)
endif
fun_plugin_close_ptr = DLSym(handle,"plugin_close")
if(.not.c_associated(fun_plugin_close_ptr)) then
write(6,*) "[generic_data_plugin] - ERROR in DLSym(handle,'plugin_close'): ", c_f_string(dlerror())
loading_error_flag = .true.
else
call c_f_procpointer(cptr=fun_plugin_close_ptr, fptr=dll_plugin_close)
endif
if (loading_error_flag==.true.) then
write(6,*) "[generic_data_plugin] - ERROR - Cannot map function(s) from the dll"
error_flag = -3
else
call dll_plugin_open(image_data_filename, info_array, external_error_flag)
error_flag = external_error_flag
endif
return
end subroutine generic_open
!
! Get the header
subroutine generic_get_header(metadata_array, error_flag)
! Requirements:
! 'METADATA_ARRAY' (integer array) output Array of (1024) integers:
! METADATA_ARRAY(1) = 'NX' Number of pixels along X
! METADATA_ARRAY(2) = 'NY' Number of pixels along Y
! METADATA_ARRAY(3) = 'NBYTE' Number of bytes in the image... X*Y*DEPTH
! METADATA_ARRAY(4) = 'QX' Pixel size in [nm] (10^-9 m)
! METADATA_ARRAY(5) = 'QY' Pixel size in [nm] (10^-9 m)
! METADATA_ARRAY(6) = 'NUMBER_OF_FRAMES' (integer) Number of frames for the full dataset. So far unused"
! 'ERROR_FLAG' output Return values
! 0 Success
! -1 Cannot open library
! -2 Cannot read header (will come from C function)
! -4 Cannot read dataset informations (will come from C function)
! -10 Error in the determination of the Dataset parameters (will come from C function)
!
use iso_c_binding
use iso_c_utilities
use dlfcn
implicit none
integer(c_int) :: error_flag
integer(c_int) :: external_error_flag
integer(c_int), dimension(*) :: metadata_array
error_flag=0
write(6,*) "[generic_data_plugin] - INFO - generic_get_header"
! Check if can use handle
if(.not.c_associated(handle)) then
write(6,*) "[generic_data_plugin] - ERROR - Cannot open Handle"
write(6,*) " ", c_f_string(dlerror())
error_flag = -1
return
end if
! finally, invoke the dynamically-linked subroutine:
call dll_plugin_get_header(metadata_array, external_error_flag)
return
end subroutine generic_get_header
!
! Dynamically map function and execute it
subroutine generic_get_data(frame_number, nx, ny, data_array, data_validation_array, error_flag)
! Requirements:
! 'FRAME_NUMBER' (integer) input Number of frames for the full datase. So far unused
! 'NX' (integer) input Number of pixels along X
! 'NY' (integer) input Number of pixels along Y
! 'DATA_ARRAY' (integer array) output 1D array containing pixel data with lenght = NX*NY
! 'DATA_VALIDATION_ARRAY' output Array of (1024) integers:
! (integer array) DATA_VALIDATION_ARRAY(0:39) = 1024bit verification key
! DATA_VALIDATION_ARRAY(32:35) = Image MD5 Checksum
! DATA_VALIDATION_ARRAY() = Unused
! 'ERROR_FLAG' (integer) output Provides error state condition
! 0 Success
! -1 Cannot open library
! -2 Cannot open frame (will come from C function)
! -3 Datatype not supported (will come from C function)
! -4 Cannot read dataset informations (will come from C function)
! -10 MD5 Checksum Error
! -11 Verification key error
!
use iso_c_binding
use iso_c_utilities
use dlfcn
implicit none
integer(c_int) :: nx, ny, frame_number
integer(c_int) :: error_flag
integer(c_int), dimension(*) :: data_validation_array
integer(c_int), dimension(*) :: data_array
error_flag=0
call dll_plugin_get_data(frame_number, nx, ny, data_array, data_validation_array, error_flag)
end subroutine generic_get_data
! Close the shared-object
!
subroutine generic_close(error_flag)
! Requirements:
! 'ERROR_FLAG' (integer) output Return values:
! 0 Success
! -1 Error closing Masterfile
! -2 Error closing Shared-object
use iso_c_binding
use iso_c_utilities
use dlfcn
implicit none
integer(c_int) :: error_flag
integer(c_int) :: external_error_flag
! Check if can use handle
if(.not.c_associated(handle)) then
write(6,*) "[generic_data_plugin] - ERROR - Cannot open Handle"
write(6,*) " ", c_f_string(dlerror())
error_flag = -1
return
end if
write(6,*) "[generic_data_plugin] - INFO - 'call generic_close()'"
call dll_plugin_close(external_error_flag)
error_flag = external_error_flag
! now close the dl:
status=dlclose(handle)
if(status/=0) then
write(6,*) "[generic_data_plugin] - ERROR - Cannot open Handle"
write(6,*) " ", c_f_string(dlerror())
error_flag = -2
else
error_flag = 0
end if
return
end subroutine generic_close
end module generic_data_plugin