auxiliary.c 6.63 KB
Newer Older
1
/**
2 3
 *
 * @file auxiliary.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
 *
10
 ***
11
 *
12
 * @brief Chameleon auxiliary routines
13
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
14
 * @version 1.0.0
15 16 17 18 19 20
 * @author Jakub Kurzak
 * @author Piotr Luszczek
 * @author Emmanuel Agullo
 * @author Cedric Castagnede
 * @date 2012-09-15
 *
21
 ***
22 23 24 25 26 27
 *
 * @defgroup Auxiliary
 * @brief Group auxiliary routines exposed to users
 *
 */

28 29
#include "control/common.h"
#include "control/auxiliary.h"
30 31 32

#include <stdio.h>
#include <stdlib.h>
33
#include <limits.h>
34

35
/**
36 37 38
 *
 *  Indicates a recoverable problem.
 *  User's erroneous action without severe consequences.
39
 *  Problems occuring while CHAMELEON is being used correctly.
40 41 42 43 44 45 46 47
 *  Context aware.
 *
 * @param[in] func_name
 *          Function location where warning occurred
 *
 * @param[in] msg_text
 *          Warning message to display.
 *
48
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
49
void chameleon_warning(const char *func_name, const char *msg_text)
50
{
Mathieu Faverge's avatar
Mathieu Faverge committed
51
    CHAM_context_t *chamctxt;
52

Mathieu Faverge's avatar
Mathieu Faverge committed
53
    chamctxt = chameleon_context_self();
54
    if (chamctxt == NULL) {
Mathieu Faverge's avatar
Mathieu Faverge committed
55
        chameleon_fatal_error("chameleon_warning", "CHAMELEON not initialized");
56 57
    }
    if (chamctxt->warnings_enabled) {
58
        fprintf(stderr, "CHAMELEON WARNING: %s(): %s\n", func_name, msg_text);
59
    }
60 61
}

62
/**
63 64 65
 *
 *  Indicates a recoverable problem.
 *  User's erroneous action with potentially severe consequences.
66
 *  Problems occuring due to incorrect use of CHAMELEON.
67 68 69 70 71 72 73 74
 *  Context aware.
 *
 * @param[in] func_name
 *          Function location where warning occurred
 *
 * @param[in] msg_text
 *          Warning message to display.
 *
75
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
76
void chameleon_error(const char *func_name, const char *msg_text)
77
{
78
    fprintf(stderr, "CHAMELEON ERROR: %s(): %s\n", func_name, msg_text);
79 80
}

81
/**
82 83 84 85 86 87 88 89 90 91 92
 *
 *  Unexpected behavior within the library.
 *  Unrecoverable user errors.
 *  Context oblivious.
 *
 * @param[in] func_name
 *          Function location where warning occurred
 *
 * @param[in] msg_text
 *          Warning message to display.
 *
93
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
94
void chameleon_fatal_error(const char *func_name, const char *msg_text)
95
{
96
    fprintf(stderr, "CHAMELEON FATAL ERROR: %s(): %s\n", func_name, msg_text);
97 98 99
    exit(0);
}

100
/**
101
 *  Returns core id
102
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
103
int chameleon_rank(CHAM_context_t *chamctxt)
104
{
Mathieu Faverge's avatar
Mathieu Faverge committed
105
    return RUNTIME_thread_rank( chamctxt );
106 107
}

108
/**
109
 *  Tune block size nb and internal block size ib
110
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
111
int chameleon_tune(cham_tasktype_t func, int M, int N, int NRHS)
112
{
Mathieu Faverge's avatar
Mathieu Faverge committed
113 114 115 116
    CHAM_context_t *chamctxt;
    chamctxt = chameleon_context_self();
    if ( chamctxt && chamctxt->autotuning_enabled == CHAMELEON_TRUE ) {
        chameleon_warning( "chameleon_tune", "Autotunning not available for now" );
117
    }
118 119 120 121
    (void)func;
    (void)M;
    (void)N;
    (void)NRHS;
122
    return CHAMELEON_SUCCESS;
123 124
}

125
/**
126 127 128
 *
 * @ingroup Auxiliary
 *
129
 *  CHAMELEON_Version - Reports CHAMELEON version number.
130
 *
131
 ******************************************************************************
132 133
 *
 * @param[out] ver_major
134
 *          CHAMELEON major version number.
135 136
 *
 * @param[out] ver_minor
137
 *          CHAMELEON minor version number.
138 139
 *
 * @param[out] ver_micro
140
 *          CHAMELEON micro version number.
141
 *
142
 ******************************************************************************
143
 *
144
 * @retval CHAMELEON_SUCCESS successful exit
145
 *
146
 */
147
int CHAMELEON_Version(int *ver_major, int *ver_minor, int *ver_micro)
148 149
{
    if (! ver_major && ! ver_minor && ! ver_micro)
150
        return  CHAMELEON_ERR_ILLEGAL_VALUE;
151 152

    if (ver_major)
153
        *ver_major = CHAMELEON_VERSION_MAJOR;
154 155

    if (ver_minor)
156
        *ver_minor = CHAMELEON_VERSION_MINOR;
157 158

    if (ver_micro)
159
        *ver_micro = CHAMELEON_VERSION_MICRO;
160

161
    return CHAMELEON_SUCCESS;
162
}
163

164
/**
165 166 167
 *
 * @ingroup Auxiliary
 *
168 169
 *  CHAMELEON_Element_Size - Reports the size in bytes of a CHAMELEON precision type
 *  (e.g. ChamInteger, ChamRealFloat, etc).
170 171 172 173
 *
 ******************************************************************************
 *
 * @param[in] type
174 175 176 177 178 179 180
 *          CHAMELEON element type, can be one of the following:
 *          - ChamByte
 *          - ChamInteger
 *          - ChamRealFloat
 *          - ChamRealDouble
 *          - ChamComplexFloat
 *          - ChamComplexDouble
181 182 183
 *
 ******************************************************************************
 *
184
 * @retval Element size in bytes
185
 *
186
 */
187
int CHAMELEON_Element_Size(int type)
188 189
{
    switch(type) {
190 191 192 193 194 195
        case ChamByte:          return          1;
        case ChamInteger:       return   sizeof(int);
        case ChamRealFloat:     return   sizeof(float);
        case ChamRealDouble:    return   sizeof(double);
        case ChamComplexFloat:  return 2*sizeof(float);
        case ChamComplexDouble: return 2*sizeof(double);
Mathieu Faverge's avatar
Mathieu Faverge committed
196
        default: chameleon_fatal_error("CHAMELEON_Element_Size", "undefined type");
197
                 return CHAMELEON_ERR_ILLEGAL_VALUE;
198 199 200 201

    }
}

202
/**
203 204 205
 *
 * @ingroup Auxiliary
 *
206
 *  CHAMELEON_My_Mpi_Rank - Return the MPI rank of the calling process.
207 208 209 210 211
 *
 ******************************************************************************
 *
 ******************************************************************************
 *
212
 * @retval MPI rank
213
 *
214
 */
215
int CHAMELEON_My_Mpi_Rank(void)
216 217
{
#if defined(CHAMELEON_USE_MPI)
Mathieu Faverge's avatar
Mathieu Faverge committed
218 219 220
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Finalize()", "CHAMELEON not initialized");
221
        return CHAMELEON_ERR_NOT_INITIALIZED;
222
    }
223
    return CHAMELEON_MPI_RANK;
224
#else
225
    return CHAMELEON_SUCCESS;
226 227
#endif
}
228

229
/**
230
 *  Display a progress percentage in stderr
231
 */
232
void update_progress(int currentValue, int maximumValue) {
233 234
    div_t res ;
    static int progress = -1; /* varie de 0 a 100 au cours du calcul concerne */
235

236 237 238 239 240 241 242 243 244 245 246 247
    if (maximumValue == 0) {
        res.quot = 100;
    }
    else {
        if (currentValue < (INT_MAX / 100) ) {
            res = div(currentValue*100, maximumValue);
        }
        else {
            /* Calcule le quotient de la division */
            res.quot = (int)( (long long)( currentValue * 100 ) / maximumValue );
        }
    }
248

249 250 251 252 253
    // Print the percentage
    if (res.quot > progress) {
        fprintf(stderr, "%3d%%\b\b\b\b", res.quot);
    }
    progress = res.quot;
254

255 256 257
    if (currentValue >= maximumValue) {
        progress = -1;
    }
258 259 260 261
}

// A function to display the progress indicator.
// By default it is update_progress()
262
// The user can change it with CHAMELEON_Set_update_progress_callback()
263 264
void (*update_progress_callback)(int, int) = update_progress;

265
int CHAMELEON_Set_update_progress_callback(void (*p)(int, int)) {
Mathieu Faverge's avatar
Mathieu Faverge committed
266
  update_progress_callback = p;
267
  return CHAMELEON_SUCCESS;
268 269
}