SCM

SCM Repository

[matrix] Annotation of /pkg/src/dgBCMatrix.c
ViewVC logotype

Annotation of /pkg/src/dgBCMatrix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 485 - (view) (download) (as text)

1 : bates 485 #include "dgBCMatrix.h"
2 : bates 415 /* TODO
3 :     * - code for trans = 'T' in cscb_syrk
4 :     * - code for non-trivial cscb_trmm and cscb_ldl
5 :     */
6 : bates 447
7 : bates 485 SEXP dgBCMatrix_validate(SEXP x)
8 : bates 298 {
9 :     SEXP pp = GET_SLOT(x, Matrix_pSym),
10 :     ip = GET_SLOT(x, Matrix_iSym),
11 :     xp = GET_SLOT(x, Matrix_xSym),
12 :     dp = getAttrib(xp, R_DimSymbol);
13 :     int *pv = INTEGER(pp),
14 :     *dim = INTEGER(dp),
15 :     ncol = length(pp) - 1;
16 :     int nnz = pv[ncol];
17 :    
18 : bates 304 if (!(isReal(xp) && isArray(xp)))
19 : bates 415 return mkString("slot x should be a real array");
20 : bates 298 if (length(dp) != 3)
21 : bates 415 return mkString("slot x should be a 3-dimensional array");
22 : bates 298 if (length(ip) != nnz)
23 : bates 415 return mkString("length of slot i does not matck last element of slot p");
24 : bates 298 if (dim[2] != nnz)
25 : bates 415 return
26 :     mkString("third dimension of slot x does not match number of nonzeros");
27 : bates 298 return ScalarLogical(1);
28 :     }
29 :    
30 : bates 434 static int*
31 :     expand_column_pointers(int ncol, const int mp[], int mj[])
32 :     {
33 :     int j;
34 :     for (j = 0; j < ncol; j++) {
35 :     int j2 = mp[j+1], jj;
36 :     for (jj = mp[j]; jj < j2; jj++) mj[jj] = j;
37 :     }
38 :     return mj;
39 :     }
40 :    
41 :     static R_INLINE
42 :     int Tind(const int rowind[], const int colptr[], int i, int j)
43 :     {
44 :     int k, k2 = colptr[j + 1];
45 :     for (k = colptr[j]; k < k2; k++)
46 :     if (rowind[k] == i) return k;
47 :     error("row %d and column %d not defined in rowind and colptr",
48 :     i, j);
49 :     return -1; /* -Wall */
50 :     }
51 :    
52 : bates 300 /**
53 :     * Perform one of the matrix operations
54 :     * C := alpha*op(A)*B + beta*C
55 :     * or
56 :     * C := alpha*B*op(A) + beta*C
57 :     * where A is a compressed, sparse, blocked matrix and
58 :     * B and C are dense matrices.
59 :     *
60 :     * @param side 'L' or 'l' for left, otherwise right
61 :     * @param transa 'T' or 't' for transpose.
62 :     * @param m number of rows in C
63 :     * @param n number of columns in C
64 : bates 415 * @param k number of rows in B if side = 'L', otherwise
65 :     * number of columns in B.
66 : bates 300 * @param alpha
67 : bates 485 * @param A pointer to a dgBCMatrix object
68 : bates 415 * @param B matrix to be multiplied
69 :     * @param ldb leading dimension of b as declared in the calling
70 :     * routine
71 :     * @param beta scalar multiplier of c
72 :     * @param C product matrix to be modified
73 :     * @param ldc leading dimension of c as declared in the calling
74 :     * routine
75 :     */
76 :     void
77 : bates 447 cscb_mm(enum CBLAS_SIDE side, enum CBLAS_TRANSPOSE transa,
78 :     int m, int n, int k, double alpha, SEXP A,
79 :     const double B[], int ldb, double beta, double C[], int ldc)
80 : bates 415 {
81 :     SEXP AxP = GET_SLOT(A, Matrix_xSym),
82 :     ApP = GET_SLOT(A, Matrix_pSym);
83 :     int *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
84 :     *Ap = INTEGER(ApP),
85 :     *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
86 :     ancb = length(ApP) - 1, /* number of column blocks */
87 :     anrb; /* number of row blocks */
88 :     int absz = adims[0] * adims[1]; /* block size */
89 :     int j;
90 :     double *Ax = REAL(AxP);
91 :    
92 : bates 422 if (ldc < m) error("incompatible dims m=%d, ldc=%d", m, ldc);
93 : bates 448 if (side == LFT) {
94 : bates 415 /* B is of size k by n */
95 :     if (ldb < k)
96 :     error("incompatible L dims k=%d, ldb=%d, n=%d, nr=%d, nc=%d",
97 :     k, ldb, n, adims[0], adims[1]);
98 : bates 447 if (transa == TRN) {
99 : bates 415 if (m % adims[1] || k % adims[0])
100 :     error("incompatible LT dims m=%d, k = %d, nr=%d, nc=%d",
101 :     m, k, adims[0], adims[1]);
102 :     if (ancb != m/adims[1])
103 :     error("incompatible LT dims m=%d, ancb=%d, adims=[%d,%d,%d]",
104 : bates 447 m, ancb, adims[0], adims[1], adims[2]);
105 : bates 415 anrb = k/adims[0];
106 :     } else {
107 :     if (m % adims[0] || k % adims[1])
108 :     error("incompatible LN dims m=%d, k = %d, nr=%d, nc=%d",
109 :     m, k, adims[0], adims[1]);
110 :     if (ancb != k/adims[1])
111 :     error("incompatible LN dims k=%d, ancb=%d, adims=[%d,%d,%d]",
112 : bates 447 k, ancb, adims[0], adims[1], adims[2]);
113 : bates 415 anrb = m/adims[0];
114 :     }
115 :     for (j = 0; j < ancb; j++) {
116 :     int kk, j2 = Ap[j + 1];
117 :     for (kk = Ap[j]; kk < j2; kk++) {
118 :     int ii = Ai[kk];
119 :     if (ii < 0 || ii >= anrb)
120 :     error("improper row index ii=%d, anrb=%d", ii, anrb);
121 : bates 447 if (transa == TRN) {
122 : bates 415 F77_CALL(dgemm)("T", "N", adims+1, &n, adims,
123 :     &alpha, Ax + kk * absz, adims,
124 :     B + ii * adims[0], &ldb,
125 :     &beta, C + j * adims[1], &ldc);
126 :     } else {
127 :     F77_CALL(dgemm)("N", "N", adims, &n, adims+1,
128 :     &alpha, Ax + kk * absz, adims,
129 : bates 447 B + j * adims[1], &ldb,
130 : bates 422 &beta, C + ii * adims[0], &ldc);
131 : bates 415 }
132 :     }
133 :     }
134 :     } else {
135 :     /* B is of size m by k */
136 :     error("Call to cscb_mm must have side == 'L'");
137 :     }
138 :     }
139 :    
140 :     /**
141 : bates 339 * Perform one of the matrix operations
142 :     * C := alpha*A*A' + beta*C,
143 :     * or
144 :     * C := alpha*A'*A + beta*C,
145 :     * where A is a compressed, sparse, blocked matrix and
146 :     * C is a compressed, sparse, symmetric blocked matrix.
147 :     *
148 :     * @param uplo 'U' or 'u' for upper triangular storage, else lower.
149 :     * @param trans 'T' or 't' for transpose.
150 :     * @param alpha scalar multiplier of outer product
151 :     * @param A compressed sparse blocked matrix
152 :     * @param beta scalar multiplier of c
153 :     * @param C compressed sparse blocked symmetric matrix to be updated
154 :     */
155 : bates 447 void
156 :     cscb_syrk(enum CBLAS_UPLO uplo, enum CBLAS_TRANSPOSE trans,
157 :     double alpha, SEXP A,
158 :     double beta, SEXP C)
159 : bates 339 {
160 : bates 415 SEXP AxP = GET_SLOT(A, Matrix_xSym),
161 :     ApP = GET_SLOT(A, Matrix_pSym),
162 :     CxP = GET_SLOT(C, Matrix_xSym),
163 :     CpP = GET_SLOT(C, Matrix_pSym);
164 :     int *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
165 :     *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
166 :     *Ap = INTEGER(ApP),
167 :     *cdims = INTEGER(getAttrib(CxP, R_DimSymbol)),
168 :     *Ci = INTEGER(GET_SLOT(C, Matrix_iSym)),
169 :     *Cp = INTEGER(CpP),
170 : bates 422 j, k;
171 : bates 415 double *Ax = REAL(AxP), *Cx = REAL(CxP), one = 1.;
172 :     int scalar = (adims[0] == 1 && adims[1] == 1),
173 : bates 434 anc = length(ApP) - 1,
174 : bates 415 asz = adims[0] * adims[1],
175 :     csz = cdims[0] * cdims[1];
176 : bates 339
177 : bates 415
178 : bates 339 if (cdims[0] != cdims[1]) error("blocks in C must be square");
179 : bates 448 if (trans == TRN) {
180 : bates 451 /* FIXME: Write this part */
181 : bates 339 error("Code for trans == 'T' not yet written");
182 : bates 415 } else {
183 : bates 422 if (adims[0] != cdims[0])
184 :     error("Inconsistent dimensions: A[%d,%d,%d], C[%d,%d,%d]",
185 :     adims[0], adims[1], adims[2],
186 :     cdims[0], cdims[1], cdims[2]);
187 : bates 339 /* check the row indices */
188 : bates 415 for (k = 0; k < adims[2]; k++) {
189 :     int aik = Ai[k];
190 : bates 422 if (aik < 0 || aik >= cdims[2])
191 : bates 415 error("Row index %d = %d is out of range [0, %d]",
192 : bates 422 k, aik, cdims[2] - 1);
193 : bates 415 }
194 :     /* multiply C by beta */
195 :     if (beta != 1.)
196 :     for (j = 0; j < csz * cdims[2]; j++) Cx[j] *= beta;
197 :     /* individual products */
198 : bates 434 for (j = 0; j < anc; j++) {
199 : bates 415 int k, kk, k2 = Ap[j+1];
200 :     for (k = Ap[j]; k < k2; k++) {
201 : bates 448 int ii = Ai[k], K = Tind(Ci, Cp, ii, ii);
202 : bates 415
203 : bates 438 if (K < 0) error("cscb_syrk: C[%d,%d] not defined", ii, ii);
204 : bates 415 if (scalar) Cx[K] += alpha * Ax[k] * Ax[k];
205 : bates 447 else F77_CALL(dsyrk)((uplo == UPP)?"U":"L", "N", cdims, adims + 1,
206 : bates 415 &alpha, Ax + k * asz, adims,
207 :     &one, Cx + K * csz, cdims);
208 :     for (kk = k+1; kk < k2; kk++) {
209 :     int jj = Ai[kk];
210 : bates 448 K = (uplo == UPP) ? Tind(Ci, Cp, ii, jj) : Tind(Ci, Cp, jj, ii);
211 :    
212 : bates 415 if (scalar) Cx[K] += alpha * Ax[k] * Ax[kk];
213 :     else F77_CALL(dgemm)("N", "T", cdims, cdims + 1, adims + 1,
214 : bates 448 &alpha, Ax + ((uplo==UPP)?kk:k) * asz, adims,
215 :     Ax + ((uplo==UPP)?k:kk) * asz, adims,
216 : bates 415 &one, Cx + K * asz, cdims);
217 : bates 339 }
218 :     }
219 :     }
220 :     }
221 : bates 415 }
222 :    
223 :     /**
224 : bates 451 * Create the LD^{T/2}D^{1/2}L' decomposition of the positive definite
225 : bates 485 * symmetric dgBCMatrix matrix A (upper triangle stored) in L and
226 : bates 451 * D^{1/2}. The notation D^{1/2} denotes the upper Cholesky factor of
227 :     * the positive definite positive definite block diagonal matrix D.
228 :     * The diagonal blocks are of size nci.
229 : bates 415 *
230 : bates 485 * @param A pointer to a dgBCMatrix object containing the upper
231 : bates 415 * triangle of a positive definite symmetric matrix.
232 :     * @param Parent the parent array for A
233 : bates 485 * @param L pointer to a dgBCMatrix object to hold L
234 : bates 415 * @param D pointer to a 3D array to hold D
235 :     *
236 :     * @return n the number of column blocks in A for success. A value
237 :     * less than n indicates the first column block whose diagonal was not
238 :     * positive definite.
239 :     */
240 :     int
241 :     cscb_ldl(SEXP A, const int Parent[], SEXP L, SEXP D)
242 :     {
243 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
244 :     AxP = GET_SLOT(A, Matrix_xSym);
245 :     int *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
246 : bates 434 diag, info, j, k, n = length(ApP) - 1;
247 : bates 415 int *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
248 :     *Ap = INTEGER(ApP),
249 :     *Li = INTEGER(GET_SLOT(L, Matrix_iSym)),
250 : bates 434 *Lp = INTEGER(GET_SLOT(L, Matrix_pSym)), nci = adims[0];
251 : bates 415 double *Lx = REAL(GET_SLOT(L, Matrix_xSym)),
252 : bates 434 *Ax = REAL(AxP), *Dx = REAL(D), minus1 = -1., one = 1.;
253 : bates 415
254 : bates 434 if (adims[1] != nci || nci < 1)
255 :     error("cscb_ldl: dim(A) is [%d, %d, %d]", adims[0], adims[1], adims[2]);
256 : bates 415 for (j = 0, diag = 1; j < n; j++) { /* check for trivial structure */
257 :     if (Parent[j] >= 0) {diag = 0; break;}
258 :     }
259 :     if (diag) {
260 : bates 434 int ncisqr = nci * nci;
261 :     Memcpy(Dx, Ax, ncisqr * n);
262 :     for (j = 0; j < n; j++) { /* form D_i^{1/2} */
263 :     F77_CALL(dpotrf)("U", &nci, Dx + j * ncisqr, &nci, &k);
264 :     if (k) error("D[ , , %d], level %d, is not positive definite",
265 :     j + 1);
266 :     }
267 : bates 415 return n;
268 :     }
269 : bates 434 if (nci == 1) {
270 :     k = R_ldl_numeric(n, Ap, Ai, Ax, Lp, Parent, Li, Lx, Dx,
271 :     (int *) NULL, (int *) NULL);
272 :     if (k < n) error("cscb_ldl: error code %k from R_ldl_numeric", k);
273 :     for (j = 0; j < n; j++) Dx[j] = sqrt(Dx[j]);
274 :     return n;
275 :     } else { /* Copy of ldl_numeric from the LDL package
276 :     * modified for blocked sparse matrices */
277 :     int i, k, p, p2, len, nci = adims[0], ncisqr = adims[0]*adims[0], top;
278 :     int *Lnz = Calloc(n, int),
279 :     *Pattern = Calloc(n, int),
280 :     *Flag = Calloc(n, int);
281 :     double *Y = Calloc(n * ncisqr, double), *Yi = Calloc(ncisqr, double);
282 : bates 415
283 : bates 434 for (k = 0; k < n; k++) {
284 :     /* compute nonzero Pattern of kth row of L, in topological order */
285 :     AZERO(Y + k*ncisqr, ncisqr); /* Y[,,0:k] is now all zero */
286 :     top = n; /* stack for pattern is empty */
287 :     Flag[k] = k; /* mark node k as visited */
288 :     Lnz[k] = 0; /* count of nonzeros in column k of L */
289 :     p2 = Ap[k+1];
290 :     for (p = Ap[k]; p < p2; p++) {
291 :     i = Ai[p]; /* get A[i,k] */
292 :     if (i <= k) { /* [i,k] in upper triangle? Should always be true */
293 :     /* copy A(i,k) into Y */
294 :     Memcpy(Y + i * ncisqr, Ax + p * ncisqr, ncisqr);
295 :     /* follow path from i to root of etree, stop at flagged node */
296 :     for (len = 0; Flag[i] != k; i = Parent[i]) {
297 :     Pattern[len++] = i; /* L[k,i] is nonzero */
298 :     Flag[i] = k; /* mark i as visited */
299 :     }
300 :     while (len > 0) { /* push path on top of stack */
301 :     Pattern[--top] = Pattern[--len];
302 :     }
303 :     }
304 :     }
305 :     /* Pattern [top ... n-1] now contains nonzero pattern of L[,k] */
306 :     /* compute numerical values in kth row of L (a sparse triangular solve) */
307 :     Memcpy(Dx + k * ncisqr, Y + k * ncisqr, ncisqr); /* get D[,,k] */
308 :     AZERO(Y + k*ncisqr, ncisqr); /* clear Y[,,k] */
309 :     for (; top < n; top++) {
310 :     i = Pattern[top];
311 :     Memcpy(Yi, Y + i*ncisqr, ncisqr); /* copy Y[,,i] */
312 :     AZERO(Y + i*ncisqr, ncisqr); /* clear Y[,,i] */
313 :     p2 = Lp[i] + Lnz[i];
314 :     for (p = Lp[i]; p < p2; p++) {
315 :     F77_CALL(dgemm)("N", "N", &nci, &nci, &nci, &minus1,
316 :     Lx + p*ncisqr, &nci, Yi, &nci,
317 :     &one, Y + Li[p]*ncisqr, &nci);
318 :     }
319 :     /* FIXME: Check that this is the correct order and transposition */
320 : bates 451 F77_CALL(dtrsm)("R", "U", "N", "N", &nci, &nci,
321 : bates 434 &one, Dx + i*ncisqr, &nci, Yi, &nci);
322 :     F77_CALL(dsyrk)("U", "N", &nci, &nci, &minus1, Yi, &nci,
323 :     &one, Dx + k*ncisqr, &nci);
324 : bates 451 F77_CALL(dtrsm)("R", "U", "T", "N", &nci, &nci,
325 : bates 434 &one, Dx + i*ncisqr, &nci, Yi, &nci);
326 :     Li[p] = k; /* store L[k,i] in column form of L */
327 :     Memcpy(Lx + p * ncisqr, Yi, ncisqr);
328 :     Lnz[i]++; /* increment count of nonzeros in col i */
329 :     }
330 :     F77_CALL(dpotrf)("U", &nci, Dx + k*ncisqr, &nci, &info);
331 :     if (info) {
332 :     Free(Y); Free(Yi); Free(Pattern); Free(Flag); Free(Lnz);
333 :     return k; /* failure, D[,,k] is not positive definite */
334 :     }
335 :     }
336 :     Free(Y); Free(Yi); Free(Pattern); Free(Flag); Free(Lnz);
337 :     return n; /* success, diagonal of D is all nonzero */
338 : bates 415 }
339 :     return -1; /* keep -Wall happy */
340 :     }
341 :    
342 :     /**
343 : bates 485 * Perform one of the dgBCMatrix-matrix operations B := alpha*op(A)*B
344 : bates 415 * or B := alpha*B*op(A)
345 :     *
346 : bates 448 * @param side
347 :     * @param uplo
348 :     * @param transa
349 :     * @param diag
350 : bates 485 * @param A pointer to a triangular dgBCMatrix object
351 : bates 448 * @param B contents of the matrix B
352 : bates 415 * @param m number of rows in B
353 :     * @param n number of columns in B
354 :     * @param ldb leading dimension of B as declared in the calling function
355 :     */
356 : bates 447 void
357 :     cscb_trmm(enum CBLAS_SIDE side, enum CBLAS_UPLO uplo,
358 :     enum CBLAS_TRANSPOSE transa, enum CBLAS_DIAG diag,
359 :     double alpha, SEXP A, double B[], int m, int n, int ldb)
360 : bates 415 {
361 : bates 451 SEXP /* ApP = GET_SLOT(A, Matrix_pSym), */
362 : bates 415 AxP = GET_SLOT(A, Matrix_xSym);
363 : bates 451 int /* *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)), */
364 :     /* *Ap = INTEGER(ApP), */
365 : bates 415 *xdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
366 : bates 451 i, j/* , nb = length(ApP) - 1 */;
367 : bates 415
368 :     if (xdims[0] != xdims[1])
369 :     error("Argument A to cscb_trmm is not triangular");
370 :     if (alpha != 1.0) {
371 :     for (j = 0; j < n; j++) { /* scale by alpha */
372 :     for (i = 0; i < m; i++)
373 :     B[i + j * ldb] *= alpha;
374 :     }
375 :     }
376 : bates 448 if (diag == UNT && xdims[2] < 1) return; /* A is the identity */
377 : bates 415 error("Code for non-identity cases of cscb_trmm not yet written");
378 :     }
379 :    
380 :     /**
381 : bates 434 * Solve a triangular system of the form op(A)*X = alpha*B where A
382 : bates 485 * is a dgBCMatrix triangular matrix and B is a dense matrix.
383 : bates 434 *
384 :     * @param uplo 'U' or 'L' for upper or lower
385 :     * @param trans 'T' or 'N' for transpose or no transpose
386 :     * @param diag 'U' or 'N' for unit diagonal or non-unit
387 : bates 485 * @param A pointer to a triangular dgBCMatrix object
388 : bates 434 * @param B pointer to the contents of the matrix B
389 :     * @param m number of rows in B
390 :     * @param n number of columns in B
391 :     * @param ldb leading dimension of B as declared in the calling function
392 :     */
393 : bates 447 void
394 :     cscb_trsm(enum CBLAS_UPLO uplo, enum CBLAS_TRANSPOSE transa, enum CBLAS_DIAG diag,
395 :     double alpha, SEXP A, double B[], int m, int n, int ldb)
396 : bates 434 {
397 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
398 :     AxP = GET_SLOT(A, Matrix_xSym);
399 :     int *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
400 :     *Ap = INTEGER(ApP),
401 :     *xdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
402 :     i, j, nb = length(ApP) - 1;
403 : bates 448 double *Ax = REAL(GET_SLOT(A, Matrix_xSym)), minus1 = -1., one = 1.;
404 : bates 434
405 :     if (xdims[0] != xdims[1])
406 :     error("Argument A to cscb_trsm is not triangular");
407 :     if (ldb < m || ldb <= 0 || n <= 0)
408 :     error("cscb_trsm: inconsistent dims m = %d, n = %d, ldb = %d",
409 :     m, n, ldb);
410 :     if (m != (nb * xdims[0]))
411 :     error("cscb_trsm: inconsistent dims m = %d, A[%d,%d,]x%d",
412 :     m, xdims[0], xdims[1], xdims[2]);
413 :     if (alpha != 1.0) {
414 :     for (j = 0; j < n; j++) { /* scale by alpha */
415 :     for (i = 0; i < m; i++)
416 :     B[i + j * ldb] *= alpha;
417 :     }
418 :     }
419 : bates 448 if (diag == UNT) {
420 : bates 434 if (xdims[2] < 1) return; /* A is the identity */
421 : bates 448 if (xdims[0] == 1) { /* scalar case */
422 :     if (uplo == UPP) error("Code for upper triangle not yet written");
423 :     if (transa == TRN) {
424 : bates 434 for (j = 0; j < n; j++)
425 :     R_ldl_ltsolve(m, B + j * ldb, Ap, Ai, Ax);
426 :     } else {
427 :     for (j = 0; j < n; j++)
428 :     R_ldl_lsolve(m, B + j * ldb, Ap, Ai, Ax);
429 :     }
430 :     return;
431 : bates 448 } else {
432 :     int p, p2, sza = xdims[0] * xdims[0], szb = xdims[0] * n;
433 :     double *tmp = Calloc(szb, double);
434 :     if (uplo == UPP) error("Code for upper triangle not yet written");
435 :     if (transa == TRN) {
436 :     for (j = nb - 1; j >= 0; j--) {
437 :     p2 = Ap[j+1];
438 :    
439 :     F77_CALL(dlacpy)("A", xdims, &n, B + j * xdims[0], &ldb,
440 :     tmp, xdims);
441 :     for (p = Ap[j]; p < p2; p++)
442 :     F77_CALL(dgemm)("T", "N", xdims, &n, xdims,
443 :     &minus1, Ax + p * sza, xdims,
444 :     B + Ai[p] * xdims[0], &ldb,
445 :     &one, tmp, xdims);
446 :     F77_CALL(dlacpy)("A", xdims, &n, tmp, xdims,
447 :     B + j * xdims[0], &ldb);
448 :     }
449 :     } else {
450 :     for (j = 0; j < nb; j++) {
451 :     p2 = Ap[j+1];
452 :    
453 :     F77_CALL(dlacpy)("A", xdims, &n, B + j * xdims[0], &ldb,
454 :     tmp, xdims);
455 :     for (p = Ap[j]; p < p2; p++)
456 :     F77_CALL(dgemm)("N", "N", xdims, &n, xdims,
457 :     &minus1, Ax + p * sza, xdims,
458 :     B + Ai[p] * xdims[0], &ldb,
459 :     &one, tmp, xdims);
460 :     F77_CALL(dlacpy)("A", xdims, &n, tmp, xdims,
461 :     B + j * xdims[0], &ldb);
462 :     }
463 :     }
464 : bates 434 }
465 : bates 448 } else {error("Code for non-unit cases of cscb_trsm not yet written");}
466 : bates 434 }
467 :    
468 :     /**
469 : bates 415 * Perform one of the operations B := alpha*op(A)*B or
470 : bates 485 * B := alpha*B*op(A) where A and B are both dgBCMatrix.
471 : bates 415 *
472 : bates 448 * @param side
473 :     * @param uplo
474 :     * @param transa
475 :     * @param diag
476 : bates 415 * @param alpha scalar multiplier
477 : bates 485 * @param A pointer to a triangular dgBCMatrix object
478 :     * @param B pointer to a general dgBCMatrix matrix
479 : bates 415 */
480 : bates 447 void
481 : bates 448 cscb_trcbm(enum CBLAS_SIDE side, enum CBLAS_UPLO uplo,
482 :     enum CBLAS_TRANSPOSE transa, enum CBLAS_DIAG diag,
483 : bates 447 double alpha, SEXP A, SEXP B)
484 : bates 415 {
485 : bates 451 SEXP
486 :     /* ApP = GET_SLOT(A, Matrix_pSym), */
487 :     AxP = GET_SLOT(A, Matrix_xSym),
488 :     /* , BpP = GET_SLOT(B, Matrix_pSym) */
489 :     BxP = GET_SLOT(B, Matrix_xSym);
490 : bates 448 int
491 : bates 451 /* *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)), */
492 :     /* *Ap = INTEGER(ApP), */
493 :     /* *Bi = INTEGER(GET_SLOT(B, Matrix_iSym)), */
494 :     /* *Bp = INTEGER(BpP), */
495 : bates 415 *axdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
496 : bates 451 *bxdims = INTEGER(getAttrib(BxP, R_DimSymbol))
497 :     /* , ncbA = length(ApP) - 1 */
498 :     /* , ncbB = length(BpP) - 1 */
499 :     ;
500 : bates 415 int i, nbx = bxdims[0] * bxdims[1] * bxdims[2];
501 :    
502 :     if (axdims[0] != axdims[1])
503 :     error("Argument A to cscb_trcbm is not triangular");
504 :     if (alpha != 1.0) {
505 :     for (i = 0; i < nbx; i++) { /* scale by alpha */
506 :     REAL(BxP)[i] *= alpha;
507 :     }
508 :     }
509 : bates 448 if (diag == UNT && axdims[2] < 1) return; /* A is the identity */
510 : bates 415 error("Code for non-trivial cscb_trcbm not yet written");
511 :     }
512 :    
513 :     /**
514 : bates 434 * Expand a column of a compressed, sparse, column-oriented matrix.
515 :     *
516 :     * @param dest array to hold the result
517 :     * @param m number of rows in the matrix
518 :     * @param j index (0-based) of column to expand
519 :     * @param Ap array of column pointers
520 :     * @param Ai array of row indices
521 :     * @param Ax array of non-zero values
522 :     *
523 :     * @return dest
524 :     */
525 :     static
526 :     double *expand_column(double *dest, int m, int j,
527 :     const int Ap[], const int Ai[], const double Ax[])
528 :     {
529 :     int k, k2 = Ap[j + 1];
530 :    
531 :     for (k = 0; k < m; k++) dest[k] = 0.;
532 :     for (k = Ap[j]; k < k2; k++) dest[Ai[k]] = Ax[k];
533 :     return dest;
534 :     }
535 :    
536 :     /**
537 :     * Solve one of the systems op(A)*X = alpha*B or
538 : bates 485 * X*op(A) = alpha*B where A dgBCMatrix triangular and B is dgBCMatrix.
539 : bates 434 *
540 :     * @param side 'L' or 'R' for left or right
541 :     * @param uplo 'U' or 'L' for upper or lower
542 :     * @param transa 'T' or 'N' for transpose or no transpose
543 :     * @param diag 'U' or 'N' for unit diagonal or non-unit
544 :     * @param alpha scalar multiplier
545 : bates 485 * @param A pointer to a triangular dgBCMatrix object
546 :     * @param B pointer to a general dgBCMatrix matrix
547 : bates 434 */
548 : bates 447 void
549 : bates 448 cscb_trcbsm(enum CBLAS_SIDE side, enum CBLAS_UPLO uplo,
550 :     enum CBLAS_TRANSPOSE transa, enum CBLAS_DIAG diag,
551 : bates 447 double alpha, SEXP A, const int Parent[], SEXP B)
552 : bates 434 {
553 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
554 :     AxP = GET_SLOT(A, Matrix_xSym),
555 :     BpP = GET_SLOT(B, Matrix_pSym),
556 :     BxP = GET_SLOT(B, Matrix_xSym);
557 :     int *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
558 :     *Ap = INTEGER(ApP),
559 :     *Bi = INTEGER(GET_SLOT(B, Matrix_iSym)),
560 :     *Bp = INTEGER(BpP),
561 :     *axdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
562 :     *bxdims = INTEGER(getAttrib(BxP, R_DimSymbol)),
563 : bates 451 /* ncbA = length(ApP) - 1, */
564 : bates 434 ncbB = length(BpP) - 1;
565 :     int i, j, nbx = bxdims[0] * bxdims[1] * bxdims[2];
566 :     double *Ax = REAL(AxP), *Bx = REAL(BxP);
567 :    
568 :     if (axdims[0] != axdims[1])
569 :     error("Argument A to cscb_trcbm is not triangular");
570 :     if (alpha != 1.0) {
571 :     for (i = 0; i < nbx; i++) { /* scale by alpha */
572 :     REAL(BxP)[i] *= alpha;
573 :     }
574 :     }
575 : bates 448 if (diag == UNT && axdims[2] < 1) return; /* A is the identity */
576 :     if (diag == UNT && axdims[0] == 1) { /* can use R_ldl code */
577 :     if ((side != LFT) && transa == TRN) { /* case required for lmer */
578 : bates 434 int *BTp, nnz = bxdims[2], nrbB;
579 :     int *tmp = expand_column_pointers(ncbB, Bp, Calloc(nnz, int));
580 :     int *BTi = Calloc(nnz, int);
581 :     double *BTx = Calloc(nnz, double), *rhs;
582 :    
583 :     /* transpose B */
584 :     for (i = 0, nrbB = -1; i < nnz; i++) if (Bi[i] > nrbB) nrbB = Bi[i];
585 :     BTp = Calloc(nrbB, int);
586 : bates 478 dgTMatrix_to_dgCMatrix(ncbB, nrbB, nnz, tmp, Bi, Bx, BTp, BTi, BTx);
587 : bates 434 /* sanity check */
588 :     if (BTp[nrbB] != nnz) error("cscb_trcbsm: transpose operation failed");
589 :     Free(tmp);
590 :     /* Solve one column at a time */
591 :     rhs = Calloc(ncbB, double);
592 :     AZERO(Bx, nnz); /* zero the result */
593 :     for (i = 0; i < nrbB; i++) {
594 :     R_ldl_lsolve(ncbB, expand_column(rhs, ncbB, i, BTp, BTi, BTx),
595 :     Ap, Ai, Ax);
596 :     for (j = 0; j < ncbB; j++) { /* write non-zeros in sol'n into B */
597 :     if (BTx[j]) Bx[Tind(Bi, Bp, j, i)] = BTx[j];
598 :     }
599 :     Free(rhs); Free(BTp); Free(BTx); Free(BTi);
600 :     }
601 :     }
602 :     error("cscb_trcbsm: method not yet written");
603 :     }
604 :     error("cscb_trcbsm: method not yet written");
605 :     }
606 :    
607 :     /**
608 : bates 415 * Perform one of the matrix-matrix operations
609 :     * C := alpha*op(A)*op(B) + beta*C
610 :     * on compressed, sparse, blocked matrices.
611 :     *
612 :     * @param transa 'T' for transpose of A, else 'N'
613 :     * @param transb 'T' for transpose of B, else 'N'
614 :     * @param alpha scalar multiplier
615 : bates 485 * @param A pointer to a dgBCMatrix object
616 :     * @param B pointer to a dgBCMatrix object
617 : bates 415 * @param beta scalar multiplier
618 : bates 485 * @param C pointer to a dgBCMatrix object
619 : bates 415 */
620 : bates 447 void
621 :     cscb_cscbm(enum CBLAS_TRANSPOSE transa, enum CBLAS_TRANSPOSE transb,
622 :     double alpha, SEXP A, SEXP B, double beta, SEXP C)
623 : bates 415 {
624 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
625 :     AxP = GET_SLOT(A, Matrix_xSym),
626 :     BpP = GET_SLOT(B, Matrix_pSym),
627 :     BxP = GET_SLOT(B, Matrix_xSym),
628 :     CxP = GET_SLOT(C, Matrix_xSym);
629 :     int *Ap = INTEGER(ApP),
630 :     *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
631 :     *Bp = INTEGER(BpP),
632 :     *Bi = INTEGER(GET_SLOT(B, Matrix_iSym)),
633 :     *Cp = INTEGER(GET_SLOT(C, Matrix_pSym)),
634 :     *Ci = INTEGER(GET_SLOT(C, Matrix_iSym)),
635 :     *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
636 :     *bdims = INTEGER(getAttrib(BxP, R_DimSymbol)),
637 :     *cdims = INTEGER(getAttrib(CxP, R_DimSymbol)),
638 :     nca = length(ApP) - 1,
639 :     ncb = length(BpP) - 1;
640 :     int ablk = adims[0] * adims[1],
641 :     bblk = bdims[0] * bdims[1],
642 :     cblk = cdims[0] * cdims[1];
643 :     double *Ax = REAL(AxP),
644 :     *Bx = REAL(BxP),
645 :     *Cx = REAL(CxP),
646 :     one = 1.0;
647 :    
648 : bates 448 if ((transa == NTR) && transb == TRN) { /* transposed crossproduct */
649 : bates 415 int jj;
650 :    
651 :     if (adims[1] != bdims[1] ||
652 :     adims[0] != cdims[0] ||
653 :     bdims[0] != cdims[1])
654 :     error("C[%d,%d,%d] := A[%d,%d,%d] %*% t(B[%d,%d,%d])",
655 :     cdims[0], cdims[1], cdims[2],
656 :     adims[0], adims[1], adims[2],
657 :     bdims[0], bdims[1], bdims[2]);
658 :     if (nca != ncb)
659 :     error("C := A(ncblocks = %d) %*% t(B(ncblocks = %d)", nca, ncb);
660 :     if (beta != 1.) { /* scale C by beta */
661 :     int ctot = cdims[0] * cdims[1] * cdims[2];
662 :     for (jj = 0; jj < ctot; jj++) Cx[jj] *= beta;
663 :     }
664 :     for (jj = 0; jj < nca; jj++) {
665 :     int ia, ib, a2 = Ap[jj + 1], b2 = Bp[jj + 1];
666 :     for (ia = Ap[jj]; ia < a2; ia++) {
667 : bates 448 for (ib = Bp[jj]; ib < b2; ib++) {
668 :     F77_CALL(dgemm)("N", "T", cdims, cdims + 1, adims + 1,
669 : bates 415 &alpha, Ax + ia * ablk, adims,
670 :     Bx + ib * bblk, bdims, &one,
671 : bates 448 Cx + Tind(Ci, Cp, Ai[ia], Bi[ib])*cblk, cdims);
672 : bates 339 }
673 :     }
674 :     }
675 : bates 415 return;
676 : bates 339 }
677 : bates 415 error("Code not yet written");
678 : bates 339 }
679 : bates 451
680 : bates 485 SEXP dgBCMatrix_to_dgCMatrix(SEXP A)
681 : bates 451 {
682 : bates 478 SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dgCMatrix"))),
683 : bates 451 ApP = GET_SLOT(A, Matrix_pSym),
684 :     AiP = GET_SLOT(A, Matrix_iSym),
685 :     AxP = GET_SLOT(A, Matrix_xSym);
686 :     int *Ai = INTEGER(AiP), *Ap = INTEGER(ApP), *Bi, *Bp, *Dims,
687 :     *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
688 :     ii, j, ncb = length(ApP) - 1, nnz, nrb;
689 :     int nc = adims[1], nr = adims[0];
690 :     int sz = nc * nr;
691 :     double *Ax = REAL(AxP), *Bx;
692 :    
693 : bates 476 SET_SLOT(val, Matrix_factorSym, allocVector(VECSXP, 0));
694 : bates 451 SET_SLOT(val, Matrix_DimSym, allocVector(INTSXP, 2));
695 :     Dims = INTEGER(GET_SLOT(val, Matrix_DimSym));
696 :     Dims[1] = ncb * adims[1];
697 :     /* find number of row blocks */
698 :     for (j = 0, nrb = -1; j < adims[2]; j++) if (Ai[j] > nrb) nrb = Ai[j];
699 :     Dims[0] = nrb * adims[0];
700 :     nnz = length(AxP);
701 :    
702 :     if (nc == 1) { /* x slot is in the correct order */
703 :     SET_SLOT(val, Matrix_pSym, duplicate(ApP));
704 :     SET_SLOT(val, Matrix_iSym, allocVector(INTSXP, nnz));
705 :     SET_SLOT(val, Matrix_xSym, allocVector(REALSXP, nnz));
706 :     Memcpy(REAL(GET_SLOT(val, Matrix_xSym)), Ax, nnz);
707 :     if (nr == 1) {
708 :     Memcpy(INTEGER(GET_SLOT(val, Matrix_iSym)), Ai, nnz);
709 :     } else {
710 :     Bi = INTEGER(GET_SLOT(val, Matrix_iSym));
711 :     Bp = INTEGER(GET_SLOT(val, Matrix_pSym));
712 :     for (j = 0; j <= ncb; j++) Bp[j] *= nr;
713 :     for (j = 0; j < adims[2]; j++) {
714 :     for (ii = 0; ii < nr; ii++) {
715 :     Bi[j * nr + ii] = Ai[j] * nr + ii;
716 :     }
717 :     }
718 :     }
719 :     } else {
720 :     SET_SLOT(val, Matrix_pSym, allocVector(INTSXP, Dims[1] + 1));
721 :     Bp = INTEGER(GET_SLOT(val, Matrix_pSym));
722 :     SET_SLOT(val, Matrix_iSym, allocVector(INTSXP, nnz));
723 :     Bi = INTEGER(GET_SLOT(val, Matrix_iSym));
724 :     SET_SLOT(val, Matrix_xSym, allocVector(REALSXP, nnz));
725 :     Bx = REAL(GET_SLOT(val, Matrix_xSym));
726 :    
727 :     Bp[0] = 0;
728 :     for (j = 0; j < ncb; j++) { /* Column blocks of A */
729 :     int i, i1 = Ap[j], i2 = Ap[j + 1], jj;
730 :     int nzbc = (i2 - i1) * nr; /* No. of non-zeroes in B column */
731 :    
732 :     for (jj = 0; jj < nc; jj++) { /* column within blocks */
733 :     int jb = nc * j + jj; /* Column number in B */
734 :    
735 :     Bp[jb] = i1 * sz + jj * nzbc;
736 :     for (i = i1; i < i2; i++) { /* index in Ai and Ax */
737 :     for (ii = 0; ii < adims[0]; ii++) { /* row within blocks */
738 :     int ind = ii + (i - i1) * nr + Bp[jb];
739 :    
740 :     Bi[ind] = Ai[i] * sz + jj * nzbc + ii;
741 :     Bx[ind] = Ax[i * sz + jj * nc + ii];
742 :     }
743 :     }
744 :     }
745 :     }
746 :     }
747 :     UNPROTECT(1);
748 :     return val;
749 :     }

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge