cufile_h_wrapper.hpp
1 /*
2  * Copyright (c) 2022-2024, 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 <cuda.h>
19 #include <sys/types.h>
20 
28 #ifdef KVIKIO_CUFILE_FOUND
29 #include <cufile.h>
30 #else
31 
32 // If cuFile isn't defined, we define some of the data types here.
33 // Notice, this doesn't need to be ABI compatible with the cufile definitions.
34 
35 using CUfileHandle_t = void*;
36 using CUfileOpError = int;
37 #define CUFILE_ERRSTR(x) ("KvikIO not compiled with cuFile.h")
38 #define CU_FILE_SUCCESS 0
39 #define CU_FILE_CUDA_DRIVER_ERROR 1
40 
41 struct CUfileError_t {
42  CUfileOpError err; // cufile error
43  CUresult cu_err; // cuda driver error
44 };
45 
46 using CUfileDriverControlFlags_t = enum CUfileDriverControlFlags {
47  CU_FILE_USE_POLL_MODE = 0,
48  CU_FILE_ALLOW_COMPAT_MODE = 1
49 };
50 
51 enum CUfileFileHandleType { CU_FILE_HANDLE_TYPE_OPAQUE_FD = 1 };
52 
53 struct CUfileDescr_t {
54  enum CUfileFileHandleType type;
55  struct handle_t {
56  int fd;
57  } handle;
58 };
59 
60 static inline const char* cufileop_status_error(CUfileOpError err) { return CUFILE_ERRSTR(err); };
61 CUfileError_t cuFileHandleRegister(...);
62 CUfileError_t cuFileHandleDeregister(...);
63 ssize_t cuFileRead(...);
64 ssize_t cuFileWrite(...);
65 CUfileError_t cuFileBufRegister(...);
66 CUfileError_t cuFileBufDeregister(...);
67 CUfileError_t cuFileDriverOpen(...);
68 CUfileError_t cuFileDriverClose(...);
69 CUfileError_t cuFileDriverGetProperties(...);
70 CUfileError_t cuFileDriverSetPollMode(...);
71 CUfileError_t cuFileDriverSetMaxCacheSize(...);
72 CUfileError_t cuFileDriverSetMaxPinnedMemSize(...);
73 
74 #endif
75 
76 // If the Batch API isn't defined, we define some of the data types here.
77 // Notice, this doesn't need to be ABI compatible with the cufile definitions and
78 // the lack of definitions is not a problem because the linker will never look for
79 // these symbols because the "real" function calls are made through the shim instance.
80 #ifndef KVIKIO_CUFILE_BATCH_API_FOUND
81 typedef enum CUfileOpcode { CUFILE_READ = 0, CUFILE_WRITE } CUfileOpcode_t;
82 
83 typedef enum CUFILEStatus_enum {
84  CUFILE_WAITING = 0x000001, /* required value prior to submission */
85  CUFILE_PENDING = 0x000002, /* once enqueued */
86  CUFILE_INVALID = 0x000004, /* request was ill-formed or could not be enqueued */
87  CUFILE_CANCELED = 0x000008, /* request successfully canceled */
88  CUFILE_COMPLETE = 0x0000010, /* request successfully completed */
89  CUFILE_TIMEOUT = 0x0000020, /* request timed out */
90  CUFILE_FAILED = 0x0000040 /* unable to complete */
91 } CUfileStatus_t;
92 
93 typedef struct CUfileIOEvents {
94  void* cookie;
95  CUfileStatus_t status; /* status of the operation */
96  size_t ret; /* -ve error or amount of I/O done. */
98 
99 CUfileError_t cuFileBatchIOSetUp(...);
100 CUfileError_t cuFileBatchIOSubmit(...);
101 CUfileError_t cuFileBatchIOGetStatus(...);
102 CUfileError_t cuFileBatchIOCancel(...);
103 CUfileError_t cuFileBatchIODestroy(...);
104 #endif
105 
106 // If the Stream API isn't defined, we define some of the data types here.
107 #ifndef KVIKIO_CUFILE_STREAM_API_FOUND
108 CUfileError_t cuFileReadAsync(...);
109 CUfileError_t cuFileWriteAsync(...);
110 CUfileError_t cuFileStreamRegister(...);
111 CUfileError_t cuFileStreamDeregister(...);
112 #endif