Update to add Cuda to build system

This commit is contained in:
Alex Selimov 2025-04-15 14:10:01 -04:00
parent 8408036078
commit 68f8b02f0a
8 changed files with 120 additions and 17 deletions

View File

@ -1,17 +1,34 @@
cmake_minimum_required(VERSION 3.9) cmake_minimum_required(VERSION 3.9)
project(MyProject) project(MyProject LANGUAGES CUDA CXX)
add_compile_options(-Wall -Wextra -Wpedantic)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CUDA_ARCHITECTURES 61)
set(CUDA_SEPARABLE_COMPILATION ON)
set(SOURCE_FILES main.cpp)
add_executable(${CMAKE_PROJECT_NAME}_run ${SOURCE_FILES})
include_directories(src) include_directories(src)
include_directories(kernels)
include_directories(/usr/local/cuda-12.8/include)
add_subdirectory(src) add_subdirectory(src)
add_subdirectory(kernels)
add_subdirectory(tests) add_subdirectory(tests)
target_link_libraries(${CMAKE_PROJECT_NAME}_run ${CMAKE_PROJECT_NAME}_lib) add_executable(${CMAKE_PROJECT_NAME}_run main.cpp)
target_link_libraries(
${CMAKE_PROJECT_NAME}_run
PRIVATE
${CMAKE_PROJECT_NAME}_lib
${CMAKE_PROJECT_NAME}_cuda_lib
${CUDA_LIBRARIES}
)
# Doxygen Build # Doxygen Build
option(BUILD_DOC "Build Documentation" ON) option(BUILD_DOC "Build Documentation" ON)

18
kernels/CMakeLists.txt Normal file
View File

@ -0,0 +1,18 @@
project(${CMAKE_PROJECT_NAME}_cuda_lib CUDA CXX)
set(HEADER_FILES
hello_world.h
)
set(SOURCE_FILES
hello_world.cu
)
# The library contains header and source files.
add_library(${CMAKE_PROJECT_NAME}_cuda_lib STATIC
${SOURCE_FILES}
${HEADER_FILES}
)
if(CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(${CMAKE_PROJECT_NAME}_cuda_lib PRIVATE -Wno-gnu-line-marker)
endif()

46
kernels/hello_world.cu Normal file
View File

@ -0,0 +1,46 @@
#include <cuda_runtime.h>
#include <stdio.h>
__global__ void hello_cuda() {
printf("Hello CUDA from thread %d\n", threadIdx.x);
}
extern "C" void launch_hello_cuda() {
// First check device properties
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, 1);
printf("Using device: %s with compute capability %d.%d\n", prop.name,
prop.major, prop.minor);
hello_cuda<<<1, 10>>>();
cudaDeviceSynchronize();
fflush(stdout);
}
extern "C" void check_cuda() {
int deviceCount = 0;
cudaError_t error = cudaGetDeviceCount(&deviceCount);
if (error != cudaSuccess) {
printf("CUDA error: %s\n", cudaGetErrorString(error));
}
printf("Found %d CUDA devices\n", deviceCount);
for (int i = 0; i < deviceCount; i++) {
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, i);
printf("Device %d: %s\n", i, prop.name);
printf(" Compute capability: %d.%d\n", prop.major, prop.minor);
printf(" Total global memory: %.2f GB\n",
static_cast<float>(prop.totalGlobalMem) / (1024 * 1024 * 1024));
printf(" Multiprocessors: %d\n", prop.multiProcessorCount);
printf(" Max threads per block: %d\n", prop.maxThreadsPerBlock);
printf(" Max threads dimensions: (%d, %d, %d)\n", prop.maxThreadsDim[0],
prop.maxThreadsDim[1], prop.maxThreadsDim[2]);
printf(" Max grid dimensions: (%d, %d, %d)\n", prop.maxGridSize[0],
prop.maxGridSize[1], prop.maxGridSize[2]);
printf("\n");
}
}

10
kernels/hello_world.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef HELLO_WORLD_CU_H
#define HELLO_WORLD_CU_H
extern "C" {
// Declaration of the CUDA function that will be called from C++
void launch_hello_cuda();
void check_cuda();
}
#endif // HELLO_WORLD_CU_H

View File

@ -1,3 +1,10 @@
#include "hello_world.h"
#include <iostream>
int main() { int main() {
std::cout << "Starting CUDA example..." << std::endl; // Using endl to flush
check_cuda();
launch_hello_cuda();
std::cout << "Ending CUDA example" << std::endl; // Using endl to flush
return 0; return 0;
} }

View File

@ -1,17 +1,16 @@
project(${CMAKE_PROJECT_NAME}_lib) project(${CMAKE_PROJECT_NAME}_lib CUDA CXX)
set(HEADER_FILES set(HEADER_FILES
./test.h
) )
set(SOURCE_FILES set(SOURCE_FILES
./test.cpp
) )
if (EXISTS ${SOURCE_FILES}) # The library contains header and source files.
# The library contains header and source files. add_library(${CMAKE_PROJECT_NAME}_lib
add_library(${CMAKE_PROJECT_NAME}_lib STATIC
${SOURCE_FILES} ${SOURCE_FILES}
${HEADER_FILES} ${HEADER_FILES}
) )
else()
# The library only contains header files. target_include_directories(${CMAKE_PROJECT_NAME}_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_library(${CMAKE_PROJECT_NAME}_lib INTERFACE)
endif()

4
src/test.cpp Normal file
View File

@ -0,0 +1,4 @@
#include "test.h"
#include <iostream>
void test_hello() { std::cout << "Hello!"; }

2
src/test.h Normal file
View File

@ -0,0 +1,2 @@
#include <iostream>
void test_hello();