-
Notifications
You must be signed in to change notification settings - Fork 200
Enable restore_tracker usage in device kernels #1893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vgvassilev
merged 1 commit into
vgvassilev:master
from
Vedant2005goyal:RestoreTracker_Device
Jul 21, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,12 @@ | |
| #include <map> | ||
| #include <utility> | ||
| #include <vector> | ||
| #ifndef Max_Records | ||
| #define Max_Records 64 | ||
| #endif | ||
| #ifndef Max_Bytes | ||
| #define Max_Bytes 1024 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: macro 'Max_Bytes' used to declare a constant; consider using a 'constexpr' constant [cppcoreguidelines-macro-usage] #define Max_Bytes 1024
^ |
||
| #endif | ||
|
|
||
| namespace clad { | ||
|
|
||
|
|
@@ -21,6 +27,41 @@ namespace clad { | |
| /// clad::tape is not viable. | ||
| class restore_tracker { | ||
| // m_data consists of pairs of memory addresses and bitwise values | ||
| #ifdef __CUDACC__ | ||
| struct MetaData { | ||
| char* addr; | ||
| size_t size; | ||
| size_t off; | ||
| }; | ||
| MetaData m_meta[Max_Records]; | ||
| uint8_t m_buf[Max_Bytes]; | ||
| size_t m_cnt = 0, m_off = 0; | ||
|
|
||
| public: | ||
| __host__ __device__ restore_tracker() = default; | ||
|
|
||
| template <typename T> __host__ __device__ void store(const T& val) { | ||
| for (size_t i = 0; i < m_cnt; ++i) | ||
| if (m_meta[i].addr == (char*)&val) | ||
| return; | ||
|
|
||
| if (m_cnt >= Max_Records || m_off + sizeof(T) > Max_Bytes) { | ||
| // Clad restore_tracker GPU capacity exceeded. Try again with larger value | ||
| return; | ||
| } | ||
|
|
||
| m_meta[m_cnt] = {(char*)&val, sizeof(T), m_off}; | ||
| std::memcpy(m_buf + m_off, &val, sizeof(T)); | ||
| m_off += sizeof(T); | ||
| m_cnt++; | ||
| } | ||
|
|
||
| __host__ __device__ void restore() { | ||
| for (size_t i = 0; i < m_cnt; ++i) | ||
| std::memcpy(m_meta[i].addr, m_buf + m_meta[i].off, m_meta[i].size); | ||
| m_cnt = m_off = 0; | ||
| } | ||
| #else | ||
| using RawMemory = std::vector<uint8_t>; | ||
| using Address = char*; | ||
| std::map<const Address, RawMemory> m_data; | ||
|
|
@@ -47,6 +88,7 @@ class restore_tracker { | |
| } | ||
| m_data.clear(); | ||
| } | ||
| #endif | ||
| }; | ||
| } // namespace clad | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // RUN: %cladclang_cuda -I%S/../../include --cuda-path=%cudapath \ | ||
| // RUN: --cuda-gpu-arch=%cudaarch %cudaldflags -o RestoreTrackerDevice.out %s | ||
| // | ||
| // RUN: %cudarun ./RestoreTrackerDevice.out | %filecheck_exec %s | ||
| // | ||
| // REQUIRES: cuda-runtime | ||
| #include "clad/Differentiator/Differentiator.h" | ||
| #include <iostream> | ||
|
|
||
| __global__ void test_kernel() { | ||
| clad::restore_tracker tracker; | ||
|
|
||
| double val = 3.14; | ||
| tracker.store(val); | ||
| val = 0.0; | ||
| tracker.restore(); | ||
|
|
||
| if (val == 3.14) { | ||
| printf("Working on device!\n"); | ||
| } | ||
| } | ||
|
|
||
| int main() { | ||
|
|
||
| test_kernel<<<1, 1>>>(); | ||
| cudaDeviceSynchronize(); | ||
|
|
||
| // CHECK-EXEC: Working on device! | ||
| return 0; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
warning: macro 'Max_Records' used to declare a constant; consider using a 'constexpr' constant [cppcoreguidelines-macro-usage]