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

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