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 556 - (view) (download) (as text)

1 : bates 485 #include "dgBCMatrix.h"
2 : bates 415 /* TODO
3 :     * - code for trans = 'T' in cscb_syrk
4 : bates 556 * - code for non-trivial cscb_trmm
5 : bates 415 */
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 : bates 556 static void
205 :     copy_transpose(double dest[], const double src[], int n)
206 :     {
207 :     int i, j;
208 :     for (i = 0; i < n; i++) {
209 :     for (j = 0; j < n; j++) {
210 :     dest[i + j * n] = src[j + i * n];
211 :     }
212 :     }
213 :     }
214 :    
215 : bates 415 /**
216 : bates 451 * Create the LD^{T/2}D^{1/2}L' decomposition of the positive definite
217 : bates 536 * symmetric dgBCMatrix matrix A (upper triangle stored) in L and D^{1/2}.
218 :     * D^{1/2} denotes the upper Cholesky factor of the positive definite positive
219 :     * definite block diagonal matrix D. The diagonal blocks are of size nci.
220 : bates 415 *
221 : bates 485 * @param A pointer to a dgBCMatrix object containing the upper
222 : bates 415 * triangle of a positive definite symmetric matrix.
223 :     * @param Parent the parent array for A
224 : bates 485 * @param L pointer to a dgBCMatrix object to hold L
225 : bates 415 * @param D pointer to a 3D array to hold D
226 :     *
227 :     * @return n the number of column blocks in A for success. A value
228 :     * less than n indicates the first column block whose diagonal was not
229 :     * positive definite.
230 :     */
231 :     int
232 :     cscb_ldl(SEXP A, const int Parent[], SEXP L, SEXP D)
233 :     {
234 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
235 :     AxP = GET_SLOT(A, Matrix_xSym);
236 :     int *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
237 : bates 434 diag, info, j, k, n = length(ApP) - 1;
238 : bates 415 int *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
239 :     *Ap = INTEGER(ApP),
240 :     *Li = INTEGER(GET_SLOT(L, Matrix_iSym)),
241 : bates 434 *Lp = INTEGER(GET_SLOT(L, Matrix_pSym)), nci = adims[0];
242 : bates 536 int ncisqr = nci * nci;
243 : bates 415 double *Lx = REAL(GET_SLOT(L, Matrix_xSym)),
244 : bates 434 *Ax = REAL(AxP), *Dx = REAL(D), minus1 = -1., one = 1.;
245 : bates 415
246 : bates 434 if (adims[1] != nci || nci < 1)
247 :     error("cscb_ldl: dim(A) is [%d, %d, %d]", adims[0], adims[1], adims[2]);
248 : bates 415 for (j = 0, diag = 1; j < n; j++) { /* check for trivial structure */
249 :     if (Parent[j] >= 0) {diag = 0; break;}
250 :     }
251 :     if (diag) {
252 : bates 434 Memcpy(Dx, Ax, ncisqr * n);
253 :     for (j = 0; j < n; j++) { /* form D_i^{1/2} */
254 :     F77_CALL(dpotrf)("U", &nci, Dx + j * ncisqr, &nci, &k);
255 : bates 556 if (k) return j; /* return block number, not col no. */
256 : bates 434 }
257 : bates 415 return n;
258 :     }
259 : bates 434 if (nci == 1) {
260 :     k = R_ldl_numeric(n, Ap, Ai, Ax, Lp, Parent, Li, Lx, Dx,
261 :     (int *) NULL, (int *) NULL);
262 : bates 556 if (k < n) return k;
263 : bates 434 for (j = 0; j < n; j++) Dx[j] = sqrt(Dx[j]);
264 :     return n;
265 :     } else { /* Copy of ldl_numeric from the LDL package
266 :     * modified for blocked sparse matrices */
267 : bates 536 int i, k, p, p2, len, top;
268 : bates 434 int *Lnz = Calloc(n, int),
269 :     *Pattern = Calloc(n, int),
270 :     *Flag = Calloc(n, int);
271 :     double *Y = Calloc(n * ncisqr, double), *Yi = Calloc(ncisqr, double);
272 : bates 415
273 : bates 434 for (k = 0; k < n; k++) {
274 :     /* compute nonzero Pattern of kth row of L, in topological order */
275 :     AZERO(Y + k*ncisqr, ncisqr); /* Y[,,0:k] is now all zero */
276 :     top = n; /* stack for pattern is empty */
277 :     Flag[k] = k; /* mark node k as visited */
278 :     Lnz[k] = 0; /* count of nonzeros in column k of L */
279 :     p2 = Ap[k+1];
280 :     for (p = Ap[k]; p < p2; p++) {
281 :     i = Ai[p]; /* get A[i,k] */
282 : bates 556 if (i > k) error("cscb_ldl: A has nonzeros below diagonal");
283 : bates 434 /* copy A(i,k) into Y */
284 : bates 556 Memcpy(Y + i * ncisqr, Ax + p * ncisqr, ncisqr);
285 :     /* follow path from i to root of etree,
286 :     * stop at flagged node */
287 :     for (len = 0; Flag[i] != k; i = Parent[i]) {
288 :     Pattern[len++] = i; /* L[k,i] is nonzero */
289 :     Flag[i] = k; /* mark i as visited */
290 : bates 434 }
291 : bates 556 while (len > 0) { /* push path on top of stack */
292 :     Pattern[--top] = Pattern[--len];
293 :     }
294 : bates 434 }
295 :     /* Pattern [top ... n-1] now contains nonzero pattern of L[,k] */
296 : bates 553 /* compute numerical values in kth row of L
297 :     * (a sparse triangular solve) */
298 : bates 434 Memcpy(Dx + k * ncisqr, Y + k * ncisqr, ncisqr); /* get D[,,k] */
299 :     AZERO(Y + k*ncisqr, ncisqr); /* clear Y[,,k] */
300 :     for (; top < n; top++) {
301 :     i = Pattern[top];
302 :     Memcpy(Yi, Y + i*ncisqr, ncisqr); /* copy Y[,,i] */
303 :     AZERO(Y + i*ncisqr, ncisqr); /* clear Y[,,i] */
304 :     p2 = Lp[i] + Lnz[i];
305 :     for (p = Lp[i]; p < p2; p++) {
306 :     F77_CALL(dgemm)("N", "N", &nci, &nci, &nci, &minus1,
307 :     Lx + p*ncisqr, &nci, Yi, &nci,
308 :     &one, Y + Li[p]*ncisqr, &nci);
309 :     }
310 : bates 553 /* FIXME: Is this the correct order and transposition? */
311 : bates 556 F77_CALL(dtrsm)("L", "U", "T", "N", &nci, &nci,
312 : bates 434 &one, Dx + i*ncisqr, &nci, Yi, &nci);
313 : bates 556 F77_CALL(dsyrk)("U", "T", &nci, &nci, &minus1, Yi, &nci,
314 : bates 434 &one, Dx + k*ncisqr, &nci);
315 : bates 556 F77_CALL(dtrsm)("L", "U", "N", "N", &nci, &nci,
316 : bates 434 &one, Dx + i*ncisqr, &nci, Yi, &nci);
317 :     Li[p] = k; /* store L[k,i] in column form of L */
318 : bates 556 /* Yi contains L[k,i]', not L[k,i] */
319 :     copy_transpose(Lx + p * ncisqr, Yi, nci);
320 :     Lnz[i]++; /* increment count of nonzeros in col i */
321 : bates 434 }
322 :     F77_CALL(dpotrf)("U", &nci, Dx + k*ncisqr, &nci, &info);
323 :     if (info) {
324 :     Free(Y); Free(Yi); Free(Pattern); Free(Flag); Free(Lnz);
325 : bates 556 return k; /* failure, D[,,k] not positive definite */
326 : bates 434 }
327 :     }
328 :     Free(Y); Free(Yi); Free(Pattern); Free(Flag); Free(Lnz);
329 :     return n; /* success, diagonal of D is all nonzero */
330 : bates 415 }
331 : bates 536 return -1; /* -Wall */
332 : bates 415 }
333 :    
334 :     /**
335 : bates 485 * Perform one of the dgBCMatrix-matrix operations B := alpha*op(A)*B
336 : bates 415 * or B := alpha*B*op(A)
337 :     *
338 : bates 448 * @param side
339 :     * @param uplo
340 :     * @param transa
341 :     * @param diag
342 : bates 485 * @param A pointer to a triangular dgBCMatrix object
343 : bates 448 * @param B contents of the matrix B
344 : bates 415 * @param m number of rows in B
345 :     * @param n number of columns in B
346 :     * @param ldb leading dimension of B as declared in the calling function
347 :     */
348 : bates 447 void
349 :     cscb_trmm(enum CBLAS_SIDE side, enum CBLAS_UPLO uplo,
350 :     enum CBLAS_TRANSPOSE transa, enum CBLAS_DIAG diag,
351 :     double alpha, SEXP A, double B[], int m, int n, int ldb)
352 : bates 415 {
353 : bates 451 SEXP /* ApP = GET_SLOT(A, Matrix_pSym), */
354 : bates 415 AxP = GET_SLOT(A, Matrix_xSym);
355 : bates 451 int /* *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)), */
356 :     /* *Ap = INTEGER(ApP), */
357 : bates 415 *xdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
358 : bates 451 i, j/* , nb = length(ApP) - 1 */;
359 : bates 415
360 :     if (xdims[0] != xdims[1])
361 :     error("Argument A to cscb_trmm is not triangular");
362 :     if (alpha != 1.0) {
363 :     for (j = 0; j < n; j++) { /* scale by alpha */
364 :     for (i = 0; i < m; i++)
365 :     B[i + j * ldb] *= alpha;
366 :     }
367 :     }
368 : bates 448 if (diag == UNT && xdims[2] < 1) return; /* A is the identity */
369 : bates 415 error("Code for non-identity cases of cscb_trmm not yet written");
370 :     }
371 :    
372 :     /**
373 : bates 434 * Solve a triangular system of the form op(A)*X = alpha*B where A
374 : bates 485 * is a dgBCMatrix triangular matrix and B is a dense matrix.
375 : bates 434 *
376 :     * @param uplo 'U' or 'L' for upper or lower
377 :     * @param trans 'T' or 'N' for transpose or no transpose
378 :     * @param diag 'U' or 'N' for unit diagonal or non-unit
379 : bates 485 * @param A pointer to a triangular dgBCMatrix object
380 : bates 434 * @param B pointer to the contents of the matrix B
381 :     * @param m number of rows in B
382 :     * @param n number of columns in B
383 :     * @param ldb leading dimension of B as declared in the calling function
384 :     */
385 : bates 447 void
386 : bates 556 cscb_trsm(enum CBLAS_UPLO uplo, enum CBLAS_TRANSPOSE transa,
387 :     enum CBLAS_DIAG diag, double alpha, SEXP A,
388 :     double B[], int m, int n, int ldb)
389 : bates 434 {
390 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
391 :     AxP = GET_SLOT(A, Matrix_xSym);
392 :     int *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
393 :     *Ap = INTEGER(ApP),
394 :     *xdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
395 :     i, j, nb = length(ApP) - 1;
396 : bates 448 double *Ax = REAL(GET_SLOT(A, Matrix_xSym)), minus1 = -1., one = 1.;
397 : bates 434
398 :     if (xdims[0] != xdims[1])
399 :     error("Argument A to cscb_trsm is not triangular");
400 :     if (ldb < m || ldb <= 0 || n <= 0)
401 :     error("cscb_trsm: inconsistent dims m = %d, n = %d, ldb = %d",
402 :     m, n, ldb);
403 :     if (m != (nb * xdims[0]))
404 :     error("cscb_trsm: inconsistent dims m = %d, A[%d,%d,]x%d",
405 :     m, xdims[0], xdims[1], xdims[2]);
406 :     if (alpha != 1.0) {
407 :     for (j = 0; j < n; j++) { /* scale by alpha */
408 :     for (i = 0; i < m; i++)
409 :     B[i + j * ldb] *= alpha;
410 :     }
411 :     }
412 : bates 448 if (diag == UNT) {
413 : bates 434 if (xdims[2] < 1) return; /* A is the identity */
414 : bates 448 if (xdims[0] == 1) { /* scalar case */
415 :     if (uplo == UPP) error("Code for upper triangle not yet written");
416 :     if (transa == TRN) {
417 : bates 434 for (j = 0; j < n; j++)
418 :     R_ldl_ltsolve(m, B + j * ldb, Ap, Ai, Ax);
419 :     } else {
420 :     for (j = 0; j < n; j++)
421 :     R_ldl_lsolve(m, B + j * ldb, Ap, Ai, Ax);
422 :     }
423 :     return;
424 : bates 448 } else {
425 :     int p, p2, sza = xdims[0] * xdims[0], szb = xdims[0] * n;
426 :     double *tmp = Calloc(szb, double);
427 :     if (uplo == UPP) error("Code for upper triangle not yet written");
428 :     if (transa == TRN) {
429 :     for (j = nb - 1; j >= 0; j--) {
430 :     p2 = Ap[j+1];
431 :    
432 :     F77_CALL(dlacpy)("A", xdims, &n, B + j * xdims[0], &ldb,
433 :     tmp, xdims);
434 :     for (p = Ap[j]; p < p2; p++)
435 :     F77_CALL(dgemm)("T", "N", xdims, &n, xdims,
436 :     &minus1, Ax + p * sza, xdims,
437 :     B + Ai[p] * xdims[0], &ldb,
438 :     &one, tmp, xdims);
439 :     F77_CALL(dlacpy)("A", xdims, &n, tmp, xdims,
440 :     B + j * xdims[0], &ldb);
441 :     }
442 :     } else {
443 :     for (j = 0; j < nb; j++) {
444 :     p2 = Ap[j+1];
445 :    
446 :     F77_CALL(dlacpy)("A", xdims, &n, B + j * xdims[0], &ldb,
447 :     tmp, xdims);
448 :     for (p = Ap[j]; p < p2; p++)
449 :     F77_CALL(dgemm)("N", "N", xdims, &n, xdims,
450 :     &minus1, Ax + p * sza, xdims,
451 :     B + Ai[p] * xdims[0], &ldb,
452 :     &one, tmp, xdims);
453 :     F77_CALL(dlacpy)("A", xdims, &n, tmp, xdims,
454 :     B + j * xdims[0], &ldb);
455 :     }
456 :     }
457 : bates 434 }
458 : bates 448 } else {error("Code for non-unit cases of cscb_trsm not yet written");}
459 : bates 434 }
460 :    
461 :     /**
462 : bates 415 * Perform one of the operations B := alpha*op(A)*B or
463 : bates 485 * B := alpha*B*op(A) where A and B are both dgBCMatrix.
464 : bates 415 *
465 : bates 448 * @param side
466 :     * @param uplo
467 :     * @param transa
468 :     * @param diag
469 : bates 415 * @param alpha scalar multiplier
470 : bates 485 * @param A pointer to a triangular dgBCMatrix object
471 :     * @param B pointer to a general dgBCMatrix matrix
472 : bates 415 */
473 : bates 447 void
474 : bates 448 cscb_trcbm(enum CBLAS_SIDE side, enum CBLAS_UPLO uplo,
475 :     enum CBLAS_TRANSPOSE transa, enum CBLAS_DIAG diag,
476 : bates 447 double alpha, SEXP A, SEXP B)
477 : bates 415 {
478 : bates 451 SEXP
479 :     /* ApP = GET_SLOT(A, Matrix_pSym), */
480 :     AxP = GET_SLOT(A, Matrix_xSym),
481 :     /* , BpP = GET_SLOT(B, Matrix_pSym) */
482 :     BxP = GET_SLOT(B, Matrix_xSym);
483 : bates 448 int
484 : bates 451 /* *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)), */
485 :     /* *Ap = INTEGER(ApP), */
486 :     /* *Bi = INTEGER(GET_SLOT(B, Matrix_iSym)), */
487 :     /* *Bp = INTEGER(BpP), */
488 : bates 415 *axdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
489 : bates 451 *bxdims = INTEGER(getAttrib(BxP, R_DimSymbol))
490 :     /* , ncbA = length(ApP) - 1 */
491 :     /* , ncbB = length(BpP) - 1 */
492 :     ;
493 : bates 415 int i, nbx = bxdims[0] * bxdims[1] * bxdims[2];
494 :    
495 :     if (axdims[0] != axdims[1])
496 :     error("Argument A to cscb_trcbm is not triangular");
497 :     if (alpha != 1.0) {
498 :     for (i = 0; i < nbx; i++) { /* scale by alpha */
499 :     REAL(BxP)[i] *= alpha;
500 :     }
501 :     }
502 : bates 448 if (diag == UNT && axdims[2] < 1) return; /* A is the identity */
503 : bates 415 error("Code for non-trivial cscb_trcbm not yet written");
504 :     }
505 :    
506 :     /**
507 : bates 434 * Solve one of the systems op(A)*X = alpha*B or
508 : bates 485 * X*op(A) = alpha*B where A dgBCMatrix triangular and B is dgBCMatrix.
509 : bates 434 *
510 :     * @param side 'L' or 'R' for left or right
511 :     * @param uplo 'U' or 'L' for upper or lower
512 :     * @param transa 'T' or 'N' for transpose or no transpose
513 :     * @param diag 'U' or 'N' for unit diagonal or non-unit
514 :     * @param alpha scalar multiplier
515 : bates 485 * @param A pointer to a triangular dgBCMatrix object
516 :     * @param B pointer to a general dgBCMatrix matrix
517 : bates 434 */
518 : bates 447 void
519 : bates 448 cscb_trcbsm(enum CBLAS_SIDE side, enum CBLAS_UPLO uplo,
520 :     enum CBLAS_TRANSPOSE transa, enum CBLAS_DIAG diag,
521 : bates 447 double alpha, SEXP A, const int Parent[], SEXP B)
522 : bates 434 {
523 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
524 :     AxP = GET_SLOT(A, Matrix_xSym),
525 :     BpP = GET_SLOT(B, Matrix_pSym),
526 :     BxP = GET_SLOT(B, Matrix_xSym);
527 :     int *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
528 :     *Ap = INTEGER(ApP),
529 :     *Bi = INTEGER(GET_SLOT(B, Matrix_iSym)),
530 :     *Bp = INTEGER(BpP),
531 :     *axdims = INTEGER(getAttrib(AxP, R_DimSymbol)),
532 :     *bxdims = INTEGER(getAttrib(BxP, R_DimSymbol)),
533 : bates 451 /* ncbA = length(ApP) - 1, */
534 : bates 434 ncbB = length(BpP) - 1;
535 :     int i, j, nbx = bxdims[0] * bxdims[1] * bxdims[2];
536 :     double *Ax = REAL(AxP), *Bx = REAL(BxP);
537 :    
538 :     if (axdims[0] != axdims[1])
539 :     error("Argument A to cscb_trcbm is not triangular");
540 :     if (alpha != 1.0) {
541 :     for (i = 0; i < nbx; i++) { /* scale by alpha */
542 :     REAL(BxP)[i] *= alpha;
543 :     }
544 :     }
545 : bates 448 if (diag == UNT && axdims[2] < 1) return; /* A is the identity */
546 :     if (diag == UNT && axdims[0] == 1) { /* can use R_ldl code */
547 :     if ((side != LFT) && transa == TRN) { /* case required for lmer */
548 : bates 434 int *BTp, nnz = bxdims[2], nrbB;
549 :     int *tmp = expand_column_pointers(ncbB, Bp, Calloc(nnz, int));
550 :     int *BTi = Calloc(nnz, int);
551 :     double *BTx = Calloc(nnz, double), *rhs;
552 :    
553 :     /* transpose B */
554 : bates 556 for (i = 0, nrbB = -1; i < nnz; i++)
555 :     if (Bi[i] > nrbB) nrbB = Bi[i];
556 : bates 434 BTp = Calloc(nrbB, int);
557 : bates 488 triplet_to_col(ncbB, nrbB, nnz, tmp, Bi, Bx, BTp, BTi, BTx);
558 : bates 434 /* sanity check */
559 :     if (BTp[nrbB] != nnz) error("cscb_trcbsm: transpose operation failed");
560 :     Free(tmp);
561 :     /* Solve one column at a time */
562 :     rhs = Calloc(ncbB, double);
563 :     AZERO(Bx, nnz); /* zero the result */
564 :     for (i = 0; i < nrbB; i++) {
565 : bates 556 R_ldl_lsolve(ncbB,
566 :     expand_csc_column(rhs, ncbB, i, BTp, BTi, BTx),
567 : bates 434 Ap, Ai, Ax);
568 : bates 556 /* write non-zeros in sol'n into B */
569 :     for (j = 0; j < ncbB; j++) {
570 : bates 536 if (BTx[j]) Bx[check_csc_index(Bp, Bi, j, i, 0)] = BTx[j];
571 : bates 434 }
572 :     Free(rhs); Free(BTp); Free(BTx); Free(BTi);
573 :     }
574 :     }
575 :     error("cscb_trcbsm: method not yet written");
576 :     }
577 :     error("cscb_trcbsm: method not yet written");
578 :     }
579 :    
580 :     /**
581 : bates 415 * Perform one of the matrix-matrix operations
582 :     * C := alpha*op(A)*op(B) + beta*C
583 :     * on compressed, sparse, blocked matrices.
584 :     *
585 :     * @param transa 'T' for transpose of A, else 'N'
586 :     * @param transb 'T' for transpose of B, else 'N'
587 :     * @param alpha scalar multiplier
588 : bates 485 * @param A pointer to a dgBCMatrix object
589 :     * @param B pointer to a dgBCMatrix object
590 : bates 415 * @param beta scalar multiplier
591 : bates 485 * @param C pointer to a dgBCMatrix object
592 : bates 415 */
593 : bates 447 void
594 :     cscb_cscbm(enum CBLAS_TRANSPOSE transa, enum CBLAS_TRANSPOSE transb,
595 :     double alpha, SEXP A, SEXP B, double beta, SEXP C)
596 : bates 415 {
597 :     SEXP ApP = GET_SLOT(A, Matrix_pSym),
598 :     AxP = GET_SLOT(A, Matrix_xSym),
599 :     BpP = GET_SLOT(B, Matrix_pSym),
600 :     BxP = GET_SLOT(B, Matrix_xSym),
601 :     CxP = GET_SLOT(C, Matrix_xSym);
602 :     int *Ap = INTEGER(ApP),
603 :     *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)),
604 :     *Bp = INTEGER(BpP),
605 :     *Bi = INTEGER(GET_SLOT(B, Matrix_iSym)),
606 :     *Cp = INTEGER(GET_SLOT(C, Matrix_pSym)),
607 :     *Ci = INTEGER(GET_SLOT(C, Matrix_iSym)),
608 :     *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
609 :     *bdims = INTEGER(getAttrib(BxP, R_DimSymbol)),
610 :     *cdims = INTEGER(getAttrib(CxP, R_DimSymbol)),
611 :     nca = length(ApP) - 1,
612 :     ncb = length(BpP) - 1;
613 :     int ablk = adims[0] * adims[1],
614 :     bblk = bdims[0] * bdims[1],
615 :     cblk = cdims[0] * cdims[1];
616 :     double *Ax = REAL(AxP),
617 :     *Bx = REAL(BxP),
618 :     *Cx = REAL(CxP),
619 :     one = 1.0;
620 :    
621 : bates 448 if ((transa == NTR) && transb == TRN) { /* transposed crossproduct */
622 : bates 415 int jj;
623 :    
624 :     if (adims[1] != bdims[1] ||
625 :     adims[0] != cdims[0] ||
626 :     bdims[0] != cdims[1])
627 :     error("C[%d,%d,%d] := A[%d,%d,%d] %*% t(B[%d,%d,%d])",
628 :     cdims[0], cdims[1], cdims[2],
629 :     adims[0], adims[1], adims[2],
630 :     bdims[0], bdims[1], bdims[2]);
631 :     if (nca != ncb)
632 :     error("C := A(ncblocks = %d) %*% t(B(ncblocks = %d)", nca, ncb);
633 :     if (beta != 1.) { /* scale C by beta */
634 :     int ctot = cdims[0] * cdims[1] * cdims[2];
635 :     for (jj = 0; jj < ctot; jj++) Cx[jj] *= beta;
636 :     }
637 :     for (jj = 0; jj < nca; jj++) {
638 :     int ia, ib, a2 = Ap[jj + 1], b2 = Bp[jj + 1];
639 :     for (ia = Ap[jj]; ia < a2; ia++) {
640 : bates 448 for (ib = Bp[jj]; ib < b2; ib++) {
641 :     F77_CALL(dgemm)("N", "T", cdims, cdims + 1, adims + 1,
642 : bates 536 &alpha, Ax + ia * ablk, adims,
643 :     Bx + ib * bblk, bdims, &one,
644 : bates 556 Cx + check_csc_index(Cp,Ci,Ai[ia],Bi[ib],0)*cblk,
645 : bates 536 cdims);
646 : bates 339 }
647 :     }
648 :     }
649 : bates 415 return;
650 : bates 339 }
651 : bates 415 error("Code not yet written");
652 : bates 339 }
653 : bates 451
654 : bates 485 SEXP dgBCMatrix_to_dgCMatrix(SEXP A)
655 : bates 451 {
656 : bates 478 SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dgCMatrix"))),
657 : bates 451 ApP = GET_SLOT(A, Matrix_pSym),
658 :     AiP = GET_SLOT(A, Matrix_iSym),
659 :     AxP = GET_SLOT(A, Matrix_xSym);
660 :     int *Ai = INTEGER(AiP), *Ap = INTEGER(ApP), *Bi, *Bp, *Dims,
661 :     *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
662 :     ii, j, ncb = length(ApP) - 1, nnz, nrb;
663 :     int nc = adims[1], nr = adims[0];
664 :     int sz = nc * nr;
665 :     double *Ax = REAL(AxP), *Bx;
666 :    
667 : bates 476 SET_SLOT(val, Matrix_factorSym, allocVector(VECSXP, 0));
668 : bates 451 SET_SLOT(val, Matrix_DimSym, allocVector(INTSXP, 2));
669 :     Dims = INTEGER(GET_SLOT(val, Matrix_DimSym));
670 :     Dims[1] = ncb * adims[1];
671 :     /* find number of row blocks */
672 :     for (j = 0, nrb = -1; j < adims[2]; j++) if (Ai[j] > nrb) nrb = Ai[j];
673 : bates 536 Dims[0] = (nrb + 1) * adims[0]; /* +1 because of 0-based indices */
674 : bates 451 nnz = length(AxP);
675 :    
676 :     if (nc == 1) { /* x slot is in the correct order */
677 :     SET_SLOT(val, Matrix_pSym, duplicate(ApP));
678 :     SET_SLOT(val, Matrix_iSym, allocVector(INTSXP, nnz));
679 :     SET_SLOT(val, Matrix_xSym, allocVector(REALSXP, nnz));
680 :     Memcpy(REAL(GET_SLOT(val, Matrix_xSym)), Ax, nnz);
681 :     if (nr == 1) {
682 :     Memcpy(INTEGER(GET_SLOT(val, Matrix_iSym)), Ai, nnz);
683 :     } else {
684 :     Bi = INTEGER(GET_SLOT(val, Matrix_iSym));
685 :     Bp = INTEGER(GET_SLOT(val, Matrix_pSym));
686 :     for (j = 0; j <= ncb; j++) Bp[j] *= nr;
687 :     for (j = 0; j < adims[2]; j++) {
688 :     for (ii = 0; ii < nr; ii++) {
689 :     Bi[j * nr + ii] = Ai[j] * nr + ii;
690 :     }
691 :     }
692 :     }
693 :     } else {
694 :     SET_SLOT(val, Matrix_pSym, allocVector(INTSXP, Dims[1] + 1));
695 :     Bp = INTEGER(GET_SLOT(val, Matrix_pSym));
696 :     SET_SLOT(val, Matrix_iSym, allocVector(INTSXP, nnz));
697 :     Bi = INTEGER(GET_SLOT(val, Matrix_iSym));
698 :     SET_SLOT(val, Matrix_xSym, allocVector(REALSXP, nnz));
699 :     Bx = REAL(GET_SLOT(val, Matrix_xSym));
700 :    
701 :     Bp[0] = 0;
702 :     for (j = 0; j < ncb; j++) { /* Column blocks of A */
703 :     int i, i1 = Ap[j], i2 = Ap[j + 1], jj;
704 :     int nzbc = (i2 - i1) * nr; /* No. of non-zeroes in B column */
705 :    
706 :     for (jj = 0; jj < nc; jj++) { /* column within blocks */
707 :     int jb = nc * j + jj; /* Column number in B */
708 :    
709 :     Bp[jb] = i1 * sz + jj * nzbc;
710 :     for (i = i1; i < i2; i++) { /* index in Ai and Ax */
711 :     for (ii = 0; ii < adims[0]; ii++) { /* row within blocks */
712 :     int ind = ii + (i - i1) * nr + Bp[jb];
713 :    
714 :     Bi[ind] = Ai[i] * sz + jj * nzbc + ii;
715 :     Bx[ind] = Ax[i * sz + jj * nc + ii];
716 :     }
717 :     }
718 :     }
719 :     }
720 :     }
721 :     UNPROTECT(1);
722 :     return val;
723 :     }
724 : bates 536
725 :     SEXP dgBCMatrix_to_dgTMatrix(SEXP A)
726 :     {
727 :     SEXP val = PROTECT(NEW_OBJECT(MAKE_CLASS("dgTMatrix"))),
728 :     ApP = GET_SLOT(A, Matrix_pSym),
729 :     AxP = GET_SLOT(A, Matrix_xSym);
730 :     int *Ai = INTEGER(GET_SLOT(A, Matrix_iSym)), *Ap = INTEGER(ApP),
731 :     *bdims = INTEGER(GET_SLOT(val, Matrix_DimSym)),
732 :     *adims = INTEGER(getAttrib(AxP, R_DimSymbol)),
733 :     i, j, k, kk, ncb = length(ApP) - 1, nnz = length(AxP), nrb;
734 :     int *Aj = expand_column_pointers(ncb, Ap, Calloc(nnz, int)),
735 :     *Bi = INTEGER(ALLOC_SLOT(val, Matrix_iSym, INTSXP, nnz)),
736 :     *Bj = INTEGER(ALLOC_SLOT(val, Matrix_jSym, INTSXP, nnz)),
737 :     nblk = adims[2], nc = adims[1], nr = adims[0];
738 :     int sz = nc * nr;
739 :     int *ai = Calloc(sz, int), *aj = Calloc(sz, int);
740 :     double *Ax = REAL(AxP),
741 :     *Bx = REAL(ALLOC_SLOT(val, Matrix_xSym, REALSXP, nnz));
742 :    
743 : bates 556 Memcpy(Bx, Ax, nnz); /* x slot stays as is but w/o dim attribute */
744 : bates 536
745 : bates 556 bdims[1] = ncb * adims[1];
746 :     /* find number of row blocks */
747 : bates 536 for (j = 0, nrb = -1; j < adims[2]; j++) if (Ai[j] > nrb) nrb = Ai[j];
748 :     bdims[0] = (nrb + 1) * adims[0]; /* +1 because of 0-based indices */
749 :    
750 :     for (j = 0; j < nc; j++) { /* arrays of inner indices */
751 :     for (i = 0; i < nr; i++) {
752 :     int ind = j * nc + i;
753 :     ai[ind] = i;
754 :     aj[ind] = j;
755 :     }
756 :     }
757 :     for (i = 0, k = 0; k < nblk; k++) {
758 :     for (kk = 0; kk < sz; kk++) {
759 :     Bi[k * sz + kk] = Ai[k] * nr + ai[kk];
760 :     Bj[k * sz + kk] = Aj[k] * nc + aj[kk];
761 :     }
762 :     }
763 :    
764 :     Free(Aj); Free(ai); Free(aj);
765 :     UNPROTECT(1);
766 :     return val;
767 :     }

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