-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuildAchol.c
189 lines (149 loc) · 5.48 KB
/
buildAchol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*=================================================================
% function Achol = buildAchol(Chat, U, sqlambda, I, J, n)
%
% Specific function to reduce the computation time in a bottleneck
% portion of the Matlab code for our low-rank matrix completion
% software RTRMC.
%
% Chat is a real, double >= 0 col vector, U is an orthonormal (real,double)
% m-by-r matrix, sqlambda is a positive real number, I and J are uint32
% vectors of the same size as Chat; together, Chat, I and J define an
% m-by-n sparse matrix; n must be a uint32.
%
% I and J are assumed to be sorted just like the output of the FIND
% function in Matlab.
%
% Achol is a cell containing n upper triangular r-by-r matrices: the
% Cholesky factors of the diagonal blocks of A. See the notes
% accompanying the software for details.
%
% Compile with: mex -lmwlapack -lmwblas -largeArrayDims buildAchol.c
%
% May 19, 2011 Nicolas Boumal, UCLouvain
*=================================================================*/
#include "mex.h"
#include "math.h"
#include "matrix.h"
#include "lapack.h"
#include "blas.h"
/* Input Arguments */
#define pChat prhs[0]
#define pU prhs[1]
#define psqlambda prhs[2]
#define pI prhs[3]
#define pJ prhs[4]
#define pn prhs[5]
/* Output Arguments */
#define pAchol plhs[0]
/* Helper function */
/* mwSize max(mwSize a, mwSize b) { return a > b ? a : b; } */
#define max(a, b) ((a)>(b)?(a):(b))
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray* prhs[] )
{
/* Counters and sizes*/
mwSize m, n, r, known, start, end;
mwIndex i, j, k;
/* Data arrays */
double *Chat, *U;
uint32_T *I, *J, *nn;
double sqlambda;
/* Temporary computation variables */
mxArray *pscaledUi, *pAi;
double *scaledUi, *Ai;
mwSize nb;
double coeff;
/* BLAS/LAPACK stuff*/
ptrdiff_t N, M, R, NB, info = 0;
double one = 1.0;
double zero = 0.0;
char *uplo = "U";
char *TRANS = "T";
char *NOTRANS = "N";
if(nrhs != 6 || nlhs != 1)
mexErrMsgTxt("Invalid number of input/output parameter. Need 6 inputs and 1 output.");
r = mxGetN(pU);
m = mxGetM(pU);
nn = (uint32_T*) mxGetData(pn);
n = (mwSize) (nn[0]);
known = max(mxGetM(pI), mxGetN(pI));
if(max(mxGetM(pJ), mxGetN(pJ)) != known || max(mxGetM(pChat), mxGetN(pChat)) != known)
mexErrMsgTxt("I, J and Chat must be vectors of the same length.");
sqlambda = mxGetScalar(psqlambda);
Chat = mxGetPr(pChat);
U = mxGetPr(pU);
I = (uint32_T*) mxGetData(pI);
J = (uint32_T*) mxGetData(pJ);
N = (ptrdiff_t) n;
M = (ptrdiff_t) m;
R = (ptrdiff_t) r;
/* dummy matrix, for memory allocation */
pscaledUi = mxCreateDoubleMatrix(m, r, mxREAL);
scaledUi = mxGetPr(pscaledUi);
/* Create a suitable cell */
pAchol = mxCreateCellMatrix(n, 1);
for(i = 0; i < n; ++i)
mxSetCell(pAchol, i, NULL);
start = 0;
end = 0;
for(i = 0; i < n; ++i)
{
/* allocate space for an r-by-r matrix in each cell entry */
mxSetCell(pAchol, i, mxCreateDoubleMatrix(r, r, mxREAL));
pAi = mxGetCell(pAchol, i);
Ai = mxGetPr(pAi);
/* Compute the entries of the i-th diagonal block of A
nb is the number of nonzero entries in the i-th column of Chat
seen as a sparse matrix of size m-by-n with nonzero entries I, J. */
/* TODO TODO TODO
It would be much better to have an additionnal guard element at
the end of the J vector ... :(
Actually, it would be even better to compute the Jc vector
like the one you get in the Matlab sparse representation.
And even better would be to precompute it just once. */
while(end < known && J[end] == i+1) {
++end;
}
nb = end-start;
if(nb > 0)
{
/*///////////////////////////////////////////////////////////////////////*/
for(j = 0; j < nb; ++j)
{
coeff = sqrt(Chat[start+j]);
for(k = 0; k < r; ++k)
{
scaledUi[j+k*m] = coeff * U[I[start+j]-1+k*m];
}
}
/*///////////////////////////////////////////////////////////////////////*/
/* compute the matrix product of the interesting part of
scaledUi with itself (transposed): Ai = scaledUi.'*scaledUi */
NB = (ptrdiff_t) nb;
dgemm(TRANS, NOTRANS, &R, &R, &NB, &one,
scaledUi, &M, scaledUi, &M, &zero, Ai, &R);
}
else
{
for(j = 0; j < r*r; ++j)
Ai[j] = 0.0;
}
start = end;
/* add sqlambda*eye(r) to Ai */
for(j = 0; j < r; ++j)
Ai[j*(r+1)] += sqlambda;
/* remplace Ai by its Cholesky factor by calling Lapack's dpotrf. */
dpotrf(uplo, &R, Ai, &R, &info);
/* write zeroes on the lower triangular parts of Ai (optional) */
for(j = 1; j < r; ++j)
for(k = 0; k < j; ++k)
Ai[j+r*k] = 0.0;
if(info < 0)
mexErrMsgTxt("dpotrf (in buildAchol): invalid argument.");
if(info > 0)
mexErrMsgTxt("dpotrf (in buildAchol): a leading minor is not positive definite.");
}
mxDestroyArray(pscaledUi);
return;
}