Skip to content

Commit

Permalink
Change copy_if_safe to call thrust instead of the overload function (#…
Browse files Browse the repository at this point in the history
…15018)

Found while working on large strings where copy-if is called. In places where `copy_if_safe` utility is called the non-stencil overload calls the stencil-ed function by forwarding the `first` iterator as the `stencil` parameter. This works logically because both values will return the same result. Unfortunately, this can be a performance issue if the iterator is complex/slow transform iterator since it would be called twice (an inlined twice). Changing the non-stencil version to call `thrust::copy_if` directly fixes the potential issue.

Authors:
  - David Wendt (https://github.com/davidwendt)

Approvers:
  - Bradley Dice (https://github.com/bdice)
  - Yunsong Wang (https://github.com/PointKernel)
  - Mike Wilson (https://github.com/hyperbolic2346)

URL: #15018
  • Loading branch information
davidwendt authored Feb 13, 2024
1 parent d848707 commit d6902b0
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions cpp/include/cudf/detail/utilities/algorithm.cuh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
* Copyright (c) 2022-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,7 +89,17 @@ OutputIterator copy_if_safe(InputIterator first,
Predicate pred,
rmm::cuda_stream_view stream)
{
return copy_if_safe(first, last, first, result, pred, stream);
auto const copy_size = std::min(static_cast<std::size_t>(std::distance(first, last)),
static_cast<std::size_t>(std::numeric_limits<int>::max()));

auto itr = first;
while (itr != last) {
auto const copy_end =
static_cast<std::size_t>(std::distance(itr, last)) <= copy_size ? last : itr + copy_size;
result = thrust::copy_if(rmm::exec_policy(stream), itr, copy_end, result, pred);
itr = copy_end;
}
return result;
}

} // namespace cudf::detail

0 comments on commit d6902b0

Please sign in to comment.