control.c 8.08 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-2015 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
8 9 10 11 12 13 14 15 16 17 18
 *
 **/

/**
 *
 * @file control.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
 * @author Jakub Kurzak
 * @author Mathieu Faverge
 * @author Cedric Castagnede
 * @date 2012-09-15
 *
 **/
26 27 28 29 30 31 32 33

/**
 *
 * @defgroup Control
 * @brief Group routines exposed to users to control MORSE state
 *
 */

34 35
#include <stdio.h>
#include <stdlib.h>
36 37
#include "control/auxiliary.h"
#include "control/common.h"
38 39
#include "runtime.h"

40
/** ***************************************************************************
41
 *
42
 * @ingroup Control
43 44 45
 *
 *  MORSE_Init - Initialize MORSE.
 *
46
 ******************************************************************************
47 48
 *
 * @param[in] cores
49
 *          Number of cores to use.
50
 *
51 52 53 54
 * @param[in] gpus
 *          Number of cuda devices to use.
 *
 ******************************************************************************
55 56 57 58
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
59
 *****************************************************************************/
60 61 62 63 64
int MORSE_Init(int cores, int gpus)
{
    return MORSE_InitPar(cores, gpus, -1);
}

65
/** ***************************************************************************
66
 *
67
 * @ingroup Control
68
 *
69
 *  MORSE_InitPar - Initialize MORSE.
70
 *
71
 ******************************************************************************
72
 *
73 74
 * @param[in] ncpus
 *          Number of cores to use.
75
 *
76 77
 * @param[in] ncudas
 *          Number of cuda devices to use.
78
 *
79 80 81 82
 * @param[in] nthreads_per_worker
 *          Number of threads per worker (cpu, cuda device).
 *
 ******************************************************************************
83 84 85 86
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
87 88
 *****************************************************************************/
int MORSE_InitPar(int ncpus, int ncudas, int nthreads_per_worker)
89 90 91 92 93 94 95 96 97 98
{
    MORSE_context_t *morse;

    /* Create context and insert in the context map */
    morse = morse_context_create();
    if (morse == NULL) {
        morse_fatal_error("MORSE_Init", "morse_context_create() failed");
        return MORSE_ERR_OUT_OF_RESOURCES;
    }

99
#if defined(CHAMELEON_USE_MPI)
100
#  if defined(CHAMELEON_SIMULATION)
101 102
    /* Assuming that we don't initialize MPI ourself (which SMPI doesn't support anyway) */
    morse->mpi_outer_init = 1;
103
#  else
104 105 106
    {
      int flag = 0, provided = 0;
      MPI_Initialized( &flag );
107
      morse->mpi_outer_init = flag;
108
      if ( !flag ) {
109
          MPI_Init_thread( NULL, NULL, MPI_THREAD_MULTIPLE, &provided );
110 111
      }
    }
112
#  endif
113
#endif
114
    RUNTIME_init_scheduler( morse, ncpus, ncudas, nthreads_per_worker );
115 116 117
    return MORSE_SUCCESS;
}

118
/** ***************************************************************************
119
 *
120
 * @ingroup Control
121 122 123
 *
 *  MORSE_Finalize - Finalize MORSE.
 *
124
 ******************************************************************************
125 126 127 128
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
129
 *****************************************************************************/
130 131 132 133 134 135 136 137
int MORSE_Finalize(void)
{
    MORSE_context_t *morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Finalize()", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    MORSE_TASK_dataflush_all();
138 139 140
#  if !defined(CHAMELEON_SIMULATION)
    RUNTIME_barrier(morse);
#  endif
141
    RUNTIME_finalize_scheduler( morse );
142

143
#if defined(CHAMELEON_USE_MPI)
144 145
    if (!morse->mpi_outer_init)
        MPI_Finalize();
146 147
#endif

148
    morse_context_destroy();
149 150 151
    return MORSE_SUCCESS;
}

152
/** ***************************************************************************
153
 *
154
 * @ingroup Control
155 156 157
 *
 *  MORSE_Pause - Suspend MORSE runtime to poll for new tasks.
 *
158
 ******************************************************************************
159 160 161 162
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
163
 *****************************************************************************/
164 165 166 167 168 169 170 171 172 173 174
int MORSE_Pause(void)
{
    MORSE_context_t *morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Pause()", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    RUNTIME_pause(morse);
    return MORSE_SUCCESS;
}

175
/** ***************************************************************************
176
 *
177
 * @ingroup Control
178 179 180 181
 *
 *  MORSE_Resume - Symmetrical call to MORSE_Pause,
 *  used to resume the workers polling for new tasks.
 *
182
 ******************************************************************************
183 184 185 186
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
187
 *****************************************************************************/
188 189 190 191 192 193 194 195 196 197
int MORSE_Resume(void)
{
    MORSE_context_t *morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Resume()", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    RUNTIME_resume(morse);
    return MORSE_SUCCESS;
}
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

/** ***************************************************************************
 *
 * @ingroup Control
 *
 *  MORSE_Distributed_start - Prepare the distributed processes for computation
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
int MORSE_Distributed_start(void)
{
213 214 215 216 217 218
    MORSE_context_t *morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Finalize()", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    RUNTIME_barrier (morse);
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    return MORSE_SUCCESS;
}

/** ***************************************************************************
 *
 * @ingroup Control
 *
 *  MORSE_Distributed_stop - Clean the distributed processes after computation
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
int MORSE_Distributed_stop(void)
{
236 237 238 239 240 241
    MORSE_context_t *morse = morse_context_self();
    if (morse == NULL) {
        morse_error("MORSE_Finalize()", "MORSE not initialized");
        return MORSE_ERR_NOT_INITIALIZED;
    }
    RUNTIME_barrier (morse);
242 243 244 245 246 247 248
    return MORSE_SUCCESS;
}

/** ***************************************************************************
 *
 * @ingroup Control
 *
249
 *  MORSE_Comm_size - Return the size of the distributed computation
250 251 252 253 254 255 256
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
257
int MORSE_Comm_size( int *size )
258
{
259
    RUNTIME_comm_size (size);
260 261 262 263 264 265 266
    return MORSE_SUCCESS;
}

/** ***************************************************************************
 *
 * @ingroup Control
 *
267
 *  MORSE_Comm_rank - Return the rank of the distributed computation
268 269 270 271 272 273 274
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
275
int MORSE_Comm_rank( int *rank )
276
{
277
    RUNTIME_comm_rank (rank);
278 279
    return MORSE_SUCCESS;
}
Mathieu Faverge's avatar
Mathieu Faverge committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

/** ***************************************************************************
 *
 * @ingroup Control
 *
 *  MORSE_GetThreadNbr - Return the number of CPU workers initialized by the
 *  runtime
 *
 ******************************************************************************
 *
 * @return
 *          \retval The number of CPU workers started
 *
 *****************************************************************************/
int MORSE_GetThreadNbr( )
{
    return RUNTIME_get_thread_nbr();
}