descriptor.c 27.3 KB
Newer Older
1
2
/**
 *
3
4
 * @copyright (c) 2009-2014 The University of Tennessee and The University
 *                          of Tennessee Research Foundation.
5
6
 *                          All rights reserved.
 * @copyright (c) 2012-2014 Inria. All rights reserved.
7
 * @copyright (c) 2012-2016 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
8
9
10
11
12
13
14
15
16
17
18
 *
 **/

/**
 *
 * @file descriptor.c
 *
 *  MORSE auxiliary routines
 *  MORSE is a software package provided by Univ. of Tennessee,
 *  Univ. of California Berkeley and Univ. of Colorado Denver
 *
19
 * @version 0.9.0
20
21
22
23
24
 * @author Mathieu Faverge
 * @author Cedric Castagnede
 * @date 2010-11-15
 *
 **/
25
26
27
28
29
30
31

/**
 *
 * @defgroup Descriptor
 * @brief Group descriptor routines exposed to users
 *
 */
32
33
#include <stdlib.h>
#include <assert.h>
34
#include <string.h>
35
36
#include "control/common.h"
#include "control/descriptor.h"
37
#include "chameleon/morse_runtime.h"
38
39
40

static int nbdesc = 0;

41
42
43
/**
 ******************************************************************************
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
44
 * @ingroup Descriptor
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
 *
 * morse_desc_init_user - Internal function to create tiled matrix descriptor
 * with generic function for data distribution and storage format.
 *
 ******************************************************************************
 *
 * @param[in] dtyp
 *          Data type of the matrix:
 *          @arg MorseRealFloat:     single precision real (S),
 *          @arg MorseRealDouble:    double precision real (D),
 *          @arg MorseComplexFloat:  single precision complex (C),
 *          @arg MorseComplexDouble: double precision complex (Z).
 *
 * @param[in] mb
 *          Number of rows in a tile.
 *
 * @param[in] nb
 *          Number of columns in a tile.
 *
 * @param[in] bsiz
 *          Size in bytes including padding.
 *
 * @param[in] lm
 *          Number of rows of the entire matrix.
 *
 * @param[in] ln
 *          Number of columns of the entire matrix.
 *
 * @param[in] i
 *          Row index to the beginning of the submatrix.
 *
 * @param[in] j
 *          Column indes to the beginning of the submatrix.
 *
 * @param[in] m
 *          Number of rows of the submatrix.
 *
 * @param[in] n
 *          Number of columns of the submatrix.
 *
 * @param[in] p
 *          2D-block cyclic distribution in rows.
 *
 * @param[in] q
 *          2D-block cyclic distribution in columns.
 *
 * @param[in] (*get_blkaddr)( const MORSE_desc_t *A, int m, int n)
 *          A function which return the address of the data corresponding to
 *          the tile A(m,n).
 *
 * @param[in] (*get_blkldd)( const MORSE_desc_t *A, int m )
 *          A function that return the leading dimension of the tile A(m,*).
 *
 * @param[in] (*get_rankof)( const MORSE_desc_t *A, int m, int n)
 *          A function that return the MPI rank of the tile A(m,n).
 *
 ******************************************************************************
 *
 * @return  The descriptor with the matrix description parameters set.
 *
 *****************************************************************************/
106
107
108
109
MORSE_desc_t morse_desc_init_user(MORSE_enum dtyp, int mb, int nb, int bsiz,
                                  int lm, int ln, int i, int j,
                                  int m,  int n,  int p, int q,
                                  void* (*get_blkaddr)( const MORSE_desc_t*, int, int ),
110
111
                                  int   (*get_blkldd) ( const MORSE_desc_t*, int      ),
                                  int   (*get_rankof) ( const MORSE_desc_t*, int, int ))
112
{
113
    MORSE_context_t *morse;
114
    MORSE_desc_t desc;
115
116
117
118
119
120
121
122
123

    memset( &desc, 0, sizeof(MORSE_desc_t) );

    morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Desc_Create", "MORSE not initialized");
        return desc;
    }

124
125
    // If one of the function get_* is NULL, we switch back to the default, like in morse_desc_init()
    desc.get_blkaddr = get_blkaddr ? get_blkaddr : morse_getaddr_ccrb;
126
127
    desc.get_blkldd  = get_blkldd  ? get_blkldd  : morse_getblkldd_ccrb;
    desc.get_rankof  = get_rankof  ? get_rankof  : morse_getrankof_2d;
128
129
    // Matrix properties
    desc.dtyp = dtyp;
130
    // Should be given as parameter to follow get_blkaddr (unused)
Mathieu Faverge's avatar
Mathieu Faverge committed
131
    desc.styp = MorseCCRB;
132
133
    desc.mb   = mb;
    desc.nb   = nb;
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    desc.bsiz = bsiz;
    // Large matrix parameters
    desc.lm = lm;
    desc.ln = ln;
    // Large matrix derived parameters
    desc.lmt = (lm%mb==0) ? (lm/mb) : (lm/mb+1);
    desc.lnt = (ln%nb==0) ? (ln/nb) : (ln/nb+1);
    // Submatrix parameters
    desc.i = i;
    desc.j = j;
    desc.m = m;
    desc.n = n;
    // Submatrix derived parameters
    desc.mt = (m == 0) ? 0 : (i+m-1)/mb - i/mb + 1;
    desc.nt = (n == 0) ? 0 : (j+n-1)/nb - j/nb + 1;

    desc.id = nbdesc; nbdesc++;
151
    desc.occurences = 0;
152
153
    desc.use_mat      = 1;
    desc.alloc_mat    = 1;
154
    desc.register_mat = (morse->ncudas > 0) ? 1 : 0;
155
    desc.ooc          = 0;
156

157
    desc.myrank = RUNTIME_comm_rank( morse );
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

    // Grid size
    desc.p = p;
    desc.q = q;

    // Local dimensions in tiles
    if ( desc.myrank < (p*q) ) {
        desc.llmt = (desc.lmt + p - 1) / p;
        desc.llnt = (desc.lnt + q - 1) / q;

        // Local dimensions
        if ( ((desc.lmt-1) % p) == (desc.myrank / q) ) {
            desc.llm  = ( desc.llmt - 1 ) * mb + ((lm%mb==0) ? mb : (lm%mb));
        } else {
            desc.llm  =  desc.llmt * mb;
        }

        if ( ((desc.lnt-1) % q) == (desc.myrank % q) ) {
            desc.lln  = ( desc.llnt - 1 ) * nb + ((ln%nb==0) ? nb : (ln%nb));
        } else {
            desc.lln  =  desc.llnt * nb;
        }

        desc.llm1 = (desc.llm/mb);
        desc.lln1 = (desc.lln/nb);
    } else {
184
185
186
187
188
189
        desc.llmt = 0;
        desc.llnt = 0;
        desc.llm  = 0;
        desc.lln  = 0;
        desc.llm1 = 0;
        desc.lln1 = 0;
190
191
192
193
194
195
196
197
198
199
200
    }

    // Matrix address
    desc.mat = NULL;
    desc.A21 = (size_t)(desc.llm - desc.llm%mb)*(size_t)(desc.lln - desc.lln%nb);
    desc.A12 = (size_t)(           desc.llm%mb)*(size_t)(desc.lln - desc.lln%nb) + desc.A21;
    desc.A22 = (size_t)(desc.llm - desc.llm%mb)*(size_t)(           desc.lln%nb) + desc.A12;

    return desc;
}

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*******************************************************************************
 *  Internal static descriptor initializer
 **/
MORSE_desc_t morse_desc_init(MORSE_enum dtyp, int mb, int nb, int bsiz,
                             int lm, int ln, int i, int j,
                             int m,  int n,  int p, int q)
{
    return morse_desc_init_user(dtyp, mb, nb, bsiz, lm, ln, i, j, m, n, p, q,
                                morse_getaddr_ccrb, morse_getblkldd_ccrb, morse_getrankof_2d);
}

/*******************************************************************************
 *  Internal static descriptor initializer for a block diagonal matrix
 **/
MORSE_desc_t morse_desc_init_diag(MORSE_enum dtyp, int mb, int nb, int bsiz,
                                  int lm, int ln, int i, int j,
                                  int m,  int n,  int p, int q)
{
    return morse_desc_init_user(dtyp, mb, nb, bsiz, lm, ln, i, j, m, n, p, q,
                                morse_getaddr_ccrb, morse_getblkldd_ccrb, morse_getrankof_2d_diag);
}

223
224
225
/*******************************************************************************
 *  Internal static descriptor initializer for submatrices
 **/
226
MORSE_desc_t* morse_desc_submatrix(MORSE_desc_t *descA, int i, int j, int m, int n)
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
{
    MORSE_desc_t *descB = malloc(sizeof(MORSE_desc_t));
    int mb, nb;

    if ( (descA->i + i + m) > descA->lm ) {
        morse_error("morse_desc_submatrix", "The number of rows (i+m) of the submatrix doesn't fit in the parent matrix");
        assert((descA->i + i + m) > descA->lm);
    }
    if ( (descA->j + j + n) > descA->ln ) {
        morse_error("morse_desc_submatrix", "The number of rows (j+n) of the submatrix doesn't fit in the parent matrix");
        assert((descA->j + j + n) > descA->ln);
    }

    memcpy( descB, descA, sizeof(MORSE_desc_t) );
    mb = descA->mb;
    nb = descA->nb;
    // Submatrix parameters
    descB->i = descA->i + i;
    descB->j = descA->j + j;
    descB->m = m;
    descB->n = n;
    // Submatrix derived parameters
    descB->mt = (m == 0) ? 0 : (descB->i+m-1)/mb - descB->i/mb + 1;
    descB->nt = (n == 0) ? 0 : (descB->j+n-1)/nb - descB->j/nb + 1;

252
253
    // Increase the number of occurences to avoid multiple free of runtime specific data structures.
    descB->occurences++;
254
255
256
257
258
259
260

    return descB;
}

/*******************************************************************************
 *  Check for descriptor correctness
 **/
261
int morse_desc_check(const MORSE_desc_t *desc)
262
263
264
265
266
{
    if (desc == NULL) {
        morse_error("morse_desc_check", "NULL descriptor");
        return MORSE_ERR_NOT_INITIALIZED;
    }
267
    if (desc->mat == NULL && desc->use_mat == 1) {
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
        morse_error("morse_desc_check", "NULL matrix pointer");
        return MORSE_ERR_UNALLOCATED;
    }
    if (desc->dtyp != MorseRealFloat &&
        desc->dtyp != MorseRealDouble &&
        desc->dtyp != MorseComplexFloat &&
        desc->dtyp != MorseComplexDouble  ) {
        morse_error("morse_desc_check", "invalid matrix type");
        return MORSE_ERR_ILLEGAL_VALUE;
    }
    if (desc->mb <= 0 || desc->nb <= 0) {
        morse_error("morse_desc_check", "negative tile dimension");
        return MORSE_ERR_ILLEGAL_VALUE;
    }
    if (desc->bsiz < desc->mb*desc->nb) {
        morse_error("morse_desc_check", "tile memory size smaller than the product of dimensions");
        return MORSE_ERR_ILLEGAL_VALUE;
    }
    if (desc->lm <= 0 || desc->ln <= 0) {
        morse_error("morse_desc_check", "negative matrix dimension");
        return MORSE_ERR_ILLEGAL_VALUE;
    }
    if ((desc->lm < desc->m) || (desc->ln < desc->n)) {
        morse_error("morse_desc_check", "matrix dimensions larger than leading dimensions");
        return MORSE_ERR_ILLEGAL_VALUE;
    }
    if ((desc->i > 0 && desc->i >= desc->lm) || (desc->j > 0 && desc->j >= desc->ln)) {
        morse_error("morse_desc_check", "beginning of the matrix out of scope");
        return MORSE_ERR_ILLEGAL_VALUE;
    }
    if (desc->i+desc->m > desc->lm || desc->j+desc->n > desc->ln) {
        morse_error("morse_desc_check", "submatrix out of scope");
        return MORSE_ERR_ILLEGAL_VALUE;
    }
    return MORSE_SUCCESS;
}

/*******************************************************************************
 *
 **/
int morse_desc_mat_alloc( MORSE_desc_t *desc )
{

    size_t size = (size_t)(desc->llm) * (size_t)(desc->lln)
312
        * (size_t)MORSE_Element_Size(desc->dtyp);
313
    if ((desc->mat = RUNTIME_malloc(size)) == NULL) {
314
315
316
317
        morse_error("morse_desc_mat_alloc", "malloc() failed");
        return MORSE_ERR_OUT_OF_RESOURCES;
    }

318
319
    /* The matrix has already been registered by the Runtime alloc */
    desc->register_mat = 0;
320
321
322
323
324
325
326
327
328
329
330
331
    RUNTIME_desc_create(desc);

    return MORSE_SUCCESS;
}

/*******************************************************************************
 *
 **/
int morse_desc_mat_free( MORSE_desc_t *desc )
{
    RUNTIME_desc_destroy( desc );

332
333
334
335
    if ( (desc->mat       != NULL) &&
         (desc->use_mat   == 1   ) &&
         (desc->alloc_mat == 1   ) )
    {
336
337
338
        size_t size = (size_t)(desc->llm) * (size_t)(desc->lln)
            * (size_t)MORSE_Element_Size(desc->dtyp);

339
        RUNTIME_free(desc->mat, size);
340
341
342
343
344
        desc->mat = NULL;
    }
    return MORSE_SUCCESS;
}

345
346
/**
 *****************************************************************************
347
 *
348
 * @ingroup Descriptor
349
 *
350
 *  MORSE_Desc_Create - Create tiled matrix descriptor.
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
 *
 * @param[out] desc
 *          On exit, descriptor of the matrix.
 *
 * @param[in] mat
 *          Memory location of the matrix. If mat is NULL, the space to store
 *          the data is automatically allocated by the call to the function.
 *
 * @param[in] dtyp
 *          Data type of the matrix:
 *          @arg MorseRealFloat:     single precision real (S),
 *          @arg MorseRealDouble:    double precision real (D),
 *          @arg MorseComplexFloat:  single precision complex (C),
 *          @arg MorseComplexDouble: double precision complex (Z).
 *
 * @param[in] mb
 *          Number of rows in a tile.
 *
 * @param[in] nb
 *          Number of columns in a tile.
 *
 * @param[in] bsiz
 *          Size in bytes including padding.
 *
 * @param[in] lm
 *          Number of rows of the entire matrix.
 *
 * @param[in] ln
 *          Number of columns of the entire matrix.
 *
 * @param[in] i
 *          Row index to the beginning of the submatrix.
 *
 * @param[in] j
 *          Column indes to the beginning of the submatrix.
 *
 * @param[in] m
 *          Number of rows of the submatrix.
 *
 * @param[in] n
 *          Number of columns of the submatrix.
 *
395
396
397
398
399
400
 * @param[in] p
 *          2D-block cyclic distribution in rows.
 *
 * @param[in] q
 *          2D-block cyclic distribution in columns.
 *
401
 ******************************************************************************
402
403
404
405
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
406
 *****************************************************************************/
407
int MORSE_Desc_Create(MORSE_desc_t **descptr, void *mat, MORSE_enum dtyp, int mb, int nb, int bsiz,
408
409
410
                      int lm, int ln, int i, int j, int m, int n, int p, int q)
{
    MORSE_context_t *morse;
411
    MORSE_desc_t *desc;
412
413
    int status;

414
415
    *descptr = NULL;

416
417
418
419
420
    morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Desc_Create", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
421

422
    /* Allocate memory and initialize the descriptor */
423
424
    desc = (MORSE_desc_t*)malloc(sizeof(MORSE_desc_t));
    if (desc == NULL) {
425
426
427
        morse_error("MORSE_Desc_Create", "malloc() failed");
        return MORSE_ERR_OUT_OF_RESOURCES;
    }
428
    *desc = morse_desc_init(dtyp, mb, nb, bsiz, lm, ln, i, j, m, n, p, q);
429
430
431

    if (mat == NULL) {

432
433
        size_t size = (size_t)(desc->llm) * (size_t)(desc->lln)
            * (size_t)MORSE_Element_Size(desc->dtyp);
434

435
        if ((desc->mat = RUNTIME_malloc(size)) == NULL) {
436
437
438
            morse_error("MORSE_Desc_Create", "malloc() failed");
            return MORSE_ERR_OUT_OF_RESOURCES;
        }
439
440
441
        desc->use_mat      = 1;
        desc->alloc_mat    = 1;
        desc->register_mat = 0;
442
443

    } else {
444
445
446
447
448
        desc->mat = mat;
        /* memory of the matrix is handled by users */
        desc->alloc_mat    = 0;
        desc->use_mat      = 1;
        desc->register_mat = 0;
449
450
451
    }

    /* Create scheduler structure like registering data */
452
    RUNTIME_desc_create( desc );
453

454
    status = morse_desc_check( desc );
455
456
    if (status != MORSE_SUCCESS) {
        morse_error("MORSE_Desc_Create", "invalid descriptor");
457
        MORSE_Desc_Destroy( &desc );
458
459
460
        return status;
    }

461
    *descptr = desc;
462
463
464
    return MORSE_SUCCESS;
}

465
466
/**
 *****************************************************************************
THIBAULT Samuel's avatar
THIBAULT Samuel committed
467
468
469
 *
 * @ingroup Descriptor
 *
470
471
 *  MORSE_Desc_Create_User - Create generic tiled matrix descriptor for general
 *  applications.
THIBAULT Samuel's avatar
THIBAULT Samuel committed
472
473
474
475
476
477
 *
 ******************************************************************************
 *
 * @param[out] desc
 *          On exit, descriptor of the matrix.
 *
478
479
480
481
 * @param[in] mat
 *          Memory location of the matrix. If mat is NULL, the space to store
 *          the data is automatically allocated by the call to the function.
 *
THIBAULT Samuel's avatar
THIBAULT Samuel committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
 * @param[in] dtyp
 *          Data type of the matrix:
 *          @arg MorseRealFloat:     single precision real (S),
 *          @arg MorseRealDouble:    double precision real (D),
 *          @arg MorseComplexFloat:  single precision complex (C),
 *          @arg MorseComplexDouble: double precision complex (Z).
 *
 * @param[in] nb
 *          Number of rows and columns in a tile.
 *
 * @param[in] m
 *          Number of rows of the entire matrix.
 *
 * @param[in] n
 *          Number of columns of the entire matrix.
 *
 * @param[in] p
 *          2d-block cyclic partitioning, number of tiles in rows.
 *
 * @param[in] q
 *          2d-block cyclic partitioning, number of tiles in columns.
 *
504
505
506
507
508
509
510
 * @param[in] (*get_blkaddr)( const MORSE_desc_t *A, int m, int n)
 *          A function which return the address of the data corresponding to
 *          the tile A(m,n).
 *
 * @param[in] (*get_blkldd)( const MORSE_desc_t *A, int m)
 *          A function that return the leading dimension of the tile A(m,*).
 *
THIBAULT Samuel's avatar
THIBAULT Samuel committed
511
512
513
514
515
516
517
518
519
 * @param[in] (*get_rankof)( const MORSE_desc_t *A, int m, int n)
 *          A function that return the MPI rank of the tile A(m,n).
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
520
521
522
523
524
int MORSE_Desc_Create_User(MORSE_desc_t **descptr, void *mat, MORSE_enum dtyp, int mb, int nb, int bsiz,
                           int lm, int ln, int i, int j, int m, int n, int p, int q,
                           void* (*get_blkaddr)( const MORSE_desc_t*, int, int ),
                           int   (*get_blkldd) ( const MORSE_desc_t*, int      ),
                           int   (*get_rankof) ( const MORSE_desc_t*, int, int ))
THIBAULT Samuel's avatar
THIBAULT Samuel committed
525
526
{
    MORSE_context_t *morse;
527
    MORSE_desc_t *desc;
THIBAULT Samuel's avatar
THIBAULT Samuel committed
528
529
    int status;

530
531
    *descptr = NULL;

THIBAULT Samuel's avatar
THIBAULT Samuel committed
532
533
    morse = morse_context_self();
    if (morse == NULL) {
534
        morse_error("MORSE_Desc_Create_User", "MORSE not initialized");
THIBAULT Samuel's avatar
THIBAULT Samuel committed
535
536
        return MORSE_ERR_NOT_INITIALIZED;
    }
537

THIBAULT Samuel's avatar
THIBAULT Samuel committed
538
    /* Allocate memory and initialize the descriptor */
539
540
    desc = (MORSE_desc_t*)malloc(sizeof(MORSE_desc_t));
    if (desc == NULL) {
541
        morse_error("MORSE_Desc_Create_User", "malloc() failed");
THIBAULT Samuel's avatar
THIBAULT Samuel committed
542
543
544
        return MORSE_ERR_OUT_OF_RESOURCES;
    }

545
546
547
548
549
    *desc = morse_desc_init_user(dtyp, mb, nb, bsiz, lm, ln, i, j, m, n, p, q,
                                 get_blkaddr, get_blkldd, get_rankof);

    /* if the user gives a pointer to the overall data (tiles) we can use it */
    desc->use_mat = (mat == NULL) ? 0 : 1;
THIBAULT Samuel's avatar
THIBAULT Samuel committed
550

551
552
    /* memory of the matrix is handled by the user */
    desc->alloc_mat = 0;
THIBAULT Samuel's avatar
THIBAULT Samuel committed
553

554
555
556
557
558
559
560
    /* users data can have multiple forms: let him register tiles */
    desc->register_mat = 0;

    desc->mat = mat;

    /* Create runtime specific structure like registering data */
    RUNTIME_desc_create( desc );
THIBAULT Samuel's avatar
THIBAULT Samuel committed
561

562
    status = morse_desc_check( desc );
THIBAULT Samuel's avatar
THIBAULT Samuel committed
563
    if (status != MORSE_SUCCESS) {
564
        morse_error("MORSE_Desc_Create_User", "invalid descriptor");
565
        MORSE_Desc_Destroy( &desc );
THIBAULT Samuel's avatar
THIBAULT Samuel committed
566
567
568
        return status;
    }

569
    *descptr = desc;
THIBAULT Samuel's avatar
THIBAULT Samuel committed
570
571
572
    return MORSE_SUCCESS;
}

Mathieu Faverge's avatar
Mathieu Faverge committed
573
574
/**
 *****************************************************************************
575
 *
576
 * @ingroup Descriptor
577
 *
578
579
 *  MORSE_Desc_Create_OOC_User - Create matrix descriptor for tiled matrix which
 *  may not fit memory.
580
 *
581
 ******************************************************************************
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
 *
 * @param[out] desc
 *          On exit, descriptor of the matrix.
 *
 * @param[in] dtyp
 *          Data type of the matrix:
 *          @arg MorseRealFloat:     single precision real (S),
 *          @arg MorseRealDouble:    double precision real (D),
 *          @arg MorseComplexFloat:  single precision complex (C),
 *          @arg MorseComplexDouble: double precision complex (Z).
 *
 * @param[in] nb
 *          Number of rows and columns in a tile.
 *
 * @param[in] m
 *          Number of rows of the entire matrix.
 *
 * @param[in] n
 *          Number of columns of the entire matrix.
 *
602
 * @param[in] p
603
604
605
606
607
608
609
610
 *          2d-block cyclic partitioning, number of tiles in rows.
 *
 * @param[in] q
 *          2d-block cyclic partitioning, number of tiles in columns.
 *
 * @param[in] (*get_rankof)( const MORSE_desc_t *A, int m, int n)
 *          A function that return the MPI rank of the tile A(m,n).
 *
611
 ******************************************************************************
612
613
614
615
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
616
 *****************************************************************************/
617
618
619
int MORSE_Desc_Create_OOC_User(MORSE_desc_t **descptr, MORSE_enum dtyp, int mb, int nb, int bsiz,
                               int lm, int ln, int i, int j, int m, int n, int p, int q,
                               int (*get_rankof)( const MORSE_desc_t*, int, int ))
620
{
621
#if !defined (CHAMELEON_SCHED_STARPU)
622
623
624
625
    (void)descptr; (void)dtyp; (void)mb; (void)nb; (void)bsiz;
    (void)lm; (void)ln; (void)i; (void)j; (void)m; (void)n; (void)p; (void)q;
    (void)get_rankof;

626
    morse_error("MORSE_Desc_Create_OOC_User", "Only StarPU supports on-demand tile allocation");
627
628
    return MORSE_ERR_NOT_INITIALIZED;
#else
629
    MORSE_context_t *morse;
630
    MORSE_desc_t *desc;
631
632
    int status;

633
634
    *descptr = NULL;

635
636
    morse = morse_context_self();
    if (morse == NULL) {
637
        morse_error("MORSE_Desc_Create_OOC_User", "MORSE not initialized");
638
639
640
        return MORSE_ERR_NOT_INITIALIZED;
    }
    /* Allocate memory and initialize the descriptor */
641
642
    desc = (MORSE_desc_t*)malloc(sizeof(MORSE_desc_t));
    if (desc == NULL) {
643
        morse_error("MORSE_Desc_Create_OOC_User", "malloc() failed");
644
645
        return MORSE_ERR_OUT_OF_RESOURCES;
    }
646
647
    *desc = morse_desc_init_user(dtyp, mb, nb, bsiz, lm, ln, i, j, m, n, p, q,
                                 morse_getaddr_null, NULL, get_rankof);
648

649
650
651
652
    /* memory of the matrix is completely handled by runtime */
    desc->use_mat      = 0;
    desc->alloc_mat    = 0;
    desc->register_mat = 0;
653

654
655
    desc->mat = NULL;
    desc->ooc = 1;
656
657

    /* Create scheduler structure like registering data */
658
    RUNTIME_desc_create( desc );
659

660
    status = morse_desc_check( desc );
661
    if (status != MORSE_SUCCESS) {
662
        morse_error("MORSE_Desc_Create_OOC_User", "invalid descriptor");
663
        MORSE_Desc_Destroy( &desc );
664
665
666
        return status;
    }

667
    *descptr = desc;
668
    return MORSE_SUCCESS;
669
#endif
670
671
}

672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/**
 *****************************************************************************
 *
 * @ingroup Descriptor
 *
 *  MORSE_Desc_Create_OOC - Create matrix descriptor for tiled matrix which may
 *  not fit memory.
 *
 ******************************************************************************
 *
 * @param[out] desc
 *          On exit, descriptor of the matrix.
 *
 * @param[in] dtyp
 *          Data type of the matrix:
 *          @arg MorseRealFloat:     single precision real (S),
 *          @arg MorseRealDouble:    double precision real (D),
 *          @arg MorseComplexFloat:  single precision complex (C),
 *          @arg MorseComplexDouble: double precision complex (Z).
 *
 * @param[in] nb
 *          Number of rows and columns in a tile.
 *
 * @param[in] m
 *          Number of rows of the entire matrix.
 *
 * @param[in] n
 *          Number of columns of the entire matrix.
 *
 * @param[in] p
 *          2d-block cyclic partitioning, number of tiles in rows.
 *
 * @param[in] q
 *          2d-block cyclic partitioning, number of tiles in columns.
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
int MORSE_Desc_Create_OOC(MORSE_desc_t **descptr, MORSE_enum dtyp, int mb, int nb, int bsiz,
                          int lm, int ln, int i, int j, int m, int n, int p, int q)
{
    return MORSE_Desc_Create_OOC_User( descptr, dtyp, mb, nb, bsiz,
                                       lm, ln, i, j, m, n, p, q,
                                       morse_getrankof_2d );
}

Mathieu Faverge's avatar
Mathieu Faverge committed
721
722
/**
 *****************************************************************************
723
 *
724
 * @ingroup Descriptor
725
726
727
 *
 *  MORSE_Desc_Destroy - Destroys matrix descriptor.
 *
728
 ******************************************************************************
729
730
731
732
 *
 * @param[in] desc
 *          Matrix descriptor.
 *
733
 ******************************************************************************
734
735
736
737
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
738
 *****************************************************************************/
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
int MORSE_Desc_Destroy(MORSE_desc_t **desc)
{
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Desc_Destroy", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }

    if (*desc == NULL) {
        morse_error("MORSE_Desc_Destroy", "attempting to destroy a NULL descriptor");
        return MORSE_ERR_UNALLOCATED;
    }

    morse_desc_mat_free( *desc );
    free(*desc);
    *desc = NULL;
    return MORSE_SUCCESS;
}

Mathieu Faverge's avatar
Mathieu Faverge committed
760
761
/**
 *****************************************************************************
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
 *
 * @ingroup Descriptor
 *
 *  MORSE_Desc_Acquire - Ensures that all data of the descriptor are
 *  up-to-date.
 *
 ******************************************************************************
 *
 * @param[in] desc
 *          Matrix descriptor.
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
779
int MORSE_Desc_Acquire (MORSE_desc_t  *desc) {
780
    return RUNTIME_desc_acquire( desc );
781
782
}

Mathieu Faverge's avatar
Mathieu Faverge committed
783
784
/**
 *****************************************************************************
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
 *
 * @ingroup Descriptor
 *
 *  MORSE_Desc_Release - Release the data of the descriptor acquired by the
 *  application. Should be called if MORSE_Desc_Acquire has been called on the
 *  descriptor and if you do not need to access to its data anymore.
 *
 ******************************************************************************
 *
 * @param[in] desc
 *          Matrix descriptor.
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
803
int MORSE_Desc_Release (MORSE_desc_t  *desc) {
804
    return RUNTIME_desc_release( desc );
805
806
}

Mathieu Faverge's avatar
Mathieu Faverge committed
807
808
/**
 *****************************************************************************
809
810
811
 *
 * @ingroup Descriptor
 *
812
 *  MORSE_Desc_Flush - Flushes the data in the sequence when they won't be reused. This calls cleans up the distributed communication caches, and transfer the data back to the CPU.
813
814
815
816
817
818
819
820
821
822
823
824
 *
 ******************************************************************************
 *
 * @param[in] desc
 *          Matrix descriptor.
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
825
826
827
828
829
int MORSE_Desc_Flush( MORSE_desc_t     *desc,
                      MORSE_sequence_t *sequence )
{
    RUNTIME_desc_flush( desc, sequence );
    return MORSE_SUCCESS;
830
}
831

Mathieu Faverge's avatar
Mathieu Faverge committed
832
833
/**
 *****************************************************************************
834
835
836
837
 *
 * @ingroup Descriptor
 *
 *  MORSE_user_tag_size - Set the sizes for the MPI tags
Mathieu Faverge's avatar
Mathieu Faverge committed
838
839
840
 *  Default value: tag_width=31, tag_sep=24, meaning that the MPI tag is stored
 *  in 31 bits, with 24 bits for the tile tag and 7 for the descriptor.  This
 *  function must be called before any descriptor creation.
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
 *
 ******************************************************************************
 *
 * @param[in] user_tag_width
 *          The new value for tag_width.
 *
 * @param[in] user_tag_sep
 *          The new value for tag_sep.
 *
 ******************************************************************************
 *
 * @return
 *          \retval none
 *
 *****************************************************************************/
void MORSE_user_tag_size(int user_tag_width, int user_tag_sep) {
857
    RUNTIME_comm_set_tag_sizes( user_tag_width, user_tag_sep );
858
    return;
859
}