pzlansy.c 12.2 KB
Newer Older
1
/**
2 3
 *
 * @file pzlansy.c
4
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
5 6
 * @copyright 2009-2014 The University of Tennessee and The University of
 *                      Tennessee Research Foundation. All rights reserved.
Mathieu Faverge's avatar
Mathieu Faverge committed
7
 * @copyright 2012-2018 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
8
 *                      Univ. Bordeaux. All rights reserved.
9
 *
10
 ***
11
 *
12
 * @brief Chameleon zlansy parallel algorithm
13
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
14
 * @version 1.0.0
15
 * @comment This file has been automatically generated
16
 *          from Plasma 2.6.0 for CHAMELEON 1.0.0
17 18 19 20 21
 * @author Emmanuel Agullo
 * @author Mathieu Faverge
 * @date 2010-11-15
 * @precisions normal z -> c d s
 *
22
 */
23
//ALLOC_WS :  A->mb
Mathieu Faverge's avatar
Mathieu Faverge committed
24 25
//WS_ADD :  A->mb

26 27
#include <stdlib.h>
#include <math.h>
28
#include "control/common.h"
29

30 31
#define A( m, n )        A,     (m), (n)
#define W( desc, m, n ) (desc), (m), (n)
32

Mathieu Faverge's avatar
Mathieu Faverge committed
33 34 35 36 37
static inline void
chameleon_pzlansy_inf( cham_uplo_t uplo, CHAM_desc_t *A,
                       CHAM_desc_t *Wcol, CHAM_desc_t *Welt,
                       RUNTIME_option_t *options)
{
38
    int m, n;
Mathieu Faverge's avatar
Mathieu Faverge committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52
    int MT = A->mt;
    int NT = A->nt;
    int M  = A->m;
    int N  = A->n;
    int P  = Welt->p;
    int Q  = Welt->q;

    /**
     * Step 1:
     *  For j in [1,Q], Wcol(m, j) = reduce( A(m, j+k*Q) )
     */
    for(m = 0; m < MT; m++) {
        int nmin = ( uplo == ChamUpper ) ? m                      : 0;
        int nmax = ( uplo == ChamLower ) ? chameleon_min(m+1, NT) : NT;
53

Mathieu Faverge's avatar
Mathieu Faverge committed
54 55
        int tempmm = ( m == (MT-1) ) ? M - m * A->mb : A->mb;
        int ldam = BLKLDD( A, m );
56

Mathieu Faverge's avatar
Mathieu Faverge committed
57 58
        for(n = nmin; n < nmax; n++) {
            int tempnn = ( n == (NT-1) ) ? N - n * A->nb : A->nb;
59

Mathieu Faverge's avatar
Mathieu Faverge committed
60 61 62 63
            if ( n == m ) {
                INSERT_TASK_dzasum(
                    options,
                    ChamRowwise, uplo, tempmm, tempnn,
64
                    A(m, n), ldam, W( Wcol, m, n) );
Mathieu Faverge's avatar
Mathieu Faverge committed
65 66 67 68 69
            }
            else {
                INSERT_TASK_dzasum(
                    options,
                    ChamRowwise, ChamUpperLower, tempmm, tempnn,
70
                    A(m, n), ldam, W( Wcol, m, n) );
Mathieu Faverge's avatar
Mathieu Faverge committed
71 72 73 74

                INSERT_TASK_dzasum(
                    options,
                    ChamColumnwise, ChamUpperLower, tempmm, tempnn,
75
                    A(m, n), ldam, W( Wcol, n, m) );
Mathieu Faverge's avatar
Mathieu Faverge committed
76 77 78
            }
        }
    }
79

Mathieu Faverge's avatar
Mathieu Faverge committed
80 81
    for(m = 0; m < MT; m++) {
        int tempmm = ( m == (MT-1) ) ? M - m * A->mb : A->mb;
82

Mathieu Faverge's avatar
Mathieu Faverge committed
83
        for(n = Q; n < NT; n++) {
84 85 86 87
            INSERT_TASK_daxpy(
                options, tempmm, 1.,
                W( Wcol, m, n   ), 1,
                W( Wcol, m, n%Q ), 1 );
Mathieu Faverge's avatar
Mathieu Faverge committed
88
        }
89

Mathieu Faverge's avatar
Mathieu Faverge committed
90 91 92 93 94
        /**
         * Step 2:
         *  For each j, W(m, j) = reduce( Wcol(m, 0..Q-1) )
         */
        for(n = 1; n < Q; n++) {
95 96 97 98
            INSERT_TASK_daxpy(
                options, tempmm, 1.,
                W( Wcol, m, n ), 1,
                W( Wcol, m, 0 ), 1 );
99 100
        }

Mathieu Faverge's avatar
Mathieu Faverge committed
101 102 103
        INSERT_TASK_dlange(
            options,
            ChamMaxNorm, tempmm, 1, A->nb,
104
            W( Wcol, m, 0), 1, W( Welt, m, 0));
Mathieu Faverge's avatar
Mathieu Faverge committed
105
    }
106

Mathieu Faverge's avatar
Mathieu Faverge committed
107 108
    /**
     * Step 3:
109
     *  For m in 0..P-1, W( Welt, m, n) = max( Wcol(m..mt[P], n ) )
Mathieu Faverge's avatar
Mathieu Faverge committed
110 111 112 113
     */
    for(m = P; m < MT; m++) {
        INSERT_TASK_dlange_max(
            options,
114
            W( Welt, m, 0), W( Welt, m%P, 0) );
Mathieu Faverge's avatar
Mathieu Faverge committed
115 116 117 118
    }

    /**
     * Step 4:
119
     *  For each i, W( Welt, i, n) = max( W( Welt, 0..P-1, n) )
Mathieu Faverge's avatar
Mathieu Faverge committed
120 121 122 123
     */
    for(m = 1; m < P; m++) {
        INSERT_TASK_dlange_max(
            options,
124
            W( Welt, m, 0), W( Welt, 0, 0) );
Mathieu Faverge's avatar
Mathieu Faverge committed
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
    }
}

static inline void
chameleon_pzlansy_max( cham_trans_t trans, cham_uplo_t uplo, CHAM_desc_t *A,
                       CHAM_desc_t *Welt, RUNTIME_option_t *options)
{
    int m, n;
    int MT = A->mt;
    int NT = A->nt;
    int M  = A->m;
    int N  = A->n;
    int P  = Welt->p;
    int Q  = Welt->q;

    /**
     * Step 1:
     *  For j in [1,Q], Welt(m, j) = reduce( A(m, j+k*Q) )
     */
    for(m = 0; m < MT; m++) {
        int nmin = (uplo == ChamUpper ) ? m                      : 0;
        int nmax = (uplo == ChamLower ) ? chameleon_min(m+1, NT) : NT;

        int tempmm = ( m == (MT-1) ) ? M - m * A->mb : A->mb;
        int ldam = BLKLDD( A, m );

        for(n = nmin; n < nmax; n++) {
            int tempnn = ( n == (NT-1) ) ? N - n * A->nb : A->nb;

            if ( n == m ) {
                if ( trans == ChamConjTrans) {
                    INSERT_TASK_zlanhe(
                        options,
                        ChamMaxNorm, uplo, tempmm, A->nb,
159
                        A(m, n), ldam, W( Welt, m, n));
Mathieu Faverge's avatar
Mathieu Faverge committed
160 161 162 163 164
                }
                else {
                    INSERT_TASK_zlansy(
                        options,
                        ChamMaxNorm, uplo, tempmm, A->nb,
165
                        A(m, n), ldam, W( Welt, m, n));
166 167 168
                }
            }
            else {
Mathieu Faverge's avatar
Mathieu Faverge committed
169 170 171
                INSERT_TASK_zlange(
                    options,
                    ChamMaxNorm, tempmm, tempnn, A->nb,
172
                    A(m, n), ldam, W( Welt, m, n));
173 174
            }

Mathieu Faverge's avatar
Mathieu Faverge committed
175
            if ( n >= Q ) {
176
                INSERT_TASK_dlange_max(
Mathieu Faverge's avatar
Mathieu Faverge committed
177
                    options,
178
                    W( Welt, m, n), W( Welt, m, n%Q) );
179 180 181
            }
        }

Mathieu Faverge's avatar
Mathieu Faverge committed
182 183 184 185 186 187 188
        /**
         * Step 2:
         *  For each j, W(m, j) = reduce( Welt(m, 0..Q-1) )
         */
        for(n = 1; n < Q; n++) {
            INSERT_TASK_dlange_max(
                options,
189
                W( Welt, m, n), W( Welt, m, 0) );
190
        }
Mathieu Faverge's avatar
Mathieu Faverge committed
191 192 193 194 195
    }

    /**
     * Step 3:
     *  For m in 0..P-1, Welt(m, n) = max( Welt(m..mt[P], n ) )
196
     */
Mathieu Faverge's avatar
Mathieu Faverge committed
197 198 199
    for(m = P; m < MT; m++) {
        INSERT_TASK_dlange_max(
            options,
200
            W( Welt, m, 0), W( Welt, m%P, 0) );
Mathieu Faverge's avatar
Mathieu Faverge committed
201
    }
202

Mathieu Faverge's avatar
Mathieu Faverge committed
203 204 205 206 207 208 209
    /**
     * Step 4:
     *  For each i, Welt(i, n) = max( Welt(0..P-1, n) )
     */
    for(m = 1; m < P; m++) {
        INSERT_TASK_dlange_max(
            options,
210
            W( Welt, m, 0), W( Welt, 0, 0) );
Mathieu Faverge's avatar
Mathieu Faverge committed
211 212
    }
}
213

Mathieu Faverge's avatar
Mathieu Faverge committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
static inline void
chameleon_pzlansy_frb( cham_trans_t trans, cham_uplo_t uplo,
                       CHAM_desc_t *A, CHAM_desc_t *Welt,
                       RUNTIME_option_t *options)
{
    int m, n;
    int MT = A->mt;
    int NT = A->nt;
    int M  = A->m;
    int N  = A->n;
    int P  = Welt->p;
    int Q  = Welt->q;

    /**
     * Step 1:
     *  For j in [1,Q], Welt(m, j) = reduce( A(m, j+k*Q) )
     */
    for(m = 0; m < MT; m++) {
        int nmin = (uplo == ChamUpper ) ? m                      : 0;
        int nmax = (uplo == ChamLower ) ? chameleon_min(m+1, NT) : NT;
234

Mathieu Faverge's avatar
Mathieu Faverge committed
235 236
        int tempmm = ( m == (MT-1) ) ? M - m * A->mb : A->mb;
        int ldam = BLKLDD( A, m );
237

Mathieu Faverge's avatar
Mathieu Faverge committed
238 239 240 241 242 243 244
        for(n = nmin; n < nmax; n++) {
            int tempnn = ( n == (NT-1) ) ? N - n * A->nb : A->nb;

            if ( n == m ) {
                if ( trans == ChamConjTrans) {
                    INSERT_TASK_zhessq(
                        options, uplo, tempmm,
245
                        A(m, n), ldam, W( Welt, m, n) );
Mathieu Faverge's avatar
Mathieu Faverge committed
246 247 248 249
                }
                else {
                    INSERT_TASK_zsyssq(
                        options, uplo, tempmm,
250
                        A(m, n), ldam, W( Welt, m, n) );
251 252 253
                }
            }
            else {
Mathieu Faverge's avatar
Mathieu Faverge committed
254 255
                INSERT_TASK_zgessq(
                    options, tempmm, tempnn,
256
                    A(m, n), ldam, W( Welt, m, n) );
Mathieu Faverge's avatar
Mathieu Faverge committed
257 258
                INSERT_TASK_zgessq(
                    options, tempmm, tempnn,
259
                    A(m, n), ldam, W( Welt, n, m) );
260 261
            }
        }
Mathieu Faverge's avatar
Mathieu Faverge committed
262
    }
263

Mathieu Faverge's avatar
Mathieu Faverge committed
264 265 266
    for(m = 0; m < MT; m++) {
        for(n = Q; n < NT; n++) {
            INSERT_TASK_dplssq(
267
                options, W( Welt, m, n), W( Welt, m, n%Q) );
Mathieu Faverge's avatar
Mathieu Faverge committed
268
        }
269

Mathieu Faverge's avatar
Mathieu Faverge committed
270 271
        /**
         * Step 2:
272
         *  For each j, W(m, j) = reduce( W( Welt, m, 0..Q-1) )
Mathieu Faverge's avatar
Mathieu Faverge committed
273 274 275
         */
        for(n = 1; n < Q; n++) {
            INSERT_TASK_dplssq(
276
                options, W( Welt, m, n), W( Welt, m, 0) );
277
        }
Mathieu Faverge's avatar
Mathieu Faverge committed
278
    }
279

Mathieu Faverge's avatar
Mathieu Faverge committed
280 281 282 283 284 285
    /**
     * Step 3:
     *  For m in 0..P-1, Welt(m, n) = max( Welt(m..mt[P], n ) )
     */
    for(m = P; m < MT; m++) {
        INSERT_TASK_dplssq(
286
            options, W( Welt, m, 0), W( Welt, m%P, 0) );
Mathieu Faverge's avatar
Mathieu Faverge committed
287
    }
288

Mathieu Faverge's avatar
Mathieu Faverge committed
289 290 291
    /**
     * Step 4:
     *  For each i, Welt(i, n) = max( Welt(0..P-1, n) )
292
     */
Mathieu Faverge's avatar
Mathieu Faverge committed
293 294
    for(m = 1; m < P; m++) {
        INSERT_TASK_dplssq(
295
            options, W( Welt, m, 0), W( Welt, 0, 0) );
Mathieu Faverge's avatar
Mathieu Faverge committed
296 297 298
    }

    INSERT_TASK_dplssq2(
299
        options, W( Welt, 0, 0) );
Mathieu Faverge's avatar
Mathieu Faverge committed
300 301 302 303 304 305 306 307 308 309 310
}

/**
 *
 */
void chameleon_pzlansy_generic( cham_normtype_t norm, cham_uplo_t uplo, cham_trans_t trans,
                                CHAM_desc_t *A, double *result,
                                RUNTIME_sequence_t *sequence, RUNTIME_request_t *request )
{
    CHAM_context_t *chamctxt;
    RUNTIME_option_t options;
311 312
    CHAM_desc_t Wcol;
    CHAM_desc_t Welt;
Mathieu Faverge's avatar
Mathieu Faverge committed
313 314 315
    double alpha = 0.0;
    double beta  = 0.0;

Mathieu Faverge's avatar
Mathieu Faverge committed
316
    int workmt, worknt;
317
    int m, n, wcol_init = 0;
Mathieu Faverge's avatar
Mathieu Faverge committed
318 319

    chamctxt = chameleon_context_self();
Mathieu Faverge's avatar
Mathieu Faverge committed
320
    if ( sequence->status != CHAMELEON_SUCCESS ) {
Mathieu Faverge's avatar
Mathieu Faverge committed
321
        return;
Mathieu Faverge's avatar
Mathieu Faverge committed
322
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
323 324 325 326 327 328 329 330 331 332
    RUNTIME_options_init(&options, chamctxt, sequence, request);

    *result = 0.0;

    workmt = chameleon_max( A->mt, A->p );
    worknt = chameleon_max( A->nt, A->q );

    switch ( norm ) {
    case ChamOneNorm:
    case ChamInfNorm:
333 334
        RUNTIME_options_ws_alloc( &options, 1, 0 );

335
        chameleon_desc_init( &Wcol, CHAMELEON_MAT_ALLOC_TILE, ChamRealDouble, A->mb, 1, A->mb,
336 337 338
                             workmt * A->mb, worknt, 0, 0, workmt * A->mb, worknt, A->p, A->q,
                             NULL, NULL, NULL );
        wcol_init = 1;
339

340 341 342
        /*
         * Use the global allocator for Welt, otherwise flush may free the data before the result is read.
         */
343 344 345
        chameleon_desc_init( &Welt, CHAMELEON_MAT_ALLOC_GLOBAL, ChamRealDouble, 1, 1, 1,
                             workmt, A->q, 0, 0, workmt, A->q, A->p, A->q,
                             NULL, NULL, NULL );
Mathieu Faverge's avatar
Mathieu Faverge committed
346
        break;
347

Mathieu Faverge's avatar
Mathieu Faverge committed
348 349 350 351 352
        /*
         *  ChamFrobeniusNorm
         */
    case ChamFrobeniusNorm:
        RUNTIME_options_ws_alloc( &options, 1, 0 );
353

Mathieu Faverge's avatar
Mathieu Faverge committed
354
        alpha = 1.;
355
        chameleon_desc_init( &Welt, CHAMELEON_MAT_ALLOC_GLOBAL, ChamRealDouble, 2, 1, 2,
356
                             workmt*2, worknt, 0, 0, workmt*2, worknt, A->p, A->q,
357
                             NULL, NULL, NULL );
Mathieu Faverge's avatar
Mathieu Faverge committed
358 359 360 361 362 363 364 365 366
        break;

        /*
         *  ChamMaxNorm
         */
    case ChamMaxNorm:
    default:
        RUNTIME_options_ws_alloc( &options, 1, 0 );

367
        chameleon_desc_init( &Welt, CHAMELEON_MAT_ALLOC_GLOBAL, ChamRealDouble, 1, 1, 1,
368
                             workmt, worknt, 0, 0, workmt, worknt, A->p, A->q,
369
                             NULL, NULL, NULL );
Mathieu Faverge's avatar
Mathieu Faverge committed
370 371 372 373 374 375 376
    }

    /* Initialize workspaces */
    if ( (norm == ChamInfNorm) ||
         (norm == ChamOneNorm) )
    {
        /* Initialize Wcol tile */
377 378
        for(m = 0; m < Wcol.mt; m++) {
            for(n = 0; n < Wcol.nt; n++) {
Mathieu Faverge's avatar
Mathieu Faverge committed
379 380
                INSERT_TASK_dlaset(
                    &options,
381
                    ChamUpperLower, Wcol.mb, Wcol.nb,
Mathieu Faverge's avatar
Mathieu Faverge committed
382
                    alpha, beta,
383
                    W( &Wcol, m, n ), Wcol.mb );
384 385
            }
        }
Mathieu Faverge's avatar
Mathieu Faverge committed
386
    }
387 388
    for(m = 0; m < Welt.mt; m++) {
        for(n = 0; n < Welt.nt; n++) {
Mathieu Faverge's avatar
Mathieu Faverge committed
389 390
            INSERT_TASK_dlaset(
                &options,
391
                ChamUpperLower, Welt.mb, Welt.nb,
Mathieu Faverge's avatar
Mathieu Faverge committed
392
                alpha, beta,
393
                W( &Welt, m, n ), Welt.mb );
Mathieu Faverge's avatar
Mathieu Faverge committed
394 395 396 397 398 399
        }
    }

    switch ( norm ) {
    case ChamOneNorm:
    case ChamInfNorm:
400 401
        chameleon_pzlansy_inf( uplo, A, &Wcol, &Welt, &options );
        CHAMELEON_Desc_Flush( &Wcol, sequence );
Mathieu Faverge's avatar
Mathieu Faverge committed
402 403 404
        break;

    case ChamFrobeniusNorm:
405
        chameleon_pzlansy_frb( trans, uplo, A, &Welt, &options );
Mathieu Faverge's avatar
Mathieu Faverge committed
406 407 408 409
        break;

    case ChamMaxNorm:
    default:
410
        chameleon_pzlansy_max( trans, uplo, A, &Welt, &options );
Mathieu Faverge's avatar
Mathieu Faverge committed
411
    }
412

Mathieu Faverge's avatar
Mathieu Faverge committed
413 414 415 416 417
    /**
     * Broadcast the result
     */
    for(m = 0; m < A->p; m++) {
        for(n = 0; n < A->q; n++) {
Mathieu Faverge's avatar
Mathieu Faverge committed
418
            if ( (m != 0) || (n != 0) ) {
419
                INSERT_TASK_dlacpy(
420
                    &options,
421
                    ChamUpperLower, 1, 1, 1,
422
                    W( &Welt, 0, 0 ), 1, W( &Welt, m, n ), 1);
423 424 425
            }
        }
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
426

Mathieu Faverge's avatar
Mathieu Faverge committed
427 428 429
    if ( wcol_init ) {
        CHAMELEON_Desc_Flush( &Wcol, sequence );
    }
430
    CHAMELEON_Desc_Flush( &Welt, sequence );
Mathieu Faverge's avatar
Mathieu Faverge committed
431
    CHAMELEON_Desc_Flush( A, sequence );
Mathieu Faverge's avatar
Mathieu Faverge committed
432 433
    RUNTIME_sequence_wait(chamctxt, sequence);

434
    *result = *(double *)Welt.get_blkaddr( &Welt, A->myrank / A->q, A->myrank % A->q );
Mathieu Faverge's avatar
Mathieu Faverge committed
435

436 437
    if ( wcol_init ) {
        chameleon_desc_destroy( &Wcol );
Mathieu Faverge's avatar
Mathieu Faverge committed
438
    }
439
    chameleon_desc_destroy( &Welt );
Mathieu Faverge's avatar
Mathieu Faverge committed
440

441
    RUNTIME_options_ws_free(&options);
Mathieu Faverge's avatar
Mathieu Faverge committed
442
    RUNTIME_options_finalize(&options, chamctxt);
443
}