Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exp: cuda ci #548

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/cuda_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: cuda-compile

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
cuda-compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: Jimver/[email protected]
id: cuda-toolkit
with:
cuda: '12.4.1'

- name: Display CUDA version
run: |
echo "Installed CUDA version is: ${{ steps.cuda-toolkit.outputs.cuda }}"

- name: Display CUDA install location
run: |
echo "CUDA install location: ${{ steps.cuda-toolkit.outputs.CUDA_PATH }}"

- 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(kernel main.cpp kernel.cu)

set_target_properties(kernel PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(kernel PRIVATE cuda)
32 changes: 32 additions & 0 deletions examples/cuda/kernel.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);
}
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;
}
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 ../..
Loading