60 template <
typename Functor>
63 Evaluator(Functor func,
Range range, int32_t n_threads, int32_t device_idx)
64 : func_(func), range_{std::move(range)}, n_threads_{n_threads}, device_{device_idx} {}
72 template <
typename... HDV>
73 void Eval(HDV... vectors)
const {
74 bool on_device = device_ >= 0;
77 LaunchCUDA(func_, vectors...);
79 LaunchCPU(func_, vectors...);
87 auto span = _vec->DeviceSpan();
92 auto span = _vec->ConstDeviceSpan();
98 return Span<T> {_vec->HostPointer(),
99 static_cast<typename Span<T>::index_type
>(_vec->Size())};
101 template <
typename T>
104 static_cast<typename Span<T>::index_type
>(_vec->Size())};
107 template <
typename T>
109 _vector->ConstHostPointer();
111 template <
typename Head,
typename... Rest>
114 _vector->ConstHostPointer();
115 SyncHost(_vectors...);
118 template <
typename T>
120 vector->SetDevice(device);
122 template <
typename Head,
typename... Rest>
123 void UnpackShard(
int device,
126 _vector->SetDevice(device);
127 UnpackShard(device, _vectors...);
130#if defined(__CUDACC__)
131 template <typename std::enable_if<CompiledWithCuda>::type* =
nullptr,
133 void LaunchCUDA(Functor _func, HDV*... _vectors)
const {
134 UnpackShard(device_, _vectors...);
136 size_t range_size = *range_.end() - *range_.begin();
141 size_t shard_size = range_size;
142 Range shard_range {0,
static_cast<Range::DifferenceType
>(shard_size)};
143 dh::safe_cuda(cudaSetDevice(device_));
145 static_cast<int>(DivRoundUp(*(range_.end()), kBlockThreads));
149 detail::LaunchCUDAKernel<<<kGrids, kBlockThreads>>>(
150 _func, shard_range, UnpackHDVOnDevice(_vectors)...);
154 template <typename std::enable_if<!CompiledWithCuda>::type* =
nullptr,
156 void LaunchCUDA(Functor _func, HDV*...)
const {
160 LOG(FATAL) <<
"Not part of device code. WITH_CUDA: " << WITH_CUDA();
164 template <
typename... HDV>
165 void LaunchCPU(Functor func, HDV *...vectors)
const {
167 SyncHost(vectors...);
168 ParallelFor(end, n_threads_, [&](
omp_ulong idx) { func(idx, UnpackHDV(vectors)...); });
193 template <
typename Functor>
194 static Evaluator<Functor>
Init(Functor func,
Range const range, int32_t n_threads,
195 int32_t device_idx) {
196 return Evaluator<Functor>{func, std::move(range), n_threads, device_idx};