-
Notifications
You must be signed in to change notification settings - Fork 14
feat: GPU locking via atomic operation using POSIX shared memory #54
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
dong-hao
merged 4 commits into
magnetotellurics:main
from
dong-hao:feature/add-atomic-gpu-lock
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bba15c9
feat: GPU locking via atomic operation using POSIX shared memory
dong-hao 0897a99
fix: on GPU lock race conditions and clean-up
dong-hao e6ddc23
refactor: extract GPU lock into header gpu_lock.h
dong-hao 36621bb
refactor: simplify construction coordination with single atomic field
dong-hao 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
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
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,97 @@ | ||
| // gpu_lock.h — Platform-independent, cross-process GPU locking via POSIX | ||
| // shared memory. | ||
| // Multiple MPI ranks (should...) safely target the same GPU: | ||
|
|
||
| #ifndef MODEM_GPU_LOCK_H | ||
| #define MODEM_GPU_LOCK_H | ||
|
|
||
| #include <atomic> | ||
| #include <fcntl.h> | ||
| #include <new> | ||
| #include <sys/mman.h> | ||
| #include <unistd.h> | ||
|
|
||
| // Define device state flags | ||
|
|
||
| #define DEVICE_FREE 0 | ||
| #define DEVICE_IN_USE 1 | ||
|
|
||
| // Lock structure & global state | ||
| // we probably don't have more than 64 devices on a single node | ||
| static constexpr int LOCK_MAX_DEVICES = 64; | ||
|
|
||
| // cnstr: compiler-built-in atomic on raw int — safe before object lifetime begins. | ||
| // All other members are std::atomic<int>, constructed by placement-new once, | ||
| // then used via normal C++ atomic operations. | ||
| struct alignas(64) GpuLock { | ||
| int cnstr; // 0 = not constructed, 1 = atomics live | ||
| std::atomic<int> occupied[LOCK_MAX_DEVICES]; | ||
| }; | ||
|
|
||
| static GpuLock* g_lock = nullptr; | ||
| static bool g_lock_inited = false; | ||
|
|
||
| // Internal: create / map shared-memory segment and construct atomics | ||
| static inline int init_gpu_lock() | ||
| { | ||
| const char* name = "/ModEM_gpu_lock"; | ||
|
|
||
| // Try to open existing shared memory segment first | ||
| int fd = shm_open(name, O_RDWR, 0600); | ||
| if (fd < 0) { | ||
| // Doesn't exist -- create it | ||
| fd = shm_open(name, O_CREAT | O_RDWR, 0600); | ||
| if (fd < 0) return 1; | ||
| if (ftruncate(fd, sizeof(GpuLock)) < 0) { close(fd); return 1; } | ||
| } | ||
|
|
||
| g_lock = static_cast<GpuLock*>(mmap(nullptr, sizeof(GpuLock), | ||
| PROT_READ | PROT_WRITE, | ||
| MAP_SHARED, fd, 0)); | ||
| close(fd); | ||
| if (g_lock == MAP_FAILED) { g_lock = nullptr; return 1; } | ||
|
|
||
| // Coordinate exactly-once construction of std::atomic members. | ||
| // cnstr uses __atomic_* built-ins: safe on raw storage | ||
| // the winner does placement-new on every std::atomic<int> | ||
| // the other (losers) spin-wait with ACQUIRE on "cnstr" until it's 1, | ||
| // then proceed to use the atomics. | ||
|
|
||
| if (__atomic_exchange_n(&g_lock->cnstr, 1, __ATOMIC_ACQ_REL) != 0) { | ||
| // Another process won the race — spin-wait until construction finishes. | ||
| while (!__atomic_load_n(&g_lock->cnstr, __ATOMIC_ACQUIRE)) | ||
| ; | ||
| } else { | ||
| // We are the winner — construct all std::atomic members. | ||
| for (int i = 0; i < LOCK_MAX_DEVICES; i++) | ||
| new (&g_lock->occupied[i]) std::atomic<int>(DEVICE_FREE); | ||
|
|
||
| // cnstr is already 1 from the exchange above (ACQ_REL ensures | ||
| // the constructed atomics are visible to other processes). | ||
| } | ||
|
|
||
| g_lock_inited = true; | ||
| return 0; | ||
| } | ||
|
|
||
| // Public C-bindings (called from Fortran) | ||
|
|
||
| extern "C" void cf_releaseDev(int dev_idx) | ||
| { | ||
| if (g_lock_inited && g_lock != nullptr && | ||
| dev_idx >= 0 && dev_idx < LOCK_MAX_DEVICES) | ||
| { | ||
| g_lock->occupied[dev_idx].store(DEVICE_FREE, std::memory_order_release); | ||
| } | ||
| } | ||
|
|
||
| extern "C" void cf_cleanupLock() | ||
| { | ||
| if (g_lock != nullptr && g_lock != MAP_FAILED) | ||
| munmap(g_lock, sizeof(GpuLock)); | ||
| shm_unlink("/ModEM_gpu_lock"); | ||
| g_lock = nullptr; | ||
| g_lock_inited = false; | ||
| } | ||
|
|
||
| #endif // MODEM_GPU_LOCK_H |
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
Oops, something went wrong.
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.
Well, NO- resetting this (while finishing one solve task for a transmitter) will reset the shared lock for the other processes - will definitely cause some (undefined) problems.