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

Add deal.II example (with Kokkos) #1461

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 15 additions & 7 deletions examples/deal.II/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ IF(NOT ${deal.II_FOUND})
)
ENDIF()

DEAL_II_INITIALIZE_CACHED_VARIABLES()
PROJECT("bps")
FILE(GLOB SOURCE_FILES "*.cc")

DEAL_II_INITIALIZE_CACHED_VARIABLES()
FOREACH ( source_file ${SOURCE_FILES} )
GET_FILENAME_COMPONENT(file_name ${source_file} NAME)
STRING( REPLACE ".cc" "" exec ${file_name} )

ADD_EXECUTABLE(bps bps.cc)
DEAL_II_SETUP_TARGET(bps)
DEAL_II_INITIALIZE_CACHED_VARIABLES()
PROJECT(${exec})

TARGET_INCLUDE_DIRECTORIES(bps PUBLIC ${CEED_DIR}/include)
TARGET_LINK_LIBRARIES(bps ${CEED_DIR}/lib/libceed.so)
DEAL_II_INITIALIZE_CACHED_VARIABLES()

ADD_EXECUTABLE(${exec} ${source_file})
DEAL_II_SETUP_TARGET(${exec})

TARGET_INCLUDE_DIRECTORIES(${exec} PUBLIC ${CEED_DIR}/include)
TARGET_LINK_LIBRARIES(${exec} ${CEED_DIR}/lib/libceed.so)

ENDFOREACH ( source_file ${SOURCE_FILES} )
2 changes: 1 addition & 1 deletion examples/deal.II/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ make
To run the executable, write:

```
./bps
./bps_01
```

Optional command-line arguments are shown by adding the command-line argument "--help".
196 changes: 10 additions & 186 deletions examples/deal.II/bps.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
// deal.II includes
#include <deal.II/dofs/dof_tools.h>

#include <deal.II/fe/fe_nothing.h>
#include <deal.II/fe/fe_q.h>
#include <deal.II/fe/fe_tools.h>
#include <deal.II/fe/fe_values.h>
#include <deal.II/fe/mapping.h>
#include <deal.II/fe/mapping_q1.h>

#include <deal.II/lac/la_parallel_vector.h>

#include <deal.II/matrix_free/fe_evaluation.h>
#include <deal.II/matrix_free/matrix_free.h>
#include <deal.II/matrix_free/tools.h>

// libCEED includes
#include <ceed/ceed.h>

Expand Down Expand Up @@ -92,14 +93,14 @@ struct BPInfo
/**
* Base class of operators.
*/
template <typename Number>
template <typename Number, typename MemorySpace>
class OperatorBase
{
public:
/**
* deal.II vector type
*/
using VectorType = LinearAlgebra::distributed::Vector<Number>;
using VectorType = LinearAlgebra::distributed::Vector<Number, MemorySpace>;

/**
* Initialize vector.
Expand Down Expand Up @@ -130,11 +131,11 @@ class OperatorBase
/**
* Operator implementation using libCEED.
*/
template <int dim, typename Number>
class OperatorCeed : public OperatorBase<Number>
template <int dim, typename Number, typename MemorySpace = MemorySpace::Host>
class OperatorCeed : public OperatorBase<Number, MemorySpace>
{
public:
using VectorType = typename OperatorBase<Number>::VectorType;
using VectorType = typename OperatorBase<Number, MemorySpace>::VectorType;

/**
* Constructor.
Expand Down Expand Up @@ -712,180 +713,3 @@ class OperatorCeed : public OperatorBase<Number>
mutable VectorType src_tmp;
mutable VectorType dst_tmp;
};



template <int dim, typename Number>
class OperatorDealii : public OperatorBase<Number>
{
public:
using VectorType = typename OperatorBase<Number>::VectorType;

/**
* Constructor.
*/
OperatorDealii(const Mapping<dim> &mapping,
const DoFHandler<dim> &dof_handler,
const AffineConstraints<Number> &constraints,
const Quadrature<dim> &quadrature,
const BPType &bp)
: mapping(mapping)
, dof_handler(dof_handler)
, constraints(constraints)
, quadrature(quadrature)
, bp(bp)
{
reinit();
}

/**
* Destructor.
*/
~OperatorDealii() = default;

/**
* Initialized internal data structures, particularly, MatrixFree.
*/
void
reinit() override
{
// configure MatrixFree
typename MatrixFree<dim, Number>::AdditionalData additional_data;
additional_data.tasks_parallel_scheme =
MatrixFree<dim, Number>::AdditionalData::TasksParallelScheme::none;

// create MatrixFree
matrix_free.reinit(mapping, dof_handler, constraints, quadrature, additional_data);
}

/**
* Matrix-vector product.
*/
void
vmult(VectorType &dst, const VectorType &src) const override
{
if (dof_handler.get_fe().n_components() == 1)
{
matrix_free.cell_loop(&OperatorDealii::do_cell_integral_range<1>, this, dst, src, true);
}
else
{
AssertThrow(dof_handler.get_fe().n_components() == dim, ExcInternalError());

matrix_free.cell_loop(&OperatorDealii::do_cell_integral_range<dim>, this, dst, src, true);
}
}

/**
* Initialize vector.
*/
void
initialize_dof_vector(VectorType &vec) const override
{
matrix_free.initialize_dof_vector(vec);
}

/**
* Compute inverse of diagonal.
*/
void
compute_inverse_diagonal(VectorType &diagonal) const override
{
this->initialize_dof_vector(diagonal);

if (dof_handler.get_fe().n_components() == 1)
{
MatrixFreeTools::compute_diagonal(matrix_free,
diagonal,
&OperatorDealii::do_cell_integral_local<1>,
this);
}
else
{
AssertThrow(dof_handler.get_fe().n_components() == dim, ExcInternalError());

MatrixFreeTools::compute_diagonal(matrix_free,
diagonal,
&OperatorDealii::do_cell_integral_local<dim>,
this);
}

for (auto &i : diagonal)
i = (std::abs(i) > 1.0e-10) ? (1.0 / i) : 1.0;
}

private:
/**
* Cell integral without vector access.
*/
template <int n_components>
void
do_cell_integral_local(FEEvaluation<dim, -1, 0, n_components, Number> &phi) const
{
if (bp <= BPType::BP2) // mass matrix
{
phi.evaluate(EvaluationFlags::values);
for (const auto q : phi.quadrature_point_indices())
phi.submit_value(phi.get_value(q), q);
phi.integrate(EvaluationFlags::values);
}
else // Poisson operator
{
phi.evaluate(EvaluationFlags::gradients);
for (const auto q : phi.quadrature_point_indices())
phi.submit_gradient(phi.get_gradient(q), q);
phi.integrate(EvaluationFlags::gradients);
}
}

/**
* Cell integral on a range of cells.
*/
template <int n_components>
void
do_cell_integral_range(const MatrixFree<dim, Number> &matrix_free,
VectorType &dst,
const VectorType &src,
const std::pair<unsigned int, unsigned int> &range) const
{
FEEvaluation<dim, -1, 0, n_components, Number> phi(matrix_free, range);

for (unsigned cell = range.first; cell < range.second; ++cell)
{
phi.reinit(cell);
phi.read_dof_values(src); // read source vector
do_cell_integral_local(phi); // cell integral
phi.distribute_local_to_global(dst); // write to destination vector
}
}

/**
* Mapping object passed to the constructor.
*/
const Mapping<dim> &mapping;

/**
* DoFHandler object passed to the constructor.
*/
const DoFHandler<dim> &dof_handler;

/**
* Constraints object passed to the constructor.
*/
const AffineConstraints<Number> &constraints;

/**
* Quadrature rule object passed to the constructor.
*/
const Quadrature<dim> &quadrature;

/**
* Selected BP.
*/
const BPType bp;

/**
* MatrixFree object.
*/
MatrixFree<dim, Number> matrix_free;
};
Loading
Loading