Skip to content

Commit 159997b

Browse files
authored
🚀 CUDA hello world (#7)
1 parent 93c1c06 commit 159997b

File tree

7 files changed

+104
-6
lines changed

7 files changed

+104
-6
lines changed

.github/workflows/cpp-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
shell: spack-bash {0}
5656
run: |
5757
spack env activate lobxp
58-
cmake -S . -B build -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
58+
cmake -S . -B build -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DBUILD_CUDA=OFF
5959
6060
- name: Build
6161
run: cmake --build build -j $(nproc)

README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,62 @@
44

55
The purpose of this repository is to provide a space for exploring numerical methods, algorithms, and patterns in C++ in a practical and modular way. It is not intended as a tutorial or comprehensive learning resource, but rather as a set of working examples and references.
66

7-
Contributions are welcome. If you spot an issue, find an area that could be improved, or want to add your own example, feel free to open an issue or submit a pull request.
7+
Contributions are welcome. If you spot an issue, find an area that could be improved, or want to add your own example, feel free to open an issue or submit a pull request.
8+
9+
# Developper guide
10+
11+
## Spack
12+
13+
We use spack to manage dependencies and build the projects. To get started, clone the repository and run:
14+
15+
```bash
16+
git clone https://github.com/bstaber/cppplorers.git
17+
cd cppXplorers
18+
spack env activate .
19+
spack concretize -f
20+
spack install
21+
```
22+
23+
This will set up the environment and install the necessary packages. You can then enter the environment with:
24+
25+
```bash
26+
spack env activate .
27+
```
28+
29+
Note that we set the CUDA dependency externally, so if you want to use CUDA, make sure to have it installed and configured properly.
30+
31+
## Building
32+
33+
Each crate has its own `CMakeLists.txt` file and can be built independently. However, we provide a top-level `CMakeLists.txt` that can be used to build all crates at once. To build all crates, run:
34+
35+
```bash
36+
just
37+
```
38+
39+
This will run the `justfile` which builds all crates and runs all tests.
40+
41+
## Adding a new crate
42+
43+
To add a new crate, create a new directory under `crates/` and add a `CMakeLists.txt` file. You can use one of the existing crates as a template. Make sure to update the top-level `CMakeLists.txt` to include your new crate.
44+
45+
## Documenting a crate
46+
We use `mdbook` to document each crate. You can find the book source files in the `books/` directory. To build and serve the book, run:
47+
48+
```bash
49+
just serve-book
50+
```
51+
52+
This will build the book and serve it at `http://localhost:3000`. You can then navigate to the book in your web browser.
53+
54+
## Just
55+
56+
We use `just` as a task runner to automate common tasks. You can find the `justfile` in the root directory. Some common tasks include:
57+
58+
- `just build`: Build all crates
59+
- `just test`: Run all tests
60+
- `just serve-book`: Build and serve the documentation book
61+
- `just clean`: Clean all build artifacts
62+
- `just fmt`: Format all code using `clang-format`
63+
- `just lint`: Run `clang-tidy` on all code
64+
- `just configure`: Configure all crates with CMake
65+
- `just`: Configure, build, and test all crates

crates/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
add_subdirectory("kf_linear")
22
add_subdirectory("simple_optimizers")
3-
add_subdirectory("ar_models")
3+
add_subdirectory("ar_models")
4+
if(BUILD_CUDA)
5+
add_subdirectory("cuda_hello")
6+
endif()

crates/cuda_hello/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cuda_hello LANGUAGES CXX CUDA)
3+
4+
set(CMAKE_CUDA_STANDARD 17)
5+
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
6+
7+
set(CMAKE_BUILD_RPATH_USE_ORIGIN ON)
8+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH ON)
9+
10+
add_executable(cuda_hello main.cu)
11+
12+
find_package(CUDAToolkit REQUIRED)
13+
target_link_libraries(cuda_hello PRIVATE CUDA::cudart)
14+
15+
set_property(TARGET cuda_hello PROPERTY CUDA_ARCHITECTURES native)

crates/cuda_hello/main.cu

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <cstdio>
2+
3+
// Kernel function that runs on the GPU
4+
__global__ void hello_kernel() {
5+
printf("Hello from GPU thread %d in block %d!\n", threadIdx.x, blockIdx.x);
6+
}
7+
8+
int main() {
9+
// Launch 2 blocks with 4 threads each
10+
hello_kernel<<<2, 4>>>();
11+
cudaDeviceSynchronize(); // wait for GPU to finish
12+
13+
printf("Hello from CPU!\n");
14+
return 0;
15+
}

spack.lock

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

spack.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ spack:
66
specs:
77
- eigen@3.4
88
- catch2@3
9-
9+
- cuda
10+
- cmake
1011
packages:
1112
all:
1213
target: [x86_64]
@@ -15,6 +16,11 @@ spack:
1516
- spec: cmake@3.22.1
1617
prefix: /usr
1718
buildable: false
18-
19+
cuda:
20+
externals:
21+
- spec: cuda@12.0
22+
prefix: /usr/local/cuda-12.0
23+
buildable: false
1924
config:
20-
install_tree: $spack/opt/spack
25+
install_tree:
26+
root: $spack/opt/spack

0 commit comments

Comments
 (0)