auxiliary.c 6.67 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 54 55 56
    chamctxt = chameleon_context_self();
    if (chamctxt == NULL)
        chameleon_fatal_error("chameleon_warning", "CHAMELEON not initialized");
    if (chamctxt->warnings_enabled)
57
        fprintf(stderr, "CHAMELEON WARNING: %s(): %s\n", func_name, msg_text);
58 59
}

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

79
/**
80 81 82 83 84 85 86 87 88 89 90
 *
 *  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.
 *
91
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
92
void chameleon_fatal_error(const char *func_name, const char *msg_text)
93
{
94
    fprintf(stderr, "CHAMELEON FATAL ERROR: %s(): %s\n", func_name, msg_text);
95 96 97
    exit(0);
}

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

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

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

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

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

    if (ver_micro)
158
        *ver_micro = CHAMELEON_VERSION_MICRO;
159

160
    return CHAMELEON_SUCCESS;
161
}
162

163
/**
164 165 166
 *
 * @ingroup Auxiliary
 *
167 168
 *  CHAMELEON_Element_Size - Reports the size in bytes of a CHAMELEON precision type
 *  (e.g. ChamInteger, ChamRealFloat, etc).
169 170 171 172
 *
 ******************************************************************************
 *
 * @param[in] type
173 174 175 176 177 178 179
 *          CHAMELEON element type, can be one of the following:
 *          - ChamByte
 *          - ChamInteger
 *          - ChamRealFloat
 *          - ChamRealDouble
 *          - ChamComplexFloat
 *          - ChamComplexDouble
180 181 182 183 184 185
 *
 ******************************************************************************
 *
 * @return
 *          \retval Element size in bytes
 *
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 213 214
 *
 ******************************************************************************
 *
 ******************************************************************************
 *
 * @return
 *          \retval MPI rank
 *
215
 */
216
int CHAMELEON_My_Mpi_Rank(void)
217 218
{
#if defined(CHAMELEON_USE_MPI)
Mathieu Faverge's avatar
Mathieu Faverge committed
219 220 221
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Finalize()", "CHAMELEON not initialized");
222
        return CHAMELEON_ERR_NOT_INITIALIZED;
223
    }
224
    return CHAMELEON_MPI_RANK;
225
#else
226
    return CHAMELEON_SUCCESS;
227 228
#endif
}
229

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

237 238 239 240 241 242 243 244 245 246 247 248
    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 );
        }
    }
249

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

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

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

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