Skip to content

Commit

Permalink
some example
Browse files Browse the repository at this point in the history
  • Loading branch information
strasdat committed Jun 17, 2024
1 parent a0547db commit fe90957
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 2 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/cuda_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main

jobs:
pre-commit-check:
cuda-compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -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
19 changes: 18 additions & 1 deletion Sophus.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
25 changes: 25 additions & 0 deletions examples/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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)
33 changes: 33 additions & 0 deletions examples/cuda/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <cmath>
#include <iostream>
#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;
}
32 changes: 32 additions & 0 deletions examples/cuda/veccAdd.cu
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);
}
19 changes: 19 additions & 0 deletions scripts/install_ubuntu_deps_no_ceres.sh
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 ../..

0 comments on commit fe90957

Please sign in to comment.