cmake_minimum_required(VERSION 3.23)

project(Lab1CUDA LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

# Directory holding the files you edit: sqrt.cu, matrix_add.cu, mandelbrot.cu
# (lets the teacher build the solution:
#   cmake -DLAB_SRC=/path/to/CGPU-Fall2026-Lab1-Solution/src ..)
set(LAB_SRC ${PROJECT_SOURCE_DIR}/src CACHE PATH "Directory containing the exercises")

# ---- CPU references ---------------------------------------------------------
add_executable(matrix_cpu src/matrix_cpu.cpp)
add_executable(mandelbrot_cpu src/mandelbrot_cpu.cpp)
foreach(target matrix_cpu mandelbrot_cpu)
  target_include_directories(${target} PRIVATE include)
endforeach()
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
  target_link_libraries(matrix_cpu PRIVATE OpenMP::OpenMP_CXX)
endif()

# ---- GPU --------------------------------------------------------------------
include(CheckLanguage)
check_language(CUDA)
if(CMAKE_CUDA_COMPILER)
  # Faster build: pass the architecture of the lab GPU, e.g. -DCMAKE_CUDA_ARCHITECTURES=86
  if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
    set(CMAKE_CUDA_ARCHITECTURES all-major)
  endif()
  enable_language(CUDA)
  set(CMAKE_CUDA_STANDARD 17)

  add_executable(device_query src/device_query.cu)
  add_executable(simple src/simple.cu)
  add_executable(gpu_sqrt ${LAB_SRC}/sqrt.cu)
  add_executable(gpu_sqrt_fast ${LAB_SRC}/sqrt.cu)
  add_executable(matrix_add ${LAB_SRC}/matrix_add.cu)
  add_executable(mandelbrot ${LAB_SRC}/mandelbrot.cu)

  # Same source, fast (approximate) math functions
  target_compile_options(gpu_sqrt_fast PRIVATE --use_fast_math)

  foreach(target device_query simple gpu_sqrt gpu_sqrt_fast matrix_add mandelbrot)
    target_include_directories(${target} PRIVATE include)
    target_compile_options(${target} PRIVATE -lineinfo)
  endforeach()
else()
  message(WARNING "No CUDA compiler found: only the CPU programs will be built.")
endif()
