-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Josef Edwards <joed6834@colorado.edu>
- Loading branch information
1 parent
c6bccc3
commit cdefe81
Showing
1 changed file
with
32 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,25 +1,53 @@ | ||
#include <cuda_runtime.h> | ||
#include <stdio.h> | ||
|
||
#define MAX_ITERATIONS 1024 // Define the maximum number of iterations | ||
|
||
// Error checking macro | ||
#define CUDA_CHECK(call) | ||
do { | ||
cudaError_t err = call; | ||
if (err != cudaSuccess) { | ||
fprintf(stderr, "CUDA Error: %s\n", | ||
cudaGetErrorString(err)); | ||
exit(err); | ||
} | ||
} while (0) | ||
|
||
__global__ void PMLL_LogicLoop_GPU(int *counter) { | ||
int tid = blockIdx.x * blockDim.x + threadIdx.x; | ||
if (tid < MAX_ITERATIONS) { | ||
printf("Updating memory graph at iteration %d\n", tid); | ||
atomicAdd(counter, 1); // Safely update the counter | ||
} | ||
} | ||
|
||
int main() { | ||
int *d_counter; | ||
cudaMalloc((void **)&d_counter, sizeof(int)); | ||
cudaMemset(d_counter, 0, sizeof(int)); | ||
int h_counter = 0; // Host counter | ||
|
||
// Allocate device memory for the counter | ||
CUDA_CHECK(cudaMalloc((void **)&d_counter, sizeof(int))); | ||
// Initialize device counter | ||
CUDA_CHECK(cudaMemcpy(d_counter, &h_counter, sizeof(int), cudaMemcpyHostToDevice)); | ||
|
||
dim3 blockSize(256); | ||
dim3 gridSize((MAX_ITERATIONS + blockSize.x - 1) / blockSize.x); | ||
|
||
// Launch the kernel | ||
PMLL_LogicLoop_GPU<<<gridSize, blockSize>>>(d_counter); | ||
|
||
cudaDeviceSynchronize(); | ||
cudaFree(d_counter); | ||
// Synchronize the device | ||
CUDA_CHECK(cudaDeviceSynchronize()); | ||
|
||
// Copy the counter value back to the host | ||
CUDA_CHECK(cudaMemcpy(&h_counter, d_counter, sizeof(int), cudaMemcpyDeviceToHost)); | ||
|
||
// Display the total iterations processed | ||
printf("Total iterations processed: %d\n", h_counter); | ||
|
||
// Free device memory | ||
CUDA_CHECK(cudaFree(d_counter)); | ||
|
||
return 0; | ||
} |