-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathunique_copy.h
More file actions
177 lines (152 loc) · 5.79 KB
/
unique_copy.h
File metadata and controls
177 lines (152 loc) · 5.79 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
//===----------------------------------------------------------------------===//
//
// Part of libcu++, the C++ Standard Library for your entire system,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
#ifndef _CUDA_STD___PSTL_CUDA_UNIQUE_COPY_H
#define _CUDA_STD___PSTL_CUDA_UNIQUE_COPY_H
#include <cuda/std/detail/__config>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#if _CCCL_HAS_BACKEND_CUDA()
_CCCL_DIAG_PUSH
_CCCL_DIAG_SUPPRESS_CLANG("-Wshadow")
_CCCL_DIAG_SUPPRESS_CLANG("-Wunused-local-typedef")
_CCCL_DIAG_SUPPRESS_NVHPC(attribute_requires_external_linkage)
# include <cub/device/device_select.cuh>
_CCCL_DIAG_POP
# include <cuda/__execution/policy.h>
# include <cuda/__memory_pool/device_memory_pool.h>
# include <cuda/__memory_resource/get_memory_resource.h>
# include <cuda/__runtime/api_wrapper.h>
# include <cuda/__stream/get_stream.h>
# include <cuda/__stream/stream_ref.h>
# include <cuda/std/__algorithm/unique_copy.h>
# include <cuda/std/__exception/cuda_error.h>
# include <cuda/std/__exception/exception_macros.h>
# include <cuda/std/__execution/env.h>
# include <cuda/std/__execution/policy.h>
# include <cuda/std/__iterator/incrementable_traits.h>
# include <cuda/std/__iterator/iterator_traits.h>
# include <cuda/std/__iterator/next.h>
# include <cuda/std/__pstl/cuda/temporary_storage.h>
# include <cuda/std/__pstl/dispatch.h>
# include <cuda/std/__type_traits/always_false.h>
# include <cuda/std/__utility/move.h>
# include <cuda/std/cstdint>
# include <cuda/std/__cccl/prologue.h>
_CCCL_BEGIN_NAMESPACE_CUDA_STD_EXECUTION
_CCCL_BEGIN_NAMESPACE_ARCH_DEPENDENT
template <>
struct __pstl_dispatch<__pstl_algorithm::__unique_copy, __execution_backend::__cuda>
{
template <class _Policy, class _InputIterator, class _OutputIterator, class _BinaryPredicate>
[[nodiscard]] _CCCL_HOST_API static _OutputIterator __par_impl(
const _Policy& __policy,
_InputIterator __first,
_InputIterator __last,
_OutputIterator __result,
_BinaryPredicate __pred)
{
// DeviceSelect always uses int64_t
using _OffsetType = ::cuda::std::int64_t;
_OffsetType __ret;
const auto __count = static_cast<_OffsetType>(::cuda::std::distance(__first, __last));
// Determine temporary device storage requirements for DeviceSelect::Unique
size_t __num_bytes = 0;
_CCCL_TRY_CUDA_API(
CUB_NS_QUALIFIER::DeviceSelect::Unique,
"__pstl_cuda_unique_copy: determination of device storage for cub::DeviceSelect::Unique failed",
static_cast<void*>(nullptr),
__num_bytes,
__first,
__result,
static_cast<_OffsetType*>(nullptr),
__count,
__pred,
0);
auto __stream = ::cuda::__call_or(::cuda::get_stream, ::cuda::stream_ref{cudaStreamPerThread}, __policy);
auto __resource = ::cuda::__call_or(
::cuda::mr::get_memory_resource, ::cuda::device_default_memory_pool(__stream.device()), __policy);
{
__temporary_storage<_OffsetType, decltype(__resource)> __storage{__stream, __resource, __num_bytes};
_CCCL_TRY_CUDA_API(
CUB_NS_QUALIFIER::DeviceSelect::Unique,
"__pstl_cuda_unique_copy: kernel launch of cub::DeviceSelect::Unique failed",
__storage.__get_temp_storage(),
__num_bytes,
::cuda::std::move(__first),
__result,
__storage.__get_result_iter(),
__count,
::cuda::std::move(__pred),
__stream.get());
_CCCL_TRY_CUDA_API(
::cudaMemcpyAsync,
"__pstl_cuda_unique_copy: copy of result from device to host failed",
::cuda::std::addressof(__ret),
__storage.__res_,
sizeof(_OffsetType),
cudaMemcpyDefault,
__stream.get());
}
__stream.sync();
return __result + static_cast<iter_difference_t<_OutputIterator>>(__ret);
}
_CCCL_TEMPLATE(class _Policy, class _InputIterator, class _OutputIterator, class _BinaryPredicate)
_CCCL_REQUIRES(__has_forward_traversal<_OutputIterator>)
[[nodiscard]] _CCCL_HOST_API _OutputIterator operator()(
[[maybe_unused]] const _Policy& __policy,
_InputIterator __first,
_InputIterator __last,
_OutputIterator __result,
_BinaryPredicate __pred) const
{
if constexpr (::cuda::std::__has_random_access_traversal<_InputIterator>
&& ::cuda::std::__has_random_access_traversal<_OutputIterator>)
{
try
{
return __par_impl(
__policy,
::cuda::std::move(__first),
::cuda::std::move(__last),
::cuda::std::move(__result),
::cuda::std::move(__pred));
}
catch (const ::cuda::cuda_error& __err)
{
if (__err.status() == ::cudaErrorMemoryAllocation)
{
_CCCL_THROW(::std::bad_alloc);
}
else
{
throw __err;
}
}
}
else
{
static_assert(__always_false_v<_Policy>,
"__pstl_dispatch: CUDA backend of cuda::std::unique_copy requires at least random access "
"iterators");
return ::cuda::std::unique_copy(
::cuda::std::move(__first), ::cuda::std::move(__last), ::cuda::std::move(__result), ::cuda::std::move(__pred));
}
}
};
_CCCL_END_NAMESPACE_ARCH_DEPENDENT
_CCCL_END_NAMESPACE_CUDA_STD_EXECUTION
# include <cuda/std/__cccl/epilogue.h>
#endif // _CCCL_HAS_BACKEND_CUDA()
#endif // _CUDA_STD___PSTL_CUDA_UNIQUE_COPY_H