Skip to content

Commit

Permalink
remove get_diagonal_block, and add et_sub_hmatrix
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreMarchand20 committed Nov 16, 2023
1 parent d8bf9ae commit 2976587
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
4 changes: 2 additions & 2 deletions include/htool/distributed_operator/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DefaultApproximationBuilder {

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) : target_partition(target_cluster), source_partition(source_cluster), hmatrix(HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>(target_cluster, source_cluster, epsilon, eta, symmetry, UPLO, -1, get_rankWorld(communicator)).build(generator)), local_hmatrix(hmatrix, target_cluster.get_cluster_on_partition(get_rankWorld(communicator)), source_cluster, symmetry, UPLO, false, false), distributed_operator(target_partition, source_partition, symmetry, UPLO, communicator) {
distributed_operator.add_local_operator(&local_hmatrix);
block_diagonal_hmatrix = hmatrix.get_diagonal_hmatrix();
block_diagonal_hmatrix = hmatrix.get_sub_hmatrix(target_cluster.get_cluster_on_partition(get_rankWorld(communicator)), source_cluster.get_cluster_on_partition(get_rankWorld(communicator)));
}
};

Expand All @@ -54,7 +54,7 @@ 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) : target_partition(target_cluster), source_partition(source_cluster), hmatrix(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, -1).build(generator)), local_hmatrix(hmatrix, target_cluster.get_cluster_on_partition(get_rankWorld(communicator)), source_cluster.get_cluster_on_partition(get_rankWorld(communicator)), symmetry, UPLO, false, false), distributed_operator(target_partition, source_partition, symmetry, UPLO, communicator) {
distributed_operator.add_local_operator(&local_hmatrix);
block_diagonal_hmatrix = hmatrix.get_diagonal_hmatrix();
block_diagonal_hmatrix = hmatrix.get_sub_hmatrix(target_cluster.get_cluster_on_partition(get_rankWorld(communicator)), source_cluster.get_cluster_on_partition(get_rankWorld(communicator)));
}
};

Expand Down
23 changes: 20 additions & 3 deletions include/htool/hmatrix/hmatrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#if defined(_OPENMP)
# include <omp.h>
#endif

#include "../basic_types/tree.hpp"
#include "../clustering/cluster_node.hpp"
#include "../misc/logger.hpp"
Expand All @@ -13,6 +12,7 @@
#include "interfaces/virtual_dense_blocks_generator.hpp"
#include "interfaces/virtual_generator.hpp"
#include "lrmat/lrmat.hpp"
#include <queue>

#include <mpi.h>
namespace htool {
Expand Down Expand Up @@ -117,6 +117,25 @@ class HMatrix : public TreeNode<HMatrix<CoefficientPrecision, CoordinatePrecisio
char get_symmetry() const { return m_symmetry; }
char get_UPLO() const { return m_UPLO; }
const HMatrixTreeData<CoefficientPrecision, CoordinatePrecision> *get_hmatrix_tree_data() const { return this->m_tree_data.get(); }
const HMatrix<CoefficientPrecision> *get_sub_hmatrix(const Cluster<CoordinatePrecision> &target_cluster, const Cluster<CoordinatePrecision> &source_cluster) const {
std::queue<const HMatrix<CoefficientPrecision> *> hmatrix_queue;
hmatrix_queue.push(this);

while (!hmatrix_queue.empty()) {
const HMatrix<CoefficientPrecision> *current_hmatrix = hmatrix_queue.front();
hmatrix_queue.pop();

if (target_cluster == current_hmatrix->get_target_cluster() && source_cluster == current_hmatrix->get_source_cluster()) {
return current_hmatrix;
}

const auto &children = current_hmatrix->get_children();
for (auto &child : children) {
hmatrix_queue.push(child.get());
}
}
return nullptr;
}

// HMatrix node setters
void set_symmetry(char symmetry) { m_symmetry = symmetry; }
Expand All @@ -133,12 +152,10 @@ class HMatrix : public TreeNode<HMatrix<CoefficientPrecision, CoordinatePrecisio
void set_low_rank_generator(std::shared_ptr<VirtualLowRankGenerator<CoefficientPrecision, CoordinatePrecision>> ptr) { this->m_tree_data->m_low_rank_generator = ptr; }
void set_admissibility_condition(std::shared_ptr<VirtualAdmissibilityCondition<CoordinatePrecision>> ptr) { this->m_tree_data->m_admissibility_condition = ptr; }
void set_maximal_block_size(int maxblock_size) { this->m_tree_data->m_maxblocksize = maxblock_size; }
void set_diagonal_hmatrix(const HMatrix<CoefficientPrecision, CoordinatePrecision> *diagonal_hmatrix) { this->m_tree_data->m_block_diagonal_hmatrix = diagonal_hmatrix; }
void set_minimal_target_depth(unsigned int minimal_target_depth) { this->m_tree_data->m_minimal_target_depth = minimal_target_depth; }
void set_minimal_source_depth(unsigned int minimal_source_depth) { this->m_tree_data->m_minimal_source_depth = minimal_source_depth; }

// HMatrix Tree setters
const HMatrix<CoefficientPrecision, CoordinatePrecision> *get_diagonal_hmatrix() const { return this->m_tree_data->m_block_diagonal_hmatrix; }
char get_symmetry_for_leaves() const { return m_symmetry_type_for_leaves; }

// Infos
Expand Down
3 changes: 0 additions & 3 deletions include/htool/hmatrix/hmatrix_tree_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ struct HMatrixTreeData {
bool m_delay_dense_computation{false};
int m_reqrank{-1};

// Views
const HMatrix<CoefficientPrecision, CoordinatePrecision> *m_block_diagonal_hmatrix{nullptr};

// Information
mutable std::map<std::string, std::string> m_information;
mutable std::map<std::string, std::chrono::duration<double>> m_timings;
Expand Down
31 changes: 18 additions & 13 deletions include/htool/hmatrix/tree_builder/tree_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ bool HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>::build_block_

///////////////////// Diagonal blocks
// std::cout << target_cluster.get_offset() << " " << target_cluster.get_size() << " " << source_cluster.get_offset() << " " << source_cluster.get_size() << " " << is_block_diagonal(*current_hmatrix) << " " << is_target_cluster_in_target_partition(target_cluster) << " " << target_cluster.get_rank() << "\n";
if (is_block_diagonal(*current_hmatrix)) {
current_hmatrix->set_diagonal_hmatrix(current_hmatrix);
}
// if (is_block_diagonal(*current_hmatrix)) {
// current_hmatrix->set_diagonal_hmatrix(current_hmatrix);
// }

///////////////////// Recursion
const auto &target_children = target_cluster.get_children();
Expand Down Expand Up @@ -229,8 +229,9 @@ bool HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>::build_block_
&& is_target_cluster_in_target_partition(target_cluster)
&& target_cluster.get_depth() >= m_mintargetdepth
&& source_cluster.get_depth() >= m_minsourcedepth
&& std::all_of(child_blocks.begin(), child_blocks.end(), [](HMatrixType *block) {
return (block != block->get_diagonal_hmatrix());
&& std::all_of(child_blocks.begin(), child_blocks.end(), [this](HMatrixType *block) {
// return (block != block->get_diagonal_hmatrix());
return !is_block_diagonal(*block);
})) {
child_blocks.clear();
current_hmatrix->delete_children();
Expand Down Expand Up @@ -263,8 +264,9 @@ bool HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>::build_block_
&& is_target_cluster_in_target_partition(target_cluster)
&& target_cluster.get_depth() >= m_mintargetdepth
&& source_cluster.get_depth() >= m_minsourcedepth
&& std::all_of(child_blocks.begin(), child_blocks.end(), [](HMatrixType *block) {
return (block != block->get_diagonal_hmatrix());
&& std::all_of(child_blocks.begin(), child_blocks.end(), [this](HMatrixType *block) {
// return (block != block->get_diagonal_hmatrix());
return !is_block_diagonal(*block);
})) {
child_blocks.clear();
current_hmatrix->delete_children();
Expand Down Expand Up @@ -294,8 +296,9 @@ bool HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>::build_block_
&& is_target_cluster_in_target_partition(target_cluster)
&& target_cluster.get_depth() >= m_mintargetdepth
&& source_cluster.get_depth() >= m_minsourcedepth
&& std::all_of(child_blocks.begin(), child_blocks.end(), [](HMatrixType *block) {
return (block != block->get_diagonal_hmatrix());
&& std::all_of(child_blocks.begin(), child_blocks.end(), [this](HMatrixType *block) {
// return (block != block->get_diagonal_hmatrix());
return !is_block_diagonal(*block);
})) {
child_blocks.clear();
current_hmatrix->delete_children();
Expand Down Expand Up @@ -324,8 +327,9 @@ bool HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>::build_block_
&& is_target_cluster_in_target_partition(target_cluster)
&& target_cluster.get_depth() >= m_mintargetdepth
&& source_cluster.get_depth() >= m_minsourcedepth
&& std::all_of(child_blocks.begin(), child_blocks.end(), [](HMatrixType *block) {
return (block != block->get_diagonal_hmatrix());
&& std::all_of(child_blocks.begin(), child_blocks.end(), [this](HMatrixType *block) {
// return (block != block->get_diagonal_hmatrix());
return !is_block_diagonal(*block);
})) {
child_blocks.clear();
current_hmatrix->delete_children();
Expand Down Expand Up @@ -356,8 +360,9 @@ bool HMatrixTreeBuilder<CoefficientPrecision, CoordinatePrecision>::build_block_
&& is_target_cluster_in_target_partition(target_cluster)
&& target_cluster.get_depth() >= m_mintargetdepth
&& source_cluster.get_depth() >= m_minsourcedepth
&& std::all_of(child_blocks.begin(), child_blocks.end(), [](HMatrixType *block) {
return (block != block->get_diagonal_hmatrix());
&& std::all_of(child_blocks.begin(), child_blocks.end(), [this](HMatrixType *block) {
// return (block != block->get_diagonal_hmatrix());
return !is_block_diagonal(*block);
})) {
child_blocks.clear();
current_hmatrix->delete_children();
Expand Down
8 changes: 4 additions & 4 deletions tests/functional_tests/hmatrix/test_hmatrix_build.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ bool test_hmatrix_build(int nr, int nc, bool use_local_cluster, char Symmetry, c
}

// Check integrity of diagonal block
const auto &diagonal_hmatrix = root_hmatrix.get_diagonal_hmatrix();
const auto &diagonal_hmatrix = root_hmatrix.get_sub_hmatrix(*target_root_cluster->get_clusters_on_partition()[rankWorld], *source_root_cluster->get_clusters_on_partition()[rankWorld]);
if (diagonal_hmatrix == nullptr) {
// test = test || !(Symmetry == 'N' && nr != nc);
std::cout << "No diagonal hmatrix\n";
Expand Down Expand Up @@ -166,15 +166,15 @@ bool test_hmatrix_build(int nr, int nc, bool use_local_cluster, char Symmetry, c

// Check get diagonal conversion
if (Symmetry != 'N' || nr == nc) {
int local_size = root_hmatrix.get_diagonal_hmatrix()->get_target_cluster().get_size();
int local_offset = root_hmatrix.get_diagonal_hmatrix()->get_target_cluster().get_offset();
int local_size = diagonal_hmatrix->get_target_cluster().get_size();
int local_offset = diagonal_hmatrix->get_target_cluster().get_offset();
// const auto &permutation = root_hmatrix.get_target_cluster().get_permutation();
vector<T> dense_diagonal(local_size);
for (int i = 0; i < dense_matrix.nb_rows(); i++) {
dense_diagonal[i] = generator.get_coef(i + local_offset, i + local_offset);
}
vector<T> hmatrix_diagonal_to_dense(local_size);
copy_diagonal(*root_hmatrix.get_diagonal_hmatrix(), hmatrix_diagonal_to_dense.data());
copy_diagonal(*diagonal_hmatrix, hmatrix_diagonal_to_dense.data());
htool::underlying_type<T> error_on_diagonal = norm2(hmatrix_diagonal_to_dense - dense_diagonal) / norm2(dense_diagonal);

is_error = is_error || !(error_on_diagonal < epsilon);
Expand Down

0 comments on commit 2976587

Please sign in to comment.