Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CategoricalMatrix A.Tb reproducibility. #348

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ Unreleased

- Added cython compiler directive legacy_implicit_noexcept = True to fix performance regression with cython 3.


**Other changes:**

- Refactored the pre-commit hooks to use ruff.
- Refactored CategoricalMatrix's transpose_matvec to be deterministic when using OpenMP.

3.1.13 - 2023-10-17
-------------------
Expand Down
25 changes: 15 additions & 10 deletions src/tabmat/ext/cat_split_helpers-tmpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <vector>

#include <omp.h>

<%def name="transpose_matvec(dropfirst)">
template <typename Int, typename F>
Expand All @@ -10,24 +10,29 @@ void _transpose_matvec_${dropfirst}(
F* res,
Int res_size
) {
#pragma omp parallel
int num_threads = omp_get_max_threads();
std::vector<F> all_res(num_threads * res_size, 0.0);

Choose a reason for hiding this comment

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

Am I understanding correctly that we are using ~N times the memory compared to before (N being the number of threads)?
For transpose matvec this can be a problem because the result is as large as the input. Therefore, if the user has a very large categorical matrix as the main part of X, it will require N times more memory than X.

Copy link
Member Author

@adityagoel4512 adityagoel4512 Feb 26, 2024

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

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

You are right. Then let's merge this. Thanks for the contribution!

#pragma omp parallel shared(all_res)
{
std::vector<F> restemp(res_size, 0.0);
#pragma omp for
int tid = omp_get_thread_num();
F* res_slice = &all_res[tid * res_size];
#pragma omp for
for (Py_ssize_t i = 0; i < n_rows; i++) {
% if dropfirst == 'all_rows_drop_first':
Py_ssize_t col_idx = indices[i] - 1;
if (col_idx != -1) {
restemp[col_idx] += other[i];
res_slice[col_idx] += other[i];
}
% else:
restemp[indices[i]] += other[i];
res_slice[indices[i]] += other[i];
% endif
}
for (Py_ssize_t i = 0; i < res_size; i++) {
# pragma omp atomic
res[i] += restemp[i];
}
#pragma omp for
for (Py_ssize_t i = 0; i < res_size; ++i) {
for (int tid = 0; tid < num_threads; ++tid) {
res[i] += all_res[tid * res_size + i];
}
}
}
}
</%def>
Expand Down