runtime_control.c 5.61 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
 * @file runtime_control.c
14 15 16 17 18
 *
 *  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 26 27
 * @author Mathieu Faverge
 * @author Cedric Augonnet
 * @author Cedric Castagnede
 * @date 2010-11-15
 *
 **/
#include <stdio.h>
#include <stdlib.h>
28
#include "runtime/starpu/include/morse_starpu.h"
29 30 31 32 33 34 35 36 37 38 39 40 41

/*******************************************************************************
 * Thread rank.
 **/
int RUNTIME_rank(MORSE_context_t *morse)
{
    (void)morse;
    return starpu_worker_get_id();
}

/*******************************************************************************
 *
 **/
42
int RUNTIME_init_scheduler( MORSE_context_t *morse, int ncpus, int ncudas, int nthreads_per_worker)
43 44 45 46
{
    starpu_conf_t *conf = (starpu_conf_t*)(morse->schedopt);
    int hres = -1;

47 48 49 50 51
    /* StarPU was already initialized by an external library */
    if (conf == NULL) {
        return 0;
    }

52
    conf->ncpus = ncpus;
53
    conf->ncuda = ncudas;
54 55 56 57 58
    conf->nopencl = 0;

    /* By default, enable calibration */
    if (!getenv("STARPU_CALIBRATE"))
        conf->calibrate = 1;
59

Mathieu Faverge's avatar
Mathieu Faverge committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73
    /* By default, use the dmdas strategy */
    if (!getenv("STARPU_SCHED")) {
        if (conf->ncuda > 0) {
            conf->sched_policy_name = "dmdas";
        }
        else {
            /**
             * Set scheduling to "ws"/"lws" if no cuda devices used because it
             * behaves better on homogneneous architectures. If the user wants
             * to use another scheduling strategy, he can set STARPU_SCHED
             * env. var. to whatever he wants
             */
#if (STARPU_MAJOR_VERSION > 1) || ((STARPU_MAJOR_VERSION == 1) && (STARPU_MINOR_VERSION >= 2))
            conf->sched_policy_name = "lws";
74
#else
Mathieu Faverge's avatar
Mathieu Faverge committed
75
            conf->sched_policy_name = "ws";
76
#endif
Mathieu Faverge's avatar
Mathieu Faverge committed
77 78
        }
    }
79

80
    if ((ncpus == -1)||(nthreads_per_worker == -1))
81 82 83 84 85 86 87 88 89 90
    {
        morse->parallel_enabled = MORSE_FALSE;

        hres = starpu_init( conf );
    }
    else {
        int worker;

        morse->parallel_enabled = MORSE_TRUE;

91
        for (worker = 0; worker < ncpus; worker++)
92 93
            conf->workers_bindid[worker] = (worker+1)*nthreads_per_worker - 1;

94
        for (worker = 0; worker < ncpus; worker++)
95 96 97 98 99 100
            conf->workers_bindid[worker + ncudas] = worker*nthreads_per_worker;

        conf->use_explicit_workers_bindid = 1;

        hres = starpu_init( conf );

101
        morse->nworkers = ncpus;
102 103 104
        morse->nthreads_per_worker = nthreads_per_worker;
    }

105
#if defined(CHAMELEON_USE_MPI)
106 107 108 109
    {
        int flag = 0;
        MPI_Initialized( &flag );
        starpu_mpi_init(NULL, NULL, !flag);
110 111
        starpu_mpi_comm_rank(MPI_COMM_WORLD, &(morse->my_mpi_rank));
        starpu_mpi_comm_size(MPI_COMM_WORLD, &(morse->mpi_comm_size));
112 113 114 115 116 117 118
    }
#endif

#if defined(HAVE_STARPU_FXT_PROFILING)
    starpu_fxt_stop_profiling();
#endif

119
#if defined(CHAMELEON_USE_CUDA)
120 121 122 123 124 125 126 127 128 129 130 131
    starpu_cublas_init();
#endif

    return hres;
}

/*******************************************************************************
 *
 */
void RUNTIME_finalize_scheduler( MORSE_context_t *morse )
{
    (void)morse;
132 133 134

    /* StarPU was already initialized by an external library */
    if (morse->schedopt == NULL) {
135
        return;
136 137
    }

138
#if defined(CHAMELEON_USE_MPI)
139 140
    starpu_mpi_shutdown();
#endif
141
#if defined(CHAMELEON_USE_CUDA)
142 143 144 145 146 147 148 149 150 151 152 153 154 155
    starpu_cublas_shutdown();
#endif

    starpu_shutdown();
    return;
}

/*******************************************************************************
 *  Busy-waiting barrier
 **/
void RUNTIME_barrier( MORSE_context_t *morse )
{
    (void)morse;
    starpu_task_wait_for_all();
156
#if defined(CHAMELEON_USE_MPI)
157 158 159
    starpu_mpi_barrier(MPI_COMM_WORLD);
#endif
}
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

/*******************************************************************************
 *  To suspend the processing of new tasks by workers
 **/
void RUNTIME_pause( MORSE_context_t *morse )
{
    (void)morse;
    starpu_pause();
    return;
}

/*******************************************************************************
 *  This is the symmetrical call to RUNTIME_pause,
 *  used to resume the workers polling for new tasks.
 **/
void RUNTIME_resume( MORSE_context_t *morse )
{
    (void)morse;
    starpu_resume();
    return;
}
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

/*******************************************************************************
 *  This returns the rank of this process
 **/
void RUNTIME_distributed_rank( int *rank )
{
#if defined(CHAMELEON_USE_MPI)
#  if defined(HAVE_STARPU_MPI_RANK)
    starpu_mpi_comm_rank(MPI_COMM_WORLD, rank);
#  else
    MPI_Comm_rank(MPI_COMM_WORLD, rank);
#  endif
#else
    *rank = 0;
#endif
    return;
}

/*******************************************************************************
 *  This returns the size of the distributed computation
 **/
void RUNTIME_distributed_size( int *size )
{
#if defined(CHAMELEON_USE_MPI)
#  if defined(HAVE_STARPU_MPI_RANK)
    starpu_mpi_comm_size(MPI_COMM_WORLD, size);
#  else
    MPI_Comm_size(MPI_COMM_WORLD, size);
#  endif
#else
    *size = 1;
#endif
    return;
}

/*******************************************************************************
 *  Barrier between processes of the distributed computation
 **/
void RUNTIME_distributed_barrier( void )
{
#if defined(CHAMELEON_USE_MPI)
    starpu_mpi_barrier(MPI_COMM_WORLD);
#endif
    return;
}