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

Check that SingleThreadedBlasScope is created and destroyed on the same std::thread #889

Merged
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
7 changes: 7 additions & 0 deletions include/dlaf/common/single_threaded_blas.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#pragma once

#ifdef DLAF_ASSERT_MODERATE_ENABLE
#include <thread>
#endif

namespace dlaf::common::internal {
class [[nodiscard]] SingleThreadedBlasScope {
public:
Expand All @@ -21,6 +25,9 @@ class [[nodiscard]] SingleThreadedBlasScope {
SingleThreadedBlasScope& operator=(SingleThreadedBlasScope const&) = delete;

private:
#ifdef DLAF_ASSERT_MODERATE_ENABLE
std::thread::id calling_thread;
#endif
#ifdef DLAF_WITH_OPENMP
int omp_num_threads;
#endif
Expand Down
11 changes: 11 additions & 0 deletions src/common/single_threaded_blas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@

#include <dlaf/common/single_threaded_blas.h>

#ifdef DLAF_ASSERT_MODERATE_ENABLE
#include <dlaf/common/assert.h>
#endif

namespace dlaf::common::internal {
SingleThreadedBlasScope::SingleThreadedBlasScope() {
#ifdef DLAF_ASSERT_MODERATE_ENABLE
calling_thread = std::this_thread::get_id();
#endif
#ifdef DLAF_WITH_OPENMP
omp_num_threads = omp_get_max_threads();
omp_set_num_threads(1);
Expand All @@ -29,6 +36,10 @@ SingleThreadedBlasScope::SingleThreadedBlasScope() {
}

SingleThreadedBlasScope::~SingleThreadedBlasScope() {
#ifdef DLAF_ASSERT_MODERATE_ENABLE
auto current_thread = std::this_thread::get_id();
DLAF_ASSERT_MODERATE(calling_thread == current_thread, calling_thread, current_thread);
#endif
#ifdef DLAF_WITH_OPENMP
omp_set_num_threads(omp_num_threads);
#endif
Expand Down
60 changes: 32 additions & 28 deletions test/unit/eigensolver/test_bt_band_to_tridiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,26 @@ void testBacktransformation(SizeType m, SizeType n, SizeType mb, SizeType nb, co
if (m == 0 || n == 0)
return;

dlaf::common::internal::SingleThreadedBlasScope single;
{
dlaf::common::internal::SingleThreadedBlasScope single;

using eigensolver::internal::nrStepsForSweep;
using eigensolver::internal::nrSweeps;
for (SizeType sweep = nrSweeps<T>(m) - 1; sweep >= 0; --sweep) {
for (SizeType step = nrStepsForSweep(sweep, m, b) - 1; step >= 0; --step) {
const SizeType j = sweep;
const SizeType i = j + 1 + step * b;
using eigensolver::internal::nrStepsForSweep;
using eigensolver::internal::nrSweeps;
for (SizeType sweep = nrSweeps<T>(m) - 1; sweep >= 0; --sweep) {
for (SizeType step = nrStepsForSweep(sweep, m, b) - 1; step >= 0; --step) {
const SizeType j = sweep;
const SizeType i = j + 1 + step * b;

const SizeType size = std::min(b, m - i);
const SizeType i_v = (i - 1) / b * b;
const SizeType size = std::min(b, m - i);
const SizeType i_v = (i - 1) / b * b;

T& v_head = *mat_hh_local.ptr({i_v, j});
const T tau = v_head;
v_head = 1;
T& v_head = *mat_hh_local.ptr({i_v, j});
const T tau = v_head;
v_head = 1;

using blas::Side;
lapack::larf(Side::Left, size, n, &v_head, 1, tau, mat_e_local.ptr({i, 0}), mat_e_local.ld());
using blas::Side;
lapack::larf(Side::Left, size, n, &v_head, 1, tau, mat_e_local.ptr({i, 0}), mat_e_local.ld());
}
}
}

Expand Down Expand Up @@ -212,24 +214,26 @@ void testBacktransformation(comm::CommunicatorGrid grid, SizeType m, SizeType n,
if (m == 0 || n == 0)
return;

dlaf::common::internal::SingleThreadedBlasScope single;
{
dlaf::common::internal::SingleThreadedBlasScope single;

using eigensolver::internal::nrStepsForSweep;
using eigensolver::internal::nrSweeps;
for (SizeType sweep = nrSweeps<T>(m) - 1; sweep >= 0; --sweep) {
for (SizeType step = nrStepsForSweep(sweep, m, b) - 1; step >= 0; --step) {
const SizeType j = sweep;
const SizeType i = j + 1 + step * b;
using eigensolver::internal::nrStepsForSweep;
using eigensolver::internal::nrSweeps;
for (SizeType sweep = nrSweeps<T>(m) - 1; sweep >= 0; --sweep) {
for (SizeType step = nrStepsForSweep(sweep, m, b) - 1; step >= 0; --step) {
const SizeType j = sweep;
const SizeType i = j + 1 + step * b;

const SizeType size = std::min(b, m - i);
const SizeType i_v = (i - 1) / b * b;
const SizeType size = std::min(b, m - i);
const SizeType i_v = (i - 1) / b * b;

T& v_head = *mat_hh_local.ptr({i_v, j});
const T tau = v_head;
v_head = 1;
T& v_head = *mat_hh_local.ptr({i_v, j});
const T tau = v_head;
v_head = 1;

using blas::Side;
lapack::larf(Side::Left, size, n, &v_head, 1, tau, mat_e_local.ptr({i, 0}), mat_e_local.ld());
using blas::Side;
lapack::larf(Side::Left, size, n, &v_head, 1, tau, mat_e_local.ptr({i, 0}), mat_e_local.ld());
}
}
}

Expand Down
12 changes: 7 additions & 5 deletions test/unit/eigensolver/test_tridiag_solver_rot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ void testApplyGivenRotations(comm::CommunicatorGrid grid, const SizeType m, cons
const SizeType n = std::min((idx_end) *mb, m) - idx_begin * mb;
const GlobalElementSize offset(idx_begin * mb, idx_begin * mb);

dlaf::common::internal::SingleThreadedBlasScope single;
{
dlaf::common::internal::SingleThreadedBlasScope single;

for (auto rot : rots) {
T* x = mat_loc.ptr(GlobalElementIndex{0, rot.i} + offset);
T* y = mat_loc.ptr(GlobalElementIndex{0, rot.j} + offset);
blas::rot(n, x, 1, y, 1, rot.c, rot.s);
for (auto rot : rots) {
T* x = mat_loc.ptr(GlobalElementIndex{0, rot.i} + offset);
T* y = mat_loc.ptr(GlobalElementIndex{0, rot.j} + offset);
blas::rot(n, x, 1, y, 1, rot.c, rot.s);
}
}

auto result = [&dist = mat_h.distribution(), &mat_local = mat_loc](const GlobalElementIndex& element) {
Expand Down