|
| 1 | +/* |
| 2 | +* Copyright 2021-2022 NVIDIA Corporation |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +/** |
| 18 | + * \file |
| 19 | + * Utilities for CUDA dynamic parallelism. |
| 20 | + */ |
| 21 | + |
| 22 | +#pragma once |
| 23 | + |
| 24 | +#include <cub/config.cuh> |
| 25 | +#include <cub/detail/detect_cuda_runtime.cuh> |
| 26 | + |
| 27 | +#include <nv/target> |
| 28 | + |
| 29 | +/** |
| 30 | + * \def THRUST_CDP_DISPATCH |
| 31 | + * |
| 32 | + * If CUDA Dynamic Parallelism / CUDA Nested Parallelism is available, always |
| 33 | + * run the parallel implementation. Otherwise, run the parallel implementation |
| 34 | + * when called from the host, and fallback to the sequential implementation on |
| 35 | + * the device. |
| 36 | + * |
| 37 | + * `par_impl` and `seq_impl` are blocks of C++ statements enclosed in |
| 38 | + * parentheses, similar to NV_IF_TARGET blocks: |
| 39 | + * |
| 40 | + * \code |
| 41 | + * THRUST_CDP_DISPATCH((launch_parallel_kernel();), (run_serial_impl();)); |
| 42 | + * \endcode |
| 43 | + */ |
| 44 | + |
| 45 | +#ifdef THRUST_RDC_ENABLED |
| 46 | + |
| 47 | +// seq_impl unused. |
| 48 | +#define THRUST_CDP_DISPATCH(par_impl, seq_impl) \ |
| 49 | + NV_IF_TARGET(NV_ANY_TARGET, par_impl) |
| 50 | + |
| 51 | +#else // THRUST_RDC_ENABLED |
| 52 | + |
| 53 | +// Special case for NVCC -- need to inform the device path about the kernels |
| 54 | +// that are launched from the host path. |
| 55 | +#if defined(__CUDACC__) && defined(__CUDA_ARCH__) |
| 56 | + |
| 57 | +// Device-side launch not supported, fallback to sequential in device code. |
| 58 | +#define THRUST_CDP_DISPATCH(par_impl, seq_impl) \ |
| 59 | + if (false) \ |
| 60 | + { /* Without this, the device pass won't compile any kernels. */ \ |
| 61 | + NV_IF_TARGET(NV_ANY_TARGET, par_impl); \ |
| 62 | + } \ |
| 63 | + NV_IF_TARGET(NV_IS_HOST, par_impl, seq_impl) |
| 64 | + |
| 65 | +#else // NVCC device pass |
| 66 | + |
| 67 | +#define THRUST_CDP_DISPATCH(par_impl, seq_impl) \ |
| 68 | + NV_IF_TARGET(NV_IS_HOST, par_impl, seq_impl) |
| 69 | + |
| 70 | +#endif // NVCC device pass |
| 71 | + |
| 72 | +#endif // THRUST_RDC_ENABLED |
0 commit comments