Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajit283 committed Sep 6, 2024
1 parent 7d144cf commit 9e63623
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 @@ -148,6 +148,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 @@ -23,6 +23,7 @@
#include <rmm/cuda_stream_view.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 @@ -111,6 +112,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 @@ -58,6 +58,15 @@ int main()
cuvsError_t reset_error = cuvsRMMMemoryResourceReset();
if (reset_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
cuvsError_t destroy_error = cuvsResourcesDestroy(res);
if (destroy_error == CUVS_ERROR) { exit(EXIT_FAILURE); }
Expand Down

0 comments on commit 9e63623

Please sign in to comment.