-
Notifications
You must be signed in to change notification settings - Fork 448
/
BumpAllocatorTest.cpp
318 lines (258 loc) · 9.78 KB
/
BumpAllocatorTest.cpp
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright 2022 HEAVY.AI, 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.
*/
#include "TestHelpers.h"
#include <Catalog/Catalog.h>
#include <CudaMgr/CudaMgr.h>
#include <QueryEngine/ResultSet.h>
#include <QueryRunner/QueryRunner.h>
#include <gtest/gtest.h>
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
using QR = QueryRunner::QueryRunner;
extern bool g_allow_cpu_retry;
extern size_t g_max_memory_allocation_size;
extern size_t g_min_memory_allocation_size;
extern bool g_enable_bump_allocator;
namespace {
size_t g_num_gpus{0};
bool g_keep_data{false};
bool skip_tests(const ExecutorDeviceType device_type) {
#ifdef HAVE_CUDA
return device_type == ExecutorDeviceType::GPU && !QR::get()->gpusPresent();
#else
return device_type == ExecutorDeviceType::GPU;
#endif
}
#define SKIP_NO_GPU() \
if (skip_tests(ExecutorDeviceType::GPU)) { \
LOG(WARNING) << "GPU not available, skipping GPU tests"; \
}
std::shared_ptr<ResultSet> run_multiple_agg(const std::string& query_str,
const ExecutorDeviceType device_type) {
return QR::get()->runSQL(query_str, device_type, true, true);
}
// Note: This assumes a homogenous GPU setup
struct GpuInfo {
size_t global_memory_size;
size_t num_gpus;
};
GpuInfo get_gpu_info() {
auto cat = QR::get()->getCatalog();
CHECK(cat);
auto& data_mgr = cat->getDataMgr();
auto cuda_mgr = data_mgr.getCudaMgr();
CHECK(cuda_mgr);
const auto device_props_vec = cuda_mgr->getAllDeviceProperties();
CHECK_GE(device_props_vec.size(), size_t(1));
return GpuInfo{device_props_vec.front().globalMem, device_props_vec.size()};
}
bool setup() {
// Only initialize the QueryRunner once. We will reset the system catalog using the
// resetWithParameters method as needed for each test.
QR::init(BASE_PATH, /*udf_filename=*/"", /*max_gpu_mem=*/1000000000);
if (!QR::get()->gpusPresent()) {
LOG(WARNING) << "No GPUs detected. Skipping all Bump Allocator tests.";
return false;
}
const auto gpu_info = get_gpu_info();
g_num_gpus = gpu_info.num_gpus; // Using the global to pass into tests
return true;
}
} // namespace
class LowGpuBufferMemory : public ::testing::Test {
public:
void SetUp() override {
const std::string drop_table_stmt{"DROP TABLE IF EXISTS test;"};
QR::get()->runDDLStatement(drop_table_stmt);
const std::string create_table_stmt{
"CREATE TABLE test (x INT, y BIGINT) WITH (FRAGMENT_SIZE=32);"};
QR::get()->runDDLStatement(create_table_stmt);
// Insert enough data to just barely overflow
for (size_t i = 0; i < 64 * g_num_gpus; i++) {
const std::string insert_stmt{"INSERT INTO test VALUES (" + std::to_string(i) +
", " + std::to_string(i) + ");"};
run_multiple_agg(insert_stmt, ExecutorDeviceType::CPU);
}
// Min memory allocation size set to 2GB to guarantee OOM during allocation
min_mem_allocation_state_ = g_min_memory_allocation_size;
g_min_memory_allocation_size = 2000000000;
// CPU retry off to disable automatic punt to CPU
allow_cpu_retry_state_ = g_allow_cpu_retry;
g_allow_cpu_retry = false;
}
void TearDown() override {
if (!g_keep_data) {
const std::string drop_table_stmt{"DROP TABLE IF EXISTS test;"};
QR::get()->runDDLStatement(drop_table_stmt);
}
g_min_memory_allocation_size = min_mem_allocation_state_;
g_allow_cpu_retry = allow_cpu_retry_state_;
}
private:
size_t min_mem_allocation_state_;
bool allow_cpu_retry_state_;
};
TEST_F(LowGpuBufferMemory, CPUMode) {
// Baseline correctness
auto result_rows =
run_multiple_agg("SELECT x FROM test WHERE x < 500;", ExecutorDeviceType::CPU);
ASSERT_EQ(result_rows->rowCount(), size_t(64 * g_num_gpus));
}
TEST_F(LowGpuBufferMemory, OutOfMemory) {
SKIP_NO_GPU();
try {
run_multiple_agg("SELECT x FROM test WHERE x < 500;", ExecutorDeviceType::GPU);
ASSERT_TRUE(false) << "Expected query to throw exception";
} catch (const std::exception& e) {
ASSERT_EQ(
std::string{"Query ran out of GPU memory, unable to automatically retry on CPU"},
std::string(e.what()));
}
}
constexpr size_t row_count_per_gpu = 64;
class LowGpuBufferMemoryCpuRetry : public ::testing::Test {
public:
void SetUp() override {
const std::string drop_table_stmt{"DROP TABLE IF EXISTS test;"};
QR::get()->runDDLStatement(drop_table_stmt);
const std::string create_table_stmt{
"CREATE TABLE test (x INT, y BIGINT) WITH (FRAGMENT_SIZE=32);"};
QR::get()->runDDLStatement(create_table_stmt);
// Insert enough data to exceed the max buffer entry guess and force a pre-flight CPU
// count
for (size_t i = 0; i < row_count_per_gpu * g_num_gpus; i++) {
const std::string insert_stmt{"INSERT INTO test VALUES (" + std::to_string(i) +
", " + std::to_string(i) + ");"};
run_multiple_agg(insert_stmt, ExecutorDeviceType::CPU);
}
// Min memory allocation size set to 2GB to guarantee OOM during allocation
min_mem_allocation_state_ = g_min_memory_allocation_size;
g_min_memory_allocation_size = 2000000000;
// allow CPU retry on
allow_cpu_retry_state_ = g_allow_cpu_retry;
g_allow_cpu_retry = true;
}
void TearDown() override {
if (!g_keep_data) {
const std::string drop_table_stmt{"DROP TABLE IF EXISTS test;"};
QR::get()->runDDLStatement(drop_table_stmt);
}
g_min_memory_allocation_size = min_mem_allocation_state_;
g_allow_cpu_retry = allow_cpu_retry_state_;
}
private:
size_t min_mem_allocation_state_;
bool allow_cpu_retry_state_;
};
TEST_F(LowGpuBufferMemoryCpuRetry, OOMRetryOnCPU) {
SKIP_NO_GPU();
try {
auto result_rows =
run_multiple_agg("SELECT x FROM test WHERE x < 500;", ExecutorDeviceType::GPU);
ASSERT_EQ(result_rows->rowCount(), size_t(row_count_per_gpu * g_num_gpus));
} catch (const std::exception& e) {
ASSERT_TRUE(false) << "Expected query to not throw exception. Query threw: "
<< e.what();
}
}
class MediumGpuBufferMemory : public ::testing::Test {
public:
void SetUp() override {
const std::string drop_table_stmt{"DROP TABLE IF EXISTS test;"};
QR::get()->runDDLStatement(drop_table_stmt);
const std::string create_table_stmt{
"CREATE TABLE test (x INT, y BIGINT) WITH (FRAGMENT_SIZE=32);"};
QR::get()->runDDLStatement(create_table_stmt);
// Insert enough data to just barely overflow
for (size_t i = 0; i < 64 * g_num_gpus; i++) {
const std::string insert_stmt{"INSERT INTO test VALUES (" + std::to_string(i) +
", " + std::to_string(i) + ");"};
run_multiple_agg(insert_stmt, ExecutorDeviceType::CPU);
}
max_mem_allocation_state_ = g_max_memory_allocation_size;
g_max_memory_allocation_size = 512;
// CPU retry off to disable automatic punt to CPU
allow_cpu_retry_state_ = g_allow_cpu_retry;
g_allow_cpu_retry = false;
}
void TearDown() override {
if (!g_keep_data) {
const std::string drop_table_stmt{"DROP TABLE IF EXISTS test;"};
QR::get()->runDDLStatement(drop_table_stmt);
}
g_max_memory_allocation_size = max_mem_allocation_state_;
g_allow_cpu_retry = allow_cpu_retry_state_;
}
private:
size_t max_mem_allocation_state_;
bool allow_cpu_retry_state_;
};
TEST_F(MediumGpuBufferMemory, OutOfSlots) {
SKIP_NO_GPU();
try {
run_multiple_agg("SELECT x FROM test WHERE x < 5000;", ExecutorDeviceType::GPU);
ASSERT_TRUE(false) << "Expected query to throw exception";
} catch (const std::exception& e) {
ASSERT_EQ(std::string(e.what()),
std::string{"Ran out of slots in the query output buffer"});
}
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
namespace po = boost::program_options;
po::options_description desc("Options");
// these two are here to allow passing correctly google testing parameters
desc.add_options()("gtest_list_tests", "list all tests");
desc.add_options()("gtest_filter", "filters tests, use --help for details");
desc.add_options()("keep-data",
"Don't drop tables at the end of the tests. Note that individual "
"tests may still create and drop tables. Use in combination with "
"--gtest_filter to preserve tables for a specific test group.");
desc.add_options()(
"test-help",
"Print all BumpAllocatorTest specific options (for gtest options use `--help`).");
logger::LogOptions log_options(argv[0]);
log_options.max_files_ = 0; // stderr only by default
log_options.set_base_path(BASE_PATH);
desc.add(log_options.get_options());
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
if (vm.count("test-help")) {
std::cout << "Usage: BumpAllocatorTest" << std::endl << std::endl;
std::cout << desc << std::endl;
return 0;
}
if (vm.count("keep-data")) {
g_keep_data = true;
}
logger::init(log_options);
g_enable_bump_allocator = true;
if (!setup()) {
// No GPUs detected, bypass the test
QR::reset();
return 0;
}
int err{0};
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
QR::reset();
return err;
}