Skip to content

Commit d86900a

Browse files
yuvaltassacopybara-github
authored andcommitted
Speed up mju_cholUpdateSparse by not checking for varying sparsity pattern where it is guaranteed to not vary.
PiperOrigin-RevId: 720126576 Change-Id: I3a43bdd0259656fab462b1cd155cb422982e970d
1 parent 4ab274a commit d86900a

File tree

4 files changed

+8
-13
lines changed

4 files changed

+8
-13
lines changed

src/engine/engine_util_solve.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ void mju_cholSolveSparse(mjtNum* res, const mjtNum* mat, const mjtNum* vec, int
238238
// sparse reverse-order Cholesky rank-one update: L'*L +/- x*x'; return rank
239239
// x is sparse, change in sparsity pattern of mat is not allowed
240240
int mju_cholUpdateSparse(mjtNum* mat, mjtNum* x, int n, int flg_plus,
241-
const int* rownnz, const int* rowadr, int* colind, int x_nnz, int* x_ind,
241+
const int* rownnz, const int* rowadr, const int* colind,
242+
int x_nnz, int* x_ind,
242243
mjData* d) {
243244
mj_markStack(d);
244245
int* buf_ind = mjSTACKALLOC(d, n, int);
@@ -264,14 +265,8 @@ int mju_cholUpdateSparse(mjtNum* mat, mjtNum* x, int n, int flg_plus,
264265
mat[adr+nnz-1] = r;
265266

266267
// update row: mat(r,1:r-1) = (mat(r,1:r-1) + s*x(1:r-1)) / c
267-
int new_nnz = mju_combineSparse(mat + adr, x, 1 / c, (flg_plus ? s / c : -s / c),
268-
nnz-1, i, colind + adr, x_ind,
269-
sparse_buf, buf_ind);
270-
271-
// check for size change
272-
if (new_nnz != nnz-1) {
273-
mjERROR("varying sparsity pattern");
274-
}
268+
mju_combineSparseInc(mat + adr, x, n, 1 / c, (flg_plus ? s / c : -s / c),
269+
nnz-1, i, colind + adr, x_ind);
275270

276271
// update x: x(1:r-1) = c*x(1:r-1) - s*mat(r,1:r-1)
277272
int new_x_nnz = mju_combineSparse(x, mat+adr, c, -s, i, nnz-1, x_ind,

src/engine/engine_util_solve.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ void mju_cholSolveSparse(mjtNum* res, const mjtNum* mat, const mjtNum* vec, int
4545
// sparse reverse-order Cholesky rank-one update: L'*L +/i x*x'; return rank
4646
// x is sparse, change in sparsity pattern of mat is not allowed
4747
int mju_cholUpdateSparse(mjtNum* mat, mjtNum* x, int n, int flg_plus,
48-
const int* rownnz, const int* rowadr, int* colind, int x_nnz, int* x_ind,
49-
mjData* d);
48+
const int* rownnz, const int* rowadr, const int* colind,
49+
int x_nnz, int* x_ind, mjData* d);
5050

5151
// band-dense Cholesky decomposition
5252
// returns minimum value in the factorized diagonal, or 0 if rank-deficient

src/engine/engine_util_sparse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ int mju_combineSparse(mjtNum* dst, const mjtNum* src, mjtNum a, mjtNum b,
298298

299299
// incomplete combine sparse: dst = a*dst + b*src at common indices
300300
void mju_combineSparseInc(mjtNum* dst, const mjtNum* src, int n, mjtNum a, mjtNum b,
301-
int dst_nnz, int src_nnz, int* dst_ind, const int* src_ind) {
301+
int dst_nnz, int src_nnz, const int* dst_ind, const int* src_ind) {
302302
// check for identical pattern
303303
if (dst_nnz == src_nnz) {
304304
if (mju_compare(dst_ind, src_ind, dst_nnz)) {

src/engine/engine_util_sparse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int mju_combineSparse(mjtNum* dst, const mjtNum* src, mjtNum a, mjtNum b,
6464

6565
// incomplete combine sparse: dst = a*dst + b*src at common indices
6666
void mju_combineSparseInc(mjtNum* dst, const mjtNum* src, int n, mjtNum a, mjtNum b,
67-
int dst_nnz, int src_nnz, int* dst_ind, const int* src_ind);
67+
int dst_nnz, int src_nnz, const int* dst_ind, const int* src_ind);
6868

6969
// dst += scl * src, only at common non-zero indices
7070
void mju_addToSclSparseInc(mjtNum* dst, const mjtNum* src,

0 commit comments

Comments
 (0)