Skip to content

Commit

Permalink
PGI openacc compiler modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan committed Feb 14, 2022
1 parent d9cbcdf commit 354572b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 55 deletions.
1 change: 1 addition & 0 deletions src/core/data/grid/gridlayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ namespace core
* @brief AMRToLocal returns the local index associated with the given AMR one.
* This method only deals with **cell** indexes.
*/

template<typename T>
auto AMRToLocal(Point<T, dimension> AMRPoint) const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class MaxwellianParticleInitializer : public ParticleInitializer<ParticleArray,


/**
* @brief load pacore
} // namespace PHARErticles in a ParticleArray in a domain defined by the given layout
* @brief load particles in a ParticleArray in a domain defined by the given layout
*/
void loadParticles(ParticleArray& particles, GridLayout const& layout) const override;

Expand Down Expand Up @@ -143,16 +142,6 @@ void MaxwellianParticleInitializer<ParticleArray, GridLayout>::loadParticles(
};


auto deltas = [](auto& pos, auto& gen) -> std::array<double, dimension> {
if constexpr (dimension == 1)
return {pos(gen)};
if constexpr (dimension == 2)
return {pos(gen), pos(gen)};
if constexpr (dimension == 3)
return {pos(gen), pos(gen), pos(gen)};
};


// in the following two calls,
// primal indexes are given here because that's what cellCenteredCoordinates takes

Expand Down Expand Up @@ -196,9 +185,10 @@ void MaxwellianParticleInitializer<ParticleArray, GridLayout>::loadParticles(
if (basis_ == Basis::Magnetic)
particleVelocity = basisTransform(basis, particleVelocity);

particles.emplace_back(Particle{cellWeight, particleCharge_,
AMRCellIndex.template toArray<int>(),
deltas(deltaDistrib, randGen), particleVelocity});
particles.emplace_back(
Particle{cellWeight, particleCharge_, AMRCellIndex.template toArray<int>(),
core::ConstArrayFrom<dimension>([&] { return deltaDistrib(randGen); }),
particleVelocity});
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/core/data/particles/particle_packer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

namespace PHARE::core
{
// PGI compiler (nvc++ 21.3-0) doesn't like static initializations of arrays,
// would result in empty strings
inline std::array<std::string, 5> packer_keys()
{
// The order of this array must match the tuple order of ParticlePacker::get(particle)
return {"weight", "charge", "iCell", "delta", "v"};
}

template<std::size_t dim>
class ParticlePacker
{
Expand All @@ -31,8 +39,6 @@ class ParticlePacker
return get(particle);
}

static auto& keys() { return keys_; }

auto get(std::size_t i) const { return get(particles_[i]); }
bool hasNext() const { return it_ < particles_.size(); }
auto next() { return get(it_++); }
Expand All @@ -55,10 +61,10 @@ class ParticlePacker
}
}


private:
ParticleArray<dim> const& particles_;
std::size_t it_ = 0;
static inline std::array<std::string, 5> keys_{"weight", "charge", "iCell", "delta", "v"};
};


Expand Down
2 changes: 1 addition & 1 deletion src/core/data/particles/particle_utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ auto positionAsPoint(Particle<GridLayout::dimension> const& particle, GridLayout
auto origin = layout.origin();
auto startIndexes = layout.physicalStartIndex(QtyCentering::primal);
auto meshSize = layout.meshSize();
auto iCell = layout.AMRToLocal(Point{particle.iCell});
auto iCell = layout.AMRToLocal(Point<int, GridLayout::dimension>{particle.iCell});

for (auto iDim = 0u; iDim < GridLayout::dimension; ++iDim)
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/utilities/mpi_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ int rank();
void barrier();

template<typename Data>
auto mpi_type_for()
MPI_Datatype mpi_type_for()
{
if constexpr (std::is_same_v<double, Data>)
return MPI_DOUBLE;
Expand Down
6 changes: 5 additions & 1 deletion src/core/utilities/point/point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace core
static constexpr std::size_t dimension = dim;
using type = Type;


template<typename... Indexes>
constexpr Point(Indexes... index)
: r{{index...}}
Expand Down Expand Up @@ -172,12 +173,15 @@ namespace core
std::array<Type, dim> r{};
};

template<typename... Indexes>
template<typename... Indexes, // block constructor from use if not int/float/etc
typename
= typename std::enable_if<(true && ... && std::is_arithmetic_v<Indexes>), void>::type>
Point(Indexes... indexes)
->Point<typename std::tuple_element<0, std::tuple<Indexes...>>::type, sizeof...(indexes)>;




} // namespace core
} // namespace PHARE

Expand Down
9 changes: 9 additions & 0 deletions src/core/utilities/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ namespace core
return arr;
}

template<std::size_t size, typename FN>
constexpr auto ConstArrayFrom(FN fn)
{
std::array<decltype(fn()), size> arr{};
for (uint8_t i = 0; i < size; i++)
arr[i] = fn();
return arr;
}

template<typename Type>
std::vector<Type> displacementFrom(std::vector<Type> const& input)
{
Expand Down
64 changes: 40 additions & 24 deletions src/diagnostic/detail/h5writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,16 @@ void Writer<ModelView>::createDatasetsPerMPI(HiFile& h5file, std::string path, S
auto mpi_size = core::mpi::size();
auto sizes = core::mpi::collect(dataSetSize, mpi_size);
auto paths = core::mpi::collect(path, mpi_size);

for (int i = 0; i < mpi_size; i++)
{
if (is_zero(sizes[i]))
continue;

createGroupsToDataSet(h5file, paths[i]);

assert(paths[i].back() != '/');

h5file.createDataSet<Type>(paths[i], HighFive::DataSpace(sizes[i]));
}
}
Expand All @@ -328,34 +333,43 @@ void Writer<ModelView>::createDatasetsPerMPI(HiFile& h5file, std::string path, S
* sizes. Recommended to use similar sized paths, if possible. key is always assumed to the be
* the same
*/
namespace
{
// openacc compiler has issues with the lambda version
template<typename H5Node, typename T>
void _doAttribute(H5Node&& node, std::string const& key, core::Span<T, int> const& value)
{
node.template createAttribute<T>(key, HighFive::DataSpace(value.size()))
.write(value.data());
}

template<typename H5Node, typename T>
void _doAttribute(H5Node&& node, std::string const& key, T const& value)
{
node.template createAttribute<T>(key, HighFive::DataSpace::From(value)).write(value);
}

template<typename T>
auto _values(std::vector<T> const& data, int mpi_size)
{
return core::mpi::collect_raw(data, mpi_size);
}

template<typename T>
auto _values(T const& data, int mpi_size)
{
return core::mpi::collect(data, mpi_size);
}
} // namespace

template<typename ModelView>
template<typename Data>
void Writer<ModelView>::writeAttributesPerMPI(HiFile& h5file, std::string path, std::string key,
Data const& data)
{
constexpr bool data_is_vector = core::is_std_vector_v<Data>;

auto doAttribute = [&](auto node, auto const& _key, auto const& value) {
if constexpr (data_is_vector)
{
if (value.size())
node.template createAttribute<typename Data::value_type>(
_key, HighFive::DataSpace(value.size()))
.write(value.data());
}
else
node.template createAttribute<Data>(_key, HighFive::DataSpace::From(value))
.write(value);
};

int mpi_size = core::mpi::size();
auto values = [&]() {
if constexpr (data_is_vector)
return core::mpi::collect_raw(data, mpi_size);
else
return core::mpi::collect(data, mpi_size);
}();
auto paths = core::mpi::collect(path, mpi_size);
auto values = _values(data, mpi_size);
auto paths = core::mpi::collect(path, mpi_size);

for (int i = 0; i < mpi_size; i++)
{
Expand All @@ -366,13 +380,13 @@ void Writer<ModelView>::writeAttributesPerMPI(HiFile& h5file, std::string path,
if (h5file.exist(keyPath) && h5file.getObjectType(keyPath) == HighFive::ObjectType::Dataset)
{
if (!h5file.getDataSet(keyPath).hasAttribute(key))
doAttribute(h5file.getDataSet(keyPath), key, values[i]);
_doAttribute(h5file.getDataSet(keyPath), key, values[i]);
}
else // group
{
createGroupsToDataSet(h5file, keyPath + "/dataset");
if (!h5file.getGroup(keyPath).hasAttribute(key))
doAttribute(h5file.getGroup(keyPath), key, values[i]);
_doAttribute(h5file.getGroup(keyPath), key, values[i]);
}
}
}
Expand Down Expand Up @@ -454,4 +468,6 @@ void Writer<ModelView>::writeDatasets_(std::vector<DiagnosticProperties*> const&

} /* namespace PHARE::diagnostic::h5 */



#endif /* PHARE_DETAIL_DIAGNOSTIC_HIGHFIVE_H */
27 changes: 17 additions & 10 deletions src/diagnostic/detail/types/particle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <string>
#include <memory>



namespace PHARE::diagnostic::h5
{
/*
Expand Down Expand Up @@ -61,6 +63,10 @@ class ParticlesDiagnosticWriter : public H5TypeWriter<H5Writer>
DiagnosticProperties&, Attributes&,
std::unordered_map<std::size_t, std::vector<std::pair<std::string, Attributes>>>&,
std::size_t maxLevel) override;

private:
// PGI compiler (nvc++ 21.3-0) doesn't like static initializations of arrays
std::array<std::string, 5> packer_keys_ = core::packer_keys();
};


Expand Down Expand Up @@ -95,8 +101,9 @@ void ParticlesDiagnosticWriter<H5Writer>::getDataSetInfo(DiagnosticProperties& d

auto particleInfo = [&](auto& attr, auto& particles) {
std::size_t part_idx = 0;
auto const& keys = packer_keys_;
core::apply(Packer::empty(), [&](auto const& arg) {
attr[Packer::keys()[part_idx]] = getSize(arg, particles.size());
attr[keys[part_idx]] = getSize(arg, particles.size());
++part_idx;
});
};
Expand Down Expand Up @@ -147,8 +154,8 @@ void ParticlesDiagnosticWriter<H5Writer>::initDataSets(
std::string path{h5Writer_.getPatchPathAddTimestamp(lvl, patchID) + "/"};
std::size_t part_idx = 0;
core::apply(Packer::empty(), [&](auto const& arg) {
createDataSet(path + Packer::keys()[part_idx], attr, Packer::keys()[part_idx], arg,
null);
auto const& keys = packer_keys_;
createDataSet(path + keys[part_idx], attr, keys[part_idx], arg, null);
++part_idx;
});
this->writeGhostsAttr_(h5file, path, core::ghostWidthForParticles<interpOrder>(), null);
Expand Down Expand Up @@ -187,12 +194,12 @@ void ParticlesDiagnosticWriter<H5Writer>::write(DiagnosticProperties& diagnostic
core::ContiguousParticles<dimension> copy{particles.size()};
packer.pack(copy);


h5file.template write_data_set_flat<2>(path + packer.keys()[0], copy.weight.data());
h5file.template write_data_set_flat<2>(path + packer.keys()[1], copy.charge.data());
h5file.template write_data_set_flat<2>(path + packer.keys()[2], copy.iCell.data());
h5file.template write_data_set_flat<2>(path + packer.keys()[3], copy.delta.data());
h5file.template write_data_set_flat<2>(path + packer.keys()[4], copy.v.data());
auto const& keys = packer_keys_;
h5file.template write_data_set_flat<2>(path + keys[0], copy.weight.data());
h5file.template write_data_set_flat<2>(path + keys[1], copy.charge.data());
h5file.template write_data_set_flat<2>(path + keys[2], copy.iCell.data());
h5file.template write_data_set_flat<2>(path + keys[3], copy.delta.data());
h5file.template write_data_set_flat<2>(path + keys[4], copy.v.data());
};

auto checkWrite = [&](auto& tree, auto pType, auto& ps) {
Expand Down Expand Up @@ -237,7 +244,7 @@ void ParticlesDiagnosticWriter<H5Writer>::writeAttributes(
writeAttributes_(diagnostic, h5file, fileAttributes, patchAttributes, maxLevel);
}


} // namespace PHARE::diagnostic::h5


#endif /* PHARE_DIAGNOSTIC_DETAIL_TYPES_PARTICLE_H */

0 comments on commit 354572b

Please sign in to comment.