Skip to content

GH-40997: [C++] Get null_bit_id according to are_cols_in_encoding_order in NullUpdateColumnToRow_avx2 - #40998

Merged
pitrou merged 6 commits into
apache:mainfrom
ZhangHuiGui:fix-avx2-null-compare-condition
May 7, 2024
Merged

pitrou merged 6 commits into
apache:mainfrom
ZhangHuiGui:fix-avx2-null-compare-condition

Conversation

@ZhangHuiGui

@ZhangHuiGui ZhangHuiGui commented Apr 4, 2024 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Recently, we find that the compare internal's avx2 function NullUpdateColumnToRowImp_avx2 lost the are_cols_in_encoding_order check when get null_bit_id. It may cause grouper's compare result wrong(are_cols_in_encoding_order = true in grouper).

What changes are included in this PR?

Get null_bit_id according to are_cols_in_encoding_order in NullUpdateColumnToRow_avx2.

Are there any user-facing changes?

No

Co-authored-by laotan332 qinpengxiang@outlook.com
Co-authored-by ZhangHuiGui 2689496754@qq.com

@github-actions

github-actions Bot commented Apr 4, 2024

Copy link
Copy Markdown

⚠️ GitHub issue #40997 has been automatically assigned in GitHub to PR creator.

@ZhangHuiGui
ZhangHuiGui force-pushed the fix-avx2-null-compare-condition branch from dce2fa9 to ba0215e Compare April 4, 2024 06:23

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ColIdInEncodingOrder?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, nice name!

@mapleFU

mapleFU commented Apr 4, 2024

Copy link
Copy Markdown
Member

Any way is it hard to have a unittest here?

@ZhangHuiGui

ZhangHuiGui commented Apr 4, 2024 •

Copy link
Copy Markdown
Contributor Author

Any way is it hard to have a unittest here?

Yes, there are some very special cases need to be constructed here:

  1. The col-id order of key-columns in the constructed rowtable needs to be changed after FromColumnMetadataVector. That is, we need to construct columns that can support column-sort. This is easy(two array cols with int32() and boolean() are enough).
  2. The key problem is that entering columns with nulls requires a difference in the following codes when we use the current col_id or the reversed col_id in rowtable:
    for (uint32_t i = num_processed; i < num_rows_to_compare; ++i) {
    uint32_t irow_left = use_selection ? sel_left_maybe_null[i] : i;
    uint32_t irow_right = left_to_right_map[irow_left];
    int64_t bitid_right = irow_right * null_mask_num_bytes * 8 + null_bit_id;
    int right_null = bit_util::GetBit(null_masks, bitid_right) ? 0xff : 0;
    int left_null =
    bit_util::GetBit(non_nulls, irow_left + col.bit_offset(0)) ? 0 : 0xff;
    match_bytevector[i] |= left_null & right_null;
    match_bytevector[i] &= ~(left_null ^ right_null);
    }

@ZhangHuiGui

Copy link
Copy Markdown
Contributor Author

2. The key problem is that entering columns with nulls requires a difference in the following codes when we use the current col_id or the reversed col_id in rowtable:

I will try to figure out a special data set to cover the null_bit_id related case. :)

@pitrou
pitrou requested a review from westonpace April 4, 2024 09:10
@westonpace

Copy link
Copy Markdown
Member

I will try to figure out a special data set to cover the null_bit_id related case. :)

The hash join tests cover many different scenarios with random data. Maybe changing the schema to require column re-ordering would be enough to trigger this bug?

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I question the merits of reordering the columns in the first place (I never saw benchmarks showing a clear benefit) but the change seems correct to me.

I agree that having a unit test (that detects the regression) in place would be helpful.

@ZhangHuiGui

Copy link
Copy Markdown
Contributor Author

The hash join tests cover many different scenarios with random data. Maybe changing the schema to require column re-ordering would be enough to trigger this bug?

Thanks, i'll try it.

@ZhangHuiGui

Copy link
Copy Markdown
Contributor Author

I question the merits of reordering the columns in the first place (I never saw benchmarks showing a clear benefit) but the change seems correct to me.

Yes, you're right. I'm confused too. Seems reordering the columns only used in grouper and we currently have no benchmark to prove its benefits, i'll take a look after this PR.

@ZhangHuiGui

Copy link
Copy Markdown
Contributor Author

@mapleFU @westonpace PTAL the test, it will cover the case.

@ZhangHuiGui
ZhangHuiGui force-pushed the fix-avx2-null-compare-condition branch 2 times, most recently from 890fdd3 to 07533f9 Compare April 5, 2024 06:24
@ZhangHuiGui

Copy link
Copy Markdown
Contributor Author

I question the merits of reordering the columns in the first place (I never saw benchmarks showing a clear benefit) but the change seems correct to me.

I've tested this, looks like a big performance improvement if use the resorted columns (perf shows it's benefit from easier vectorization).

origin columns order : string_values, i32_values, i64_values, fsb_values, bool_values.
Use resorted columns:

GrouperWithResortedColumns/1024/0            68.6 us         68.6 us        10057 bytes_per_second=14.2436Mi/s items_per_second=14.9355M/s null_percent=0 size=1.024k
GrouperWithResortedColumns/2048/0             138 us          138 us         4989 bytes_per_second=14.1325Mi/s items_per_second=14.819M/s null_percent=0 size=2.048k
GrouperWithResortedColumns/4096/0             297 us          297 us         2450 bytes_per_second=13.1419Mi/s items_per_second=13.7803M/s null_percent=0 size=4.096k
GrouperWithResortedColumns/8192/0             635 us          635 us         1026 bytes_per_second=12.3119Mi/s items_per_second=12.91M/s null_percent=0 size=8.192k
GrouperWithResortedColumns/16384/0           1899 us         1899 us          351 bytes_per_second=8.22997Mi/s items_per_second=8.62975M/s null_percent=0 size=16.384k
GrouperWithResortedColumns/32768/10000       4444 us         4441 us          155 bytes_per_second=7.03607Mi/s items_per_second=7.37785M/s null_percent=0.01 size=32.768k
GrouperWithResortedColumns/32768/100        11958 us        11953 us           71 bytes_per_second=2.61451Mi/s items_per_second=2.74151M/s null_percent=1 size=32.768k
GrouperWithResortedColumns/32768/10         29060 us        29048 us           34 bytes_per_second=1.07582Mi/s items_per_second=1.12808M/s null_percent=10 size=32.768k
GrouperWithResortedColumns/32768/2          46366 us        46347 us           30 bytes_per_second=690.451Ki/s items_per_second=707.022k/s null_percent=50 size=32.768k
GrouperWithResortedColumns/32768/1           1089 us         1089 us          644 bytes_per_second=28.699Mi/s items_per_second=30.0931M/s null_percent=100 size=32.768k
GrouperWithResortedColumns/32768/0           4273 us         4271 us          162 bytes_per_second=7.31762Mi/s items_per_second=7.67308M/s null_percent=0 size=32.768k

Use origin columns:

GrouperWithResortedColumns/1024/0            6628 us         6625 us          100 bytes_per_second=150.947Ki/s items_per_second=154.57k/s null_percent=0 size=1.024k
GrouperWithResortedColumns/2048/0           13385 us        13376 us          100 bytes_per_second=149.516Ki/s items_per_second=153.105k/s null_percent=0 size=2.048k
GrouperWithResortedColumns/4096/0           28524 us        28510 us          130 bytes_per_second=140.303Ki/s items_per_second=143.67k/s null_percent=0 size=4.096k
GrouperWithResortedColumns/8192/0           33133 us        33114 us           66 bytes_per_second=241.59Ki/s items_per_second=247.388k/s null_percent=0 size=8.192k
GrouperWithResortedColumns/16384/0          40724 us        40705 us           33 bytes_per_second=393.073Ki/s items_per_second=402.506k/s null_percent=0 size=16.384k
GrouperWithResortedColumns/32768/10000      57044 us        57008 us           17 bytes_per_second=561.327Ki/s items_per_second=574.799k/s null_percent=0.01 size=32.768k
GrouperWithResortedColumns/32768/100        56691 us        56668 us           16 bytes_per_second=564.691Ki/s items_per_second=578.244k/s null_percent=1 size=32.768k
GrouperWithResortedColumns/32768/10         56045 us        56024 us           17 bytes_per_second=571.188Ki/s items_per_second=584.897k/s null_percent=10 size=32.768k
GrouperWithResortedColumns/32768/2          53605 us        53577 us           25 bytes_per_second=597.276Ki/s items_per_second=611.61k/s null_percent=50 size=32.768k
GrouperWithResortedColumns/32768/1           1102 us         1102 us          643 bytes_per_second=28.3595Mi/s items_per_second=29.7371M/s null_percent=100 size=32.768k
GrouperWithResortedColumns/32768/0          54583 us        54560 us           17 bytes_per_second=586.508Ki/s items_per_second=600.585k/s null_percent=0 size=32.768k

I'll add more tests on it and push the benchmark in another pr.

@westonpace westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this, looks like a big performance improvement if use the resorted columns (perf shows it's benefit from easier vectorization).

Thank you for both the test and the benchmark!

@ZhangHuiGui
ZhangHuiGui force-pushed the fix-avx2-null-compare-condition branch from 07533f9 to b14cdf2 Compare April 10, 2024 14:45
@ZhangHuiGui

Copy link
Copy Markdown
Contributor Author

Can this PR be moved forward? We recently encountered this problem in Aggregate-Node. :(

@ZhangHuiGui

ZhangHuiGui commented Apr 16, 2024 •

Copy link
Copy Markdown
Contributor Author

I'll add more tests on it and push the benchmark in another pr.

There is a bug when set are_cols_in_encoding_order to false in grouper, in order to achieve the memory alignment required when SIMD instructions access data, the columns need to be sorted.

When i fix the are_cols_in_encoding_order=false 's bug locally, i find the performance almost same and the improvement from column resorted seems to be limited.
Details:
#41036 (comment)

@mapleFU

mapleFU commented Apr 16, 2024

Copy link
Copy Markdown
Member

cc @pitrou @zanmato1984 Would you mind take a look? I've check the logic here but I'm not familiar enough with here

@zanmato1984

Copy link
Copy Markdown
Contributor

Maybe a dumb question. Seems the column sorting will happen anyway in encoding [1], how can Grouper disrespect this fact and do the comparison with are_cols_in_encoding_order=true?

[1]


[2]
/* are_cols_in_encoding_order=*/true);

@ZhangHuiGui

Copy link
Copy Markdown
Contributor Author

are_cols_in_encoding_order=true

It's nice question!

Use are_cols_in_encoding_order=true in Grouper to perform correct comparison. This process can be broken down into the following three steps:

  1. Column sort
    After the column sorting in FromColumnMetadataVector is completed, an inverse_column_order will be generated to identify the column order after sorting. For example, [0,1,2] before sorting, and inverse_column_order will save [2, 1, 0] after sorting. At the same time, a column_offsets array will also be generated, used to identify the offset position of the sorted column in the row, so that the column data can be accessed in RowTableImpl, it should be noted that the order of this array is constructed according to the input column order.

  2. Encode columns into RowTableEncoder in sorted order

    encoder_.PrepareEncodeSelected(start_row, batch_size_next, cols_);

    column_order has been changed in step 1.
    void RowTableEncoder::PrepareKeyColumnArrays(int64_t start_row, int64_t num_rows,
    const std::vector<KeyColumnArray>& cols_in) {
    const auto num_cols = static_cast<uint32_t>(cols_in.size());
    DCHECK(batch_all_cols_.size() == num_cols);
    uint32_t num_varbinary_visited = 0;
    for (uint32_t i = 0; i < num_cols; ++i) {
    const KeyColumnArray& col = cols_in[row_metadata_.column_order[i]];
    KeyColumnArray col_window = col.Slice(start_row, num_rows);
    batch_all_cols_[i] = col_window;

  3. Compare the data in RowTableEncoder and RowTableImpl (at this time, the RowTableEncoder data in the grouper is already sorted).
    Compare through KeyCompare::CompareColumnsToRows, there are two main places where are_cols_in_encoding_order is used
    a. Get the column_offsets in step1. If are_cols_in_encoding_order = true in this process, you can directly use the sorting column to get the offset. The current column is already a sorted column; otherwise, you need to use inverse_column_order to access the offset.

    uint32_t offset_within_row = rows.metadata().encoded_field_offset(
    are_cols_in_encoding_order
    ? static_cast<uint32_t>(icol)
    : rows.metadata().pos_after_encoding(static_cast<uint32_t>(icol)));

    b. Get the position of the current column in null_mask. You also need to judge whether you need to take col-idx from inverse_column_order or directly use the input column as col-idx according to are_cols_in_encoding_order. The problem to be solved by the current PR is actually the loss in NullUpdateColumnToRowImp_avx2 The check for are_cols_in_encoding_order = true resulted in getting a wrong bit_id as a judgment condition for whether the corresponding column is empty.
    NullUpdateColumnToRow
    uint32_t null_bit_id =
    are_cols_in_encoding_order ? id_col : rows.metadata().pos_after_encoding(id_col);

    NullUpdateColumnToRowImp_avx2
    uint32_t null_bit_id = rows.metadata().pos_after_encoding(id_col);

@zanmato1984

Copy link
Copy Markdown
Contributor

are_cols_in_encoding_order=true

It's nice question!

Use are_cols_in_encoding_order=true in Grouper to perform correct comparison. This process can be broken down into the following three steps:

  1. Column sort
    After the column sorting in FromColumnMetadataVector is completed, an inverse_column_order will be generated to identify the column order after sorting. For example, [0,1,2] before sorting, and inverse_column_order will save [2, 1, 0] after sorting. At the same time, a column_offsets array will also be generated, used to identify the offset position of the sorted column in the row, so that the column data can be accessed in RowTableImpl, it should be noted that the order of this array is constructed according to the input column order.

  2. Encode columns into RowTableEncoder in sorted order

    encoder_.PrepareEncodeSelected(start_row, batch_size_next, cols_);

    column_order has been changed in step 1.

    void RowTableEncoder::PrepareKeyColumnArrays(int64_t start_row, int64_t num_rows,
    const std::vector<KeyColumnArray>& cols_in) {
    const auto num_cols = static_cast<uint32_t>(cols_in.size());
    DCHECK(batch_all_cols_.size() == num_cols);
    uint32_t num_varbinary_visited = 0;
    for (uint32_t i = 0; i < num_cols; ++i) {
    const KeyColumnArray& col = cols_in[row_metadata_.column_order[i]];
    KeyColumnArray col_window = col.Slice(start_row, num_rows);
    batch_all_cols_[i] = col_window;

  3. Compare the data in RowTableEncoder and RowTableImpl (at this time, the RowTableEncoder data in the grouper is already sorted).
    Compare through KeyCompare::CompareColumnsToRows, there are two main places where are_cols_in_encoding_order is used
    a. Get the column_offsets in step1. If are_cols_in_encoding_order = true in this process, you can directly use the sorting column to get the offset. The current column is already a sorted column; otherwise, you need to use inverse_column_order to access the offset.

    uint32_t offset_within_row = rows.metadata().encoded_field_offset(
    are_cols_in_encoding_order
    ? static_cast<uint32_t>(icol)
    : rows.metadata().pos_after_encoding(static_cast<uint32_t>(icol)));

    b. Get the position of the current column in null_mask. You also need to judge whether you need to take col-idx from inverse_column_order or directly use the input column as col-idx according to are_cols_in_encoding_order. The problem to be solved by the current PR is actually the loss in NullUpdateColumnToRowImp_avx2 The check for are_cols_in_encoding_order = true resulted in getting a wrong bit_id as a judgment condition for whether the corresponding column is empty.
    NullUpdateColumnToRow

    uint32_t null_bit_id =
    are_cols_in_encoding_order ? id_col : rows.metadata().pos_after_encoding(id_col);

    NullUpdateColumnToRowImp_avx2

    uint32_t null_bit_id = rows.metadata().pos_after_encoding(id_col);

Ah I see now, thank you for explaining this to me!

@zanmato1984 zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Quoting from your other PR [1]:

And also, the purpose of the column-sort in RowTableEncoder is construct an aligned friendly condition for memory access. Resorted order will be used to produce an aligned column_offset for access the row data in encode/decode/compare.

Since I saw different people confused about the column sorting, maybe you can update the comment in [2] with the quote.

[1] #41036 (comment)
[2]

@ZhangHuiGui
ZhangHuiGui force-pushed the fix-avx2-null-compare-condition branch from b14cdf2 to 74b4e9f Compare April 17, 2024 14:24

@zanmato1984 zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested comment tuning. Didn't format.

@ZhangHuiGui
ZhangHuiGui force-pushed the fix-avx2-null-compare-condition branch from 74b4e9f to d69e609 Compare April 17, 2024 15:25
@mapleFU

mapleFU commented May 6, 2024

Copy link
Copy Markdown
Member

@pitrou do you have some comment on this? If no negative comment, I'll merge this patch in 3days

@ZhangHuiGui
ZhangHuiGui force-pushed the fix-avx2-null-compare-condition branch from d69e609 to ff0cea2 Compare May 7, 2024 02:30
@pitrou

pitrou commented May 7, 2024

Copy link
Copy Markdown
Member

@github-actions crossbow submit -g cpp

@pitrou pitrou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update!

@github-actions

github-actions Bot commented May 7, 2024

Copy link
Copy Markdown

Revision: ff0cea2

Submitted crossbow builds: ursacomputing/crossbow @ actions-21cd06c87f

Task Status
test-alpine-linux-cpp GitHub Actions
test-build-cpp-fuzz GitHub Actions
test-conda-cpp GitHub Actions
test-conda-cpp-valgrind GitHub Actions
test-cuda-cpp GitHub Actions
test-debian-12-cpp-amd64 GitHub Actions
test-debian-12-cpp-i386 GitHub Actions
test-fedora-39-cpp GitHub Actions
test-ubuntu-20.04-cpp GitHub Actions
test-ubuntu-20.04-cpp-bundled GitHub Actions
test-ubuntu-20.04-cpp-minimal-with-formats GitHub Actions
test-ubuntu-20.04-cpp-thread-sanitizer GitHub Actions
test-ubuntu-22.04-cpp GitHub Actions
test-ubuntu-22.04-cpp-20 GitHub Actions
test-ubuntu-22.04-cpp-emscripten GitHub Actions
test-ubuntu-22.04-cpp-no-threading GitHub Actions
test-ubuntu-24.04-cpp GitHub Actions
test-ubuntu-24.04-cpp-gcc-14 GitHub Actions

@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 7 benchmarking runs that have been run so far on merge-commit 5232137.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 44 possible false positives for unstable benchmarks that are known to sometimes produce them.

@ZhangHuiGui
ZhangHuiGui deleted the fix-avx2-null-compare-condition branch May 8, 2024 12:52
pitrou pushed a commit that referenced this pull request May 21, 2024
…gression (#41036)

### Rationale for this change
Add a grouper benchmark for preventing performance regression . 

#40998 (comment).

### What changes are included in this PR?
Added a benchmark.

### Are these changes tested?
Needn't.

### Are there any user-facing changes?
No

* GitHub Issue: #41035

Authored-by: ZhangHuiGui <hugo.zhang@openpie.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
vibhatha pushed a commit to vibhatha/arrow that referenced this pull request May 25, 2024
…ng_order in NullUpdateColumnToRow_avx2 (apache#40998)

### Rationale for this change
Recently, we find that the compare internal's avx2 function NullUpdateColumnToRowImp_avx2 lost the are_cols_in_encoding_order check when get null_bit_id. It may cause grouper's compare result wrong(are_cols_in_encoding_order = true in grouper).

### What changes are included in this PR?
Get `null_bit_id` according to `are_cols_in_encoding_order` in NullUpdateColumnToRow_avx2.

### Are there any user-facing changes?
No

Co-authored-by laotan332 <qinpengxiang@ outlook.com>
Co-authored-by ZhangHuiGui <2689496754@ qq.com>

* GitHub Issue: apache#40997

Lead-authored-by: ZhangHuiGui <hugo.zhang@openpie.com>
Co-authored-by: ZhangHuiGui <2689496754@qq.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
vibhatha pushed a commit to vibhatha/arrow that referenced this pull request May 25, 2024
…nce regression (apache#41036)

### Rationale for this change
Add a grouper benchmark for preventing performance regression . 

apache#40998 (comment).

### What changes are included in this PR?
Added a benchmark.

### Are these changes tested?
Needn't.

### Are there any user-facing changes?
No

* GitHub Issue: apache#41035

Authored-by: ZhangHuiGui <hugo.zhang@openpie.com>
Signed-off-by: Antoine Pitrou <antoine@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants