-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshared_memory.jl
331 lines (253 loc) · 12.3 KB
/
shared_memory.jl
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
"""
Unlike python interface, all external function call was done in place and the functions are not saved
into variables since all of them are called once.
"""
module shared_memory
export set_shared_memory_path ,
set_shared_memory_data ,
get_shared_memory_data ,
get_shared_memory_data_type ,
get_shared_memory_dimensions ,
get_shared_memory_flatten_data ,
get_shared_memory_flatten_length ,
delete_shared_memory ,
get_shared_memory_rank ,
SharedMemoryLibraryError;
# CAN BE CHANGED BY USER
LIBRARY_PATH::String = joinpath(@__DIR__,"..","shared_memory.dll")
# END
"""
Used to show the proper error based on the value.
Error values reflect values written in Rust library.
# Parameters
- `error_code::Int64`: value of the error. Should be negative.
"""
struct SharedMemoryLibraryError <: Exception
error_code::Int64
end
"""
Add customize formatting for the SharedMemoryLibraryError error when it's raised.
"""
function Base.showerror(io::IO, e::SharedMemoryLibraryError)
error_code::Int64 = e.error_code
if error_code === -1
print("Library path is not set")
elseif error_code === -2
print("Error in accessing shared file (probably file does not exist)")
elseif error_code === -3
print("Can't read library_path from memory")
elseif error_code === -4
print("Can't create shared memory")
elseif error_code === -5
print("New Rank doesn't match the previous rank")
else
throw(ArgumentError("Invalid error code is given: $(error_code)"))
end
end
"""
This function is used to check the return type of all the external functions calls.
# Arguments
- `value::Union{Cint,Clonglong}`: If value is negative, it's error otherwise should be return.
## Return
If value is negative, an exception is raised, otherwise the value will be returned.
"""
function check_library_error(value::Union{Cint,Clonglong})::Union{Cint,Clonglong}
if value >= 0
return value
else
throw(SharedMemoryLibraryError(value))
end
end
"""
Set the shared memory path. Will raise SharedMemoryLibraryError if the operation was no successful.
# Arguments
- `path::String`: A valid path used to access the shared memory. Will be encoded as UTF-8 before calling the function.
## Return
An int is returned.
"""
function set_shared_memory_path(path::String)::Nothing
check_library_error(ccall((:set_shared_memory_path,LIBRARY_PATH), Cint, (Cstring,), path))
nothing
end
"""
Flatten length is the product of dimensions. Will raise SharedMemoryLibraryError if the operation was no successful.
## Return
Returns the shared memory flatten data length as Int64.
"""
function get_shared_memory_flatten_length()::Int64
return check_library_error(ccall((:get_shared_memory_flatten_length,LIBRARY_PATH), Clonglong, ()))
end
"""
Will raise SharedMemoryLibraryError if the operation was no successful.
## Return
Return the shared memory data type that is either numeric data type or String.
"""
function get_shared_memory_data_type()::DataType
temp::Cint = check_library_error(ccall((:get_shared_memory_data_type,LIBRARY_PATH), Cint, ()));
return [UInt8,UInt16,UInt32,UInt64,Int8,Int16,Int32,Int64,Float32,Float64,ComplexF32,ComplexF64,String][temp+1]
end
"""
Will raise SharedMemoryLibraryError if the operation was no successful.
## Return
Return the shared memory data rank.
"""
function get_shared_memory_rank()::Int32
return check_library_error(ccall((:get_shared_memory_rank,LIBRARY_PATH), Cint, ()))
end
"""
Get the shared memory dimension. Will raise SharedMemoryLibraryError if the operation was no successful.
## Return
The dimension of store data in shared memory.
"""
function get_shared_memory_dimensions()::Vector{UInt64}
dims::Vector{UInt64} = Vector{UInt64}(undef,get_shared_memory_rank())
check_library_error(ccall((:get_shared_memory_dimensions,LIBRARY_PATH), Cint, (Ptr{UInt64},),dims))
return dims
end
"""
Will raise SharedMemoryLibraryError if the operation was no successful.
## Return
Return the shared memory flatten data. Output type is either a Numpy array or a string.
"""
function get_shared_memory_flatten_data()::Union{Vector,String}
shared_memory_type::DataType = get_shared_memory_data_type();
shared_memory_flatten_length::Int64 = get_shared_memory_flatten_length();
if shared_memory_type === UInt8
data = Vector{UInt8}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_unsigned_8, LIBRARY_PATH), Cint, (Ptr{UInt8},), data))
elseif shared_memory_type === UInt16
data = Vector{UInt16}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_unsigned_16, LIBRARY_PATH), Cint, (Ptr{UInt16},), data))
elseif shared_memory_type === UInt32
data = Vector{UInt32}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_unsigned_32, LIBRARY_PATH), Cint, (Ptr{UInt32},), data))
elseif shared_memory_type === UInt64
data = Vector{UInt64}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_unsigned_64, LIBRARY_PATH), Cint, (Ptr{UInt64},), data))
elseif shared_memory_type === Int8
data = Vector{Int8}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_signed_8, LIBRARY_PATH), Cint, (Ptr{Int8},), data))
elseif shared_memory_type === Int16
data = Vector{Int16}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_signed_16, LIBRARY_PATH), Cint, (Ptr{Int16},), data))
elseif shared_memory_type === Int32
data = Vector{Int32}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_signed_32, LIBRARY_PATH), Cint, (Ptr{Int32},), data))
elseif shared_memory_type === Int64
data = Vector{Int64}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_signed_64, LIBRARY_PATH), Cint, (Ptr{Int64},), data))
elseif shared_memory_type === Float32
data = Vector{Float32}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_float32, LIBRARY_PATH), Cint, (Ptr{Float32},), data))
elseif shared_memory_type === Float64
data = Vector{Float64}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_float64, LIBRARY_PATH), Cint, (Ptr{Float64},), data))
elseif shared_memory_type === ComplexF32
data = Vector{ComplexF32}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_float32, LIBRARY_PATH), Cint, (Ptr{Float32},), data))
elseif shared_memory_type === ComplexF64
data = Vector{ComplexF64}(undef,shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_flatten_data_float64, LIBRARY_PATH), Cint, (Ptr{Float64},), data))
elseif shared_memory_type === String
# data::String
data = repeat(" ", shared_memory_flatten_length)
check_library_error(ccall((:get_shared_memory_string, LIBRARY_PATH), Cint, (Cstring,), data))
end
return data
end
"""
Will raise SharedMemoryLibraryError if the operation was no successful.
## Return
Either string or the data store in the shared memory in row-major layout.
"""
function get_shared_memory_data()::Union{Array,String}
type::DataType = get_shared_memory_data_type()
data::Union{Vector,String} = get_shared_memory_flatten_data()
if type === String
return data
end
size::Vector{UInt64} = get_shared_memory_dimensions()
return reshape(data, Tuple(size))
end
"""
Will raise SharedMemoryLibraryError if the operation was no successful.
# Arguments
- `data::String`: data to be stored in shared memory.
"""
function set_shared_memory_data(data::String)::Nothing
check_library_error(ccall((:set_shared_memory_string,LIBRARY_PATH), Cint, (Cstring,),data))
nothing
end
"""
Array with float16 type will be converted to float32.
Will raise SharedMemoryLibraryError if the operation was no successful.
# Arguments
- `data::Array`: data to be stored in shared memory. It can be in any dimension with supported types.
"""
function set_shared_memory_data(data::Array)::Nothing
temp_dims::Vector{UInt64} = convert(Vector{UInt64},collect(size(data)))
temp_rank::Int = length(temp_dims)
data_type::DataType = eltype(data)
if data_type === UInt8
check_library_error(ccall((:set_shared_memory_data_unsigned_8, LIBRARY_PATH), Cint
, (Ptr{UInt8},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === UInt16
check_library_error(ccall((:set_shared_memory_data_unsigned_16, LIBRARY_PATH), Cint
, (Ptr{UInt16},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === UInt32
check_library_error(ccall((:set_shared_memory_data_unsigned_32, LIBRARY_PATH), Cint
, (Ptr{UInt32},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === UInt64
check_library_error(ccall((:set_shared_memory_data_unsigned_64, LIBRARY_PATH), Cint
, (Ptr{UInt64},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === Int8
check_library_error(ccall((:set_shared_memory_data_signed_8, LIBRARY_PATH), Cint
, (Ptr{Int8},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === Int16
check_library_error(ccall((:set_shared_memory_data_signed_16, LIBRARY_PATH), Cint
, (Ptr{Int16},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === Int32
check_library_error(ccall((:set_shared_memory_data_signed_32, LIBRARY_PATH), Cint
, (Ptr{Int32},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === Int64
check_library_error(ccall((:set_shared_memory_data_signed_64, LIBRARY_PATH), Cint
, (Ptr{Int64},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === Float32
check_library_error(ccall((:set_shared_memory_data_float32, LIBRARY_PATH), Cint
, (Ptr{Float32},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === Float64
check_library_error(ccall((:set_shared_memory_data_float64, LIBRARY_PATH), Cint
, (Ptr{Float64},Ptr{UInt64},UInt64)
, data,temp_dims,temp_rank))
elseif data_type === ComplexF32
check_library_error(ccall((:set_shared_memory_data_complex_float32, LIBRARY_PATH), Cint, (Ptr{Float32},Ptr{UInt64},UInt64), data,temp_dims,temp_rank));
elseif data_type === ComplexF64
check_library_error(ccall((:set_shared_memory_data_complex_float64, LIBRARY_PATH), Cint, (Ptr{Float64},Ptr{UInt64},UInt64), data,temp_dims,temp_rank));
# Convert float16 array to float32 array
elseif data_type === Float16
println("Float16 is not supported. Automatically converted to Float32");
set_shared_memory_data(convert(Array{Float32},data));
# Raise error if type is not supported
else
throw(ArgumentError("DataType "*string(data_type)*" is not supported."));
end
nothing
end
"""
Unload the data from memory if it can connect to it, otherwise will remove the specified file.
Will raise SharedMemoryLibraryError if the operation was no successful.
"""
function delete_shared_memory()::Nothing
check_library_error(ccall((:delete_shared_memory, LIBRARY_PATH), Cint, ()))
nothing
end
end