-
Notifications
You must be signed in to change notification settings - Fork 9
Separate optimize #484
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
Merged
Merged
Separate optimize #484
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
623f11f
Make optimize an independent file set again
Krzmbrzl f6a5e66
Consistently use opt namespace
Krzmbrzl 23718ba
Disentangle optimize implementation
Krzmbrzl 1781a02
remaining #include path update: core/eval/optimize -> core/optimize/o…
evaleev bec0fa0
restored tail_factor in integration eval tests
evaleev 97a8cb8
as of #476 (ca4303962) AppleClang 16 does not build with AppleClang 1…
evaleev 5c05dbe
[cmake] add a CMake check for usability of the given clang compiler a…
evaleev 3cfd897
work around the valgrind test timeout by omitting longest tests + tur…
evaleev 4e01d12
optimize.cpp: add missing range-v3 #includes
evaleev 2354eb9
global linker flags are only updated if SeQuant is top project, but l…
evaleev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
SeQuant/core/eval/optimize/fusion.cpp → SeQuant/core/optimize/fusion.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #include <SeQuant/core/binary_node.hpp> | ||
| #include <SeQuant/core/complex.hpp> | ||
| #include <SeQuant/core/container.hpp> | ||
| #include <SeQuant/core/eval/eval_expr.hpp> | ||
| #include <SeQuant/core/expr.hpp> | ||
| #include <SeQuant/core/hash.hpp> | ||
| #include <SeQuant/core/optimize/optimize.hpp> | ||
| #include <SeQuant/core/optimize/single_term.hpp> | ||
| #include <SeQuant/core/optimize/sum.hpp> | ||
| #include <SeQuant/core/utility/indices.hpp> | ||
| #include <SeQuant/core/utility/macros.hpp> | ||
|
|
||
| #include <range/v3/algorithm/all_of.hpp> | ||
| #include <range/v3/iterator/basic_iterator.hpp> | ||
| #include <range/v3/range/access.hpp> | ||
| #include <range/v3/range/conversion.hpp> | ||
| #include <range/v3/view/tail.hpp> | ||
| #include <range/v3/view/transform.hpp> | ||
| #include <range/v3/view/view.hpp> | ||
|
|
||
| #include <cstddef> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| namespace sequant { | ||
|
|
||
| namespace opt { | ||
|
|
||
| /// | ||
| /// \param expr Expression to be optimized. | ||
| /// \param idxsz An invocable object that maps an Index object to size. | ||
| /// \param reorder_sum If true, the summands are reordered so that terms with | ||
| /// common sub-expressions appear closer to each other. | ||
| /// \return Optimized expression for lower evaluation cost. | ||
| template <typename IdxToSize, typename = std::enable_if_t<std::is_invocable_r_v< | ||
| size_t, IdxToSize, const Index&>>> | ||
| ExprPtr optimize(ExprPtr const& expr, IdxToSize const& idx2size, | ||
| bool reorder_sum) { | ||
| using ranges::views::transform; | ||
| if (expr->is<Product>()) { | ||
| if (ranges::all_of(*expr, [](auto&& x) { | ||
| return x->template is<Tensor>() || x->template is<Variable>(); | ||
| })) | ||
| return opt::single_term_opt(expr->as<Product>(), idx2size); | ||
| else { | ||
| auto const& prod = expr->as<Product>(); | ||
|
|
||
| container::svector<ExprPtr> non_tensors(prod.size()); | ||
| container::svector<ExprPtr> new_factors; | ||
|
|
||
| for (auto i = 0; i < prod.size(); ++i) { | ||
| auto&& f = prod.factor(i); | ||
| if (f.is<Tensor>() || f.is<Variable>()) | ||
| new_factors.emplace_back(f); | ||
| else { | ||
| non_tensors[i] = f; | ||
| auto target_idxs = get_unique_indices(f); | ||
| new_factors.emplace_back( | ||
| ex<Tensor>(L"I_" + std::to_wstring(i), bra(target_idxs.bra), | ||
| ket(target_idxs.ket), aux(target_idxs.aux))); | ||
| } | ||
| } | ||
|
|
||
| auto result = opt::single_term_opt( | ||
| Product(prod.scalar(), new_factors, Product::Flatten::No), idx2size); | ||
|
|
||
| auto replacer = [&non_tensors](ExprPtr& out) { | ||
| if (!out->is<Tensor>()) return; | ||
| auto const& tnsr = out->as<Tensor>(); | ||
| auto&& label = tnsr.label(); | ||
| if (label.at(0) == L'I' && label.at(1) == L'_') { | ||
| size_t suffix = std::stoi(std::wstring(label.data() + 2)); | ||
| out = non_tensors[suffix].clone(); | ||
| } | ||
| }; | ||
|
|
||
| result->visit(replacer, /* atoms_only = */ true); | ||
| return result; | ||
| } | ||
| } else if (expr->is<Sum>()) { | ||
| auto smands = *expr | transform([&idx2size](auto&& s) { | ||
| return optimize(s, idx2size, /* reorder_sum= */ false); | ||
| }) | ranges::to_vector; | ||
| auto sum = Sum{smands.begin(), smands.end()}; | ||
| return reorder_sum ? ex<Sum>(opt::reorder(sum)) : ex<Sum>(std::move(sum)); | ||
| } else | ||
| return expr->clone(); | ||
| } | ||
|
|
||
| } // namespace opt | ||
|
|
||
| ExprPtr optimize(ExprPtr const& expr, bool reorder_sum) { | ||
| return opt::optimize( | ||
| expr, [](Index const& ix) { return ix.space().approximate_size(); }, | ||
| reorder_sum); | ||
| } | ||
|
|
||
| ResultExpr& optimize(ResultExpr& expr, bool reorder_sum) { | ||
| expr.expression() = optimize(expr.expression(), reorder_sum); | ||
|
|
||
| return expr; | ||
| } | ||
|
|
||
| ResultExpr& optimize(ResultExpr&& expr, bool reorder_sum) { | ||
| return optimize(expr, reorder_sum); | ||
| } | ||
|
|
||
| } // namespace sequant | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #ifndef SEQUANT_OPTIMIZE_OPTIMIZE_HPP | ||
| #define SEQUANT_OPTIMIZE_OPTIMIZE_HPP | ||
|
|
||
| #include <SeQuant/core/expr_fwd.hpp> | ||
|
|
||
| namespace sequant { | ||
|
|
||
| /// | ||
| /// Optimize the expression using IndexSpace::approximate_size() for reference | ||
| /// index extent. | ||
| /// | ||
| /// \param expr Expression to be optimized. | ||
| /// \param reorder_sum If true, the summands are reordered so that terms with | ||
| /// common sub-expressions appear closer to each other. | ||
| /// True by default. | ||
| /// \return Optimized expression for lower evaluation cost. | ||
| ExprPtr optimize(ExprPtr const& expr, bool reorder_sum = true); | ||
|
|
||
| /// Optimize the expression using IndexSpace::approximate_size() for reference | ||
| /// index extent. | ||
| /// | ||
| /// \param expr Expression to be optimized. | ||
| /// \param reorder_sum If true, the summands are reordered so that terms with | ||
| /// common sub-expressions appear closer to each other. | ||
| /// True by default. | ||
| /// \return Optimized expression for lower evaluation cost. | ||
| ResultExpr& optimize(ResultExpr& expr, bool reorder_sum = true); | ||
|
|
||
| /// Optimize the expression using IndexSpace::approximate_size() for reference | ||
| /// index extent. | ||
| /// | ||
| /// \param expr Expression to be optimized. | ||
| /// \param reorder_sum If true, the summands are reordered so that terms with | ||
| /// common sub-expressions appear closer to each other. | ||
| /// True by default. | ||
| /// \return Optimized expression for lower evaluation cost. | ||
| [[nodiscard]] ResultExpr& optimize(ResultExpr&& expr, bool reorder_sum = true); | ||
|
|
||
| } // namespace sequant | ||
|
|
||
| #endif // SEQUANT_OPTIMIZE_OPTIMIZE_HPP |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.