libkvikio  23.12.00
buffer.hpp
1 /*
2  * Copyright (c) 2021-2023, 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 <algorithm>
19 #include <iostream>
20 #include <map>
21 #include <vector>
22 
23 #include <kvikio/defaults.hpp>
24 #include <kvikio/error.hpp>
25 #include <kvikio/shim/cufile.hpp>
26 #include <kvikio/shim/cufile_h_wrapper.hpp>
27 #include <kvikio/utils.hpp>
28 
29 namespace kvikio {
30 
46 /*NOLINTNEXTLINE(readability-function-cognitive-complexity)*/
47 inline void buffer_register(const void* devPtr_base,
48  std::size_t size,
49  int flags = 0,
50  const std::vector<int>& errors_to_ignore = std::vector<int>())
51 {
52  if (defaults::compat_mode()) { return; }
53 #ifdef KVIKIO_CUFILE_FOUND
54  CUfileError_t status = cuFileAPI::instance().BufRegister(devPtr_base, size, flags);
55  if (status.err != CU_FILE_SUCCESS) {
56  // Check if `status.err` is in `errors_to_ignore`
57  if (std::find(errors_to_ignore.begin(), errors_to_ignore.end(), status.err) ==
58  errors_to_ignore.end()) {
59  CUFILE_TRY(status);
60  }
61  }
62 #endif
63 }
64 
70 inline void buffer_deregister(const void* devPtr_base)
71 {
72  if (defaults::compat_mode()) { return; }
73 #ifdef KVIKIO_CUFILE_FOUND
74  CUFILE_TRY(cuFileAPI::instance().BufDeregister(devPtr_base));
75 #endif
76 }
77 
92 inline void memory_register(const void* devPtr,
93  int flags = 0,
94  const std::vector<int>& errors_to_ignore = {})
95 {
96  auto [base, nbytes, offset] = get_alloc_info(devPtr);
97  buffer_register(base, nbytes, flags, errors_to_ignore);
98 }
99 
105 inline void memory_deregister(const void* devPtr)
106 {
107  auto [base, nbytes, offset] = get_alloc_info(devPtr);
108  buffer_deregister(base);
109 }
110 
111 } // namespace kvikio
static bool compat_mode()
Return whether the KvikIO library is running in compatibility mode or not.
Definition: defaults.hpp:148