Skip to content
Open
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ repos:
hooks:
- id: clang-format
name: clang-format (C/C++)
files: \.(c|cpp|cc|cxx|h|hpp|hh)$
files: \.(c|cpp|cc|cxx|cu|h|hpp|hh)$
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
Expand Down
25 changes: 21 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ option(LIBDIFFPY_TEST_INSTALLED
"Build only the unit tests and link them with the installed libdiffpy" OFF)
set(LIBDIFFPY_TESTS "" CACHE STRING
"Comma or space separated patterns selecting the Test*.cpp unit test files")
option(LIBDIFFPY_CUDA "Build the CUDA calculation kernels" OFF)
option(LIBDIFFPY_COVERAGE "Instrument the build for coverage analysis" OFF)
option(LIBDIFFPY_PROFILE "Build with gprof profiling information" OFF)

Expand Down Expand Up @@ -78,10 +79,13 @@ if(PROJECT_IS_TOP_LEVEL)
if(MSVC)
# /W3 rather than /Wall, which floods the log with warnings from the
# standard library and boost headers.
add_compile_options(/W3)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/W3>)
add_link_options(/OPT:NOREF /OPT:NOICF)
else()
add_compile_options(-Wall $<$<CONFIG:Release>:-ffast-math>)
# for the C++ sources, nvcc does not take these options
add_compile_options(
$<$<COMPILE_LANGUAGE:CXX>:-Wall>
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:Release>>:-ffast-math>)
endif()
endif()

Expand All @@ -91,12 +95,12 @@ if(MSVC AND (LIBDIFFPY_COVERAGE OR LIBDIFFPY_PROFILE))
endif()

if(LIBDIFFPY_COVERAGE)
add_compile_options(--coverage -O0 -g)
add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:--coverage;-fprofile-update=atomic;-O0;-g>")
add_link_options(--coverage)
endif()

if(LIBDIFFPY_PROFILE)
add_compile_options(-pg)
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-pg>)
add_link_options(-pg)
endif()

Expand Down Expand Up @@ -247,6 +251,11 @@ if(LIBDIFFPY_HAS_OBJCRYST)
else()
set(DIFFPY_HAS_OBJCRYST 0)
endif()
if(LIBDIFFPY_CUDA)
set(DIFFPY_HAS_CUDA 1)
else()
set(DIFFPY_HAS_CUDA 0)
endif()
set(_features_header "${_generated_include_dir}/diffpy/features.hpp")
configure_file("${PROJECT_SOURCE_DIR}/src/diffpy/features.tpl"
"${_features_header}")
Expand Down Expand Up @@ -343,11 +352,19 @@ if(LIBDIFFPY_HAS_OBJCRYST)
target_link_libraries(diffpy PUBLIC ObjCryst::ObjCryst)
endif()
include(CalculationKernels)
if(LIBDIFFPY_CUDA)
include(Cuda)
endif()

# Installation ----------------------------------------------------------------

if(LIBDIFFPY_INSTALL)
set(LIBDIFFPY_CMAKE_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/libdiffpy")
if(LIBDIFFPY_CUDA AND NOT _libdiffpy_library_type STREQUAL "SHARED_LIBRARY")
# A static libdiffpy leaves the CUDA kernels to the program that links it.
install(TARGETS diffpy_cuda EXPORT libdiffpyTargets
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT Development)
endif()

install(TARGETS diffpy EXPORT libdiffpyTargets
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT Runtime
Expand Down
72 changes: 53 additions & 19 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@
"CMAKE_GENERATOR_TOOLSET": ""
}
},
{
"name": "debug",
"inherits": "ninja",
"displayName": "Debug build with C assertions and unit tests",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"LIBDIFFPY_BUILD_TESTS": "ON"
}
},
{
"name": "debug-cuda",
"inherits": "debug",
"displayName": "Debug build with CUDA kernels and unit tests",
"cacheVariables": {
"LIBDIFFPY_CUDA": "ON"
}
},
{
"name": "release",
"inherits": "ninja",
Expand All @@ -26,35 +43,42 @@
}
},
{
"name": "debug",
"inherits": "ninja",
"displayName": "Debug build with C assertions and unit tests",
"name": "release-cuda",
"inherits": "release",
"displayName": "Optimized build with CUDA kernels",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"LIBDIFFPY_BUILD_TESTS": "ON"
"LIBDIFFPY_CUDA": "ON"
}
},
{
"name": "coverage",
"inherits": "ninja",
"inherits": "debug",
"displayName": "Debug build instrumented for coverage analysis",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"LIBDIFFPY_BUILD_TESTS": "ON",
"LIBDIFFPY_COVERAGE": "ON"
}
}
],
"buildPresets": [
{
"name": "debug",
"configurePreset": "debug",
"configuration": "Debug"
},
{
"name": "debug-cuda",
"configurePreset": "debug-cuda",
"configuration": "Debug"
},
{
"name": "release",
"configurePreset": "release",
"configuration": "Release"
},
{
"name": "debug",
"configurePreset": "debug",
"configuration": "Debug"
"name": "release-cuda",
"configurePreset": "release-cuda",
"configuration": "Release"
},
{
"name": "coverage",
Expand All @@ -74,16 +98,26 @@
"outputOnFailure": true
}
},
{
"name": "debug-cuda",
"inherits": "debug",
"configurePreset": "debug-cuda"
},
{
"name": "release",
"inherits": "debug",
"configurePreset": "release",
"configuration": "Release"
},
{
"name": "release-cuda",
"inherits": "release",
"configurePreset": "release-cuda"
},
{
"name": "coverage",
"configurePreset": "coverage",
"configuration": "Debug",
"execution": {
"noTestsAction": "error"
},
"output": {
"outputOnFailure": true
}
"inherits": "debug",
"configurePreset": "coverage"
}
]
}
11 changes: 9 additions & 2 deletions cmake/CalculationKernels.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ endif()

# The main source glob is not recursive, add the kernels explicitly.
set(_kernel_dir "${PROJECT_SOURCE_DIR}/src/diffpy/srreal/kernels")
find_package(Threads REQUIRED)
target_link_libraries(diffpy PRIVATE Threads::Threads)
target_sources(diffpy PRIVATE
"${_kernel_dir}/CalculationKernelCPU.cpp"
"${_kernel_dir}/CalculationKernelScalar.cpp"
"${_kernel_dir}/CalculationKernelDispatch.cpp")
"${_kernel_dir}/CalculationKernelDispatch.cpp"
"${_kernel_dir}/PairTable.cpp")
set(_kernel_names scalar)

# All kernels need IEEE arithmetic to give consistent results.
Expand All @@ -51,7 +55,10 @@ if(MSVC)
else()
set(_kernel_math_options -fno-fast-math -ffp-contract=off)
endif()
set_source_files_properties("${_kernel_dir}/CalculationKernelScalar.cpp"
set_source_files_properties(
"${_kernel_dir}/CalculationKernelCPU.cpp"
"${_kernel_dir}/CalculationKernelScalar.cpp"
"${_kernel_dir}/PairTable.cpp"
PROPERTIES COMPILE_OPTIONS "${_kernel_math_options}")

# Inline functions compiled for the SIMD instruction sets must not be shared
Expand Down
37 changes: 37 additions & 0 deletions cmake/Cuda.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# CUDA calculation kernels, see
# src/diffpy/srreal/kernels/CalculationKernelDispatch.cpp.
#
# The CUDA source makes the static library diffpy_cuda, which the diffpy
# library links. It is compiled by nvcc and statically linked with its
# own C++ standard and without the options of the C++ sources.

include(CheckLanguage)
check_language(CUDA)
if(NOT CMAKE_CUDA_COMPILER)
message(FATAL_ERROR
"LIBDIFFPY_CUDA needs the CUDA compiler nvcc. Set CMAKE_CUDA_COMPILER "
"or the environment variable CUDACXX to it.")
endif()
enable_language(CUDA)

add_library(diffpy_cuda STATIC
"${PROJECT_SOURCE_DIR}/src/diffpy/srreal/kernels/CalculationKernelCuda.cu")
set_target_properties(diffpy_cuda PROPERTIES
CUDA_STANDARD 17
CUDA_STANDARD_REQUIRED ON
CUDA_EXTENSIONS OFF
CUDA_RUNTIME_LIBRARY Static
POSITION_INDEPENDENT_CODE ON
# The objects of nvcc have no data for link-time optimization.
INTERPROCEDURAL_OPTIMIZATION OFF)
target_include_directories(diffpy_cuda PRIVATE "${PROJECT_SOURCE_DIR}/src")
# Use the CPU kernels' rounding order for distances, widths and grid cutoffs.
target_compile_options(diffpy_cuda PRIVATE
$<$<CUDA_COMPILER_ID:NVIDIA>:--fmad=false>
$<$<CUDA_COMPILER_ID:Clang>:-ffp-contract=off>)
# The runtime is named here for the programs that link diffpy_cuda through
# a static or OBJECT diffpy library, whose link step is not one of CUDA.
find_package(CUDAToolkit REQUIRED)
target_link_libraries(diffpy_cuda PRIVATE CUDA::cudart_static)
target_link_libraries(diffpy PRIVATE diffpy_cuda)
target_compile_definitions(diffpy PRIVATE LIBDIFFPY_KERNEL_CUDA=1)
6 changes: 6 additions & 0 deletions cmake/libdiffpyConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
# Variables:
#
# libdiffpy_HAS_OBJCRYST true when libdiffpy was built with ObjCryst
# libdiffpy_HAS_CUDA true when libdiffpy has the CUDA kernels
# libdiffpy_RUNTIME_DIR directory of the installed data files

@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

set(libdiffpy_HAS_OBJCRYST @LIBDIFFPY_HAS_OBJCRYST@)
set(libdiffpy_HAS_CUDA @LIBDIFFPY_CUDA@)
set_and_check(libdiffpy_RUNTIME_DIR "@PACKAGE_LIBDIFFPY_RUNTIME_INSTALL_DIR@")

find_dependency(Boost CONFIG COMPONENTS serialization)
if(NOT "@_libdiffpy_library_type@" STREQUAL "SHARED_LIBRARY")
find_dependency(GSL)
find_dependency(Threads)
if(libdiffpy_HAS_CUDA)
find_dependency(CUDAToolkit)
endif()
endif()
if(libdiffpy_HAS_OBJCRYST)
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
Expand Down
28 changes: 25 additions & 3 deletions docs/source/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ The CMake presets require CMake 3.22 or later and use Ninja. On Windows,
run them in a command prompt with the conda build environment activated.
The build presets are

* ``release`` - optimized build with C assertion checks disabled
* ``debug`` - unoptimized build with debug information and C assertion checks
* ``debug-cuda`` - debug build with CUDA calculation kernels
* ``release`` - optimized build with C assertion checks disabled
* ``release-cuda`` - optimized build with CUDA calculation kernels
* ``coverage`` - unoptimized build instrumented for coverage analysis

The library is built in the ``build/<preset>`` directory, for example
Expand All @@ -48,8 +50,8 @@ ObjCryst adapter, pass ``-DLIBDIFFPY_ENABLE_OBJCRYST=OFF``. For gprof
instrumentation, use ``-DLIBDIFFPY_PROFILE=ON``. To build the optimized
shared library without link-time optimization, pass ``-DLIBDIFFPY_LTO=OFF``.

Calculation kernels
-------------------
SIMD calculation kernels
------------------------

``PDFCalculator`` and ``DebyePDFCalculator`` run their inner loops in a
calculation kernel. The scalar kernel is always built. With the default
Expand All @@ -61,6 +63,26 @@ The SIMD kernels are ``avx512``, ``avx2`` and ``sse2`` on x86-64 and ``neon``
on arm64. On first use the library picks the best kernel that the CPU
supports, for example ``avx2`` on a CPU without AVX-512.

CUDA calculation kernels
------------------------

Build and test with the ``debug-cuda`` preset ::

cmake --preset debug-cuda
cmake --build --preset debug-cuda --parallel 4
ctest --preset debug-cuda

Use ``release-cuda`` for an optimized build. To run the tests with either
release preset, pass ``-DLIBDIFFPY_BUILD_TESTS=ON`` when configuring, then
build and run CTest with the same preset.
To select ``nvcc`` or its host C++ compiler, set ``CUDACXX`` or
``CUDAHOSTCXX``, respectively, before the first configure.

On a compatible GPU, ``availableCalculationKernels`` lists ``cuda``
(double precision) and ``cuda-mixed`` (single-precision sampling with
double-precision distances and sums). Without a compatible GPU, CPU
kernels remain available and the ``[cuda]`` tests skip.

Embed libdiffpy in another CMake project
----------------------------------------

Expand Down
40 changes: 40 additions & 0 deletions news/cuda.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
**Added:**

* Build with ``LIBDIFFPY_CUDA=ON`` or either CUDA preset (``debug-cuda``,
``release-cuda``) to enable the ``cuda`` and ``cuda-mixed`` kernels for full
PDF and Debye sums. Query build support with ``libdiffpy_HAS_CUDA`` (CMake)
or ``DIFFPY_HAS_CUDA`` (C++).
CUDA-enabled builds also run on CPU-only machines.
* Derived calculators can implement ``addAllPairContributions`` or reuse
``addAllPairsOfPDF`` and ``addAllPairsOfDebyeSum``. The constant, Debye-Waller
and Jeong peak width models now provide ``calculateFromMSD``.

**Changed:**

* ``PDFCalculator`` and ``DebyePDFCalculator`` accelerate full sums with
batched pairs and multiple CPU threads, up to the hardware concurrency
reported by the system. Changing the summation order can affect rounding.
* ``OPTIMIZED`` can choose a full calculation when it costs less than updating
changed sites. ``getEvaluatorTypeUsed`` then returns ``BASIC``.
* ``PDFCalculator::getQmax`` and ``getQstep`` now return ``double`` by value.

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Separate calculators can run concurrently and share a read-only structure.
* ``DebyePDFCalculator`` uses each site's own occupancy when several sites
share an element.
* The ``OPTIMIZED`` evaluator recalculates when a change of adapter class
changes the structure, for example when a cluster becomes periodic.
* Copies of a calculator now have independent evaluators.

**Security:**

* <news item>
Loading
Loading