-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathtest_utils.hpp
More file actions
109 lines (92 loc) · 2.87 KB
/
test_utils.hpp
File metadata and controls
109 lines (92 loc) · 2.87 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
// Copyright 2026 TIER IV, Inc.
//
// 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 <cuda_runtime_api.h>
#include <gtest/gtest.h>
#include <cstddef>
#include <stdexcept>
#include <vector>
namespace autoware::tensorrt_plugins::test
{
class CudaStreamGuard
{
public:
CudaStreamGuard()
{
const cudaError_t status = cudaStreamCreate(&stream_);
if (status != cudaSuccess) {
throw std::runtime_error(cudaGetErrorString(status));
}
}
~CudaStreamGuard()
{
if (stream_ != nullptr) {
cudaStreamDestroy(stream_);
}
}
CudaStreamGuard(const CudaStreamGuard &) = delete;
CudaStreamGuard & operator=(const CudaStreamGuard &) = delete;
CudaStreamGuard(CudaStreamGuard &&) = delete;
CudaStreamGuard & operator=(CudaStreamGuard &&) = delete;
[[nodiscard]] cudaStream_t get() const { return stream_; }
private:
cudaStream_t stream_{nullptr};
};
template <typename T>
class DeviceBuffer
{
public:
explicit DeviceBuffer(std::size_t element_count) : element_count_(element_count)
{
const cudaError_t status = cudaMalloc(&data_, sizeof(T) * element_count_);
if (status != cudaSuccess) {
throw std::runtime_error(cudaGetErrorString(status));
}
}
~DeviceBuffer()
{
if (data_ != nullptr) {
cudaFree(data_);
}
}
DeviceBuffer(const DeviceBuffer &) = delete;
DeviceBuffer & operator=(const DeviceBuffer &) = delete;
DeviceBuffer(DeviceBuffer &&) = delete;
DeviceBuffer & operator=(DeviceBuffer &&) = delete;
[[nodiscard]] T * get() const { return static_cast<T *>(data_); }
private:
void * data_{nullptr};
std::size_t element_count_{0U};
};
template <typename T>
void copy_to_device(T * device_ptr, const std::vector<T> & host_values)
{
const cudaError_t status = cudaMemcpy(
device_ptr, host_values.data(), sizeof(T) * host_values.size(), cudaMemcpyHostToDevice);
if (status != cudaSuccess) {
throw std::runtime_error(cudaGetErrorString(status));
}
}
template <typename T>
std::vector<T> copy_to_host(const T * device_ptr, const std::size_t element_count)
{
std::vector<T> host_values(element_count);
const cudaError_t status =
cudaMemcpy(host_values.data(), device_ptr, sizeof(T) * element_count, cudaMemcpyDeviceToHost);
if (status != cudaSuccess) {
throw std::runtime_error(cudaGetErrorString(status));
}
return host_values;
}
} // namespace autoware::tensorrt_plugins::test