All Classes Namespaces Functions Enumerations Enumerator Modules Pages
cufile.hpp
1 /*
2  * Copyright (c) 2022-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <kvikio/shim/cufile_h_wrapper.hpp>
19 #include <kvikio/shim/utils.hpp>
20 
21 namespace kvikio {
22 
30 class cuFileAPI {
31  public:
32  decltype(cuFileHandleRegister)* HandleRegister{nullptr};
33  decltype(cuFileHandleDeregister)* HandleDeregister{nullptr};
34  decltype(cuFileRead)* Read{nullptr};
35  decltype(cuFileWrite)* Write{nullptr};
36  decltype(cuFileBufRegister)* BufRegister{nullptr};
37  decltype(cuFileBufDeregister)* BufDeregister{nullptr};
38  decltype(cuFileDriverGetProperties)* DriverGetProperties{nullptr};
39  decltype(cuFileDriverSetPollMode)* DriverSetPollMode{nullptr};
40  decltype(cuFileDriverSetMaxCacheSize)* DriverSetMaxCacheSize{nullptr};
41  decltype(cuFileDriverSetMaxPinnedMemSize)* DriverSetMaxPinnedMemSize{nullptr};
42  decltype(cuFileBatchIOSetUp)* BatchIOSetUp{nullptr};
43  decltype(cuFileBatchIOSubmit)* BatchIOSubmit{nullptr};
44  decltype(cuFileBatchIOGetStatus)* BatchIOGetStatus{nullptr};
45  decltype(cuFileBatchIOCancel)* BatchIOCancel{nullptr};
46  decltype(cuFileBatchIODestroy)* BatchIODestroy{nullptr};
47  decltype(cuFileReadAsync)* ReadAsync{nullptr};
48  decltype(cuFileWriteAsync)* WriteAsync{nullptr};
49  decltype(cuFileStreamRegister)* StreamRegister{nullptr};
50  decltype(cuFileStreamDeregister)* StreamDeregister{nullptr};
51 
52  private:
53  // Don't call driver open and close directly, use `.driver_open()` and `.driver_close()`.
54  decltype(cuFileDriverOpen)* DriverOpen{nullptr};
55  decltype(cuFileDriverClose)* DriverClose{nullptr};
56 
57  // Don't call `GetVersion` directly, use `cuFileAPI::instance().version`.
58  decltype(cuFileGetVersion)* GetVersion{nullptr};
59 
60  public:
61  int version{0};
62 
63  private:
64  cuFileAPI();
65 
66 #ifdef KVIKIO_CUFILE_FOUND
67  // Notice, we have to close the driver at program exit (if we opened it) even though we are
68  // not allowed to call CUDA after main[1]. This is because, cuFile will segfault if the
69  // driver isn't closed on program exit i.e. we are doomed if we do, doomed if we don't, but
70  // this seems to be the lesser of two evils.
71  // [1] <https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#initialization>
72  ~cuFileAPI();
73 #endif
74 
75  public:
76  cuFileAPI(cuFileAPI const&) = delete;
77  void operator=(cuFileAPI const&) = delete;
78  cuFileAPI(cuFileAPI const&&) = delete;
79  void operator=(cuFileAPI const&&) = delete;
80 
81  KVIKIO_EXPORT static cuFileAPI& instance();
82 
89  void driver_open();
90 
94  void driver_close();
95 };
96 
104 #ifdef KVIKIO_CUFILE_FOUND
105 bool is_cufile_library_available() noexcept;
106 #else
107 constexpr bool is_cufile_library_available() noexcept { return false; }
108 #endif
109 
118 bool is_cufile_available() noexcept;
119 
131 #ifdef KVIKIO_CUFILE_FOUND
132 int cufile_version() noexcept;
133 #else
134 constexpr int cufile_version() noexcept { return 0; }
135 #endif
136 
146 bool is_batch_api_available() noexcept;
147 
157 bool is_stream_api_available() noexcept;
158 
159 } // namespace kvikio
Shim layer of the cuFile C-API.
Definition: cufile.hpp:30
void driver_open()
Open the cuFile driver.
void driver_close()
Close the cuFile driver.
KvikIO namespace.
Definition: batch.hpp:27
constexpr bool is_cufile_library_available() noexcept
Check if the cuFile library is available.
Definition: cufile.hpp:107
bool is_cufile_available() noexcept
Check if the cuFile is available and expected to work.
bool is_stream_api_available() noexcept
Check if cuFile's stream (async) API is available.
constexpr int cufile_version() noexcept
Get cufile version (or zero if older than v1.8).
Definition: cufile.hpp:134
bool is_batch_api_available() noexcept
Check if cuFile's batch API is available.