ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
CodeInfo.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "CodeInfo.hpp"
21
22#include "config/config-features.hpp"
23#include "config/config-features.impl.hpp"
24#include "config/version.hpp"
26
27#include <boost/algorithm/string/join.hpp>
28
29#include <cstddef>
30#include <stdexcept>
31#include <string>
32#include <unordered_set>
33#include <vector>
34
35namespace ScriptInterface {
36namespace CodeInfo {
37
38static auto get_feature_vector(char const *const ptr[], std::size_t len) {
39 return std::vector<std::string>{ptr, ptr + len};
40}
41
42static auto get_feature_set(char const *const ptr[], std::size_t len) {
43 return std::unordered_set<std::string>(ptr, ptr + len);
44}
45
46Variant CodeInfo::do_call_method(std::string const &name, VariantMap const &) {
47 if (name == "features") {
48 return make_vector_of_variants(get_feature_vector(FEATURES, NUM_FEATURES));
49 }
50 if (name == "all_features") {
52 get_feature_vector(FEATURES_ALL, NUM_FEATURES_ALL));
53 }
54 if (name == "build_type") {
55 return std::string(ESPRESSO_BUILD_TYPE);
56 }
57 if (name == "scafacos_methods") {
58#ifdef ESPRESSO_SCAFACOS
60#else // ESPRESSO_SCAFACOS
61 return make_vector_of_variants(std::vector<std::string>(0));
62#endif // ESPRESSO_SCAFACOS
63 }
64 if (name == "has_fast_math") {
65#if defined(__FAST_MATH__) or defined(__ASSOCIATIVE_MATH__) or \
66 defined(__RECIPROCAL_MATH__)
67 return true;
68#else
69 return false;
70#endif
71 }
72 if (name == "toolchain") {
73#define ESPRESSO_REGISTER_METADATA(name) \
74 flat_map[#name] = serializer(ESPRESSO_##name);
75 VariantMap flat_map;
76 auto const serializer = [](std::string const value) {
77 Variant result = None{};
78 if (not value.empty()) {
79 result = value;
80 }
81 return result;
82 };
83 ESPRESSO_REGISTER_METADATA(CMAKE_C_COMPILER_ID)
84 ESPRESSO_REGISTER_METADATA(CMAKE_C_COMPILER_VERSION)
85 ESPRESSO_REGISTER_METADATA(CMAKE_CXX_COMPILER_ID)
86 ESPRESSO_REGISTER_METADATA(CMAKE_CXX_COMPILER_VERSION)
87 ESPRESSO_REGISTER_METADATA(CMAKE_CUDA_COMPILER_ID)
88 ESPRESSO_REGISTER_METADATA(CMAKE_CUDA_COMPILER_VERSION)
89 ESPRESSO_REGISTER_METADATA(CMAKE_CUDA_HOST_COMPILER_ID)
90 ESPRESSO_REGISTER_METADATA(CMAKE_CUDA_HOST_COMPILER_VERSION)
91 ESPRESSO_REGISTER_METADATA(OpenMP_VERSION)
92 ESPRESSO_REGISTER_METADATA(OpenMP_C_VERSION)
93 ESPRESSO_REGISTER_METADATA(OpenMP_CXX_VERSION)
94 ESPRESSO_REGISTER_METADATA(OpenMP_CUDA_VERSION)
95 ESPRESSO_REGISTER_METADATA(ESPRESSO_MPIEXEC_VENDOR)
96 ESPRESSO_REGISTER_METADATA(ESPRESSO_MPIEXEC_VERSION)
97 return flat_map;
98 }
99 return {};
100}
101
102std::string check_features_msg(std::vector<std::string> const &features) {
103 auto const allowed = get_feature_set(FEATURES_ALL, NUM_FEATURES_ALL);
104 auto const compiled_features = get_feature_set(FEATURES, NUM_FEATURES);
105 std::vector<std::string> missing_features{};
106 for (auto const &feature : features) {
107 if (not allowed.contains(feature)) {
108 return "Unknown feature '" + feature + "'";
109 }
110 if (not compiled_features.contains(feature)) {
111 missing_features.emplace_back(feature);
112 }
113 }
114 if (not missing_features.empty()) {
115 return "Missing features " + boost::algorithm::join(missing_features, ", ");
116 }
117 return "";
118}
119
120void check_features(std::vector<std::string> const &features) {
121 auto const error_msg = check_features_msg(features);
122 if (not error_msg.empty()) {
123 throw std::runtime_error(error_msg);
124 }
125}
126
127} // namespace CodeInfo
128} // namespace ScriptInterface
#define ESPRESSO_REGISTER_METADATA(name)
Variant do_call_method(std::string const &name, VariantMap const &parameters) override
Definition CodeInfo.cpp:46
Type to indicate no value in Variant.
Definition None.hpp:31
std::string_view name() const
static auto get_feature_set(char const *const ptr[], std::size_t len)
Definition CodeInfo.cpp:42
void check_features(std::vector< std::string > const &features)
Throw an exception when a feature is missing or not recognized.
Definition CodeInfo.cpp:120
std::string check_features_msg(std::vector< std::string > const &features)
Generate a message when a feature is missing or not recognized.
Definition CodeInfo.cpp:102
static auto get_feature_vector(char const *const ptr[], std::size_t len)
Definition CodeInfo.cpp:38
std::vector< std::string > available_methods()
Fetch list of methods compiled in ScaFaCoS.
Definition scafacos.cpp:50
make_recursive_variant< ObjectRef > Variant
Possible types for parameters.
Definition Variant.hpp:131
auto make_vector_of_variants(std::vector< T > const &v)
Definition Variant.hpp:148
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133