Skip to content

Commit

Permalink
[WIP] Add pinned memory resource to C API (#311)
Browse files Browse the repository at this point in the history
Let me know if this is out of scope for cuVS!

Authors:
  - Ajit Mistry (https://github.com/ajit283)
  - Ben Frederickson (https://github.com/benfred)
  - Corey J. Nolet (https://github.com/cjnolet)

Approvers:
  - Corey J. Nolet (https://github.com/cjnolet)
  - Ben Frederickson (https://github.com/benfred)

URL: #311
  • Loading branch information
ajit283 authored Dec 4, 2024
1 parent acbd097 commit a96b720
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cpp/include/cuvs/core/c_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ cuvsError_t cuvsRMMPoolMemoryResourceEnable(int initial_pool_size_percent,
*/
cuvsError_t cuvsRMMMemoryResourceReset();

/**
* @brief Allocates pinned memory on the host using RMM
* @param[out] ptr Pointer to allocated host memory
* @param[in] bytes Size in bytes to allocate
* @return cuvsError_t
*/
cuvsError_t cuvsRMMHostAlloc(void** ptr, size_t bytes);

/**
* @brief Deallocates pinned memory on the host using RMM
* @param[in] ptr Pointer to allocated host memory to free
* @param[in] bytes Size in bytes to deallocate
* @return cuvsError_t
*/
cuvsError_t cuvsRMMHostFree(void* ptr, size_t bytes);

/** @} */

#ifdef __cplusplus
Expand Down
16 changes: 16 additions & 0 deletions cpp/src/core/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <rmm/mr/device/owning_wrapper.hpp>
#include <rmm/mr/device/per_device_resource.hpp>
#include <rmm/mr/device/pool_memory_resource.hpp>
#include <rmm/mr/host/pinned_memory_resource.hpp>
#include <thread>

extern "C" cuvsError_t cuvsResourcesCreate(cuvsResources_t* res)
Expand Down Expand Up @@ -130,6 +131,21 @@ extern "C" cuvsError_t cuvsRMMMemoryResourceReset()
});
}

thread_local std::unique_ptr<rmm::mr::pinned_memory_resource> pinned_mr;

extern "C" cuvsError_t cuvsRMMHostAlloc(void** ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] {
if (pinned_mr == nullptr) { pinned_mr = std::make_unique<rmm::mr::pinned_memory_resource>(); }
*ptr = pinned_mr->allocate(bytes);
});
}

extern "C" cuvsError_t cuvsRMMHostFree(void* ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] { pinned_mr->deallocate(ptr, bytes); });
}

thread_local std::string last_error_text = "";

extern "C" const char* cuvsGetLastErrorText()
Expand Down
9 changes: 9 additions & 0 deletions cpp/test/core/c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ int main()
error = cuvsRMMMemoryResourceReset();
if (error == CUVS_ERROR) { exit(EXIT_FAILURE); }

// Alloc memory on host (pinned)
void* ptr3;
cuvsError_t alloc_error_pinned = cuvsRMMHostAlloc(&ptr3, 1024);
if (alloc_error_pinned == CUVS_ERROR) { exit(EXIT_FAILURE); }

// Free memory
cuvsError_t free_error_pinned = cuvsRMMHostFree(ptr3, 1024);
if (free_error_pinned == CUVS_ERROR) { exit(EXIT_FAILURE); }

// Destroy resources
error = cuvsResourcesDestroy(res);
if (error == CUVS_ERROR) { exit(EXIT_FAILURE); }
Expand Down

0 comments on commit a96b720

Please sign in to comment.