Skip to content

Commit 7d16b70

Browse files
authored
Merge pull request #30 from ali5h/lsolve-perf
reduce memory access in linear solve loop
2 parents 69e845b + ace8c5c commit 7d16b70

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/qdldl.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,12 @@ void QDLDL_Lsolve(const QDLDL_int n,
239239
const QDLDL_float* Lx,
240240
QDLDL_float* x){
241241

242-
QDLDL_int i,j;
242+
QDLDL_int i,j;
243243
for(i = 0; i < n; i++){
244-
for(j = Lp[i]; j < Lp[i+1]; j++){
245-
x[Li[j]] -= Lx[j]*x[i];
246-
}
244+
QDLDL_float val = x[i];
245+
for(j = Lp[i]; j < Lp[i+1]; j++){
246+
x[Li[j]] -= Lx[j]*val;
247+
}
247248
}
248249
}
249250

@@ -254,11 +255,13 @@ void QDLDL_Ltsolve(const QDLDL_int n,
254255
const QDLDL_float* Lx,
255256
QDLDL_float* x){
256257

257-
QDLDL_int i,j;
258+
QDLDL_int i,j;
258259
for(i = n-1; i>=0; i--){
259-
for(j = Lp[i]; j < Lp[i+1]; j++){
260-
x[i] -= Lx[j]*x[Li[j]];
261-
}
260+
QDLDL_float val = x[i];
261+
for(j = Lp[i]; j < Lp[i+1]; j++){
262+
val -= Lx[j]*x[Li[j]];
263+
}
264+
x[i] = val;
262265
}
263266
}
264267

@@ -270,10 +273,9 @@ void QDLDL_solve(const QDLDL_int n,
270273
const QDLDL_float* Dinv,
271274
QDLDL_float* x){
272275

273-
QDLDL_int i;
274-
275-
QDLDL_Lsolve(n,Lp,Li,Lx,x);
276-
for(i = 0; i < n; i++) x[i] *= Dinv[i];
277-
QDLDL_Ltsolve(n,Lp,Li,Lx,x);
276+
QDLDL_int i;
278277

278+
QDLDL_Lsolve(n,Lp,Li,Lx,x);
279+
for(i = 0; i < n; i++) x[i] *= Dinv[i];
280+
QDLDL_Ltsolve(n,Lp,Li,Lx,x);
279281
}

0 commit comments

Comments
 (0)