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
16 changes: 14 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,21 @@ jobs:
run: |
uv tree

- name: Get monoprop version
- name: Get monoprop information
run: |
uv run python -c "import monoprop as mp; print(mp.__version__)"
uv run python <<'EOF'
import pprint

import monoprop as mp

pprint.pprint(
{
"version": mp.__version__,
"variant": mp.__variant__,
"compiler_flags": mp.__compiler_flags__,
}
)
EOF

- name: Verify that find_package(monoprop) works
run: |
Expand Down
2 changes: 2 additions & 0 deletions benches/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ def _meta() -> dict[str, Any]:
"cpu_count_physical": psutil.cpu_count(logical=False),
"hostname": socket.gethostname(),
"monoprop_version": monoprop.__version__,
"monoprop_variant": monoprop.__variant__,
"monoprop_compiler_flags": monoprop.__compiler_flags__,
}


Expand Down
108 changes: 104 additions & 4 deletions cmake/compiler_flags/CXXFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# do not use compiler extensions to the C++ standard
set(CMAKE_CXX_EXTENSIONS FALSE)
# CMP0155 has CMake scan every C++20-or-later source for `import`s. There are no modules here, so
# the scan is pure build overhead, and it is not portable: Clang needs the separate clang-scan-deps
# binary (packaged apart from the compiler), whose absence surfaces as a build failure rather than a
# configure error. Must be set before any target is created.
# disable scanning for C++20 modules (unused)
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
# generate a JSON database of compiler commands (useful for LSP IDEs)
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
Expand All @@ -74,6 +71,109 @@ if(monoprop_ENABLE_ARCH_FLAGS AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
endif()
endif()

# Query the machine-dependent flags for a given -march value and store the
# cleaned, space-separated string in the variable named by OUTPUT_VARIABLE. A
# MARCH of "default" queries the default target (no -march flag).
#
# Usage:
# _monoprop_query_machine_flags(MARCH <arch> OUTPUT_VARIABLE <var>)
function(_monoprop_query_machine_flags)
set(
_one_value_args
MARCH
OUTPUT_VARIABLE
)
cmake_parse_arguments(PARSE_ARGV 0 _arg "" "${_one_value_args}" "")

if(NOT _arg_OUTPUT_VARIABLE)
message(
FATAL_ERROR
"_monoprop_query_machine_flags: OUTPUT_VARIABLE is required"
)
endif()
if(NOT _arg_MARCH)
message(FATAL_ERROR "_monoprop_query_machine_flags: MARCH is required")
endif()

if(_arg_MARCH STREQUAL "default")
set(_march_args "")
else()
set(_march_args "-march=${_arg_MARCH}")
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
execute_process(
COMMAND
# gersemi: off
${CMAKE_CXX_COMPILER} ${_march_args} -\#\#\# -x c++ -c /dev/null
# gersemi: on
ERROR_VARIABLE _query_output
ERROR_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _query_result
)
if(NOT _query_result EQUAL 0)
message(
WARNING
"Failed to query machine-dependent flags for '${_arg_MARCH}' with AppleClang (exit code ${_query_result}). Continuing with empty machine flags."
)
set(_flags "")
else()
execute_process(
COMMAND
${CMAKE_COMMAND} -E echo "${_query_output}"
COMMAND
${Python_EXECUTABLE}
"${PROJECT_SOURCE_DIR}/tools/target-help-clean.py" --mode clang
OUTPUT_VARIABLE _flags
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _parse_result
)
if(NOT _parse_result EQUAL 0)
message(
WARNING
"Failed to parse AppleClang machine-dependent flags for '${_arg_MARCH}' (exit code ${_parse_result}). Continuing with empty machine flags."
)
set(_flags "")
endif()
endif()
else()
execute_process(
COMMAND
${CMAKE_CXX_COMPILER} ${_march_args} -Q --help=target
COMMAND
${Python_EXECUTABLE} "${PROJECT_SOURCE_DIR}/tools/target-help-clean.py"
--mode gcc
OUTPUT_VARIABLE _flags
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE _result
)
if(NOT _result EQUAL 0)
message(
FATAL_ERROR
"Failed to query machine-dependent flags for '${_arg_MARCH}' (exit code ${_result})"
)
endif()
endif()
set(${_arg_OUTPUT_VARIABLE} "${_flags}" PARENT_SCOPE)
endfunction()

set(monoprop_DEFAULT_VARIANT_FLAGS "")
if(monoprop_ENABLE_ARCH_FLAGS)
_monoprop_query_machine_flags(MARCH native OUTPUT_VARIABLE monoprop_DEFAULT_VARIANT_FLAGS)
else()
_monoprop_query_machine_flags(MARCH default OUTPUT_VARIABLE monoprop_DEFAULT_VARIANT_FLAGS)
endif()

set(monoprop_VARIANTS "")
set(monoprop_VARIANT_FLAGS "")

# generate a header file with the macros needed to describe the variant
configure_file(
${PROJECT_SOURCE_DIR}/cpp/include/monoprop/Variants.h.in
${PROJECT_BINARY_DIR}/include/monoprop/Variants.h
@ONLY
)

set(monoprop_CXX_FLAGS "")
include(${CMAKE_CURRENT_LIST_DIR}/GNU.CXX.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/Intel.CXX.cmake)
Expand Down
5 changes: 3 additions & 2 deletions cpp/include/monoprop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ target_sources(
${PROJECT_SOURCE_DIR}/cpp/include
${PROJECT_BINARY_DIR}/include
FILES
"${PROJECT_BINARY_DIR}/include/${PROJECT_NAME}/${PROJECT_NAME}Export.h"
"${PROJECT_BINARY_DIR}/include/${PROJECT_NAME}/Info.h"
"${PROJECT_BINARY_DIR}/include/${PROJECT_NAME}/Variants.h"
"${PROJECT_SOURCE_DIR}/cpp/include/${PROJECT_NAME}/Evolution.h"
"${PROJECT_SOURCE_DIR}/cpp/include/${PROJECT_NAME}/MPFunctions.h"
"${PROJECT_SOURCE_DIR}/cpp/include/${PROJECT_NAME}/MPGraph.h"
"${PROJECT_SOURCE_DIR}/cpp/include/${PROJECT_NAME}/MonomialPropagator.h"
"${PROJECT_BINARY_DIR}/include/${PROJECT_NAME}/${PROJECT_NAME}Export.h"
"${PROJECT_BINARY_DIR}/include/${PROJECT_NAME}/Info.h"
)
4 changes: 3 additions & 1 deletion cpp/include/monoprop/Info.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <string>
#include <string_view>

#include "monoprop/Variants.h"

namespace monoprop {
static constexpr auto build_type() noexcept -> std::string_view {
return "@CMAKE_BUILD_TYPE@";
Expand All @@ -27,7 +29,7 @@ static auto compiler_flags() noexcept -> std::map<std::string, std::string> {
return {
{"from-environment", "@CMAKE_CXX_FLAGS@"},
{"build-type-flags", "@_cmake_build_type_specific_flags@"},
{"vectorization", "@ARCH_FLAG@"},
{"machine-flags", std::string(variant_flags())},
{"project-defaults", "@CMAKE_CXX23_STANDARD_COMPILE_OPTION@ @monoprop_CXX_FLAGS@"},
{"user-appended", "@EXTRA_CXXFLAGS@"},
};
Expand Down
69 changes: 69 additions & 0 deletions cpp/include/monoprop/Variants.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2026 Algorithmiq
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <string_view>

/**
* @brief Declares a compile-time function that reports the active variant.
*
* Expands to a `consteval` function named `variant()` with a GNU
* `target("arch=...")` attribute bound to the provided architecture string.
*
* @param archstr Architecture suffix used in `arch=<archstr>`.
*/
#define monoprop_VARIANT(archstr) \
[[using gnu: target("arch=" archstr)]] consteval auto variant() noexcept -> std::string_view { \
return "arch=" archstr; \
}

/**
* @brief Declares a compile-time function that reports the machine-dependent
* flags GCC applies for the given variant.
*
* Expands to a `consteval` function named `machine_flags()` with a GNU
* `target("arch=...")` attribute bound to the provided architecture string. The
* returned value is the cleaned, space-separated list of machine flags GCC uses
* for that architecture, as reported by `gcc -march=<archstr> -Q --help=target`.
*
* @param archstr Architecture suffix used in `arch=<archstr>`.
* @param flagsstr Machine-dependent flags reported for the architecture.
*/
#define monoprop_VARIANT_FLAGS(archstr, flagsstr) \
[[using gnu: target("arch=" archstr)]] consteval auto variant_flags() noexcept -> std::string_view { \
return flagsstr; \
}

namespace monoprop {
#if defined(__x86_64__) || defined(_M_X64)
[[using gnu: target("default")]]
#endif
consteval auto variant() noexcept -> std::string_view {
return "default";
}

#if defined(__x86_64__) || defined(_M_X64)
[[using gnu: target("default")]]
#endif
consteval auto variant_flags() noexcept -> std::string_view {
return "@monoprop_DEFAULT_VARIANT_FLAGS@";
}

// clang-format off
@monoprop_VARIANTS@

@monoprop_VARIANT_FLAGS@
// clang-format on
} // namespace monoprop
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ keyring-provider = "subprocess"
cache-keys = [
{ file = "pyproject.toml" },
{ file = "cpp/include/**/*.h" },
{ file = "cpp/include/**/*.h.in" },
{ file = "cpp/monoprop/**/*.{h,cpp}" },
{ file = "**/CMakeLists.txt" },
{ file = "cmake/**/*" },
Expand Down
2 changes: 2 additions & 0 deletions src/monoprop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
MAX_NUM_MODES,
__build_type__,
__compiler_flags__,
__variant__,
antihermitian_generator_correction,
has_mpi,
is_antihermitian,
Expand Down Expand Up @@ -57,6 +58,7 @@
"PauliPropagator",
"__build_type__",
"__compiler_flags__",
"__variant__",
"__version__",
"antihermitian_generator_correction",
"expand_monomials",
Expand Down
2 changes: 2 additions & 0 deletions src/monoprop/bindings/bindings.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ NB_MODULE(_core, m) {
// clang-format on
m.attr("__build_type__") = std::string(build_type());
m.attr("__compiler_flags__") = compiler_flags();
m.attr("__variant__") = std::string(variant());

#ifdef monoprop_ENABLE_MPI
m.attr("has_mpi") = true;
#else
Expand Down
Loading
Loading