Created
August 22, 2025 15:08
-
-
Save bynect/66533126403b4429f7dc85f32d24886b to your computer and use it in GitHub Desktop.
memfrob implementation in cuda
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
#include <cuda_runtime.h> | |
__global__ void frobnicator(unsigned char *dev, int n) { | |
int tid = blockIdx.x * blockDim.x + threadIdx.x; | |
if (tid < n) | |
dev[tid] ^= 42; | |
} | |
void *cuda_memfrob(void *mem, size_t n) | |
{ | |
unsigned char *dev; | |
cudaMalloc(&dev, n); | |
cudaMemcpy(dev, mem, n, cudaMemcpyHostToDevice); | |
int threads = 256; | |
int blocks = (n + threads - 1) / threads; | |
frobnicator<<<blocks, threads>>>(dev, n); | |
cudaDeviceSynchronize(); | |
cudaMemcpy(mem, dev, n, cudaMemcpyDeviceToHost); | |
cudaFree(dev); | |
return mem; | |
} | |
#include <iostream> | |
int main() { | |
char x[] = "CAPPUCCINO\0"; | |
char y[] = "CAPPUCCINO\0"; | |
cuda_memfrob(x, sizeof(x)); | |
memfrob(y, sizeof(y)); | |
for (int i = 0; i < sizeof(x); i++) { | |
if (x[i] != y[i]) | |
puts("MISMATCH"); | |
} | |
printf("CUDA: %.*s\n", sizeof(x), x); | |
printf("CPU: %.*s\n", sizeof(y), y); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment