forked from rapidsai/cudf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.cu
786 lines (700 loc) · 30.9 KB
/
find.cu
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
* Copyright (c) 2019-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.
* 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.
*/
#include <cudf/column/column_device_view.cuh>
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/detail/null_mask.hpp>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/utilities/cuda.cuh>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/strings/detail/utilities.hpp>
#include <cudf/strings/find.hpp>
#include <cudf/strings/string_view.cuh>
#include <cudf/strings/strings_column_view.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/error.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/exec_policy.hpp>
#include <rmm/resource_ref.hpp>
#include <cuda/atomic>
#include <thrust/binary_search.h>
#include <thrust/fill.h>
#include <thrust/for_each.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/transform.h>
namespace cudf {
namespace strings {
namespace detail {
namespace {
/**
* @brief Threshold to decide on using string or warp parallel functions.
*
* If the average byte length of a string in a column exceeds this value then
* a warp-parallel function is used.
*
* Note that this value is shared by find, rfind, and contains functions.
*/
constexpr size_type AVG_CHAR_BYTES_THRESHOLD = 64;
/**
* @brief Find function handles a string per thread
*/
template <typename TargetIterator, bool forward = true>
struct finder_fn {
column_device_view const d_strings;
TargetIterator const d_targets;
size_type const start;
size_type const stop;
__device__ size_type operator()(size_type idx) const
{
if (d_strings.is_null(idx)) { return -1; }
auto const d_str = d_strings.element<string_view>(idx);
if (d_str.empty() && (start > 0)) { return -1; }
auto const d_target = d_targets[idx];
auto const length = d_str.length();
auto const begin = (start > length) ? length : start;
auto const end = (stop < 0) || (stop > length) ? length : stop;
return forward ? d_str.find(d_target, begin, end - begin)
: d_str.rfind(d_target, begin, end - begin);
}
};
/**
* @brief Special logic handles an empty target for find/rfind
*
* where length = number of characters in the input string
* if forward = true:
* return start iff (start <= length), otherwise return -1
* if forward = false:
* return stop iff (0 <= stop <= length), otherwise return length
*/
template <bool forward = true>
struct empty_target_fn {
column_device_view const d_strings;
size_type const start;
size_type const stop;
__device__ size_type operator()(size_type idx) const
{
if (d_strings.is_null(idx)) { return -1; }
auto d_str = d_strings.element<string_view>(idx);
// common case shortcut
if (forward && start == 0) { return 0; }
auto const length = d_str.length();
if (start > length) { return -1; }
if constexpr (forward) { return start; }
return (stop < 0) || (stop > length) ? length : stop;
}
};
/**
* @brief String per warp function for find/rfind
*/
template <typename TargetIterator, bool forward = true>
CUDF_KERNEL void finder_warp_parallel_fn(column_device_view const d_strings,
TargetIterator const d_targets,
size_type const start,
size_type const stop,
size_type* d_results)
{
size_type const idx = static_cast<size_type>(threadIdx.x + blockIdx.x * blockDim.x);
if (idx >= (d_strings.size() * cudf::detail::warp_size)) { return; }
auto const str_idx = idx / cudf::detail::warp_size;
auto const lane_idx = idx % cudf::detail::warp_size;
if (d_strings.is_null(str_idx)) { return; }
// initialize the output for the atomicMin/Max
if (lane_idx == 0) { d_results[str_idx] = forward ? std::numeric_limits<size_type>::max() : -1; }
__syncwarp();
auto const d_str = d_strings.element<string_view>(str_idx);
auto const d_target = d_targets[str_idx];
auto const [begin, left_over] = bytes_to_character_position(d_str, start);
auto const start_char_pos = start - left_over; // keep track of character position
auto const end = [d_str, start, stop, begin = begin] {
if (stop < 0) { return d_str.size_bytes(); }
if (stop <= start) { return begin; }
// we count from `begin` instead of recounting from the beginning of the string
return begin + std::get<0>(bytes_to_character_position(
string_view(d_str.data() + begin, d_str.size_bytes() - begin), stop - start));
}();
// each thread compares the target with the thread's individual starting byte
size_type position = forward ? std::numeric_limits<size_type>::max() : -1;
for (auto itr = begin + lane_idx; itr + d_target.size_bytes() <= end;
itr += cudf::detail::warp_size) {
if (d_target.compare(d_str.data() + itr, d_target.size_bytes()) == 0) {
position = itr;
if (forward) break;
}
}
// find stores the minimum position while rfind stores the maximum position
// note that this was slightly faster than using cub::WarpReduce
cuda::atomic_ref<size_type, cuda::thread_scope_block> ref{*(d_results + str_idx)};
forward ? ref.fetch_min(position, cuda::std::memory_order_relaxed)
: ref.fetch_max(position, cuda::std::memory_order_relaxed);
__syncwarp();
if (lane_idx == 0) {
// the final result needs to be fixed up convert max() to -1
// and a byte position to a character position
auto const result = d_results[str_idx];
d_results[str_idx] =
((result < std::numeric_limits<size_type>::max()) && (result >= begin))
? start_char_pos + characters_in_string(d_str.data() + begin, result - begin)
: -1;
}
}
template <typename TargetIterator, bool forward = true>
void find_utility(strings_column_view const& input,
TargetIterator const& target_itr,
column& output,
size_type start,
size_type stop,
rmm::cuda_stream_view stream)
{
auto d_strings = column_device_view::create(input.parent(), stream);
auto d_results = output.mutable_view().data<size_type>();
if ((input.chars_size(stream) / (input.size() - input.null_count())) > AVG_CHAR_BYTES_THRESHOLD) {
// warp-per-string runs faster for longer strings (but not shorter ones)
constexpr int block_size = 256;
cudf::detail::grid_1d grid{input.size() * cudf::detail::warp_size, block_size};
finder_warp_parallel_fn<TargetIterator, forward>
<<<grid.num_blocks, grid.num_threads_per_block, 0, stream.value()>>>(
*d_strings, target_itr, start, stop, d_results);
} else {
// string-per-thread function
thrust::transform(rmm::exec_policy(stream),
thrust::make_counting_iterator<size_type>(0),
thrust::make_counting_iterator<size_type>(input.size()),
d_results,
finder_fn<TargetIterator, forward>{*d_strings, target_itr, start, stop});
}
}
template <bool forward = true>
std::unique_ptr<column> find_fn(strings_column_view const& input,
string_scalar const& target,
size_type start,
size_type stop,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_EXPECTS(target.is_valid(stream), "Parameter target must be valid.");
CUDF_EXPECTS(start >= 0, "Parameter start must be positive integer or zero.");
if ((stop > 0) && (start > stop)) CUDF_FAIL("Parameter start must be less than stop.");
// create output column
auto results = make_numeric_column(data_type{type_to_id<size_type>()},
input.size(),
cudf::detail::copy_bitmask(input.parent(), stream, mr),
input.null_count(),
stream,
mr);
// if input is empty or all-null then we are done
if (input.size() == input.null_count()) { return results; }
auto d_target = string_view(target.data(), target.size());
// special logic for empty target results
if (d_target.empty()) {
auto d_strings = column_device_view::create(input.parent(), stream);
auto d_results = results->mutable_view().data<size_type>();
thrust::transform(rmm::exec_policy(stream),
thrust::counting_iterator<size_type>(0),
thrust::counting_iterator<size_type>(input.size()),
d_results,
empty_target_fn<forward>{*d_strings, start, stop});
return results;
}
// find-utility function fills in the results column
auto target_itr = thrust::make_constant_iterator(d_target);
using TargetIterator = decltype(target_itr);
find_utility<TargetIterator, forward>(input, target_itr, *results, start, stop, stream);
results->set_null_count(input.null_count());
return results;
}
} // namespace
std::unique_ptr<column> find(strings_column_view const& input,
string_scalar const& target,
size_type start,
size_type stop,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
return find_fn<true>(input, target, start, stop, stream, mr);
}
std::unique_ptr<column> rfind(strings_column_view const& input,
string_scalar const& target,
size_type start,
size_type stop,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
return find_fn<false>(input, target, start, stop, stream, mr);
}
template <bool forward = true>
std::unique_ptr<column> find(strings_column_view const& input,
strings_column_view const& target,
size_type start,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_EXPECTS(start >= 0, "Parameter start must be positive integer or zero.");
CUDF_EXPECTS(input.size() == target.size(), "input and target columns must be the same size");
// create output column
auto results = make_numeric_column(
data_type{type_to_id<size_type>()}, input.size(), rmm::device_buffer{}, 0, stream, mr);
// if input is empty or all-null then we are done
if (input.size() == input.null_count()) { return results; }
// call find utility with target iterator
auto d_targets = column_device_view::create(target.parent(), stream);
auto target_itr = cudf::detail::make_null_replacement_iterator<string_view>(
*d_targets, string_view{}, target.has_nulls());
find_utility<decltype(target_itr), forward>(input, target_itr, *results, start, -1, stream);
// AND the bitmasks from input and target
auto [null_mask, null_count] =
cudf::detail::bitmask_and(table_view({input.parent(), target.parent()}), stream, mr);
results->set_null_mask(std::move(null_mask), null_count);
return results;
}
} // namespace detail
// external APIs
std::unique_ptr<column> find(strings_column_view const& strings,
string_scalar const& target,
size_type start,
size_type stop,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::find(strings, target, start, stop, stream, mr);
}
std::unique_ptr<column> rfind(strings_column_view const& strings,
string_scalar const& target,
size_type start,
size_type stop,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::rfind(strings, target, start, stop, stream, mr);
}
std::unique_ptr<column> find(strings_column_view const& input,
strings_column_view const& target,
size_type start,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::find<true>(input, target, start, stream, mr);
}
namespace detail {
namespace {
#if 0
// neater than compare_vector1() but not faster
struct vloader_unaligned {
uint32_t const* ptr;
int const offset{};
uint32_t value{};
__device__ inline vloader_unaligned(unsigned char const* in)
: offset(static_cast<int>(reinterpret_cast<uintptr_t>(in) & 3) * 8)
{
ptr = reinterpret_cast<uint32_t const*>(in - offset / 8);
value = *ptr++;
}
//__device__ inline uint32_t next()
//{
// uint32_t const block = *ptr++;
// uint32_t const rtn = __funnelshift_r(value, block, offset);
// value = block;
// return rtn;
//}
// way cool, but not faster
__device__ inline uint64_t next2()
{
uint32_t const block1 = *ptr++;
uint32_t const block2 = *ptr++;
uint32_t const rtn1 = __funnelshift_r(value, block1, offset);
uint32_t const rtn2 = __funnelshift_r(block1, block2, offset);
value = block2;
return static_cast<uint64_t>(rtn1) << 32 | static_cast<uint64_t>(rtn2);
}
};
__device__ inline int compare_vload(const char* data1, int len1, const char* data2, int len2)
{
unsigned char const* ptr1 = reinterpret_cast<const unsigned char*>(data1);
unsigned char const* ptr2 = reinterpret_cast<const unsigned char*>(data2);
int const len = min(len1, len2);
int idx = 0;
if (len >= 8) {
vloader_unaligned loader1{ptr1};
vloader_unaligned loader2{ptr2};
do {
auto const a = loader1.next2();
auto const b = loader2.next2();
// if (a != b) { return __byte_perm(a, 0, 0x0123) < __byte_perm(b, 0, 0x0123) ? -1 : 1; }
if (a != b) { return 1; }
idx += sizeof(a);
} while (idx + 8 <= len); // 4
}
while (idx < len) {
auto const a = ptr1[idx];
auto const b = ptr2[idx];
if (a != b) { return static_cast<int>(a) - static_cast<int>(b); }
++idx;
}
if (len1 < len2) return -1;
if (len2 < len1) return 1;
return 0;
}
// vector loading is not showing up faster even for long strings
__device__ int compare_vector1(const char* data1, int len1, const char* data2, int len2)
{
unsigned char const* ptr1 = reinterpret_cast<const unsigned char*>(data1);
unsigned char const* ptr2 = reinterpret_cast<const unsigned char*>(data2);
int const len = min(len1, len2);
int idx = 0;
if (len >= 8) {
uint32_t const align_a = (3 & reinterpret_cast<uintptr_t>(ptr1));
uint32_t const align_b = (3 & reinterpret_cast<uintptr_t>(ptr2));
auto s32_a = reinterpret_cast<uint32_t const*>(ptr1 - align_a) + 1;
auto s32_b = reinterpret_cast<uint32_t const*>(ptr2 - align_b) + 1;
uint32_t const offset_a = align_a * 8;
uint32_t const offset_b = align_b * 8;
do {
uint32_t const a = __funnelshift_r(*(s32_a - 1), *s32_a, offset_a);
uint32_t const b = __funnelshift_r(*(s32_b - 1), *s32_b, offset_b);
if (a != b) { return __byte_perm(a, 0, 0x0123) < __byte_perm(b, 0, 0x0123) ? -1 : 1; }
idx += 4;
++s32_a; // value_a = *s32_a++; // block_a;
++s32_b; // value_b = *s32_b++; // block_b;
} while (idx + 4 <= len);
}
while (idx < len) {
auto const a = ptr1[idx];
auto const b = ptr2[idx];
if (a != b) { return static_cast<int>(a) - static_cast<int>(b); }
++idx;
}
if (len1 < len2) return -1;
if (len2 < len1) return 1;
return 0;
}
#endif
/**
* @brief Check if `d_target` appears in a row in `d_strings`.
*
* This executes as a warp per string/row and performs well for longer strings.
* @see AVG_CHAR_BYTES_THRESHOLD
*
* @param d_strings Column of input strings
* @param d_target String to search for in each row of `d_strings`
* @param d_results Indicates which rows contain `d_target`
*/
CUDF_KERNEL void contains_warp_parallel_fn(column_device_view const d_strings,
string_view const d_target,
bool* d_results)
{
size_type const idx = static_cast<size_type>(threadIdx.x + blockIdx.x * blockDim.x);
using warp_reduce = cub::WarpReduce<bool>;
__shared__ typename warp_reduce::TempStorage temp_storage;
if (idx >= (d_strings.size() * cudf::detail::warp_size)) { return; }
auto const str_idx = idx / cudf::detail::warp_size;
auto const lane_idx = idx % cudf::detail::warp_size;
if (lane_idx) { d_results[str_idx] = false; } // not faster
if (d_strings.is_null(str_idx)) { return; }
// get the string for this warp
auto const d_str = d_strings.element<string_view>(str_idx);
// each thread of the warp will check just part of the string
auto found = false;
for (auto i = lane_idx; !found && ((i + d_target.size_bytes()) <= d_str.size_bytes());
i += cudf::detail::warp_size) {
// check the target matches this part of the d_str data
found = (d_target.compare(d_str.data() + i, d_target.size_bytes()) == 0);
// not faster
// found = compare_vload(
// d_target.data(), d_target.size_bytes(), d_str.data() + i, d_target.size_bytes()) ==
// 0;
// not faster
// auto result = warp_reduce(temp_storage).Reduce(found, cub::Max());
// found = result;
}
auto const result = warp_reduce(temp_storage).Reduce(found, cub::Max());
if (lane_idx == 0) { d_results[str_idx] = result; }
// if (lane_idx == 0) { d_results[str_idx] = found; }
}
std::unique_ptr<column> contains_warp_parallel(strings_column_view const& input,
string_scalar const& target,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_EXPECTS(target.is_valid(stream), "Parameter target must be valid.");
auto d_target = string_view(target.data(), target.size());
// create output column
auto results = make_numeric_column(data_type{type_id::BOOL8},
input.size(),
cudf::detail::copy_bitmask(input.parent(), stream, mr),
input.null_count(),
stream,
mr);
// fill the output with `false` unless the `d_target` is empty
auto results_view = results->mutable_view();
if (d_target.empty()) {
thrust::fill(
rmm::exec_policy_nosync(stream), results_view.begin<bool>(), results_view.end<bool>(), true);
} else {
// launch warp per string
auto const d_strings = column_device_view::create(input.parent(), stream);
constexpr int block_size = 256;
cudf::detail::grid_1d grid{input.size() * cudf::detail::warp_size, block_size};
contains_warp_parallel_fn<<<grid.num_blocks, grid.num_threads_per_block, 0, stream.value()>>>(
*d_strings, d_target, results_view.data<bool>());
}
results->set_null_count(input.null_count());
return results;
}
/**
* @brief Utility to return a bool column indicating the presence of
* a given target string in a strings column.
*
* Null string entries return corresponding null output column entries.
*
* @tparam BoolFunction Return bool value given two strings.
*
* @param strings Column of strings to check for target.
* @param target UTF-8 encoded string to check in strings column.
* @param pfn Returns bool value if target is found in the given string.
* @param stream CUDA stream used for device memory operations and kernel launches.
* @param mr Device memory resource used to allocate the returned column's device memory.
* @return New BOOL column.
*/
template <typename BoolFunction>
std::unique_ptr<column> contains_fn(strings_column_view const& strings,
string_scalar const& target,
BoolFunction pfn,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
auto strings_count = strings.size();
if (strings_count == 0) return make_empty_column(type_id::BOOL8);
CUDF_EXPECTS(target.is_valid(stream), "Parameter target must be valid.");
if (target.size() == 0) // empty target string returns true
{
auto const true_scalar = make_fixed_width_scalar<bool>(true, stream);
auto results = make_column_from_scalar(*true_scalar, strings.size(), stream, mr);
results->set_null_mask(cudf::detail::copy_bitmask(strings.parent(), stream, mr),
strings.null_count());
return results;
}
auto d_target = string_view(target.data(), target.size());
auto strings_column = column_device_view::create(strings.parent(), stream);
auto d_strings = *strings_column;
// create output column
auto results = make_numeric_column(data_type{type_id::BOOL8},
strings_count,
cudf::detail::copy_bitmask(strings.parent(), stream, mr),
strings.null_count(),
stream,
mr);
auto results_view = results->mutable_view();
auto d_results = results_view.data<bool>();
// set the bool values by evaluating the passed function
thrust::transform(rmm::exec_policy(stream),
thrust::make_counting_iterator<size_type>(0),
thrust::make_counting_iterator<size_type>(strings_count),
d_results,
[d_strings, pfn, d_target] __device__(size_type idx) {
if (!d_strings.is_null(idx))
return bool{pfn(d_strings.element<string_view>(idx), d_target)};
return false;
});
results->set_null_count(strings.null_count());
return results;
}
/**
* @brief Utility to return a bool column indicating the presence of
* a string targets[i] in strings[i].
*
* Null string entries return corresponding null output column entries.
*
* @tparam BoolFunction Return bool value given two strings.
*
* @param strings Column of strings to check for `targets[i]`.
* @param targets Column of strings to be checked in `strings[i]``.
* @param pfn Returns bool value if target is found in the given string.
* @param stream CUDA stream used for device memory operations and kernel launches.
* @param mr Device memory resource used to allocate the returned column's device memory.
* @return New BOOL column.
*/
template <typename BoolFunction>
std::unique_ptr<column> contains_fn(strings_column_view const& strings,
strings_column_view const& targets,
BoolFunction pfn,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
if (strings.is_empty()) return make_empty_column(type_id::BOOL8);
CUDF_EXPECTS(targets.size() == strings.size(),
"strings and targets column must be the same size");
auto targets_column = column_device_view::create(targets.parent(), stream);
auto d_targets = *targets_column;
auto strings_column = column_device_view::create(strings.parent(), stream);
auto d_strings = *strings_column;
// create output column
auto results = make_numeric_column(data_type{type_id::BOOL8},
strings.size(),
cudf::detail::copy_bitmask(strings.parent(), stream, mr),
strings.null_count(),
stream,
mr);
auto results_view = results->mutable_view();
auto d_results = results_view.data<bool>();
// set the bool values by evaluating the passed function
thrust::transform(
rmm::exec_policy(stream),
thrust::make_counting_iterator<size_type>(0),
thrust::make_counting_iterator<size_type>(strings.size()),
d_results,
[d_strings, pfn, d_targets] __device__(size_type idx) {
// empty target string returns true
if (d_targets.is_valid(idx) && d_targets.element<string_view>(idx).length() == 0) {
return true;
} else if (!d_strings.is_null(idx) && !d_targets.is_null(idx)) {
return bool{pfn(d_strings.element<string_view>(idx), d_targets.element<string_view>(idx))};
} else {
return false;
}
});
results->set_null_count(strings.null_count());
return results;
}
} // namespace
std::unique_ptr<column> contains(strings_column_view const& input,
string_scalar const& target,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
// use warp parallel when the average string width is greater than the threshold
if ((input.null_count() < input.size()) &&
((input.chars_size(stream) / input.size()) > AVG_CHAR_BYTES_THRESHOLD)) {
return contains_warp_parallel(input, target, stream, mr);
}
// benchmark measurements showed this to be faster for smaller strings
auto pfn = [] __device__(string_view d_string, string_view d_target) {
return d_string.find(d_target) != string_view::npos;
};
return contains_fn(input, target, pfn, stream, mr);
}
std::unique_ptr<column> contains(strings_column_view const& strings,
strings_column_view const& targets,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
auto pfn = [] __device__(string_view d_string, string_view d_target) {
return d_string.find(d_target) != string_view::npos;
};
return contains_fn(strings, targets, pfn, stream, mr);
}
std::unique_ptr<column> starts_with(strings_column_view const& strings,
string_scalar const& target,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
auto pfn = [] __device__(string_view d_string, string_view d_target) {
return (d_target.size_bytes() <= d_string.size_bytes()) &&
(d_target.compare(d_string.data(), d_target.size_bytes()) == 0);
};
return contains_fn(strings, target, pfn, stream, mr);
}
std::unique_ptr<column> starts_with(strings_column_view const& strings,
strings_column_view const& targets,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
auto pfn = [] __device__(string_view d_string, string_view d_target) {
return (d_target.size_bytes() <= d_string.size_bytes()) &&
(d_target.compare(d_string.data(), d_target.size_bytes()) == 0);
};
return contains_fn(strings, targets, pfn, stream, mr);
}
std::unique_ptr<column> ends_with(strings_column_view const& strings,
string_scalar const& target,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
auto pfn = [] __device__(string_view d_string, string_view d_target) {
auto const str_size = d_string.size_bytes();
auto const tgt_size = d_target.size_bytes();
return (tgt_size <= str_size) &&
(d_target.compare(d_string.data() + str_size - tgt_size, tgt_size) == 0);
};
return contains_fn(strings, target, pfn, stream, mr);
}
std::unique_ptr<column> ends_with(strings_column_view const& strings,
strings_column_view const& targets,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
auto pfn = [] __device__(string_view d_string, string_view d_target) {
auto const str_size = d_string.size_bytes();
auto const tgt_size = d_target.size_bytes();
return (tgt_size <= str_size) &&
(d_target.compare(d_string.data() + str_size - tgt_size, tgt_size) == 0);
};
return contains_fn(strings, targets, pfn, stream, mr);
}
} // namespace detail
// external APIs
std::unique_ptr<column> contains(strings_column_view const& strings,
string_scalar const& target,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::contains(strings, target, stream, mr);
}
std::unique_ptr<column> contains(strings_column_view const& strings,
strings_column_view const& targets,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::contains(strings, targets, stream, mr);
}
std::unique_ptr<column> starts_with(strings_column_view const& strings,
string_scalar const& target,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::starts_with(strings, target, stream, mr);
}
std::unique_ptr<column> starts_with(strings_column_view const& strings,
strings_column_view const& targets,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::starts_with(strings, targets, stream, mr);
}
std::unique_ptr<column> ends_with(strings_column_view const& strings,
string_scalar const& target,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::ends_with(strings, target, stream, mr);
}
std::unique_ptr<column> ends_with(strings_column_view const& strings,
strings_column_view const& targets,
rmm::cuda_stream_view stream,
rmm::device_async_resource_ref mr)
{
CUDF_FUNC_RANGE();
return detail::ends_with(strings, targets, stream, mr);
}
} // namespace strings
} // namespace cudf