context.c 11.4 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.
7 8
 * @copyright 2012-2016 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
 *                      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 26
 *
 * @defgroup Options
 * @brief Group routines exposed to users to handle options
 *
 */

27 28
#include <stdlib.h>
#if defined( _WIN32 ) || defined( _WIN64 )
29
#include "control/morsewinthread.h"
30 31 32 33
#else
#include <pthread.h>
#endif

34 35 36
#include "control/common.h"
#include "control/auxiliary.h"
#include "control/context.h"
Mathieu Faverge's avatar
Mathieu Faverge committed
37
#include "chameleon/runtime.h"
38

39 40 41 42
#if !defined(CHAMELEON_SIMULATION)
#include "coreblas.h"
#endif

43
/**
44
 *  Global data
45
 */
46
/* master threads context lookup table */
Mathieu Faverge's avatar
Mathieu Faverge committed
47
static CHAM_context_t *morse_ctxt = NULL;
48

49
/**
50
 *  Create new context
51
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
52
CHAM_context_t *morse_context_create()
53
{
Mathieu Faverge's avatar
Mathieu Faverge committed
54
    CHAM_context_t *morse;
55 56 57 58 59 60

    if ( morse_ctxt != NULL ) {
        morse_error("morse_context_create", "a context is already existing\n");
        return NULL;
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
61
    morse = (CHAM_context_t*)malloc(sizeof(CHAM_context_t));
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    if (morse == NULL) {
        morse_error("morse_context_create", "malloc() failed");
        return NULL;
    }

    /* These initializations are just in case the user
       disables autotuning and does not set nb and ib */
    morse->nb                 = 128;
    morse->ib                 = 32;
    morse->rhblock            = 4;

    morse->nworkers           = 1;
    morse->ncudas             = 0;
    morse->nthreads_per_worker= 1;

Mathieu Faverge's avatar
Mathieu Faverge committed
77 78 79 80 81
    morse->warnings_enabled     = CHAMELEON_TRUE;
    morse->autotuning_enabled   = CHAMELEON_TRUE;
    morse->parallel_enabled     = CHAMELEON_FALSE;
    morse->profiling_enabled    = CHAMELEON_FALSE;
    morse->progress_enabled     = CHAMELEON_FALSE;
82

Mathieu Faverge's avatar
Mathieu Faverge committed
83 84
    morse->householder        = ChamFlatHouseholder;
    morse->translation        = ChamOutOfPlace;
85 86 87 88 89 90 91 92 93 94


    /* Initialize scheduler */
    RUNTIME_context_create(morse);

    morse_ctxt = morse;
    return morse;
}


95
/**
96
 *  Return context for a thread
97
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
98
CHAM_context_t *morse_context_self()
99 100 101 102
{
    return morse_ctxt;
}

103
/**
104
 *  Clean the context
105
 */
106 107 108 109 110
int morse_context_destroy(){

    RUNTIME_context_destroy(morse_ctxt);
    free(morse_ctxt);
    morse_ctxt = NULL;
111

Mathieu Faverge's avatar
Mathieu Faverge committed
112
    return CHAMELEON_SUCCESS;
113 114
}

115
/**
116
 *
117
 * @ingroup Options
118
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
119
 *  CHAMELEON_Enable - Enable CHAMELEON feature.
120 121 122
 *
 *******************************************************************************
 *
123
 * @param[in] option
124
 *          Feature to be enabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
125 126 127 128 129
 *          @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
130 131 132 133
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
134
 *          \retval CHAMELEON_SUCCESS successful exit
135
 *
136
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
137
int CHAMELEON_Enable(int option)
138
{
Mathieu Faverge's avatar
Mathieu Faverge committed
139
    CHAM_context_t *morse;
140 141 142

    morse = morse_context_self();
    if (morse == NULL) {
Mathieu Faverge's avatar
Mathieu Faverge committed
143 144
        morse_error("CHAMELEON_Enable", "CHAMELEON not initialized");
        return CHAMELEON_ERR_NOT_INITIALIZED;
145 146 147 148
    }

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

178 179 180
    /* Enable at the lower level if required */
    RUNTIME_enable( option );

Mathieu Faverge's avatar
Mathieu Faverge committed
181
    return CHAMELEON_SUCCESS;
182 183
}

184
/**
185
 *
186
 * @ingroup Options
187
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
188
 *  CHAMELEON_Disable - Disable CHAMELEON feature.
189 190 191
 *
 *******************************************************************************
 *
192
 * @param[in] option
193
 *          Feature to be disabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
194 195 196 197 198
 *          @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
199 200 201 202
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
203
 *          \retval CHAMELEON_SUCCESS successful exit
204
 *
205
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
206
int CHAMELEON_Disable(int option)
207
{
Mathieu Faverge's avatar
Mathieu Faverge committed
208
    CHAM_context_t *morse;
209 210 211

    morse = morse_context_self();
    if (morse == NULL) {
Mathieu Faverge's avatar
Mathieu Faverge committed
212 213
        morse_error("CHAMELEON_Disable", "CHAMELEON not initialized");
        return CHAMELEON_ERR_NOT_INITIALIZED;
214 215 216
    }
    switch ( option )
    {
Mathieu Faverge's avatar
Mathieu Faverge committed
217 218
        case CHAMELEON_WARNINGS:
            morse->warnings_enabled = CHAMELEON_FALSE;
219
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
220 221
        case CHAMELEON_AUTOTUNING:
            morse->autotuning_enabled = CHAMELEON_FALSE;
222
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
223 224
        case CHAMELEON_PROFILING_MODE:
            morse->profiling_enabled = CHAMELEON_FALSE;
225
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
226 227
        case CHAMELEON_PROGRESS:
            morse->progress_enabled = CHAMELEON_FALSE;
228
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
229
        case CHAMELEON_GEMM3M:
230
#if defined(CBLAS_HAS_ZGEMM3M) && !defined(CHAMELEON_SIMULATION)
231
            set_coreblas_gemm3m_enabled(0);
232
#endif
233
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
234 235
        case CHAMELEON_PARALLEL_MODE:
            morse->parallel_enabled = CHAMELEON_FALSE;
236 237
            break;
        default:
Mathieu Faverge's avatar
Mathieu Faverge committed
238 239
            morse_error("CHAMELEON_Disable", "illegal parameter value");
            return CHAMELEON_ERR_ILLEGAL_VALUE;
240
    }
241

242 243 244
    /* Disable at the lower level if required */
    RUNTIME_disable( option );

Mathieu Faverge's avatar
Mathieu Faverge committed
245
    return CHAMELEON_SUCCESS;
246 247
}

248
/**
249
 *
250
 * @ingroup Options
251
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
252
 *  CHAMELEON_Set - Set CHAMELEON parameter.
253 254 255 256 257
 *
 *******************************************************************************
 *
 * @param[in] param
 *          Feature to be enabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
258 259
 *          @arg CHAMELEON_TILE_SIZE:        size matrix tile,
 *          @arg CHAMELEON_INNER_BLOCK_SIZE: size of tile inner block,
260 261 262 263 264 265 266
 *
 * @param[in] value
 *          Value of the parameter.
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
267
 *          \retval CHAMELEON_SUCCESS successful exit
268
 *
269
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
270
int CHAMELEON_Set(int param, int value)
271
{
Mathieu Faverge's avatar
Mathieu Faverge committed
272
    CHAM_context_t *morse;
273 274 275

    morse = morse_context_self();
    if (morse == NULL) {
Mathieu Faverge's avatar
Mathieu Faverge committed
276 277
        morse_error("CHAMELEON_Set", "CHAMELEON not initialized");
        return CHAMELEON_ERR_NOT_INITIALIZED;
278 279
    }
    switch (param) {
Mathieu Faverge's avatar
Mathieu Faverge committed
280
        case CHAMELEON_TILE_SIZE:
281
            if (value <= 0) {
Mathieu Faverge's avatar
Mathieu Faverge committed
282 283
                morse_error("CHAMELEON_Set", "negative tile size");
                return CHAMELEON_ERR_ILLEGAL_VALUE;
284 285 286
            }
            morse->nb = value;
            if ( morse->autotuning_enabled ) {
Mathieu Faverge's avatar
Mathieu Faverge committed
287 288
                morse->autotuning_enabled = CHAMELEON_FALSE;
                morse_warning("CHAMELEON_Set", "autotuning has been automatically disable\n");
289 290
            }
            /* Limit ib to nb */
Mathieu Faverge's avatar
Mathieu Faverge committed
291
            morse->ib = chameleon_min( morse->nb, morse->ib );
292
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
293
        case CHAMELEON_INNER_BLOCK_SIZE:
294
            if (value <= 0) {
Mathieu Faverge's avatar
Mathieu Faverge committed
295 296
                morse_error("CHAMELEON_Set", "negative inner block size");
                return CHAMELEON_ERR_ILLEGAL_VALUE;
297 298
            }
            if (value > morse->nb) {
Mathieu Faverge's avatar
Mathieu Faverge committed
299 300
                morse_error("CHAMELEON_Set", "inner block larger than tile");
                return CHAMELEON_ERR_ILLEGAL_VALUE;
301 302
            }
            /* if (morse->nb % value != 0) { */
Mathieu Faverge's avatar
Mathieu Faverge committed
303 304
            /*     morse_error("CHAMELEON_Set", "inner block does not divide tile"); */
            /*     return CHAMELEON_ERR_ILLEGAL_VALUE; */
305 306 307 308
            /* } */
            morse->ib = value;

            if ( morse->autotuning_enabled ) {
Mathieu Faverge's avatar
Mathieu Faverge committed
309 310
                morse->autotuning_enabled = CHAMELEON_FALSE;
                morse_warning("CHAMELEON_Set", "autotuning has been automatically disable\n");
311 312
            }
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
313 314 315 316
        case CHAMELEON_HOUSEHOLDER_MODE:
            if (value != ChamFlatHouseholder && value != ChamTreeHouseholder) {
                morse_error("CHAMELEON_Set", "illegal value of CHAMELEON_HOUSEHOLDER_MODE");
                return CHAMELEON_ERR_ILLEGAL_VALUE;
317 318 319
            }
            morse->householder = value;
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
320
        case CHAMELEON_HOUSEHOLDER_SIZE:
321
            if (value <= 0) {
Mathieu Faverge's avatar
Mathieu Faverge committed
322 323
                morse_error("CHAMELEON_Set", "negative householder size");
                return CHAMELEON_ERR_ILLEGAL_VALUE;
324 325 326
            }
            morse->rhblock = value;
            break;
Mathieu Faverge's avatar
Mathieu Faverge committed
327 328 329 330
        case CHAMELEON_TRANSLATION_MODE:
            if (value != ChamInPlace && value != ChamOutOfPlace) {
                morse_error("CHAMELEON_Set", "illegal value of CHAMELEON_TRANSLATION_MODE");
                return CHAMELEON_ERR_ILLEGAL_VALUE;
331 332 333 334
            }
            morse->translation = value;
            break;
        default:
Mathieu Faverge's avatar
Mathieu Faverge committed
335 336
            morse_error("CHAMELEON_Set", "unknown parameter");
            return CHAMELEON_ERR_ILLEGAL_VALUE;
337 338
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
339
    return CHAMELEON_SUCCESS;
340 341
}

342
/**
343
 *
344
 * @ingroup Options
345
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
346
 *  CHAMELEON_Get - Get value of CHAMELEON parameter.
347 348 349 350 351
 *
 *******************************************************************************
 *
 * @param[in] param
 *          Feature to be enabled:
Mathieu Faverge's avatar
Mathieu Faverge committed
352 353
 *          @arg CHAMELEON_TILE_SIZE:        size matrix tile,
 *          @arg CHAMELEON_INNER_BLOCK_SIZE: size of tile inner block,
354 355 356 357 358 359 360
 *
 * @param[out] value
 *          Value of the parameter.
 *
 *******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
361
 *          \retval CHAMELEON_SUCCESS successful exit
362
 *
363
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
364
int CHAMELEON_Get(int param, int *value)
365
{
Mathieu Faverge's avatar
Mathieu Faverge committed
366
    CHAM_context_t *morse;
367 368 369

    morse = morse_context_self();
    if (morse == NULL) {
Mathieu Faverge's avatar
Mathieu Faverge committed
370 371
        morse_error("CHAMELEON_Get", "CHAMELEON not initialized");
        return CHAMELEON_ERR_NOT_INITIALIZED;
372 373
    }
    switch (param) {
Mathieu Faverge's avatar
Mathieu Faverge committed
374
        case CHAMELEON_TILE_SIZE:
375
            *value = morse->nb;
Mathieu Faverge's avatar
Mathieu Faverge committed
376 377
            return CHAMELEON_SUCCESS;
        case CHAMELEON_INNER_BLOCK_SIZE:
378
            *value = morse->ib;
Mathieu Faverge's avatar
Mathieu Faverge committed
379 380
            return CHAMELEON_SUCCESS;
        case CHAMELEON_HOUSEHOLDER_MODE:
381
            *value = morse->householder;
Mathieu Faverge's avatar
Mathieu Faverge committed
382 383
            return CHAMELEON_SUCCESS;
        case CHAMELEON_HOUSEHOLDER_SIZE:
384
            *value = morse->rhblock;
Mathieu Faverge's avatar
Mathieu Faverge committed
385 386
            return CHAMELEON_SUCCESS;
        case CHAMELEON_TRANSLATION_MODE:
387
            *value = morse->translation;
Mathieu Faverge's avatar
Mathieu Faverge committed
388
            return CHAMELEON_SUCCESS;
389
        default:
Mathieu Faverge's avatar
Mathieu Faverge committed
390 391
            morse_error("CHAMELEON_Get", "unknown parameter");
            return CHAMELEON_ERR_ILLEGAL_VALUE;
392 393
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
394
    return CHAMELEON_SUCCESS;
395
}