context.c 11.6 KB
Newer Older
1
/**
2 3
 *
 * @file context.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
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
10
 ***
11
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
12
 * @brief Chameleon context management routines
13
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
14
 * @version 1.0.0
15 16 17 18 19
 * @author Jakub Kurzak
 * @author Mathieu Faverge
 * @author Cedric Castagnede
 * @date 2012-09-15
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
20
 ***
21 22 23 24 25
 *
 * @defgroup Options
 * @brief Group routines exposed to users to handle options
 *
 */
26 27 28
#include "control/common.h"
#include "control/auxiliary.h"
#include "control/context.h"
Mathieu Faverge's avatar
Mathieu Faverge committed
29
#include "chameleon/runtime.h"
30

31 32 33 34
#if !defined(CHAMELEON_SIMULATION)
#include "coreblas.h"
#endif

35
/**
36
 *  Global data
37
 */
38
/* master threads context lookup table */
Mathieu Faverge's avatar
Mathieu Faverge committed
39
static CHAM_context_t *chameleon_ctxt = NULL;
40

41
/**
42
 *  Create new context
43
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
44
CHAM_context_t *chameleon_context_create()
45
{
Mathieu Faverge's avatar
Mathieu Faverge committed
46
    CHAM_context_t *chamctxt;
47

Mathieu Faverge's avatar
Mathieu Faverge committed
48 49
    if ( chameleon_ctxt != NULL ) {
        chameleon_error("chameleon_context_create", "a context is already existing\n");
50 51 52
        return NULL;
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
53 54 55
    chamctxt = (CHAM_context_t*)malloc(sizeof(CHAM_context_t));
    if (chamctxt == NULL) {
        chameleon_error("chameleon_context_create", "malloc() failed");
56 57 58 59 60
        return NULL;
    }

    /* These initializations are just in case the user
       disables autotuning and does not set nb and ib */
Mathieu Faverge's avatar
Mathieu Faverge committed
61 62 63
    chamctxt->nb                 = 128;
    chamctxt->ib                 = 32;
    chamctxt->rhblock            = 4;
64

Mathieu Faverge's avatar
Mathieu Faverge committed
65 66 67
    chamctxt->nworkers           = 1;
    chamctxt->ncudas             = 0;
    chamctxt->nthreads_per_worker= 1;
68

Mathieu Faverge's avatar
Mathieu Faverge committed
69 70 71 72 73
    chamctxt->warnings_enabled     = CHAMELEON_TRUE;
    chamctxt->autotuning_enabled   = CHAMELEON_TRUE;
    chamctxt->parallel_enabled     = CHAMELEON_FALSE;
    chamctxt->profiling_enabled    = CHAMELEON_FALSE;
    chamctxt->progress_enabled     = CHAMELEON_FALSE;
74

Mathieu Faverge's avatar
Mathieu Faverge committed
75 76
    chamctxt->householder        = ChamFlatHouseholder;
    chamctxt->translation        = ChamOutOfPlace;
77 78 79


    /* Initialize scheduler */
Mathieu Faverge's avatar
Mathieu Faverge committed
80
    RUNTIME_context_create(chamctxt);
81

Mathieu Faverge's avatar
Mathieu Faverge committed
82 83
    chameleon_ctxt = chamctxt;
    return chamctxt;
84 85 86
}


87
/**
88
 *  Return context for a thread
89
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
90
CHAM_context_t *chameleon_context_self()
91
{
Mathieu Faverge's avatar
Mathieu Faverge committed
92
    return chameleon_ctxt;
93 94
}

95
/**
96
 *  Clean the context
97
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
98
int chameleon_context_destroy(){
99

Mathieu Faverge's avatar
Mathieu Faverge committed
100 101 102
    RUNTIME_context_destroy(chameleon_ctxt);
    free(chameleon_ctxt);
    chameleon_ctxt = NULL;
103

Mathieu Faverge's avatar
Mathieu Faverge committed
104
    return CHAMELEON_SUCCESS;
105 106
}

107
/**
108
 *
109
 * @ingroup Options
110
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
111
 *  CHAMELEON_Enable - Enable CHAMELEON feature.
112 113 114
 *
 *******************************************************************************
 *
115
 * @param[in] option
116
 *          Feature to be enabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
117 118 119 120 121
 *          @arg CHAMELEON_WARNINGS   printing of warning messages,
 *          @arg CHAMELEON_AUTOTUNING autotuning for tile size and inner block size.
 *          @arg CHAMELEON_PROFILING_MODE  activate profiling of kernels
 *          @arg CHAMELEON_PROGRESS  activate progress indicator
 *          @arg CHAMELEON_GEMM3M  Use z/cgemm3m for complexe matrix-matrix products
122 123 124 125
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
126
 *          \retval CHAMELEON_SUCCESS successful exit
127
 *
128
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
129
int CHAMELEON_Enable(int option)
130
{
Mathieu Faverge's avatar
Mathieu Faverge committed
131
    CHAM_context_t *chamctxt;
132

Mathieu Faverge's avatar
Mathieu Faverge committed
133 134 135
    chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Enable", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
136
        return CHAMELEON_ERR_NOT_INITIALIZED;
137 138 139 140
    }

    switch (option)
    {
Mathieu Faverge's avatar
Mathieu Faverge committed
141
        case CHAMELEON_WARNINGS:
Mathieu Faverge's avatar
Mathieu Faverge committed
142
            chamctxt->warnings_enabled = CHAMELEON_TRUE;
143
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
144
        case CHAMELEON_AUTOTUNING:
Mathieu Faverge's avatar
Mathieu Faverge committed
145
            chamctxt->autotuning_enabled = CHAMELEON_TRUE;
146
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
147
        case CHAMELEON_PROFILING_MODE:
Mathieu Faverge's avatar
Mathieu Faverge committed
148
            chamctxt->profiling_enabled = CHAMELEON_TRUE;
149
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
150
        case CHAMELEON_PROGRESS:
Mathieu Faverge's avatar
Mathieu Faverge committed
151
            chamctxt->progress_enabled = CHAMELEON_TRUE;
152
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
153
        case CHAMELEON_GEMM3M:
154
#if defined(CBLAS_HAS_ZGEMM3M) && !defined(CHAMELEON_SIMULATION)
155
            set_coreblas_gemm3m_enabled(1);
156
#else
Mathieu Faverge's avatar
Mathieu Faverge committed
157
            chameleon_error("CHAMELEON_Enable", "cannot enable GEMM3M (not available in cblas)");
158 159
#endif
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
160
        /* case CHAMELEON_PARALLEL: */
Mathieu Faverge's avatar
Mathieu Faverge committed
161
        /*     chamctxt->parallel_enabled = CHAMELEON_TRUE; */
162 163
        /*     break; */
        default:
Mathieu Faverge's avatar
Mathieu Faverge committed
164
            chameleon_error("CHAMELEON_Enable", "illegal parameter value");
Mathieu Faverge's avatar
Mathieu Faverge committed
165 166
            return CHAMELEON_ERR_ILLEGAL_VALUE;
        case CHAMELEON_BOUND:
167 168
            break;
    }
169

170 171 172
    /* Enable at the lower level if required */
    RUNTIME_enable( option );

Mathieu Faverge's avatar
Mathieu Faverge committed
173
    return CHAMELEON_SUCCESS;
174 175
}

176
/**
177
 *
178
 * @ingroup Options
179
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
180
 *  CHAMELEON_Disable - Disable CHAMELEON feature.
181 182 183
 *
 *******************************************************************************
 *
184
 * @param[in] option
185
 *          Feature to be disabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
186 187 188 189 190
 *          @arg CHAMELEON_WARNINGS   printing of warning messages,
 *          @arg CHAMELEON_AUTOTUNING autotuning for tile size and inner block size.
 *          @arg CHAMELEON_PROFILING_MODE  deactivate profiling of kernels
 *          @arg CHAMELEON_PROGRESS  deactivate progress indicator
 *          @arg CHAMELEON_GEMM3M  Use z/cgemm3m for complexe matrix-matrix products
191 192 193 194
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
195
 *          \retval CHAMELEON_SUCCESS successful exit
196
 *
197
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
198
int CHAMELEON_Disable(int option)
199
{
Mathieu Faverge's avatar
Mathieu Faverge committed
200
    CHAM_context_t *chamctxt;
201

Mathieu Faverge's avatar
Mathieu Faverge committed
202 203 204
    chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Disable", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
205
        return CHAMELEON_ERR_NOT_INITIALIZED;
206 207 208
    }
    switch ( option )
    {
Mathieu Faverge's avatar
Mathieu Faverge committed
209
        case CHAMELEON_WARNINGS:
Mathieu Faverge's avatar
Mathieu Faverge committed
210
            chamctxt->warnings_enabled = CHAMELEON_FALSE;
211
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
212
        case CHAMELEON_AUTOTUNING:
Mathieu Faverge's avatar
Mathieu Faverge committed
213
            chamctxt->autotuning_enabled = CHAMELEON_FALSE;
214
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
215
        case CHAMELEON_PROFILING_MODE:
Mathieu Faverge's avatar
Mathieu Faverge committed
216
            chamctxt->profiling_enabled = CHAMELEON_FALSE;
217
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
218
        case CHAMELEON_PROGRESS:
Mathieu Faverge's avatar
Mathieu Faverge committed
219
            chamctxt->progress_enabled = CHAMELEON_FALSE;
220
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
221
        case CHAMELEON_GEMM3M:
222
#if defined(CBLAS_HAS_ZGEMM3M) && !defined(CHAMELEON_SIMULATION)
223
            set_coreblas_gemm3m_enabled(0);
224
#endif
225
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
226
        case CHAMELEON_PARALLEL_MODE:
Mathieu Faverge's avatar
Mathieu Faverge committed
227
            chamctxt->parallel_enabled = CHAMELEON_FALSE;
228 229
            break;
        default:
Mathieu Faverge's avatar
Mathieu Faverge committed
230
            chameleon_error("CHAMELEON_Disable", "illegal parameter value");
Mathieu Faverge's avatar
Mathieu Faverge committed
231
            return CHAMELEON_ERR_ILLEGAL_VALUE;
232
    }
233

234 235 236
    /* Disable at the lower level if required */
    RUNTIME_disable( option );

Mathieu Faverge's avatar
Mathieu Faverge committed
237
    return CHAMELEON_SUCCESS;
238 239
}

240
/**
241
 *
242
 * @ingroup Options
243
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
244
 *  CHAMELEON_Set - Set CHAMELEON parameter.
245 246 247 248 249
 *
 *******************************************************************************
 *
 * @param[in] param
 *          Feature to be enabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
250 251
 *          @arg CHAMELEON_TILE_SIZE:        size matrix tile,
 *          @arg CHAMELEON_INNER_BLOCK_SIZE: size of tile inner block,
252 253 254 255 256 257 258
 *
 * @param[in] value
 *          Value of the parameter.
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
259
 *          \retval CHAMELEON_SUCCESS successful exit
260
 *
261
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
262
int CHAMELEON_Set(int param, int value)
263
{
Mathieu Faverge's avatar
Mathieu Faverge committed
264
    CHAM_context_t *chamctxt;
265

Mathieu Faverge's avatar
Mathieu Faverge committed
266 267 268
    chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Set", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
269
        return CHAMELEON_ERR_NOT_INITIALIZED;
270 271
    }
    switch (param) {
Mathieu Faverge's avatar
Mathieu Faverge committed
272
        case CHAMELEON_TILE_SIZE:
273
            if (value <= 0) {
Mathieu Faverge's avatar
Mathieu Faverge committed
274
                chameleon_error("CHAMELEON_Set", "negative tile size");
Mathieu Faverge's avatar
Mathieu Faverge committed
275
                return CHAMELEON_ERR_ILLEGAL_VALUE;
276
            }
Mathieu Faverge's avatar
Mathieu Faverge committed
277 278 279 280
            chamctxt->nb = value;
            if ( chamctxt->autotuning_enabled ) {
                chamctxt->autotuning_enabled = CHAMELEON_FALSE;
                chameleon_warning("CHAMELEON_Set", "autotuning has been automatically disable\n");
281 282
            }
            /* Limit ib to nb */
Mathieu Faverge's avatar
Mathieu Faverge committed
283
            chamctxt->ib = chameleon_min( chamctxt->nb, chamctxt->ib );
284
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
285
        case CHAMELEON_INNER_BLOCK_SIZE:
286
            if (value <= 0) {
Mathieu Faverge's avatar
Mathieu Faverge committed
287
                chameleon_error("CHAMELEON_Set", "negative inner block size");
Mathieu Faverge's avatar
Mathieu Faverge committed
288
                return CHAMELEON_ERR_ILLEGAL_VALUE;
289
            }
Mathieu Faverge's avatar
Mathieu Faverge committed
290 291
            if (value > chamctxt->nb) {
                chameleon_error("CHAMELEON_Set", "inner block larger than tile");
Mathieu Faverge's avatar
Mathieu Faverge committed
292
                return CHAMELEON_ERR_ILLEGAL_VALUE;
293
            }
Mathieu Faverge's avatar
Mathieu Faverge committed
294 295
            /* if (chamctxt->nb % value != 0) { */
            /*     chameleon_error("CHAMELEON_Set", "inner block does not divide tile"); */
Mathieu Faverge's avatar
Mathieu Faverge committed
296
            /*     return CHAMELEON_ERR_ILLEGAL_VALUE; */
297
            /* } */
Mathieu Faverge's avatar
Mathieu Faverge committed
298
            chamctxt->ib = value;
299

Mathieu Faverge's avatar
Mathieu Faverge committed
300 301 302
            if ( chamctxt->autotuning_enabled ) {
                chamctxt->autotuning_enabled = CHAMELEON_FALSE;
                chameleon_warning("CHAMELEON_Set", "autotuning has been automatically disable\n");
303 304
            }
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
305 306
        case CHAMELEON_HOUSEHOLDER_MODE:
            if (value != ChamFlatHouseholder && value != ChamTreeHouseholder) {
Mathieu Faverge's avatar
Mathieu Faverge committed
307
                chameleon_error("CHAMELEON_Set", "illegal value of CHAMELEON_HOUSEHOLDER_MODE");
Mathieu Faverge's avatar
Mathieu Faverge committed
308
                return CHAMELEON_ERR_ILLEGAL_VALUE;
309
            }
Mathieu Faverge's avatar
Mathieu Faverge committed
310
            chamctxt->householder = value;
311
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
312
        case CHAMELEON_HOUSEHOLDER_SIZE:
313
            if (value <= 0) {
Mathieu Faverge's avatar
Mathieu Faverge committed
314
                chameleon_error("CHAMELEON_Set", "negative householder size");
Mathieu Faverge's avatar
Mathieu Faverge committed
315
                return CHAMELEON_ERR_ILLEGAL_VALUE;
316
            }
Mathieu Faverge's avatar
Mathieu Faverge committed
317
            chamctxt->rhblock = value;
318
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
319 320
        case CHAMELEON_TRANSLATION_MODE:
            if (value != ChamInPlace && value != ChamOutOfPlace) {
Mathieu Faverge's avatar
Mathieu Faverge committed
321
                chameleon_error("CHAMELEON_Set", "illegal value of CHAMELEON_TRANSLATION_MODE");
Mathieu Faverge's avatar
Mathieu Faverge committed
322
                return CHAMELEON_ERR_ILLEGAL_VALUE;
323
            }
Mathieu Faverge's avatar
Mathieu Faverge committed
324
            chamctxt->translation = value;
325 326
            break;
        default:
Mathieu Faverge's avatar
Mathieu Faverge committed
327
            chameleon_error("CHAMELEON_Set", "unknown parameter");
Mathieu Faverge's avatar
Mathieu Faverge committed
328
            return CHAMELEON_ERR_ILLEGAL_VALUE;
329 330
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
331
    return CHAMELEON_SUCCESS;
332 333
}

334
/**
335
 *
336
 * @ingroup Options
337
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
338
 *  CHAMELEON_Get - Get value of CHAMELEON parameter.
339 340 341 342 343
 *
 *******************************************************************************
 *
 * @param[in] param
 *          Feature to be enabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
344 345
 *          @arg CHAMELEON_TILE_SIZE:        size matrix tile,
 *          @arg CHAMELEON_INNER_BLOCK_SIZE: size of tile inner block,
346 347 348 349 350 351 352
 *
 * @param[out] value
 *          Value of the parameter.
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
353
 *          \retval CHAMELEON_SUCCESS successful exit
354
 *
355
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
356
int CHAMELEON_Get(int param, int *value)
357
{
Mathieu Faverge's avatar
Mathieu Faverge committed
358
    CHAM_context_t *chamctxt;
359

Mathieu Faverge's avatar
Mathieu Faverge committed
360 361 362
    chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Get", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
363
        return CHAMELEON_ERR_NOT_INITIALIZED;
364 365
    }
    switch (param) {
Mathieu Faverge's avatar
Mathieu Faverge committed
366
        case CHAMELEON_TILE_SIZE:
Mathieu Faverge's avatar
Mathieu Faverge committed
367
            *value = chamctxt->nb;
Mathieu Faverge's avatar
Mathieu Faverge committed
368 369
            return CHAMELEON_SUCCESS;
        case CHAMELEON_INNER_BLOCK_SIZE:
Mathieu Faverge's avatar
Mathieu Faverge committed
370
            *value = chamctxt->ib;
Mathieu Faverge's avatar
Mathieu Faverge committed
371 372
            return CHAMELEON_SUCCESS;
        case CHAMELEON_HOUSEHOLDER_MODE:
Mathieu Faverge's avatar
Mathieu Faverge committed
373
            *value = chamctxt->householder;
Mathieu Faverge's avatar
Mathieu Faverge committed
374 375
            return CHAMELEON_SUCCESS;
        case CHAMELEON_HOUSEHOLDER_SIZE:
Mathieu Faverge's avatar
Mathieu Faverge committed
376
            *value = chamctxt->rhblock;
Mathieu Faverge's avatar
Mathieu Faverge committed
377 378
            return CHAMELEON_SUCCESS;
        case CHAMELEON_TRANSLATION_MODE:
Mathieu Faverge's avatar
Mathieu Faverge committed
379
            *value = chamctxt->translation;
Mathieu Faverge's avatar
Mathieu Faverge committed
380
            return CHAMELEON_SUCCESS;
381
        default:
Mathieu Faverge's avatar
Mathieu Faverge committed
382
            chameleon_error("CHAMELEON_Get", "unknown parameter");
Mathieu Faverge's avatar
Mathieu Faverge committed
383
            return CHAMELEON_ERR_ILLEGAL_VALUE;
384 385
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
386
    return CHAMELEON_SUCCESS;
387
}