Skip to content

Commit 7ac3140

Browse files
authored
Merge pull request #188 from aragilar/lapack_fix
Fix BLAS/LAPACK builds (based on #160)
2 parents 82efaee + 2db0ccf commit 7ac3140

File tree

5 files changed

+282
-163
lines changed

5 files changed

+282
-163
lines changed

packages/scikits-odes-sundials/setup_build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def get_sundials_config_pxi(include_dirs, dist):
128128
# Check for blas/lapack
129129
if check_macro_def(
130130
config_cmd,
131-
"SUNDIALS_BLAS_LAPACK", headers=[SUNDIALS_CONFIG_H],
131+
"SUNDIALS_BLAS_LAPACK_ENABLED", headers=[SUNDIALS_CONFIG_H],
132132
include_dirs=include_dirs
133133
):
134134
has_lapack = True

packages/scikits-odes-sundials/src/scikits_odes_sundials/c_sunlinsol.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ IF SUNDIALS_BLAS_LAPACK:
6363
ctypedef _SUNLinearSolverContent_LapackDense *SUNLinearSolverContent_LapackDense
6464

6565
SUNLinearSolver SUNLinSol_LapackDense(N_Vector y, SUNMatrix A, SUNContext sunctx)
66-
SUNLinearSolver SUNLapackDense(N_Vector y, SUNMatrix A) #deprecated
6766

6867
SUNLinearSolver_Type SUNLinSolGetType_LapackDense(SUNLinearSolver S)
6968
SUNLinearSolver_ID SUNLinSolGetID_LapackDense(SUNLinearSolver S)
@@ -87,7 +86,6 @@ IF SUNDIALS_BLAS_LAPACK:
8786
ctypedef _SUNLinearSolverContent_LapackBand *SUNLinearSolverContent_LapackBand
8887

8988
SUNLinearSolver SUNLinSol_LapackBand(N_Vector y, SUNMatrix A, SUNContext sunctx)
90-
SUNLinearSolver SUNLapackBand(N_Vector y, SUNMatrix A) # deprecated
9189

9290
SUNLinearSolver_Type SUNLinSolGetType_LapackBand(SUNLinearSolver S)
9391
SUNLinearSolver_ID SUNLinSolGetID_LapackBand(SUNLinearSolver S)

packages/scikits-odes-sundials/src/scikits_odes_sundials/cvode.pyx

Lines changed: 162 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,31 @@ from .c_sunlinsol cimport (
2727
SUNLinSol_Dense, SUNLinSol_Band, SUNLinSol_SPGMR, SUNLinSol_SPBCGS,
2828
SUNLinSol_SPTFQMR,
2929
)
30+
if SUNDIALS_BLAS_LAPACK:
31+
from .c_sunlinsol cimport (
32+
SUNLinSol_LapackBand, SUNLinSol_LapackDense,
33+
)
3034
from .c_sunnonlinsol cimport SUNNonlinSol_FixedPoint
3135

3236
from .c_cvode cimport (
33-
CV_SUCCESS, CV_TSTOP_RETURN, CV_ROOT_RETURN, CV_WARNING, CV_TOO_MUCH_WORK,
34-
CV_TOO_MUCH_ACC, CV_ERR_FAILURE, CV_CONV_FAILURE, CV_LINIT_FAIL,
35-
CV_LSETUP_FAIL, CV_LSOLVE_FAIL, CV_RHSFUNC_FAIL, CV_FIRST_RHSFUNC_ERR,
36-
CV_REPTD_RHSFUNC_ERR, CV_UNREC_RHSFUNC_ERR, CV_RTFUNC_FAIL,
37-
CV_NLS_INIT_FAIL, CV_NLS_SETUP_FAIL, CV_CONSTR_FAIL, CV_NLS_FAIL,
38-
CV_MEM_FAIL, CV_MEM_NULL, CV_ILL_INPUT, CV_NO_MALLOC, CV_BAD_K, CV_BAD_T,
39-
CV_BAD_DKY, CV_TOO_CLOSE, CV_VECTOROP_ERR, CV_UNRECOGNIZED_ERR,
40-
CVodeRootInit, CVodeSStolerances, CVodeSVtolerances, CVodeSetStopTime,
41-
CV_BDF, CV_ADAMS, CVodeFree, CVodeCreate, CVodeInit, CVodeReInit,
42-
CVodeSetUserData, CVodeSetMaxOrd, CVodeSetMaxNumSteps, CVodeSetStabLimDet,
43-
CVodeSetInitStep, CVodeSetMinStep, CVodeSetMaxStep, CVodeSetMaxNonlinIters,
44-
CVodeSetMaxConvFails, CVodeSetNonlinConvCoef, CVodeSetLinearSolver,
45-
CVLS_ILL_INPUT, CVLS_MEM_FAIL, CVLS_SUCCESS, CVDiag, CVDIAG_ILL_INPUT,
46-
CVDIAG_MEM_FAIL, CVDIAG_SUCCESS, CVodeSetPreconditioner,
47-
CVodeGetNumRhsEvals, CVodeGetNumLinIters, CVodeGetNumJtimesEvals,
48-
CVodeGetNumPrecSolves, CVodeGetNumPrecEvals, CVodeGetIntegratorStats,
49-
CVodeSetJacFn, CVodeSetNonlinearSolver, CVodeSetJacTimes, CVLS_LMEM_NULL,
50-
CVLS_MEM_NULL, CVode, CV_NORMAL, CV_ONE_STEP,
37+
CV_ADAMS, CV_BAD_DKY, CV_BAD_K, CV_BAD_T, CV_BDF, CV_CONSTR_FAIL,
38+
CV_CONV_FAILURE, CV_ERR_FAILURE, CV_FIRST_RHSFUNC_ERR, CV_ILL_INPUT,
39+
CV_LINIT_FAIL, CV_LSETUP_FAIL, CV_LSOLVE_FAIL, CV_MEM_FAIL, CV_MEM_NULL,
40+
CV_NLS_FAIL, CV_NLS_INIT_FAIL, CV_NLS_SETUP_FAIL, CV_NORMAL, CV_NO_MALLOC,
41+
CV_ONE_STEP, CV_REPTD_RHSFUNC_ERR, CV_RHSFUNC_FAIL, CV_ROOT_RETURN,
42+
CV_RTFUNC_FAIL, CV_SUCCESS, CV_TOO_CLOSE, CV_TOO_MUCH_ACC, CV_TOO_MUCH_WORK,
43+
CV_TSTOP_RETURN, CV_UNRECOGNIZED_ERR, CV_UNREC_RHSFUNC_ERR, CV_VECTOROP_ERR,
44+
CV_WARNING, CVDIAG_ILL_INPUT, CVDIAG_MEM_FAIL, CVDIAG_SUCCESS, CVDiag,
45+
CVLS_ILL_INPUT, CVLS_LMEM_NULL, CVLS_MEM_FAIL, CVLS_MEM_NULL, CVLS_SUCCESS,
46+
CVode, CVodeCreate, CVodeFree, CVodeGetIntegratorStats,
47+
CVodeGetNumJtimesEvals, CVodeGetNumLinIters, CVodeGetNumPrecEvals,
48+
CVodeGetNumPrecSolves, CVodeGetNumRhsEvals, CVodeInit, CVodeReInit,
49+
CVodeRootInit, CVodeSStolerances, CVodeSVtolerances, CVodeSetInitStep,
50+
CVodeSetJacFn, CVodeSetJacTimes, CVodeSetLinearSolver, CVodeSetMaxConvFails,
51+
CVodeSetMaxNonlinIters, CVodeSetMaxNumSteps, CVodeSetMaxOrd,
52+
CVodeSetMaxStep, CVodeSetMinStep, CVodeSetNonlinConvCoef,
53+
CVodeSetNonlinearSolver, CVodeSetPreconditioner, CVodeSetStabLimDet,
54+
CVodeSetStopTime, CVodeSetUserData,
5155
)
5256

5357

@@ -1448,44 +1452,62 @@ cdef class CVODE(BaseSundialsSolver):
14481452
LS = SUNLinSol_Dense(self.y0, A, self.sunctx)
14491453
# check if memory was allocated
14501454
if (A == NULL or LS == NULL):
1451-
raise ValueError('Could not allocate matrix or linear solver')
1455+
raise ValueError(
1456+
'Could not allocate matrix or linear solver'
1457+
)
14521458
# attach matrix and linear solver to cvode
14531459
flag = CVodeSetLinearSolver(cv_mem, LS, A)
14541460
if flag == CVLS_ILL_INPUT:
1455-
raise ValueError('CVDense linear solver setting failed, '
1456-
'arguments incompatible')
1461+
raise ValueError(
1462+
'CVDense linear solver setting failed, '
1463+
'arguments incompatible'
1464+
)
14571465
elif flag == CVLS_MEM_FAIL:
1458-
raise MemoryError('CVDense linear solver memory allocation error.')
1466+
raise MemoryError(
1467+
'CVDense linear solver memory allocation error.'
1468+
)
14591469
elif flag != CVLS_SUCCESS:
1460-
raise ValueError('CVodeSetLinearSolver failed with code {}'
1461-
.format(flag))
1470+
raise ValueError(
1471+
f'CVodeSetLinearSolver failed with code {flag}'
1472+
)
14621473
elif linsolver == 'band':
1463-
A = SUNBandMatrix(N, <int> opts['uband'], <int> opts['lband'], self.sunctx);
1464-
LS = SUNLinSol_Band(self.y0, A, self.sunctx);
1474+
A = SUNBandMatrix(
1475+
N, <int> opts['uband'], <int> opts['lband'], self.sunctx
1476+
)
1477+
LS = SUNLinSol_Band(self.y0, A, self.sunctx)
14651478
if (A == NULL or LS == NULL):
1466-
raise ValueError('Could not allocate matrix or linear solver')
1479+
raise ValueError(
1480+
'Could not allocate matrix or linear solver'
1481+
)
14671482
flag = CVodeSetLinearSolver(cv_mem, LS, A)
1468-
1483+
14691484
if flag == CVLS_ILL_INPUT:
1470-
raise ValueError('CVBand linear solver setting failed, '
1471-
'arguments incompatible')
1485+
raise ValueError(
1486+
'CVBand linear solver setting failed, '
1487+
'arguments incompatible'
1488+
)
14721489
elif flag == CVLS_MEM_FAIL:
1473-
raise MemoryError('CVBand linear solver memory allocation error.')
1490+
raise MemoryError(
1491+
'CVBand linear solver memory allocation error.'
1492+
)
14741493
elif flag != CVLS_SUCCESS:
1475-
raise ValueError('CVodeSetLinearSolver failed with code {}'
1476-
.format(flag))
1494+
raise ValueError(
1495+
f'CVodeSetLinearSolver failed with code {flag}'
1496+
)
14771497
elif linsolver == 'diag':
14781498
flag = CVDiag(cv_mem)
14791499
if flag == CVDIAG_ILL_INPUT:
1480-
raise ValueError('CVDiag solver is not compatible with'
1481-
' the current nvector implementation.')
1500+
raise ValueError(
1501+
'CVDiag solver is not compatible with '
1502+
'the current nvector implementation.'
1503+
)
14821504
elif flag == CVDIAG_MEM_FAIL:
1483-
raise MemoryError('CVDiag memory allocation error.')
1505+
raise MemoryError(
1506+
'CVDiag memory allocation error.'
1507+
)
14841508
elif flag != CVDIAG_SUCCESS:
1485-
raise ValueError('CVDiag failed with code {}'
1486-
.format(flag))
1487-
elif ((linsolver == 'spgmr') or (linsolver == 'spbcgs')
1488-
or (linsolver == 'sptfqmr')):
1509+
raise ValueError(f'CVDiag failed with code {flag}')
1510+
elif linsolver in ('spgmr', 'spbcgs', 'sptfqmr'):
14891511
precond_type = opts['precond_type'].lower()
14901512
if precond_type == 'none':
14911513
pretype = SUN_PREC_NONE
@@ -1496,105 +1518,151 @@ cdef class CVODE(BaseSundialsSolver):
14961518
elif precond_type == 'both':
14971519
pretype = SUN_PREC_BOTH
14981520
else:
1499-
raise ValueError('LinSolver::Precondition: Unknown type: %s'
1500-
% opts['precond_type'])
1501-
1521+
raise ValueError(
1522+
'LinSolver::Precondition: Unknown type: '
1523+
f'{opts["precond_type"]}'
1524+
)
1525+
15021526
if linsolver == 'spgmr':
1503-
LS = SUNLinSol_SPGMR(self.y0, pretype, <int> opts['maxl'], self.sunctx);
1527+
LS = SUNLinSol_SPGMR(
1528+
self.y0, pretype, <int> opts['maxl'], self.sunctx
1529+
)
15041530
if LS == NULL:
15051531
raise ValueError('Could not allocate linear solver')
15061532
elif linsolver == 'spbcgs':
1507-
LS = SUNLinSol_SPBCGS(self.y0, pretype, <int> opts['maxl'], self.sunctx);
1533+
LS = SUNLinSol_SPBCGS(
1534+
self.y0, pretype, <int> opts['maxl'], self.sunctx
1535+
)
15081536
if LS == NULL:
15091537
raise ValueError('Could not allocate linear solver')
15101538
elif linsolver == 'sptfqmr':
1511-
LS = SUNLinSol_SPTFQMR(self.y0, pretype, <int> opts['maxl'], self.sunctx);
1539+
LS = SUNLinSol_SPTFQMR(
1540+
self.y0, pretype, <int> opts['maxl'], self.sunctx
1541+
)
15121542
if LS == NULL:
15131543
raise ValueError('Could not allocate linear solver')
15141544
else:
1515-
raise ValueError('Given linsolver {} not implemented in odes'.format(linsolver))
1516-
1545+
raise NotImplementedError(
1546+
f'Given linsolver {linsolver} not implemented in odes'
1547+
)
1548+
15171549
flag = CVodeSetLinearSolver(cv_mem, LS, NULL);
15181550
if flag == CVLS_MEM_FAIL:
1519-
raise MemoryError('LinSolver:CVode memory allocation '
1520-
'error.')
1551+
raise MemoryError(
1552+
'LinSolver:CVode memory allocation error.'
1553+
)
15211554
elif flag != CVLS_SUCCESS:
1522-
raise ValueError('CVodeSetLinearSolver failed with code {}'
1523-
.format(flag))
1555+
raise ValueError(
1556+
f'CVodeSetLinearSolver failed with code {flag}'
1557+
)
15241558
# TODO: make option for the Gram-Schmidt orthogonalization
15251559
#flag = SUNSPGMRSetGSType(LS, gstype);
1526-
1560+
15271561
# TODO make option
15281562
#flag = CVodeSetEpsLin(cvode_mem, DELT);
15291563
if self.aux_data.prec_solvefn:
15301564
if self.aux_data.prec_setupfn:
1531-
flag = CVodeSetPreconditioner(cv_mem, _prec_setupfn, _prec_solvefn)
1565+
flag = CVodeSetPreconditioner(
1566+
cv_mem, _prec_setupfn, _prec_solvefn
1567+
)
15321568
else:
1533-
flag = CVodeSetPreconditioner(cv_mem, NULL, _prec_solvefn)
1569+
flag = CVodeSetPreconditioner(
1570+
cv_mem, NULL, _prec_solvefn
1571+
)
15341572
if flag == CVLS_MEM_NULL:
1535-
raise ValueError('LinSolver: The cvode mem pointer is NULL.')
1573+
raise ValueError(
1574+
'LinSolver: The cvode mem pointer is NULL.'
1575+
)
15361576
elif flag == CVLS_LMEM_NULL:
1537-
raise ValueError('LinSolver: The cvspils linear solver has '
1538-
'not been initialized.')
1577+
raise ValueError(
1578+
'LinSolver: The cvspils linear solver has not been '
1579+
'initialized.'
1580+
)
15391581
elif flag != CVLS_SUCCESS:
1540-
raise ValueError('CVodeSetPreconditioner failed with code {}'
1541-
.format(flag))
1542-
1582+
raise ValueError(
1583+
'CVodeSetPreconditioner failed with code {flag}'
1584+
)
1585+
15431586
if self.aux_data.jac_times_vecfn:
15441587
if self.aux_data.jac_times_setupfn:
1545-
flag = CVodeSetJacTimes(cv_mem, _jac_times_setupfn,
1546-
_jac_times_vecfn)
1588+
flag = CVodeSetJacTimes(
1589+
cv_mem, _jac_times_setupfn, _jac_times_vecfn
1590+
)
15471591
else:
1548-
flag = CVodeSetJacTimes(cv_mem, NULL, _jac_times_vecfn)
1592+
flag = CVodeSetJacTimes(cv_mem, NULL, _jac_times_vecfn)
15491593
if flag == CVLS_MEM_NULL:
1550-
raise ValueError('LinSolver: The cvode mem pointer is NULL.')
1594+
raise ValueError(
1595+
'LinSolver: The cvode mem pointer is NULL.'
1596+
)
15511597
elif flag == CVLS_LMEM_NULL:
1552-
raise ValueError('LinSolver: The cvspils linear solver has '
1553-
'not been initialized.')
1598+
raise ValueError(
1599+
'LinSolver: The cvspils linear solver has not been '
1600+
'initialized.'
1601+
)
15541602
elif flag != CVLS_SUCCESS:
1555-
raise ValueError('CVodeSetJacTimes failed with code {}'
1556-
.format(flag))
1557-
else:
1603+
raise ValueError(
1604+
f'CVodeSetJacTimes failed with code {flag}'
1605+
)
1606+
elif linsolver in ['lapackdense', 'lapackband']:
15581607
if SUNDIALS_BLAS_LAPACK:
15591608
if linsolver == 'lapackdense':
15601609
A = SUNDenseMatrix(N, N, self.sunctx)
1561-
LS = SUNLapackDense(self.y0, A)
1610+
LS = SUNLinSol_LapackDense(self.y0, A, self.sunctx)
15621611
# check if memory was allocated
15631612
if (A == NULL or LS == NULL):
1564-
raise ValueError('Could not allocate matrix or linear solver')
1613+
raise ValueError(
1614+
'Could not allocate matrix or linear solver'
1615+
)
15651616
# attach matrix and linear solver to cvode
15661617
flag = CVodeSetLinearSolver(cv_mem, LS, A)
15671618
if flag == CVLS_ILL_INPUT:
1568-
raise ValueError('CVDense lapack linear solver setting failed, '
1569-
'arguments incompatible')
1619+
raise ValueError(
1620+
'CVDense lapack linear solver setting failed, '
1621+
'arguments incompatible'
1622+
)
15701623
elif flag == CVLS_MEM_FAIL:
1571-
raise MemoryError('CVDense lapack linear solver memory allocation error.')
1624+
raise MemoryError(
1625+
'CVDense lapack linear solver memory '
1626+
'allocation error.'
1627+
)
15721628
elif flag != CVLS_SUCCESS:
1573-
raise ValueError('CVodeSetLinearSolver failed with code {}'
1574-
.format(flag))
1629+
raise ValueError(
1630+
f'CVodeSetLinearSolver failed with code {flag}'
1631+
)
15751632
elif linsolver == 'lapackband':
1576-
A = SUNBandMatrix(N, <int> opts['uband'], <int> opts['lband'], self.sunctx)
1577-
LS = SUNLapackBand(self.y0, A)
1633+
A = SUNBandMatrix(
1634+
N, <int> opts['uband'], <int> opts['lband'],
1635+
self.sunctx
1636+
)
1637+
LS = SUNLinSol_LapackBand(self.y0, A, self.sunctx)
15781638
if (A == NULL or LS == NULL):
1579-
raise ValueError('Could not allocate matrix or linear solver')
1639+
raise ValueError(
1640+
'Could not allocate matrix or linear solver'
1641+
)
15801642
flag = CVodeSetLinearSolver(cv_mem, LS, A)
15811643
if flag == CVLS_ILL_INPUT:
1582-
raise ValueError('CVLapackBand linear solver setting failed, '
1583-
'arguments incompatible')
1644+
raise ValueError(
1645+
'CVLapackBand linear solver setting failed, '
1646+
'arguments incompatible'
1647+
)
15841648
elif flag == CVLS_MEM_FAIL:
1585-
raise MemoryError('CVLapackBand linear solver memory allocation error.')
1649+
raise MemoryError(
1650+
'CVLapackBand linear solver memory '
1651+
'allocation error.'
1652+
)
15861653
elif flag != CVLS_SUCCESS:
1587-
raise ValueError('CVodeSetLinearSolver failed with code {}'
1588-
.format(flag))
1589-
else:
1590-
raise ValueError('LinSolver: Unknown solver type: %s'
1591-
% opts['linsolver'])
1592-
elif linsolver in ['lapackdense', 'lapackband']:
1593-
raise ValueError('LinSolver: LAPACK not available, cannot execute solver type: %s'
1594-
% opts['linsolver'])
1654+
raise ValueError(
1655+
f'CVodeSetLinearSolver failed with code {flag}'
1656+
)
15951657
else:
1596-
raise ValueError('LinSolver: Unknown solver type: %s'
1597-
% opts['linsolver'])
1658+
raise ValueError(
1659+
'LinSolver: LAPACK not available, cannot execute '
1660+
f'solver type: {linsolver}'
1661+
)
1662+
else:
1663+
raise ValueError(
1664+
'LinSolver: Unknown solver type {linsolver}'
1665+
)
15981666
elif nonlinsolver == 'fixedpoint':
15991667
# create fixed point nonlinear solver object
16001668
NLS = SUNNonlinSol_FixedPoint(self.y0, 0, self.sunctx);
@@ -1603,7 +1671,7 @@ cdef class CVODE(BaseSundialsSolver):
16031671
if flag != CV_SUCCESS:
16041672
raise ValueError('CVodeSetNonlinearSolver failed with code {}'
16051673
.format(flag))
1606-
1674+
16071675
if (linsolver in ['dense', 'lapackdense', 'lapackband', 'band']
16081676
and self.aux_data.jac):
16091677
# we need to create the correct shape for jacobian output, here is

0 commit comments

Comments
 (0)