Source

mpi3-fortran / ompi / mca / coll / ml / coll_ml_component.c

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * Copyright (c) 2009-2012 Oak Ridge National Laboratory.  All rights reserved.
 * Copyright (c) 2009-2012 Mellanox Technologies.  All rights reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */
/**
 * @file
 *
 * Most of the description of the data layout is in the
 * coll_sm_module.c file.
 */

#include "ompi_config.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "ompi/constants.h"
#include "ompi/mca/coll/base/base.h"
#include "ompi/mca/mpool/base/base.h"
#include "ompi/mca/mpool/mpool.h"
#include "ompi/mca/bcol/base/base.h"
#include "ompi/mca/sbgp/base/base.h"

#include "orte/mca/rml/rml.h"
#include "orte/util/proc_info.h"
#include "orte/util/name_fns.h"
#include "orte/mca/errmgr/errmgr.h"

#include "coll_ml.h"
#include "coll_ml_inlines.h"

#include "ompi/mca/common/netpatterns/common_netpatterns.h"
#include "coll_ml_mca.h"
#include "coll_ml_custom_utils.h"


/*
 * Public string showing the coll ompi_ml V2 component version number
 */
const char *mca_coll_ml_component_version_string =
"Open MPI ml-V2 collective MCA component version " OMPI_VERSION;

/*
 * Local functions
 */

static int ml_open(void);
static int ml_close(void);
static int coll_ml_progress(void);

/*
 * Instantiate the public struct with all of our public information
 * and pointers to our public functions in it
 */

mca_coll_ml_component_t mca_coll_ml_component = {

    /* First, fill in the super */

    {
        /* First, the mca_component_t struct containing meta
           information about the component itself */

        {
            MCA_COLL_BASE_VERSION_2_0_0,

            /* Component name and version */

            "ml",
            OMPI_MAJOR_VERSION,
            OMPI_MINOR_VERSION,
            OMPI_RELEASE_VERSION,

            /* Component open and close functions */

            ml_open,
            ml_close,
        },
        {
            /* The component is not checkpoint ready */
            MCA_BASE_METADATA_PARAM_NONE
        },

        /* Initialization / querying functions */
        mca_coll_ml_init_query,
        mca_coll_ml_comm_query,
    },

    /* ml-component specifc information */

    /* (default) priority */
    0,
    /* Number of levels */
    0,
    /* subgrouping components to use */
    NULL,
    /* basic collectives components to use */
    NULL,
    /* verbose */
    0,
    /* max of communicators */
    0,
    /* min size of comm */
    0,
    /* base_sequence_number */
    0,
};

void mca_coll_ml_abort_ml(char *message)
{
    ML_ERROR(("ML Collective FATAL ERROR: %s", message));
    /* shutdown the MPI */
    ompi_mpi_abort(&ompi_mpi_comm_world.comm, MPI_ERR_INTERN, true);
}
/*
 * progress function
 */

#define INDEX(task) ((task)->my_index_in_coll_schedule)
#define ACTIVE_L    (&mca_coll_ml_component.active_tasks)
#define PENDING_L   (&mca_coll_ml_component.pending_tasks)
#define SEQ_L       (&mca_coll_ml_component.sequential_collectives)

static int coll_ml_progress()
{

    int rc = OMPI_SUCCESS;
    int fn_idx;

    mca_coll_ml_task_status_t *task_status, *task_status_tmp;
    mca_coll_ml_collective_operation_progress_t *seq_coll_op;
    mca_coll_ml_collective_operation_progress_t *seq_coll_op_tmp;

    mca_bcol_base_module_collective_fn_primitives_t progress_fn,
                                                    coll_fn;
    mca_coll_ml_utility_data_t *const_args;
    mca_coll_ml_component_t *cm = &mca_coll_ml_component;

    /* Pasha: Not sure that is it correct way to resolve the problem.
       Iprobe call for progress engine. The progress engine calls for our
       progress and as result the first element on the list is progressed again
       and so we call for Iprobe again.... as result we get HUGE stack.

       One way to prevent it - remove the item from the list, and once you finish
       to process it - put it back.

       Other way - put flag on component, if the progress is running - exit immediate.
     */
    if (cm->progress_is_busy) {
        /* We are already working...*/
        return OMPI_SUCCESS;
    } else {
        cm->progress_is_busy = true;
    }

    /* progress sequential collective operations */
    /* RLG - need to do better here for parallel progress */
    OPAL_THREAD_LOCK(&(cm->sequential_collectives_mutex));
    for (seq_coll_op  = (mca_coll_ml_collective_operation_progress_t *)opal_list_get_first(SEQ_L);
         seq_coll_op != (mca_coll_ml_collective_operation_progress_t *)opal_list_get_end(SEQ_L);
         seq_coll_op  = (mca_coll_ml_collective_operation_progress_t *)opal_list_get_next((opal_list_item_t *)seq_coll_op)){

        fn_idx      = seq_coll_op->sequential_routine.current_active_bcol_fn;
        /* initialize the task */

        if(SEQ_TASK_IN_PROG == seq_coll_op->sequential_routine.current_bcol_status){
            progress_fn = seq_coll_op->coll_schedule->
                component_functions[fn_idx].bcol_function->progress_fn;
        }else{
            /* PPP Pasha - apparently task setup should be called only here. see linr 190 */
            progress_fn = seq_coll_op->coll_schedule->
                            component_functions[fn_idx].bcol_function->coll_fn;
            seq_coll_op->sequential_routine.current_bcol_status =
                SEQ_TASK_IN_PROG;
        }

        const_args  = &seq_coll_op->coll_schedule->component_functions[fn_idx].constant_group_data;
        /* RLG - note need to move to useing coll_ml_utility_data_t as
         * collective argument, rather than  coll_ml_function_t
         */
        rc = progress_fn(&(seq_coll_op->variable_fn_params), (coll_ml_function_t *)const_args);
        if (BCOL_FN_COMPLETE == rc) {
            /* done with this routine */
            seq_coll_op->sequential_routine.current_active_bcol_fn++;
            /* this is totally hardwired for bcast, need a general call-back */
            /*seq_coll_op->variable_fn_params.root_flag = true;*/
            fn_idx      = seq_coll_op->sequential_routine.current_active_bcol_fn;
            if (seq_coll_op->sequential_routine.current_active_bcol_fn  ==
                    seq_coll_op->coll_schedule->n_fns) {
                /* done with this collective - recycle descriptor */

                /* remove from the progress list */
                seq_coll_op_tmp = (mca_coll_ml_collective_operation_progress_t *)
                    opal_list_remove_item(SEQ_L, (opal_list_item_t *)seq_coll_op);

                /* handle fragment completion */
                rc = coll_ml_fragment_completion_processing(seq_coll_op);

                if (OMPI_SUCCESS != rc) {
                    mca_coll_ml_abort_ml("Failed to run coll_ml_fragment_completion_processing");
                }
                /* make sure that for will pick up right one */
                seq_coll_op = seq_coll_op_tmp;
            }else {
                /* task setup */
                /* Pasha - Another call for task setup ? Why ?*/
                rc = seq_coll_op->sequential_routine.seq_task_setup(seq_coll_op);
                /* else, start firing bcol functions */
                while(true) {

                    fn_idx      = seq_coll_op->sequential_routine.current_active_bcol_fn;
                    const_args  = &seq_coll_op->coll_schedule->
                        component_functions[fn_idx].constant_group_data;
                    coll_fn = seq_coll_op->coll_schedule->
                        component_functions[fn_idx].bcol_function->coll_fn;
                    rc = coll_fn(&seq_coll_op->variable_fn_params,
                            (coll_ml_function_t *) const_args);

                    if (BCOL_FN_COMPLETE == rc) {

                        seq_coll_op->sequential_routine.current_active_bcol_fn++;
                        fn_idx  = seq_coll_op->sequential_routine.current_active_bcol_fn;

                        /* done with this routine,
                         * check for collective completion */
                        if (seq_coll_op->sequential_routine.current_active_bcol_fn  ==
                                seq_coll_op->coll_schedule->n_fns) {
                            /* remove from the progress list */
                            seq_coll_op_tmp = (mca_coll_ml_collective_operation_progress_t *)
                                opal_list_remove_item(SEQ_L, (opal_list_item_t *)seq_coll_op);

                            /* handle fragment completion */
                            rc = coll_ml_fragment_completion_processing(seq_coll_op);
                            if (OMPI_SUCCESS != rc) {
                                mca_coll_ml_abort_ml("Failed to run coll_ml_fragment_completion_processing");
                            }

                            /* make sure that for will pick up right one */
                            seq_coll_op = seq_coll_op_tmp;

                            /* break out of while loop */
                            break;
                        }else {
                            /* setup the next task */
                            /* sequential task setup */
                            seq_coll_op->sequential_routine.seq_task_setup(seq_coll_op);
                        }

                    }else if (BCOL_FN_NOT_STARTED == rc) {

                        seq_coll_op->sequential_routine.current_bcol_status = SEQ_TASK_PENDING;

                        break;
                    } else {
                        break;
                    }

                }
            }


        } else if (BCOL_FN_NOT_STARTED == rc ){
            seq_coll_op->sequential_routine.current_bcol_status = SEQ_TASK_PENDING;
        }

    }
    OPAL_THREAD_UNLOCK(&(cm->sequential_collectives_mutex));

    /* general dag's */
    /* see if active tasks can be progressed */
    OPAL_THREAD_LOCK(&(cm->active_tasks_mutex));
    for (task_status  = (mca_coll_ml_task_status_t *)opal_list_get_first(ACTIVE_L);
         task_status != (mca_coll_ml_task_status_t *)opal_list_get_end(ACTIVE_L);
         task_status  = (mca_coll_ml_task_status_t *)opal_list_get_next(task_status)
        )
    {
        /* progress task */
        progress_fn = task_status->bcol_fn->progress_fn;
        const_args = &task_status->ml_coll_operation->coll_schedule->
            component_functions[INDEX(task_status)].constant_group_data;
        rc = progress_fn(&(task_status->ml_coll_operation->variable_fn_params),
                (coll_ml_function_t *)const_args);
        if (BCOL_FN_COMPLETE == rc) {
            ML_VERBOSE(3, ("GOT BCOL_COMPLETED!!!!"));
            rc = mca_coll_ml_task_completion_processing(&task_status, ACTIVE_L);
            if (OMPI_SUCCESS != rc) {
                mca_coll_ml_abort_ml("Failed to run mca_coll_ml_task_completion_processing");
            }
        } else if (BCOL_FN_STARTED == rc) {
            /* nothing to do */
        } else {
            mca_coll_ml_abort_ml("Failed to run mca_coll_ml_task_completion_processing");
        }
    }
    OPAL_THREAD_UNLOCK(&(cm->active_tasks_mutex));

    /* see if new tasks can be initiated */
    OPAL_THREAD_LOCK(&(cm->pending_tasks_mutex));
    for (task_status  = (mca_coll_ml_task_status_t *) opal_list_get_first(PENDING_L);
         task_status != (mca_coll_ml_task_status_t *) opal_list_get_end(PENDING_L);
         task_status  = (mca_coll_ml_task_status_t *) opal_list_get_next(task_status))
    {
        /* check to see if dependencies are satisfied */
        int n_dependencies = task_status->rt_num_dependencies;
        int n_dependencies_satisfied = task_status->n_dep_satisfied;

        if (n_dependencies == n_dependencies_satisfied) {
            /* initiate the task */
            coll_fn = task_status->bcol_fn->coll_fn;
            const_args = &task_status->ml_coll_operation->coll_schedule->
                component_functions[INDEX(task_status)].constant_group_data;
            rc = coll_fn(&(task_status->ml_coll_operation->variable_fn_params),
                    (coll_ml_function_t *)const_args);
            if (BCOL_FN_COMPLETE == rc) {
                ML_VERBOSE(3, ("GOT BCOL_COMPLETED!"));
                rc = mca_coll_ml_task_completion_processing(&task_status, PENDING_L);
                if (OMPI_SUCCESS != rc) {
                    mca_coll_ml_abort_ml("Failed to run mca_coll_ml_task_completion_processing");
                }
            } else if ( BCOL_FN_STARTED == rc ) {
                ML_VERBOSE(3, ("GOT BCOL_STARTED!"));
                task_status_tmp = (mca_coll_ml_task_status_t *)
                    opal_list_remove_item(PENDING_L, (opal_list_item_t *)task_status);
                /* RLG - is there potential for deadlock here ?  Need to
                 * look at this closely
                 */
                OPAL_THREAD_LOCK(&(cm->active_tasks_mutex));
                opal_list_append(ACTIVE_L, (opal_list_item_t *)task_status);
                OPAL_THREAD_UNLOCK(&(cm->active_tasks_mutex));
                task_status = task_status_tmp;
            } else if( BCOL_FN_NOT_STARTED == rc ) {
                /* nothing to do */
                ML_VERBOSE(10, ("GOT BCOL_FN_NOT_STARTED!"));
            } else {
                OPAL_THREAD_UNLOCK(&(cm->pending_tasks_mutex));
                /* error will be returned - RLG : need to reconsider return
                 * types - we have no way to convey error information
                 * the way the code is implemented now */
                ML_VERBOSE(3, ("GOT error !"));
                rc = OMPI_ERROR;
                break;
            }
        }
    }
    OPAL_THREAD_UNLOCK(&(cm->pending_tasks_mutex));

    /* return */
    cm->progress_is_busy = false;

    return rc;
}


static void adjust_coll_config_by_mca_param(void)
{
    assert(false == mca_coll_ml_component.use_static_bcast ||
           false == mca_coll_ml_component.use_sequential_bcast);
	
    /* setting bcast mca params */
    if (mca_coll_ml_component.use_static_bcast) {
        mca_coll_ml_component.coll_config[ML_BCAST][ML_SMALL_MSG].algorithm_id = ML_BCAST_SMALL_DATA_KNOWN;
        mca_coll_ml_component.coll_config[ML_BCAST][ML_LARGE_MSG].algorithm_id = ML_BCAST_LARGE_DATA_KNOWN;
    } else if (mca_coll_ml_component.use_sequential_bcast){
        mca_coll_ml_component.coll_config[ML_BCAST][ML_SMALL_MSG].algorithm_id = ML_BCAST_SMALL_DATA_SEQUENTIAL;
        mca_coll_ml_component.coll_config[ML_BCAST][ML_LARGE_MSG].algorithm_id = ML_BCAST_LARGE_DATA_SEQUENTIAL;
    } else { /* Unknown root */
        mca_coll_ml_component.coll_config[ML_BCAST][ML_SMALL_MSG].algorithm_id = ML_BCAST_SMALL_DATA_UNKNOWN;
        mca_coll_ml_component.coll_config[ML_BCAST][ML_LARGE_MSG].algorithm_id = ML_BCAST_LARGE_DATA_UNKNOWN;
    }
}

/*
 * Open the component
 */
static int ml_open(void)
{
    /* local variables */
    int rc, c_idx, m_idx;
    mca_coll_ml_component_t *cs = &mca_coll_ml_component;


    /* set the starting sequence number */
    cs->base_sequence_number = -1;
    cs->progress_is_busy = false;

    mca_coll_ml_component.config_file_name = NULL;

    /* load mca parametres */
    rc = mca_coll_ml_register_params();
    if (OMPI_SUCCESS != rc) {
        return OMPI_ERROR;
    }

    /* If the priority is zero (default) disable the component */
    if (mca_coll_ml_component.ml_priority <= 0) {
        return OMPI_ERR_NOT_AVAILABLE;
    }

    /* Init memory structures (no real memory is allocated) */
    OBJ_CONSTRUCT(&cs->memory_manager, mca_coll_ml_lmngr_t);

    if (OMPI_SUCCESS != (rc = mca_sbgp_base_open())) {
        fprintf(stderr," failure in open mca_sbgp_base_open \n");
        return rc;
    }
    if (OMPI_SUCCESS != (rc = mca_bcol_base_open())) {
        fprintf(stderr," failure in open mca_bcol_base_open \n");
        return rc;
    }

    /* Reset collective tunings cache */
    for (c_idx = 0; c_idx < ML_NUM_OF_FUNCTIONS; c_idx++) {
        for (m_idx = 0; m_idx < ML_NUM_MSG; m_idx++) {
            mca_coll_ml_reset_config(&cs->coll_config[c_idx][m_idx]);
        }
    }

    adjust_coll_config_by_mca_param();

    /* Load configuration file and cache the configuration on component */
    rc = mca_coll_ml_config_file_init();
    if (OMPI_SUCCESS != rc) {
        return OMPI_ERROR;
    }

    /* reigster the progress function */
    rc = opal_progress_register(coll_ml_progress);
    if (OMPI_SUCCESS != rc ) {
        fprintf(stderr," failed to register the ml progress function \n");
        fflush(stderr);
        return rc;
    }

    OBJ_CONSTRUCT(&(cs->pending_tasks_mutex), opal_mutex_t);
    OBJ_CONSTRUCT(&(cs->pending_tasks), opal_list_t);
    OBJ_CONSTRUCT(&(cs->active_tasks_mutex), opal_mutex_t);
    OBJ_CONSTRUCT(&(cs->active_tasks), opal_list_t);
    OBJ_CONSTRUCT(&(cs->sequential_collectives_mutex), opal_mutex_t);
    OBJ_CONSTRUCT(&(cs->sequential_collectives), opal_list_t);

    rc = ompi_common_netpatterns_init();
    if (OMPI_SUCCESS != rc) {
        return rc;
    }

    cs->topo_discovery_fn[COLL_ML_HR_FULL] =
        mca_coll_ml_fulltree_hierarchy_discovery;

    cs->topo_discovery_fn[COLL_ML_HR_ALLREDUCE] =
        mca_coll_ml_allreduce_hierarchy_discovery;

    cs->topo_discovery_fn[COLL_ML_HR_NBS] =
        mca_coll_ml_fulltree_exclude_basesmsocket_hierarchy_discovery;

    cs->topo_discovery_fn[COLL_ML_HR_SINGLE_PTP] =
        mca_coll_ml_fulltree_ptp_only_hierarchy_discovery;

    cs->topo_discovery_fn[COLL_ML_HR_SINGLE_IBOFFLOAD] =
        mca_coll_ml_fulltree_iboffload_only_hierarchy_discovery;

    cs->need_allreduce_support = false;

    return OMPI_SUCCESS;
}

/*
 * Close the component
 */
static int ml_close(void)
{
    int ret;

    mca_coll_ml_component_t *cs = &mca_coll_ml_component;

    /* Free the config file name (allocated by mca_coll_ml_register_params) */
    if (NULL != mca_coll_ml_component.config_file_name) {
        free (mca_coll_ml_component.config_file_name);
        mca_coll_ml_component.config_file_name = NULL;
    }

    /* There is not need to release/close resource if the 
     * priority was set to zero */
    if (cs->ml_priority <= 0) {
        return OMPI_SUCCESS;
    }

    OBJ_DESTRUCT(&cs->memory_manager);

    /* deregister progress function */
    ret=opal_progress_unregister(coll_ml_progress);
    if (OMPI_SUCCESS != ret ) {
        fprintf(stderr," failed to un-register the ml progress function \n");
        fflush(stderr);
        return ret;
    }

    return OMPI_SUCCESS;
}

/* query to see if the component is available for use, and can
 * satisfy the thread and progress requirements
 */
int mca_coll_ml_init_query(bool enable_progress_threads,
                           bool enable_mpi_threads)
{
    int ret;

    /* at this stage there is no reason to disaulify this component */
    /* Add here bcol init nand sbgp init */
    ret = mca_sbgp_base_init(enable_progress_threads, enable_mpi_threads);
    if (OMPI_SUCCESS != ret) {
        return ret;
    }

    ret = mca_bcol_base_init(enable_progress_threads, enable_mpi_threads);
    if (OMPI_SUCCESS != ret) {
        return ret;
    }

    /* done */
    return OMPI_SUCCESS;
}