Skip to content

Instantly share code, notes, and snippets.

@ChrisKitching
Last active May 13, 2026 18:55
Show Gist options
  • Select an option

  • Save ChrisKitching/53d9b3503c11881c5ea395ec41aff419 to your computer and use it in GitHub Desktop.

Select an option

Save ChrisKitching/53d9b3503c11881c5ea395ec41aff419 to your computer and use it in GitHub Desktop.
Managed memory microbenchmark (HIP vs. SCALE)
#ifdef __HIP__
#include <hip/hip_runtime.h>
#define cudaError_t hipError_t
#define cudaSuccess hipSuccess
#define cudaMalloc hipMalloc
#define cudaMallocManaged hipMallocManaged
#define cudaDeviceSynchronize hipDeviceSynchronize
#endif
#include <linux/mman.h>
#include <sys/mman.h>
#include <chrono>
#include <cstdio>
using T = float;
//constexpr unsigned int blockSize = 1 << 10;
//constexpr unsigned int numBlocks = 1 << 10;
//constexpr size_t eltsPerThread = 1 << 6;
constexpr unsigned int blockSize = 1 << 10;
constexpr unsigned int numBlocks = 1 << 8;
constexpr size_t eltsPerThread = 1 << 12;
constexpr size_t bytes = blockSize * numBlocks * eltsPerThread * sizeof(T);
__global__ void warmup(){}
__global__ void k(T *dst, const T *src)
{
size_t tid = blockIdx.x * blockSize + threadIdx.x;
T acc = 0;
for (unsigned int i = 0; i < eltsPerThread; i++) {
acc += src[tid * eltsPerThread + i];
}
dst[tid] = acc;
}
void check(cudaError_t e)
{
if (e != cudaSuccess) {
abort();
}
}
int main()
{
printf("Dimensions: %u, %u, %zu\n", blockSize, numBlocks, eltsPerThread);
T *dst = nullptr;
T *src = nullptr;
warmup<<<1, 1>>>();
check(cudaDeviceSynchronize());
check(cudaMalloc(&dst, blockSize * numBlocks * sizeof(T)));
check(cudaMallocManaged(&src, bytes));
{
printf("Starting source data generation to %p (0x%llx bytes).\n", (void *)src, (unsigned long long)bytes);
auto start = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < blockSize * numBlocks * eltsPerThread; i++) {
src[i] = i;
}
auto end = std::chrono::high_resolution_clock::now();
printf("Source number generation took %0.03f ms.\n", std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count() * 1e-6);
}
for (unsigned int i = 0; i < 20; i++) {
auto start = std::chrono::high_resolution_clock::now();
k<<<numBlocks, blockSize>>>(dst, src);
check(cudaDeviceSynchronize());
auto end = std::chrono::high_resolution_clock::now();
int64_t ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
printf("Iteration %u: %0.03f ms (%0.03f GiB/s).\n", i, ns * 1e-6, (double)bytes / ns);
}
return 0;
}
@ChrisKitching

Copy link
Copy Markdown
Author

Read more about XNACK on AMD GPUs with SCALE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment