-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
FIX: support_enumeration: Use _numba_linalg_solve
#311
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9000327
support_enumeration: Remove fallback for Numba < 0.28
oyamad d812866
support_enumeration: Add a test
oyamad 44c9919
util: Add `_numba_linalg_solve`
oyamad 20da0f8
FIX: support_enumeration: Use `_numba_linalg_solve`
oyamad e7bcb02
support_enumeration: Remove `any()`
oyamad 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 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 |
---|---|---|
|
@@ -7,5 +7,6 @@ Utilities | |
util/array | ||
util/common_messages | ||
util/notebooks | ||
util/numba | ||
util/random | ||
util/timing |
This file contains 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,7 @@ | ||
numba | ||
===== | ||
|
||
.. automodule:: quantecon.util.numba | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains 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 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 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,71 @@ | ||
""" | ||
Utilities to support Numba jitted functions | ||
|
||
""" | ||
import numpy as np | ||
from numba import generated_jit, types | ||
from numba.targets.linalg import _LAPACK | ||
|
||
|
||
# BLAS kinds as letters | ||
_blas_kinds = { | ||
types.float32: 's', | ||
types.float64: 'd', | ||
types.complex64: 'c', | ||
types.complex128: 'z', | ||
} | ||
|
||
|
||
@generated_jit(nopython=True, cache=True) | ||
def _numba_linalg_solve(a, b): | ||
""" | ||
Solve the linear equation ax = b directly calling a Numba internal | ||
function. The data in `a` and `b` are interpreted in Fortran order, | ||
and dtype of `a` and `b` must be the same, one of {float32, float64, | ||
complex64, complex128}. `a` and `b` are modified in place, and the | ||
solution is stored in `b`. *No error check is made for the inputs.* | ||
|
||
Parameters | ||
---------- | ||
a : ndarray(ndim=2) | ||
2-dimensional ndarray of shape (n, n). | ||
|
||
b : ndarray(ndim=1 or 2) | ||
1-dimensional ndarray of shape (n,) or 2-dimensional ndarray of | ||
shape (n, nrhs). | ||
|
||
Returns | ||
------- | ||
r : scalar(int) | ||
r = 0 if successful. | ||
|
||
Notes | ||
----- | ||
From github.com/numba/numba/blob/master/numba/targets/linalg.py | ||
|
||
""" | ||
numba_xgesv = _LAPACK().numba_xgesv(a.dtype) | ||
kind = ord(_blas_kinds[a.dtype]) | ||
|
||
def _numba_linalg_solve_impl(a, b): # pragma: no cover | ||
n = a.shape[-1] | ||
if b.ndim == 1: | ||
nrhs = 1 | ||
else: # b.ndim == 2 | ||
nrhs = b.shape[-1] | ||
F_INT_nptype = np.int32 | ||
ipiv = np.empty(n, dtype=F_INT_nptype) | ||
|
||
r = numba_xgesv( | ||
kind, # kind | ||
n, # n | ||
nrhs, # nhrs | ||
a.ctypes, # a | ||
n, # lda | ||
ipiv.ctypes, # ipiv | ||
b.ctypes, # b | ||
n # ldb | ||
) | ||
return r | ||
|
||
return _numba_linalg_solve_impl |
This file contains 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,59 @@ | ||
""" | ||
Tests for Numba support utilities | ||
|
||
""" | ||
import numpy as np | ||
from numpy.testing import assert_array_equal | ||
from numba import jit | ||
from nose.tools import eq_, ok_ | ||
from quantecon.util.numba import _numba_linalg_solve | ||
|
||
|
||
@jit(nopython=True) | ||
def numba_linalg_solve_orig(a, b): | ||
return np.linalg.solve(a, b) | ||
|
||
|
||
class TestNumbaLinalgSolve: | ||
def setUp(self): | ||
self.dtypes = [np.float32, np.float64] | ||
self.a = np.array([[3, 2, 0], [1, -1, 0], [0, 5, 1]]) | ||
self.b_1dim = np.array([2, 4, -1]) | ||
self.b_2dim = np.array([[2, 3], [4, 1], [-1, 0]]) | ||
self.a_singular = np.array([[0, 1, 2], [3, 4, 5], [3, 5, 7]]) | ||
|
||
def test_b_1dim(self): | ||
for dtype in self.dtypes: | ||
a = np.asfortranarray(self.a, dtype=dtype) | ||
b = np.asfortranarray(self.b_1dim, dtype=dtype) | ||
sol_orig = numba_linalg_solve_orig(a, b) | ||
r = _numba_linalg_solve(a, b) | ||
eq_(r, 0) | ||
assert_array_equal(b, sol_orig) | ||
|
||
def test_b_2dim(self): | ||
for dtype in self.dtypes: | ||
a = np.asfortranarray(self.a, dtype=dtype) | ||
b = np.asfortranarray(self.b_2dim, dtype=dtype) | ||
sol_orig = numba_linalg_solve_orig(a, b) | ||
r = _numba_linalg_solve(a, b) | ||
eq_(r, 0) | ||
assert_array_equal(b, sol_orig) | ||
|
||
def test_singular_a(self): | ||
for b in [self.b_1dim, self.b_2dim]: | ||
for dtype in self.dtypes: | ||
a = np.asfortranarray(self.a_singular, dtype=dtype) | ||
b = np.asfortranarray(b, dtype=dtype) | ||
r = _numba_linalg_solve(a, b) | ||
ok_(r != 0) | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
import nose | ||
|
||
argv = sys.argv[:] | ||
argv.append('--verbose') | ||
argv.append('--nocapture') | ||
nose.main(argv=argv, defaultTest=__file__) |
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.
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.
@oyamad is it standard convention to return the solution in
b
and not explicitly return the solution and a status code from the function as a tuple?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.
I am not sure if I understand your question, but here is where this is used, where it is check whether matrix
A
is nonsingular (instead oftry
-except
which is not available in nopython mode), and in caseA
is singular I don't know what is in there inb
.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.
ah. I see. In the code example
b
isout
and it is modified in place (rather than returned). Thanks @oyamad