context.c 12 KB
Newer Older
1 2
/**
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
3 4
 * @copyright 2009-2014 The University of Tennessee and The University of
 *                      Tennessee Research Foundation. All rights reserved.
5
 * @copyright (c) 2012-2016 Inria. All rights reserved.
6
 * @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
7 8 9 10 11 12 13 14 15 16 17
 *
 **/

/**
 *
 * @file context.c
 *
 *  MORSE auxiliary routines
 *  MORSE is a software package provided by Univ. of Tennessee,
 *  Univ. of California Berkeley and Univ. of Colorado Denver
 *
18
 * @version 0.9.0
19 20 21 22 23 24 25
 * @author Jakub Kurzak
 * @author Mathieu Faverge
 * @author Cedric Castagnede
 * @date 2012-09-15
 *
 **/

26 27 28 29 30 31 32
/**
 *
 * @defgroup Options
 * @brief Group routines exposed to users to handle options
 *
 */

33 34
#include <stdlib.h>
#if defined( _WIN32 ) || defined( _WIN64 )
35
#include "control/morsewinthread.h"
36 37 38 39
#else
#include <pthread.h>
#endif

40 41 42
#include "control/common.h"
#include "control/auxiliary.h"
#include "control/context.h"
43
#include "chameleon/morse_runtime.h"
44

45 46 47 48
#if !defined(CHAMELEON_SIMULATION)
#include "coreblas.h"
#endif

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
/*******************************************************************************
 *  Global data
 **/
/* master threads context lookup table */
static MORSE_context_t *morse_ctxt = NULL;

/*******************************************************************************
 *  Create new context
 **/
MORSE_context_t *morse_context_create()
{
    MORSE_context_t *morse;

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

    morse = (MORSE_context_t*)malloc(sizeof(MORSE_context_t));
    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;

83
    morse->warnings_enabled     = MORSE_TRUE;
84 85 86 87
    morse->autotuning_enabled   = MORSE_TRUE;
    morse->parallel_enabled     = MORSE_FALSE;
    morse->profiling_enabled    = MORSE_FALSE;
    morse->progress_enabled     = MORSE_FALSE;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    morse->householder        = MORSE_FLAT_HOUSEHOLDER;
    morse->translation        = MORSE_OUTOFPLACE;


    /* Initialize scheduler */
    RUNTIME_context_create(morse);

    morse_ctxt = morse;
    return morse;
}


/*******************************************************************************
 *  Return context for a thread
 **/
MORSE_context_t *morse_context_self()
{
    return morse_ctxt;
}

/*******************************************************************************
 *  Clean the context
 **/

int morse_context_destroy(){

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

119 120 121
    return MORSE_SUCCESS;
}

122
/** ***************************************************************************
123
 *
124
 * @ingroup Options
125 126 127 128 129
 *
 *  MORSE_Enable - Enable MORSE feature.
 *
 *******************************************************************************
 *
130
 * @param[in] option
131 132 133 134
 *          Feature to be enabled:
 *          @arg MORSE_WARNINGS   printing of warning messages,
 *          @arg MORSE_AUTOTUNING autotuning for tile size and inner block size.
 *          @arg MORSE_PROFILING_MODE  activate profiling of kernels
135
 *          @arg MORSE_PROGRESS  activate progress indicator
136
 *          @arg MORSE_GEMM3M  Use z/cgemm3m for complexe matrix-matrix products
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 162 163 164
 *
 *******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 ******************************************************************************/
int MORSE_Enable(MORSE_enum option)
{
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Enable", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }

    switch (option)
    {
        case MORSE_WARNINGS:
            morse->warnings_enabled = MORSE_TRUE;
            break;
        case MORSE_AUTOTUNING:
            morse->autotuning_enabled = MORSE_TRUE;
            break;
        case MORSE_PROFILING_MODE:
            morse->profiling_enabled = MORSE_TRUE;
            break;
165 166 167
        case MORSE_PROGRESS:
            morse->progress_enabled = MORSE_TRUE;
            break;
168
        case MORSE_GEMM3M:
169
#if defined(CBLAS_HAS_ZGEMM3M) && !defined(CHAMELEON_SIMULATION)
170
            set_coreblas_gemm3m_enabled(1);
171 172 173 174
#else
            morse_error("MORSE_Enable", "cannot enable GEMM3M (not available in cblas)");
#endif
            break;
175 176 177 178 179 180 181 182 183
        /* case MORSE_PARALLEL: */
        /*     morse->parallel_enabled = MORSE_TRUE; */
        /*     break; */
        default:
            morse_error("MORSE_Enable", "illegal parameter value");
            return MORSE_ERR_ILLEGAL_VALUE;
        case MORSE_BOUND:
            break;
    }
184

185 186 187 188 189 190
    /* Enable at the lower level if required */
    RUNTIME_enable( option );

    return MORSE_SUCCESS;
}

191
/** ***************************************************************************
192
 *
193
 * @ingroup Options
194 195 196 197 198
 *
 *  MORSE_Disable - Disable MORSE feature.
 *
 *******************************************************************************
 *
199
 * @param[in] option
200 201 202
 *          Feature to be disabled:
 *          @arg MORSE_WARNINGS   printing of warning messages,
 *          @arg MORSE_AUTOTUNING autotuning for tile size and inner block size.
203 204
 *          @arg MORSE_PROFILING_MODE  deactivate profiling of kernels
 *          @arg MORSE_PROGRESS  deactivate progress indicator
205
 *          @arg MORSE_GEMM3M  Use z/cgemm3m for complexe matrix-matrix products
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
 *
 *******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 ******************************************************************************/
int MORSE_Disable(MORSE_enum option)
{
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Disable", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    switch ( option )
    {
        case MORSE_WARNINGS:
            morse->warnings_enabled = MORSE_FALSE;
            break;
        case MORSE_AUTOTUNING:
            morse->autotuning_enabled = MORSE_FALSE;
            break;
        case MORSE_PROFILING_MODE:
            morse->profiling_enabled = MORSE_FALSE;
            break;
233 234 235
        case MORSE_PROGRESS:
            morse->progress_enabled = MORSE_FALSE;
            break;
236
        case MORSE_GEMM3M:
237
#if defined(CBLAS_HAS_ZGEMM3M) && !defined(CHAMELEON_SIMULATION)
238
            set_coreblas_gemm3m_enabled(0);
239
#endif
240
            break;
241 242 243 244 245 246 247
        case MORSE_PARALLEL_MODE:
            morse->parallel_enabled = MORSE_FALSE;
            break;
        default:
            morse_error("MORSE_Disable", "illegal parameter value");
            return MORSE_ERR_ILLEGAL_VALUE;
    }
248

249 250 251 252 253 254
    /* Disable at the lower level if required */
    RUNTIME_disable( option );

    return MORSE_SUCCESS;
}

255
/** ***************************************************************************
256
 *
257
 * @ingroup Options
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 291 292 293 294 295 296 297
 *
 *  MORSE_Set - Set MORSE parameter.
 *
 *******************************************************************************
 *
 * @param[in] param
 *          Feature to be enabled:
 *          @arg MORSE_TILE_SIZE:        size matrix tile,
 *          @arg MORSE_INNER_BLOCK_SIZE: size of tile inner block,
 *
 * @param[in] value
 *          Value of the parameter.
 *
 *******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 ******************************************************************************/
int MORSE_Set(MORSE_enum param, int value)
{
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Set", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    switch (param) {
        case MORSE_TILE_SIZE:
            if (value <= 0) {
                morse_error("MORSE_Set", "negative tile size");
                return MORSE_ERR_ILLEGAL_VALUE;
            }
            morse->nb = value;
            if ( morse->autotuning_enabled ) {
                morse->autotuning_enabled = MORSE_FALSE;
                morse_warning("MORSE_Set", "autotuning has been automatically disable\n");
            }
            /* Limit ib to nb */
Mathieu Faverge's avatar
Mathieu Faverge committed
298
            morse->ib = chameleon_min( morse->nb, morse->ib );
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
            break;
        case MORSE_INNER_BLOCK_SIZE:
            if (value <= 0) {
                morse_error("MORSE_Set", "negative inner block size");
                return MORSE_ERR_ILLEGAL_VALUE;
            }
            if (value > morse->nb) {
                morse_error("MORSE_Set", "inner block larger than tile");
                return MORSE_ERR_ILLEGAL_VALUE;
            }
            /* if (morse->nb % value != 0) { */
            /*     morse_error("MORSE_Set", "inner block does not divide tile"); */
            /*     return MORSE_ERR_ILLEGAL_VALUE; */
            /* } */
            morse->ib = value;

            if ( morse->autotuning_enabled ) {
                morse->autotuning_enabled = MORSE_FALSE;
                morse_warning("MORSE_Set", "autotuning has been automatically disable\n");
            }
            break;
        case MORSE_HOUSEHOLDER_MODE:
            if (value != MORSE_FLAT_HOUSEHOLDER && value != MORSE_TREE_HOUSEHOLDER) {
                morse_error("MORSE_Set", "illegal value of MORSE_HOUSEHOLDER_MODE");
                return MORSE_ERR_ILLEGAL_VALUE;
            }
            morse->householder = value;
            break;
        case MORSE_HOUSEHOLDER_SIZE:
            if (value <= 0) {
                morse_error("MORSE_Set", "negative householder size");
                return MORSE_ERR_ILLEGAL_VALUE;
            }
            morse->rhblock = value;
            break;
        case MORSE_TRANSLATION_MODE:
            if (value != MORSE_INPLACE && value != MORSE_OUTOFPLACE) {
                morse_error("MORSE_Set", "illegal value of MORSE_TRANSLATION_MODE");
                return MORSE_ERR_ILLEGAL_VALUE;
            }
            morse->translation = value;
            break;
        default:
            morse_error("MORSE_Set", "unknown parameter");
            return MORSE_ERR_ILLEGAL_VALUE;
    }

    return MORSE_SUCCESS;
}

349
/** ***************************************************************************
350
 *
351
 * @ingroup Options
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
 *
 *  MORSE_Get - Get value of MORSE parameter.
 *
 *******************************************************************************
 *
 * @param[in] param
 *          Feature to be enabled:
 *          @arg MORSE_TILE_SIZE:        size matrix tile,
 *          @arg MORSE_INNER_BLOCK_SIZE: size of tile inner block,
 *
 * @param[out] value
 *          Value of the parameter.
 *
 *******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 ******************************************************************************/
int MORSE_Get(MORSE_enum param, int *value)
{
    MORSE_context_t *morse;

    morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Get", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    switch (param) {
        case MORSE_TILE_SIZE:
            *value = morse->nb;
            return MORSE_SUCCESS;
        case MORSE_INNER_BLOCK_SIZE:
            *value = morse->ib;
            return MORSE_SUCCESS;
        case MORSE_HOUSEHOLDER_MODE:
            *value = morse->householder;
            return MORSE_SUCCESS;
        case MORSE_HOUSEHOLDER_SIZE:
            *value = morse->rhblock;
            return MORSE_SUCCESS;
        case MORSE_TRANSLATION_MODE:
            *value = morse->translation;
            return MORSE_SUCCESS;
        default:
            morse_error("MORSE_Get", "unknown parameter");
            return MORSE_ERR_ILLEGAL_VALUE;
    }

    return MORSE_SUCCESS;
}