File: magma_dgeisai_tools.cpp

package info (click to toggle)
magma 2.9.0%2Bds-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 83,212 kB
  • sloc: cpp: 709,115; fortran: 121,916; ansic: 32,343; python: 25,603; f90: 15,208; makefile: 942; xml: 253; csh: 232; sh: 203; perl: 104
file content (557 lines) | stat: -rw-r--r-- 14,988 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
    -- MAGMA (version 2.9.0) --
       Univ. of Tennessee, Knoxville
       Univ. of California, Berkeley
       Univ. of Colorado, Denver
       @date January 2025

       @generated from sparse/control/magma_zgeisai_tools.cpp, normal z -> d, Wed Jan 22 14:42:31 2025
       @author Hartwig Anzt

*/

#include "magmasparse_internal.h"

#define WARP_SIZE 32


/***************************************************************************//**
    Purpose
    -------
    Takes a sparse matrix and generates
        * an array containing the sizes of the different systems
        * an array containing the indices with the locations in the sparse
          matrix where the data comes from and goes back to
        * an array containing all the sparse triangular systems
            - padded with zeros to size 32x32
        * an array containing the RHS

    Arguments
    ---------


    @param[in]
    uplotype    magma_uplo_t
                lower or upper triangular

    @param[in]
    transtype   magma_trans_t
                possibility for transposed matrix

    @param[in]
    diagtype    magma_diag_t
                unit diagonal or not

    @param[in]
    L           magma_d_matrix
                Matrix in CSR format

    @param[in]
    LC          magma_d_matrix
                same matrix, also CSR, but col-major

    @param[in,out]
    sizes       magma_int_t*
                Number of Elements that are replaced.

    @param[in,out]
    locations   magma_int_t*
                Array indicating the locations.

    @param[in,out]
    trisystems  double*
                trisystems

    @param[in,out]
    rhs         double*
                right-hand sides

    @param[in]
    queue       magma_queue_t
                Queue to execute in.

    @ingroup magmasparse_daux
    ********************************************************************/

extern "C" magma_int_t
magma_dmprepare_batched(
    magma_uplo_t uplotype,
    magma_trans_t transtype,
    magma_diag_t diagtype,
    magma_d_matrix L,
    magma_d_matrix LC,
    magma_index_t *sizes,
    magma_index_t *locations,
    double *trisystems,
    double *rhs,
    magma_queue_t queue )
{
    magma_int_t info = 0;

    magma_int_t warpsize = WARP_SIZE;

    #pragma omp parallel for
    for( magma_int_t i=0; i<L.num_rows*warpsize*warpsize; i++ ){
        trisystems[i] = MAGMA_D_ZERO;
    }
    #pragma omp parallel for
    for( magma_int_t i=0; i<L.num_rows*warpsize; i++ ){
        rhs[i] = MAGMA_D_ZERO;
        locations[i] = 0;
    }

    if( uplotype == MagmaLower ){
        // fill sizes and rhs and first column of trisystems
        #pragma omp parallel for
        for( magma_int_t i=0; i<L.num_rows; i++ ){
            magma_int_t size = 0;
            for( magma_int_t j=LC.row[i]; j<LC.row[i+1]; j++ ){
                locations[ i*warpsize + size ] = LC.col[j];
                //trisystems[ i*warpsize*warpsize + size ] = LC.val[j];
                size++;
            }
            sizes[ i ] = size;
            rhs[ i*warpsize ] = MAGMA_D_ONE;
        }
    } else {
        // fill sizes and rhs and first column of trisystems
        #pragma omp parallel for
        for( magma_int_t i=0; i<L.num_rows; i++ ){
            magma_int_t size = 0;
            for( magma_int_t j=LC.row[i]; j<LC.row[i+1]; j++ ){
                locations[ i*warpsize + size ] = LC.col[j];
                //trisystems[ i*warpsize*warpsize + size ] = LC.val[j];
                size++;
            }
            sizes[ i ] = size;
            rhs[ i*(warpsize)+sizes[i]-1 ] = MAGMA_D_ONE;
        }
    }

    // fill rest of trisystems
    #pragma omp parallel for
    for( magma_int_t i=0; i<L.num_rows; i++ ){
        for( magma_int_t j=0; j<sizes[ i ]; j++ ){// no need for first
            magma_int_t k = L.row[ locations[ j+i*warpsize ] ];
            magma_int_t l = i*warpsize;
            magma_int_t idx = 0;
            while( k < L.row[ locations[ j+i*warpsize ]+1 ] && l < (i+1)*warpsize ){ // stop once this column is done
                // printf("k:%d<%d l:%d<%d\n",k,L.row[ locations[ j ]+1 ],l,(i+1)*warpsize );

                if( locations[ l ] == L.col[k] ){ //match
                    // printf("match: %d = %d insert %.2f at %d\n",locations[ l ], L.col[k], L.val[ k ], loc);
                    trisystems[ i*warpsize*warpsize + j*warpsize + idx ]
                                                            = L.val[ k ];
                    k++;
                    l++;
                    idx++;
                } else if( L.col[k] < locations[ l ] ){// need to check next element
                    // printf("increment k\n");
                    k++;
                } else { // element does not exist, i.e. l < L.col[k]
                    // printf("increment l\n");
                    l++; // check next elment in the sparsity pattern
                    idx++; // leave this element equal zero
                }
            }
        }
    }

    return info;
}


/***************************************************************************//**
    Purpose
    -------
    Does all triangular solves

    Arguments
    ---------


    @param[in]
    uplotype    magma_uplo_t
                lower or upper triangular

    @param[in]
    transtype   magma_trans_t
                possibility for transposed matrix

    @param[in]
    diagtype    magma_diag_t
                unit diagonal or not

    @param[in]
    L           magma_d_matrix
                Matrix in CSR format

    @param[in]
    LC          magma_d_matrix
                same matrix, also CSR, but col-major

    @param[out]
    sizes       magma_int_t*
                Number of Elements that are replaced.

    @param[out]
    locations   magma_int_t*
                Array indicating the locations.

    @param[out]
    trisystems  double*
                trisystems

    @param[out]
    rhs         double*
                right-hand sides

    @param[in]
    queue       magma_queue_t
                Queue to execute in.

    @ingroup magmasparse_daux
    ********************************************************************/

extern "C" magma_int_t
magma_dmtrisolve_batched(
    magma_uplo_t uplotype,
    magma_trans_t transtype,
    magma_diag_t diagtype,
    magma_d_matrix L,
    magma_d_matrix LC,
    magma_index_t *sizes,
    magma_index_t *locations,
    double *trisystems,
    double *rhs,
    magma_queue_t queue )
{
    magma_int_t info = 0;

    magma_int_t warpsize = WARP_SIZE;
    magma_int_t ione     = 1;

    #pragma omp parallel for
    for(magma_int_t i=0; i<L.num_rows; i++){
        blasf77_dtrsv( lapack_uplo_const(uplotype),
                        lapack_trans_const(transtype),
                        lapack_diag_const(diagtype),
                           (magma_int_t*)&sizes[i],
                           &trisystems[i*warpsize*warpsize], &warpsize,
                           &rhs[i*warpsize], &ione );
    }

    return info;
}


/***************************************************************************//**
    Purpose
    -------
    Inserts the values into the preconditioner matrix

    Arguments
    ---------


    @param[in]
    uplotype    magma_uplo_t
                lower or upper triangular

    @param[in]
    transtype   magma_trans_t
                possibility for transposed matrix

    @param[in]
    diagtype    magma_diag_t
                unit diagonal or not

    @param[in,out]
    M           magma_d_matrix*
                SPAI preconditioner CSR col-major

    @param[out]
    sizes       magma_int_t*
                Number of Elements that are replaced.

    @param[out]
    locations   magma_int_t*
                Array indicating the locations.

    @param[out]
    trisystems  double*
                trisystems

    @param[out]
    rhs         double*
                right-hand sides

    @param[in]
    queue       magma_queue_t
                Queue to execute in.

    @ingroup magmasparse_daux
    ********************************************************************/

extern "C" magma_int_t
magma_dmbackinsert_batched(
    magma_uplo_t uplotype,
    magma_trans_t transtype,
    magma_diag_t diagtype,
    magma_d_matrix *M,
    magma_index_t *sizes,
    magma_index_t *locations,
    double *trisystems,
    double *rhs,
    magma_queue_t queue )
{
    magma_int_t info = 0;

    magma_int_t warpsize = WARP_SIZE;

    #pragma omp parallel for
    for(magma_int_t i=0; i<M->num_rows; i++){
        for(magma_int_t j=0; j<sizes[i]; j++){
            M->val[M->row[i]+j] = rhs[i*warpsize+j];
        }
    }

    return info;
}


/***************************************************************************//**
    Purpose
    -------
    Checks for a matrix whether the batched ISAI works for a given
    thread-block size

    Arguments
    ---------

    @param[in]
    A           magma_d_matrix
                system matrix

    @param[in]
    batchsize   magma_int_t
                Size of the batch (GPU thread block).

    @param[out]
    maxsize     magma_int_t*
                maximum A(:,i) and A(i,:).

    @param[in]
    queue       magma_queue_t
                Queue to execute in.

    @ingroup magmasparse_daux
    ********************************************************************/

extern "C" magma_int_t
magma_dmiluspai_sizecheck(
    magma_d_matrix A,
    magma_index_t batchsize,
    magma_index_t *maxsize,
    magma_queue_t queue )
{
    magma_int_t info = 0;
    magma_d_matrix AT={Magma_CSR};

    CHECK( magma_dmtranspose( A, &AT, queue ) );
    *maxsize = 0;
    for( magma_int_t i=0; i<A.num_rows; i++ ){
        if( A.row[i+1] - A.row[i] > *maxsize ){
            *maxsize = A.row[i+1] - A.row[i];
        }
    }
    for( magma_int_t i=0; i<AT.num_rows; i++ ){
        if( AT.row[i+1] - AT.row[i] > *maxsize ){
            *maxsize = AT.row[i+1] - AT.row[i];
        }
    }

    if( *maxsize > batchsize ){
        info = -(*maxsize - batchsize);
    }

    magma_dmfree( &AT, queue );

cleanup:
    return info;
}


/***************************************************************************//**
    Purpose
    -------
    Generates a block-diagonal sparsity pattern with block-size bs

    Arguments
    ---------

    @param[in]
    n           magma_int_t
                Size of the matrix.

    @param[in]
    bs          magma_int_t
                Size of the diagonal blocks.

    @param[in]
    offs        magma_int_t
                Size of the first diagonal block.

    @param[in]
    uplotype    magma_uplo_t
                lower or upper triangular

    @param[in,out]
    A           magma_d_matrix*
                Generated sparsity pattern matrix.

    @param[in]
    queue       magma_queue_t
                Queue to execute in.

    @ingroup magmasparse_daux
    ********************************************************************/

extern "C" magma_int_t
magma_dmisai_blockstruct(
    magma_int_t n,
    magma_int_t bs,
    magma_int_t offs,
    magma_uplo_t uplotype,
    magma_d_matrix *A,
    magma_queue_t queue )
{
    magma_int_t info = 0;

    magma_int_t i, k, j;

    A->val = NULL;
    A->col = NULL;
    A->row = NULL;
    A->rowidx = NULL;
    A->blockinfo = NULL;
    A->diag = NULL;
    A->dval = NULL;
    A->dcol = NULL;
    A->drow = NULL;
    A->drowidx = NULL;
    A->ddiag = NULL;
    A->num_rows = n;
    A->num_cols = n;
    A->nnz = n*max(bs,offs);
    A->memory_location = Magma_CPU;
    A->storage_type = Magma_CSR;

    CHECK( magma_dmalloc_cpu( &A->val, A->nnz ));
    CHECK( magma_index_malloc_cpu( &A->row, A->num_rows+1 ));
    CHECK( magma_index_malloc_cpu( &A->col, A->nnz ));

    // default: every row has bs elements
    #pragma omp parallel for
    for( i=0; i<offs; i++ ){
        A->row[i] = offs * i;
    }
    // default: every other row has bs elements
    #pragma omp parallel for
    for( i=offs; i<n+1; i++ ){
        A->row[i] = bs * (i-offs)+offs*offs;
    }

    if( uplotype == MagmaLower ){
        // make the first block different
        for( i=0; i<offs; i+=offs ){
            for( k=i; k<min(A->num_rows,i+offs); k++ ){
                int c = i;
                for( j=A->row[k]; j<A->row[k+1]; j++ ){
                    if( c<n ){
                        A->col[j] = c;
                        if( c <= k){
                            A->val[j] = MAGMA_D_ONE;
                        } else {
                            A->val[j] = MAGMA_D_ZERO;
                        }
                        c++;
                    } else {
                        A->col[j] = 0;
                        A->val[j] = MAGMA_D_ZERO;
                        c++;
                    }
                }
            }
        }
        // now the rest
        for( i=offs; i<n; i+=bs ){
            for( k=i; k<min(A->num_rows,i+bs); k++ ){
                int c = i;
                for( j=A->row[k]; j<A->row[k+1]; j++ ){
                    if( c<n ){
                        A->col[j] = c;
                        if( c <= k){
                            A->val[j] = MAGMA_D_ONE;
                        } else {
                            A->val[j] = MAGMA_D_ZERO;
                        }
                        c++;
                    } else {
                        A->col[j] = 0;
                        A->val[j] = MAGMA_D_ZERO;
                        c++;
                    }
                }
            }
        }
    } else if( uplotype == MagmaUpper ){
        // make the first block different
        for( i=0; i<offs; i+=offs ){
            for( k=i; k<min(A->num_rows,i+offs); k++ ){
                int c = i;
                for( j=A->row[k]; j<A->row[k+1]; j++ ){
                    if( c<n ){
                        A->col[j] = c;
                        if( c >= k){
                            A->val[j] = MAGMA_D_ONE;
                        } else {
                            A->val[j] = MAGMA_D_ZERO;
                        }
                        c++;
                    } else {
                        A->col[j] = 0;
                        A->val[j] = MAGMA_D_ZERO;
                        c++;
                    }
                }
            }
        }
        // now the rest
        for( i=offs; i<n; i+=bs ){
            for( k=i; k<min(A->num_rows,i+bs); k++ ){
                int c = i;
                for( j=A->row[k]; j<A->row[k+1]; j++ ){
                    if( c<n ){
                        A->col[j] = c;
                        if( c >= k){
                            A->val[j] = MAGMA_D_ONE;
                        } else {
                            A->val[j] = MAGMA_D_ZERO;
                        }
                        c++;
                    } else {
                        A->col[j] = 0;
                        A->val[j] = MAGMA_D_ZERO;
                        c++;
                    }
                }
            }
        }
    }

    CHECK( magma_dmcsrcompressor( A, queue ) );

cleanup:
    return info;
}