Skip to content

Commit 82ab54a

Browse files
committed
Modify tape to slab-based and fixes #793
1 parent b17af42 commit 82ab54a

4 files changed

Lines changed: 282 additions & 150 deletions

File tree

benchmark/MemoryComplexity.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ void operator delete(void* p) noexcept {
5858
free(p);
5959
}
6060

61-
template <typename T> void func(clad::tape<T>& t, T x, int n) {
61+
template <typename T, std::size_t SBO_SIZE = 64, std::size_t SLAB_SIZE = 1024>
62+
void func(clad::tape<T, SBO_SIZE, SLAB_SIZE>& t, T x, int n) {
6263
for (int i = 0; i < n; i++)
63-
clad::push<T>(t, x);
64+
clad::push<T, SBO_SIZE, SLAB_SIZE>(t, x);
6465

6566
for (int i = 0; i < n; i++)
66-
clad::pop<T>(t);
67+
clad::pop<T, SBO_SIZE, SLAB_SIZE>(t);
6768
}
6869

6970
static void BM_TapeMemory(benchmark::State& state) {
@@ -76,6 +77,25 @@ static void BM_TapeMemory(benchmark::State& state) {
7677
}
7778
BENCHMARK(BM_TapeMemory)->RangeMultiplier(2)->Range(0, 4096)->Iterations(1);
7879

80+
template <std::size_t SBO_SIZE, std::size_t SLAB_SIZE>
81+
static void BM_TapeMemory_Templated(benchmark::State& state) {
82+
int block = state.range(0);
83+
AddBMCounterRAII MemCounters(*mm.get(), state);
84+
clad::tape<double, SBO_SIZE, SLAB_SIZE> t;
85+
for (auto _ : state)
86+
func<double, SBO_SIZE, SLAB_SIZE>(t, 1, block * 2 + 1);
87+
}
88+
89+
#define REGISTER_TAPE_BENCHMARK(sbo, slab) \
90+
BENCHMARK_TEMPLATE(BM_TapeMemory_Templated, sbo, slab) \
91+
->RangeMultiplier(2) \
92+
->Range(0, 4096) \
93+
->Iterations(1) \
94+
->Name("BM_TapeMemory/SBO_" #sbo "_SLAB_" #slab)
95+
96+
REGISTER_TAPE_BENCHMARK(64, 1024);
97+
REGISTER_TAPE_BENCHMARK(32, 512);
98+
7999
#include "BenchmarkedFunctions.h"
80100

81101
static void BM_ReverseGausMemoryP(benchmark::State& state) {

include/clad/Differentiator/Differentiator.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ inline CUDA_HOST_DEVICE unsigned int GetLength(const char* code) {
5353
#endif
5454

5555
/// Tape type used for storing values in reverse-mode AD inside loops.
56-
template <typename T> using tape = tape_impl<T>;
56+
template <typename T, std::size_t SBO_SIZE = 64, std::size_t SLAB_SIZE = 1024>
57+
using tape = tape_impl<T>;
5758

5859
/// Add value to the end of the tape, return the same value.
59-
template <typename T, typename... ArgsT>
60-
CUDA_HOST_DEVICE T push(tape<T>& to, ArgsT... val) {
60+
template <typename T, std::size_t SBO_SIZE = 64, std::size_t SLAB_SIZE = 1024,
61+
typename... ArgsT>
62+
CUDA_HOST_DEVICE T push(tape<T, SBO_SIZE, SLAB_SIZE>& to, ArgsT... val) {
6163
to.emplace_back(std::forward<ArgsT>(val)...);
6264
return to.back();
6365
}
@@ -72,8 +74,8 @@ CUDA_HOST_DEVICE T push(tape<T>& to, ArgsT... val) {
7274
}
7375

7476
/// Remove the last value from the tape, return it.
75-
template <typename T>
76-
CUDA_HOST_DEVICE T pop(tape<T>& to) {
77+
template <typename T, std::size_t SBO_SIZE = 64, std::size_t SLAB_SIZE = 1024>
78+
CUDA_HOST_DEVICE T pop(tape<T, SBO_SIZE, SLAB_SIZE>& to) {
7779
T val = std::move(to.back());
7880
to.pop_back();
7981
return val;

0 commit comments

Comments
 (0)