Skip to content

Fix issue #793: Implement slab based tape - #1404

Merged
vgvassilev merged 1 commit into
vgvassilev:masterfrom
aditimjoshi:slab-based-tape
Jul 18, 2025
Merged

Fix issue #793: Implement slab based tape#1404
vgvassilev merged 1 commit into
vgvassilev:masterfrom
aditimjoshi:slab-based-tape

Conversation

@aditimjoshi

@aditimjoshi aditimjoshi commented Jun 11, 2025

Copy link
Copy Markdown
Contributor

Fixes #793. Modifies tape implementation to a slab based structure instead of relocating elements each time the tape grows in size.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/Tape.h Outdated
class tape_impl {
T* _data = nullptr;
struct Slab {
alignas(T) char raw_data[32 * sizeof(T)];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

      alignas(T) char raw_data[32 * sizeof(T)];
                 ^

Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
alignas(T) char raw_data[32 * sizeof(T)];
Slab* next;
Slab() : next(nullptr) {}
T* elements() { return reinterpret_cast<T*>(raw_data); }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      T* elements() { return reinterpret_cast<T*>(raw_data); }
                             ^

Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
}
Slab* tmp = slab;
slab = slab->next;
delete tmp;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead [cppcoreguidelines-owning-memory]

        delete tmp;
        ^
Additional context

include/clad/Differentiator/Tape.h:133: variable declared here

        Slab* tmp = slab;
        ^

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/Tape.h Outdated
size_t i = _size / slab_size;
while (i--) slab = slab->next;

::new (const_cast<void*>(static_cast<const volatile void*>(slab->elements() + (_size % slab_size)))) T(std::forward<ArgsT>(args)...);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use const_cast to remove const and volatile qualifier [cppcoreguidelines-pro-type-const-cast]

      ::new (const_cast<void*>(static_cast<const volatile void*>(slab->elements() + (_size % slab_size)))) T(std::forward<ArgsT>(args)...);
             ^

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be some idiom that's used in STL for such operations instead of these casts...

@codecov

codecov Bot commented Jun 12, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 14. Check the log or trigger a new build to see more.

Comment thread include/clad/Differentiator/Tape.h Outdated
class tape_impl {
T* _data = nullptr;
struct Slab {
alignas(T) char raw_data[1024 * sizeof(T)];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

      alignas(T) char raw_data[1024 * sizeof(T)];
                 ^

Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
constexpr static std::size_t slab_size = 1024;

CUDA_HOST_DEVICE T* sbo_elements() {
return reinterpret_cast<T*>(static_buffer_);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      return reinterpret_cast<T*>(static_buffer_);
             ^

Comment thread include/clad/Differentiator/Tape.h Outdated
}

CUDA_HOST_DEVICE const T* sbo_elements() const {
return reinterpret_cast<const T*>(static_buffer_);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      return reinterpret_cast<const T*>(static_buffer_);
             ^

Comment thread include/clad/Differentiator/Tape.h Outdated
T(std::forward<ArgsT>(args)...);
_size += 1;
if (_size < SBO_SIZE) {
::new (const_cast<void*>(static_cast<const volatile void*>(sbo_elements() + _size)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use const_cast to remove const and volatile qualifier [cppcoreguidelines-pro-type-const-cast]

        ::new (const_cast<void*>(static_cast<const volatile void*>(sbo_elements() + _size)))
               ^

Comment thread include/clad/Differentiator/Tape.h Outdated
using_sbo_ = false;
}
if ((_size - SBO_SIZE) % slab_size == 0) {
Slab* new_slab = new Slab();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: initializing non-owner 'Slab *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory]

          Slab* new_slab = new Slab();
          ^

Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h Outdated
class tape_impl {
T* _data = nullptr;
struct Slab {
alignas(T) char raw_data[1024 * sizeof(T)];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also test with bigger objects where sizeof(T) influences the overall size?

Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 12. Check the log or trigger a new build to see more.

Comment thread include/clad/Differentiator/Tape.h Outdated
T* _data = nullptr;
/// A block of contiguous storage allocated dynamically when SBO capacity is exceeded.
struct Slab {
alignas(T) char raw_data[SLAB_SIZE * sizeof(T)]{};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

      alignas(T) char raw_data[SLAB_SIZE * sizeof(T)]{};
                 ^

Comment thread include/clad/Differentiator/Tape.h Outdated
alignas(T) char raw_data[SLAB_SIZE * sizeof(T)]{};
Slab* next;
CUDA_HOST_DEVICE Slab() : next(nullptr) {}
CUDA_HOST_DEVICE T* elements() { return reinterpret_cast<T*>(raw_data); }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      CUDA_HOST_DEVICE T* elements() { return reinterpret_cast<T*>(raw_data); }
                                              ^

Comment thread include/clad/Differentiator/Tape.h Outdated
CUDA_HOST_DEVICE T* elements() { return reinterpret_cast<T*>(raw_data); }
};

alignas(T) char static_buffer[SBO_SIZE * sizeof(T)];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

    alignas(T) char static_buffer[SBO_SIZE * sizeof(T)];
               ^

Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
std::size_t _capacity = 0;

CUDA_HOST_DEVICE T* sbo_elements() {
return reinterpret_cast<T*>(static_buffer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      return reinterpret_cast<T*>(static_buffer);
             ^

Comment thread include/clad/Differentiator/Tape.h Outdated
}

CUDA_HOST_DEVICE const T* sbo_elements() const {
return reinterpret_cast<const T*>(static_buffer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      return reinterpret_cast<const T*>(static_buffer);
             ^

Comment thread include/clad/Differentiator/Tape.h Outdated
while (idx--) slab = slab->next;

// Construct element in-place
::new (const_cast<void*>(static_cast<const volatile void*>(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use const_cast to remove const and volatile qualifier [cppcoreguidelines-pro-type-const-cast]

        ::new (const_cast<void*>(static_cast<const volatile void*>(
               ^

Comment thread include/clad/Differentiator/Tape.h Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/Tape.h Outdated
CUDA_HOST_DEVICE T* elements() { return reinterpret_cast<T*>(raw_data); }
};

alignas(T) char m_static_buffer[SBO_SIZE * sizeof(T)];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not declare C-style arrays, use std::array<> instead [cppcoreguidelines-avoid-c-arrays]

    alignas(T) char m_static_buffer[SBO_SIZE * sizeof(T)];
               ^

Comment thread include/clad/Differentiator/Tape.h Outdated
std::size_t _capacity = 0;

CUDA_HOST_DEVICE T* sbo_elements() {
return reinterpret_cast<T*>(m_static_buffer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      return reinterpret_cast<T*>(m_static_buffer);
             ^

Comment thread include/clad/Differentiator/Tape.h Outdated
}

CUDA_HOST_DEVICE const T* sbo_elements() const {
return reinterpret_cast<const T*>(m_static_buffer);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast]

      return reinterpret_cast<const T*>(m_static_buffer);
             ^

Comment thread include/clad/Differentiator/Tape.h Outdated
}
Slab* tmp = slab;
slab = slab->next;
delete tmp;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead [cppcoreguidelines-owning-memory]

        delete tmp;
        ^
Additional context

include/clad/Differentiator/Tape.h:194: variable declared here

        Slab* tmp = slab;
        ^

Comment thread include/clad/Differentiator/Tape.h Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/Tape.h Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

There were too many comments to post at once. Showing the first 10 out of 16. Check the log or trigger a new build to see more.

Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h

@vgvassilev vgvassilev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are getting very close to merging this.

Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated
Comment thread include/clad/Differentiator/Tape.h Outdated

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/Tape.h
Comment thread include/clad/Differentiator/Tape.h

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

(elems + i)->~T();
Slab* tmp = slab;
slab = slab->next;
delete tmp;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead [cppcoreguidelines-owning-memory]

      delete tmp;
      ^
Additional context

include/clad/Differentiator/Tape.h:262: variable declared here

      Slab* tmp = slab;
      ^

@vgvassilev vgvassilev left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT! Gread job, @aditimjoshi!

@vgvassilev
vgvassilev merged commit 5a9dc91 into vgvassilev:master Jul 18, 2025
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Do not relocate the tape elements

3 participants