-
Notifications
You must be signed in to change notification settings - Fork 915
/
writer_impl.cu
1373 lines (1245 loc) · 54.3 KB
/
writer_impl.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) 2019-2021, 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.
*/
/**
* @file writer_impl.cu
* @brief cuDF-IO ORC writer class implementation
*/
#include "writer_impl.hpp"
#include <io/utilities/column_utils.cuh>
#include <cudf/null_mask.hpp>
#include <cudf/strings/strings_column_view.hpp>
#include <cudf/utilities/span.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_buffer.hpp>
#include <rmm/device_uvector.hpp>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <utility>
namespace cudf {
namespace io {
namespace detail {
namespace orc {
using namespace cudf::io::orc;
using namespace cudf::io;
using cudf::io::orc::gpu::nvstrdesc_s;
struct row_group_index_info {
int32_t pos = -1; // Position
int32_t blk_pos = -1; // Block Position
int32_t comp_pos = -1; // Compressed Position
int32_t comp_size = -1; // Compressed size
};
namespace {
/**
* @brief Helper for pinned host memory
*/
template <typename T>
using pinned_buffer = std::unique_ptr<T, decltype(&cudaFreeHost)>;
/**
* @brief Function that translates GDF compression to ORC compression
*/
orc::CompressionKind to_orc_compression(compression_type compression)
{
switch (compression) {
case compression_type::AUTO:
case compression_type::SNAPPY: return orc::CompressionKind::SNAPPY;
case compression_type::NONE: return orc::CompressionKind::NONE;
default: CUDF_EXPECTS(false, "Unsupported compression type"); return orc::CompressionKind::NONE;
}
}
/**
* @brief Function that translates GDF dtype to ORC datatype
*/
constexpr orc::TypeKind to_orc_type(cudf::type_id id)
{
switch (id) {
case cudf::type_id::INT8: return TypeKind::BYTE;
case cudf::type_id::INT16: return TypeKind::SHORT;
case cudf::type_id::INT32: return TypeKind::INT;
case cudf::type_id::INT64: return TypeKind::LONG;
case cudf::type_id::FLOAT32: return TypeKind::FLOAT;
case cudf::type_id::FLOAT64: return TypeKind::DOUBLE;
case cudf::type_id::BOOL8: return TypeKind::BOOLEAN;
case cudf::type_id::TIMESTAMP_DAYS: return TypeKind::DATE;
case cudf::type_id::TIMESTAMP_SECONDS:
case cudf::type_id::TIMESTAMP_MICROSECONDS:
case cudf::type_id::TIMESTAMP_MILLISECONDS:
case cudf::type_id::TIMESTAMP_NANOSECONDS: return TypeKind::TIMESTAMP;
case cudf::type_id::STRING: return TypeKind::STRING;
default: return TypeKind::INVALID_TYPE_KIND;
}
}
/**
* @brief Function that translates time unit to nanoscale multiple
*/
template <typename T>
constexpr T to_clockscale(cudf::type_id timestamp_id)
{
switch (timestamp_id) {
case cudf::type_id::TIMESTAMP_SECONDS: return 9;
case cudf::type_id::TIMESTAMP_MILLISECONDS: return 6;
case cudf::type_id::TIMESTAMP_MICROSECONDS: return 3;
case cudf::type_id::TIMESTAMP_NANOSECONDS:
default: return 0;
}
}
} // namespace
/**
* @brief Helper kernel for converting string data/offsets into nvstrdesc
* REMOVEME: Once we eliminate the legacy readers/writers, the kernels could be
* made to use the native offset+data layout.
*/
__global__ void stringdata_to_nvstrdesc(gpu::nvstrdesc_s *dst,
const size_type *offsets,
const char *strdata,
const uint32_t *nulls,
const size_type column_offset,
size_type column_size)
{
size_type row = blockIdx.x * blockDim.x + threadIdx.x;
if (row < column_size) {
uint32_t is_valid = (nulls != nullptr)
? (nulls[(row + column_offset) / 32] >> ((row + column_offset) % 32)) & 1
: 1;
size_t count;
const char *ptr;
if (is_valid) {
size_type cur = offsets[row];
size_type next = offsets[row + 1];
ptr = strdata + cur;
count = (next > cur) ? next - cur : 0;
} else {
ptr = nullptr;
count = 0;
}
dst[row].ptr = ptr;
dst[row].count = count;
}
}
/**
* @brief Helper class that adds ORC-specific column info
*/
class orc_column_view {
public:
/**
* @brief Constructor that extracts out the string position + length pairs
* for building dictionaries for string columns
*/
explicit orc_column_view(size_t id,
size_t str_id,
column_view const &col,
const table_metadata *metadata,
rmm::cuda_stream_view stream)
: _id(id),
_str_id(str_id),
_string_type(col.type().id() == type_id::STRING),
_type_width(_string_type ? 0 : cudf::size_of(col.type())),
_data_count(col.size()),
_null_count(col.null_count()),
_data(col.head<uint8_t>() + col.offset() * _type_width),
_nulls(col.null_mask()),
_column_offset(col.offset()),
_clockscale(to_clockscale<uint8_t>(col.type().id())),
_type_kind(to_orc_type(col.type().id()))
{
if (_string_type && _data_count > 0) {
strings_column_view view{col};
_indexes = rmm::device_buffer(_data_count * sizeof(gpu::nvstrdesc_s), stream);
stringdata_to_nvstrdesc<<<((_data_count - 1) >> 8) + 1, 256, 0, stream.value()>>>(
static_cast<gpu::nvstrdesc_s *>(_indexes.data()),
view.offsets().data<size_type>() + view.offset(),
view.chars().data<char>(),
_nulls,
_column_offset,
_data_count);
_data = _indexes.data();
stream.synchronize();
}
// Generating default name if name isn't present in metadata
if (metadata && _id < metadata->column_names.size()) {
_name = metadata->column_names[_id];
} else {
_name = "_col" + std::to_string(_id);
}
}
auto is_string() const noexcept { return _string_type; }
void set_dict_stride(size_t stride) noexcept { dict_stride = stride; }
auto get_dict_stride() const noexcept { return dict_stride; }
/**
* @brief Function that associates an existing dictionary chunk allocation
*/
void attach_dict_chunk(gpu::DictionaryChunk *host_dict, gpu::DictionaryChunk *dev_dict)
{
dict = host_dict;
d_dict = dev_dict;
}
auto host_dict_chunk(size_t rowgroup) const
{
assert(_string_type);
return &dict[rowgroup * dict_stride + _str_id];
}
auto device_dict_chunk() const { return d_dict; }
/**
* @brief Function that associates an existing stripe dictionary allocation
*/
void attach_stripe_dict(gpu::StripeDictionary *host_stripe_dict,
gpu::StripeDictionary *dev_stripe_dict)
{
stripe_dict = host_stripe_dict;
d_stripe_dict = dev_stripe_dict;
}
auto host_stripe_dict(size_t stripe) const
{
assert(_string_type);
return &stripe_dict[stripe * dict_stride + _str_id];
}
auto device_stripe_dict() const { return d_stripe_dict; }
auto id() const noexcept { return _id; }
size_t type_width() const noexcept { return _type_width; }
size_t data_count() const noexcept { return _data_count; }
size_t null_count() const noexcept { return _null_count; }
bool nullable() const noexcept { return (_nulls != nullptr); }
void const *data() const noexcept { return _data; }
uint32_t const *nulls() const noexcept { return _nulls; }
size_type column_offset() const noexcept { return _column_offset; }
uint8_t clockscale() const noexcept { return _clockscale; }
void set_orc_encoding(ColumnEncodingKind e) { _encoding_kind = e; }
auto orc_kind() const noexcept { return _type_kind; }
auto orc_encoding() const noexcept { return _encoding_kind; }
auto orc_name() const noexcept { return _name; }
private:
// Identifier within set of columns and string columns, respectively
size_t _id = 0;
size_t _str_id = 0;
bool _string_type = false;
size_t _type_width = 0;
size_t _data_count = 0;
size_t _null_count = 0;
void const *_data = nullptr;
uint32_t const *_nulls = nullptr;
size_type _column_offset = 0;
uint8_t _clockscale = 0;
// ORC-related members
std::string _name{};
TypeKind _type_kind;
ColumnEncodingKind _encoding_kind;
// String dictionary-related members
rmm::device_buffer _indexes;
size_t dict_stride = 0;
gpu::DictionaryChunk const *dict = nullptr;
gpu::StripeDictionary const *stripe_dict = nullptr;
gpu::DictionaryChunk *d_dict = nullptr;
gpu::StripeDictionary *d_stripe_dict = nullptr;
};
std::vector<stripe_rowgroups> writer::impl::gather_stripe_info(
host_span<orc_column_view const> columns, size_t num_rowgroups)
{
auto const is_any_column_string =
std::any_of(columns.begin(), columns.end(), [](auto const &col) { return col.is_string(); });
// Apply rows per stripe limit to limit string dictionaries
size_t const max_stripe_rows = is_any_column_string ? 1000000 : 5000000;
std::vector<stripe_rowgroups> infos;
for (size_t rowgroup = 0, stripe_start = 0, stripe_size = 0; rowgroup < num_rowgroups;
++rowgroup) {
auto const rowgroup_size =
std::accumulate(columns.begin(), columns.end(), 0ul, [&](size_t total_size, auto const &col) {
if (col.is_string()) {
const auto dt = col.host_dict_chunk(rowgroup);
return total_size + row_index_stride_ + dt->string_char_count;
} else {
return total_size + col.type_width() * row_index_stride_;
}
});
if ((rowgroup > stripe_start) &&
(stripe_size + rowgroup_size > max_stripe_size_ ||
(rowgroup + 1 - stripe_start) * row_index_stride_ > max_stripe_rows)) {
infos.emplace_back(infos.size(), stripe_start, rowgroup - stripe_start);
stripe_start = rowgroup;
stripe_size = 0;
}
stripe_size += rowgroup_size;
if (rowgroup + 1 == num_rowgroups) {
infos.emplace_back(infos.size(), stripe_start, num_rowgroups - stripe_start);
}
}
return infos;
}
void writer::impl::init_dictionaries(orc_column_view *columns,
std::vector<int> const &str_col_ids,
uint32_t *dict_data,
uint32_t *dict_index,
hostdevice_vector<gpu::DictionaryChunk> *dict)
{
const size_t num_rowgroups = dict->size() / str_col_ids.size();
// Setup per-rowgroup dictionary indexes for each dictionary-aware column
for (size_t i = 0; i < str_col_ids.size(); ++i) {
auto &str_column = columns[str_col_ids[i]];
str_column.set_dict_stride(str_col_ids.size());
str_column.attach_dict_chunk(dict->host_ptr(), dict->device_ptr());
for (size_t g = 0; g < num_rowgroups; g++) {
auto *ck = &(*dict)[g * str_col_ids.size() + i];
ck->valid_map_base = str_column.nulls();
ck->column_offset = str_column.column_offset();
ck->column_data_base = str_column.data();
ck->dict_data = dict_data + i * str_column.data_count() + g * row_index_stride_;
ck->dict_index = dict_index + i * str_column.data_count(); // Indexed by abs row
ck->start_row = g * row_index_stride_;
ck->num_rows = std::min<uint32_t>(row_index_stride_,
std::max<int>(str_column.data_count() - ck->start_row, 0));
ck->num_strings = 0;
ck->string_char_count = 0;
ck->num_dict_strings = 0;
ck->dict_char_count = 0;
}
}
dict->host_to_device(stream);
gpu::InitDictionaryIndices(dict->device_ptr(), str_col_ids.size(), num_rowgroups, stream);
dict->device_to_host(stream, true);
}
void writer::impl::build_dictionaries(orc_column_view *columns,
std::vector<int> const &str_col_ids,
host_span<stripe_rowgroups const> stripe_bounds,
hostdevice_vector<gpu::DictionaryChunk> const &dict,
uint32_t *dict_index,
hostdevice_vector<gpu::StripeDictionary> &stripe_dict)
{
const auto num_rowgroups = dict.size() / str_col_ids.size();
for (size_t col_idx = 0; col_idx < str_col_ids.size(); ++col_idx) {
auto &str_column = columns[str_col_ids[col_idx]];
str_column.attach_stripe_dict(stripe_dict.host_ptr(), stripe_dict.device_ptr());
for (auto const &stripe : stripe_bounds) {
auto &sd = stripe_dict[stripe.id * str_col_ids.size() + col_idx];
sd.column_data_base = str_column.host_dict_chunk(0)->column_data_base;
sd.dict_data = str_column.host_dict_chunk(stripe.first)->dict_data;
sd.dict_index = dict_index + col_idx * str_column.data_count(); // Indexed by abs row
sd.column_id = str_col_ids[col_idx];
sd.start_chunk = stripe.first;
sd.num_chunks = stripe.size;
sd.dict_char_count = 0;
sd.num_strings =
std::accumulate(stripe.cbegin(), stripe.cend(), 0, [&](auto dt_str_cnt, auto rg_idx) {
const auto &dt = dict[rg_idx * str_col_ids.size() + col_idx];
return dt_str_cnt + dt.num_dict_strings;
});
}
if (enable_dictionary_) {
struct string_column_cost {
size_t direct = 0;
size_t dictionary = 0;
};
auto const col_cost =
std::accumulate(stripe_bounds.front().cbegin(),
stripe_bounds.back().cend(),
string_column_cost{},
[&](auto cost, auto rg_idx) -> string_column_cost {
const auto &dt = dict[rg_idx * str_col_ids.size() + col_idx];
return {cost.dictionary + dt.dict_char_count + dt.num_dict_strings,
cost.direct + dt.string_char_count};
});
// Disable dictionary if it does not reduce the output size
if (col_cost.dictionary >= col_cost.direct) {
for (auto const &stripe : stripe_bounds) {
stripe_dict[stripe.id * str_col_ids.size() + col_idx].dict_data = nullptr;
}
}
}
}
stripe_dict.host_to_device(stream);
gpu::BuildStripeDictionaries(stripe_dict.device_ptr(),
stripe_dict.host_ptr(),
dict.device_ptr(),
stripe_bounds.size(),
num_rowgroups,
str_col_ids.size(),
stream);
stripe_dict.device_to_host(stream, true);
}
orc_streams writer::impl::create_streams(host_span<orc_column_view> columns,
host_span<stripe_rowgroups const> stripe_bounds)
{
// First n + 1 streams are row index streams, including 'column 0'
std::vector<Stream> streams{{ROW_INDEX, 0, 0}}; // TODO: Separate index and data streams?
streams.resize(columns.size() + 1);
std::vector<int32_t> ids(columns.size() * gpu::CI_NUM_STREAMS, -1);
for (auto &column : columns) {
TypeKind kind = column.orc_kind();
StreamKind data_kind = DATA;
StreamKind data2_kind = LENGTH;
ColumnEncodingKind encoding_kind = DIRECT;
int64_t present_stream_size = 0;
int64_t data_stream_size = 0;
int64_t data2_stream_size = 0;
int64_t dict_stream_size = 0;
auto const is_nullable = [&]() {
if (single_write_mode) {
return column.nullable();
} else {
return (column.id() < user_metadata_with_nullability.column_nullable.size())
? user_metadata_with_nullability.column_nullable[column.id()]
: true;
}
}();
if (is_nullable) {
present_stream_size = ((row_index_stride_ + 7) >> 3);
present_stream_size += (present_stream_size + 0x7f) >> 7;
}
switch (kind) {
case TypeKind::BOOLEAN:
data_stream_size = div_rowgroups_by<int64_t>(1024) * (128 + 1);
encoding_kind = DIRECT;
break;
case TypeKind::BYTE:
data_stream_size = div_rowgroups_by<int64_t>(128) * (128 + 1);
encoding_kind = DIRECT;
break;
case TypeKind::SHORT:
data_stream_size = div_rowgroups_by<int64_t>(512) * (512 * 2 + 2);
encoding_kind = DIRECT_V2;
break;
case TypeKind::FLOAT:
// Pass through if no nulls (no RLE encoding for floating point)
data_stream_size =
(column.null_count() != 0) ? div_rowgroups_by<int64_t>(512) * (512 * 4 + 2) : INT64_C(-1);
encoding_kind = DIRECT;
break;
case TypeKind::INT:
case TypeKind::DATE:
data_stream_size = div_rowgroups_by<int64_t>(512) * (512 * 4 + 2);
encoding_kind = DIRECT_V2;
break;
case TypeKind::DOUBLE:
// Pass through if no nulls (no RLE encoding for floating point)
data_stream_size =
(column.null_count() != 0) ? div_rowgroups_by<int64_t>(512) * (512 * 8 + 2) : INT64_C(-1);
encoding_kind = DIRECT;
break;
case TypeKind::LONG:
data_stream_size = div_rowgroups_by<int64_t>(512) * (512 * 8 + 2);
encoding_kind = DIRECT_V2;
break;
case TypeKind::STRING: {
bool enable_dict = enable_dictionary_;
size_t dict_data_size = 0;
size_t dict_strings = 0;
size_t dict_lengths_div512 = 0;
for (auto const &stripe : stripe_bounds) {
const auto sd = column.host_stripe_dict(stripe.id);
enable_dict = (enable_dict && sd->dict_data != nullptr);
if (enable_dict) {
dict_strings += sd->num_strings;
dict_lengths_div512 += (sd->num_strings + 0x1ff) >> 9;
dict_data_size += sd->dict_char_count;
}
}
auto const direct_data_size =
std::accumulate(stripe_bounds.front().cbegin(),
stripe_bounds.back().cend(),
size_t{0},
[&](auto data_size, auto rg_idx) {
return data_size + column.host_dict_chunk(rg_idx)->string_char_count;
});
if (enable_dict) {
uint32_t dict_bits = 0;
for (dict_bits = 1; dict_bits < 32; dict_bits <<= 1) {
if (dict_strings <= (1ull << dict_bits)) break;
}
const auto valid_count = column.data_count() - column.null_count();
dict_data_size += (dict_bits * valid_count + 7) >> 3;
}
// Decide between direct or dictionary encoding
if (enable_dict && dict_data_size < direct_data_size) {
data_stream_size = div_rowgroups_by<int64_t>(512) * (512 * 4 + 2);
data2_stream_size = dict_lengths_div512 * (512 * 4 + 2);
dict_stream_size = std::max<size_t>(dict_data_size, 1);
encoding_kind = DICTIONARY_V2;
} else {
data_stream_size = std::max<size_t>(direct_data_size, 1);
data2_stream_size = div_rowgroups_by<int64_t>(512) * (512 * 4 + 2);
encoding_kind = DIRECT_V2;
}
break;
}
case TypeKind::TIMESTAMP:
data_stream_size = ((row_index_stride_ + 0x1ff) >> 9) * (512 * 4 + 2);
data2_stream_size = data_stream_size;
data2_kind = SECONDARY;
encoding_kind = DIRECT_V2;
break;
default: CUDF_FAIL("Unsupported ORC type kind");
}
// Initialize the column's metadata (this is the only reason columns is in/out param)
column.set_orc_encoding(encoding_kind);
// Initialize the column's index stream
const auto id = static_cast<uint32_t>(1 + column.id());
streams[id].column = id;
streams[id].kind = ROW_INDEX;
streams[id].length = 0;
// Initialize the column's data stream(s)
const auto base = column.id() * gpu::CI_NUM_STREAMS;
if (present_stream_size != 0) {
auto len = static_cast<uint64_t>(present_stream_size);
ids[base + gpu::CI_PRESENT] = streams.size();
streams.push_back(orc::Stream{PRESENT, id, len});
}
if (data_stream_size != 0) {
auto len = static_cast<uint64_t>(std::max<int64_t>(data_stream_size, 0));
ids[base + gpu::CI_DATA] = streams.size();
streams.push_back(orc::Stream{data_kind, id, len});
}
if (data2_stream_size != 0) {
auto len = static_cast<uint64_t>(std::max<int64_t>(data2_stream_size, 0));
ids[base + gpu::CI_DATA2] = streams.size();
streams.push_back(orc::Stream{data2_kind, id, len});
}
if (dict_stream_size != 0) {
auto len = static_cast<uint64_t>(dict_stream_size);
ids[base + gpu::CI_DICTIONARY] = streams.size();
streams.push_back(orc::Stream{DICTIONARY_DATA, id, len});
}
}
return {std::move(streams), std::move(ids)};
}
orc_streams::orc_stream_offsets orc_streams::compute_offsets(
host_span<orc_column_view const> columns, size_t num_rowgroups) const
{
std::vector<size_t> strm_offsets(streams.size());
size_t str_data_size = 0;
size_t rle_data_size = 0;
for (size_t i = 0; i < streams.size(); ++i) {
const auto &stream = streams[i];
const auto &column = columns[stream.column - 1];
if (((stream.kind == DICTIONARY_DATA || stream.kind == LENGTH) &&
(column.orc_encoding() == DICTIONARY_V2)) ||
((stream.kind == DATA) &&
(column.orc_kind() == TypeKind::STRING && column.orc_encoding() == DIRECT_V2))) {
strm_offsets[i] = str_data_size;
str_data_size += stream.length;
} else {
strm_offsets[i] = rle_data_size;
rle_data_size += (stream.length * num_rowgroups + 7) & ~7;
}
}
str_data_size = (str_data_size + 7) & ~7;
return {std::move(strm_offsets), str_data_size, rle_data_size};
}
struct segmented_valid_cnt_input {
bitmask_type const *mask;
std::vector<size_type> indices;
};
encoded_data writer::impl::encode_columns(host_span<orc_column_view const> columns,
std::vector<int> const &str_col_ids,
host_span<stripe_rowgroups const> stripe_bounds,
orc_streams const &streams)
{
auto const num_columns = columns.size();
auto const num_rowgroups = stripes_size(stripe_bounds);
hostdevice_2dvector<gpu::EncChunk> chunks(num_columns, num_rowgroups);
hostdevice_2dvector<gpu::encoder_chunk_streams> chunk_streams(num_columns, num_rowgroups);
auto const stream_offsets = streams.compute_offsets(columns, num_rowgroups);
rmm::device_uvector<uint8_t> encoded_data(stream_offsets.data_size(), stream);
// Initialize column chunks' descriptions
std::map<size_type, segmented_valid_cnt_input> validity_check_inputs;
for (auto const &column : columns) {
for (auto const &stripe : stripe_bounds) {
for (auto rg_idx_it = stripe.cbegin(); rg_idx_it < stripe.cend(); ++rg_idx_it) {
auto const rg_idx = *rg_idx_it;
auto &ck = chunks[column.id()][rg_idx];
ck.start_row = (rg_idx * row_index_stride_);
ck.num_rows = std::min<uint32_t>(row_index_stride_, column.data_count() - ck.start_row);
ck.valid_rows = column.data_count();
ck.encoding_kind = column.orc_encoding();
ck.type_kind = column.orc_kind();
if (ck.type_kind == TypeKind::STRING) {
ck.valid_map_base = column.nulls();
ck.column_offset = column.column_offset();
ck.column_data_base = (ck.encoding_kind == DICTIONARY_V2)
? column.host_stripe_dict(stripe.id)->dict_index
: column.data();
ck.dtype_len = 1;
} else {
ck.valid_map_base = column.nulls();
ck.column_offset = column.column_offset();
ck.column_data_base = column.data();
ck.dtype_len = column.type_width();
}
ck.scale = column.clockscale();
// Only need to check row groups that end within the stripe
}
}
}
auto validity_check_indices = [&](size_t col_idx) {
std::vector<size_type> indices;
for (auto const &stripe : stripe_bounds) {
for (auto rg_idx_it = stripe.cbegin(); rg_idx_it < stripe.cend() - 1; ++rg_idx_it) {
auto const &chunk = chunks[col_idx][*rg_idx_it];
indices.push_back(chunk.start_row);
indices.push_back(chunk.start_row + chunk.num_rows);
}
}
return indices;
};
for (auto const &column : columns) {
if (column.orc_kind() == TypeKind::BOOLEAN && column.nullable()) {
validity_check_inputs[column.id()] = {column.nulls(), validity_check_indices(column.id())};
}
}
for (auto &cnt_in : validity_check_inputs) {
auto const valid_counts = segmented_count_set_bits(cnt_in.second.mask, cnt_in.second.indices);
CUDF_EXPECTS(std::none_of(valid_counts.cbegin(),
valid_counts.cend(),
[](auto valid_count) { return valid_count % 8; }),
"There's currently a bug in encoding boolean columns. Suggested workaround "
"is to convert "
"to "
"int8 type. Please see https://github.com/rapidsai/cudf/issues/6763 for "
"more information.");
}
for (size_t col_idx = 0; col_idx < num_columns; col_idx++) {
auto const &column = columns[col_idx];
auto col_streams = chunk_streams[col_idx];
for (auto const &stripe : stripe_bounds) {
for (auto rg_idx_it = stripe.cbegin(); rg_idx_it < stripe.cend(); ++rg_idx_it) {
auto const rg_idx = *rg_idx_it;
auto const &ck = chunks[col_idx][rg_idx];
auto &strm = col_streams[rg_idx];
for (int strm_type = 0; strm_type < gpu::CI_NUM_STREAMS; ++strm_type) {
auto const strm_id = streams.id(col_idx * gpu::CI_NUM_STREAMS + strm_type);
strm.ids[strm_type] = strm_id;
if (strm_id >= 0) {
if ((strm_type == gpu::CI_DICTIONARY) ||
(strm_type == gpu::CI_DATA2 && ck.encoding_kind == DICTIONARY_V2)) {
if (rg_idx_it == stripe.cbegin()) {
const int32_t dict_stride = column.get_dict_stride();
const auto stripe_dict = column.host_stripe_dict(stripe.id);
strm.lengths[strm_type] =
(strm_type == gpu::CI_DICTIONARY)
? stripe_dict->dict_char_count
: (((stripe_dict->num_strings + 0x1ff) >> 9) * (512 * 4 + 2));
if (stripe.id == 0) {
strm.data_ptrs[strm_type] = encoded_data.data() + stream_offsets.offsets[strm_id];
} else {
auto const &strm_up = col_streams[stripe_dict[-dict_stride].start_chunk];
strm.data_ptrs[strm_type] =
strm_up.data_ptrs[strm_type] + strm_up.lengths[strm_type];
}
} else {
strm.lengths[strm_type] = 0;
strm.data_ptrs[strm_type] = col_streams[rg_idx - 1].data_ptrs[strm_type];
}
} else if (strm_type == gpu::CI_DATA && ck.type_kind == TypeKind::STRING &&
ck.encoding_kind == DIRECT_V2) {
strm.lengths[strm_type] = column.host_dict_chunk(rg_idx)->string_char_count;
auto const &prev_strm = col_streams[rg_idx - 1];
strm.data_ptrs[strm_type] =
(rg_idx == 0) ? encoded_data.data() + stream_offsets.offsets[strm_id]
: (prev_strm.data_ptrs[strm_type] + prev_strm.lengths[strm_type]);
} else if (strm_type == gpu::CI_DATA && streams[strm_id].length == 0 &&
(ck.type_kind == DOUBLE || ck.type_kind == FLOAT)) {
// Pass-through
strm.lengths[strm_type] = ck.num_rows * ck.dtype_len;
strm.data_ptrs[strm_type] = nullptr;
} else {
strm.lengths[strm_type] = streams[strm_id].length;
strm.data_ptrs[strm_type] = encoded_data.data() + stream_offsets.str_data_size +
stream_offsets.offsets[strm_id] +
streams[strm_id].length * rg_idx;
}
} else {
strm.lengths[strm_type] = 0;
strm.data_ptrs[strm_type] = nullptr;
}
}
}
}
}
chunks.host_to_device(stream);
chunk_streams.host_to_device(stream);
if (!str_col_ids.empty()) {
auto d_stripe_dict = columns[str_col_ids[0]].device_stripe_dict();
gpu::EncodeStripeDictionaries(
d_stripe_dict, chunks, str_col_ids.size(), stripe_bounds.size(), chunk_streams, stream);
}
gpu::EncodeOrcColumnData(chunks, chunk_streams, stream);
stream.synchronize();
return {std::move(encoded_data), std::move(chunk_streams)};
}
std::vector<StripeInformation> writer::impl::gather_stripes(
size_t num_rows,
size_t num_index_streams,
host_span<stripe_rowgroups const> stripe_bounds,
hostdevice_2dvector<gpu::encoder_chunk_streams> *enc_streams,
hostdevice_2dvector<gpu::StripeStream> *strm_desc)
{
std::vector<StripeInformation> stripes(stripe_bounds.size());
for (auto const &stripe : stripe_bounds) {
for (size_t col_idx = 0; col_idx < enc_streams->size().first; col_idx++) {
const auto &strm = (*enc_streams)[col_idx][stripe.first];
// Assign stream data of column data stream(s)
for (int k = 0; k < gpu::CI_INDEX; k++) {
const auto stream_id = strm.ids[k];
if (stream_id != -1) {
auto *ss = &(*strm_desc)[stripe.id][stream_id - num_index_streams];
ss->stream_size = 0;
ss->first_chunk_id = stripe.first;
ss->num_chunks = stripe.size;
ss->column_id = col_idx;
ss->stream_type = k;
}
}
}
auto const stripe_group_end = *stripe.cend();
auto const stripe_end = std::min(stripe_group_end * row_index_stride_, num_rows);
stripes[stripe.id].numberOfRows = stripe_end - stripe.first * row_index_stride_;
}
strm_desc->host_to_device(stream);
gpu::CompactOrcDataStreams(*strm_desc, *enc_streams, stream);
strm_desc->device_to_host(stream);
enc_streams->device_to_host(stream, true);
return stripes;
}
std::vector<std::vector<uint8_t>> writer::impl::gather_statistic_blobs(
const table_device_view &table,
host_span<orc_column_view const> columns,
host_span<stripe_rowgroups const> stripe_bounds)
{
auto const num_rowgroups = stripes_size(stripe_bounds);
size_t num_stat_blobs = (1 + stripe_bounds.size()) * columns.size();
size_t num_chunks = num_rowgroups * columns.size();
std::vector<std::vector<uint8_t>> stat_blobs(num_stat_blobs);
hostdevice_vector<stats_column_desc> stat_desc(columns.size());
hostdevice_vector<statistics_merge_group> stat_merge(num_stat_blobs);
rmm::device_uvector<statistics_chunk> stat_chunks(num_chunks + num_stat_blobs, stream);
rmm::device_uvector<statistics_group> stat_groups(num_chunks, stream);
for (auto const &column : columns) {
stats_column_desc *desc = &stat_desc[column.id()];
switch (column.orc_kind()) {
case TypeKind::BYTE: desc->stats_dtype = dtype_int8; break;
case TypeKind::SHORT: desc->stats_dtype = dtype_int16; break;
case TypeKind::INT: desc->stats_dtype = dtype_int32; break;
case TypeKind::LONG: desc->stats_dtype = dtype_int64; break;
case TypeKind::FLOAT: desc->stats_dtype = dtype_float32; break;
case TypeKind::DOUBLE: desc->stats_dtype = dtype_float64; break;
case TypeKind::BOOLEAN: desc->stats_dtype = dtype_bool; break;
case TypeKind::DATE: desc->stats_dtype = dtype_int32; break;
case TypeKind::TIMESTAMP: desc->stats_dtype = dtype_timestamp64; break;
case TypeKind::STRING: desc->stats_dtype = dtype_string; break;
default: desc->stats_dtype = dtype_none; break;
}
desc->num_rows = column.data_count();
desc->num_values = column.data_count();
desc->valid_map_base = column.nulls();
desc->column_offset = column.column_offset();
desc->column_data_base = column.data();
if (desc->stats_dtype == dtype_timestamp64) {
// Timestamp statistics are in milliseconds
switch (column.clockscale()) {
case 9: desc->ts_scale = 1000; break;
case 6: desc->ts_scale = 0; break;
case 3: desc->ts_scale = -1000; break;
case 0: desc->ts_scale = -1000000; break;
default: desc->ts_scale = 0; break;
}
} else {
desc->ts_scale = 0;
}
for (auto const &stripe : stripe_bounds) {
auto grp = &stat_merge[column.id() * stripe_bounds.size() + stripe.id];
grp->col = stat_desc.device_ptr(column.id());
grp->start_chunk = static_cast<uint32_t>(column.id() * num_rowgroups + stripe.first);
grp->num_chunks = stripe.size;
}
statistics_merge_group *col_stats =
&stat_merge[stripe_bounds.size() * columns.size() + column.id()];
col_stats->col = stat_desc.device_ptr(column.id());
col_stats->start_chunk = static_cast<uint32_t>(column.id() * stripe_bounds.size());
col_stats->num_chunks = static_cast<uint32_t>(stripe_bounds.size());
}
stat_desc.host_to_device(stream);
stat_merge.host_to_device(stream);
rmm::device_uvector<column_device_view> leaf_column_views =
create_leaf_column_device_views<stats_column_desc>(stat_desc, table, stream);
gpu::orc_init_statistics_groups(stat_groups.data(),
stat_desc.device_ptr(),
columns.size(),
num_rowgroups,
row_index_stride_,
stream);
GatherColumnStatistics(stat_chunks.data(), stat_groups.data(), num_chunks, stream);
MergeColumnStatistics(stat_chunks.data() + num_chunks,
stat_chunks.data(),
stat_merge.device_ptr(),
stripe_bounds.size() * columns.size(),
stream);
MergeColumnStatistics(stat_chunks.data() + num_chunks + stripe_bounds.size() * columns.size(),
stat_chunks.data() + num_chunks,
stat_merge.device_ptr(stripe_bounds.size() * columns.size()),
columns.size(),
stream);
gpu::orc_init_statistics_buffersize(
stat_merge.device_ptr(), stat_chunks.data() + num_chunks, num_stat_blobs, stream);
stat_merge.device_to_host(stream, true);
hostdevice_vector<uint8_t> blobs(stat_merge[num_stat_blobs - 1].start_chunk +
stat_merge[num_stat_blobs - 1].num_chunks);
gpu::orc_encode_statistics(blobs.device_ptr(),
stat_merge.device_ptr(),
stat_chunks.data() + num_chunks,
num_stat_blobs,
stream);
stat_merge.device_to_host(stream);
blobs.device_to_host(stream, true);
for (size_t i = 0; i < num_stat_blobs; i++) {
const uint8_t *stat_begin = blobs.host_ptr(stat_merge[i].start_chunk);
const uint8_t *stat_end = stat_begin + stat_merge[i].num_chunks;
stat_blobs[i].assign(stat_begin, stat_end);
}
return stat_blobs;
}
void writer::impl::write_index_stream(int32_t stripe_id,
int32_t stream_id,
host_span<orc_column_view const> columns,
stripe_rowgroups const &rowgroups_range,
host_2dspan<gpu::encoder_chunk_streams const> enc_streams,
host_2dspan<gpu::StripeStream const> strm_desc,
host_span<gpu_inflate_status_s const> comp_out,
StripeInformation *stripe,
orc_streams *streams,
ProtobufWriter *pbw)
{
row_group_index_info present;
row_group_index_info data;
row_group_index_info data2;
auto kind = TypeKind::STRUCT;
auto const column_id = stream_id - 1;
auto find_record = [=, &strm_desc](gpu::encoder_chunk_streams const &stream,
gpu::StreamIndexType type) {
row_group_index_info record;
if (stream.ids[type] > 0) {
record.pos = 0;
if (compression_kind_ != NONE) {
auto const &ss = strm_desc[stripe_id][stream.ids[type] - (columns.size() + 1)];
record.blk_pos = ss.first_block;
record.comp_pos = 0;
record.comp_size = ss.stream_size;
}
}
return record;
};
auto scan_record = [=, &comp_out](gpu::encoder_chunk_streams const &stream,
gpu::StreamIndexType type,
row_group_index_info &record) {
if (record.pos >= 0) {
record.pos += stream.lengths[type];
while ((record.pos >= 0) && (record.blk_pos >= 0) &&
(static_cast<size_t>(record.pos) >= compression_blocksize_) &&
(record.comp_pos + 3 + comp_out[record.blk_pos].bytes_written <
static_cast<size_t>(record.comp_size))) {
record.pos -= compression_blocksize_;
record.comp_pos += 3 + comp_out[record.blk_pos].bytes_written;
record.blk_pos += 1;
}
}
};
// TBD: Not sure we need an empty index stream for column 0
if (stream_id != 0) {
const auto &strm = enc_streams[column_id][0];
present = find_record(strm, gpu::CI_PRESENT);
data = find_record(strm, gpu::CI_DATA);
data2 = find_record(strm, gpu::CI_DATA2);
// Change string dictionary to int from index point of view
kind = columns[column_id].orc_kind();
if (kind == TypeKind::STRING && columns[column_id].orc_encoding() == DICTIONARY_V2) {
kind = TypeKind::INT;
}
}
buffer_.resize((compression_kind_ != NONE) ? 3 : 0);
// Add row index entries
std::for_each(rowgroups_range.cbegin(), rowgroups_range.cend(), [&](auto rowgroup) {
pbw->put_row_index_entry(
present.comp_pos, present.pos, data.comp_pos, data.pos, data2.comp_pos, data2.pos, kind);
if (stream_id != 0) {
const auto &strm = enc_streams[column_id][rowgroup];
scan_record(strm, gpu::CI_PRESENT, present);
scan_record(strm, gpu::CI_DATA, data);
scan_record(strm, gpu::CI_DATA2, data2);
}
});
(*streams)[stream_id].length = buffer_.size();
if (compression_kind_ != NONE) {
uint32_t uncomp_ix_len = (uint32_t)((*streams)[stream_id].length - 3) * 2 + 1;
buffer_[0] = static_cast<uint8_t>(uncomp_ix_len >> 0);
buffer_[1] = static_cast<uint8_t>(uncomp_ix_len >> 8);
buffer_[2] = static_cast<uint8_t>(uncomp_ix_len >> 16);
}
out_sink_->host_write(buffer_.data(), buffer_.size());
stripe->indexLength += buffer_.size();
}
void writer::impl::write_data_stream(gpu::StripeStream const &strm_desc,
gpu::encoder_chunk_streams const &enc_stream,
uint8_t const *compressed_data,
uint8_t *stream_out,
StripeInformation *stripe,
orc_streams *streams)
{
const auto length = strm_desc.stream_size;
(*streams)[enc_stream.ids[strm_desc.stream_type]].length = length;
if (length != 0) {
const auto *stream_in = (compression_kind_ == NONE)
? enc_stream.data_ptrs[strm_desc.stream_type]
: (compressed_data + strm_desc.bfr_offset);
CUDA_TRY(
cudaMemcpyAsync(stream_out, stream_in, length, cudaMemcpyDeviceToHost, stream.value()));
stream.synchronize();
out_sink_->host_write(stream_out, length);
}
stripe->dataLength += length;
}
void writer::impl::add_uncompressed_block_headers(std::vector<uint8_t> &v)