Repository navigation
Expand file tree
/
Copy pathToCudf.cpp
More file actions
357 lines (314 loc) · 12.8 KB
/
Copy pathToCudf.cpp
File metadata and controls
357 lines (314 loc) · 12.8 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* 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 "velox/experimental/cudf/exec/CudfConversion.h"
#include "velox/experimental/cudf/exec/CudfFilterProject.h"
#include "velox/experimental/cudf/exec/CudfHashAggregation.h"
#include "velox/experimental/cudf/exec/CudfHashJoin.h"
#include "velox/experimental/cudf/exec/CudfLimit.h"
#include "velox/experimental/cudf/exec/CudfLocalPartition.h"
#include "velox/experimental/cudf/exec/CudfOrderBy.h"
#include "velox/experimental/cudf/exec/ExpressionEvaluator.h"
#include "velox/experimental/cudf/exec/ToCudf.h"
#include "velox/experimental/cudf/exec/Utilities.h"
#include "velox/exec/Driver.h"
#include "velox/exec/FilterProject.h"
#include "velox/exec/HashAggregation.h"
#include "velox/exec/HashBuild.h"
#include "velox/exec/HashProbe.h"
#include "velox/exec/Limit.h"
#include "velox/exec/Operator.h"
#include "velox/exec/OrderBy.h"
#include "velox/exec/TableScan.h"
#include <cudf/detail/nvtx/ranges.hpp>
#include <cuda.h>
#include <iostream>
DEFINE_bool(velox_cudf_enabled, true, "Enable cuDF-Velox acceleration");
DEFINE_string(velox_cudf_memory_resource, "async", "Memory resource for cuDF");
DEFINE_bool(velox_cudf_debug, false, "Enable debug printing");
DEFINE_bool(velox_cudf_table_scan, true, "Enable cuDF table scan");
namespace facebook::velox::cudf_velox {
namespace {
template <class... Deriveds, class Base>
bool isAnyOf(const Base* p) {
return ((dynamic_cast<const Deriveds*>(p) != nullptr) || ...);
}
} // namespace
bool CompileState::compile() {
auto operators = driver_.operators();
if (FLAGS_velox_cudf_debug) {
std::cout << "Operators before adapting for cuDF: count ["
<< operators.size() << "]" << std::endl;
for (auto& op : operators) {
std::cout << " Operator: ID " << op->operatorId() << ": "
<< op->toString() << std::endl;
}
}
// Make sure operator states are initialized. We will need to inspect some of
// them during the transformation.
driver_.initializeOperators();
bool replacementsMade = false;
auto ctx = driver_.driverCtx();
// Get plan node by id lookup.
auto getPlanNode = [&](const core::PlanNodeId& id) {
auto& nodes = driverFactory_.planNodes;
auto it =
std::find_if(nodes.cbegin(), nodes.cend(), [&id](const auto& node) {
return node->id() == id;
});
if (it != nodes.end()) {
return *it;
}
VELOX_CHECK(driverFactory_.consumerNode->id() == id);
return driverFactory_.consumerNode;
};
const bool isParquetConnectorRegistered =
facebook::velox::connector::getAllConnectors().count("test-parquet") > 0;
auto isTableScanSupported =
[isParquetConnectorRegistered](const exec::Operator* op) {
return isAnyOf<exec::TableScan>(op) && isParquetConnectorRegistered &&
cudfTableScanEnabled();
};
auto isFilterProjectSupported = [](const exec::Operator* op) {
if (auto filterProjectOp = dynamic_cast<const exec::FilterProject*>(op)) {
auto info = filterProjectOp->exprsAndProjection();
return ExpressionEvaluator::canBeEvaluated(info.exprs->exprs());
}
return false;
};
auto isJoinSupported = [getPlanNode](const exec::Operator* op) {
if (!isAnyOf<exec::HashBuild, exec::HashProbe>(op)) {
return false;
}
auto planNode = std::dynamic_pointer_cast<const core::HashJoinNode>(
getPlanNode(op->planNodeId()));
if (!planNode) {
return false;
}
if (!CudfHashJoinProbe::isSupportedJoinType(planNode->joinType())) {
return false;
}
return true;
};
auto isSupportedGpuOperator =
[isFilterProjectSupported, isJoinSupported, isTableScanSupported](
const exec::Operator* op) {
return isAnyOf<
exec::OrderBy,
exec::HashAggregation,
exec::Limit,
exec::LocalPartition,
exec::LocalExchange>(op) ||
isFilterProjectSupported(op) || isJoinSupported(op) ||
isTableScanSupported(op);
};
std::vector<bool> isSupportedGpuOperators(operators.size());
std::transform(
operators.begin(),
operators.end(),
isSupportedGpuOperators.begin(),
isSupportedGpuOperator);
auto acceptsGpuInput = [isFilterProjectSupported,
isJoinSupported](const exec::Operator* op) {
return isAnyOf<
exec::OrderBy,
exec::HashAggregation,
exec::Limit,
exec::LocalPartition>(op) ||
isFilterProjectSupported(op) || isJoinSupported(op);
};
auto producesGpuOutput = [isFilterProjectSupported,
isJoinSupported,
isTableScanSupported](const exec::Operator* op) {
return isAnyOf<
exec::OrderBy,
exec::HashAggregation,
exec::Limit,
exec::LocalExchange>(op) ||
isFilterProjectSupported(op) ||
(isAnyOf<exec::HashProbe>(op) && isJoinSupported(op)) ||
(isTableScanSupported(op));
};
int32_t operatorsOffset = 0;
for (int32_t operatorIndex = 0; operatorIndex < operators.size();
++operatorIndex) {
std::vector<std::unique_ptr<exec::Operator>> replaceOp;
exec::Operator* oper = operators[operatorIndex];
auto replacingOperatorIndex = operatorIndex + operatorsOffset;
VELOX_CHECK(oper);
const bool previousOperatorIsNotGpu =
(operatorIndex > 0 and !isSupportedGpuOperators[operatorIndex - 1]);
const bool nextOperatorIsNotGpu =
(operatorIndex < operators.size() - 1 and
!isSupportedGpuOperators[operatorIndex + 1]);
const bool isLastOperatorOfTask =
driverFactory_.outputDriver and operatorIndex == operators.size() - 1;
auto id = oper->operatorId();
if (previousOperatorIsNotGpu and acceptsGpuInput(oper)) {
auto planNode = getPlanNode(oper->planNodeId());
replaceOp.push_back(std::make_unique<CudfFromVelox>(
id, planNode->outputType(), ctx, planNode->id() + "-from-velox"));
replaceOp.back()->initialize();
}
// This is used to denote if the current operator is kept or replaced.
auto keepOperator = 0;
// TableScan
if (isTableScanSupported(oper)) {
auto planNode = std::dynamic_pointer_cast<const core::TableScanNode>(
getPlanNode(oper->planNodeId()));
VELOX_CHECK(planNode != nullptr);
keepOperator = 1;
} else if (isJoinSupported(oper)) {
if (auto joinBuildOp = dynamic_cast<exec::HashBuild*>(oper)) {
auto planNode = std::dynamic_pointer_cast<const core::HashJoinNode>(
getPlanNode(joinBuildOp->planNodeId()));
VELOX_CHECK(planNode != nullptr);
// From-Velox (optional)
replaceOp.push_back(
std::make_unique<CudfHashJoinBuild>(id, ctx, planNode));
replaceOp.back()->initialize();
} else if (auto joinProbeOp = dynamic_cast<exec::HashProbe*>(oper)) {
auto planNode = std::dynamic_pointer_cast<const core::HashJoinNode>(
getPlanNode(joinProbeOp->planNodeId()));
VELOX_CHECK(planNode != nullptr);
// From-Velox (optional)
replaceOp.push_back(
std::make_unique<CudfHashJoinProbe>(id, ctx, planNode));
replaceOp.back()->initialize();
// To-Velox (optional)
}
} else if (auto orderByOp = dynamic_cast<exec::OrderBy*>(oper)) {
auto id = orderByOp->operatorId();
auto planNode = std::dynamic_pointer_cast<const core::OrderByNode>(
getPlanNode(orderByOp->planNodeId()));
VELOX_CHECK(planNode != nullptr);
replaceOp.push_back(std::make_unique<CudfOrderBy>(id, ctx, planNode));
replaceOp.back()->initialize();
} else if (auto hashAggOp = dynamic_cast<exec::HashAggregation*>(oper)) {
auto planNode = std::dynamic_pointer_cast<const core::AggregationNode>(
getPlanNode(hashAggOp->planNodeId()));
VELOX_CHECK(planNode != nullptr);
replaceOp.push_back(
std::make_unique<CudfHashAggregation>(id, ctx, planNode));
replaceOp.back()->initialize();
} else if (isFilterProjectSupported(oper)) {
auto filterProjectOp = dynamic_cast<exec::FilterProject*>(oper);
auto info = filterProjectOp->exprsAndProjection();
auto& idProjections = filterProjectOp->identityProjections();
auto projectPlanNode = std::dynamic_pointer_cast<const core::ProjectNode>(
getPlanNode(filterProjectOp->planNodeId()));
auto filterPlanNode = std::dynamic_pointer_cast<const core::FilterNode>(
getPlanNode(filterProjectOp->planNodeId()));
// If filter only, filter node only exists.
// If project only, or filter and project, project node only exists.
VELOX_CHECK(projectPlanNode != nullptr or filterPlanNode != nullptr);
replaceOp.push_back(std::make_unique<CudfFilterProject>(
id, ctx, info, idProjections, filterPlanNode, projectPlanNode));
replaceOp.back()->initialize();
} else if (auto limitOp = dynamic_cast<exec::Limit*>(oper)) {
auto planNode = std::dynamic_pointer_cast<const core::LimitNode>(
getPlanNode(limitOp->planNodeId()));
VELOX_CHECK(planNode != nullptr);
replaceOp.push_back(std::make_unique<CudfLimit>(id, ctx, planNode));
replaceOp.back()->initialize();
} else if (
auto localPartitionOp = dynamic_cast<exec::LocalPartition*>(oper)) {
auto planNode = std::dynamic_pointer_cast<const core::LocalPartitionNode>(
getPlanNode(localPartitionOp->planNodeId()));
VELOX_CHECK(planNode != nullptr);
replaceOp.push_back(
std::make_unique<CudfLocalPartition>(id, ctx, planNode));
replaceOp.back()->initialize();
}
if (producesGpuOutput(oper) and
(nextOperatorIsNotGpu or isLastOperatorOfTask)) {
auto planNode = getPlanNode(oper->planNodeId());
replaceOp.push_back(std::make_unique<CudfToVelox>(
id, planNode->outputType(), ctx, planNode->id() + "-to-velox"));
replaceOp.back()->initialize();
}
if (not replaceOp.empty()) {
operatorsOffset += replaceOp.size() - 1 + keepOperator;
[[maybe_unused]] auto replaced = driverFactory_.replaceOperators(
driver_,
replacingOperatorIndex + keepOperator,
replacingOperatorIndex + 1,
std::move(replaceOp));
replacementsMade = true;
}
}
if (FLAGS_velox_cudf_debug) {
operators = driver_.operators();
std::cout << "Operators after adapting for cuDF: count ["
<< operators.size() << "]" << std::endl;
for (auto& op : operators) {
std::cout << " Operator: ID " << op->operatorId() << ": "
<< op->toString() << std::endl;
}
}
return replacementsMade;
}
struct CudfDriverAdapter {
std::shared_ptr<rmm::mr::device_memory_resource> mr_;
CudfDriverAdapter(std::shared_ptr<rmm::mr::device_memory_resource> mr)
: mr_(mr) {}
// Call operator needed by DriverAdapter
bool operator()(const exec::DriverFactory& factory, exec::Driver& driver) {
auto state = CompileState(factory, driver);
auto res = state.compile();
return res;
}
};
static bool isCudfRegistered = false;
void registerCudf(const CudfOptions& options) {
if (cudfIsRegistered()) {
return;
}
if (!options.cudfEnabled) {
return;
}
CUDF_FUNC_RANGE();
cudaFree(nullptr); // Initialize CUDA context at startup
const std::string mrMode = options.cudfMemoryResource;
auto mr = cudf_velox::createMemoryResource(mrMode);
cudf::set_current_device_resource(mr.get());
exec::Operator::registerOperator(
std::make_unique<CudfHashJoinBridgeTranslator>());
CudfDriverAdapter cda{mr};
exec::DriverAdapter cudfAdapter{kCudfAdapterName, {}, cda};
exec::DriverFactory::registerAdapter(cudfAdapter);
isCudfRegistered = true;
}
void unregisterCudf() {
exec::DriverFactory::adapters.erase(
std::remove_if(
exec::DriverFactory::adapters.begin(),
exec::DriverFactory::adapters.end(),
[](const exec::DriverAdapter& adapter) {
return adapter.label == kCudfAdapterName;
}),
exec::DriverFactory::adapters.end());
isCudfRegistered = false;
}
bool cudfIsRegistered() {
return isCudfRegistered;
}
bool cudfDebugEnabled() {
return FLAGS_velox_cudf_debug;
}
bool cudfTableScanEnabled() {
return FLAGS_velox_cudf_table_scan;
}
} // namespace facebook::velox::cudf_velox