From e9e64b4eb7c8e65ca06b081cf00ef59840eb3f8c Mon Sep 17 00:00:00 2001 From: Michele Ceriotti Date: Sat, 1 May 2021 23:38:45 +0200 Subject: [PATCH 1/2] Modified iterators to enable #omp clauses --- CMakeLists.txt | 2 +- .../structure_managers/structure_manager.hh | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e2e6d6abb..37e1987f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ SET(TYPE_ARCHITECTURE "native" CACHE STRING ) ########## COMPILATION FLAGS ########## -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Weffc++ -Wno-non-virtual-dtor") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Weffc++ -Wno-non-virtual-dtor -O3 -fopenmp") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR diff --git a/src/rascal/structure_managers/structure_manager.hh b/src/rascal/structure_managers/structure_manager.hh index 76a349dae..50622bdc0 100644 --- a/src/rascal/structure_managers/structure_manager.hh +++ b/src/rascal/structure_managers/structure_manager.hh @@ -1387,6 +1387,20 @@ namespace rascal { return *this; } + //! int increment + Iterator & operator+=(size_t increment) { + this->index += increment; + return *this; + } + + //! int decrement + Iterator & operator-=(size_t increment) { + this->index -= increment; + return *this; + } + //! subtraction between iterators + auto operator-(const Iterator & other) { return other.index - this->index; } + value_type operator*() { auto & cluster_indices_properties = std::get( this->get_manager().get_cluster_indices_container()); @@ -1420,6 +1434,19 @@ namespace rascal { return not(*this == other); } + //! comparison operators + bool operator<(const Iterator & other) { return this->index < other.index; } + + bool operator>(const Iterator & other) { return this->index > other.index; } + + bool operator<=(const Iterator & other) { + return this->index <= other.index; + } + + bool operator>=(const Iterator & other) { + return this->index >= other.index; + } + /** * const access to container */ From cdf2d7266d14af732b081138dfdaa63bf3a6da57 Mon Sep 17 00:00:00 2001 From: Michele Ceriotti Date: Sun, 2 May 2021 11:30:51 +0200 Subject: [PATCH 2/2] openmp works, but attempts to parallelize any loop breaks havoc this is not a superficial restructure, because loops over centers are not thread-safe --- .gitattributes | 3 +++ CMakeLists.txt | 2 +- src/rascal/models/sparse_kernel_predict.hh | 2 ++ src/rascal/models/sparse_kernels.hh | 4 ++++ .../representations/calculator_spherical_expansion.hh | 1 + .../representations/calculator_spherical_invariants.hh | 10 +++++++++- src/rascal/structure_managers/structure_manager.hh | 5 ++++- 7 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.gitattributes b/.gitattributes index 305a9b048..f4d6083ab 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,5 @@ *.ipynb filter=nbstripout *.ipynb diff=ipynb + +examples/zundel_i-PI.ipynb filter= diff= + diff --git a/CMakeLists.txt b/CMakeLists.txt index 37e1987f4..487faa403 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,7 +81,7 @@ SET(TYPE_ARCHITECTURE "native" CACHE STRING ) ########## COMPILATION FLAGS ########## -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Weffc++ -Wno-non-virtual-dtor -O3 -fopenmp") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Weffc++ -Wno-non-virtual-dtor -fopenmp") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR diff --git a/src/rascal/models/sparse_kernel_predict.hh b/src/rascal/models/sparse_kernel_predict.hh index f27621904..087c91d72 100644 --- a/src/rascal/models/sparse_kernel_predict.hh +++ b/src/rascal/models/sparse_kernel_predict.hh @@ -127,6 +127,8 @@ namespace rascal { math::Vector_t weights_scaled(weights.size()); size_t i_row{0}; + +#pragma omp for for (auto center : manager) { const int a_sp{center.get_atom_type()}; // compute contraction of the model weights with the gradient of the diff --git a/src/rascal/models/sparse_kernels.hh b/src/rascal/models/sparse_kernels.hh index 656ad77f2..35d1536b0 100644 --- a/src/rascal/models/sparse_kernels.hh +++ b/src/rascal/models/sparse_kernels.hh @@ -178,6 +178,7 @@ namespace rascal { for (auto & manager : managers) { auto && propA{*manager->template get_property( representation_name, true)}; +#pragma omp for for (auto center : manager) { int sp = center.get_atom_type(); KNM.row(ii_A) = @@ -321,6 +322,7 @@ namespace rascal { if (zeta > 1) { dkdX_missing_T.setZero(); // zeta * (X * T)**(zeta-1) +#pragma omp for for (auto center : manager) { int a_species{center.get_atom_type()}; dkdX_missing_T[center] = @@ -362,6 +364,7 @@ namespace rascal { if (do_block_by_key_dot) { size_t idx_row{0}; auto repr_grads = prop_repr_grad.get_raw_data_view(); +#pragma omp for for (auto center : manager) { int a_species{center.get_atom_type()}; Eigen::Vector3d r_i = center.get_position(); @@ -447,6 +450,7 @@ namespace rascal { idx_row += nb_rows; } // center } else { +#pragma omp for for (auto center : manager) { int a_species{center.get_atom_type()}; Eigen::Vector3d r_i = center.get_position(); diff --git a/src/rascal/representations/calculator_spherical_expansion.hh b/src/rascal/representations/calculator_spherical_expansion.hh index 786925d3c..1682ce353 100644 --- a/src/rascal/representations/calculator_spherical_expansion.hh +++ b/src/rascal/representations/calculator_spherical_expansion.hh @@ -1791,6 +1791,7 @@ namespace rascal { // coeff C^{ij}_{nlm} auto c_ij_nlm = math::Matrix_t(n_row, n_col); +#pragma omp for for (auto center : manager) { // c^{i} auto & coefficients_center = expansions_coefficients[center]; diff --git a/src/rascal/representations/calculator_spherical_invariants.hh b/src/rascal/representations/calculator_spherical_invariants.hh index ad543d970..135da8076 100644 --- a/src/rascal/representations/calculator_spherical_invariants.hh +++ b/src/rascal/representations/calculator_spherical_invariants.hh @@ -605,6 +605,7 @@ namespace rascal { Eigen::Vector3d soap_vector_dot_gradient{}; // compute the dot product and update the gradients to be normalized +#pragma omp for for (auto center : manager) { for (auto neigh : center.pairs_with_self_pair()) { const auto & soap_vector = soap_vectors[center]; @@ -744,6 +745,7 @@ namespace rascal { *manager, "power spectrums inverse norms", true}; soap_vector_norm_inv.resize(); +#pragma omp for for (auto center : manager) { auto & coefficients{expansions_coefficients[center]}; auto & soap_vector{soap_vectors[center]}; @@ -976,6 +978,7 @@ namespace rascal { *manager, "radial spectrum inverse norms", ExcludeGhosts}; soap_vector_norm_inv.resize(); +#pragma omp for for (auto center : manager) { const auto & coefficients{expansions_coefficients[center]}; auto & soap_vector{soap_vectors[center]}; @@ -1048,7 +1051,12 @@ namespace rascal { double mult{1.0}; Key_t trip_type{0, 0, 0}; internal::SortedKey triplet_type{trip_type}; - for (auto center : manager) { + +#pragma omp for + for (auto iter = manager->begin(); iterend(); ++iter) { + std::cerr<<"computing "<end().get_index()<<"\n"; + auto center = *iter; +// for (auto center : manager) { auto & coefficients{expansions_coefficients[center]}; auto & soap_vector{soap_vectors[center]}; diff --git a/src/rascal/structure_managers/structure_manager.hh b/src/rascal/structure_managers/structure_manager.hh index 50622bdc0..e65d5303e 100644 --- a/src/rascal/structure_managers/structure_manager.hh +++ b/src/rascal/structure_managers/structure_manager.hh @@ -1399,7 +1399,7 @@ namespace rascal { return *this; } //! subtraction between iterators - auto operator-(const Iterator & other) { return other.index - this->index; } + auto operator-(const Iterator & other) { return this->index - other.index; } value_type operator*() { auto & cluster_indices_properties = std::get( @@ -1447,6 +1447,9 @@ namespace rascal { return this->index >= other.index; } + size_t get_index() { + return this->index; + } /** * const access to container */