Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

145 lines
4.4 KiB
C++

#include "hip/mutex"
#include "hip/pseudo_mutex" // From hip::wthread library
#include "hip/hip_runtime.h"
#include <iostream>
#include <cassert>
#include <mutex>
#include <vector>
#define CHECK(cmd) \
{ \
hipError_t error = cmd; \
if (error != hipSuccess) { \
fprintf(stderr, "error: '%s'(%d) at %s:%d\n", hipGetErrorString(error), error, \
__FILE__, __LINE__); \
exit(EXIT_FAILURE); \
} \
}
__device__ void gmain() {
hip::pseudo_mutex m, m2;
m.lock();
m.unlock();
m.try_lock();
m.unlock();
{
hip::lock_guard guard(m);
}
{
m.lock();
hip::lock_guard guard(m, ::std::adopt_lock);
}
{
hip::unique_lock guard(m);
hip::unique_lock guard2(m2);
assert(guard.owns_lock());
assert(guard2.owns_lock());
guard.unlock();
assert(!guard.owns_lock());
assert(guard2.owns_lock());
guard.lock();
assert(guard.owns_lock());
assert(guard2.owns_lock());
guard2.unlock();
assert(guard.owns_lock());
assert(!guard2.owns_lock());
}
{
hip::unique_lock guard(m, ::std::defer_lock);
assert(!guard.owns_lock());
guard.lock();
assert(guard.owns_lock());
}
{
hip::unique_lock guard(m, ::std::try_to_lock);
assert(guard.owns_lock());
}
{
m.lock();
hip::unique_lock guard(m, ::std::try_to_lock);
assert(!guard.owns_lock());
m.unlock();
assert(!guard.owns_lock());
guard.lock();
assert(guard.owns_lock());
}
{
hip::lock(m, m2);
m.unlock();
m2.unlock();
}
}
__device__ void block_sync_test() {
static __device__ hip::pseudo_mutex m;
static __device__ volatile int count = 0;
{
hip::unique_lock guard(m);
for (int i = 0; i < 32; ++i)
{
assert(count++ == i);
}
for (int i = 32; i > 0; --i)
{
assert(count-- == i);
}
}
}
// TODO: Test using multiple threads per block
#if 0
[[clang::optnone]] __device__ bool critical_section(const hip::unique_lock<hip::pseudo_mutex> &guard, volatile int &count) {
if (guard.owns_lock()) {
int threadId = threadIdx.x;
printf("Thread %u owns lock\n", threadId);
for (int i = 0; i < 2; ++i)
{
assert(count++ == i);
}
for (int i = 2; i > 0; --i)
{
assert(count-- == i);
}
return true;
}
return false;
}
__global__ void thread_test() {
static __device__ hip::pseudo_mutex m [[maybe_unused]];
static __device__ volatile int count [[maybe_unused]] = 0;
int attempt [[maybe_unused]] = 0;
int threadId = threadIdx.x;
printf("Thread %u starting\n", threadId);
for (bool success = false; !success;) {
if (attempt++ == 50)
printf("Attempt #50 for thread %u\n", threadId);
hip::unique_lock guard(m, ::std::try_to_lock);
// TODO: if (guard.owns_lock()) { critical_section(count); break; /* OR */ success = true; }
// results in critical section getting hoiseted out to AFTER the loop, breaking this code
success = critical_section(guard, count);
}
}
#endif // 0
int main() {
hip::wthread([] __device__(){gmain();}).join();
::std::vector<hip::wthread> threads(1<<16);
for (unsigned int i = 0; i < threads.size(); ++i) {
threads[i] = hip::wthread([] __device__(){block_sync_test();});
assert(threads[i].joinable());
}
for (unsigned int i = 0; i < threads.size(); ++i) {
try {
threads[i].join();
} catch (...) {
::std::cerr << "Exception when joining thread " << i << "\n";
::std::cerr << "threads[" << i << "].get_id() = " << threads[i].get_id() << "\n";
throw;
}
}
return 0;
}