-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor scalar reductions to use common execution policy (#573)
Refactor scalar reductions to use common execution policy
- Loading branch information
Showing
8 changed files
with
268 additions
and
478 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/cunumeric/execution_policy/reduction/scalar_reduction.cuh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* Copyright 2021-2022 NVIDIA Corporation | ||
* | ||
* 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 "cunumeric/execution_policy/reduction/scalar_reduction.h" | ||
#include "cunumeric/cuda_help.h" | ||
|
||
namespace cunumeric { | ||
namespace scalar_reduction_impl { | ||
|
||
template <class AccessorRD, class Kernel, class LHS, class Tag> | ||
static __global__ void __launch_bounds__(THREADS_PER_BLOCK, MIN_CTAS_PER_SM) | ||
scalar_unary_red_kernel( | ||
size_t volume, size_t iters, AccessorRD out, Kernel kernel, LHS identity, Tag tag) | ||
{ | ||
auto value = identity; | ||
for (size_t idx = 0; idx < iters; idx++) { | ||
const size_t offset = (idx * gridDim.x + blockIdx.x) * blockDim.x + threadIdx.x; | ||
if (offset < volume) { kernel(value, offset, tag); } | ||
} | ||
// Every thread in the thread block must participate in the exchange to get correct results | ||
reduce_output(out, value); | ||
} | ||
|
||
template <typename Buffer, typename RedAcc> | ||
static __global__ void __launch_bounds__(1, 1) copy_kernel(Buffer result, RedAcc out) | ||
{ | ||
out.reduce(0, result.read()); | ||
} | ||
|
||
} // namespace scalar_reduction_impl | ||
|
||
template <class LG_OP, class Tag> | ||
struct ScalarReductionPolicy<VariantKind::GPU, LG_OP, Tag> { | ||
template <class AccessorRD, class LHS, class Kernel> | ||
void __attribute__((visibility("hidden"))) operator()(size_t volume, | ||
AccessorRD& out, | ||
const LHS& identity, | ||
Kernel&& kernel) | ||
{ | ||
auto stream = get_cached_stream(); | ||
|
||
const size_t blocks = (volume + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK; | ||
DeviceScalarReductionBuffer<LG_OP> result(stream); | ||
size_t shmem_size = THREADS_PER_BLOCK / 32 * sizeof(LHS); | ||
|
||
if (blocks >= MAX_REDUCTION_CTAS) { | ||
const size_t iters = (blocks + MAX_REDUCTION_CTAS - 1) / MAX_REDUCTION_CTAS; | ||
scalar_reduction_impl:: | ||
scalar_unary_red_kernel<<<MAX_REDUCTION_CTAS, THREADS_PER_BLOCK, shmem_size, stream>>>( | ||
volume, iters, result, std::forward<Kernel>(kernel), identity, Tag{}); | ||
} else { | ||
scalar_reduction_impl:: | ||
scalar_unary_red_kernel<<<blocks, THREADS_PER_BLOCK, shmem_size, stream>>>( | ||
volume, 1, result, std::forward<Kernel>(kernel), identity, Tag{}); | ||
} | ||
scalar_reduction_impl::copy_kernel<<<1, 1, 0, stream>>>(result, out); | ||
CHECK_CUDA_STREAM(stream); | ||
} | ||
}; | ||
|
||
} // namespace cunumeric |
50 changes: 50 additions & 0 deletions
50
src/cunumeric/execution_policy/reduction/scalar_reduction.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* Copyright 2021-2022 NVIDIA Corporation | ||
* | ||
* 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 "cunumeric/cunumeric.h" | ||
|
||
namespace cunumeric { | ||
|
||
template <VariantKind KIND, class LG_OP, class Tag = void> | ||
struct ScalarReductionPolicy { | ||
// No C++-20 yet. This is just here to illustrate the expected concept | ||
// that all kernels passed to this execution should have. | ||
struct KernelConcept { | ||
// Every operator should take a scalar LHS as the | ||
// target of the reduction and an index represeting the point | ||
// in the iteration space being added into the reduction. | ||
template <class LHS> | ||
void operator()(LHS& lhs, size_t idx) | ||
{ | ||
// LHS <- op[idx] | ||
} | ||
}; | ||
}; | ||
|
||
template <class LG_OP, class Tag> | ||
struct ScalarReductionPolicy<VariantKind::CPU, LG_OP, Tag> { | ||
template <class AccessorRD, class LHS, class Kernel> | ||
void operator()(size_t volume, AccessorRD& out, const LHS& identity, Kernel&& kernel) | ||
{ | ||
auto result = identity; | ||
for (size_t idx = 0; idx < volume; ++idx) { kernel(result, idx, Tag{}); } | ||
out.reduce(0, result); | ||
} | ||
}; | ||
|
||
} // namespace cunumeric |
44 changes: 44 additions & 0 deletions
44
src/cunumeric/execution_policy/reduction/scalar_reduction_omp.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* Copyright 2021-2022 NVIDIA Corporation | ||
* | ||
* 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 "cunumeric/execution_policy/reduction/scalar_reduction.h" | ||
#include "cunumeric/omp_help.h" | ||
|
||
#include <omp.h> | ||
|
||
namespace cunumeric { | ||
|
||
template <class LG_OP, class Tag> | ||
struct ScalarReductionPolicy<VariantKind::OMP, LG_OP, Tag> { | ||
template <class AccessorRD, class LHS, class Kernel> | ||
void operator()(size_t volume, AccessorRD& out, const LHS& identity, Kernel&& kernel) | ||
{ | ||
const auto max_threads = omp_get_max_threads(); | ||
ThreadLocalStorage<LHS> locals(max_threads); | ||
for (auto idx = 0; idx < max_threads; ++idx) locals[idx] = identity; | ||
#pragma omp parallel | ||
{ | ||
const int tid = omp_get_thread_num(); | ||
#pragma omp for schedule(static) | ||
for (size_t idx = 0; idx < volume; ++idx) { kernel(locals[tid], idx, Tag{}); } | ||
} | ||
for (auto idx = 0; idx < max_threads; ++idx) out.reduce(0, locals[idx]); | ||
} | ||
}; | ||
|
||
} // namespace cunumeric |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.