Skip to content

Commit

Permalink
Create RNG Factory only if it is needed
Browse files Browse the repository at this point in the history
MySimulation.hpp: - RNGFactory will only be created if it is required by either
                    ionization or the photon creation

UsesRNG.hpp: - PIConGPU trait that checks if an object requires an RNG
             - Default is "false"

FilterByRNG.hpp: - PMacc particle trait that takes a list of species and
                   checks all of them with the UsesRNG trait to create
                   a new list of species.

ADL_Impl: - specialized the UsesRNG trait for "ADK_Impl" to return "true"

Status: compiles [x], runs[x]
  • Loading branch information
Marco Garten committed Aug 11, 2016
1 parent df8a24b commit 7edd7a3
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/libPMacc/include/particles/traits/FilterByRNG.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2016 Marco Garten, Heiko Burau
*
* This file is part of libPMacc.
*
* libPMacc is free software: you can redistribute it and/or modify
* it under the terms of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libPMacc is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with libPMacc.
* If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "pmacc_types.hpp"
#include "traits/UsesRNG.hpp"
#include <boost/mpl/copy_if.hpp>
#include <boost/mpl/placeholders.hpp>

namespace PMacc
{
namespace particles
{
namespace traits
{

/** Return a new sequence of particle species that require random number
* generation for their ionization.
*
* @tparam T_MPLSeq sequence of particle species
*
* \TODO rather pass a sequence of objects, like ionization modules
*/
template<typename T_MPLSeq>
struct FilterByRNG
{
typedef T_MPLSeq MPLSeq;

template<typename T_Species>
struct UsesRNG
{
typedef T_Species SpeciesType;
/* SelectIonizer will be either the specified one or fallback: None */
typedef typename picongpu::traits::GetIonizer<SpeciesType>::type SelectIonizer;
/* check if ionization module requires RNG: type is boost::mpl::bool_<> */
typedef typename ::picongpu::traits::UsesRNG<
SelectIonizer
>::type type;
};

typedef typename bmpl::copy_if<MPLSeq, UsesRNG<bmpl::_> >::type type;
};

}//namespace traits
}//namespace particles
}//namespace PMacc
12 changes: 12 additions & 0 deletions src/picongpu/include/particles/ionization/byField/ADK/ADK_Impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "simulation_defines.hpp"
#include "traits/Resolve.hpp"
#include "traits/UsesRNG.hpp"
#include "mappings/kernel/AreaMapping.hpp"

#include "fields/FieldB.hpp"
Expand All @@ -42,6 +43,17 @@

namespace picongpu
{
namespace traits
{

template<typename T_IonizationAlgorithm, typename T_DestSpecies, typename T_SrcSpecies>
struct UsesRNG<particles::ionization::ADK_Impl<T_IonizationAlgorithm, T_DestSpecies, T_SrcSpecies> >
{
/* ionization module uses random number generation */
typedef boost::mpl::bool_<true> type;
};
} // namespace traits

namespace particles
{
namespace ionization
Expand Down
18 changes: 17 additions & 1 deletion src/picongpu/include/simulationControl/MySimulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "particles/InitFunctors.hpp"
#include "particles/memory/buffers/MallocMCBuffer.hpp"
#include "particles/traits/FilterByFlag.hpp"
#include "particles/traits/FilterByRNG.hpp"
#include "particles/IdProvider.hpp"

#include <boost/mpl/int.hpp>
Expand Down Expand Up @@ -285,17 +286,32 @@ class MySimulation : public SimulationHelper<simDim>

laser = new LaserPhysics(cellDescription->getGridLayout());

// Make a list of all species that can be ionized
typedef typename PMacc::particles::traits::FilterByFlag
<
VectorAllSpecies,
ionizer<>
>::type VectorSpeciesWithIonizer;

// filter the list of ionized species for those that require random number generation
typedef typename PMacc::particles::traits::FilterByRNG<VectorSpeciesWithIonizer>::type VectorSpeciesRequiringRNG;

// Initialize random number generator and synchrotron functions, if there are synchrotron photon species
typedef typename PMacc::particles::traits::FilterByFlag<VectorAllSpecies,
synchrotronPhotons<> >::type AllSynchrotronPhotonsSpecies;
if(!bmpl::empty<AllSynchrotronPhotonsSpecies>::value)

if(!bmpl::empty<AllSynchrotronPhotonsSpecies>::value || !bmpl::empty<VectorSpeciesRequiringRNG>::value)
{
// create factory for the random number generator
this->rngFactory = new RNGFactory(Environment<simDim>::get().SubGrid().getLocalDomain().size);
// init factory
PMacc::GridController<simDim>& gridCon = PMacc::Environment<simDim>::get().GridController();
this->rngFactory->init(gridCon.getScalarPosition());
}

// Initialize synchrotron functions, if there are synchrotron photon species
if(!bmpl::empty<AllSynchrotronPhotonsSpecies>::value)
{
this->synchrotronFunctions.init();
}

Expand Down
52 changes: 52 additions & 0 deletions src/picongpu/include/traits/UsesRNG.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2016 Marco Garten, Rene Widera
*
* This file is part of PIConGPU.
*
* PIConGPU is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PIConGPU is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PIConGPU.
* If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <boost/mpl/bool.hpp>

namespace picongpu
{
namespace traits
{

/** Checks if an object requires the RNG
*
* @tparam T_Object any object (class or typename)
*
* This struct must define
* ::type (boost::mpl::bool_<>)
*/
template<typename T_Object>
struct UsesRNG
{
/* default implementation */
typedef boost::mpl::bool_<false> type;
};

template<typename T_Object>
bool usesRNG(const T_Object& obj)
{
return UsesRNG<T_Object>::type::value;
}

}// namespace traits

}// namespace picongpu

0 comments on commit 7edd7a3

Please sign in to comment.