-
Notifications
You must be signed in to change notification settings - Fork 276
Speed up Expr * Expr
#1175
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
Open
Zeroto521
wants to merge
5
commits into
scipopt:master
Choose a base branch
from
Zeroto521:expr/mul
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Speed up Expr * Expr
#1175
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
08b11b7
Refactor Expr multiplication logic and Term operator
Zeroto521 b8e2249
Optimize Term multiplication in expr.pxi
Zeroto521 28de39e
Update CHANGELOG to reorder quicksum optimization entry
Zeroto521 8ec24a4
Update changelog with Expr multiplication speedup
Zeroto521 fd06702
Fix Term class operator signature in type stub
Zeroto521 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,9 +45,10 @@ | |
| import math | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pyscipopt.scip cimport Variable, Solution | ||
| from cpython.dict cimport PyDict_Next | ||
| from cpython.dict cimport PyDict_Next, PyDict_GetItem | ||
| from cpython.tuple cimport PyTuple_GET_ITEM | ||
| from cpython.ref cimport PyObject | ||
| from pyscipopt.scip cimport Variable, Solution | ||
|
|
||
| import numpy as np | ||
|
|
||
|
|
@@ -122,9 +123,39 @@ cdef class Term: | |
| def __len__(self): | ||
| return len(self.vartuple) | ||
|
|
||
| def __add__(self, other): | ||
| both = self.vartuple + other.vartuple | ||
| return Term(*both) | ||
| def __mul__(self, Term other): | ||
| cdef int n1 = len(self) | ||
| cdef int n2 = len(other) | ||
| if n1 == 0: return other | ||
| if n2 == 0: return self | ||
|
|
||
| cdef list vartuple = [None] * (n1 + n2) | ||
| cdef int i = 0, j = 0, k = 0 | ||
| cdef Variable var1, var2 | ||
| while i < n1 and j < n2: | ||
| var1 = <Variable>PyTuple_GET_ITEM(self.vartuple, i) | ||
| var2 = <Variable>PyTuple_GET_ITEM(other.vartuple, j) | ||
| if var1.ptr() <= var2.ptr(): | ||
| vartuple[k] = var1 | ||
| i += 1 | ||
| else: | ||
| vartuple[k] = var2 | ||
| j += 1 | ||
| k += 1 | ||
|
Comment on lines
+135
to
+144
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the Merge Sort Algorithm, with a time complexity of |
||
| while i < n1: | ||
| vartuple[k] = <Variable>PyTuple_GET_ITEM(self.vartuple, i) | ||
| i += 1 | ||
| k += 1 | ||
| while j < n2: | ||
| vartuple[k] = <Variable>PyTuple_GET_ITEM(other.vartuple, j) | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
| cdef Term res = Term.__new__(Term) | ||
| res.vartuple = tuple(vartuple) | ||
| res.ptrtuple = tuple(v.ptr() for v in res.vartuple) | ||
| res.hashval = <Py_ssize_t>hash(res.ptrtuple) | ||
| return res | ||
|
|
||
| def __repr__(self): | ||
| return 'Term(%s)' % ', '.join([str(v) for v in self.vartuple]) | ||
|
|
@@ -251,19 +282,42 @@ cdef class Expr: | |
| if isinstance(other, np.ndarray): | ||
| return other * self | ||
|
|
||
| cdef dict res = {} | ||
| cdef Py_ssize_t pos1 = <Py_ssize_t>0, pos2 = <Py_ssize_t>0 | ||
| cdef PyObject *k1_ptr = NULL | ||
| cdef PyObject *v1_ptr = NULL | ||
| cdef PyObject *k2_ptr = NULL | ||
| cdef PyObject *v2_ptr = NULL | ||
| cdef PyObject *old_v_ptr = NULL | ||
| cdef Term child | ||
| cdef double v1_val, v2_val, prod_v | ||
|
|
||
| if _is_number(other): | ||
| f = float(other) | ||
| return Expr({v:f*c for v,c in self.terms.items()}) | ||
|
|
||
| elif _is_number(self): | ||
| f = float(self) | ||
| return Expr({v:f*c for v,c in other.terms.items()}) | ||
|
|
||
| elif isinstance(other, Expr): | ||
| terms = {} | ||
| for v1, c1 in self.terms.items(): | ||
| for v2, c2 in other.terms.items(): | ||
| v = v1 + v2 | ||
| terms[v] = terms.get(v, 0.0) + c1 * c2 | ||
| return Expr(terms) | ||
| while PyDict_Next(self.terms, &pos1, &k1_ptr, &v1_ptr): | ||
| if (v1_val := <double>(<object>v1_ptr)) == 0: | ||
| continue | ||
|
|
||
| pos2 = <Py_ssize_t>0 | ||
| while PyDict_Next(other.terms, &pos2, &k2_ptr, &v2_ptr): | ||
| if (v2_val := <double>(<object>v2_ptr)) == 0: | ||
| continue | ||
|
|
||
| child = (<Term>k1_ptr) * (<Term>k2_ptr) | ||
| prod_v = v1_val * v2_val | ||
| if (old_v_ptr := PyDict_GetItem(res, child)) != NULL: | ||
| res[child] = <double>(<object>old_v_ptr) + prod_v | ||
| else: | ||
| res[child] = prod_v | ||
| return Expr(res) | ||
|
Comment on lines
+304
to
+319
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Cython API to speed up. |
||
|
|
||
| elif isinstance(other, GenExpr): | ||
| return buildGenExprObj(self) * other | ||
| else: | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
__mul__is better. We call this function actually to multiply