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