Skip to content

Commit

Permalink
Remove unnecessary checks for non-default parallelization schemes.
Browse files Browse the repository at this point in the history
These checks are a historical artifact of the time when these functions
lived in tatami itself, and are largely redundant with the existing
checks for correct behavior of parallelize().

We also switch to int instead of size_t for the thread ID to avoid an
unnecessary cast inside of parallelize().
  • Loading branch information
LTLA committed Aug 26, 2024
1 parent 87ed962 commit 2baa749
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 146 deletions.
4 changes: 2 additions & 2 deletions include/tatami_stats/grouped_sums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, const Group_* grou
tatami::Options opt;
opt.sparse_ordered_index = false;

tatami::parallelize([&](size_t thread, Index_ start, Index_ len) -> void {
tatami::parallelize([&](int thread, Index_ start, Index_ len) -> void {
std::vector<sums::RunningSparse<Output_, Value_, Index_> > runners;
runners.reserve(num_groups);
std::vector<LocalOutputBuffer<Output_> > local_output;
Expand Down Expand Up @@ -167,7 +167,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, const Group_* grou
}, dim, sopt.num_threads);

} else {
tatami::parallelize([&](size_t thread, Index_ start, Index_ len) -> void {
tatami::parallelize([&](int thread, Index_ start, Index_ len) -> void {
std::vector<sums::RunningDense<Output_, Value_, Index_> > runners;
runners.reserve(num_groups);
std::vector<LocalOutputBuffer<Output_> > local_output;
Expand Down
4 changes: 2 additions & 2 deletions include/tatami_stats/grouped_variances.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, const Group_* grou
tatami::Options opt;
opt.sparse_ordered_index = false;

tatami::parallelize([&](size_t thread, Index_ start, Index_ len) -> void {
tatami::parallelize([&](int thread, Index_ start, Index_ len) -> void {
std::vector<variances::RunningSparse<Output_, Value_, Index_> > runners;
runners.reserve(num_groups);
std::vector<LocalOutputBuffer<Output_> > local_var_output;
Expand Down Expand Up @@ -382,7 +382,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, const Group_* grou
}, dim, sopt.num_threads);

} else {
tatami::parallelize([&](size_t thread, Index_ start, Index_ len) -> void {
tatami::parallelize([&](int thread, Index_ start, Index_ len) -> void {
std::vector<variances::RunningDense<Output_, Value_, Index_> > runners;
runners.reserve(num_groups);
std::vector<LocalOutputBuffer<Output_> > local_var_output;
Expand Down
8 changes: 4 additions & 4 deletions include/tatami_stats/ranges.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* min_out,

if (direct) {
opt.sparse_extract_index = false;
tatami::parallelize([&](size_t, Index_ s, Index_ l) {
tatami::parallelize([&](int, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, row, s, l, opt);
std::vector<Value_> vbuffer(otherdim);
for (Index_ x = 0; x < l; ++x) {
Expand All @@ -367,7 +367,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* min_out,
}, dim, ropt.num_threads);

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
tatami::parallelize([&](int thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, !row, static_cast<Index_>(0), otherdim, s, l, opt);
std::vector<Value_> vbuffer(l);
std::vector<Index_> ibuffer(l);
Expand Down Expand Up @@ -400,7 +400,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* min_out,

} else {
if (direct) {
tatami::parallelize([&](size_t, Index_ s, Index_ l) {
tatami::parallelize([&](int, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, row, s, l);
std::vector<Value_> buffer(otherdim);
for (Index_ x = 0; x < l; ++x) {
Expand All @@ -415,7 +415,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* min_out,
}, dim, ropt.num_threads);

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
tatami::parallelize([&](int thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> buffer(l);

Expand Down
8 changes: 4 additions & 4 deletions include/tatami_stats/sums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c
tatami::Options opt;
opt.sparse_extract_index = false;

tatami::parallelize([&](size_t, Index_ s, Index_ l) {
tatami::parallelize([&](int, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, row, s, l, opt);
std::vector<Value_> vbuffer(otherdim);
for (Index_ x = 0; x < l; ++x) {
Expand All @@ -226,7 +226,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c
tatami::Options opt;
opt.sparse_ordered_index = false;

tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
tatami::parallelize([&](int thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, !row, static_cast<Index_>(0), otherdim, s, l, opt);
std::vector<Value_> vbuffer(l);
std::vector<Index_> ibuffer(l);
Expand All @@ -245,7 +245,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c

} else {
if (direct) {
tatami::parallelize([&](size_t, Index_ s, Index_ l) {
tatami::parallelize([&](int, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, row, s, l);
std::vector<Value_> buffer(otherdim);
for (Index_ x = 0; x < l; ++x) {
Expand All @@ -255,7 +255,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c
}, dim, sopt.num_threads);

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
tatami::parallelize([&](int thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> buffer(l);

Expand Down
8 changes: 4 additions & 4 deletions include/tatami_stats/variances.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c
if (direct) {
tatami::Options opt;
opt.sparse_extract_index = false;
tatami::parallelize([&](size_t, Index_ s, Index_ l) {
tatami::parallelize([&](int, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, row, s, l);
std::vector<Value_> vbuffer(otherdim);
for (Index_ x = 0; x < l; ++x) {
Expand All @@ -412,7 +412,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c
}, dim, vopt.num_threads);

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
tatami::parallelize([&](int thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<true>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> vbuffer(l);
std::vector<Index_> ibuffer(l);
Expand All @@ -433,7 +433,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c

} else {
if (direct) {
tatami::parallelize([&](size_t, Index_ s, Index_ l) {
tatami::parallelize([&](int, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, row, s, l);
std::vector<Value_> buffer(otherdim);
for (Index_ x = 0; x < l; ++x) {
Expand All @@ -443,7 +443,7 @@ void apply(bool row, const tatami::Matrix<Value_, Index_>* p, Output_* output, c
}, dim, vopt.num_threads);

} else {
tatami::parallelize([&](size_t thread, Index_ s, Index_ l) {
tatami::parallelize([&](int thread, Index_ s, Index_ l) {
auto ext = tatami::consecutive_extractor<false>(p, !row, static_cast<Index_>(0), otherdim, s, l);
std::vector<Value_> buffer(l);

Expand Down
67 changes: 24 additions & 43 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,32 @@ FetchContent_MakeAvailable(googletest)

enable_testing()

include(GoogleTest)
add_executable(
libtest
src/sums.cpp
src/variances.cpp
src/medians.cpp
src/ranges.cpp
src/counts.cpp
src/grouped_medians.cpp
src/grouped_sums.cpp
src/grouped_variances.cpp
src/utils.cpp
)

target_link_libraries(
libtest
gtest_main
tatami_stats
)

target_compile_options(libtest PRIVATE -Wall -Wextra -Wpedantic -Werror)

set(CODE_COVERAGE OFF CACHE BOOL "Enable coverage testing")
set(DO_CODE_COVERAGE OFF)
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(DO_CODE_COVERAGE ON)
target_compile_options(libtest PRIVATE -O0 -g --coverage)
target_link_options(libtest PRIVATE --coverage)
endif()

macro(create_test target)
add_executable(
${target}
src/sums.cpp
src/variances.cpp
src/medians.cpp
src/ranges.cpp
src/counts.cpp
src/grouped_medians.cpp
src/grouped_sums.cpp
src/grouped_variances.cpp
src/utils.cpp
)

target_link_libraries(
${target}
gtest_main
tatami_stats
)

target_compile_options(${target} PRIVATE -Wall -Wextra -Wpedantic -Werror)

if(DO_CODE_COVERAGE)
target_compile_options(${target} PRIVATE -O0 -g --coverage)
target_link_options(${target} PRIVATE --coverage)
endif()

gtest_discover_tests(${target})
endmacro()

create_test(libtest)

create_test(cuspartest)
target_compile_definitions(cuspartest PRIVATE CUSTOM_PARALLEL_TEST=1)

find_package(OpenMP)
if(OpenMP_FOUND)
create_test(omptest)
target_link_libraries(omptest OpenMP::OpenMP_CXX)
endif()
include(GoogleTest)
gtest_discover_tests(libtest)
5 changes: 0 additions & 5 deletions tests/src/counts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "tatami_stats/counts.hpp"
#include "tatami_test/tatami_test.hpp"

Expand Down
47 changes: 0 additions & 47 deletions tests/src/custom_parallel.h

This file was deleted.

5 changes: 0 additions & 5 deletions tests/src/grouped_medians.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "tatami/tatami.hpp"
#include "tatami_stats/grouped_medians.hpp"
#include "tatami_test/tatami_test.hpp"
Expand Down
5 changes: 0 additions & 5 deletions tests/src/grouped_sums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "tatami_stats/grouped_sums.hpp"
#include "tatami_stats/sums.hpp"
#include "tatami_test/tatami_test.hpp"
Expand Down
5 changes: 0 additions & 5 deletions tests/src/grouped_variances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "tatami_stats/grouped_variances.hpp"
#include "tatami_stats/variances.hpp"
#include "tatami_test/tatami_test.hpp"
Expand Down
5 changes: 0 additions & 5 deletions tests/src/medians.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "tatami_stats/medians.hpp"
#include "tatami_test/tatami_test.hpp"

Expand Down
5 changes: 0 additions & 5 deletions tests/src/ranges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "tatami_stats/ranges.hpp"
#include "tatami_test/tatami_test.hpp"

Expand Down
5 changes: 0 additions & 5 deletions tests/src/sums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "tatami_stats/sums.hpp"
#include "tatami_test/tatami_test.hpp"
#include "utils.h"
Expand Down
5 changes: 0 additions & 5 deletions tests/src/variances.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

#include <vector>

#ifdef CUSTOM_PARALLEL_TEST
// Put this before any tatami imports.
#include "custom_parallel.h"
#endif

#include "utils.h"
#include "tatami_stats/variances.hpp"
#include "tatami_test/tatami_test.hpp"
Expand Down

0 comments on commit 2baa749

Please sign in to comment.