forked from rapidsai/cuvs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc_api.cpp
More file actions
164 lines (142 loc) · 5.58 KB
/
c_api.cpp
File metadata and controls
164 lines (142 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdint>
#include <cuvs/core/c_api.h>
#include <cuvs/core/exceptions.hpp>
#include <memory>
#include <raft/core/resource/cuda_stream.hpp>
#include <raft/core/resources.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/mr/device/device_memory_resource.hpp>
#include <rmm/mr/device/managed_memory_resource.hpp>
#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)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = new raft::resources{};
*res = reinterpret_cast<uintptr_t>(res_ptr);
});
}
extern "C" cuvsError_t cuvsResourcesDestroy(cuvsResources_t res)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
delete res_ptr;
});
}
extern "C" cuvsError_t cuvsStreamSet(cuvsResources_t res, cudaStream_t stream)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
raft::resource::set_cuda_stream(*res_ptr, static_cast<rmm::cuda_stream_view>(stream));
});
}
extern "C" cuvsError_t cuvsStreamGet(cuvsResources_t res, cudaStream_t* stream)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
*stream = raft::resource::get_cuda_stream(*res_ptr);
});
}
extern "C" cuvsError_t cuvsStreamSync(cuvsResources_t res)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
raft::resource::sync_stream(*res_ptr);
});
}
extern "C" cuvsError_t cuvsRMMAlloc(cuvsResources_t res, void** ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
auto mr = rmm::mr::get_current_device_resource();
*ptr = mr->allocate(bytes, raft::resource::get_cuda_stream(*res_ptr));
});
}
extern "C" cuvsError_t cuvsRMMFree(cuvsResources_t res, void* ptr, size_t bytes)
{
return cuvs::core::translate_exceptions([=] {
auto res_ptr = reinterpret_cast<raft::resources*>(res);
auto mr = rmm::mr::get_current_device_resource();
mr->deallocate(ptr, bytes, raft::resource::get_cuda_stream(*res_ptr));
});
}
thread_local std::shared_ptr<
rmm::mr::owning_wrapper<rmm::mr::pool_memory_resource<rmm::mr::device_memory_resource>,
rmm::mr::device_memory_resource>>
pool_mr;
extern "C" cuvsError_t cuvsRMMPoolMemoryResourceEnable(int initial_pool_size_percent,
int max_pool_size_percent,
bool managed)
{
return cuvs::core::translate_exceptions([=] {
// Upstream memory resource needs to be a cuda_memory_resource
auto cuda_mr = rmm::mr::get_current_device_resource();
auto* cuda_mr_casted = dynamic_cast<rmm::mr::cuda_memory_resource*>(cuda_mr);
if (cuda_mr_casted == nullptr) {
throw std::runtime_error("Current memory resource is not a cuda_memory_resource");
}
auto initial_size = rmm::percent_of_free_device_memory(initial_pool_size_percent);
auto max_size = rmm::percent_of_free_device_memory(max_pool_size_percent);
auto mr = std::shared_ptr<rmm::mr::device_memory_resource>();
if (managed) {
mr = std::static_pointer_cast<rmm::mr::device_memory_resource>(
std::make_shared<rmm::mr::managed_memory_resource>());
} else {
mr = std::static_pointer_cast<rmm::mr::device_memory_resource>(
std::make_shared<rmm::mr::cuda_memory_resource>());
}
pool_mr =
rmm::mr::make_owning_wrapper<rmm::mr::pool_memory_resource>(mr, initial_size, max_size);
rmm::mr::set_current_device_resource(pool_mr.get());
});
}
extern "C" cuvsError_t cuvsRMMMemoryResourceReset()
{
return cuvs::core::translate_exceptions([=] {
rmm::mr::set_current_device_resource(nullptr);
pool_mr.reset();
});
}
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()
{
return last_error_text.empty() ? NULL : last_error_text.c_str();
}
extern "C" void cuvsSetLastErrorText(const char* error) { last_error_text = error ? error : ""; }
extern "C" cuvsError_t cuvsVersionGet(uint16_t* major, uint16_t* minor, uint16_t* patch)
{
*major = CUVS_VERSION_MAJOR;
*minor = CUVS_VERSION_MINOR;
*patch = CUVS_VERSION_PATCH;
return CUVS_SUCCESS;
}