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

Xiangyu/sycl loop ranges #675

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions src/shared/bodies/base_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include "base_particle_generator.h"
#include "base_particles.h"
#include "cell_linked_list.h"
#include "execution.h"
#include "base_implementation.h"
#include "sph_system.h"
#include "sphinxsys_containers.h"

Expand Down Expand Up @@ -90,7 +90,7 @@ class SPHBody
SPHBody(SPHSystem &sph_system, const std::string &name);
SPHBody(SPHSystem &sph_system, SharedPtr<Shape> shape_ptr, const std::string &name);
SPHBody(SPHSystem &sph_system, SharedPtr<Shape> shape_ptr);
virtual ~SPHBody(){};
virtual ~SPHBody() {};

std::string getName() { return body_name_; };
SPHSystem &getSPHSystem();
Expand Down Expand Up @@ -202,7 +202,7 @@ class RealBody : public SPHBody
{
this->getSPHSystem().addRealBody(this);
};
virtual ~RealBody(){};
virtual ~RealBody() {};
BaseCellLinkedList &getCellLinkedList();
void updateCellLinkedList();
};
Expand Down
2 changes: 1 addition & 1 deletion src/shared/body_relations/base_body_relation.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "base_geometry.h"
#include "base_particles.h"
#include "cell_linked_list.h"
#include "execution.h"
#include "base_implementation.h"
#include "neighborhood.h"

namespace SPH
Expand Down
2 changes: 2 additions & 0 deletions src/shared/body_relations/contact_body_relation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "base_particle_dynamics.h"
#include "cell_linked_list.hpp"

#include <numeric>

namespace SPH
{
//=================================================================================================//
Expand Down
1 change: 1 addition & 0 deletions src/shared/particle_dynamics/base_local_dynamics.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class BaseLocalDynamics
sph_body_(identifier.getSPHBody()),
particles_(&sph_body_.getBaseParticles()){};
virtual ~BaseLocalDynamics(){};
typedef DynamicsIdentifier DynamicsIdentifierType;
DynamicsIdentifier &getDynamicsIdentifier() { return identifier_; };
SPHBody &getSPHBody() { return sph_body_; };
BaseParticles *getParticles() { return particles_; };
Expand Down
59 changes: 59 additions & 0 deletions src/shared/particle_dynamics/execution/base_implementation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ------------------------------------------------------------------------- *
* SPHinXsys *
* ------------------------------------------------------------------------- *
* SPHinXsys (pronunciation: s'finksis) is an acronym from Smoothed Particle *
* Hydrodynamics for industrial compleX systems. It provides C++ APIs for *
* physical accurate simulation and aims to model coupled industrial dynamic *
* systems including fluid, solid, multi-body dynamics and beyond with SPH *
* (smoothed particle hydrodynamics), a meshless computational method using *
* particle discretization. *
* *
* SPHinXsys is partially funded by German Research Foundation *
* (Deutsche Forschungsgemeinschaft) DFG HU1527/6-1, HU1527/10-1, *
* HU1527/12-1 and HU1527/12-4. *
* *
* Portions copyright (c) 2017-2023 Technical University of Munich and *
* the authors' affiliations. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may *
* not use this file except in compliance with the License. You may obtain a *
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
* *
* ------------------------------------------------------------------------- */
/**
* @file base_implementation.h
* @brief Here we define the execution policy relevant to parallel computing.
* @details This analog of the standard library on the same functions.
* @author Alberto Guarnieri and Xiangyu Hu
*/

#ifndef BASE_IMPLEMENTATION_H
#define BASE_IMPLEMENTATION_H

#include "sphinxsys_containers.h"

namespace SPH
{
namespace execution
{
template <typename... T>
class Implementation;

template <>
class Implementation<Base>
{
public:
explicit Implementation() {}
~Implementation() {}

bool isUpdated() { return is_updated_; };
void resetUpdated() { is_updated_ = false; };

protected:
bool is_updated_ = false;
void setUpdated() { is_updated_ = true; };
};

} // namespace execution
} // namespace SPH
#endif // BASE_IMPLEMENTATION_H
Original file line number Diff line number Diff line change
Expand Up @@ -21,67 +21,79 @@
* *
* ------------------------------------------------------------------------- */
/**
* @file execution.h
* @file implementation.h
* @brief Here we define the execution policy relevant to parallel computing.
* @details This analog of the standard library on the same functions.
* @author Alberto Guarnieri and Xiangyu Hu
*/

#ifndef EXECUTION_H
#define EXECUTION_H
#ifndef IMPLEMENTATION_H
#define IMPLEMENTATION_H

#include "base_implementation.h"
#include "base_data_type.h"
#include "execution_policy.h"
#include "loop_range.h"
#include "ownership.h"
#include "sphinxsys_containers.h"

namespace SPH
{
namespace execution
{
template <typename... T>
class Implementation;

template <>
class Implementation<Base>
template <class ExecutionPolicy, class LocalDynamicsType>
class Implementation<ExecutionPolicy, LocalDynamicsType> : public Implementation<Base>
{
using DynamicsIdentifier = typename LocalDynamicsType::DynamicsIdentifierType;
using LoopRangeType = LoopRangeCK<ExecutionPolicy, DynamicsIdentifier>;

public:
explicit Implementation() {}
~Implementation() {}
explicit Implementation(LocalDynamicsType &local_dynamics)
: Implementation<Base>(), local_dynamics_(local_dynamics),
loop_range_(nullptr) {};
~Implementation()
{
delete loop_range_;
};

bool isUpdated() { return is_updated_; };
void resetUpdated() { is_updated_ = false; };
LoopRangeType &getLoopRange()
{
if (loop_range_ == nullptr)
{
loop_range_ = new LoopRangeType(local_dynamics_.getDynamicsIdentifier());
}
return *loop_range_;
};

protected:
bool is_updated_ = false;
void setUpdated() { is_updated_ = true; };
LocalDynamicsType &local_dynamics_;
LoopRangeType *loop_range_;
};

template <class ExecutionPolicy, class LocalDynamicsType, class ComputingKernelType>
class Implementation<ExecutionPolicy, LocalDynamicsType, ComputingKernelType>
: public Implementation<Base>
: public Implementation<ExecutionPolicy, LocalDynamicsType>
{
public:
explicit Implementation(LocalDynamicsType &local_dynamics)
: Implementation<Base>(),
local_dynamics_(local_dynamics), computing_kernel_(nullptr) {}
: Implementation<ExecutionPolicy, LocalDynamicsType>(local_dynamics),
computing_kernel_(nullptr) {}
~Implementation()
{
delete computing_kernel_;
}

template <typename... Args>
ComputingKernelType *getComputingKernel(Args &&... args)
ComputingKernelType *getComputingKernel(Args &&...args)
{
if (computing_kernel_ == nullptr)
{
local_dynamics_.registerComputingKernel(this, std::forward<Args>(args)...);
this->local_dynamics_.registerComputingKernel(this, std::forward<Args>(args)...);
computing_kernel_ = new ComputingKernelType(
ExecutionPolicy{}, local_dynamics_, std::forward<Args>(args)...);
setUpdated();
ExecutionPolicy{}, this->local_dynamics_, std::forward<Args>(args)...);
this->setUpdated();
}

if (!isUpdated())
if (!this->isUpdated())
{
overwriteComputingKernel(std::forward<Args>(args)...);
}
Expand All @@ -90,17 +102,16 @@ class Implementation<ExecutionPolicy, LocalDynamicsType, ComputingKernelType>
}

template <typename... Args>
void overwriteComputingKernel(Args &&... args)
void overwriteComputingKernel(Args &&...args)
{
*computing_kernel_ = ComputingKernelType(
ExecutionPolicy{}, local_dynamics_, std::forward<Args>(args)...);
setUpdated();
ExecutionPolicy{}, this->local_dynamics_, std::forward<Args>(args)...);
this->setUpdated();
}

private:
LocalDynamicsType &local_dynamics_;
protected:
ComputingKernelType *computing_kernel_;
};
} // namespace execution
} // namespace SPH
#endif // EXECUTION_H
#endif // IMPLEMENTATION_H
42 changes: 2 additions & 40 deletions src/shared/particle_dynamics/particle_iterators.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
#define PARTICLE_ITERATORS_H

#include "base_data_package.h"
#include "execution.h"
#include "implementation.h"
#include "sphinxsys_containers.h"

#include <numeric>

namespace SPH
{
using namespace execution;
Expand Down Expand Up @@ -245,6 +243,7 @@ inline ReturnType particle_reduce(const ParallelPolicy &par, const IndexVector &
return operation(x, y);
});
};

/**
* BodypartByCell-wise reduce iterators (for sequential and parallel computing).
*/
Expand Down Expand Up @@ -288,42 +287,5 @@ inline ReturnType particle_reduce(const ParallelPolicy &par, const ConcurrentCel
[&](const ReturnType &x, const ReturnType &y) -> ReturnType
{ return operation(x, y); });
}

template <typename T, typename Op>
T exclusive_scan(const SequencedPolicy &seq_policy, T *first, T *d_first, UnsignedInt d_size, Op op)
{
UnsignedInt scan_size = d_size - 1;
std::exclusive_scan(first, first + d_size, d_first, T{0}, op);
return d_first[scan_size];
}

template <typename T, typename Op>
T exclusive_scan(const ParallelPolicy &par_policy, T *first, T *d_first, UnsignedInt d_size, Op op)
{
// Exclusive scan is the same as inclusive, but shifted by one
UnsignedInt scan_size = d_size - 1;
d_first[0] = T{0};
using range_type = tbb::blocked_range<UnsignedInt>;
tbb::parallel_scan(
range_type(0, scan_size), d_first[0],
[=](const range_type &r, T sum, bool is_final_scan) -> T
{
T tmp = sum;
for (UnsignedInt i = r.begin(); i < r.end(); ++i)
{
tmp = op(tmp, first[i]);
if (is_final_scan)
{
d_first[i + 1] = tmp;
}
}
return tmp;
},
[&](const T &a, const T &b)
{
return op(a, b);
});
return d_first[scan_size];
}
} // namespace SPH
#endif // PARTICLE_ITERATORS_H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "loading_dynamics.h"
#include "base_general_dynamics.h"

#include <numeric>

namespace SPH
{
namespace solid_dynamics
Expand Down
6 changes: 3 additions & 3 deletions src/shared/particles/base_particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SPH
{
//=================================================================================================//
BaseParticles::BaseParticles(SPHBody &sph_body, BaseMaterial *base_material)
: v_total_real_particles_(nullptr), real_particles_bound_(0), particles_bound_(0),
: sv_total_real_particles_(nullptr), real_particles_bound_(0), particles_bound_(0),
original_id_(nullptr), sorted_id_(nullptr),
pos_(nullptr), Vol_(nullptr), rho_(nullptr), mass_(nullptr),
sph_body_(sph_body), body_name_(sph_body.getName()),
Expand All @@ -23,7 +23,7 @@ BaseParticles::BaseParticles(SPHBody &sph_body, BaseMaterial *base_material)
read_restart_variable_from_xml_(variables_to_restart_, restart_xml_parser_)
{
sph_body.assignBaseParticles(this);
v_total_real_particles_ = registerSingularVariable<UnsignedInt>("TotalRealParticles");
sv_total_real_particles_ = registerSingularVariable<UnsignedInt>("TotalRealParticles");
}
//=================================================================================================//
void BaseParticles::initializeBasicParticleVariables()
Expand Down Expand Up @@ -58,7 +58,7 @@ void BaseParticles::registerPositionAndVolumetricMeasureFromReload()
//=================================================================================================//
void BaseParticles::initializeAllParticlesBounds(size_t number_of_particles)
{
UnsignedInt *total_real_particles = v_total_real_particles_->ValueAddress();
UnsignedInt *total_real_particles = sv_total_real_particles_->ValueAddress();
*total_real_particles = number_of_particles;
real_particles_bound_ = number_of_particles;
particles_bound_ = real_particles_bound_;
Expand Down
15 changes: 8 additions & 7 deletions src/shared/particles/base_particles.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class BaseParticles

public:
explicit BaseParticles(SPHBody &sph_body, BaseMaterial *base_material);
virtual ~BaseParticles(){};
virtual ~BaseParticles() {};
SPHBody &getSPHBody() { return sph_body_; };
BaseMaterial &getBaseMaterial() { return base_material_; };

Expand All @@ -99,7 +99,7 @@ class BaseParticles
// particles_bound_ gives the total number of particles in all groups.
//----------------------------------------------------------------------
protected:
SingularVariable<UnsignedInt> *v_total_real_particles_;
SingularVariable<UnsignedInt> *sv_total_real_particles_;
UnsignedInt real_particles_bound_;
UnsignedInt particles_bound_;

Expand All @@ -109,9 +109,10 @@ class BaseParticles
//----------------------------------------------------------------------
// Generalized particle manipulation
//----------------------------------------------------------------------
UnsignedInt TotalRealParticles() { return *v_total_real_particles_->ValueAddress(); };
void incrementTotalRealParticles(UnsignedInt increment = 1) { *v_total_real_particles_->ValueAddress() += increment; };
void decrementTotalRealParticles(UnsignedInt decrement = 1) { *v_total_real_particles_->ValueAddress() -= decrement; };
SingularVariable<UnsignedInt> *svTotalRealParticles() { return sv_total_real_particles_; };
UnsignedInt TotalRealParticles() { return *sv_total_real_particles_->ValueAddress(); };
void incrementTotalRealParticles(UnsignedInt increment = 1) { *sv_total_real_particles_->ValueAddress() += increment; };
void decrementTotalRealParticles(UnsignedInt decrement = 1) { *sv_total_real_particles_->ValueAddress() -= decrement; };
UnsignedInt RealParticlesBound() { return real_particles_bound_; };
UnsignedInt ParticlesBound() { return particles_bound_; };
void initializeAllParticlesBounds(size_t total_real_particles);
Expand Down Expand Up @@ -258,7 +259,7 @@ class BaseParticles
struct WriteAParticleVariableToXml
{
XmlParser &xml_parser_;
WriteAParticleVariableToXml(XmlParser &xml_parser) : xml_parser_(xml_parser){};
WriteAParticleVariableToXml(XmlParser &xml_parser) : xml_parser_(xml_parser) {};

template <typename DataType>
void operator()(DataContainerAddressKeeper<DiscreteVariable<DataType>> &variables);
Expand All @@ -267,7 +268,7 @@ class BaseParticles
struct ReadAParticleVariableFromXml
{
XmlParser &xml_parser_;
ReadAParticleVariableFromXml(XmlParser &xml_parser) : xml_parser_(xml_parser){};
ReadAParticleVariableFromXml(XmlParser &xml_parser) : xml_parser_(xml_parser) {};

template <typename DataType>
void operator()(DataContainerAddressKeeper<DiscreteVariable<DataType>> &variables, BaseParticles *base_particles);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/shared_ck/body_relation/relation_ck.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include "base_body.h"
#include "base_particles.h"
#include "execution.h"
#include "implementation.h"

namespace SPH
{
Expand Down
Loading
Loading