-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
2 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
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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
cmake_minimum_required(VERSION 3.24) | ||
|
||
project(CUDAVectorAdd) | ||
|
||
enable_language(CUDA) # Enable CUDA language support | ||
|
||
set(CMAKE_CXX_STANDARD 17) | ||
set(CMAKE_CUDA_STANDARD 14) | ||
set(CMAKE_CUDA_ARCHITECTURES 52 60 61 70 75 80 86) | ||
|
||
if(NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE RelWithDebInfo) | ||
endif() | ||
|
||
add_executable(vecAdd main.cpp vecAdd.cu) | ||
|
||
set_target_properties(vecAdd PROPERTIES | ||
CUDA_SEPARABLE_COMPILATION ON) | ||
target_link_libraries(vecAdd PRIVATE cuda) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <iostream> | ||
#include <cmath> | ||
#include <vector> | ||
|
||
extern void cudaVecAdd(float* A, float* B, float* C, int N); | ||
|
||
int main() { | ||
int N = 1024; | ||
std::vector<float> h_A(N, 0); | ||
std::vector<float> h_B(N, 0); | ||
std::vector<float> h_C(N, 0); | ||
|
||
// Initialize vectors | ||
for (int i = 0; i < N; ++i) { | ||
h_A[i] = sin(i) * sin(i); | ||
h_B[i] = cos(i) * cos(i); | ||
} | ||
|
||
// Call the CUDA kernel wrapper function | ||
cudaVecAdd(h_A.data(), h_B.data(), h_C.data(), N); | ||
|
||
// Check the result | ||
for (int i = 0; i < N; ++i) { | ||
float expected = h_A[i] + h_B[i]; | ||
if (abs(h_C[i] - expected) > 1e-5) { | ||
std::cerr << "Result verification failed at element " << i << "!\n"; | ||
return EXIT_FAILURE; | ||
} | ||
} | ||
|
||
std::cout << "Test PASSED\n"; | ||
return 0; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <cuda_runtime.h> | ||
#include <iostream> | ||
|
||
__global__ void vecAddKernel(float* A, float* B, float* C, int N) { | ||
int i = blockDim.x * blockIdx.x + threadIdx than that on x; | ||
if (i < N) { | ||
C[i] = A[i] + B[i]; | ||
} | ||
} | ||
|
||
// Wrapper function for the CUDA kernel | ||
void cudaVecAdd(float* A, float* B, float* C, int N) { | ||
float *d_A, *d_B, *d_C; | ||
size_t size = N * sizeof(float); | ||
|
||
cudaMalloc(&d_A, size); | ||
cudaMalloc(&d_B, size); | ||
cudaMalloc(&d_C, size); | ||
|
||
cudaMemcpy(d_A, A, size, cudaMemcpyHostToDevice); | ||
cudaMemcpy(d_B, B, size, cudaMemcpyHostToDevice); | ||
|
||
int threadsPerBlock = 256; | ||
int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock; | ||
vecAddKernel<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, N); | ||
|
||
cudaMemcpy(C, d_C, size, cudaMemcpyDeviceToHost); | ||
|
||
cudaFree(d_A); | ||
cudaFree(d_B); | ||
cudaFree(d_C); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
|
||
set -x # echo on | ||
set -e # exit on error | ||
|
||
cmake --version | ||
|
||
sudo apt update -y | ||
sudo apt install libc++-dev libgflags-dev libsuitesparse-dev clang | ||
|
||
git clone https://gitlab.com/libeigen/eigen.git | ||
cd eigen | ||
git checkout c1d637433e3b3f9012b226c2c9125c494b470ae6 | ||
|
||
mkdir build-eigen | ||
cd build-eigen | ||
cmake .. -DEIGEN_DEFAULT_TO_ROW_MAJOR=$ROW_MAJOR_DEFAULT | ||
sudo make install | ||
cd ../.. |