-
Notifications
You must be signed in to change notification settings - Fork 17
/
MemoryOperations.hpp
104 lines (99 loc) · 3.34 KB
/
MemoryOperations.hpp
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
// Copyright 2023 Robotec.AI
//
// 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.
#pragma once
#include <functional>
#include <macros/cuda.hpp>
#include <memory/MemoryKind.hpp>
#include <CudaStream.hpp>
/**
* MemoryOperations encapsulate 4 basic memory operations needed to implement dynamic-size array.
* It also provides factory method to create MemoryOperations corresponding to those defined in MemoryKind enum.
* Warning: deallocate, copy and clear can work ONLY on the memory kind returned by allocate.
*/
struct MemoryOperations
{
std::function<void*(size_t)> allocate;
std::function<void(void*)> deallocate;
std::function<void(void*, const void*, size_t)> copy;
std::function<void(void*, int value, size_t)> clear;
/** Returns MemoryOperations for given MemoryKind */
template<MemoryKind memoryKind>
static MemoryOperations get(std::optional<CudaStream::Ptr> maybeStream = std::nullopt)
{
// clang-format off
if constexpr (memoryKind == MemoryKind::HostPageable) {
return {
.allocate = malloc,
.deallocate = free,
.copy = memcpy,
.clear = memset };
}
else if constexpr (memoryKind == MemoryKind::HostPinned) {
return {
.allocate = [](size_t bytes) {
void* ptr = nullptr;
CHECK_CUDA(cudaMallocHost(&ptr, bytes));
return ptr;
},
.deallocate = [](void* ptr) {
CHECK_CUDA(cudaFreeHost(ptr));
},
// Regular memcpy and memset avoid the overhead of cuda[Memcpy|Memset] and achieve higher performance.
.copy = memcpy,
.clear = memset };
}
else if constexpr (memoryKind == MemoryKind::DeviceSync) {
return {
.allocate = [](size_t bytes) {
void* ptr = nullptr;
CHECK_CUDA(cudaMalloc(&ptr, bytes));
return ptr;
},
.deallocate = [](void* ptr) {
CHECK_CUDA(cudaFree(ptr));
},
.copy = [](void* dst, const void* src, size_t bytes) {
CHECK_CUDA(cudaMemcpy(dst, src, bytes, cudaMemcpyDeviceToDevice));
},
.clear = [=](void* dst, int value, size_t bytes) {
CHECK_CUDA(cudaMemset(dst, value, bytes));
}
};
}
else if constexpr (memoryKind == MemoryKind::DeviceAsync) {
auto stream = maybeStream.value();
return {
// Note: capture-by-value to ensure CudaStream lifetime.
.allocate = [=](size_t bytes) {
void* ptr = nullptr;
CHECK_CUDA(cudaMallocAsync(&ptr, bytes, stream->getHandle()));
return ptr;
},
.deallocate = [=](void* ptr) {
CHECK_CUDA(cudaFreeAsync(ptr, stream->getHandle()));
},
.copy = [=](void* dst, const void* src, size_t bytes) {
CHECK_CUDA(cudaMemcpyAsync(dst, src, bytes, cudaMemcpyDeviceToDevice, stream->getHandle()));
},
.clear = [=](void* dst, int value, size_t bytes) {
CHECK_CUDA(cudaMemsetAsync(dst, value, bytes, stream->getHandle()));
}
};
}
else {
static_assert("invalid memory kind passed to MemoryOperations::get()");
}
// clang-format on
}
};