Skip to content

Commit 0be7876

Browse files
committed
Fix free() error in devArrMatch
1 parent 4273f42 commit 0be7876

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

cpp/test/test_utils.h

+24-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <raft/cudart_utils.h>
2020
#include <iostream>
2121
#include <memory>
22+
#include <vector>
2223
#include <raft/cuda_utils.cuh>
2324

2425
namespace raft {
@@ -77,14 +78,14 @@ template <typename T, typename L>
7778
testing::AssertionResult devArrMatch(const T *expected, const T *actual,
7879
size_t size, L eq_compare,
7980
cudaStream_t stream = 0) {
80-
std::shared_ptr<T> exp_h(new T[size]);
81-
std::shared_ptr<T> act_h(new T[size]);
82-
raft::update_host<T>(exp_h.get(), expected, size, stream);
83-
raft::update_host<T>(act_h.get(), actual, size, stream);
81+
std::vector<T> exp_h(size);
82+
std::vector<T> act_h(size);
83+
raft::update_host<T>(exp_h.data(), expected, size, stream);
84+
raft::update_host<T>(act_h.data(), actual, size, stream);
8485
CUDA_CHECK(cudaStreamSynchronize(stream));
8586
for (size_t i(0); i < size; ++i) {
86-
auto exp = exp_h.get()[i];
87-
auto act = act_h.get()[i];
87+
auto exp = exp_h[i];
88+
auto act = act_h[i];
8889
if (!eq_compare(exp, act)) {
8990
return testing::AssertionFailure()
9091
<< "actual=" << act << " != expected=" << exp << " @" << i;
@@ -96,11 +97,11 @@ testing::AssertionResult devArrMatch(const T *expected, const T *actual,
9697
template <typename T, typename L>
9798
testing::AssertionResult devArrMatch(T expected, const T *actual, size_t size,
9899
L eq_compare, cudaStream_t stream = 0) {
99-
std::shared_ptr<T> act_h(new T[size]);
100-
raft::update_host<T>(act_h.get(), actual, size, stream);
100+
std::vector<T> act_h(size);
101+
raft::update_host<T>(act_h.data(), actual, size, stream);
101102
CUDA_CHECK(cudaStreamSynchronize(stream));
102103
for (size_t i(0); i < size; ++i) {
103-
auto act = act_h.get()[i];
104+
auto act = act_h[i];
104105
if (!eq_compare(expected, act)) {
105106
return testing::AssertionFailure()
106107
<< "actual=" << act << " != expected=" << expected << " @" << i;
@@ -114,16 +115,16 @@ testing::AssertionResult devArrMatch(const T *expected, const T *actual,
114115
size_t rows, size_t cols, L eq_compare,
115116
cudaStream_t stream = 0) {
116117
size_t size = rows * cols;
117-
std::shared_ptr<T> exp_h(new T[size]);
118-
std::shared_ptr<T> act_h(new T[size]);
119-
raft::update_host<T>(exp_h.get(), expected, size, stream);
120-
raft::update_host<T>(act_h.get(), actual, size, stream);
118+
std::vector<T> exp_h(size);
119+
std::vector<T> act_h(size);
120+
raft::update_host<T>(exp_h.data(), expected, size, stream);
121+
raft::update_host<T>(act_h.data(), actual, size, stream);
121122
CUDA_CHECK(cudaStreamSynchronize(stream));
122123
for (size_t i(0); i < rows; ++i) {
123124
for (size_t j(0); j < cols; ++j) {
124125
auto idx = i * cols + j; // row major assumption!
125-
auto exp = exp_h.get()[idx];
126-
auto act = act_h.get()[idx];
126+
auto exp = exp_h[idx];
127+
auto act = act_h[idx];
127128
if (!eq_compare(exp, act)) {
128129
return testing::AssertionFailure()
129130
<< "actual=" << act << " != expected=" << exp << " @" << i << ","
@@ -139,13 +140,13 @@ testing::AssertionResult devArrMatch(T expected, const T *actual, size_t rows,
139140
size_t cols, L eq_compare,
140141
cudaStream_t stream = 0) {
141142
size_t size = rows * cols;
142-
std::shared_ptr<T> act_h(new T[size]);
143-
raft::update_host<T>(act_h.get(), actual, size, stream);
143+
std::vector<T> act_h(size);
144+
raft::update_host<T>(act_h.data(), actual, size, stream);
144145
CUDA_CHECK(cudaStreamSynchronize(stream));
145146
for (size_t i(0); i < rows; ++i) {
146147
for (size_t j(0); j < cols; ++j) {
147148
auto idx = i * cols + j; // row major assumption!
148-
auto act = act_h.get()[idx];
149+
auto act = act_h[idx];
149150
if (!eq_compare(expected, act)) {
150151
return testing::AssertionFailure()
151152
<< "actual=" << act << " != expected=" << expected << " @" << i
@@ -171,14 +172,14 @@ template <typename T, typename L>
171172
testing::AssertionResult devArrMatchHost(const T *expected_h, const T *actual_d,
172173
size_t size, L eq_compare,
173174
cudaStream_t stream = 0) {
174-
std::shared_ptr<T> act_h(new T[size]);
175+
std::vector<T> act_h(size);
175176
raft::update_host<T>(act_h.get(), actual_d, size, stream);
176177
CUDA_CHECK(cudaStreamSynchronize(stream));
177178
bool ok = true;
178179
auto fail = testing::AssertionFailure();
179180
for (size_t i(0); i < size; ++i) {
180181
auto exp = expected_h[i];
181-
auto act = act_h.get()[i];
182+
auto act = act_h[i];
182183
if (!eq_compare(exp, act)) {
183184
ok = false;
184185
fail << "actual=" << act << " != expected=" << exp << " @" << i << "; ";
@@ -203,14 +204,14 @@ testing::AssertionResult diagonalMatch(T expected, const T *actual, size_t rows,
203204
size_t cols, L eq_compare,
204205
cudaStream_t stream = 0) {
205206
size_t size = rows * cols;
206-
std::shared_ptr<T> act_h(new T[size]);
207-
raft::update_host<T>(act_h.get(), actual, size, stream);
207+
std::vector<T> act_h(size);
208+
raft::update_host<T>(act_h.data(), actual, size, stream);
208209
CUDA_CHECK(cudaStreamSynchronize(stream));
209210
for (size_t i(0); i < rows; ++i) {
210211
for (size_t j(0); j < cols; ++j) {
211212
if (i != j) continue;
212213
auto idx = i * cols + j; // row major assumption!
213-
auto act = act_h.get()[idx];
214+
auto act = act_h[idx];
214215
if (!eq_compare(expected, act)) {
215216
return testing::AssertionFailure()
216217
<< "actual=" << act << " != expected=" << expected << " @" << i

0 commit comments

Comments
 (0)