Skip to content

Commit

Permalink
Add skeleton optical core params and launch action (#1386)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethrj authored Sep 4, 2024
1 parent cb937da commit 8b327e0
Show file tree
Hide file tree
Showing 14 changed files with 897 additions and 55 deletions.
3 changes: 3 additions & 0 deletions src/celeritas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ list(APPEND SOURCES
neutron/process/NeutronElasticProcess.cc
neutron/process/NeutronInelasticProcess.cc
optical/CerenkovParams.cc
optical/CoreParams.cc
optical/CoreState.cc
optical/OpticalCollector.cc
optical/MaterialParams.cc
optical/TrackData.cc
optical/ScintillationParams.cc
optical/detail/OffloadParams.cc
optical/detail/OpticalLaunchAction.cc
phys/CutoffParams.cc
phys/ImportedModelAdapter.cc
phys/ImportedProcessAdapter.cc
Expand Down
140 changes: 140 additions & 0 deletions src/celeritas/optical/CoreParams.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/optical/CoreParams.cc
//---------------------------------------------------------------------------//
#include "CoreParams.hh"

#include "corecel/io/Logger.hh"
#include "corecel/sys/ActionRegistry.hh"
#include "corecel/sys/ScopedMem.hh"
#include "celeritas/geo/GeoParams.hh"
#include "celeritas/mat/MaterialParams.hh"
#include "celeritas/random/RngParams.hh"
#include "celeritas/track/SimParams.hh"
#include "celeritas/track/TrackInitParams.hh"

#include "CoreState.hh"
#include "MaterialParams.hh"

namespace celeritas
{
namespace optical
{
namespace
{
//---------------------------------------------------------------------------//
// HELPER CLASSES AND FUNCTIONS
//---------------------------------------------------------------------------//
//!@{
template<MemSpace M>
CoreParamsData<Ownership::const_reference, M>
build_params_refs(CoreParams::Input const& p, CoreScalars const& scalars)
{
CELER_EXPECT(scalars);

CoreParamsData<Ownership::const_reference, M> ref;

ref.scalars = scalars;
ref.geometry = get_ref<M>(*p.geometry);
ref.material = get_ref<M>(*p.material);
// TODO: ref.physics = get_ref<M>(*p.physics);
ref.rng = get_ref<M>(*p.rng);
ref.sim = get_ref<M>(*p.sim);
ref.init = get_ref<M>(*p.init);

CELER_ENSURE(ref);
return ref;
}

//---------------------------------------------------------------------------//
/*!
* Construct always-required actions and set IDs.
*/
CoreScalars build_actions(ActionRegistry* reg)
{
using std::make_shared;

CoreScalars scalars;

//// START ACTIONS ////

CELER_DISCARD(reg);
#if 0
// NOTE: due to ordering by {start, ID}, ExtendFromPrimariesAction *must*
// precede InitializeTracksAction
reg->insert(make_shared<ExtendFromPrimariesAction>(reg->next_id()));
reg->insert(make_shared<InitializeTracksAction>(reg->next_id()));
#endif

//// PRE-STEP ACTIONS ////

//// POST-STEP ACTIONS ////

// Construct geometry boundary action
scalars.boundary_action = reg->next_id();
#if 0
reg->insert(make_shared<detail::BoundaryAction>(
scalars.boundary_action));
#endif

//// END ACTIONS ////

// TODO: extend from secondaries action

return scalars;
}

//---------------------------------------------------------------------------//
} // namespace

//---------------------------------------------------------------------------//
/*!
* Construct with all problem data, creating some actions too.
*/
CoreParams::CoreParams(Input&& input) : input_(std::move(input))
{
#define CP_VALIDATE_INPUT(MEMBER) \
CELER_VALIDATE(input_.MEMBER, \
<< "optical core input is missing " << #MEMBER << " data")
CP_VALIDATE_INPUT(geometry);
CP_VALIDATE_INPUT(material);
// TODO: CP_VALIDATE_INPUT(physics);
CP_VALIDATE_INPUT(rng);
CP_VALIDATE_INPUT(sim);
CP_VALIDATE_INPUT(init);
CP_VALIDATE_INPUT(action_reg);
CP_VALIDATE_INPUT(max_streams);
#undef CP_VALIDATE_INPUT

CELER_EXPECT(input_);

ScopedMem record_mem("optical::CoreParams.construct");

// Construct always-on actions and save their IDs
CoreScalars scalars = build_actions(input_.action_reg.get());

// Save maximum number of streams
scalars.max_streams = input_.max_streams;

// Save host reference
host_ref_ = build_params_refs<MemSpace::host>(input_, scalars);
if (celeritas::device())
{
device_ref_ = build_params_refs<MemSpace::device>(input_, scalars);
// Copy device ref to device global memory
device_ref_vec_ = DeviceVector<DeviceRef>(1);
device_ref_vec_.copy_to_device({&device_ref_, 1});
}

CELER_LOG(status) << "Celeritas optical setup complete";

CELER_ENSURE(host_ref_);
CELER_ENSURE(host_ref_.scalars.max_streams == this->max_streams());
}

//---------------------------------------------------------------------------//
} // namespace optical
} // namespace celeritas
145 changes: 145 additions & 0 deletions src/celeritas/optical/CoreParams.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/optical/CoreParams.hh
//---------------------------------------------------------------------------//
#pragma once

#include "corecel/Assert.hh"
#include "corecel/data/DeviceVector.hh"
#include "corecel/data/ObserverPtr.hh"
#include "corecel/data/ParamsDataInterface.hh"
#include "celeritas/geo/GeoFwd.hh"
#include "celeritas/global/ActionInterface.hh"
#include "celeritas/random/RngParamsFwd.hh"

#include "TrackData.hh"

namespace celeritas
{
//---------------------------------------------------------------------------//
class ActionRegistry;
class TrackInitParams;
class SimParams;

namespace optical
{
//---------------------------------------------------------------------------//
class MaterialParams;
// TODO: class PhysicsParams;

//---------------------------------------------------------------------------//
/*!
* Shared parameters for the optical photon loop.
*/
class CoreParams final : public ParamsDataInterface<CoreParamsData>
{
public:
//!@{
//! \name Type aliases
using SPConstGeo = std::shared_ptr<GeoParams const>;
using SPConstMaterial = std::shared_ptr<MaterialParams const>;
using SPConstRng = std::shared_ptr<RngParams const>;
using SPConstSim = std::shared_ptr<SimParams const>;
using SPConstTrackInit = std::shared_ptr<TrackInitParams const>;
using SPActionRegistry = std::shared_ptr<ActionRegistry>;

template<MemSpace M>
using ConstRef = CoreParamsData<Ownership::const_reference, M>;
template<MemSpace M>
using ConstPtr = ObserverPtr<ConstRef<M> const, M>;
//!@}

struct Input
{
SPConstGeo geometry;
SPConstMaterial material;
// TODO: physics
SPConstRng rng;
SPConstSim sim;
SPConstTrackInit init;

SPActionRegistry action_reg;

//! Maximum number of simultaneous threads/tasks per process
StreamId::size_type max_streams{1};

//! True if all params are assigned and valid
explicit operator bool() const
{
return geometry && material && rng && sim && init && action_reg
&& max_streams;
}
};

public:
// Construct with all problem data, creating some actions too
CoreParams(Input&& inp);

//!@{
//! \name Data interface
//! Access data on the host
HostRef const& host_ref() const final { return host_ref_; }
//! Access data on the device
DeviceRef const& device_ref() const final { return device_ref_; }
//!@}

//!@{
//! Access shared problem parameter data.
SPConstGeo const& geometry() const { return input_.geometry; }
SPConstMaterial const& material() const { return input_.material; }
SPConstRng const& rng() const { return input_.rng; }
SPConstTrackInit const& init() const { return input_.init; }
SPActionRegistry const& action_reg() const { return input_.action_reg; }
//!@}

// Access host pointers to core data
using ParamsDataInterface<CoreParamsData>::ref;

// Access a native pointer to properties in the native memory space
template<MemSpace M>
inline ConstPtr<M> ptr() const;

//! Maximum number of streams
size_type max_streams() const { return input_.max_streams; }

private:
Input input_;
HostRef host_ref_;
DeviceRef device_ref_;

// Copy of DeviceRef in device memory
DeviceVector<DeviceRef> device_ref_vec_;
};

//---------------------------------------------------------------------------//
/*!
* Access a native pointer to a NativeCRef.
*
* This way, CUDA kernels only need to copy a pointer in the kernel arguments,
* rather than the entire (rather large) DeviceRef object.
*/
template<MemSpace M>
auto CoreParams::ptr() const -> ConstPtr<M>
{
if constexpr (M == MemSpace::host)
{
return make_observer(&host_ref_);
}
#ifndef __NVCC__
// CUDA 11.4 complains about 'else if constexpr' ("missing return
// statement") and GCC 11.2 complains about leaving off the 'else'
// ("inconsistent deduction for auto return type")
else
#endif
{
CELER_ENSURE(!device_ref_vec_.empty());
return make_observer(device_ref_vec_);
}
}

//---------------------------------------------------------------------------//
} // namespace optical
} // namespace celeritas
84 changes: 84 additions & 0 deletions src/celeritas/optical/CoreState.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//----------------------------------*-C++-*----------------------------------//
// Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file celeritas/optical/CoreState.cc
//---------------------------------------------------------------------------//
#include "CoreState.hh"

#include "corecel/data/CollectionAlgorithms.hh"
#include "corecel/data/Copier.hh"
#include "corecel/io/Logger.hh"
#include "corecel/sys/ScopedProfiling.hh"

#include "CoreParams.hh"

namespace celeritas
{
namespace optical
{
//---------------------------------------------------------------------------//
//! Support polymorphic deletion
CoreStateInterface::~CoreStateInterface() = default;

//---------------------------------------------------------------------------//
/*!
* Construct from CoreParams.
*/
template<MemSpace M>
CoreState<M>::CoreState(CoreParams const& params,
StreamId stream_id,
size_type num_track_slots)
{
CELER_VALIDATE(stream_id < params.max_streams(),
<< "stream ID " << stream_id.unchecked_get()
<< " is out of range: max streams is "
<< params.max_streams());
CELER_VALIDATE(num_track_slots > 0, << "number of track slots is not set");

ScopedProfiling profile_this{"construct-optical-state"};

states_ = CollectionStateStore<CoreStateData, M>(
params.host_ref(), stream_id, num_track_slots);

counters_.num_vacancies = num_track_slots;

if constexpr (M == MemSpace::device)
{
device_ref_vec_ = DeviceVector<Ref>(1);
device_ref_vec_.copy_to_device({&this->ref(), 1});
ptr_ = make_observer(device_ref_vec_);
}
else if constexpr (M == MemSpace::host)
{
ptr_ = make_observer(&this->ref());
}

CELER_LOG_LOCAL(status) << "Celeritas optical state initialization "
"complete";
CELER_ENSURE(states_);
CELER_ENSURE(ptr_);
}

//---------------------------------------------------------------------------//
/*!
* Inject primaries to be turned into TrackInitializers.
*
* These will be converted by the ProcessPrimaries action.
*/
template<MemSpace M>
void CoreState<M>::insert_primaries(Span<Primary const>)
{
CELER_NOT_IMPLEMENTED("primary insertion");
}

//---------------------------------------------------------------------------//
// EXPLICIT INSTANTIATION
//---------------------------------------------------------------------------//
template class CoreState<MemSpace::host>;
template class CoreState<MemSpace::device>;

//---------------------------------------------------------------------------//
} // namespace optical
} // namespace celeritas
Loading

0 comments on commit 8b327e0

Please sign in to comment.