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

/**
 *
 * @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
 *
19
 * @version 0.9.0
20 21 22 23 24 25 26
 * @author Jakub Kurzak
 * @author Mathieu Faverge
 * @author Cedric Castagnede
 * @date 2012-09-15
 *
 **/

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

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

41 42 43
#include "control/common.h"
#include "control/auxiliary.h"
#include "control/context.h"
44
#include "chameleon/morse_runtime.h"
45 46 47 48 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

/*******************************************************************************
 *  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;

80
    morse->warnings_enabled     = MORSE_TRUE;
81 82 83 84
    morse->autotuning_enabled   = MORSE_TRUE;
    morse->parallel_enabled     = MORSE_FALSE;
    morse->profiling_enabled    = MORSE_FALSE;
    morse->progress_enabled     = MORSE_FALSE;
85 86 87 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

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

116 117 118
    return MORSE_SUCCESS;
}

119
/** ***************************************************************************
120
 *
121
 * @ingroup Options
122 123 124 125 126
 *
 *  MORSE_Enable - Enable MORSE feature.
 *
 *******************************************************************************
 *
127
 * @param[in] option
128 129 130 131
 *          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
132
 *          @arg MORSE_PROGRESS  activate progress indicator
133
 *          @arg MORSE_GEMM3M  Use z/cgemm3m for complexe matrix-matrix products
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
 *
 *******************************************************************************
 *
 * @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;
162 163 164
        case MORSE_PROGRESS:
            morse->progress_enabled = MORSE_TRUE;
            break;
165 166
        case MORSE_GEMM3M:
#ifdef CBLAS_HAS_ZGEMM3M
167
            set_coreblas_gemm3m_enabled(1);
168 169 170 171
#else
            morse_error("MORSE_Enable", "cannot enable GEMM3M (not available in cblas)");
#endif
            break;
172 173 174 175 176 177 178 179 180
        /* 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;
    }
181

182 183 184 185 186 187
    /* Enable at the lower level if required */
    RUNTIME_enable( option );

    return MORSE_SUCCESS;
}

188
/** ***************************************************************************
189
 *
190
 * @ingroup Options
191 192 193 194 195
 *
 *  MORSE_Disable - Disable MORSE feature.
 *
 *******************************************************************************
 *
196
 * @param[in] option
197 198 199
 *          Feature to be disabled:
 *          @arg MORSE_WARNINGS   printing of warning messages,
 *          @arg MORSE_AUTOTUNING autotuning for tile size and inner block size.
200 201
 *          @arg MORSE_PROFILING_MODE  deactivate profiling of kernels
 *          @arg MORSE_PROGRESS  deactivate progress indicator
202
 *          @arg MORSE_GEMM3M  Use z/cgemm3m for complexe matrix-matrix products
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
 *
 *******************************************************************************
 *
 * @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;
230 231 232
        case MORSE_PROGRESS:
            morse->progress_enabled = MORSE_FALSE;
            break;
233
        case MORSE_GEMM3M:
234
#if !defined(CHAMELEON_SIMULATION)
235
            set_coreblas_gemm3m_enabled(0);
236
#endif
237
            break;
238 239 240 241 242 243 244
        case MORSE_PARALLEL_MODE:
            morse->parallel_enabled = MORSE_FALSE;
            break;
        default:
            morse_error("MORSE_Disable", "illegal parameter value");
            return MORSE_ERR_ILLEGAL_VALUE;
    }
245

246 247 248 249 250 251
    /* Disable at the lower level if required */
    RUNTIME_disable( option );

    return MORSE_SUCCESS;
}

252
/** ***************************************************************************
253
 *
254
 * @ingroup Options
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 291 292 293 294
 *
 *  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
295
            morse->ib = chameleon_min( morse->nb, morse->ib );
296 297 298 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
            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;
}

346
/** ***************************************************************************
347
 *
348
 * @ingroup Options
349 350 351 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
 *
 *  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;
}