Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Hypre directbuilder (#189)
Browse files Browse the repository at this point in the history
* [hypre]: Introduce HypreDirectBuilder

* [move]: refactor DirectMatrixBuilder

Use composition instead of inheritance.

* [ref]: refactor DirectMatrixBuilder

Use composition instead of inheritance.

* [all]: clang-format

* [core]: some cleaning

* [all]: IDirectMatrixBuilder and factory

Should be able to use hypre directly, without SimpleCSR.

* [hypre]: clang-format

* [all]: real factory for DirectMatrixBuilder

* [all]: change default DirectMatrixBuilder

* [move]: add method to choose DirectMatrixBuilder backend

* [dok]: make DokBuilder implements IDirectMatrixBuilder

* [all]: add contribute method to IDirectMatrixBuilder

This method returns whether or not the non zero was added.

* [move]: Remove DoKDirectMatrixBuilder

The functionality can be accessed with new DirectMatrixBuilder.

* [core]: Add missing header for IDirectMatrixBuilder

* [hypre]: Correct reset for DirectMatrixBuilder

* [dok]: reset flag for DirectMatrixBuilder

* [hypre] directbuilder works with reset flags

* [hypre] Call ClearAllErrors before major operations

* [dok] fix format
  • Loading branch information
cedricchevalier19 authored Nov 16, 2023
1 parent fea96bd commit 95b25f2
Show file tree
Hide file tree
Showing 32 changed files with 1,137 additions and 396 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to this project will be documented in this file.
- \[move\]: parallel matrix-market reader
- \[hypre\]: improve AMG parameter choices, especially for GPU
- \[trilinos\]: use modern CMake Trilinos and support for GPU
- \[all\]: New infra to choose the implementation backend for `DirectMatrixBuilder`

**Fixed bugs:**

Expand Down
14 changes: 8 additions & 6 deletions plugins/hypre/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ add_library(hypre_wrapper
src/hypre_linear_solver.cpp
src/hypre_instance.h
src/hypre_instance.cpp
src/hypre_direct_matrix_builder.cpp
src/hypre_direct_matrix_builder.h
src/converters/hypre_to_simplecsr_vector.cpp
src/converters/simplecsr_to_hypre_vector.cpp
src/converters/simplecsr_to_hypre_matrix.cpp
Expand All @@ -72,7 +74,7 @@ endif ()

target_link_libraries(hypre_wrapper PUBLIC
Alien::alien_core
)
)

target_link_libraries(hypre_wrapper PRIVATE
MPI::MPI_CXX
Expand All @@ -81,13 +83,13 @@ target_link_libraries(hypre_wrapper PRIVATE
Arccore::arccore_base
Arccore::arccore_message_passing_mpi
arccon::Hypre
)
)

target_include_directories(hypre_wrapper PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
)

add_library(Alien::hypre_wrapper ALIAS hypre_wrapper)

Expand All @@ -96,7 +98,7 @@ include(GenerateExportHeader)
generate_export_header(hypre_wrapper
BASE_NAME ALIEN_HYPRE
EXPORT_FILE_NAME include/alien/hypre/export.h
)
)

# Examples
set(ALIEN_BENCHMARK ON)
Expand All @@ -117,12 +119,12 @@ set(ALIENHYPRE_EXPORT_TARGET ${PROJECT_NAME}Targets)

install(DIRECTORY include/alien/hypre
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/alien
)
)

install(TARGETS hypre_wrapper EXPORT ${ALIENHYPRE_EXPORT_TARGET}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
)

install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/include/alien/hypre/export.h
Expand Down
2 changes: 1 addition & 1 deletion plugins/hypre/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ include(GNUInstallDirs)

install(TARGETS bench_hypre EXPORT Alien
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
)

if (ALIEN_UNIT_TESTS)

Expand Down
16 changes: 15 additions & 1 deletion plugins/hypre/examples/hypre_example_solve_move.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ int test()

tm->info() << "build matrix with direct matrix builder";
{
Alien::Move::DirectMatrixBuilder builder(std::move(A), Alien::DirectMatrixOptions::eResetValues);
Alien::Move::DirectMatrixBuilder builder(std::move(A), Alien::DirectMatrixOptions::eResetProfile, Alien::DirectMatrixOptions::eSymmetric, std::make_optional<Alien::BackEndId>(Alien::AlgebraTraits<Alien::BackEnd::tag::hypre>::name()));
builder.reserve(3); // Réservation de 3 coefficients par ligne
builder.allocate(); // Allocation de l'espace mémoire réservé

Expand Down Expand Up @@ -132,6 +132,20 @@ int test()

tm->info() << " => ||r|| = " << norm;

// Try to change matrix values
{
Alien::Move::DirectMatrixBuilder builder(std::move(A), Alien::DirectMatrixOptions::eResetValues, Alien::DirectMatrixOptions::eSymmetric, std::make_optional<Alien::BackEndId>(Alien::AlgebraTraits<Alien::BackEnd::tag::hypre>::name()));

for (int irow = offset; irow < offset + lsize; ++irow) {
builder(irow, irow) = 4.;
if (irow - 1 >= 0)
builder(irow, irow - 1) = -2.;
if (irow + 1 < gsize)
builder(irow, irow + 1) = -2.;
}
A = builder.release();
}

tm->info() << " ";
tm->info() << "... example finished !!!";

Expand Down
5 changes: 3 additions & 2 deletions plugins/hypre/src/converters/simplecsr_to_hypre_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class SimpleCSR_to_Hypre_MatrixConverter final : public Alien::IMatrixConverter

void _build(const Alien::SimpleCSRMatrix<Arccore::Real>& sourceImpl, Alien::Hypre::Matrix& targetImpl) const;

void _buildBlock(const Alien::SimpleCSRMatrix<Arccore::Real>& sourceImpl, Alien::Hypre::Matrix& targetImpl) const;
void
_buildBlock(const Alien::SimpleCSRMatrix<Arccore::Real>& sourceImpl, Alien::Hypre::Matrix& targetImpl) const;
};

void SimpleCSR_to_Hypre_MatrixConverter::convert(const IMatrixImpl* sourceImpl, IMatrixImpl* targetImpl) const
Expand Down Expand Up @@ -90,7 +91,7 @@ void SimpleCSR_to_Hypre_MatrixConverter::_build(const Alien::SimpleCSRMatrix<Arc
auto cols = profile.getCols();

// understand why values and cols can have different sizes !
targetImpl.setRowsValues(rows, ncols, cols, values.subConstView(0, cols.size()));
targetImpl.insertRowsValues(rows, ncols, cols, values.subConstView(0, cols.size()), true);

targetImpl.assemble();
}
Expand Down
86 changes: 86 additions & 0 deletions plugins/hypre/src/hypre_direct_matrix_builder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2023 IFPEN-CEA
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

//
// Created by chevalierc on 17/05/23.
//

#include "hypre_direct_matrix_builder.h"
#include "alien/hypre/export.h"

#include <alien/core/impl/MultiMatrixImpl.h>
#include <alien/handlers/scalar/MatrixBuilderFactory.h>

#include <alien/hypre/backend.h>

namespace Alien::Hypre
{
HypreDirectMatrixBuilder::HypreDirectMatrixBuilder(Alien::IMatrix& matrix,
Hypre::HypreDirectMatrixBuilder::ResetFlag reset_flag,
[[maybe_unused]] Hypre::HypreDirectMatrixBuilder::SymmetricFlag symmetric_flag)
: m_matrix(matrix)
{

m_matrix.impl()->lock();
m_matrix_impl = &m_matrix.impl()->get<BackEnd::tag::hypre>(true);

m_matrix_impl->init(reset_flag);
}

void HypreDirectMatrixBuilder::finalize()
{
m_matrix_impl->assemble();
m_matrix.impl()->unlock();
}

void HypreDirectMatrixBuilder::setData(Arccore::Integer iIndex, Arccore::Integer jIndex, Arccore::Real value)
{
m_matrix_impl->setRowValues(iIndex, ConstArrayView<Integer>(1, &jIndex), ConstArrayView<Real>(1, &value));
}

void HypreDirectMatrixBuilder::setData(Arccore::Integer iIndex, Arccore::Real factor,
Arccore::ConstArrayView<Arccore::Integer> jIndexes,
Arccore::ConstArrayView<Arccore::Real> jValues)
{
if (factor != 1.0) {
throw FatalErrorException("Scaling is not supported");
}

m_matrix_impl->setRowValues(iIndex, jIndexes, jValues);
}

void HypreDirectMatrixBuilder::addData(Arccore::Integer iIndex, Arccore::Integer jIndex, Arccore::Real value)
{
m_matrix_impl->addRowValues(iIndex, ConstArrayView<Integer>(1, &jIndex), ConstArrayView<Real>(1, &value));
}

void HypreDirectMatrixBuilder::addData(Arccore::Integer iIndex, Arccore::Real factor,
Arccore::ConstArrayView<Arccore::Integer> jIndexes,
Arccore::ConstArrayView<Arccore::Real> jValues)
{
if (factor != 1.0) {
throw FatalErrorException("Scaling is not supported");
}

m_matrix_impl->addRowValues(iIndex, jIndexes, jValues);
}

const ALIEN_HYPRE_EXPORT Alien::Common::MatrixBuilderFactory hypre_builder_register(
AlgebraTraits<BackEnd::tag::hypre>::name(), [](IMatrix& matrix, DirectMatrixOptions::ResetFlag reset, DirectMatrixOptions::SymmetricFlag symmetry) { return std::make_unique<HypreDirectMatrixBuilder>(matrix, reset, symmetry); });

Check warning on line 84 in plugins/hypre/src/hypre_direct_matrix_builder.cpp

View workflow job for this annotation

GitHub Actions / rhel8 spack release

'visibility' attribute ignored [-Wattributes]

}; // namespace Alien::Hypre
143 changes: 143 additions & 0 deletions plugins/hypre/src/hypre_direct_matrix_builder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* Copyright 2023 IFPEN-CEA
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

//
// Created by chevalierc on 17/05/23.
//

#ifndef ALIEN_HYPREDIRECTMATRIXBUILDER_H
#define ALIEN_HYPREDIRECTMATRIXBUILDER_H

#include <alien/data/IMatrix.h>
#include <alien/data/utils/MatrixElement.h>

#include <arccore/trace/ITraceMng.h>
#include <arccore/message_passing/IMessagePassingMng.h>

#include <alien/handlers/scalar/IDirectMatrixBuilder.h>
#include "hypre_matrix.h"

namespace Alien
{

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

namespace Hypre
{

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

class ALIEN_EXPORT HypreDirectMatrixBuilder final : public Alien::Common::IDirectMatrixBuilder
{
public:
using ResetFlag = DirectMatrixOptions::ResetFlag;
using ReserveFlag = DirectMatrixOptions::ReserveFlag;
using SymmetricFlag = DirectMatrixOptions::SymmetricFlag;

using MatrixElement = MatrixElementT<HypreDirectMatrixBuilder>;

HypreDirectMatrixBuilder(IMatrix& matrix, [[maybe_unused]] ResetFlag reset_flag,
[[maybe_unused]] SymmetricFlag symmetric_flag = SymmetricFlag::eSymmetric);

~HypreDirectMatrixBuilder() override
{
finalize();
}

HypreDirectMatrixBuilder(HypreDirectMatrixBuilder&) = delete;

HypreDirectMatrixBuilder(HypreDirectMatrixBuilder&&) = delete;

HypreDirectMatrixBuilder& operator=(const HypreDirectMatrixBuilder&) = delete;

HypreDirectMatrixBuilder& operator=(HypreDirectMatrixBuilder&&) = delete;

void reserve(Arccore::Integer n, ReserveFlag flag = ReserveFlag::eResetReservation) override
{
// Nothing yet
}

void reserve(Arccore::ConstArrayView<Arccore::Integer> indices, Arccore::Integer n,
ReserveFlag flag = ReserveFlag::eResetReservation) override
{
// Nothing yet
}

void allocate() override
{
// Nothing yet
}

void addData(Arccore::Integer iIndex, Arccore::Integer jIndex, Arccore::Real value) override;

void addData(Arccore::Integer iIndex, Arccore::Real factor,
Arccore::ConstArrayView<Arccore::Integer> jIndexes,
Arccore::ConstArrayView<Arccore::Real> jValues) override;

void setData(Arccore::Integer iIndex, Arccore::Integer jIndex, Arccore::Real value) override;

void setData(Arccore::Integer iIndex, Arccore::Real factor,
Arccore::ConstArrayView<Arccore::Integer> jIndexes,
Arccore::ConstArrayView<Arccore::Real> jValues) override;

void finalize() override;

void squeeze() override
{
// do nothing
}

[[nodiscard]] Arccore::String stats() const override
{
// Do nothing
return { "" };
}

[[nodiscard]] Arccore::String stats(Arccore::IntegerConstArrayView ids) const override
{
// Do nothing
return { "" };
}
std::optional<Arccore::Real> contribute(Arccore::Integer row, Arccore::Integer col, Arccore::Real value) override
{
addData(row, col, value);
return { value };
}

private:
IMatrix& m_matrix;

Matrix* m_matrix_impl;
};

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

} // namespace Hypre

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

} // namespace Alien

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#endif //ALIEN_HYPREDIRECTMATRIXBUILDER_H
7 changes: 6 additions & 1 deletion plugins/hypre/src/hypre_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ class AlienHypreInstance
public:
static void init_if_needed(MPI_Comm comm)
{
if (hypre_initialized)
if (hypre_initialized) {
// Clear all Hypre error for this session
HYPRE_ClearAllErrors();
return;
}

bind_devices(comm);

Expand All @@ -41,8 +44,10 @@ class AlienHypreInstance
#endif

hypre_initialized = true;
HYPRE_ClearAllErrors();

setup_devices();
HYPRE_ClearAllErrors();
}

static void bind_devices([[maybe_unused]] MPI_Comm comm)
Expand Down
Loading

0 comments on commit 95b25f2

Please sign in to comment.