|
1 | 1 | #include <cuda_runtime.h>
|
2 | 2 | #include <stdio.h>
|
3 | 3 |
|
| 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 | + |
4 | 17 | __global__ void PMLL_LogicLoop_GPU(int *counter) {
|
5 | 18 | int tid = blockIdx.x * blockDim.x + threadIdx.x;
|
6 | 19 | if (tid < MAX_ITERATIONS) {
|
7 | 20 | printf("Updating memory graph at iteration %d\n", tid);
|
| 21 | + atomicAdd(counter, 1); // Safely update the counter |
8 | 22 | }
|
9 | 23 | }
|
10 | 24 |
|
11 | 25 | int main() {
|
12 | 26 | 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)); |
15 | 33 |
|
16 | 34 | dim3 blockSize(256);
|
17 | 35 | dim3 gridSize((MAX_ITERATIONS + blockSize.x - 1) / blockSize.x);
|
18 | 36 |
|
| 37 | + // Launch the kernel |
19 | 38 | PMLL_LogicLoop_GPU<<<gridSize, blockSize>>>(d_counter);
|
20 | 39 |
|
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)); |
23 | 51 |
|
24 | 52 | return 0;
|
25 | 53 | }
|
0 commit comments