Skip to content
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

Rm #945

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Rm #945

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
1 change: 1 addition & 0 deletions src/amr/data/field/field_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace amr
static constexpr std::size_t dimension = GridLayoutT::dimension;
static constexpr std::size_t interp_order = GridLayoutT::interp_order;
using Geometry = FieldGeometry<GridLayoutT, PhysicalQuantity>;
using view_type = Grid_t::field_type;

/*** \brief Construct a FieldData from information associated to a patch
*
Expand Down
2 changes: 2 additions & 0 deletions src/amr/data/particles/particles_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ namespace amr
static constexpr int ghostSafeMapLayer = 1;

public:
using view_type = typename core::ParticlesPack<ParticleArray>;

ParticlesData(SAMRAI::hier::Box const& box, SAMRAI::hier::IntVector const& ghost,
std::string const& name)
: SAMRAI::hier::PatchData::PatchData(box, ghost)
Expand Down
33 changes: 9 additions & 24 deletions src/amr/multiphysics_integrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,25 +278,6 @@ namespace solver

/**
* @brief see SAMRAI documentation. This function initializes the data on the given level.
*
* The method first checks wether allocation of patch data must be performed.
* If it does, all objects using resources on patches must see their allocate() function
* called.
*
* This is:
* - the model (by definition the model has data defined on patches)
* - the solver (Some solvers has internal data that needs to exist on patches)
* - the messenger (the messenger has data defined on patches for internal reasons)
*
*
* then the level needs to be registered to the messenger.
*
* Then data initialization per se begins and one can be on one of the following cases:
*
* - regridding
* - initialization of the root level
* - initialization of a new level from scratch (not a regridding)
*
*/
void initializeLevelData(std::shared_ptr<SAMRAI::hier::PatchHierarchy> const& hierarchy,
int const levelNumber, double const initDataTime,
Expand Down Expand Up @@ -342,7 +323,8 @@ namespace solver
// this level is the source or destination
// we therefore need to rebuild them
auto const finestLvlNbr = hierarchy->getFinestLevelNumber();
auto nextFiner = (levelNumber == finestLvlNbr) ? levelNumber : levelNumber + 1;
auto const nextFiner
= (levelNumber == finestLvlNbr) ? levelNumber : levelNumber + 1;

for (auto ilvl = levelNumber; ilvl <= nextFiner; ++ilvl)
{
Expand All @@ -369,10 +351,13 @@ namespace solver
else
load_balancer_manager_->estimate(*level, model);

if (static_cast<std::size_t>(levelNumber) == model_views_.size())
model_views_.push_back(solver.make_view(*level, model));
else
model_views_[levelNumber] = solver.make_view(*level, model);

model.resources_manager().makeLevelViews(*hierarchy, levelNumber);

// if (static_cast<std::size_t>(levelNumber) == model_views_.size())
// model_views_.push_back(solver.make_view(*level, model));
// else
// model_views_[levelNumber] = solver.make_view(*level, model);
}


Expand Down
33 changes: 20 additions & 13 deletions src/amr/physical_models/hybrid_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "amr/messengers/hybrid_messenger_info.hpp"
#include "core/data/vecfield/vecfield.hpp"
#include "core/def.hpp"
#include "core/data/ions/ion_population/particle_pack.hpp"

namespace PHARE::solver
{
Expand All @@ -27,19 +28,20 @@ class HybridModel : public IPhysicalModel<AMR_Types>

using type_list
= PHARE::core::type_list<GridLayoutT, Electromag, Ions, Electrons, AMR_Types, Grid_t>;
using Interface = IPhysicalModel<AMR_Types>;
using amr_types = AMR_Types;
using electrons_t = Electrons;
using patch_t = typename AMR_Types::patch_t;
using level_t = typename AMR_Types::level_t;
using gridlayout_type = GridLayoutT;
using electromag_type = Electromag;
using vecfield_type = typename Electromag::vecfield_type;
using field_type = typename vecfield_type::field_type;
using grid_type = Grid_t;
using ions_type = Ions;
using particle_array_type = typename Ions::particle_array_type;
using resources_manager_type = amr::ResourcesManager<gridlayout_type, grid_type>;
using Interface = IPhysicalModel<AMR_Types>;
using amr_types = AMR_Types;
using electrons_t = Electrons;
using patch_t = typename AMR_Types::patch_t;
using level_t = typename AMR_Types::level_t;
using gridlayout_type = GridLayoutT;
using electromag_type = Electromag;
using vecfield_type = typename Electromag::vecfield_type;
using field_type = typename vecfield_type::field_type;
using grid_type = Grid_t;
using ions_type = Ions;
using particle_array_type = typename Ions::particle_array_type;
using resources_manager_type
= amr::ResourcesManager<gridlayout_type, grid_type, particle_array_type>;
using ParticleInitializerFactory
= core::ParticleInitializerFactory<particle_array_type, gridlayout_type>;

Expand All @@ -53,6 +55,11 @@ class HybridModel : public IPhysicalModel<AMR_Types>
virtual void initialize(level_t& level) override;


virtual amr::IResourcesManager& resources_manager() const override
{
return *std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
}
Comment on lines +58 to +61
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add null pointer check in resources_manager method.

The dynamic pointer cast could return nullptr if the cast fails.

Apply this diff to add null pointer validation:

 virtual amr::IResourcesManager& resources_manager() const override
 {
-    return *std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
+    auto manager = std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
+    if (!manager) {
+        throw std::runtime_error("Failed to cast resourcesManager to IResourcesManager");
+    }
+    return *manager;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
virtual amr::IResourcesManager& resources_manager() const override
{
return *std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
}
virtual amr::IResourcesManager& resources_manager() const override
{
auto manager = std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
if (!manager) {
throw std::runtime_error("Failed to cast resourcesManager to IResourcesManager");
}
return *manager;
}


/**
* @brief allocate uses the ResourcesManager to allocate HybridState physical quantities on
* the given Patch at the given allocateTime
Expand Down
4 changes: 4 additions & 0 deletions src/amr/physical_models/mhd_model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace solver

virtual void initialize(level_t& /*level*/) override {}

virtual amr::IResourcesManager& resources_manager() const override
{
return *std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
}
Comment on lines +44 to +47
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for dynamic_pointer_cast.

The dynamic_pointer_cast could fail at runtime. Consider adding error handling to prevent undefined behavior.

         virtual amr::IResourcesManager& resources_manager() const override
         {
-            return *std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
+            auto castedManager = std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
+            if (!castedManager) {
+                throw std::runtime_error("Failed to cast resourcesManager to IResourcesManager");
+            }
+            return *castedManager;
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
virtual amr::IResourcesManager& resources_manager() const override
{
return *std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
}
virtual amr::IResourcesManager& resources_manager() const override
{
auto castedManager = std::dynamic_pointer_cast<amr::IResourcesManager>(resourcesManager);
if (!castedManager) {
throw std::runtime_error("Failed to cast resourcesManager to IResourcesManager");
}
return *castedManager;
}


virtual void allocate(patch_t& patch, double const allocateTime) override
{
Expand Down
7 changes: 4 additions & 3 deletions src/amr/physical_models/physical_model.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@



#ifndef PHARE_PHYSICAL_MODEL_HPP
#define PHARE_PHYSICAL_MODEL_HPP

#include <memory>
#include <string>

#include "amr/messengers/messenger_info.hpp"
#include "amr/resources_manager/resources_manager.hpp"

namespace PHARE
{
Expand Down Expand Up @@ -43,6 +41,9 @@ namespace solver
virtual void initialize(level_t& level) = 0;


virtual amr::IResourcesManager& resources_manager() const = 0;


/**
* @brief allocate must be implemented by concrete subclasses to allocate the model
* quantities onto the given patch at the given allocateTime.
Expand Down
3 changes: 2 additions & 1 deletion src/amr/resources_manager/field_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ namespace amr
* also says the type of the actual data buffer
*/
template<typename Grid_t, typename GridLayoutT>
struct UserFieldType
struct FieldViewInfo
{
using patch_data_type = FieldData<GridLayoutT, Grid_t>;
using variable_type = FieldVariable<GridLayoutT, Grid_t>;
using view_type = patch_data_type::view_type;
};


Expand Down
14 changes: 8 additions & 6 deletions src/amr/resources_manager/particle_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ namespace PHARE
{
namespace amr
{
/** @brief tells SAMRAI which kind of variable, patchdata are used for a Field Resource
* also says the type of the actual data buffer
/** @brief tells SAMRAI which kind of variable, patchdata are used for a Particle resource
*/
template<typename ResourcesUser, std::size_t interp>
struct UserParticleType
template<typename ParticleArray, std::size_t interp>
struct ParticleViewInfo
{
static constexpr auto dimension = ResourcesUser::dimension;
static constexpr auto dimension = ParticleArray::dimension;
static constexpr auto interp_order = interp;

using particle_array_type = typename ResourcesUser::particle_array_type;
// TODORM this class could be templated by the ParticleArray type and rather
// hard code the link to the ParticlePack here
using particle_array_type = ParticleArray;
using variable_type = ParticlesVariable<particle_array_type, interp_order>;
using patch_data_type = ParticlesData<particle_array_type>;
using view_type = patch_data_type::view_type;
};

} // namespace amr
Expand Down
51 changes: 17 additions & 34 deletions src/amr/resources_manager/resources_guards.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,67 +15,50 @@ namespace PHARE
{
namespace amr
{
/** \brief ResourcesGuards maintain a link with several resources in order to give them
* an access to the data.
*
* At construction it will take the patch, and all resourcesManagerUser object that need to
* be set via the ResourcesManager. Upon destruction, it will put all the previous
* object in an inactive state (for now it just put nullptr on them)

* TODO: add the active mechanism
/** \brief ViewGuard ... TODO fix doc
*/
template<typename ResourcesManager, typename... ResourcesUsers>
class ResourcesGuard
template<typename ResourcesManager, typename... Views>
class ViewGuard
{
public:
/**
* \brief At construction, each resourcesUser will be valid. At destruction time, each
* resourcesUser will be invalid
*
* \param[in] patch
* \param[in] resourcesManager
* \param[in,out] resourcesUsers
*/
ResourcesGuard(SAMRAI::hier::Patch const& patch, ResourcesManager const& resourcesManager,
ResourcesUsers&... resourcesUsers)
: resourcesUsers_{resourcesUsers...}
ViewGuard(SAMRAI::hier::Patch const& patch, ResourcesManager const& resourcesManager,
Views&... views)
: views_{views...}
, patch_{patch}
, resourcesManager_{resourcesManager}
{
std::apply(
[this](auto&... user) {
((resourcesManager_.setResources_(user, UseResourcePtr{}, patch_)), ...);
},
resourcesUsers_);
views_);
}




~ResourcesGuard()
~ViewGuard()
{
// set nullptr to all users in resourcesUsers_
std::apply(
[this](auto&... user) {
((resourcesManager_.setResources_(user, UseNullPtr{}, patch_)), ...);
[this](auto&... view) {
((resourcesManager_.setResources_(view, UseNullPtr{}, patch_)), ...);
},
resourcesUsers_);
views_);
}




// We just need the move constructor for using ResourceManager::createResourcesGuards
ResourcesGuard(ResourcesGuard&&) = default;
ResourcesGuard() = delete;
ResourcesGuard(ResourcesGuard const&) = delete;
ResourcesGuard& operator=(ResourcesGuard const& source) = delete;
ResourcesGuard& operator=(ResourcesGuard&&) = delete;
ViewGuard(ViewGuard&&) = default;
ViewGuard() = delete;
ViewGuard(ViewGuard const&) = delete;
ViewGuard& operator=(ViewGuard const& source) = delete;
ViewGuard& operator=(ViewGuard&&) = delete;



private:
std::tuple<ResourcesUsers&...> resourcesUsers_;
std::tuple<Views&...> views_;
SAMRAI::hier::Patch const& patch_;
ResourcesManager const& resourcesManager_;
};
Expand Down
Loading
Loading