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

27 28
#include <stdio.h>
#include <stdlib.h>
29 30
#include "control/auxiliary.h"
#include "control/common.h"
Mathieu Faverge's avatar
Mathieu Faverge committed
31
#include "chameleon/runtime.h"
32

33
/**
34
 *
35
 * @ingroup Control
36
 *
Philippe Virouleau's avatar
Philippe Virouleau committed
37
 * @brief Initialize CHAMELEON.
38
 *
39
 ******************************************************************************
40 41
 *
 * @param[in] cores
42
 *          Number of cores to use.
43
 *
44 45 46 47
 * @param[in] gpus
 *          Number of cuda devices to use.
 *
 ******************************************************************************
48
 *
Philippe Virouleau's avatar
Philippe Virouleau committed
49
 * @retval CHAMELEON_SUCCESS successful exit
50
 *
51
 */
Philippe Virouleau's avatar
Philippe Virouleau committed
52
int __chameleon_init(int cores, int gpus)
53
{
Philippe Virouleau's avatar
Philippe Virouleau committed
54
   return __chameleon_initpar(cores, gpus, -1);
55 56
}

57
/**
58
 *
59
 * @ingroup Control
60
 *
Philippe Virouleau's avatar
Philippe Virouleau committed
61
 * @brief Initialize CHAMELEON.
62
 *
63
 ******************************************************************************
64
 *
65 66
 * @param[in] ncpus
 *          Number of cores to use.
67
 *
68 69
 * @param[in] ncudas
 *          Number of cuda devices to use.
70
 *
71 72 73 74
 * @param[in] nthreads_per_worker
 *          Number of threads per worker (cpu, cuda device).
 *
 ******************************************************************************
75
 *
Philippe Virouleau's avatar
Philippe Virouleau committed
76
 * @retval CHAMELEON_SUCCESS successful exit
77
 *
78
 */
Philippe Virouleau's avatar
Philippe Virouleau committed
79
int __chameleon_initpar(int ncpus, int ncudas, int nthreads_per_worker)
80
{
Mathieu Faverge's avatar
Mathieu Faverge committed
81
    CHAM_context_t *chamctxt;
82 83

    /* Create context and insert in the context map */
Mathieu Faverge's avatar
Mathieu Faverge committed
84 85 86
    chamctxt = chameleon_context_create();
    if (chamctxt == NULL) {
        chameleon_fatal_error("CHAMELEON_Init", "chameleon_context_create() failed");
Mathieu Faverge's avatar
Mathieu Faverge committed
87
        return CHAMELEON_ERR_OUT_OF_RESOURCES;
88 89
    }

90
#if defined(CHAMELEON_USE_MPI)
91
#  if defined(CHAMELEON_SIMULATION)
92
    /* Assuming that we don't initialize MPI ourself (which SMPI doesn't support anyway) */
Mathieu Faverge's avatar
Mathieu Faverge committed
93
    chamctxt->mpi_outer_init = 1;
94
#  else
95 96 97
    {
      int flag = 0, provided = 0;
      MPI_Initialized( &flag );
Mathieu Faverge's avatar
Mathieu Faverge committed
98
      chamctxt->mpi_outer_init = flag;
99
      if ( !flag ) {
100
          MPI_Init_thread( NULL, NULL, MPI_THREAD_MULTIPLE, &provided );
101 102
      }
    }
103
#  endif
104
#endif
Mathieu Faverge's avatar
Mathieu Faverge committed
105

Mathieu Faverge's avatar
Mathieu Faverge committed
106
    RUNTIME_init( chamctxt, ncpus, ncudas, nthreads_per_worker );
107

Mathieu Faverge's avatar
Mathieu Faverge committed
108
#if defined(CHAMELEON_USE_MPI)
Mathieu Faverge's avatar
Mathieu Faverge committed
109 110
    chamctxt->my_mpi_rank   = RUNTIME_comm_rank( chamctxt );
    chamctxt->mpi_comm_size = RUNTIME_comm_size( chamctxt );
Mathieu Faverge's avatar
Mathieu Faverge committed
111
#endif
112

Mathieu Faverge's avatar
Mathieu Faverge committed
113
    return CHAMELEON_SUCCESS;
114 115
}

116
/**
117
 *
118
 * @ingroup Control
119
 *
Philippe Virouleau's avatar
Philippe Virouleau committed
120
 * @brief Finalize CHAMELEON.
121
 *
122
 ******************************************************************************
123
 *
Philippe Virouleau's avatar
Philippe Virouleau committed
124
 * @retval CHAMELEON_SUCCESS successful exit
125
 *
126
 */
Philippe Virouleau's avatar
Philippe Virouleau committed
127
int __chameleon_finalize(void)
128
{
Mathieu Faverge's avatar
Mathieu Faverge committed
129 130 131
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Finalize()", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
132
        return CHAMELEON_ERR_NOT_INITIALIZED;
133
    }
134
    RUNTIME_flush();
135
#  if !defined(CHAMELEON_SIMULATION)
Mathieu Faverge's avatar
Mathieu Faverge committed
136
    RUNTIME_barrier(chamctxt);
137
#  endif
Mathieu Faverge's avatar
Mathieu Faverge committed
138
    RUNTIME_finalize( chamctxt );
139

140
#if defined(CHAMELEON_USE_MPI)
Mathieu Faverge's avatar
Mathieu Faverge committed
141
    if (!chamctxt->mpi_outer_init)
142
        MPI_Finalize();
143 144
#endif

Mathieu Faverge's avatar
Mathieu Faverge committed
145
    chameleon_context_destroy();
Mathieu Faverge's avatar
Mathieu Faverge committed
146
    return CHAMELEON_SUCCESS;
147 148
}

149
/**
150
 *
151
 * @ingroup Control
152
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
153
 *  CHAMELEON_Pause - Suspend CHAMELEON runtime to poll for new tasks.
154
 *
155
 ******************************************************************************
156 157
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
158
 *          \retval CHAMELEON_SUCCESS successful exit
159
 *
160
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
161
int CHAMELEON_Pause(void)
162
{
Mathieu Faverge's avatar
Mathieu Faverge committed
163 164 165
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Pause()", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
166
        return CHAMELEON_ERR_NOT_INITIALIZED;
167
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
168
    RUNTIME_pause(chamctxt);
Mathieu Faverge's avatar
Mathieu Faverge committed
169
    return CHAMELEON_SUCCESS;
170 171
}

172
/**
173
 *
174
 * @ingroup Control
175
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
176
 *  CHAMELEON_Resume - Symmetrical call to CHAMELEON_Pause,
177 178
 *  used to resume the workers polling for new tasks.
 *
179
 ******************************************************************************
180 181
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
182
 *          \retval CHAMELEON_SUCCESS successful exit
183
 *
184
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
185
int CHAMELEON_Resume(void)
186
{
Mathieu Faverge's avatar
Mathieu Faverge committed
187 188 189
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Resume()", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
190
        return CHAMELEON_ERR_NOT_INITIALIZED;
191
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
192
    RUNTIME_resume(chamctxt);
Mathieu Faverge's avatar
Mathieu Faverge committed
193
    return CHAMELEON_SUCCESS;
194
}
195

196
/**
197 198 199
 *
 * @ingroup Control
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
200
 *  CHAMELEON_Distributed_start - Prepare the distributed processes for computation
201 202 203 204
 *
 ******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
205
 *          \retval CHAMELEON_SUCCESS successful exit
206
 *
207
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
208
int CHAMELEON_Distributed_start(void)
209
{
Mathieu Faverge's avatar
Mathieu Faverge committed
210 211 212
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Finalize()", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
213
        return CHAMELEON_ERR_NOT_INITIALIZED;
214
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
215
    RUNTIME_barrier (chamctxt);
Mathieu Faverge's avatar
Mathieu Faverge committed
216
    return CHAMELEON_SUCCESS;
217 218
}

219
/**
220 221 222
 *
 * @ingroup Control
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
223
 *  CHAMELEON_Distributed_stop - Clean the distributed processes after computation
224 225 226 227
 *
 ******************************************************************************
 *
 * @return
Mathieu Faverge's avatar
Mathieu Faverge committed
228
 *          \retval CHAMELEON_SUCCESS successful exit
229
 *
230
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
231
int CHAMELEON_Distributed_stop(void)
232
{
Mathieu Faverge's avatar
Mathieu Faverge committed
233 234 235
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Finalize()", "CHAMELEON not initialized");
Mathieu Faverge's avatar
Mathieu Faverge committed
236
        return CHAMELEON_ERR_NOT_INITIALIZED;
237
    }
Mathieu Faverge's avatar
Mathieu Faverge committed
238
    RUNTIME_barrier (chamctxt);
Mathieu Faverge's avatar
Mathieu Faverge committed
239
    return CHAMELEON_SUCCESS;
240 241
}

242
/**
243 244 245
 *
 * @ingroup Control
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
246
 *  CHAMELEON_Comm_size - Return the size of the distributed computation
247 248 249
 *
 ******************************************************************************
 *
250 251
 * @retval The size of the distributed computation
 * @retval -1 if context not initialized
252
 *
253
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
254
int CHAMELEON_Comm_size()
255
{
Mathieu Faverge's avatar
Mathieu Faverge committed
256 257 258
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Comm_size()", "CHAMELEON not initialized");
259 260 261
        return -1;
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
262
    return RUNTIME_comm_size( chamctxt );
263 264
}

265
/**
266 267 268
 *
 * @ingroup Control
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
269
 *  CHAMELEON_Comm_rank - Return the rank of the distributed computation
270 271 272
 *
 ******************************************************************************
 *
273 274
 * @retval The rank of the distributed computation
 * @retval -1 if context not initialized
275
 *
276
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
277
int CHAMELEON_Comm_rank()
278
{
Mathieu Faverge's avatar
Mathieu Faverge committed
279 280 281
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_Comm_rank()", "CHAMELEON not initialized");
282 283 284
        return -1;
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
285
    return RUNTIME_comm_rank( chamctxt );
286
}
Mathieu Faverge's avatar
Mathieu Faverge committed
287

288
/**
Mathieu Faverge's avatar
Mathieu Faverge committed
289 290 291
 *
 * @ingroup Control
 *
Mathieu Faverge's avatar
Mathieu Faverge committed
292
 *  CHAMELEON_GetThreadNbr - Return the number of CPU workers initialized by the
Mathieu Faverge's avatar
Mathieu Faverge committed
293 294 295 296 297 298 299
 *  runtime
 *
 ******************************************************************************
 *
 * @return
 *          \retval The number of CPU workers started
 *
300
 */
Mathieu Faverge's avatar
Mathieu Faverge committed
301
int CHAMELEON_GetThreadNbr( )
Mathieu Faverge's avatar
Mathieu Faverge committed
302
{
Mathieu Faverge's avatar
Mathieu Faverge committed
303 304 305
    CHAM_context_t *chamctxt = chameleon_context_self();
    if (chamctxt == NULL) {
        chameleon_error("CHAMELEON_GetThreadNbr()", "CHAMELEON not initialized");
306 307 308
        return -1;
    }

Mathieu Faverge's avatar
Mathieu Faverge committed
309
    return RUNTIME_thread_size( chamctxt );
Mathieu Faverge's avatar
Mathieu Faverge committed
310
}