From 0f13813437f91e208ac6260bdaf3d12485f341d6 Mon Sep 17 00:00:00 2001 From: Hauke Strasdat Date: Sun, 16 Jun 2024 21:34:14 -0700 Subject: [PATCH] some example --- .github/workflows/cuda_ci.yml | 22 ++++++++++++++++- Sophus.code-workspace | 19 +++++++++++++- examples/cuda/CMakeLists.txt | 19 ++++++++++++++ examples/cuda/main.cpp | 33 +++++++++++++++++++++++++ examples/cuda/veccAdd.cu | 32 ++++++++++++++++++++++++ scripts/install_ubuntu_deps_no_ceres.sh | 19 ++++++++++++++ 6 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 examples/cuda/CMakeLists.txt create mode 100644 examples/cuda/main.cpp create mode 100644 examples/cuda/veccAdd.cu create mode 100644 scripts/install_ubuntu_deps_no_ceres.sh diff --git a/.github/workflows/cuda_ci.yml b/.github/workflows/cuda_ci.yml index e727e581..6d288604 100644 --- a/.github/workflows/cuda_ci.yml +++ b/.github/workflows/cuda_ci.yml @@ -9,7 +9,7 @@ on: - main jobs: - pre-commit-check: + cuda-compile: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 @@ -30,3 +30,23 @@ jobs: - name: Check NVCC Version run: | nvcc -V + + - name: Install dependencies (Linux no Ceres) + run: ./scripts/install_ubuntu_deps_no_ceres.sh + + - name: Install test + run: | + echo "Install test" + mkdir build_dir + cd build_dir + cmake -DCMAKE_CXX_COMPILER_LAUNCHER=ccache .. -DBUILD_SOPHUS_TESTS=Off -DCMAKE_COMPILE_WARNING_AS_ERROR=On -DSOPHUS_ENABLE_ENSURE_HANDLER=$SOPHUS_ENABLE_ENSURE_HANDLER + # Ubuntu builds via Github actions run on 2-core virtual machines + make -j2 + sudo make install + cd .. + cd examples/cuda + mkdir build_dir + cd build_dir + cmake .. + make + ls -la \ No newline at end of file diff --git a/Sophus.code-workspace b/Sophus.code-workspace index b3aa3987..3d2a2e8a 100644 --- a/Sophus.code-workspace +++ b/Sophus.code-workspace @@ -64,7 +64,24 @@ "streambuf": "cpp", "thread": "cpp", "cinttypes": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "bitset": "cpp", + "charconv": "cpp", + "condition_variable": "cpp", + "forward_list": "cpp", + "format": "cpp", + "mutex": "cpp", + "span": "cpp", + "variant": "cpp", + "__bit_reference": "cpp", + "__locale": "cpp", + "__threading_support": "cpp", + "__verbose_abort": "cpp", + "ios": "cpp", + "locale": "cpp", + "print": "cpp", + "queue": "cpp", + "stack": "cpp" } } } diff --git a/examples/cuda/CMakeLists.txt b/examples/cuda/CMakeLists.txt new file mode 100644 index 00000000..40135df6 --- /dev/null +++ b/examples/cuda/CMakeLists.txt @@ -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) diff --git a/examples/cuda/main.cpp b/examples/cuda/main.cpp new file mode 100644 index 00000000..e3cd6beb --- /dev/null +++ b/examples/cuda/main.cpp @@ -0,0 +1,33 @@ +#include +#include +#include + +extern void cudaVecAdd(float* A, float* B, float* C, int N); + +int main() { + int N = 1024; + std::vector h_A(N, 0); + std::vector h_B(N, 0); + std::vector 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; +} diff --git a/examples/cuda/veccAdd.cu b/examples/cuda/veccAdd.cu new file mode 100644 index 00000000..d42995eb --- /dev/null +++ b/examples/cuda/veccAdd.cu @@ -0,0 +1,32 @@ +#include +#include + +__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<<>>(d_A, d_B, d_C, N); + + cudaMemcpy(C, d_C, size, cudaMemcpyDeviceToHost); + + cudaFree(d_A); + cudaFree(d_B); + cudaFree(d_C); +} diff --git a/scripts/install_ubuntu_deps_no_ceres.sh b/scripts/install_ubuntu_deps_no_ceres.sh new file mode 100644 index 00000000..65a39d05 --- /dev/null +++ b/scripts/install_ubuntu_deps_no_ceres.sh @@ -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 ../..