From 8d6548bc67ed11170330beb2d01bf827bae2fde4 Mon Sep 17 00:00:00 2001 From: Neil Kichler Date: Thu, 20 Feb 2025 00:55:18 +0100 Subject: [PATCH] main.cu only includes main project header --- src/main.cu | 51 +-------------------------------------------------- 1 file changed, 1 insertion(+), 50 deletions(-) diff --git a/src/main.cu b/src/main.cu index 3bf56b9..27c0dd0 100644 --- a/src/main.cu +++ b/src/main.cu @@ -1,50 +1 @@ -#include -#include - -// Kernel function to add the elements of two arrays -__global__ void add(int n, float *x, float *y) -{ - - int index = threadIdx.x + blockDim.x * blockIdx.x; - int stride = blockDim.x * gridDim.x; - - for (int i = index; i < n; i += stride) - y[i] = x[i] + y[i]; -} - -int main(void) -{ - int N = 1 << 20; - float *x, *y; - - // Allocate Unified Memory – accessible from CPU or GPU - cudaMallocManaged(&x, N * sizeof(float)); - cudaMallocManaged(&y, N * sizeof(float)); - - // initialize x and y arrays on the host - for (int i = 0; i < N; i++) { - x[i] = 1.0f; - y[i] = 2.0f; - } - - int block_size = 256; - int num_blocks = (N + block_size - 1) / block_size; - - // Run kernel on 1M elements on the GPU - add<<>>(N, x, y); - - // Wait for GPU to finish before accessing on host - cudaDeviceSynchronize(); - - // Check for errors (all values should be 3.0f) - float maxError = 0.0f; - for (int i = 0; i < N; i++) - maxError = fmax(maxError, fabs(y[i] - 3.0f)); - std::cout << "Max error: " << maxError << std::endl; - - // Free memory - cudaFree(x); - cudaFree(y); - - return 0; -} +#include