context.c 12.3 KB
Newer Older
1 2
/**
 *
3 4
 * @copyright (c) 2009-2014 The University of Tennessee and The University
 *                          of Tennessee Research Foundation.
5 6
 *                          All rights reserved.
 * @copyright (c) 2012-2014 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 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
#include "runtime.h"

/*******************************************************************************
 *  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 81 82 83 84 85
    morse->errors_enabled       = MORSE_FALSE;
    morse->warnings_enabled     = MORSE_FALSE;
    morse->autotuning_enabled   = MORSE_TRUE;
    morse->parallel_enabled     = MORSE_FALSE;
    morse->profiling_enabled    = MORSE_FALSE;
    morse->progress_enabled     = MORSE_FALSE;
86
    morse->gemm3m_enabled       = MORSE_FALSE;
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 115 116

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

118 119 120
    return MORSE_SUCCESS;
}

121
/** ***************************************************************************
122
 *
123
 * @ingroup Options
124 125 126 127 128
 *
 *  MORSE_Enable - Enable MORSE feature.
 *
 *******************************************************************************
 *
129
 * @param[in] option
130 131 132 133 134
 *          Feature to be enabled:
 *          @arg MORSE_WARNINGS   printing of warning messages,
 *          @arg MORSE_ERRORS     printing of error 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 165 166 167
 *
 *******************************************************************************
 *
 * @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_ERRORS:
            morse->errors_enabled = MORSE_TRUE;
            break;
        case MORSE_AUTOTUNING:
            morse->autotuning_enabled = MORSE_TRUE;
            break;
        case MORSE_PROFILING_MODE:
            morse->profiling_enabled = MORSE_TRUE;
            break;
168 169 170
        case MORSE_PROGRESS:
            morse->progress_enabled = MORSE_TRUE;
            break;
171 172 173 174 175 176 177
        case MORSE_GEMM3M:
#ifdef CBLAS_HAS_ZGEMM3M
            morse->gemm3m_enabled = MORSE_TRUE;
#else
            morse_error("MORSE_Enable", "cannot enable GEMM3M (not available in cblas)");
#endif
            break;
178 179 180 181 182 183 184 185 186
        /* 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;
    }
187

188 189 190 191 192 193
    /* Enable at the lower level if required */
    RUNTIME_enable( option );

    return MORSE_SUCCESS;
}

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

254 255 256 257 258 259
    /* Disable at the lower level if required */
    RUNTIME_disable( option );

    return MORSE_SUCCESS;
}

260
/** ***************************************************************************
261
 *
262
 * @ingroup Options
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 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 346 347 348 349 350 351 352 353
 *
 *  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 */
            morse->ib = min( morse->nb, morse->ib );
            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;
}

354
/** ***************************************************************************
355
 *
356
 * @ingroup Options
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 403 404 405 406 407
 *
 *  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;
}