Skip to content

Commit

Permalink
Add Windows support (#930)
Browse files Browse the repository at this point in the history
* Fix corecel errors/warnings on windows

Remove isatty for windows:
```
E:\scale\external\celeritas\src\corecel\io\ColorUtils.cc(13): fatal
error C1083: Cannot open include file: 'unistd.h': No such file or directory
```

String view iterator is not a const char*:
```
E:\scale\external\celeritas\src\corecel\io\StringUtils.cc(66): note:
'std::basic_string_view<char,std::char_traits<char>>::
basic_string_view(const char *const ,const
std::basic_string_view<char,std::char_traits<char>>::size_type)
noexcept': canno
t convert argument 1 from 'std::_String_view_iterator<_Traits>' to
'const char *const '
        with
    [
        _Traits=std::char_traits<char>
    ]
```

'environ' is a macro on windows (boo)
```
E:\scale\external\celeritas\src\corecel\sys\Environment.cc(31): error
C2267: '__p__environ': static functions with block scope are illegal
```

psapi must be included *after* windows.h
```
C:\Program Files (x86)\Windows
Kits\10\\include\10.0.22621.0\\um\psapi.h(132): error C2146: syntax error: missing ';' before identifier 'WINAPI'
```

ptrdiff_t is implicitly converted to long
```
E:\scale\external\celeritas\src\corecel\sys\ScopedMem.cc(125): warning
C4244: 'argument': conversion from 'ptrdiff_t' to 'long', possible loss
of dat
a
```

* Fix truncation warning

* Fix missing include

* Instantiate aligned plane explicitly

* Use 'if constexpr' to avoid choking msvc

This might have relied on compiler optimization stopping the template
instantiation early. With 'if constexpr' this should always be correct.

* Define windows macros

* Fix compiler failure from divide by zero

```
E:\scale\external\celeritas\test\corecel\math\NumericLimits.test.cc(40):
error C2124: divide or mod by zero
```

* Fix integer overflow in windows integer literal

```
E:\scale\external\celeritas\src\celeritas/field/DormandPrinceStepper.hh(144):
warning C4146: unary minus operator applied to unsigned type, result st
ill unsigned
```

* Fix conversion warnings

* Disable dominance warning

```
E:\scale\external\celeritas\src\celeritas\geo\detail\BoundaryAction.hh(37):
warning C4250: 'celeritas::detail::BoundaryAction': inherits
'celeritas::C
oncreteAction::celeritas::ConcreteAction::action_id' via dominance
E:\scale\external\celeritas\src\celeritas/global/ActionInterface.hh(151):
note: see declaration of 'celeritas::ConcreteAction::action_id'
E:\scale\external\celeritas\src\celeritas\geo\detail\BoundaryAction.hh(37):
warning C4250: 'celeritas::detail::BoundaryAction': inherits
'celeritas::C
oncreteAction::celeritas::ConcreteAction::label' via dominance
E:\scale\external\celeritas\src\celeritas/global/ActionInterface.hh(154):
note: see declaration of 'celeritas::ConcreteAction::label'
E:\scale\external\celeritas\src\celeritas\geo\detail\BoundaryAction.hh(37):
warning C4250: 'celeritas::detail::BoundaryAction': inherits
'celeritas::C
oncreteAction::celeritas::ConcreteAction::description' via dominance
```

* Fix implicit cast in step conversion

* Fix MSVC implicit capture weirdness

There are weird scoping rules about static const for lambdas.
```
E:\scale\external\celeritas\test\celeritas\track\TrackInit.test.cc(417):
error C3493: 'num_tracks' cannot be implicitly captured because no
default ca
pture mode has been specified
```

* Fix use of SIGUSR2

```
E:\scale\external\celeritas\app\celer-sim\Transporter.cc(70): error
C2065: 'SIGUSR2': undeclared identifier
```

* Fix narrowing conversion

```
E:\scale\external\celeritas\test\celeritas\em\UrbanMsc.test.cc(281):
error C2397: conversion from 'const int' to 'T' requires a narrowing
conversion
with [T=celeritas::real_type ]
```

More conversion warnings

* Fix missing symbol

* Work around windows environ definition

* Explicitly specify enum type

```
E:\scale\external\celeritas\test\corecel\cont\Range.test.cc(215): error:
Value of: (std::is_same<std::underlying_type<pokemon::Pokemon>::type,
unsigne
d int>::value)
  Actual: false
  Expected: true
```

* Fix windows issues with paths

* Improve dependency logic for building inside another project

* Clang-format

* Work around dumb GCC attribute warning

* Document

* Add default type to quantity based on underlying unit

* Mark quantity functions as noexcept

* Use size_t instead of ptrdiff_t and long int for scoped mem
  • Loading branch information
sethrj authored Sep 13, 2023
1 parent 6fbc58f commit 92133db
Show file tree
Hide file tree
Showing 52 changed files with 289 additions and 161 deletions.
29 changes: 18 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ if(BUILD_SHARED_LIBS)
# Do not relink libs/binaries when dependent shared libs change
celeritas_set_default(CMAKE_LINK_DEPENDS_NO_SHARED ON)
endif()
if(BUILD_SHARED_LIBS OR CELERITAS_USE_ROOT)
# Make sure modules, sub-libraries, etc. are relocatable
celeritas_set_default(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()

### Installation flags ###
# When developing add checking for proper usage of `install(`
Expand Down Expand Up @@ -219,13 +223,17 @@ if(CELERITAS_USE_Geant4 AND NOT Geant4_FOUND)
find_package(Geant4 REQUIRED)
endif()

if(CELERITAS_USE_HepMC3 AND NOT HepMC3_FOUND)
find_package(HepMC3 REQUIRED)
if(CELERITAS_USE_HepMC3)
if(NOT HepMC3_FOUND)
find_package(HepMC3 REQUIRED)
endif()
set(HepMC3_LIBRARIES HepMC3::HepMC3)
endif()

if(CELERITAS_USE_JSON AND NOT nlohmann_json_FOUND)
find_package(nlohmann_json 3.7.0 REQUIRED)
if(CELERITAS_USE_JSON)
if(NOT nlohmann_json_FOUND)
find_package(nlohmann_json 3.7.0 REQUIRED)
endif()
set(nlohmann_json_LIBRARIES nlohmann_json::nlohmann_json)
endif()

Expand All @@ -242,7 +250,9 @@ if(CELERITAS_USE_Python)
if(CELERITAS_USE_SWIG)
list(APPEND _components Development)
endif()
find_package(Python 3.6 REQUIRED COMPONENTS ${_components})
if(NOT Python_FOUND)
find_package(Python 3.6 REQUIRED COMPONENTS ${_components})
endif()
set(CELERITAS_PYTHONPATH "$ENV{PYTHONPATH}" CACHE STRING
"Python path used for finding modules and generating documentation"
)
Expand All @@ -259,7 +269,9 @@ if(CELERITAS_USE_SWIG AND NOT SWIG_FOUND)
endif()

if(CELERITAS_USE_VecGeom)
find_package(VecGeom 1.1.17 REQUIRED)
if(NOT VecGeom_FOUND)
find_package(VecGeom 1.1.17 REQUIRED)
endif()

if(CELERITAS_USE_CUDA AND NOT VecGeom_CUDA_FOUND)
celeritas_error_incompatible_option(
Expand Down Expand Up @@ -319,11 +331,6 @@ if(CELERITAS_BUILD_DOCS)
endif()
endif()

if(BUILD_SHARED_LIBS OR CELERITAS_USE_ROOT)
# Make sure modules, sub-libraries, etc. are relocatable
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
endif()

#----------------------------------------------------------------------------#
# LIBRARY
#----------------------------------------------------------------------------#
Expand Down
7 changes: 7 additions & 0 deletions app/celer-sim/RunnerInput.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#include "celeritas/phys/PrimaryGeneratorOptions.hh"
#include "celeritas/user/RootStepWriter.hh"

#ifdef _WIN32
# include <cstdlib>
# ifdef environ
# undef environ
# endif
#endif

namespace celeritas
{
namespace app
Expand Down
4 changes: 4 additions & 0 deletions app/celer-sim/Transporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ auto Transporter<M>::operator()(SpanConstPrimary primaries)
};

// Abort cleanly for interrupt and user-defined signals
#ifndef _WIN32
ScopedSignalHandler interrupted{SIGINT, SIGUSR2};
#else
ScopedSignalHandler interrupted{SIGINT};
#endif
CELER_LOG(status) << "Transporting";

Stopwatch get_step_time;
Expand Down
2 changes: 1 addition & 1 deletion scripts/cmake-presets/wildstyle.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"CELERITAS_USE_SWIG": {"type": "BOOL", "value": "OFF"},
"CELERITAS_USE_VecGeom": {"type": "BOOL", "value": "OFF"},
"CMAKE_CXX_COMPILER": "/usr/bin/g++",
"CMAKE_CXX_FLAGS": "-Wall -Wextra -pedantic -Werror -Wno-error=deprecated-declarations -Wno-attributes -fdiagnostics-color=always",
"CMAKE_CXX_FLAGS": "-Wall -Wextra -pedantic -Werror -Wno-error=deprecated-declarations -fdiagnostics-color=always",
"CMAKE_CXX_FLAGS_RELWITHDEBINFO": "-O2 -g -DNDEBUG -fno-inline -fno-omit-frame-pointer",
"CMAKE_CXX_FLAGS_RELEASE": "-O3 -DNDEBUG -march=skylake-avx512 -mtune=skylake-avx512",
"CMAKE_CUDA_FLAGS": "-Werror all-warnings -Wno-deprecated-gpu-targets",
Expand Down
7 changes: 7 additions & 0 deletions src/celeritas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ if(NOT CELERITAS_USE_OpenMP
PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-Wno-unknown-pragmas>
)
endif()
if(WIN32 AND CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# Inheriting via dominance is correct behavior
celeritas_target_compile_options(celeritas
PUBLIC "$<$<COMPILE_LANGUAGE:CXX>:/wd4267$<SEMICOLON>/wd4250>"
)
endif()


celeritas_target_link_libraries(celeritas
PRIVATE ${PRIVATE_DEPS}
Expand Down
4 changes: 2 additions & 2 deletions src/celeritas/em/interactor/RayleighInteractor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ CELER_FUNCTION Interaction RayleighInteractor::operator()(Engine& rng)
do
{
// Sample index from input.prob
unsigned int const index = celeritas::make_selector(
[&input](unsigned int i) { return input.prob[i]; },
auto const index = celeritas::make_selector(
[&input](size_type i) { return input.prob[i]; },
input.prob.size())(rng);

const real_type w = input.weight[index];
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/em/model/RayleighModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ ActionId RayleighModel::action_id() const
void RayleighModel::build_data(HostValue* data, MaterialParams const& materials)
{
// Number of elements
unsigned int num_elements = materials.num_elements();
auto num_elements = materials.num_elements();

// Build data for available elements
auto params = make_builder(&data->params);
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/em/model/RelativisticBremModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void RelativisticBremModel::build_data(HostValue* data,
real_type particle_mass)
{
// Build element data for available elements
unsigned int num_elements = materials.num_elements();
auto num_elements = materials.num_elements();

auto elem_data = make_builder(&data->elem_data);
elem_data.reserve(num_elements);
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/em/model/WentzelModel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ void WentzelModel::build_data(HostVal<WentzelData>& host_data,
MaterialParams const& materials)
{
// Build element data
unsigned int const num_elements = materials.num_elements();
size_type const num_elements = materials.num_elements();
auto elem_data = make_builder(&host_data.elem_data);
elem_data.reserve(num_elements);

Expand Down
2 changes: 2 additions & 0 deletions src/celeritas/ext/GeantSetup.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ G4VPhysicalVolume const* GeantSetup::world() const
}

#if !CELERITAS_USE_GEANT4
inline void GeantSetup::disable_signal_handler() {}

inline GeantSetup::GeantSetup(std::string const&, Options)
{
CELER_NOT_CONFIGURED("Geant4");
Expand Down
12 changes: 6 additions & 6 deletions src/celeritas/field/DormandPrinceStepper.hh
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ DormandPrinceStepper<E>::operator()(real_type step,
constexpr R d77 = -1 / R(40);

// Coefficients for the mid point calculation by Shampine
constexpr R c71 = 6025192743 / R(30085553152);
constexpr R c73 = 51252292925 / R(65400821598);
constexpr R c74 = -2691868925 / R(45128329728);
constexpr R c75 = 187940372067 / R(1594534317056);
constexpr R c76 = -1776094331 / R(19743644256);
constexpr R c77 = 11237099 / R(235043384);
constexpr R c71 = R(6025192743.) / R(30085553152.);
constexpr R c73 = R(51252292925.) / R(65400821598.);
constexpr R c74 = R(-2691868925.) / R(45128329728.);
constexpr R c75 = R(187940372067.) / R(1594534317056.);
constexpr R c76 = R(-1776094331.) / R(19743644256.);
constexpr R c77 = R(11237099.) / R(235043384.);

result_type result;

Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/field/RZMapFieldData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ struct RZMapFieldParamsData
&& r >= grids.data_r.front && r <= grids.data_r.back);
}

inline CELER_FUNCTION ElementId id(int idx_z, int idx_r) const
inline CELER_FUNCTION ElementId id(size_type idx_z, size_type idx_r) const
{
CELER_EXPECT(grids.data_r);
return ElementId(idx_z * grids.data_r.size + idx_r);
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/grid/VectorUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ std::vector<double> space_impl(double start, double stop, size_type n)
result.front() = start;
for (auto i : range<size_type>(1, n - 1))
{
result[i] = interp(i);
result[i] = interp(static_cast<double>(i));
}
// Manually set last point to avoid any differences due to roundoff
result.back() = stop;
Expand Down
4 changes: 3 additions & 1 deletion src/celeritas/phys/PhysicsData.hh
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,9 @@ inline void resize(PhysicsStateData<Ownership::value, M>* state,
resize(&state->per_process_xs,
size * params.scalars.max_particle_processes);
resize(&state->relaxation, params.hardwired.relaxation_data, size);
resize(&state->secondaries, size * params.scalars.secondary_stack_factor);
resize(
&state->secondaries,
static_cast<size_type>(size * params.scalars.secondary_stack_factor));
}

//---------------------------------------------------------------------------//
Expand Down
4 changes: 2 additions & 2 deletions src/celeritas/random/XorwowRngData.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void resize(XorwowRngStateData<Ownership::value, M>* state,

// Seed sequence to generate well-distributed seed numbers, including
// stream ID to give strings different starting contributions
std::vector<unsigned int> host_seeds(params.seed.begin(),
params.seed.end());
std::vector<std::seed_seq::result_type> host_seeds(params.seed.begin(),
params.seed.end());
if (stream != StreamId{0})
{
// For backward compatibility with prior RNG seed, don't modify the
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/track/detail/TrackSortUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ template<>
void shuffle_track_slots<MemSpace::host>(
Span<TrackSlotId::size_type> track_slots, StreamId)
{
unsigned int seed = track_slots.size();
auto seed = static_cast<unsigned int>(track_slots.size());
std::mt19937 g{seed};
std::shuffle(track_slots.begin(), track_slots.end(), g);
}
Expand Down
4 changes: 2 additions & 2 deletions src/celeritas/track/detail/TrackSortUtils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ struct alive_predicate
{
ObserverPtr<TrackStatus const> status_;

CELER_FUNCTION bool operator()(unsigned int track_slot) const
CELER_FUNCTION bool operator()(size_type track_slot) const
{
return status_.get()[track_slot] == TrackStatus::alive;
}
Expand All @@ -93,7 +93,7 @@ struct action_comparator
{
ObserverPtr<ActionId const> action_;

CELER_FUNCTION bool operator()(unsigned int a, unsigned int b) const
CELER_FUNCTION bool operator()(size_type a, size_type b) const
{
return action_.get()[a] < action_.get()[b];
}
Expand Down
2 changes: 1 addition & 1 deletion src/celeritas/user/SimpleCalo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void SimpleCalo::output(JsonPimpl* j) const
std::vector<int> ids;
for (VolumeId vid : volume_ids_)
{
ids.push_back(vid.get());
ids.push_back(static_cast<int>(vid.get()));
}
obj["volume_ids"] = std::move(ids);
obj["volume_labels"] = volume_labels_;
Expand Down
16 changes: 15 additions & 1 deletion src/corecel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,25 @@ endif()

celeritas_add_library(corecel ${SOURCES})

# Require at least C++14
# Require at least C++17
target_compile_features(corecel PUBLIC cxx_std_17)
if(CELERITAS_USE_CUDA)
target_compile_features(corecel PUBLIC cuda_std_17)
endif()
if(WIN32)
target_compile_definitions(corecel PUBLIC
$<$<COMPILE_LANGUAGE:CXX>:NOMINMAX NOGDI>
)

# By default MSVC has __cplusplus at 199711L, this flag
# forces it to follow the standard
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(corecel PUBLIC
$<$<COMPILE_LANGUAGE:CXX>:/Zc:__cplusplus /Zc:preprocessor>
)
endif()
endif()


celeritas_target_include_directories(corecel
PUBLIC
Expand Down
2 changes: 1 addition & 1 deletion src/corecel/cont/Range.hh
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Range
template<class U, std::enable_if_t<std::is_unsigned<U>::value, bool> = true>
CELER_CONSTEXPR_FUNCTION detail::StepRange<step_type<U>> step(U step)
{
return {*begin_, *end_, step};
return {*begin_, *end_, static_cast<size_type>(step)};
}
//! \endcond

Expand Down
2 changes: 1 addition & 1 deletion src/corecel/data/Collection.hh
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class Collection
//! Direct accesors to underlying data
CELER_FORCEINLINE_FUNCTION size_type size() const
{
return this->storage().size();
return static_cast<size_type>(this->storage().size());
}
CELER_FORCEINLINE_FUNCTION bool empty() const
{
Expand Down
8 changes: 6 additions & 2 deletions src/corecel/io/ColorUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <cstdio>
#include <cstdlib>
#include <string>
#include <unistd.h>
#ifndef _WIN32
# include <unistd.h>
#endif

#include "corecel/sys/Environment.hh"

Expand All @@ -23,7 +25,7 @@ namespace celeritas
bool use_color()
{
const static bool result = [] {
FILE* stream = stderr;
[[maybe_unused]] FILE* stream = stderr;
std::string color_str = celeritas::getenv("CELER_COLOR");
if (color_str.empty())
{
Expand All @@ -44,11 +46,13 @@ bool use_color()
// Color is explicitly enabled
return true;
}
#ifndef _WIN32
if (!isatty(fileno(stream)))
{
// This stream is not a user-facing terminal
return false;
}
#endif
if (const char* term_str = std::getenv("TERM"))
{
if (std::string{term_str}.find("xterm") != std::string::npos)
Expand Down
2 changes: 1 addition & 1 deletion src/corecel/io/StringUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ std::string_view trim(std::string_view input)
{
--stop;
}
return {start, static_cast<std::size_t>(stop - start)};
return {&(*start), static_cast<std::size_t>(stop - start)};
}

//---------------------------------------------------------------------------//
Expand Down
16 changes: 13 additions & 3 deletions src/corecel/math/Algorithms.hh
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,24 @@ CELER_FORCEINLINE_FUNCTION ForwardIt min_element(ForwardIt first,
* Example: \code
assert(9.0 == ipow<2>(3.0));
assert(256 == ipow<8>(2));
static_assert(256 == ipow<8>(2));
\endcode
*/
template<unsigned int N, class T>
CELER_CONSTEXPR_FUNCTION T ipow(T v) noexcept
{
return (N == 0) ? 1
: (N % 2 == 0) ? ipow<N / 2>(v) * ipow<N / 2>(v)
: v * ipow<(N - 1) / 2>(v) * ipow<(N - 1) / 2>(v);
if constexpr (N == 0)
{
return 1;
}
else if constexpr (N % 2 == 0)
{
return ipow<N / 2>(v) * ipow<N / 2>(v);
}
else
{
return v * ipow<(N - 1) / 2>(v) * ipow<(N - 1) / 2>(v);
}
}

//---------------------------------------------------------------------------//
Expand Down
Loading

0 comments on commit 92133db

Please sign in to comment.