8 #ifndef CUDA_API_WRAPPERS_LIBRARY_HPP_ 9 #define CUDA_API_WRAPPERS_LIBRARY_HPP_ 11 #if CUDA_VERSION >= 12000 16 #if __cplusplus >= 201703L 41 using option_t = CUlibraryOption;
49 inline library_t
wrap(
51 bool take_ownership =
false) noexcept;
53 inline ::
std::
string identify(const library::
handle_t &handle)
55 return ::std::string(
"library ") + cuda::detail_::ptr_as_hex(handle);
58 ::std::string identify(
const library_t &library);
62 return cuLibraryUnload(handle);
67 auto status = unload_nothrow(handle);
69 + library::detail_::identify(handle));
80 template <
typename ContiguousContainer,
82 cuda::detail_::enable_if_t<cuda::detail_::is_kinda_like_contiguous_container<ContiguousContainer>::value,
bool> =
true >
84 ContiguousContainer library_data,
85 optional<link::options_t> link_options,
86 bool code_is_preserved);
95 auto status = cuLibraryGetKernel(&kernel_handle, library_handle, name);
97 + name +
"' from " + library::detail_::identify(library_handle));
103 CAW_SET_SCOPE_CONTEXT(context_handle);
104 return get_kernel_in_current_context(library_handle, name);
109 inline kernel_t get_kernel(
const library_t& library,
const char* name);
110 inline kernel_t get_kernel(context_t& context,
const library_t& library,
const char* name);
114 memory::region_t get_global(
const context_t& context,
const library_t& library,
const char* name);
115 memory::region_t get_managed_region(
const library_t& library,
const char* name);
119 module_t create(
const context_t& context,
const library_t& library);
120 module_t create(
const library_t& library);
124 void* get_unified_function(
const context_t& context,
const library_t& library,
const char* symbol);
147 library::kernel_t get_kernel(
const context_t& context,
const char* name)
const;
148 library::kernel_t get_kernel(
const context_t& context, const ::std::string& name)
const;
149 library::kernel_t get_kernel(
const char* name)
const;
150 library::kernel_t get_kernel(const ::std::string& name)
const;
154 return cuda::get_global(context::current::get(), *
this, name);
159 return get_global(name.c_str());
164 return cuda::get_managed_region(*
this, name);
169 return get_managed(name.c_str());
175 : handle_(handle), owning_(owning)
184 library_t(
const library_t&) =
delete;
186 library_t(library_t&& other) noexcept : library_t(other.handle_, other.owning_)
188 other.owning_ =
false;
191 ~library_t() DESTRUCTOR_EXCEPTION_SPEC
193 if (not owning_) {
return; }
194 #ifdef THROW_IN_DESTRUCTORS 195 library::detail_::unload(handle_);
197 library::detail_::unload_nothrow(handle_);
203 library_t& operator=(
const library_t&) =
delete;
204 library_t& operator=(library_t&& other) noexcept
206 ::std::swap(handle_, other.handle_);
207 ::std::swap(owning_, other.owning_);
218 inline memory::region_t get_global(
const context_t& context,
const library_t& library,
const char* name)
222 auto result = cuLibraryGetGlobal(&dptr, &size, library.handle(), name);
224 ::std::string(
"Obtaining the memory address and size for the global object '") + name +
"' from " 225 + library::detail_::identify(library) +
" in context " + context::detail_::identify(context));
235 inline memory::region_t get_managed_region(
const library_t& library,
const char* name)
239 auto status = cuLibraryGetManaged(®ion_start, ®ion_size, library.handle(), name);
240 throw_if_error_lazy(status, ::std::string(
"Failed obtaining the managed memory region '") + name
241 +
"' from " + library::detail_::identify(library));
250 inline module_t create(
const context_t& context,
const library_t& library)
252 CAW_SET_SCOPE_CONTEXT(context.handle());
254 auto status = cuLibraryGetModule(&new_handle, library.handle());
256 +
"' from " + library::detail_::identify(library) +
" in " + context::detail_::identify(context));
257 constexpr
const bool is_owning {
true };
259 is_owning, do_hold_primary_context_refcount_unit);
267 inline void* get_unified_function(
const context_t& context,
const library_t& library,
const char* symbol)
269 CAW_SET_SCOPE_CONTEXT(context.handle());
271 auto status = cuLibraryGetUnifiedFunction(&function_ptr, library.handle(), symbol);
272 throw_if_error_lazy(status, ::std::string(
"Failed obtaining a pointer for function '") + symbol
273 +
"' from " + library::detail_::identify(library) +
" in " + context::detail_::identify(context));
281 template <
typename Creator,
typename DataSource,
typename ErrorStringGenerator>
284 DataSource data_source,
285 ErrorStringGenerator error_string_generator,
286 const link::options_t& link_options = {},
287 bool code_is_preserved =
false)
290 auto raw_link_opts = link::detail_::marshal(link_options);
292 detail_::option_t options[1];
295 } raw_opts = { { CU_LIBRARY_BINARY_IS_PRESERVED }, { &code_is_preserved }, 1 };
296 auto status = creator(
297 &new_lib_handle, data_source,
298 const_cast<link::detail_::option_t*>(raw_link_opts.options()),
299 const_cast<void**>(raw_link_opts.values()), raw_link_opts.count(),
300 raw_opts.options, raw_opts.values, raw_opts.count
303 ::std::string(
"Failed loading a compiled CUDA code library from ") + error_string_generator());
304 bool do_take_ownership{
true};
324 const link::options_t& link_options = {},
325 bool code_is_preserved =
false)
327 return detail_::create(
328 cuLibraryLoadFromFile, path,
329 [path]() { return ::std::string(
"file ") + path; },
330 link_options, code_is_preserved);
334 const ::std::string& path,
335 const link::options_t& link_options = {},
336 bool code_is_preserved =
false)
338 return load_from_file(path.c_str(), link_options, code_is_preserved);
341 #if __cplusplus >= 201703L 344 const ::std::filesystem::path& path,
345 const link::options_t& link_options = {},
346 bool code_is_preserved =
false)
348 return load_from_file(path.c_str(), link_options, code_is_preserved);
356 inline library_t
wrap(
handle_t handle,
bool take_ownership) noexcept
358 return library_t{handle, take_ownership};
370 inline library_t create(
371 const void* module_data,
372 const link::options_t& link_options = {},
373 bool code_is_preserved =
false)
375 return detail_::create(
376 cuLibraryLoadData, module_data,
377 [module_data]() { return ::std::string(
"data at ") + cuda::detail_::ptr_as_hex(module_data); },
378 link_options, code_is_preserved);
387 inline ::std::string identify(
const library_t& library)
389 return identify(library.handle());
394 template <
typename ContiguousContainer,
395 cuda::detail_::enable_if_t<cuda::detail_::is_kinda_like_contiguous_container<ContiguousContainer>::value,
bool> >
397 ContiguousContainer library_data,
398 optional<link::options_t> link_options,
399 bool code_is_preserved)
401 return create(library_data.data(), link_options, code_is_preserved);
408 #endif // CUDA_VERSION >= 12000 410 #endif // CUDA_API_WRAPPERS_LIBRARY_HPP_ Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
device::id_t count()
Get the number of CUDA devices usable on the system (with the current CUDA library and kernel driver)...
Definition: miscellany.hpp:63
detail_::region_helper< memory::region_t > region_t
A child class of the generic region_t with some managed-memory-specific functionality.
Definition: memory.hpp:1974
module_t load_from_file(const context_t &context, const char *path)
Load a module from an appropriate compiled or semi-compiled file, allocating all relevant resources f...
Definition: module.hpp:321
#define throw_if_error_lazy(status__,...)
A macro for only throwing an error if we've failed - which also ensures no string is constructed unle...
Definition: error.hpp:327
CUarray handle_t
Raw CUDA driver handle for arrays (of any dimension)
Definition: array.hpp:34
array_t< T, NumDimensions > wrap(device::id_t device_id, context::handle_t context_handle, handle_t handle, dimensions_t< NumDimensions > dimensions) noexcept
Wrap an existing CUDA array in an array_t instance.
Definition: array.hpp:271
void * as_pointer(device::address_t address) noexcept
Definition: types.hpp:702
CUdeviceptr address_t
The numeric type which can represent the range of memory addresses on a CUDA device.
Definition: types.hpp:674
A host-side binary object with embedded device code; a .o file.
CUresult status_t
Indicates either the result (success or error index) of a CUDA Runtime or Driver API call...
Definition: types.hpp:74