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"
Mathieu Faverge's avatar
Mathieu Faverge committed
30
#include <stdlib.h>
31

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

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

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

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

Mathieu Faverge's avatar
Mathieu Faverge committed
54 55 56
    chamctxt = (CHAM_context_t*)malloc(sizeof(CHAM_context_t));
    if (chamctxt == NULL) {
        chameleon_error("chameleon_context_create", "malloc() failed");
57 58 59 60 61
        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
62 63 64
    chamctxt->nb                 = 128;
    chamctxt->ib                 = 32;
    chamctxt->rhblock            = 4;
65

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

Mathieu Faverge's avatar
Mathieu Faverge committed
70 71 72 73 74
    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;
75

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


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

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


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

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

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

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

108
/**
109
 *
110
 * @ingroup Options
111
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
112
 *  CHAMELEON_Enable - Enable CHAMELEON feature.
113 114 115
 *
 *******************************************************************************
 *
116
 * @param[in] option
117
 *          Feature to be enabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
118 119 120 121 122
 *          @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
123 124 125 126
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
127
 *          \retval CHAMELEON_SUCCESS successful exit
128
 *
129
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
130
int CHAMELEON_Enable(int option)
131
{
Mathieu Faverge's avatar
Mathieu Faverge committed
132
    CHAM_context_t *chamctxt;
133

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

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

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

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

177
/**
178
 *
179
 * @ingroup Options
180
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
181
 *  CHAMELEON_Disable - Disable CHAMELEON feature.
182 183 184
 *
 *******************************************************************************
 *
185
 * @param[in] option
186
 *          Feature to be disabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
187 188 189 190 191
 *          @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
192 193 194 195
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
196
 *          \retval CHAMELEON_SUCCESS successful exit
197
 *
198
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
199
int CHAMELEON_Disable(int option)
200
{
Mathieu Faverge's avatar
Mathieu Faverge committed
201
    CHAM_context_t *chamctxt;
202

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

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

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

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

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

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

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

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

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

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