~ubuntu-branches/ubuntu/karmic/hypre/karmic

« back to all changes in this revision

Viewing changes to src/FEI_mv/SuperLU/SRC/ssp_blas2.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam C. Powell, IV
  • Date: 2009-03-20 11:40:12 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090320114012-132h6ok9w2r6o609
Tags: 2.4.0b-2
Rebuild against new openmpi.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * -- SuperLU routine (version 3.0) --
 
4
 * Univ. of California Berkeley, Xerox Palo Alto Research Center,
 
5
 * and Lawrence Berkeley National Lab.
 
6
 * October 15, 2003
 
7
 *
 
8
 */
 
9
/*
 
10
 * File name:           ssp_blas2.c
 
11
 * Purpose:             Sparse BLAS 2, using some dense BLAS 2 operations.
 
12
 */
 
13
 
 
14
#include "slu_sdefs.h"
 
15
 
 
16
/* 
 
17
 * Function prototypes 
 
18
 */
 
19
void susolve(int, int, float*, float*);
 
20
void slsolve(int, int, float*, float*);
 
21
void smatvec(int, int, int, float*, float*, float*);
 
22
 
 
23
 
 
24
int
 
25
sp_strsv(char *uplo, char *trans, char *diag, SuperMatrix *L, 
 
26
         SuperMatrix *U, float *x, SuperLUStat_t *stat, int *info)
 
27
{
 
28
/*
 
29
 *   Purpose
 
30
 *   =======
 
31
 *
 
32
 *   sp_strsv() solves one of the systems of equations   
 
33
 *       A*x = b,   or   A'*x = b,
 
34
 *   where b and x are n element vectors and A is a sparse unit , or   
 
35
 *   non-unit, upper or lower triangular matrix.   
 
36
 *   No test for singularity or near-singularity is included in this   
 
37
 *   routine. Such tests must be performed before calling this routine.   
 
38
 *
 
39
 *   Parameters   
 
40
 *   ==========   
 
41
 *
 
42
 *   uplo   - (input) char*
 
43
 *            On entry, uplo specifies whether the matrix is an upper or   
 
44
 *             lower triangular matrix as follows:   
 
45
 *                uplo = 'U' or 'u'   A is an upper triangular matrix.   
 
46
 *                uplo = 'L' or 'l'   A is a lower triangular matrix.   
 
47
 *
 
48
 *   trans  - (input) char*
 
49
 *             On entry, trans specifies the equations to be solved as   
 
50
 *             follows:   
 
51
 *                trans = 'N' or 'n'   A*x = b.   
 
52
 *                trans = 'T' or 't'   A'*x = b.
 
53
 *                trans = 'C' or 'c'   A'*x = b.   
 
54
 *
 
55
 *   diag   - (input) char*
 
56
 *             On entry, diag specifies whether or not A is unit   
 
57
 *             triangular as follows:   
 
58
 *                diag = 'U' or 'u'   A is assumed to be unit triangular.   
 
59
 *                diag = 'N' or 'n'   A is not assumed to be unit   
 
60
 *                                    triangular.   
 
61
 *           
 
62
 *   L       - (input) SuperMatrix*
 
63
 *             The factor L from the factorization Pr*A*Pc=L*U. Use
 
64
 *             compressed row subscripts storage for supernodes,
 
65
 *             i.e., L has types: Stype = SC, Dtype = SLU_S, Mtype = TRLU.
 
66
 *
 
67
 *   U       - (input) SuperMatrix*
 
68
 *              The factor U from the factorization Pr*A*Pc=L*U.
 
69
 *              U has types: Stype = NC, Dtype = SLU_S, Mtype = TRU.
 
70
 *    
 
71
 *   x       - (input/output) float*
 
72
 *             Before entry, the incremented array X must contain the n   
 
73
 *             element right-hand side vector b. On exit, X is overwritten 
 
74
 *             with the solution vector x.
 
75
 *
 
76
 *   info    - (output) int*
 
77
 *             If *info = -i, the i-th argument had an illegal value.
 
78
 *
 
79
 */
 
80
#ifdef _CRAY
 
81
    _fcd ftcs1 = _cptofcd("L", strlen("L")),
 
82
         ftcs2 = _cptofcd("N", strlen("N")),
 
83
         ftcs3 = _cptofcd("U", strlen("U"));
 
84
#endif
 
85
    SCformat *Lstore;
 
86
    NCformat *Ustore;
 
87
    float   *Lval, *Uval;
 
88
    int incx = 1, incy = 1;
 
89
    float alpha = 1.0, beta = 1.0;
 
90
    int nrow;
 
91
    int fsupc, nsupr, nsupc, luptr, istart, irow;
 
92
    int i, k, iptr, jcol;
 
93
    float *work;
 
94
    flops_t solve_ops;
 
95
 
 
96
    /* Test the input parameters */
 
97
    *info = 0;
 
98
    if ( !lsame_(uplo,"L") && !lsame_(uplo, "U") ) *info = -1;
 
99
    else if ( !lsame_(trans, "N") && !lsame_(trans, "T") && 
 
100
              !lsame_(trans, "C")) *info = -2;
 
101
    else if ( !lsame_(diag, "U") && !lsame_(diag, "N") ) *info = -3;
 
102
    else if ( L->nrow != L->ncol || L->nrow < 0 ) *info = -4;
 
103
    else if ( U->nrow != U->ncol || U->nrow < 0 ) *info = -5;
 
104
    if ( *info ) {
 
105
        i = -(*info);
 
106
        xerbla_("sp_strsv", &i);
 
107
        return 0;
 
108
    }
 
109
 
 
110
    Lstore = L->Store;
 
111
    Lval = Lstore->nzval;
 
112
    Ustore = U->Store;
 
113
    Uval = Ustore->nzval;
 
114
    solve_ops = 0;
 
115
 
 
116
    if ( !(work = floatCalloc(L->nrow)) )
 
117
        ABORT("Malloc fails for work in sp_strsv().");
 
118
    
 
119
    if ( lsame_(trans, "N") ) { /* Form x := inv(A)*x. */
 
120
        
 
121
        if ( lsame_(uplo, "L") ) {
 
122
            /* Form x := inv(L)*x */
 
123
            if ( L->nrow == 0 ) return 0; /* Quick return */
 
124
            
 
125
            for (k = 0; k <= Lstore->nsuper; k++) {
 
126
                fsupc = L_FST_SUPC(k);
 
127
                istart = L_SUB_START(fsupc);
 
128
                nsupr = L_SUB_START(fsupc+1) - istart;
 
129
                nsupc = L_FST_SUPC(k+1) - fsupc;
 
130
                luptr = L_NZ_START(fsupc);
 
131
                nrow = nsupr - nsupc;
 
132
 
 
133
                solve_ops += nsupc * (nsupc - 1);
 
134
                solve_ops += 2 * nrow * nsupc;
 
135
 
 
136
                if ( nsupc == 1 ) {
 
137
                    for (iptr=istart+1; iptr < L_SUB_START(fsupc+1); ++iptr) {
 
138
                        irow = L_SUB(iptr);
 
139
                        ++luptr;
 
140
                        x[irow] -= x[fsupc] * Lval[luptr];
 
141
                    }
 
142
                } else {
 
143
#ifdef USE_VENDOR_BLAS
 
144
#ifdef _CRAY
 
145
                    STRSV(ftcs1, ftcs2, ftcs3, &nsupc, &Lval[luptr], &nsupr,
 
146
                        &x[fsupc], &incx);
 
147
                
 
148
                    SGEMV(ftcs2, &nrow, &nsupc, &alpha, &Lval[luptr+nsupc], 
 
149
                        &nsupr, &x[fsupc], &incx, &beta, &work[0], &incy);
 
150
#else
 
151
                    strsv_("L", "N", "U", &nsupc, &Lval[luptr], &nsupr,
 
152
                        &x[fsupc], &incx);
 
153
                
 
154
                    sgemv_("N", &nrow, &nsupc, &alpha, &Lval[luptr+nsupc], 
 
155
                        &nsupr, &x[fsupc], &incx, &beta, &work[0], &incy);
 
156
#endif
 
157
#else
 
158
                    slsolve ( nsupr, nsupc, &Lval[luptr], &x[fsupc]);
 
159
                
 
160
                    smatvec ( nsupr, nsupr-nsupc, nsupc, &Lval[luptr+nsupc],
 
161
                             &x[fsupc], &work[0] );
 
162
#endif          
 
163
                
 
164
                    iptr = istart + nsupc;
 
165
                    for (i = 0; i < nrow; ++i, ++iptr) {
 
166
                        irow = L_SUB(iptr);
 
167
                        x[irow] -= work[i];     /* Scatter */
 
168
                        work[i] = 0.0;
 
169
 
 
170
                    }
 
171
                }
 
172
            } /* for k ... */
 
173
            
 
174
        } else {
 
175
            /* Form x := inv(U)*x */
 
176
            
 
177
            if ( U->nrow == 0 ) return 0; /* Quick return */
 
178
            
 
179
            for (k = Lstore->nsuper; k >= 0; k--) {
 
180
                fsupc = L_FST_SUPC(k);
 
181
                nsupr = L_SUB_START(fsupc+1) - L_SUB_START(fsupc);
 
182
                nsupc = L_FST_SUPC(k+1) - fsupc;
 
183
                luptr = L_NZ_START(fsupc);
 
184
                
 
185
                solve_ops += nsupc * (nsupc + 1);
 
186
 
 
187
                if ( nsupc == 1 ) {
 
188
                    x[fsupc] /= Lval[luptr];
 
189
                    for (i = U_NZ_START(fsupc); i < U_NZ_START(fsupc+1); ++i) {
 
190
                        irow = U_SUB(i);
 
191
                        x[irow] -= x[fsupc] * Uval[i];
 
192
                    }
 
193
                } else {
 
194
#ifdef USE_VENDOR_BLAS
 
195
#ifdef _CRAY
 
196
                    STRSV(ftcs3, ftcs2, ftcs2, &nsupc, &Lval[luptr], &nsupr,
 
197
                       &x[fsupc], &incx);
 
198
#else
 
199
                    strsv_("U", "N", "N", &nsupc, &Lval[luptr], &nsupr,
 
200
                           &x[fsupc], &incx);
 
201
#endif
 
202
#else           
 
203
                    susolve ( nsupr, nsupc, &Lval[luptr], &x[fsupc] );
 
204
#endif          
 
205
 
 
206
                    for (jcol = fsupc; jcol < L_FST_SUPC(k+1); jcol++) {
 
207
                        solve_ops += 2*(U_NZ_START(jcol+1) - U_NZ_START(jcol));
 
208
                        for (i = U_NZ_START(jcol); i < U_NZ_START(jcol+1); 
 
209
                                i++) {
 
210
                            irow = U_SUB(i);
 
211
                            x[irow] -= x[jcol] * Uval[i];
 
212
                        }
 
213
                    }
 
214
                }
 
215
            } /* for k ... */
 
216
            
 
217
        }
 
218
    } else { /* Form x := inv(A')*x */
 
219
        
 
220
        if ( lsame_(uplo, "L") ) {
 
221
            /* Form x := inv(L')*x */
 
222
            if ( L->nrow == 0 ) return 0; /* Quick return */
 
223
            
 
224
            for (k = Lstore->nsuper; k >= 0; --k) {
 
225
                fsupc = L_FST_SUPC(k);
 
226
                istart = L_SUB_START(fsupc);
 
227
                nsupr = L_SUB_START(fsupc+1) - istart;
 
228
                nsupc = L_FST_SUPC(k+1) - fsupc;
 
229
                luptr = L_NZ_START(fsupc);
 
230
 
 
231
                solve_ops += 2 * (nsupr - nsupc) * nsupc;
 
232
 
 
233
                for (jcol = fsupc; jcol < L_FST_SUPC(k+1); jcol++) {
 
234
                    iptr = istart + nsupc;
 
235
                    for (i = L_NZ_START(jcol) + nsupc; 
 
236
                                i < L_NZ_START(jcol+1); i++) {
 
237
                        irow = L_SUB(iptr);
 
238
                        x[jcol] -= x[irow] * Lval[i];
 
239
                        iptr++;
 
240
                    }
 
241
                }
 
242
                
 
243
                if ( nsupc > 1 ) {
 
244
                    solve_ops += nsupc * (nsupc - 1);
 
245
#ifdef _CRAY
 
246
                    ftcs1 = _cptofcd("L", strlen("L"));
 
247
                    ftcs2 = _cptofcd("T", strlen("T"));
 
248
                    ftcs3 = _cptofcd("U", strlen("U"));
 
249
                    STRSV(ftcs1, ftcs2, ftcs3, &nsupc, &Lval[luptr], &nsupr,
 
250
                        &x[fsupc], &incx);
 
251
#else
 
252
                    strsv_("L", "T", "U", &nsupc, &Lval[luptr], &nsupr,
 
253
                        &x[fsupc], &incx);
 
254
#endif
 
255
                }
 
256
            }
 
257
        } else {
 
258
            /* Form x := inv(U')*x */
 
259
            if ( U->nrow == 0 ) return 0; /* Quick return */
 
260
            
 
261
            for (k = 0; k <= Lstore->nsuper; k++) {
 
262
                fsupc = L_FST_SUPC(k);
 
263
                nsupr = L_SUB_START(fsupc+1) - L_SUB_START(fsupc);
 
264
                nsupc = L_FST_SUPC(k+1) - fsupc;
 
265
                luptr = L_NZ_START(fsupc);
 
266
 
 
267
                for (jcol = fsupc; jcol < L_FST_SUPC(k+1); jcol++) {
 
268
                    solve_ops += 2*(U_NZ_START(jcol+1) - U_NZ_START(jcol));
 
269
                    for (i = U_NZ_START(jcol); i < U_NZ_START(jcol+1); i++) {
 
270
                        irow = U_SUB(i);
 
271
                        x[jcol] -= x[irow] * Uval[i];
 
272
                    }
 
273
                }
 
274
 
 
275
                solve_ops += nsupc * (nsupc + 1);
 
276
 
 
277
                if ( nsupc == 1 ) {
 
278
                    x[fsupc] /= Lval[luptr];
 
279
                } else {
 
280
#ifdef _CRAY
 
281
                    ftcs1 = _cptofcd("U", strlen("U"));
 
282
                    ftcs2 = _cptofcd("T", strlen("T"));
 
283
                    ftcs3 = _cptofcd("N", strlen("N"));
 
284
                    STRSV( ftcs1, ftcs2, ftcs3, &nsupc, &Lval[luptr], &nsupr,
 
285
                            &x[fsupc], &incx);
 
286
#else
 
287
                    strsv_("U", "T", "N", &nsupc, &Lval[luptr], &nsupr,
 
288
                            &x[fsupc], &incx);
 
289
#endif
 
290
                }
 
291
            } /* for k ... */
 
292
        }
 
293
    }
 
294
 
 
295
    stat->ops[SOLVE] += solve_ops;
 
296
    SUPERLU_FREE(work);
 
297
    return 0;
 
298
}
 
299
 
 
300
 
 
301
 
 
302
 
 
303
int
 
304
sp_sgemv(char *trans, float alpha, SuperMatrix *A, float *x, 
 
305
         int incx, float beta, float *y, int incy)
 
306
{
 
307
/*  Purpose   
 
308
    =======   
 
309
 
 
310
    sp_sgemv()  performs one of the matrix-vector operations   
 
311
       y := alpha*A*x + beta*y,   or   y := alpha*A'*x + beta*y,   
 
312
    where alpha and beta are scalars, x and y are vectors and A is a
 
313
    sparse A->nrow by A->ncol matrix.   
 
314
 
 
315
    Parameters   
 
316
    ==========   
 
317
 
 
318
    TRANS  - (input) char*
 
319
             On entry, TRANS specifies the operation to be performed as   
 
320
             follows:   
 
321
                TRANS = 'N' or 'n'   y := alpha*A*x + beta*y.   
 
322
                TRANS = 'T' or 't'   y := alpha*A'*x + beta*y.   
 
323
                TRANS = 'C' or 'c'   y := alpha*A'*x + beta*y.   
 
324
 
 
325
    ALPHA  - (input) float
 
326
             On entry, ALPHA specifies the scalar alpha.   
 
327
 
 
328
    A      - (input) SuperMatrix*
 
329
             Matrix A with a sparse format, of dimension (A->nrow, A->ncol).
 
330
             Currently, the type of A can be:
 
331
                 Stype = NC or NCP; Dtype = SLU_S; Mtype = GE. 
 
332
             In the future, more general A can be handled.
 
333
 
 
334
    X      - (input) float*, array of DIMENSION at least   
 
335
             ( 1 + ( n - 1 )*abs( INCX ) ) when TRANS = 'N' or 'n'   
 
336
             and at least   
 
337
             ( 1 + ( m - 1 )*abs( INCX ) ) otherwise.   
 
338
             Before entry, the incremented array X must contain the   
 
339
             vector x.   
 
340
 
 
341
    INCX   - (input) int
 
342
             On entry, INCX specifies the increment for the elements of   
 
343
             X. INCX must not be zero.   
 
344
 
 
345
    BETA   - (input) float
 
346
             On entry, BETA specifies the scalar beta. When BETA is   
 
347
             supplied as zero then Y need not be set on input.   
 
348
 
 
349
    Y      - (output) float*,  array of DIMENSION at least   
 
350
             ( 1 + ( m - 1 )*abs( INCY ) ) when TRANS = 'N' or 'n'   
 
351
             and at least   
 
352
             ( 1 + ( n - 1 )*abs( INCY ) ) otherwise.   
 
353
             Before entry with BETA non-zero, the incremented array Y   
 
354
             must contain the vector y. On exit, Y is overwritten by the 
 
355
             updated vector y.
 
356
             
 
357
    INCY   - (input) int
 
358
             On entry, INCY specifies the increment for the elements of   
 
359
             Y. INCY must not be zero.   
 
360
 
 
361
    ==== Sparse Level 2 Blas routine.   
 
362
*/
 
363
 
 
364
    /* Local variables */
 
365
    NCformat *Astore;
 
366
    float   *Aval;
 
367
    int info;
 
368
    float temp;
 
369
    int lenx, leny, i, j, irow;
 
370
    int iy, jx, jy, kx, ky;
 
371
    int notran;
 
372
 
 
373
    notran = lsame_(trans, "N");
 
374
    Astore = A->Store;
 
375
    Aval = Astore->nzval;
 
376
    
 
377
    /* Test the input parameters */
 
378
    info = 0;
 
379
    if ( !notran && !lsame_(trans, "T") && !lsame_(trans, "C")) info = 1;
 
380
    else if ( A->nrow < 0 || A->ncol < 0 ) info = 3;
 
381
    else if (incx == 0) info = 5;
 
382
    else if (incy == 0) info = 8;
 
383
    if (info != 0) {
 
384
        xerbla_("sp_sgemv ", &info);
 
385
        return 0;
 
386
    }
 
387
 
 
388
    /* Quick return if possible. */
 
389
    if (A->nrow == 0 || A->ncol == 0 || (alpha == 0. && beta == 1.))
 
390
        return 0;
 
391
 
 
392
    /* Set  LENX  and  LENY, the lengths of the vectors x and y, and set 
 
393
       up the start points in  X  and  Y. */
 
394
    if (lsame_(trans, "N")) {
 
395
        lenx = A->ncol;
 
396
        leny = A->nrow;
 
397
    } else {
 
398
        lenx = A->nrow;
 
399
        leny = A->ncol;
 
400
    }
 
401
    if (incx > 0) kx = 0;
 
402
    else kx =  - (lenx - 1) * incx;
 
403
    if (incy > 0) ky = 0;
 
404
    else ky =  - (leny - 1) * incy;
 
405
 
 
406
    /* Start the operations. In this version the elements of A are   
 
407
       accessed sequentially with one pass through A. */
 
408
    /* First form  y := beta*y. */
 
409
    if (beta != 1.) {
 
410
        if (incy == 1) {
 
411
            if (beta == 0.)
 
412
                for (i = 0; i < leny; ++i) y[i] = 0.;
 
413
            else
 
414
                for (i = 0; i < leny; ++i) y[i] = beta * y[i];
 
415
        } else {
 
416
            iy = ky;
 
417
            if (beta == 0.)
 
418
                for (i = 0; i < leny; ++i) {
 
419
                    y[iy] = 0.;
 
420
                    iy += incy;
 
421
                }
 
422
            else
 
423
                for (i = 0; i < leny; ++i) {
 
424
                    y[iy] = beta * y[iy];
 
425
                    iy += incy;
 
426
                }
 
427
        }
 
428
    }
 
429
    
 
430
    if (alpha == 0.) return 0;
 
431
 
 
432
    if ( notran ) {
 
433
        /* Form  y := alpha*A*x + y. */
 
434
        jx = kx;
 
435
        if (incy == 1) {
 
436
            for (j = 0; j < A->ncol; ++j) {
 
437
                if (x[jx] != 0.) {
 
438
                    temp = alpha * x[jx];
 
439
                    for (i = Astore->colptr[j]; i < Astore->colptr[j+1]; ++i) {
 
440
                        irow = Astore->rowind[i];
 
441
                        y[irow] += temp * Aval[i];
 
442
                    }
 
443
                }
 
444
                jx += incx;
 
445
            }
 
446
        } else {
 
447
            ABORT("Not implemented.");
 
448
        }
 
449
    } else {
 
450
        /* Form  y := alpha*A'*x + y. */
 
451
        jy = ky;
 
452
        if (incx == 1) {
 
453
            for (j = 0; j < A->ncol; ++j) {
 
454
                temp = 0.;
 
455
                for (i = Astore->colptr[j]; i < Astore->colptr[j+1]; ++i) {
 
456
                    irow = Astore->rowind[i];
 
457
                    temp += Aval[i] * x[irow];
 
458
                }
 
459
                y[jy] += alpha * temp;
 
460
                jy += incy;
 
461
            }
 
462
        } else {
 
463
            ABORT("Not implemented.");
 
464
        }
 
465
    }
 
466
    return 0;
 
467
} /* sp_sgemv */
 
468
 
 
469
 
 
470