8000 C++20 module for Kokkos std algorithms and basic setup by masterleinad · Pull Request #8132 · kokkos/kokkos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

C++20 module for Kokkos std algorithms and basic setup #8132

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

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

masterleinad
Copy link
Contributor

Part of #8117 to get started. Out of all the Kokkos functionalities, StdAlgorithms seems to be easiest to provide a module for.
This pull request adds a build with clang-19 (currently in jenkins stage-1 for convenience; I'm happy to discuss moving whereever) and just lists all symbols needed explicitly. Since we are testing some Impl functionalities in the test, we split the module into kokkos.stdalgorithms and kokkos.stdalgorithms_impl with the understanding that the latter shouldn't be used downstream.
The alternative to listing all symbolds would be duplicating all header files, possibly generating while configuring. #8117 demonstrates that for the containers.

@masterleinad masterleinad force-pushed the kokkos_module_stdalgorithms branch 2 times, most recently from 4c95efe to cf31d99 Compare May 30, 2025 20:30
@masterleinad masterleinad requested a review from pzehner May 30, 2025 20:30
@masterleinad masterleinad force-pushed the kokkos_module_stdalgorithms branch from cf31d99 to 2e335b5 Compare May 30, 2025 20:30
Signed-off-by: Daniel Arndt <arndtd@ornl.gov>
@masterleinad masterleinad force-pushed the kokkos_module_stdalgorithms branch from 7e02fa7 to aed24a0 Compare June 3, 2025 15:13
@science-enthusiast
Copy link
Contributor

I am interested in the algorithms section of Kokkos and I feel like taking a look. I am wondering why sort and random are left out?

@masterleinad
Copy link
Contributor Author

I am interested in the algorithms section of Kokkos and I feel like taking a look. I am wondering why sort and random are left out?

I was going to do them later since they either use configuration macros or have more complicated dependencies on other parts of the library (like fill_random for DynRankView). The point here is to get the basic CMake setup in place (while still creating at least one module).

@masterleinad masterleinad added the C++20 Related to the transition to C++ 20 label Jun 4, 2025
@masterleinad masterleinad requested a review from nmm0 June 4, 2025 16:37
@masterleinad
Copy link
Contributor Author
masterleinad commented Jun 10, 2025

What do others do:
https://github.com/dealii/dealii:

  • creates for every external dependency (including the C++ Standard Library) a module partition explicitly by listing all used symbols
  • for every internal header generate a corresponding module partition file replacing #includes with imports. There was some prior work needed that every header is self-contained (up to including a configuration file) and that there are no cyclic dependencies (which are not allowed with C++20 modules)
  • for every internal source file generate a corresponding module fragment file. These are leaf nodes that are never used in other files.
  • create one module from all the module partitions
  • enabling module support builds both a regular and a "module" target

https://github.com/hanickadot/compile-time-regular-expressions:

  • use macros to decide which symbols to export (somewhat similar to listing all symbols in a separate file in effect but much more intrusive). No file generation.
  • create one module by including all relevant header filles (doesn't use module partitions).
  • enabling module support also makes the header files visible (header-only library).

https://github.com/fmtlib/fmt:

  • use macros to decide which symbols to export, use macros to decide when to start exporting and stop exporting. There aren't many files. No file generation.
  • create one module by including all relevant header filles (doesn't use module partitions).
  • enabling module support also makes the header files visible.

https://github.com/llvm/llvm-project/tree/main/libcxx/modules/std:

@masterleinad masterleinad changed the title C++20 module for Kokkos algorithms and basic setup C++20 module for Kokkos std algorithms and basic setup Jun 11, 2025
…ure-time

Signed-off-by: Daniel Arndt <arndtd@ornl.gov>
@masterleinad masterleinad force-pushed the kokkos_module_stdalgorithms branch from 108b5ad to c022176 Compare June 11, 2025 21:17
@masterleinad
Copy link
Contributor Author

The last commit c022176 provides an alternative to modifying the test files directly by copying them to the build directory and then replacing includes with imports for headers that have a corresponding module.
I would rather see the explicit changes in our test files (which would look cleaner when compared to the generated files) but I'm curious about feedback.

endif()
endforeach()
get_filename_component(FILE_NAME ${FILE} NAME)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}" "${HEADER_CONTENT}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case of an install ... would this still work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't install test headers if that's what you mean.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah

Copy link
Contributor
@pzehner pzehner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Algorithms seem easier to modularize indeed.

I have concerns about the test headers that have to be duplicated and modified by CMake. I think this may be avoided.

What would be the problem of having the export in the source files directly (protected by macros, of course)? (Rather than making an explicit listing, or modifying the source files on-the-fly like in #8117.) I'm not sure about the best way to expose the modules.

I'm interested to see where this is going.

@@ -9,6 +9,8 @@ file(GLOB ALGO_SOURCES *.cpp)
append_glob(ALGO_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/std_algorithms/*.hpp)
append_glob(ALGO_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/std_algorithms/impl/*.hpp)

set(KOKKOS_ALGORITHMS_MODULE_FILES Kokkos_StdAlgorithms.ccm Kokkos_StdAlgorithmsImpl.ccm)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to prefix this variable, as it is internal? To reflect ALGO_HEADERS and ALGO_SOURCES, I'd propose ALGO_MODULES.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine with me.

Comment on lines 11 to 23
foreach(FILE ${TEST_HEADERS})
file(READ ${FILE} HEADER_CONTENT)
foreach(PAIR IN LISTS MODULE_MAP)
if(PAIR MATCHES "^([^:]+):(.*)$")
set(HEADER "${CMAKE_MATCH_1}")
set(MODULE "${CMAKE_MATCH_2}")
string(REPLACE "${HEADER}" "#include <Kokkos_Macros.hpp>\n${MODULE};\n${MODULE}_impl;" HEADER_CONTENT
"${HEADER_CONTENT}"
)
endif()
endforeach()
get_filename_component(FILE_NAME ${FILE} NAME)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}" "${HEADER_CONTENT}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, you duplicate all test header files and replace the #include ... by import ..., but why not modify the test header files just like you modified the test files?

This is better than modifying files in the source tree on-the-fly, but I'm still wondering if this is really needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't really modify files in the source tree anyway with our current CMake settings.
That being said, I would rather have the modifications to use modules instead of includes directly in the source and test files (than generating files in the build tree).

Comment on lines +24 to +45
namespace Kokkos::Experimental::Impl {
using ::Kokkos::Experimental::Impl::are_iterators_v;
using ::Kokkos::Experimental::Impl::are_random_access_iterators_v;
using ::Kokkos::Experimental::Impl::
ExclusiveScanDefaultFunctorWithValueWrapper;
using ::Kokkos::Experimental::Impl::
ExeSpaceTransformInclusiveScanNoInitValueFunctor;
using ::Kokkos::Experimental::Impl::
ExeSpaceTransformInclusiveScanWithInitValueFunctor;
using ::Kokkos::Experimental::Impl::expect_no_overlap;
using ::Kokkos::Experimental::Impl::InclusiveScanDefaultFunctor;
using ::Kokkos::Experimental::Impl::is_admissible_to_kokkos_std_algorithms;
using ::Kokkos::Experimental::Impl::RandomAccessIterator;
using ::Kokkos::Experimental::Impl::
static_assert_is_admissible_to_kokkos_std_algorithms;
using ::Kokkos::Experimental::Impl::StdAlgoLessThanBinaryPredicate;
using ::Kokkos::Experimental::Impl::
StdNumericScanIdentityReferenceUnaryFunctor;
using ::Kokkos::Experimental::Impl::
TransformExclusiveScanFunctorWithValueWrapper;
using ::Kokkos::Experimental::Impl::ValueWrapperForNoNeutralElement;
} // namespace Kokkos::Experimental::Impl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think there are some Impl symbols that should be on the public API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, not here. If we need to expose them to the user, they shouldn't be in Impl.
I recognize that we have some symbols in core that are used as customization points, though, and we will have to expose them (until they are moved out of the Impl namespace).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, it's a good timing to "promote" some of them that may be exposed, on second thought, if any.

@masterleinad
Copy link
Contributor Author

I have concerns about the test headers that have to be duplicated and modified by CMake. I think this may be avoided.

I presented to approaches and prefer the first one that modifies the test files directly instead of generating new files. @dalg24 preferred to minimize changes to existing files and so I prototyped an alternative.

What would be the problem of having the export in the source files directly (protected by macros, of course)? (Rather than making an explicit listing, or modifying the source files on-the-fly like in #8117.) I'm not sure about the best way to expose the modules.

We can do that but it's much more intrusive than the other approaches. From my last discussion with @dalg24, we want an approach that keeps changes to existing files small (so it would be easy enough to revert in case we run into major problems). The compromise I'm proposing is to limit the changes to includes.

Signed-off-by: Daniel Arndt <arndtd@ornl.gov>
…t configure-time"

This reverts commit c022176.

Signed-off-by: Daniel Arndt <arndtd@ornl.gov>
Signed-off-by: Daniel Arndt <arndtd@ornl.gov>
@masterleinad masterleinad force-pushed the kokkos_module_stdalgorithms branch from cd21e5b to 6bad1d1 Compare June 13, 2025 15:56
@masterleinad masterleinad requested a review from pzehner June 13, 2025 15:56
Signed-off-by: Daniel Arndt <arndtd@ornl.gov>
@masterleinad masterleinad force-pushed the kokkos_module_stdalgorithms branch from 6bad1d1 to 297f29a Compare June 13, 2025 15:56
Copy link
Contributor
@pzehner pzehner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. I prefer the compromise you ended up with.

@masterleinad masterleinad requested a review from JBludau June 16, 2025 15:00
Copy link
Contributor
@JBludau JBludau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the approach for integrating the module with minimal changes to existing stuff is fine. But we should not fragment examples or Dockerfiles.

@masterleinad masterleinad requested a review from JBludau June 17, 2025 13:31
Copy link
Contributor
@JBludau JBludau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a valid approach for having a module but keeping our old ways intact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C++20 Related to the transition to C++ 20
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0