runtime_descriptor.c 9.44 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-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
33
34
35
36
37
38
39
40
41
42
43
44
45

/* Take 24 bits for the tile id, and 7 bits for descriptor id */
#define TAG_WIDTH_MIN 20
static int tag_width = 31;
static int tag_sep   = 24;

#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

46
47
48
49
50
51
52
53
54
55
56
57
58
#ifdef STARPU_MALLOC_SIMULATION_FOLDED
#define FOLDED STARPU_MALLOC_SIMULATION_FOLDED
#else
#define FOLDED 0
#endif

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;

59
    if (starpu_malloc_flags(&mat, size, STARPU_MALLOC_PINNED|FOLDED|STARPU_MALLOC_COUNT) != 0)
60
61
62
63
64
65
66
67
68
69
        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
70
    starpu_free_flags(mat, size, STARPU_MALLOC_PINNED|FOLDED|STARPU_MALLOC_COUNT);
71
72
73
#endif
}

74
75
76
77
78
void RUNTIME_desc_create( MORSE_desc_t *desc )
{
    int64_t lmt = desc->lmt;
    int64_t lnt = desc->lnt;
    starpu_data_handle_t *tiles;
79
    (void)tiles;
80
81
82
83

    desc->occurences = 1;

    /*
84
85
     * Allocate starpu_handle_t array (handlers are initialized on the fly when
     * discovered by any algorithm to save space)
86
87
88
89
90
     */
    desc->schedopt = (void*)calloc(lnt*lmt,sizeof(starpu_data_handle_t));
    assert(desc->schedopt);
    tiles = (starpu_data_handle_t*)(desc->schedopt);

91
#if defined(CHAMELEON_USE_CUDA)
92
93
94
95
    if (desc->use_mat == 1 && desc->register_mat == 1){
        /*
         * Register allocated memory as CUDA pinned memory
         */
96
        {
97
            int64_t eltsze = MORSE_Element_Size(desc->dtyp);
98
99
100
101
102
103
104
            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");
            }
105
106
107
108
        }
    }
#endif

109
#if defined(CHAMELEON_USE_MPI)
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
    /*
     * Check that we are not going over MPI tag limitations
     */
    {
        static int _tag_mpi_initialized_ = 0;

        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");
            }

126
            while ( ((uintptr_t)(1UL<<tag_width - 1) > (uint)(*tag_ub) )
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
                    && (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)) ) {
            morse_error("RUNTIME_desc_create", "Too many tiles in the descriptor for MPI tags");
            return;
        }
        assert(lmt*lmt<=(1<<tag_sep));

        if (desc->id >= 1UL<<(tag_width-tag_sep)) {
            morse_error("RUNTIME_desc_create", "Number of descriptor available in MPI mode out of stock");
            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
162
     * and unregister the GPU data
163
164
165
166
167
168
169
170
171
172
     */
    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++)
            {
173
                if (*handle == NULL)
174
175
176
177
178
179
180
181
182
                {
                    handle++;
                    continue;
                }
                //printf("\nUnregister %d %d %d", MORSE_My_Mpi_Rank(), m, n);
                starpu_data_unregister(*handle);
                handle++;
            }

183
#if defined(CHAMELEON_USE_CUDA)
184
        if (desc->use_mat == 1 && desc->register_mat == 1){
185
            int64_t eltsze = MORSE_Element_Size(desc->dtyp);
186
187
188
189
190
191
            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)",
192
193
                              "cudaHostUnregister failed to unregister the "
                              "pinned memory associated to the matrix");
194
195
            }
        }
196
#endif /* defined(CHAMELEON_USE_CUDA) */
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

        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;
}

214
215
/* TODO: Acquire/Release/GetonCPU need to be studied carefully and fixed
 * because we are not using them correctly */
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
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;
}

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;
}

void *RUNTIME_desc_getaddr( MORSE_desc_t *desc, int m, int n )
{
    starpu_data_handle_t *ptrtile = (starpu_data_handle_t*)(desc->schedopt);
    ptrtile += ((int64_t)(desc->lmt) * (int64_t)n + (int64_t)m);

    if (*ptrtile == NULL) {
291
        int64_t eltsze = MORSE_Element_Size(desc->dtyp);
292
293
294
295
296
297
        int myrank = desc->myrank;
        int owner  = desc->get_rankof( desc, m, n );
        int tempmm = (m == desc->lmt-1) ? (desc->lm - m * desc->mb) : desc->mb;
        int tempnn = (n == desc->lnt-1) ? (desc->ln - n * desc->nb) : desc->nb;

        if ( myrank == owner ) {
298
299
300
301
302
303
            if ( desc->get_blkaddr(desc, m, n) == (void*)NULL ) {
                starpu_matrix_data_register(ptrtile, -1,
                                            (uintptr_t) NULL,
                                            BLKLDD(desc, m), tempmm, tempnn, eltsze);
            }
            else {
Guillaume Sylvand's avatar
Guillaume Sylvand committed
304
                starpu_matrix_data_register(ptrtile, STARPU_MAIN_RAM,
305
306
307
                                            (uintptr_t)desc->get_blkaddr(desc, m, n),
                                            BLKLDD(desc, m), tempmm, tempnn, eltsze);
            }
308
309
310
311
312
313
314
        }
        else {
            starpu_matrix_data_register(ptrtile, -1,
                                        (uintptr_t) NULL,
                                        BLKLDD(desc, m), tempmm, tempnn, eltsze);
        }

315
#if defined(CHAMELEON_USE_MPI)
316
317
318
319
        {
            int64_t block_ind = desc->lmt * n + m;
            starpu_mpi_data_register(*ptrtile, (desc->id << tag_sep) | (block_ind), owner);
        }
320
#endif /* defined(CHAMELEON_USE_MPI) */
321
322
323
324
    }

    return (void *)(*ptrtile);
}