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

/**
 *
13
 * @file runtime_descriptor.c
14
15
16
17
18
 *
 *  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
25
26
 * @author Cedric Augonnet
 * @author Mathieu Faverge
 * @author Cedric Castagnede
 * @date 2010-11-15
 *
 **/
#include <stdlib.h>
27
#include "runtime/starpu/include/morse_starpu.h"
28

29
#if defined(CHAMELEON_USE_MPI)
30

31
32
/* Take 24 bits for the tile id, and 7 bits for descriptor id.
   These values can be changed through the call MORSE_user_tag_size(int tag_width, int tag_sep) */
33
34
35
#define TAG_WIDTH_MIN 20
static int tag_width = 31;
static int tag_sep   = 24;
36
static int _tag_mpi_initialized_ = 0;
37
38
39
40
41
42
43
44
45
46
47

#ifndef HAVE_STARPU_MPI_DATA_REGISTER
#define starpu_mpi_data_register( handle_, tag_, owner_ )        \
    do {                                                         \
     starpu_data_set_rank( (handle_), (owner_) );                \
     starpu_data_set_tag( (handle_), (tag_) );                   \
    } while(0)
#endif

#endif

48
49
50
51
52
53
#ifdef STARPU_MALLOC_SIMULATION_FOLDED
#define FOLDED STARPU_MALLOC_SIMULATION_FOLDED
#else
#define FOLDED 0
#endif

54
void RUNTIME_user_tag_size(int user_tag_width, int user_tag_sep) {
THIBAULT Samuel's avatar
THIBAULT Samuel committed
55
#if defined(CHAMELEON_USE_MPI)
56
57
58
59
60
  if (_tag_mpi_initialized_ == 0) {
    tag_width=user_tag_width;
    tag_sep=user_tag_sep;
  } else
    morse_error("RUNTIME_user_tag_size", "must be called before creating any Morse descriptor with MORSE_Desc_create(). The tag sizes will not be modified.");
THIBAULT Samuel's avatar
THIBAULT Samuel committed
61
#endif
62
63
64
}


65
66
67
68
69
70
71
void *RUNTIME_mat_alloc( size_t size)
{
#if defined(CHAMELEON_SIMULATION) && !defined(STARPU_MALLOC_SIMULATION_FOLDED) && !defined(CHAMELEON_USE_MPI)
    return (void*) 1;
#else
    void *mat;

72
    if (starpu_malloc_flags(&mat, size, STARPU_MALLOC_PINNED|FOLDED|STARPU_MALLOC_COUNT) != 0)
73
74
75
76
77
78
79
80
81
82
        return NULL;
    return mat;
#endif
}

void RUNTIME_mat_free( void *mat, size_t size)
{
#if defined(CHAMELEON_SIMULATION) && !defined(STARPU_MALLOC_SIMULATION_FOLDED) && !defined(CHAMELEON_USE_MPI)
    return (void*) 1;
#else
83
    starpu_free_flags(mat, size, STARPU_MALLOC_PINNED|FOLDED|STARPU_MALLOC_COUNT);
84
85
86
#endif
}

87
88
89
90
91
void RUNTIME_desc_create( MORSE_desc_t *desc )
{
    int64_t lmt = desc->lmt;
    int64_t lnt = desc->lnt;
    starpu_data_handle_t *tiles;
92
    (void)tiles;
93
94
95
96

    desc->occurences = 1;

    /*
97
98
     * Allocate starpu_handle_t array (handlers are initialized on the fly when
     * discovered by any algorithm to save space)
99
100
101
102
103
     */
    desc->schedopt = (void*)calloc(lnt*lmt,sizeof(starpu_data_handle_t));
    assert(desc->schedopt);
    tiles = (starpu_data_handle_t*)(desc->schedopt);

104
#if defined(CHAMELEON_USE_CUDA) && !defined(CHAMELEON_SIMULATION)
105
106
107
108
    if (desc->use_mat == 1 && desc->register_mat == 1){
        /*
         * Register allocated memory as CUDA pinned memory
         */
109
        {
110
            int64_t eltsze = MORSE_Element_Size(desc->dtyp);
111
112
113
114
115
116
117
            size_t size = (size_t)(desc->llm) * (size_t)(desc->lln) * eltsze;

            /* Register the matrix as pinned memory */
            if ( cudaHostRegister( desc->mat, size, cudaHostRegisterPortable ) != cudaSuccess )
            {
                morse_warning("RUNTIME_desc_create(StarPU)", "cudaHostRegister failed to register the matrix as pinned memory");
            }
118
119
120
121
        }
    }
#endif

122
#if defined(CHAMELEON_USE_MPI)
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    /*
     * Check that we are not going over MPI tag limitations
     */
    {
        if (!_tag_mpi_initialized_) {
            int *tag_ub = NULL;
            int ok = 0;

            MPI_Comm_get_attr(MPI_COMM_WORLD, MPI_TAG_UB, &tag_ub, &ok);

            if ( !ok ) {
                morse_error("RUNTIME_desc_create", "MPI_TAG_UB not known by MPI");
            }

137
            while ( ((uintptr_t)(1UL<<tag_width - 1) > (uint)(*tag_ub) )
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
                    && (tag_width >= TAG_WIDTH_MIN) ) {
                tag_width--;
                tag_sep--;
            }

            if ( tag_width < TAG_WIDTH_MIN ) {
                morse_error("RUNTIME_desc_create", "MPI_TAG_UB too small to identify all the data");
                return;
            }

            _tag_mpi_initialized_ = 1;
        }

        /* Check that we won't create overflow in tags used */
        if ( (lnt*lmt) > ((uintptr_t)(1UL<<tag_sep)) ) {
153
            morse_fatal_error("RUNTIME_desc_create", "Too many tiles in the descriptor for MPI tags");
154
155
156
157
158
            return;
        }
        assert(lmt*lmt<=(1<<tag_sep));

        if (desc->id >= 1UL<<(tag_width-tag_sep)) {
159
            morse_fatal_error("RUNTIME_desc_create", "Number of descriptor available in MPI mode out of stock");
160
161
162
163
164
165
166
167
168
169
170
171
172
            return;
        }
        assert( desc->id < (1UL<<(tag_width-tag_sep)) );
    }
#endif
}

void RUNTIME_desc_destroy( MORSE_desc_t *desc )
{
    desc->occurences--;

    /*
     * If this is the last descriptor using the matrix, we release the handle
173
     * and unregister the GPU data
174
175
176
177
178
179
180
181
182
183
     */
    if ( desc->occurences == 0 ) {
        starpu_data_handle_t *handle = (starpu_data_handle_t*)(desc->schedopt);
        int lmt = desc->lmt;
        int lnt = desc->lnt;
        int m, n;

        for (n = 0; n < lnt; n++)
            for (m = 0; m < lmt; m++)
            {
184
                if (*handle == NULL)
185
186
187
188
189
190
191
192
193
                {
                    handle++;
                    continue;
                }
                //printf("\nUnregister %d %d %d", MORSE_My_Mpi_Rank(), m, n);
                starpu_data_unregister(*handle);
                handle++;
            }

194
#if defined(CHAMELEON_USE_CUDA) && !defined(CHAMELEON_SIMULATION)
195
        if (desc->use_mat == 1 && desc->register_mat == 1){
196
            int64_t eltsze = MORSE_Element_Size(desc->dtyp);
197
198
199
200
201
202
            size_t size = (size_t)(desc->llm) * (size_t)(desc->lln) * eltsze;

            /* Unmap the pinned memory associated to the matrix */
            if (cudaHostUnregister(desc->mat) != cudaSuccess)
            {
                morse_warning("RUNTIME_desc_destroy(StarPU)",
203
204
                              "cudaHostUnregister failed to unregister the "
                              "pinned memory associated to the matrix");
205
206
            }
        }
207
#endif /* defined(CHAMELEON_USE_CUDA) */
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224

        free(desc->schedopt);
    }
}

void RUNTIME_desc_init( MORSE_desc_t *desc )
{
  (void)desc;
  return;
}

void RUNTIME_desc_submatrix( MORSE_desc_t *desc )
{
    desc->occurences++;
    return;
}

225
226
/* TODO: Acquire/Release/GetonCPU need to be studied carefully and fixed
 * because we are not using them correctly */
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
int RUNTIME_desc_acquire( MORSE_desc_t *desc )
{
    starpu_data_handle_t *handle = (starpu_data_handle_t*)(desc->schedopt);
    int lmt = desc->lmt;
    int lnt = desc->lnt;
    int m, n;

    for (n = 0; n < lnt; n++)
        for (m = 0; m < lmt; m++)
        {
            if ( (*handle == NULL) ||
                 !morse_desc_islocal( desc, m, n ) )
            {
                handle++;
                continue;
            }
            starpu_data_acquire(*handle, STARPU_R);
            handle++;
        }
    return MORSE_SUCCESS;
}

int RUNTIME_desc_release( MORSE_desc_t *desc )
{
    starpu_data_handle_t *handle = (starpu_data_handle_t*)(desc->schedopt);
    int lmt = desc->lmt;
    int lnt = desc->lnt;
    int m, n;

    for (n = 0; n < lnt; n++)
        for (m = 0; m < lmt; m++)
        {
            if ( (*handle == NULL) ||
                 !morse_desc_islocal( desc, m, n ) )
            {
                handle++;
                continue;
            }
            starpu_data_release(*handle);
            handle++;
        }
    return MORSE_SUCCESS;
}

271
272
273
274
275
276
277
/**
 * For older revision of StarPU, STARPU_MAIN_RAM is not defined
 */
#ifndef STARPU_MAIN_RAM
#define STARPU_MAIN_RAM 0
#endif

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
int RUNTIME_desc_getoncpu( MORSE_desc_t *desc )
{
    starpu_data_handle_t *handle = (starpu_data_handle_t*)(desc->schedopt);
    int lmt = desc->lmt;
    int lnt = desc->lnt;
    int m, n;

    for (n = 0; n < lnt; n++)
        for (m = 0; m < lmt; m++)
        {
            if ( (*handle == NULL) ||
                 !morse_desc_islocal( desc, m, n ) )
            {
                handle++;
                continue;
            }

            starpu_data_acquire(*handle, STARPU_R);
            starpu_data_release(*handle);
            handle++;
        }

    return MORSE_SUCCESS;
}

303
void *RUNTIME_desc_getaddr( const MORSE_desc_t *desc, int m, int n )
304
{
305
306
307
    int64_t im = m + (desc->i / desc->mb);
    int64_t jn = n + (desc->j / desc->nb);

308
    starpu_data_handle_t *ptrtile = (starpu_data_handle_t*)(desc->schedopt);
309
    ptrtile += ((int64_t)(desc->lmt) * (int64_t)jn + (int64_t)im);
310
311

    if (*ptrtile == NULL) {
312
        int64_t eltsze = MORSE_Element_Size(desc->dtyp);
313
314
        int myrank = desc->myrank;
        int owner  = desc->get_rankof( desc, m, n );
315
316
        int tempmm = (im == desc->lmt-1) ? (desc->lm - im * desc->mb) : desc->mb;
        int tempnn = (jn == desc->lnt-1) ? (desc->ln - jn * desc->nb) : desc->nb;
317
318

        if ( myrank == owner ) {
319
320
321
            if ( desc->get_blkaddr(desc, m, n) == (void*)NULL ) {
                starpu_matrix_data_register(ptrtile, -1,
                                            (uintptr_t) NULL,
322
                                            BLKLDD(desc, im), tempmm, tempnn, eltsze);
323
324
            }
            else {
Guillaume Sylvand's avatar
Guillaume Sylvand committed
325
                starpu_matrix_data_register(ptrtile, STARPU_MAIN_RAM,
326
                                            (uintptr_t)desc->get_blkaddr(desc, m, n),
327
                                            BLKLDD(desc, im), tempmm, tempnn, eltsze);
328
            }
329
330
331
332
        }
        else {
            starpu_matrix_data_register(ptrtile, -1,
                                        (uintptr_t) NULL,
333
                                        BLKLDD(desc, im), tempmm, tempnn, eltsze);
334
335
        }

336
#if defined(CHAMELEON_USE_MPI)
337
        {
338
            int64_t block_ind = desc->lmt * jn + im;
339
340
            starpu_mpi_data_register(*ptrtile, (desc->id << tag_sep) | (block_ind), owner);
        }
341
#endif /* defined(CHAMELEON_USE_MPI) */
342
343
344
345
    }

    return (void *)(*ptrtile);
}