Skip to content

Commit

Permalink
change in generator with permutation + clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreMarchand20 committed Jul 18, 2024
1 parent dd27d0a commit d3563fc
Show file tree
Hide file tree
Showing 25 changed files with 333 additions and 425 deletions.
7 changes: 7 additions & 0 deletions include/htool/distributed_operator/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class DistributedOperatorFromHMatrix {
distributed_operator.add_local_operator(&local_hmatrix);
block_diagonal_hmatrix = hmatrix.get_sub_hmatrix(hmatrix_builder.get_target_cluster().get_cluster_on_partition(get_rankWorld(communicator)), hmatrix_builder.get_source_cluster().get_cluster_on_partition(get_rankWorld(communicator)));
}

DistributedOperatorFromHMatrix(const VirtualGeneratorInUserNumbering<CoefficientPrecision> &generator, const Cluster<CoordinatePrecision> &target_cluster, const Cluster<CoordinatePrecision> &source_cluster, const HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision> &hmatrix_builder, MPI_Comm communicator) : DistributedOperatorFromHMatrix(GeneratorWithPermutation<CoefficientPrecision>(generator, target_cluster.get_permutation().data(), source_cluster.get_permutation().data()), target_cluster, source_cluster, hmatrix_builder, communicator) {
}
};

template <typename CoefficientPrecision, typename CoordinatePrecision = underlying_type<CoefficientPrecision>>
Expand All @@ -49,6 +52,8 @@ class DefaultApproximationBuilder {
const HMatrix<CoefficientPrecision, CoordinatePrecision> *block_diagonal_hmatrix{nullptr};

DefaultApproximationBuilder(const VirtualGenerator<CoefficientPrecision> &generator, const Cluster<CoordinatePrecision> &target_cluster, const Cluster<CoordinatePrecision> &source_cluster, htool::underlying_type<CoefficientPrecision> epsilon, htool::underlying_type<CoefficientPrecision> eta, char symmetry, char UPLO, MPI_Comm communicator) : distributed_operator_builder(generator, target_cluster, source_cluster, HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>(target_cluster, source_cluster, epsilon, eta, symmetry, UPLO, -1, get_rankWorld(communicator), get_rankWorld(communicator)), communicator), hmatrix(distributed_operator_builder.hmatrix), distributed_operator(distributed_operator_builder.distributed_operator), block_diagonal_hmatrix(distributed_operator_builder.block_diagonal_hmatrix) {}

DefaultApproximationBuilder(const VirtualGeneratorInUserNumbering<CoefficientPrecision> &generator, const Cluster<CoordinatePrecision> &target_cluster, const Cluster<CoordinatePrecision> &source_cluster, htool::underlying_type<CoefficientPrecision> epsilon, htool::underlying_type<CoefficientPrecision> eta, char symmetry, char UPLO, MPI_Comm communicator) : DefaultApproximationBuilder(GeneratorWithPermutation<CoefficientPrecision>(generator, target_cluster.get_permutation().data(), source_cluster.get_permutation().data()), target_cluster, source_cluster, epsilon, eta, symmetry, UPLO, communicator) {}
};

template <typename CoefficientPrecision, typename CoordinatePrecision = underlying_type<CoefficientPrecision>>
Expand All @@ -69,6 +74,8 @@ class DefaultLocalApproximationBuilder {

public:
DefaultLocalApproximationBuilder(const VirtualGenerator<CoefficientPrecision> &generator, const Cluster<CoordinatePrecision> &target_cluster, const Cluster<CoordinatePrecision> &source_cluster, htool::underlying_type<CoefficientPrecision> epsilon, htool::underlying_type<CoefficientPrecision> eta, char symmetry, char UPLO, MPI_Comm communicator) : distributed_operator_builder(generator, target_cluster, source_cluster, HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>(target_cluster.get_cluster_on_partition(get_rankWorld(communicator)), source_cluster.get_cluster_on_partition(get_rankWorld(communicator)), epsilon, eta, symmetry, UPLO, -1, get_rankWorld(communicator), get_rankWorld(communicator)), communicator), hmatrix(distributed_operator_builder.hmatrix), distributed_operator(distributed_operator_builder.distributed_operator), block_diagonal_hmatrix(distributed_operator_builder.block_diagonal_hmatrix) {}

DefaultLocalApproximationBuilder(const VirtualGeneratorInUserNumbering<CoefficientPrecision> &generator, const Cluster<CoordinatePrecision> &target_cluster, const Cluster<CoordinatePrecision> &source_cluster, htool::underlying_type<CoefficientPrecision> epsilon, htool::underlying_type<CoefficientPrecision> eta, char symmetry, char UPLO, MPI_Comm communicator) : DefaultLocalApproximationBuilder(GeneratorWithPermutation<CoefficientPrecision>(generator, target_cluster.get_permutation().data(), source_cluster.get_permutation().data()), target_cluster, source_cluster, epsilon, eta, symmetry, UPLO, communicator) {}
};

} // namespace htool
Expand Down
4 changes: 0 additions & 4 deletions include/htool/hmatrix/hmatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "lrmat/lrmat.hpp"
#include <queue>

#include <mpi.h>
namespace htool {

// Class
Expand Down Expand Up @@ -989,9 +988,6 @@ void HMatrix<CoefficientPrecision, CoordinatePrecision>::threaded_hierarchical_a

set_leaves_in_cache();

int rankWorld;
MPI_Comm_rank(MPI_COMM_WORLD, &rankWorld);

int out_size(m_target_cluster->get_size());
auto get_output_cluster{&HMatrix::get_target_cluster};
auto get_input_cluster{&HMatrix::get_source_cluster};
Expand Down
24 changes: 18 additions & 6 deletions include/htool/hmatrix/interfaces/virtual_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class VirtualGenerator {

public:
virtual void copy_submatrix(int M, int N, int row_offset, int col_offset, CoefficientPrecision *ptr) const = 0;
// virtual void copy_submatrix(int M, int N, const int *rows, const int *cols, CoefficientPrecision *ptr) const = 0;

VirtualGenerator() {}
VirtualGenerator(const VirtualGenerator &) = default;
Expand All @@ -22,21 +21,34 @@ class VirtualGenerator {
};

template <typename CoefficientPrecision>
class VirtualGeneratorWithPermutation : public VirtualGenerator<CoefficientPrecision> {
class VirtualGeneratorInUserNumbering {

public:
virtual void copy_submatrix(int M, int N, const int *rows, const int *cols, CoefficientPrecision *ptr) const = 0;

VirtualGeneratorInUserNumbering() {}
VirtualGeneratorInUserNumbering(const VirtualGeneratorInUserNumbering &) = default;
VirtualGeneratorInUserNumbering &operator=(const VirtualGeneratorInUserNumbering &) = default;
VirtualGeneratorInUserNumbering(VirtualGeneratorInUserNumbering &&) = default;
VirtualGeneratorInUserNumbering &operator=(VirtualGeneratorInUserNumbering &&) = default;
virtual ~VirtualGeneratorInUserNumbering() {}
};

template <typename CoefficientPrecision>
class GeneratorWithPermutation : public VirtualGenerator<CoefficientPrecision> {

protected:
const VirtualGeneratorInUserNumbering<CoefficientPrecision> &m_generator_in_user_numbering;
const int *m_target_permutation;
const int *m_source_permutation;

public:
VirtualGeneratorWithPermutation(const int *target_permutation, const int *source_permutation) : m_target_permutation(target_permutation), m_source_permutation(source_permutation) {
GeneratorWithPermutation(const VirtualGeneratorInUserNumbering<CoefficientPrecision> &generator_in_user_numbering, const int *target_permutation, const int *source_permutation) : m_generator_in_user_numbering(generator_in_user_numbering), m_target_permutation(target_permutation), m_source_permutation(source_permutation) {
}

virtual void copy_submatrix(int M, int N, int row_offset, int col_offset, CoefficientPrecision *ptr) const override {
copy_submatrix_from_user_numbering(M, N, m_target_permutation + row_offset, m_source_permutation + col_offset, ptr);
m_generator_in_user_numbering.copy_submatrix(M, N, m_target_permutation + row_offset, m_source_permutation + col_offset, ptr);
}

virtual void copy_submatrix_from_user_numbering(int M, int N, const int *rows, const int *cols, CoefficientPrecision *ptr) const = 0;
};

} // namespace htool
Expand Down
3 changes: 3 additions & 0 deletions include/htool/hmatrix/tree_builder/tree_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ class HMatrixTreeBuilder {

// Build
HMatrixType build(const VirtualGenerator<CoefficientPrecision> &generator) const;
HMatrixType build(const VirtualGeneratorInUserNumbering<CoefficientPrecision> &generator) const {
return this->build(GeneratorWithPermutation<CoefficientPrecision>(generator, m_target_root_cluster.get_permutation().data(), m_source_root_cluster.get_permutation().data()));
}

// Setters
void set_low_rank_generator(std::shared_ptr<VirtualLowRankGenerator<CoefficientPrecision, CoordinatePrecision>> ptr) { m_low_rank_generator = ptr; }
Expand Down
3 changes: 0 additions & 3 deletions include/htool/local_operators/local_operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ class LocalOperator : public VirtualLocalOperator<CoefficientPrecision> {

virtual void local_sub_matrix_vector_product(const CoefficientPrecision *const in, CoefficientPrecision *const out, int mu, int offset, int size) const {
std::vector<CoefficientPrecision> temp(m_source_cluster.get_size() * mu, 0);
int rankWorld;
MPI_Comm_rank(MPI_COMM_WORLD, &rankWorld);

std::copy_n(in, size * mu, temp.data() + offset * mu);
add_matrix_product_global_to_local(1, temp.data(), 0, out, mu);
}
Expand Down
87 changes: 53 additions & 34 deletions include/htool/testing/generate_test_case.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ namespace htool {

template <typename T, typename GeneratorTestType>
class TestCaseProduct {
std::unique_ptr<GeneratorTestType> operator_in_user_numbering_A = nullptr;
std::unique_ptr<GeneratorTestType> operator_in_user_numbering_B = nullptr;
std::unique_ptr<GeneratorTestType> operator_in_user_numbering_C = nullptr;

public:
char transa;
char transb;
Expand All @@ -24,9 +28,9 @@ class TestCaseProduct {
const Cluster<underlying_type<T>> *root_cluster_B_output = nullptr;
const Cluster<underlying_type<T>> *root_cluster_C_input = nullptr;
const Cluster<underlying_type<T>> *root_cluster_C_output = nullptr;
std::unique_ptr<GeneratorTestType> operator_A = nullptr;
std::unique_ptr<GeneratorTestType> operator_B = nullptr;
std::unique_ptr<GeneratorTestType> operator_C = nullptr;
std::unique_ptr<GeneratorWithPermutation<T>> operator_A = nullptr;
std::unique_ptr<GeneratorWithPermutation<T>> operator_B = nullptr;
std::unique_ptr<GeneratorWithPermutation<T>> operator_C = nullptr;
int ni_A;
int no_A;
int ni_B;
Expand Down Expand Up @@ -79,26 +83,30 @@ class TestCaseProduct {

// Operators
if (transa == 'N') {
operator_A = std::make_unique<GeneratorTestType>(3, no_A, ni_A, x1, x2, *root_cluster_1, *root_cluster_2, true, true);
root_cluster_A_output = root_cluster_1.get();
root_cluster_A_input = root_cluster_2.get();
operator_in_user_numbering_A = std::make_unique<GeneratorTestType>(3, x1, x2);
root_cluster_A_output = root_cluster_1.get();
root_cluster_A_input = root_cluster_2.get();
} else {
operator_A = std::make_unique<GeneratorTestType>(3, no_A, ni_A, x2, x1, *root_cluster_2, *root_cluster_1, true, true);
root_cluster_A_output = root_cluster_2.get();
root_cluster_A_input = root_cluster_1.get();
operator_in_user_numbering_A = std::make_unique<GeneratorTestType>(3, x2, x1);
root_cluster_A_output = root_cluster_2.get();
root_cluster_A_input = root_cluster_1.get();
}
if (transb == 'N') {
operator_B = std::make_unique<GeneratorTestType>(3, no_B, ni_B, x2, x3, *root_cluster_2, *root_cluster_3, true, true);
root_cluster_B_output = root_cluster_2.get();
root_cluster_B_input = root_cluster_3.get();
operator_in_user_numbering_B = std::make_unique<GeneratorTestType>(3, x2, x3);
root_cluster_B_output = root_cluster_2.get();
root_cluster_B_input = root_cluster_3.get();
} else {
operator_B = std::make_unique<GeneratorTestType>(3, no_B, ni_B, x3, x2, *root_cluster_3, *root_cluster_2, true, true);
root_cluster_B_output = root_cluster_3.get();
root_cluster_B_input = root_cluster_2.get();
operator_in_user_numbering_B = std::make_unique<GeneratorTestType>(3, x3, x2);
root_cluster_B_output = root_cluster_3.get();
root_cluster_B_input = root_cluster_2.get();
}
operator_C = std::make_unique<GeneratorTestType>(3, no_C, ni_C, x1, x3, *root_cluster_1, *root_cluster_3, true, true);
root_cluster_C_input = root_cluster_3.get();
root_cluster_C_output = root_cluster_1.get();
operator_in_user_numbering_C = std::make_unique<GeneratorTestType>(3, x1, x3);
root_cluster_C_input = root_cluster_3.get();
root_cluster_C_output = root_cluster_1.get();

operator_A = std::make_unique<GeneratorWithPermutation<T>>(*operator_in_user_numbering_A, root_cluster_A_output->get_permutation().data(), root_cluster_A_input->get_permutation().data());
operator_B = std::make_unique<GeneratorWithPermutation<T>>(*operator_in_user_numbering_B, root_cluster_B_output->get_permutation().data(), root_cluster_B_input->get_permutation().data());
operator_C = std::make_unique<GeneratorWithPermutation<T>>(*operator_in_user_numbering_C, root_cluster_C_output->get_permutation().data(), root_cluster_C_input->get_permutation().data());
}
};

Expand Down Expand Up @@ -257,6 +265,10 @@ class TestCaseSymmetricRankUpdate {

template <typename T, typename GeneratorTestType>
class TestCaseSolve {
private:
std::unique_ptr<GeneratorTestType> operator_in_user_numbering_A = nullptr;
std::unique_ptr<GeneratorTestType> operator_in_user_numbering_X = nullptr;

public:
char side;
char trans;
Expand All @@ -268,8 +280,8 @@ class TestCaseSolve {
const Cluster<underlying_type<T>> *root_cluster_A_output = nullptr;
const Cluster<underlying_type<T>> *root_cluster_X_input = nullptr;
const Cluster<underlying_type<T>> *root_cluster_X_output = nullptr;
std::unique_ptr<GeneratorTestType> operator_A = nullptr;
std::unique_ptr<GeneratorTestType> operator_X = nullptr;
std::unique_ptr<GeneratorWithPermutation<T>> operator_A = nullptr;
std::unique_ptr<GeneratorWithPermutation<T>> operator_X = nullptr;
int ni_A;
int no_A;
int ni_X;
Expand Down Expand Up @@ -306,24 +318,30 @@ class TestCaseSolve {
}

// Operators
operator_A = std::make_unique<GeneratorTestType>(3, no_A, ni_A, x1, x1, *root_cluster_1, *root_cluster_1, true, true);
root_cluster_A_output = root_cluster_1.get();
root_cluster_A_input = root_cluster_1.get();
operator_in_user_numbering_A = std::make_unique<GeneratorTestType>(3, x1, x1);
root_cluster_A_output = root_cluster_1.get();
root_cluster_A_input = root_cluster_1.get();

if (side == 'L') {
operator_X = std::make_unique<GeneratorTestType>(3, no_X, ni_X, x1, x2, *root_cluster_1, *root_cluster_2, true, true);
root_cluster_X_output = root_cluster_1.get();
root_cluster_X_input = root_cluster_2.get();
operator_in_user_numbering_X = std::make_unique<GeneratorTestType>(3, x1, x2);
root_cluster_X_output = root_cluster_1.get();
root_cluster_X_input = root_cluster_2.get();
} else {
operator_X = std::make_unique<GeneratorTestType>(3, no_X, ni_X, x2, x1, *root_cluster_2, *root_cluster_1, true, true);
root_cluster_X_output = root_cluster_2.get();
root_cluster_X_input = root_cluster_1.get();
operator_in_user_numbering_X = std::make_unique<GeneratorTestType>(3, x2, x1);
root_cluster_X_output = root_cluster_2.get();
root_cluster_X_input = root_cluster_1.get();
}

operator_A = std::make_unique<GeneratorWithPermutation<T>>(*operator_in_user_numbering_A, root_cluster_A_output->get_permutation().data(), root_cluster_A_input->get_permutation().data());
operator_X = std::make_unique<GeneratorWithPermutation<T>>(*operator_in_user_numbering_X, root_cluster_X_output->get_permutation().data(), root_cluster_X_input->get_permutation().data());
}
};

template <typename T, typename GeneratorTestType>
class TestCaseAddition {
std::unique_ptr<GeneratorTestType> operator_in_user_numbering_A = nullptr;
std::unique_ptr<GeneratorTestType> operator_in_user_numbering_B = nullptr;

public:
std::vector<underlying_type<T>> x1;
std::vector<underlying_type<T>> x2;
Expand All @@ -334,8 +352,8 @@ class TestCaseAddition {
const Cluster<underlying_type<T>> *root_cluster_A_output = nullptr;
const Cluster<underlying_type<T>> *root_cluster_B_input = nullptr;
const Cluster<underlying_type<T>> *root_cluster_B_output = nullptr;
std::unique_ptr<GeneratorTestType> operator_A = nullptr;
std::unique_ptr<GeneratorTestType> operator_B = nullptr;
std::unique_ptr<GeneratorWithPermutation<T>> operator_A = nullptr;
std::unique_ptr<GeneratorWithPermutation<T>> operator_B = nullptr;
int ni_A;
int no_A;
int ni_B;
Expand Down Expand Up @@ -371,9 +389,10 @@ class TestCaseAddition {
}

// Operators
operator_A = std::make_unique<GeneratorTestType>(3, no_A, ni_A, x1, x2, *root_cluster_1, *root_cluster_2, true, true);
root_cluster_A_output = root_cluster_1.get();
root_cluster_A_input = root_cluster_2.get();
operator_in_user_numbering_A = std::make_unique<GeneratorTestType>(3, x1, x2);
root_cluster_A_output = root_cluster_1.get();
root_cluster_A_input = root_cluster_2.get();
operator_A = std::make_unique<GeneratorWithPermutation<T>>(*operator_in_user_numbering_A, root_cluster_A_output->get_permutation().data(), root_cluster_A_input->get_permutation().data());

// Sub lrmat two level deep
std::random_device rd;
Expand Down
Loading

0 comments on commit d3563fc

Please sign in to comment.