-
Notifications
You must be signed in to change notification settings - Fork 917
/
reader_impl_preprocess.cu
1632 lines (1432 loc) · 64.2 KB
/
reader_impl_preprocess.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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
* 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 "error.hpp"
#include "reader_impl.hpp"
#include <cudf/detail/iterator.cuh>
#include <cudf/detail/nvtx/ranges.hpp>
#include <cudf/detail/utilities/integer_utils.hpp>
#include <cudf/detail/utilities/vector_factories.hpp>
#include <rmm/exec_policy.hpp>
#include <cuda/functional>
#include <thrust/binary_search.h>
#include <thrust/fill.h>
#include <thrust/functional.h>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/discard_iterator.h>
#include <thrust/iterator/iterator_categories.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/logical.h>
#include <thrust/reduce.h>
#include <thrust/scan.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>
#include <thrust/transform.h>
#include <thrust/transform_scan.h>
#include <thrust/unique.h>
#include <bitset>
#include <numeric>
namespace cudf::io::parquet::detail {
namespace {
#if defined(PREPROCESS_DEBUG)
void print_pages(cudf::detail::hostdevice_vector<PageInfo>& pages, rmm::cuda_stream_view _stream)
{
pages.device_to_host_sync(_stream);
for (size_t idx = 0; idx < pages.size(); idx++) {
auto const& p = pages[idx];
// skip dictionary pages
if (p.flags & PAGEINFO_FLAGS_DICTIONARY) { continue; }
printf(
"P(%lu, s:%d): chunk_row(%d), num_rows(%d), skipped_values(%d), skipped_leaf_values(%d), "
"str_bytes(%d)\n",
idx,
p.src_col_schema,
p.chunk_row,
p.num_rows,
p.skipped_values,
p.skipped_leaf_values,
p.str_bytes);
}
}
#endif // PREPROCESS_DEBUG
/**
* @brief Generate depth remappings for repetition and definition levels.
*
* When dealing with columns that contain lists, we must examine incoming
* repetition and definition level pairs to determine what range of output nesting
* is indicated when adding new values. This function generates the mappings of
* the R/D levels to those start/end bounds
*
* @param remap Maps column schema index to the R/D remapping vectors for that column
* @param src_col_schema The column schema to generate the new mapping for
* @param md File metadata information
*/
void generate_depth_remappings(std::map<int, std::pair<std::vector<int>, std::vector<int>>>& remap,
int src_col_schema,
aggregate_reader_metadata const& md)
{
// already generated for this level
if (remap.find(src_col_schema) != remap.end()) { return; }
auto schema = md.get_schema(src_col_schema);
int max_depth = md.get_output_nesting_depth(src_col_schema);
CUDF_EXPECTS(remap.find(src_col_schema) == remap.end(),
"Attempting to remap a schema more than once");
auto inserted =
remap.insert(std::pair<int, std::pair<std::vector<int>, std::vector<int>>>{src_col_schema, {}});
auto& depth_remap = inserted.first->second;
std::vector<int>& rep_depth_remap = (depth_remap.first);
rep_depth_remap.resize(schema.max_repetition_level + 1);
std::vector<int>& def_depth_remap = (depth_remap.second);
def_depth_remap.resize(schema.max_definition_level + 1);
// the key:
// for incoming level values R/D
// add values starting at the shallowest nesting level X has repetition level R
// until you reach the deepest nesting level Y that corresponds to the repetition level R1
// held by the nesting level that has definition level D
//
// Example: a 3 level struct with a list at the bottom
//
// R / D Depth
// level0 0 / 1 0
// level1 0 / 2 1
// level2 0 / 3 2
// list 0 / 3 3
// element 1 / 4 4
//
// incoming R/D : 0, 0 -> add values from depth 0 to 3 (def level 0 always maps to depth 0)
// incoming R/D : 0, 1 -> add values from depth 0 to 3
// incoming R/D : 0, 2 -> add values from depth 0 to 3
// incoming R/D : 1, 4 -> add values from depth 4 to 4
//
// Note : the -validity- of values is simply checked by comparing the incoming D value against the
// D value of the given nesting level (incoming D >= the D for the nesting level == valid,
// otherwise NULL). The tricky part is determining what nesting levels to add values at.
//
// For schemas with no repetition level (no lists), X is always 0 and Y is always max nesting
// depth.
//
// compute "X" from above
for (int s_idx = schema.max_repetition_level; s_idx >= 0; s_idx--) {
auto find_shallowest = [&](int r) {
int shallowest = -1;
int cur_depth = max_depth - 1;
int schema_idx = src_col_schema;
while (schema_idx > 0) {
auto cur_schema = md.get_schema(schema_idx);
if (cur_schema.max_repetition_level == r) {
// if this is a repeated field, map it one level deeper
shallowest = cur_schema.is_stub() ? cur_depth + 1 : cur_depth;
}
// if it's one-level encoding list
else if (cur_schema.is_one_level_list(md.get_schema(cur_schema.parent_idx))) {
shallowest = cur_depth - 1;
}
if (!cur_schema.is_stub()) { cur_depth--; }
schema_idx = cur_schema.parent_idx;
}
return shallowest;
};
rep_depth_remap[s_idx] = find_shallowest(s_idx);
}
// compute "Y" from above
for (int s_idx = schema.max_definition_level; s_idx >= 0; s_idx--) {
auto find_deepest = [&](int d) {
SchemaElement prev_schema;
int schema_idx = src_col_schema;
int r1 = 0;
while (schema_idx > 0) {
SchemaElement cur_schema = md.get_schema(schema_idx);
if (cur_schema.max_definition_level == d) {
// if this is a repeated field, map it one level deeper
r1 = cur_schema.is_stub() ? prev_schema.max_repetition_level
: cur_schema.max_repetition_level;
break;
}
prev_schema = cur_schema;
schema_idx = cur_schema.parent_idx;
}
// we now know R1 from above. return the deepest nesting level that has the
// same repetition level
schema_idx = src_col_schema;
int depth = max_depth - 1;
while (schema_idx > 0) {
SchemaElement cur_schema = md.get_schema(schema_idx);
if (cur_schema.max_repetition_level == r1) {
// if this is a repeated field, map it one level deeper
depth = cur_schema.is_stub() ? depth + 1 : depth;
break;
}
if (!cur_schema.is_stub()) { depth--; }
prev_schema = cur_schema;
schema_idx = cur_schema.parent_idx;
}
return depth;
};
def_depth_remap[s_idx] = find_deepest(s_idx);
}
}
/**
* @brief Reads compressed page data to device memory.
*
* @param sources Dataset sources
* @param page_data Buffers to hold compressed page data for each chunk
* @param chunks List of column chunk descriptors
* @param begin_chunk Index of first column chunk to read
* @param end_chunk Index after the last column chunk to read
* @param column_chunk_offsets File offset for all chunks
* @param chunk_source_map Association between each column chunk and its source
* @param stream CUDA stream used for device memory operations and kernel launches
*
* @return A future object for reading synchronization
*/
[[nodiscard]] std::future<void> read_column_chunks_async(
std::vector<std::unique_ptr<datasource>> const& sources,
std::vector<std::unique_ptr<datasource::buffer>>& page_data,
cudf::detail::hostdevice_vector<ColumnChunkDesc>& chunks,
size_t begin_chunk,
size_t end_chunk,
std::vector<size_t> const& column_chunk_offsets,
std::vector<size_type> const& chunk_source_map,
rmm::cuda_stream_view stream)
{
// Transfer chunk data, coalescing adjacent chunks
std::vector<std::future<size_t>> read_tasks;
for (size_t chunk = begin_chunk; chunk < end_chunk;) {
size_t const io_offset = column_chunk_offsets[chunk];
size_t io_size = chunks[chunk].compressed_size;
size_t next_chunk = chunk + 1;
bool const is_compressed = (chunks[chunk].codec != Compression::UNCOMPRESSED);
while (next_chunk < end_chunk) {
size_t const next_offset = column_chunk_offsets[next_chunk];
bool const is_next_compressed = (chunks[next_chunk].codec != Compression::UNCOMPRESSED);
if (next_offset != io_offset + io_size || is_next_compressed != is_compressed ||
chunk_source_map[chunk] != chunk_source_map[next_chunk]) {
// Can't merge if not contiguous or mixing compressed and uncompressed
// Not coalescing uncompressed with compressed chunks is so that compressed buffers can be
// freed earlier (immediately after decompression stage) to limit peak memory requirements
break;
}
io_size += chunks[next_chunk].compressed_size;
next_chunk++;
}
if (io_size != 0) {
auto& source = sources[chunk_source_map[chunk]];
if (source->is_device_read_preferred(io_size)) {
// Buffer needs to be padded.
// Required by `gpuDecodePageData`.
auto buffer =
rmm::device_buffer(cudf::util::round_up_safe(io_size, BUFFER_PADDING_MULTIPLE), stream);
auto fut_read_size = source->device_read_async(
io_offset, io_size, static_cast<uint8_t*>(buffer.data()), stream);
read_tasks.emplace_back(std::move(fut_read_size));
page_data[chunk] = datasource::buffer::create(std::move(buffer));
} else {
auto const read_buffer = source->host_read(io_offset, io_size);
// Buffer needs to be padded.
// Required by `gpuDecodePageData`.
auto tmp_buffer = rmm::device_buffer(
cudf::util::round_up_safe(read_buffer->size(), BUFFER_PADDING_MULTIPLE), stream);
CUDF_CUDA_TRY(cudaMemcpyAsync(
tmp_buffer.data(), read_buffer->data(), read_buffer->size(), cudaMemcpyDefault, stream));
page_data[chunk] = datasource::buffer::create(std::move(tmp_buffer));
}
auto d_compdata = page_data[chunk]->data();
do {
chunks[chunk].compressed_data = d_compdata;
d_compdata += chunks[chunk].compressed_size;
} while (++chunk != next_chunk);
} else {
chunk = next_chunk;
}
}
auto sync_fn = [](decltype(read_tasks) read_tasks) {
for (auto& task : read_tasks) {
task.wait();
}
};
return std::async(std::launch::deferred, sync_fn, std::move(read_tasks));
}
/**
* @brief Return the number of total pages from the given column chunks.
*
* @param chunks List of column chunk descriptors
* @param stream CUDA stream used for device memory operations and kernel launches
*
* @return The total number of pages
*/
[[nodiscard]] size_t count_page_headers(cudf::detail::hostdevice_vector<ColumnChunkDesc>& chunks,
rmm::cuda_stream_view stream)
{
size_t total_pages = 0;
kernel_error error_code(stream);
chunks.host_to_device_async(stream);
DecodePageHeaders(chunks.device_ptr(), nullptr, chunks.size(), error_code.data(), stream);
chunks.device_to_host_sync(stream);
// It's required to ignore unsupported encodings in this function
// so that we can actually compile a list of all the unsupported encodings found
// in the pages. That cannot be done here since we do not have the pages vector here.
// see https://github.com/rapidsai/cudf/pull/14453#pullrequestreview-1778346688
if (auto const error = error_code.value_sync(stream);
error != 0 and error != static_cast<uint32_t>(decode_error::UNSUPPORTED_ENCODING)) {
CUDF_FAIL("Parquet header parsing failed with code(s) while counting page headers " +
kernel_error::to_string(error));
}
for (size_t c = 0; c < chunks.size(); c++) {
total_pages += chunks[c].num_data_pages + chunks[c].num_dict_pages;
}
return total_pages;
}
/**
* @brief Count the total number of pages using page index information.
*/
[[nodiscard]] size_t count_page_headers_with_pgidx(
cudf::detail::hostdevice_vector<ColumnChunkDesc>& chunks, rmm::cuda_stream_view stream)
{
size_t total_pages = 0;
for (auto& chunk : chunks) {
CUDF_EXPECTS(chunk.h_chunk_info != nullptr, "Expected non-null column info struct");
auto const& chunk_info = *chunk.h_chunk_info;
chunk.num_dict_pages = chunk_info.has_dictionary() ? 1 : 0;
chunk.num_data_pages = chunk_info.pages.size();
total_pages += chunk.num_data_pages + chunk.num_dict_pages;
}
// count_page_headers() also pushes chunks to device, so not using thrust here
chunks.host_to_device_async(stream);
return total_pages;
}
// struct used to carry info from the page indexes to the device
struct page_index_info {
int32_t num_rows;
int32_t chunk_row;
int32_t num_nulls;
int32_t num_valids;
int32_t str_bytes;
};
// functor to copy page_index_info into the PageInfo struct
struct copy_page_info {
device_span<page_index_info const> page_indexes;
device_span<PageInfo> pages;
__device__ void operator()(size_type idx)
{
auto& pg = pages[idx];
auto const& pi = page_indexes[idx];
pg.num_rows = pi.num_rows;
pg.chunk_row = pi.chunk_row;
pg.has_page_index = true;
pg.num_nulls = pi.num_nulls;
pg.num_valids = pi.num_valids;
pg.str_bytes_from_index = pi.str_bytes;
pg.str_bytes = pi.str_bytes;
pg.start_val = 0;
pg.end_val = pg.num_valids;
}
};
/**
* @brief Set fields on the pages that can be derived from page indexes.
*
* This replaces some preprocessing steps, such as page string size calculation.
*/
void fill_in_page_info(host_span<ColumnChunkDesc> chunks,
device_span<PageInfo> pages,
rmm::cuda_stream_view stream)
{
auto const num_pages = pages.size();
std::vector<page_index_info> page_indexes(num_pages);
for (size_t c = 0, page_count = 0; c < chunks.size(); c++) {
auto const& chunk = chunks[c];
CUDF_EXPECTS(chunk.h_chunk_info != nullptr, "Expected non-null column info struct");
auto const& chunk_info = *chunk.h_chunk_info;
size_t start_row = 0;
page_count += chunk.num_dict_pages;
for (size_t p = 0; p < chunk_info.pages.size(); p++, page_count++) {
auto& page = page_indexes[page_count];
page.num_rows = chunk_info.pages[p].num_rows;
page.chunk_row = start_row;
page.num_nulls = chunk_info.pages[p].num_nulls.value_or(0);
page.num_valids = chunk_info.pages[p].num_valid.value_or(0);
page.str_bytes = chunk_info.pages[p].var_bytes_size.value_or(0);
start_row += page.num_rows;
}
}
auto d_page_indexes = cudf::detail::make_device_uvector_async(
page_indexes, stream, rmm::mr::get_current_device_resource());
auto iter = thrust::make_counting_iterator<size_type>(0);
thrust::for_each(
rmm::exec_policy_nosync(stream), iter, iter + num_pages, copy_page_info{d_page_indexes, pages});
}
/**
* @brief Returns a string representation of known encodings
*
* @param encoding Given encoding
* @return String representation of encoding
*/
std::string encoding_to_string(Encoding encoding)
{
switch (encoding) {
case Encoding::PLAIN: return "PLAIN";
case Encoding::GROUP_VAR_INT: return "GROUP_VAR_INT";
case Encoding::PLAIN_DICTIONARY: return "PLAIN_DICTIONARY";
case Encoding::RLE: return "RLE";
case Encoding::BIT_PACKED: return "BIT_PACKED";
case Encoding::DELTA_BINARY_PACKED: return "DELTA_BINARY_PACKED";
case Encoding::DELTA_LENGTH_BYTE_ARRAY: return "DELTA_LENGTH_BYTE_ARRAY";
case Encoding::DELTA_BYTE_ARRAY: return "DELTA_BYTE_ARRAY";
case Encoding::RLE_DICTIONARY: return "RLE_DICTIONARY";
case Encoding::BYTE_STREAM_SPLIT: return "BYTE_STREAM_SPLIT";
case Encoding::NUM_ENCODINGS:
default: return "UNKNOWN(" + std::to_string(static_cast<int>(encoding)) + ")";
}
}
/**
* @brief Helper function to convert an encoding bitmask to a readable string
*
* @param bitmask Bitmask of found unsupported encodings
* @returns Human readable string with unsupported encodings
*/
[[nodiscard]] std::string encoding_bitmask_to_str(uint32_t encoding_bitmask)
{
std::bitset<32> bits(encoding_bitmask);
std::string result;
for (size_t i = 0; i < bits.size(); ++i) {
if (bits.test(i)) {
auto const current = static_cast<Encoding>(i);
if (!is_supported_encoding(current)) { result.append(encoding_to_string(current) + " "); }
}
}
return result;
}
/**
* @brief Create a readable string for the user that will list out all unsupported encodings found.
*
* @param pages List of page information
* @param stream CUDA stream used for device memory operations and kernel launches
* @returns Human readable string with unsupported encodings
*/
[[nodiscard]] std::string list_unsupported_encodings(device_span<PageInfo const> pages,
rmm::cuda_stream_view stream)
{
auto const to_mask = cuda::proclaim_return_type<uint32_t>([] __device__(auto const& page) {
return is_supported_encoding(page.encoding) ? 0U : encoding_to_mask(page.encoding);
});
uint32_t const unsupported = thrust::transform_reduce(
rmm::exec_policy(stream), pages.begin(), pages.end(), to_mask, 0U, thrust::bit_or<uint32_t>());
return encoding_bitmask_to_str(unsupported);
}
/**
* @brief Sort pages in chunk/schema order
*
* @param unsorted_pages The unsorted pages
* @param chunks The chunks associated with the pages
* @param stream CUDA stream used for device memory operations and kernel launches
* @returns The sorted vector of pages
*/
cudf::detail::hostdevice_vector<PageInfo> sort_pages(device_span<PageInfo const> unsorted_pages,
device_span<ColumnChunkDesc const> chunks,
rmm::cuda_stream_view stream)
{
CUDF_FUNC_RANGE();
// sort the pages in chunk/schema order. we use chunk.src_col_index instead of
// chunk.src_col_schema because the user may have reordered them (reading columns, "a" and "b" but
// returning them as "b" and "a")
//
// ordering of pages is by input column schema, repeated across row groups. so
// if we had 3 columns, each with 2 pages, and 1 row group, our schema values might look like
//
// 1, 1, 2, 2, 3, 3
//
// However, if we had more than one row group, the pattern would be
//
// 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3
// ^ row group 0 |
// ^ row group 1
//
// To process pages by key (exclusive_scan_by_key, reduce_by_key, etc), the ordering we actually
// want is
//
// 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3
//
// We also need to preserve key-relative page ordering, so we need to use a stable sort.
rmm::device_uvector<int32_t> page_keys{unsorted_pages.size(), stream};
thrust::transform(
rmm::exec_policy_nosync(stream),
unsorted_pages.begin(),
unsorted_pages.end(),
page_keys.begin(),
cuda::proclaim_return_type<int32_t>([chunks = chunks.begin()] __device__(PageInfo const& page) {
return chunks[page.chunk_idx].src_col_index;
}));
// we are doing this by sorting indices first and then transforming the output because nvcc
// started generating kernels using too much shared memory when trying to sort the pages
// directly.
rmm::device_uvector<int32_t> sort_indices(unsorted_pages.size(), stream);
thrust::sequence(rmm::exec_policy_nosync(stream), sort_indices.begin(), sort_indices.end(), 0);
thrust::stable_sort_by_key(rmm::exec_policy_nosync(stream),
page_keys.begin(),
page_keys.end(),
sort_indices.begin(),
thrust::less<int>());
auto pass_pages =
cudf::detail::hostdevice_vector<PageInfo>(unsorted_pages.size(), unsorted_pages.size(), stream);
thrust::transform(
rmm::exec_policy_nosync(stream),
sort_indices.begin(),
sort_indices.end(),
pass_pages.d_begin(),
cuda::proclaim_return_type<PageInfo>([unsorted_pages = unsorted_pages.begin()] __device__(
int32_t i) { return unsorted_pages[i]; }));
stream.synchronize();
return pass_pages;
}
/**
* @brief Decode the page information for a given pass.
*
* @param pass_intermediate_data The struct containing pass information
*/
void decode_page_headers(pass_intermediate_data& pass,
device_span<PageInfo> unsorted_pages,
bool has_page_index,
rmm::cuda_stream_view stream)
{
CUDF_FUNC_RANGE();
auto iter = thrust::make_counting_iterator(0);
rmm::device_uvector<size_t> chunk_page_counts(pass.chunks.size() + 1, stream);
thrust::transform_exclusive_scan(
rmm::exec_policy_nosync(stream),
iter,
iter + pass.chunks.size() + 1,
chunk_page_counts.begin(),
cuda::proclaim_return_type<size_t>(
[chunks = pass.chunks.d_begin(), num_chunks = pass.chunks.size()] __device__(size_t i) {
return static_cast<size_t>(
i >= num_chunks ? 0 : chunks[i].num_data_pages + chunks[i].num_dict_pages);
}),
0,
thrust::plus<size_t>{});
rmm::device_uvector<chunk_page_info> d_chunk_page_info(pass.chunks.size(), stream);
thrust::for_each(rmm::exec_policy_nosync(stream),
iter,
iter + pass.chunks.size(),
[cpi = d_chunk_page_info.begin(),
chunk_page_counts = chunk_page_counts.begin(),
unsorted_pages = unsorted_pages.begin()] __device__(size_t i) {
cpi[i].pages = &unsorted_pages[chunk_page_counts[i]];
});
kernel_error error_code(stream);
DecodePageHeaders(pass.chunks.d_begin(),
d_chunk_page_info.begin(),
pass.chunks.size(),
error_code.data(),
stream);
if (auto const error = error_code.value_sync(stream); error != 0) {
if (BitAnd(error, decode_error::UNSUPPORTED_ENCODING) != 0) {
auto const unsupported_str =
". With unsupported encodings found: " + list_unsupported_encodings(pass.pages, stream);
CUDF_FAIL("Parquet header parsing failed with code(s) " + kernel_error::to_string(error) +
unsupported_str);
} else {
CUDF_FAIL("Parquet header parsing failed with code(s) " + kernel_error::to_string(error));
}
}
if (has_page_index) { fill_in_page_info(pass.chunks, unsorted_pages, stream); }
// compute max bytes needed for level data
auto level_bit_size = cudf::detail::make_counting_transform_iterator(
0, cuda::proclaim_return_type<int>([chunks = pass.chunks.d_begin()] __device__(int i) {
auto c = chunks[i];
return static_cast<int>(
max(c.level_bits[level_type::REPETITION], c.level_bits[level_type::DEFINITION]));
}));
// max level data bit size.
int const max_level_bits = thrust::reduce(rmm::exec_policy(stream),
level_bit_size,
level_bit_size + pass.chunks.size(),
0,
thrust::maximum<int>());
pass.level_type_size = std::max(1, cudf::util::div_rounding_up_safe(max_level_bits, 8));
// sort the pages in chunk/schema order.
pass.pages = sort_pages(unsorted_pages, pass.chunks, stream);
// compute offsets to each group of input pages.
// page_keys: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3
//
// result: 0, 4, 8
rmm::device_uvector<size_type> page_counts(pass.pages.size() + 1, stream);
auto page_keys = make_page_key_iterator(pass.pages);
auto const page_counts_end = thrust::reduce_by_key(rmm::exec_policy(stream),
page_keys,
page_keys + pass.pages.size(),
thrust::make_constant_iterator(1),
thrust::make_discard_iterator(),
page_counts.begin())
.second;
auto const num_page_counts = page_counts_end - page_counts.begin();
pass.page_offsets = rmm::device_uvector<size_type>(num_page_counts + 1, stream);
thrust::exclusive_scan(rmm::exec_policy_nosync(stream),
page_counts.begin(),
page_counts.begin() + num_page_counts + 1,
pass.page_offsets.begin());
// setup dict_page for each chunk if necessary
thrust::for_each(rmm::exec_policy_nosync(stream),
pass.pages.d_begin(),
pass.pages.d_end(),
[chunks = pass.chunks.d_begin()] __device__(PageInfo const& p) {
if (p.flags & PAGEINFO_FLAGS_DICTIONARY) {
chunks[p.chunk_idx].dict_page = &p;
}
});
pass.pages.device_to_host_async(stream);
pass.chunks.device_to_host_async(stream);
stream.synchronize();
}
constexpr bool is_string_chunk(ColumnChunkDesc const& chunk)
{
auto const is_decimal =
chunk.logical_type.has_value() and chunk.logical_type->type == LogicalType::DECIMAL;
auto const is_binary =
chunk.physical_type == BYTE_ARRAY or chunk.physical_type == FIXED_LEN_BYTE_ARRAY;
return is_binary and not is_decimal;
}
struct set_str_dict_index_count {
device_span<size_t> str_dict_index_count;
device_span<ColumnChunkDesc const> chunks;
__device__ void operator()(PageInfo const& page)
{
auto const& chunk = chunks[page.chunk_idx];
if ((page.flags & PAGEINFO_FLAGS_DICTIONARY) != 0 and chunk.num_dict_pages > 0 and
is_string_chunk(chunk)) {
// there is only ever one dictionary page per chunk, so this is safe to do in parallel.
str_dict_index_count[page.chunk_idx] = page.num_input_values;
}
}
};
struct set_str_dict_index_ptr {
string_index_pair* const base;
device_span<size_t const> str_dict_index_offsets;
device_span<ColumnChunkDesc> chunks;
__device__ void operator()(size_t i)
{
auto& chunk = chunks[i];
if (chunk.num_dict_pages > 0 and is_string_chunk(chunk)) {
chunk.str_dict_index = base + str_dict_index_offsets[i];
}
}
};
/**
* @brief Functor which computes an estimated row count for list pages.
*
*/
struct set_list_row_count_estimate {
device_span<ColumnChunkDesc const> chunks;
__device__ void operator()(PageInfo& page)
{
if (page.flags & PAGEINFO_FLAGS_DICTIONARY) { return; }
auto const& chunk = chunks[page.chunk_idx];
auto const is_list = chunk.max_level[level_type::REPETITION] > 0;
if (!is_list) { return; }
// For LIST pages that we have not yet decoded, page.num_rows is not an accurate number.
// so we instead estimate the number of rows as follows:
// - each chunk stores an estimated number of bytes per row E
// - estimate number of rows in a page = page.uncompressed_page_size / E
//
// it is not required that this number is accurate. we just want it to be somewhat close so that
// we get reasonable results as we choose subpass splits.
//
// all other columns can use page.num_rows directly as it will be accurate.
page.num_rows = static_cast<size_t>(static_cast<float>(page.uncompressed_page_size) /
chunk.list_bytes_per_row_est);
}
};
/**
* @brief Set the expected row count on the final page for all columns.
*
*/
struct set_final_row_count {
device_span<PageInfo> pages;
device_span<ColumnChunkDesc const> chunks;
__device__ void operator()(size_t i)
{
auto& page = pages[i];
auto const& chunk = chunks[page.chunk_idx];
// only do this for the last page in each chunk
if (i < pages.size() - 1 && (pages[i + 1].chunk_idx == page.chunk_idx)) { return; }
size_t const page_start_row = chunk.start_row + page.chunk_row;
size_t const chunk_last_row = chunk.start_row + chunk.num_rows;
page.num_rows = chunk_last_row - page_start_row;
}
};
} // anonymous namespace
void reader::impl::build_string_dict_indices()
{
CUDF_FUNC_RANGE();
auto& pass = *_pass_itm_data;
// compute number of indices per chunk and a summed total
rmm::device_uvector<size_t> str_dict_index_count(pass.chunks.size() + 1, _stream);
thrust::fill(
rmm::exec_policy_nosync(_stream), str_dict_index_count.begin(), str_dict_index_count.end(), 0);
thrust::for_each(rmm::exec_policy_nosync(_stream),
pass.pages.d_begin(),
pass.pages.d_end(),
set_str_dict_index_count{str_dict_index_count, pass.chunks});
size_t const total_str_dict_indexes = thrust::reduce(
rmm::exec_policy(_stream), str_dict_index_count.begin(), str_dict_index_count.end());
if (total_str_dict_indexes == 0) { return; }
// convert to offsets
rmm::device_uvector<size_t>& str_dict_index_offsets = str_dict_index_count;
thrust::exclusive_scan(rmm::exec_policy_nosync(_stream),
str_dict_index_offsets.begin(),
str_dict_index_offsets.end(),
str_dict_index_offsets.begin(),
0);
// allocate and distribute pointers
pass.str_dict_index = cudf::detail::make_zeroed_device_uvector_async<string_index_pair>(
total_str_dict_indexes, _stream, rmm::mr::get_current_device_resource());
auto iter = thrust::make_counting_iterator(0);
thrust::for_each(
rmm::exec_policy_nosync(_stream),
iter,
iter + pass.chunks.size(),
set_str_dict_index_ptr{pass.str_dict_index.data(), str_dict_index_offsets, pass.chunks});
// compute the indices
BuildStringDictionaryIndex(pass.chunks.device_ptr(), pass.chunks.size(), _stream);
pass.chunks.device_to_host_sync(_stream);
}
void reader::impl::allocate_nesting_info()
{
auto& pass = *_pass_itm_data;
auto& subpass = *pass.subpass;
auto const num_columns = _input_columns.size();
auto& pages = subpass.pages;
auto& page_nesting_info = subpass.page_nesting_info;
auto& page_nesting_decode_info = subpass.page_nesting_decode_info;
// generate the number of nesting info structs needed per-page, by column
std::vector<int> per_page_nesting_info_size(num_columns);
auto iter = thrust::make_counting_iterator(size_type{0});
std::transform(iter, iter + num_columns, per_page_nesting_info_size.begin(), [&](size_type i) {
auto const schema_idx = _input_columns[i].schema_idx;
auto const& schema = _metadata->get_schema(schema_idx);
return max(schema.max_definition_level + 1, _metadata->get_output_nesting_depth(schema_idx));
});
// compute total # of page_nesting infos needed and allocate space. doing this in one
// buffer to keep it to a single gpu allocation
auto counting_iter = thrust::make_counting_iterator(size_t{0});
size_t const total_page_nesting_infos =
std::accumulate(counting_iter, counting_iter + num_columns, 0, [&](int total, size_t index) {
return total + (per_page_nesting_info_size[index] * subpass.column_page_count[index]);
});
page_nesting_info =
cudf::detail::hostdevice_vector<PageNestingInfo>{total_page_nesting_infos, _stream};
page_nesting_decode_info =
cudf::detail::hostdevice_vector<PageNestingDecodeInfo>{total_page_nesting_infos, _stream};
// update pointers in the PageInfos
int target_page_index = 0;
int src_info_index = 0;
for (size_t idx = 0; idx < _input_columns.size(); idx++) {
auto const src_col_schema = _input_columns[idx].schema_idx;
for (size_t p_idx = 0; p_idx < subpass.column_page_count[idx]; p_idx++) {
pages[target_page_index + p_idx].nesting = page_nesting_info.device_ptr() + src_info_index;
pages[target_page_index + p_idx].nesting_decode =
page_nesting_decode_info.device_ptr() + src_info_index;
pages[target_page_index + p_idx].nesting_info_size = per_page_nesting_info_size[idx];
pages[target_page_index + p_idx].num_output_nesting_levels =
_metadata->get_output_nesting_depth(src_col_schema);
src_info_index += per_page_nesting_info_size[idx];
}
target_page_index += subpass.column_page_count[idx];
}
// fill in
int nesting_info_index = 0;
std::map<int, std::pair<std::vector<int>, std::vector<int>>> depth_remapping;
for (size_t idx = 0; idx < _input_columns.size(); idx++) {
auto const src_col_schema = _input_columns[idx].schema_idx;
// schema of the input column
auto& schema = _metadata->get_schema(src_col_schema);
// real depth of the output cudf column hierarchy (1 == no nesting, 2 == 1 level, etc)
int const max_output_depth = _metadata->get_output_nesting_depth(src_col_schema);
// if this column has lists, generate depth remapping
std::map<int, std::pair<std::vector<int>, std::vector<int>>> depth_remapping;
if (schema.max_repetition_level > 0) {
generate_depth_remappings(depth_remapping, src_col_schema, *_metadata);
}
// fill in host-side nesting info
int schema_idx = src_col_schema;
auto cur_schema = _metadata->get_schema(schema_idx);
int cur_depth = max_output_depth - 1;
while (schema_idx > 0) {
// stub columns (basically the inner field of a list schema element) are not real columns.
// we can ignore them for the purposes of output nesting info
if (!cur_schema.is_stub()) {
// initialize each page within the chunk
for (size_t p_idx = 0; p_idx < subpass.column_page_count[idx]; p_idx++) {
PageNestingInfo* pni =
&page_nesting_info[nesting_info_index + (p_idx * per_page_nesting_info_size[idx])];
PageNestingDecodeInfo* nesting_info =
&page_nesting_decode_info[nesting_info_index +
(p_idx * per_page_nesting_info_size[idx])];
// if we have lists, set our start and end depth remappings
if (schema.max_repetition_level > 0) {
auto remap = depth_remapping.find(src_col_schema);
CUDF_EXPECTS(remap != depth_remapping.end(),
"Could not find depth remapping for schema");
std::vector<int> const& rep_depth_remap = (remap->second.first);
std::vector<int> const& def_depth_remap = (remap->second.second);
for (size_t m = 0; m < rep_depth_remap.size(); m++) {
nesting_info[m].start_depth = rep_depth_remap[m];
}
for (size_t m = 0; m < def_depth_remap.size(); m++) {
nesting_info[m].end_depth = def_depth_remap[m];
}
}
// values indexed by output column index
nesting_info[cur_depth].max_def_level = cur_schema.max_definition_level;
pni[cur_depth].size = 0;
pni[cur_depth].type =
to_type_id(cur_schema, _strings_to_categorical, _options.timestamp_type.id());
pni[cur_depth].nullable = cur_schema.repetition_type == OPTIONAL;
}
// move up the hierarchy
cur_depth--;
}
// next schema
schema_idx = cur_schema.parent_idx;
cur_schema = _metadata->get_schema(schema_idx);
}
nesting_info_index += (per_page_nesting_info_size[idx] * subpass.column_page_count[idx]);
}
// copy nesting info to the device
page_nesting_info.host_to_device_async(_stream);
page_nesting_decode_info.host_to_device_async(_stream);
}
void reader::impl::allocate_level_decode_space()
{
auto& pass = *_pass_itm_data;
auto& subpass = *pass.subpass;
auto& pages = subpass.pages;
// TODO: this could be made smaller if we ignored dictionary pages and pages with no
// repetition data.
size_t const per_page_decode_buf_size = LEVEL_DECODE_BUF_SIZE * 2 * pass.level_type_size;
auto const decode_buf_size = per_page_decode_buf_size * pages.size();
subpass.level_decode_data =
rmm::device_buffer(decode_buf_size, _stream, rmm::mr::get_current_device_resource());
// distribute the buffers
uint8_t* buf = static_cast<uint8_t*>(subpass.level_decode_data.data());
for (size_t idx = 0; idx < pages.size(); idx++) {
auto& p = pages[idx];
p.lvl_decode_buf[level_type::DEFINITION] = buf;
buf += (LEVEL_DECODE_BUF_SIZE * pass.level_type_size);
p.lvl_decode_buf[level_type::REPETITION] = buf;
buf += (LEVEL_DECODE_BUF_SIZE * pass.level_type_size);
}
}
std::pair<bool, std::vector<std::future<void>>> reader::impl::read_column_chunks()
{
auto const& row_groups_info = _pass_itm_data->row_groups;
auto& raw_page_data = _pass_itm_data->raw_page_data;
auto& chunks = _pass_itm_data->chunks;
// Descriptors for all the chunks that make up the selected columns
auto const num_input_columns = _input_columns.size();
auto const num_chunks = row_groups_info.size() * num_input_columns;
// Association between each column chunk and its source
std::vector<size_type> chunk_source_map(num_chunks);
// Tracker for eventually deallocating compressed and uncompressed data
raw_page_data = std::vector<std::unique_ptr<datasource::buffer>>(num_chunks);
// Keep track of column chunk file offsets
std::vector<size_t> column_chunk_offsets(num_chunks);
// Initialize column chunk information
size_t total_decompressed_size = 0;
// TODO: make this respect the pass-wide skip_rows/num_rows instead of the file-wide
// skip_rows/num_rows
// auto remaining_rows = num_rows;
std::vector<std::future<void>> read_chunk_tasks;
size_type chunk_count = 0;
for (auto const& rg : row_groups_info) {
auto const& row_group = _metadata->get_row_group(rg.index, rg.source_index);
auto const row_group_source = rg.source_index;
// generate ColumnChunkDesc objects for everything to be decoded (all input columns)
for (size_t i = 0; i < num_input_columns; ++i) {
auto const& col = _input_columns[i];
// look up metadata
auto& col_meta = _metadata->get_column_metadata(rg.index, rg.source_index, col.schema_idx);
column_chunk_offsets[chunk_count] =
(col_meta.dictionary_page_offset != 0)
? std::min(col_meta.data_page_offset, col_meta.dictionary_page_offset)
: col_meta.data_page_offset;
// Map each column chunk to its column index and its source index
chunk_source_map[chunk_count] = row_group_source;
if (col_meta.codec != Compression::UNCOMPRESSED) {
total_decompressed_size += col_meta.total_uncompressed_size;
}
chunk_count++;
}
}
// Read compressed chunk data to device memory
read_chunk_tasks.push_back(read_column_chunks_async(_sources,
raw_page_data,
chunks,
0,
chunks.size(),
column_chunk_offsets,
chunk_source_map,
_stream));
return {total_decompressed_size > 0, std::move(read_chunk_tasks)};
}
void reader::impl::read_compressed_data()
{
auto& pass = *_pass_itm_data;
// This function should never be called if `num_rows == 0`.
CUDF_EXPECTS(_pass_itm_data->num_rows > 0, "Number of reading rows must not be zero.");
auto& chunks = pass.chunks;
auto const [has_compressed_data, read_chunks_tasks] = read_column_chunks();
pass.has_compressed_data = has_compressed_data;