Skip to content

Commit cdefe81

Browse files
authored
Update GPUAceel
Signed-off-by: Josef Edwards <joed6834@colorado.edu>
1 parent c6bccc3 commit cdefe81

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

GPUAceel

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,53 @@
11
#include <cuda_runtime.h>
22
#include <stdio.h>
33

4+
#define MAX_ITERATIONS 1024 // Define the maximum number of iterations
5+
6+
// Error checking macro
7+
#define CUDA_CHECK(call)
8+
do {
9+
cudaError_t err = call;
10+
if (err != cudaSuccess) {
11+
fprintf(stderr, "CUDA Error: %s\n",
12+
cudaGetErrorString(err));
13+
exit(err);
14+
}
15+
} while (0)
16+
417
__global__ void PMLL_LogicLoop_GPU(int *counter) {
518
int tid = blockIdx.x * blockDim.x + threadIdx.x;
619
if (tid < MAX_ITERATIONS) {
720
printf("Updating memory graph at iteration %d\n", tid);
21+
atomicAdd(counter, 1); // Safely update the counter
822
}
923
}
1024

1125
int main() {
1226
int *d_counter;
13-
cudaMalloc((void **)&d_counter, sizeof(int));
14-
cudaMemset(d_counter, 0, sizeof(int));
27+
int h_counter = 0; // Host counter
28+
29+
// Allocate device memory for the counter
30+
CUDA_CHECK(cudaMalloc((void **)&d_counter, sizeof(int)));
31+
// Initialize device counter
32+
CUDA_CHECK(cudaMemcpy(d_counter, &h_counter, sizeof(int), cudaMemcpyHostToDevice));
1533

1634
dim3 blockSize(256);
1735
dim3 gridSize((MAX_ITERATIONS + blockSize.x - 1) / blockSize.x);
1836

37+
// Launch the kernel
1938
PMLL_LogicLoop_GPU<<<gridSize, blockSize>>>(d_counter);
2039

21-
cudaDeviceSynchronize();
22-
cudaFree(d_counter);
40+
// Synchronize the device
41+
CUDA_CHECK(cudaDeviceSynchronize());
42+
43+
// Copy the counter value back to the host
44+
CUDA_CHECK(cudaMemcpy(&h_counter, d_counter, sizeof(int), cudaMemcpyDeviceToHost));
45+
46+
// Display the total iterations processed
47+
printf("Total iterations processed: %d\n", h_counter);
48+
49+
// Free device memory
50+
CUDA_CHECK(cudaFree(d_counter));
2351

2452
return 0;
2553
}

0 commit comments

Comments
 (0)