Skip to content

Commit

Permalink
Removed reliance on tatami_stats::LocalOutputBuffer.
Browse files Browse the repository at this point in the history
This doesn't protect against false sharing as much as I thought, given
that separate vector allocations can still be contiguous in memory; so
it's just simpler to remove this code and zero the arrays manually.
  • Loading branch information
LTLA committed Jan 9, 2025
1 parent ee63ef1 commit 9508b07
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ if(SCRAN_AGGREGATE_FETCH_EXTERN)
add_subdirectory(extern)
else()
find_package(tatami_tatami 3.0.0 CONFIG REQUIRED)
find_package(tatami_tatami_stats 1.1.0 CONFIG REQUIRED)
endif()

target_link_libraries(scran_aggregate INTERFACE tatami::tatami tatami::tatami_stats)
target_link_libraries(scran_aggregate INTERFACE tatami::tatami)

# Tests
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
Expand Down
1 change: 0 additions & 1 deletion cmake/Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@

include(CMakeFindDependencyMacro)
find_dependency(tatami_tatami 3.0.0 CONFIG REQUIRED)
find_dependency(tatami_tatami_stats 1.1.0 CONFIG REQUIRED)

include("${CMAKE_CURRENT_LIST_DIR}/libscran_scran_aggregateTargets.cmake")
7 changes: 0 additions & 7 deletions extern/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,4 @@ FetchContent_Declare(
GIT_TAG master # ^3.0.0
)

FetchContent_Declare(
tatami_stats
GIT_REPOSITORY https://github.com/tatami-inc/tatami_stats
GIT_TAG master # ^1.1.0
)

FetchContent_MakeAvailable(tatami)
FetchContent_MakeAvailable(tatami_stats)
31 changes: 14 additions & 17 deletions include/scran_aggregate/aggregate_across_cells.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vector>

#include "tatami/tatami.hpp"
#include "tatami_stats/tatami_stats.hpp"

/**
* @file aggregate_across_cells.hpp
Expand Down Expand Up @@ -175,55 +174,53 @@ void compute_aggregate_by_column(
tatami::Options opt;
opt.sparse_ordered_index = false;

auto NC = p.ncol();
for (auto ptr : buffers.sums) {
std::fill_n(ptr, NC, static_cast<Sum_>(0));
}
for (auto ptr : buffers.detected) {
std::fill_n(ptr, NC, static_cast<Detected_>(0));
}

tatami::parallelize([&](size_t t, Index_ s, Index_ l) {
auto NC = p.ncol();
auto ext = tatami::consecutive_extractor<sparse_>(&p, false, static_cast<Index_>(0), NC, s, l, opt);
std::vector<Data_> vbuffer(l);
typename std::conditional<sparse_, std::vector<Index_>, Index_>::type ibuffer(l);

size_t num_sums = buffers.sums.size();
std::vector<tatami_stats::LocalOutputBuffer<Sum_> > local_sums;
local_sums.reserve(num_sums);
for (auto ptr : buffers.sums) {
local_sums.emplace_back(t, s, l, ptr);
}
size_t num_detected = buffers.detected.size();
std::vector<tatami_stats::LocalOutputBuffer<Detected_> > local_detected;
local_detected.reserve(num_detected);
for (auto ptr : buffers.detected) {
local_detected.emplace_back(t, s, l, ptr);
}

for (Index_ x = 0; x < NC; ++x) {
auto current = factor[x];

if constexpr(sparse_) {
auto col = ext->fetch(vbuffer.data(), ibuffer.data());
if (num_sums) {
auto cursum = local_sums[current].data();
auto cursum = buffers.sums[current];
for (Index_ i = 0; i < col.number; ++i) {
cursum[col.index[i] - s] += col.value[i];
cursum[col.index[i]] += col.value[i];
}
}

if (num_detected) {
auto curdetected = local_detected[current].data();
auto curdetected = buffers.detected[current];
for (Index_ i = 0; i < col.number; ++i) {
curdetected[col.index[i] - s] += (col.value[i] > 0);
curdetected[col.index[i]] += (col.value[i] > 0);
}
}

} else {
auto col = ext->fetch(vbuffer.data());
if (num_sums) {
auto cursum = local_sums[current].data();
auto cursum = buffers.sums[current] + s;
for (Index_ i = 0; i < l; ++i) {
cursum[i] += col[i];
}
}

if (num_detected) {
auto curdetected = local_detected[current].data();
auto curdetected = buffers.detected[current] + s;
for (Index_ i = 0; i < l; ++i) {
curdetected[i] += (col[i] > 0);
}
Expand Down

0 comments on commit 9508b07

Please sign in to comment.