-
Notifications
You must be signed in to change notification settings - Fork 61
/
cufile_stub.cpp
263 lines (235 loc) · 8.84 KB
/
cufile_stub.cpp
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
/*
* Copyright (c) 2020, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cufile_stub.h"
#include "cucim/dynlib/helper.h"
#define IMPORT_FUNCTION(handle, name) impl_##name = cucim::dynlib::get_library_symbol<t_##name>(handle, #name);
typedef CUfileError_t (*t_cuFileHandleRegister)(CUfileHandle_t* fh, CUfileDescr_t* descr);
typedef void (*t_cuFileHandleDeregister)(CUfileHandle_t fh);
typedef CUfileError_t (*t_cuFileBufRegister)(const void* devPtr_base, size_t length, int flags);
typedef CUfileError_t (*t_cuFileBufDeregister)(const void* devPtr_base);
typedef ssize_t (*t_cuFileRead)(CUfileHandle_t fh, void* devPtr_base, size_t size, off_t file_offset, off_t devPtr_offset);
typedef ssize_t (*t_cuFileWrite)(
CUfileHandle_t fh, const void* devPtr_base, size_t size, off_t file_offset, off_t devPtr_offset);
typedef CUfileError_t (*t_cuFileDriverOpen)(void);
typedef CUfileError_t (*t_cuFileDriverClose)(void);
typedef CUfileError_t (*t_cuFileDriverGetProperties)(CUfileDrvProps_t* props);
typedef CUfileError_t (*t_cuFileDriverSetPollMode)(bool poll, size_t poll_threshold_size);
typedef CUfileError_t (*t_cuFileDriverSetMaxDirectIOSize)(size_t max_direct_io_size);
typedef CUfileError_t (*t_cuFileDriverSetMaxCacheSize)(size_t max_cache_size);
typedef CUfileError_t (*t_cuFileDriverSetMaxPinnedMemSize)(size_t max_pinned_size);
static t_cuFileHandleRegister impl_cuFileHandleRegister = nullptr;
static t_cuFileHandleDeregister impl_cuFileHandleDeregister = nullptr;
static t_cuFileBufRegister impl_cuFileBufRegister = nullptr;
static t_cuFileBufDeregister impl_cuFileBufDeregister = nullptr;
static t_cuFileRead impl_cuFileRead = nullptr;
static t_cuFileWrite impl_cuFileWrite = nullptr;
static t_cuFileDriverOpen impl_cuFileDriverOpen = nullptr;
static t_cuFileDriverClose impl_cuFileDriverClose = nullptr;
static t_cuFileDriverGetProperties impl_cuFileDriverGetProperties = nullptr;
static t_cuFileDriverSetPollMode impl_cuFileDriverSetPollMode = nullptr;
static t_cuFileDriverSetMaxDirectIOSize impl_cuFileDriverSetMaxDirectIOSize = nullptr;
static t_cuFileDriverSetMaxCacheSize impl_cuFileDriverSetMaxCacheSize = nullptr;
static t_cuFileDriverSetMaxPinnedMemSize impl_cuFileDriverSetMaxPinnedMemSize = nullptr;
class CuFileStub
{
public:
void load()
{
#if !CUCIM_SUPPORT_GDS
return;
#endif
#if !CUCIM_STATIC_GDS
if (handle_ == nullptr)
{
handle_ = cucim::dynlib::load_library("libcufile.so");
if (handle_ == nullptr)
{
return;
}
IMPORT_FUNCTION(handle_, cuFileDriverOpen);
IMPORT_FUNCTION(handle_, cuFileHandleRegister);
IMPORT_FUNCTION(handle_, cuFileHandleDeregister);
IMPORT_FUNCTION(handle_, cuFileBufRegister);
IMPORT_FUNCTION(handle_, cuFileBufDeregister);
IMPORT_FUNCTION(handle_, cuFileRead);
IMPORT_FUNCTION(handle_, cuFileWrite);
IMPORT_FUNCTION(handle_, cuFileDriverOpen);
IMPORT_FUNCTION(handle_, cuFileDriverClose);
IMPORT_FUNCTION(handle_, cuFileDriverGetProperties);
IMPORT_FUNCTION(handle_, cuFileDriverSetPollMode);
IMPORT_FUNCTION(handle_, cuFileDriverSetMaxDirectIOSize);
IMPORT_FUNCTION(handle_, cuFileDriverSetMaxCacheSize);
IMPORT_FUNCTION(handle_, cuFileDriverSetMaxPinnedMemSize);
}
#endif
}
void unload()
{
#if !CUCIM_SUPPORT_GDS
return;
#endif
#if !CUCIM_STATIC_GDS
if (handle_)
{
cucim::dynlib::unload_library(handle_);
handle_ = nullptr;
impl_cuFileDriverOpen = nullptr;
impl_cuFileHandleRegister = nullptr;
impl_cuFileHandleDeregister = nullptr;
impl_cuFileBufRegister = nullptr;
impl_cuFileBufDeregister = nullptr;
impl_cuFileRead = nullptr;
impl_cuFileWrite = nullptr;
impl_cuFileDriverOpen = nullptr;
impl_cuFileDriverClose = nullptr;
impl_cuFileDriverGetProperties = nullptr;
impl_cuFileDriverSetPollMode = nullptr;
impl_cuFileDriverSetMaxDirectIOSize = nullptr;
impl_cuFileDriverSetMaxCacheSize = nullptr;
impl_cuFileDriverSetMaxPinnedMemSize = nullptr;
}
#endif
}
~CuFileStub()
{
// Note: unload() would be called explicitly by CuFileDriverInitializer to unload the shared library after calling
// cuFileDriverClose() in CuFileDriverInitializer::~CuFileDriverInitializer()
// unload();
}
private:
cucim::dynlib::LibraryHandle handle_ = nullptr;
};
static CuFileStub g_cufile_stub;
#if __cplusplus
extern "C"
{
#endif
void open_cufile_stub()
{
g_cufile_stub.load();
}
void close_cufile_stub()
{
g_cufile_stub.unload();
}
#if !CUCIM_STATIC_GDS
CUfileError_t cuFileHandleRegister(CUfileHandle_t* fh, CUfileDescr_t* descr)
{
if (impl_cuFileHandleRegister)
{
return impl_cuFileHandleRegister(fh, descr);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
void cuFileHandleDeregister(CUfileHandle_t fh)
{
if (impl_cuFileHandleDeregister)
{
impl_cuFileHandleDeregister(fh);
}
}
CUfileError_t cuFileBufRegister(const void* devPtr_base, size_t length, int flags)
{
if (impl_cuFileBufRegister)
{
return impl_cuFileBufRegister(devPtr_base, length, flags);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
CUfileError_t cuFileBufDeregister(const void* devPtr_base)
{
if (impl_cuFileBufDeregister)
{
return impl_cuFileBufDeregister(devPtr_base);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
ssize_t cuFileRead(CUfileHandle_t fh, void* devPtr_base, size_t size, off_t file_offset, off_t devPtr_offset)
{
if (impl_cuFileRead)
{
return impl_cuFileRead(fh, devPtr_base, size, file_offset, devPtr_offset);
}
return -1;
}
ssize_t cuFileWrite(CUfileHandle_t fh, const void* devPtr_base, size_t size, off_t file_offset, off_t devPtr_offset)
{
if (impl_cuFileWrite)
{
return impl_cuFileWrite(fh, devPtr_base, size, file_offset, devPtr_offset);
}
return -1;
}
CUfileError_t cuFileDriverOpen(void)
{
if (impl_cuFileDriverOpen)
{
return impl_cuFileDriverOpen();
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
CUfileError_t cuFileDriverClose(void)
{
if (impl_cuFileDriverClose)
{
return impl_cuFileDriverClose();
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
CUfileError_t cuFileDriverGetProperties(CUfileDrvProps_t* props)
{
if (impl_cuFileDriverGetProperties)
{
return impl_cuFileDriverGetProperties(props);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
CUfileError_t cuFileDriverSetPollMode(bool poll, size_t poll_threshold_size)
{
if (impl_cuFileDriverSetPollMode)
{
return impl_cuFileDriverSetPollMode(poll, poll_threshold_size);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
CUfileError_t cuFileDriverSetMaxDirectIOSize(size_t max_direct_io_size)
{
if (impl_cuFileDriverSetMaxDirectIOSize)
{
return impl_cuFileDriverSetMaxDirectIOSize(max_direct_io_size);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
CUfileError_t cuFileDriverSetMaxCacheSize(size_t max_cache_size)
{
if (impl_cuFileDriverSetMaxCacheSize)
{
return impl_cuFileDriverSetMaxCacheSize(max_cache_size);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
CUfileError_t cuFileDriverSetMaxPinnedMemSize(size_t max_pinned_size)
{
if (impl_cuFileDriverSetMaxPinnedMemSize)
{
return impl_cuFileDriverSetMaxPinnedMemSize(max_pinned_size);
}
return CUfileError_t{ CU_FILE_DRIVER_NOT_INITIALIZED, CUDA_SUCCESS };
}
#endif
#if __cplusplus
}
#endif