Skip to content

Commit

Permalink
Added explicit tests for SlabCacheStats, fleshed out docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed May 14, 2024
1 parent a7aea4d commit 00d8216
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
16 changes: 10 additions & 6 deletions include/tatami_chunked/SlabCacheStats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
namespace tatami_chunked {

/**
* @brief Typical statistics for slab cache construction.
* @brief Statistics for slab caching.
*
* This computes the slab size and the number of slabs to be cached, given the dimensions of the slab and the cache size in bytes.
* The assumption is that all slabs are of the same shape, partitioning the matrix into regular intervals along the target dimension.
* Developers should check out `CustomDenseChunkedMatrix` for some usage examples.
*/
struct SlabCacheStats {
/**
* Size of each slab, in terms of the number of elements.
* Size of each slab, in terms of the number of data elements.
*/
size_t slab_size_in_elements;

Expand All @@ -38,7 +42,7 @@ struct SlabCacheStats {
*/
SlabCacheStats(size_t target_length, size_t non_target_length, size_t target_num_slabs, size_t cache_size_in_elements, bool require_minimum_cache) :
slab_size_in_elements(target_length * non_target_length),
num_slabs_in_cache(std::min(compute_num_slabs_in_cache(slab_size_in_elements, cache_size_in_elements, require_minimum_cache), target_num_slabs))
num_slabs_in_cache(compute_num_slabs_in_cache(slab_size_in_elements, target_num_slabs, cache_size_in_elements, require_minimum_cache))
{}

/**
Expand All @@ -60,13 +64,13 @@ struct SlabCacheStats {
if (element_size == 0) {
return target_num_slabs;
} else {
return std::min(compute_num_slabs_in_cache(slab_size_in_elements, cache_size_in_bytes / element_size, require_minimum_cache), target_num_slabs);
return compute_num_slabs_in_cache(slab_size_in_elements, target_num_slabs, cache_size_in_bytes / element_size, require_minimum_cache);
}
}())
{}

private:
static size_t compute_num_slabs_in_cache(size_t slab_size_in_elements, size_t cache_size_in_elements, bool require_minimum_cache) {
static size_t compute_num_slabs_in_cache(size_t slab_size_in_elements, size_t num_slabs, size_t cache_size_in_elements, bool require_minimum_cache) {
if (slab_size_in_elements == 0) {
return 0;
}
Expand All @@ -76,7 +80,7 @@ struct SlabCacheStats {
return 1;
}

return tmp;
return std::min(tmp, num_slabs);
}
};

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ add_executable(
src/mock_dense_chunk.cpp
src/mock_sparse_chunk.cpp
src/ChunkDimensionStats.cpp
src/SlabCacheStats.cpp
src/CustomDenseChunkedMatrix.cpp
src/CustomSparseChunkedMatrix.cpp
)
Expand Down
43 changes: 43 additions & 0 deletions tests/src/SlabCacheStats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <gtest/gtest.h>
#include "tatami/tatami.hpp"
#include "tatami_chunked/SlabCacheStats.hpp"

TEST(SlabCacheStats, Basic) {
{
tatami_chunked::SlabCacheStats stats(10, 20, 50, 1000, false);
EXPECT_EQ(stats.slab_size_in_elements, 200);
EXPECT_EQ(stats.num_slabs_in_cache, 5);
}

{
tatami_chunked::SlabCacheStats stats(10, 20, 50, 100, false);
EXPECT_EQ(stats.slab_size_in_elements, 200);
EXPECT_EQ(stats.num_slabs_in_cache, 0);
}

{
tatami_chunked::SlabCacheStats stats(10, 20, 50, 100, true);
EXPECT_EQ(stats.slab_size_in_elements, 200);
EXPECT_EQ(stats.num_slabs_in_cache, 1);
}

{
tatami_chunked::SlabCacheStats stats(10, 20, 50, -1, false);
EXPECT_EQ(stats.slab_size_in_elements, 200);
EXPECT_EQ(stats.num_slabs_in_cache, 50);
}
}

TEST(SlabCacheStats, ElementSize) {
{
tatami_chunked::SlabCacheStats stats(10, 20, 50, 20000, 10, false);
EXPECT_EQ(stats.slab_size_in_elements, 200);
EXPECT_EQ(stats.num_slabs_in_cache, 10);
}

{
tatami_chunked::SlabCacheStats stats(10, 20, 50, 20000, 0, false);
EXPECT_EQ(stats.slab_size_in_elements, 200);
EXPECT_EQ(stats.num_slabs_in_cache, 50);
}
}

0 comments on commit 00d8216

Please sign in to comment.