control.c 7.71 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-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
#if defined(CHAMELEON_USE_MAGMA)
115 116
    magma_init();
#endif
117
    RUNTIME_init_scheduler( morse, ncpus, ncudas, nthreads_per_worker );
118 119 120
    return MORSE_SUCCESS;
}

121
/** ***************************************************************************
122
 *
123
 * @ingroup Control
124 125 126
 *
 *  MORSE_Finalize - Finalize MORSE.
 *
127
 ******************************************************************************
128 129 130 131
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
132
 *****************************************************************************/
133 134 135 136 137 138 139 140 141
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();
    RUNTIME_finalize_scheduler( morse );
142
#if defined(CHAMELEON_USE_MAGMA)
143 144 145
    magma_finalize();
#endif
    morse_context_destroy();
146
#  if !defined(CHAMELEON_SIMULATION)
147
    RUNTIME_barrier(morse);
148
#  endif
149
#if defined(CHAMELEON_USE_MPI)
150 151
    if (!morse->mpi_outer_init)
        MPI_Finalize();
152 153 154 155 156
#endif

    return MORSE_SUCCESS;
}

157
/** ***************************************************************************
158
 *
159
 * @ingroup Control
160 161 162
 *
 *  MORSE_Pause - Suspend MORSE runtime to poll for new tasks.
 *
163
 ******************************************************************************
164 165 166 167
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
168
 *****************************************************************************/
169 170 171 172 173 174 175 176 177 178 179
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;
}

180
/** ***************************************************************************
181
 *
182
 * @ingroup Control
183 184 185 186
 *
 *  MORSE_Resume - Symmetrical call to MORSE_Pause,
 *  used to resume the workers polling for new tasks.
 *
187
 ******************************************************************************
188 189 190 191
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
192
 *****************************************************************************/
193 194 195 196 197 198 199 200 201 202
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;
}
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

/** ***************************************************************************
 *
 * @ingroup Control
 *
 *  MORSE_Distributed_start - Prepare the distributed processes for computation
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
int MORSE_Distributed_start(void)
{
218 219 220 221 222 223
    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);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    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)
{
241 242 243 244 245 246
    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);
247 248 249 250 251 252 253
    return MORSE_SUCCESS;
}

/** ***************************************************************************
 *
 * @ingroup Control
 *
254
 *  MORSE_Comm_size - Return the size of the distributed computation
255 256 257 258 259 260 261
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
262
int MORSE_Comm_size( int *size )
263
{
264
    RUNTIME_comm_size (size);
265 266 267 268 269 270 271
    return MORSE_SUCCESS;
}

/** ***************************************************************************
 *
 * @ingroup Control
 *
272
 *  MORSE_Comm_rank - Return the rank of the distributed computation
273 274 275 276 277 278 279
 *
 ******************************************************************************
 *
 * @return
 *          \retval MORSE_SUCCESS successful exit
 *
 *****************************************************************************/
280
int MORSE_Comm_rank( int *rank )
281
{
282
    RUNTIME_comm_rank (rank);
283 284
    return MORSE_SUCCESS;
}