coll_tuned_reduce.c

来自「MPI stands for the Message Passing Inter」· C语言 代码 · 共 549 行 · 第 1/2 页

C
549
字号
/* * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana *                         University Research and Technology *                         Corporation.  All rights reserved. * Copyright (c) 2004-2006 The University of Tennessee and The University *                         of Tennessee Research Foundation.  All rights *                         reserved. * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart, *                         University of Stuttgart.  All rights reserved. * Copyright (c) 2004-2005 The Regents of the University of California. *                         All rights reserved. * $COPYRIGHT$ * * Additional copyrights may follow * * $HEADER$ */#include "ompi_config.h"#include "mpi.h"#include "ompi/constants.h"#include "ompi/datatype/datatype.h"#include "ompi/communicator/communicator.h"#include "ompi/mca/coll/coll.h"#include "ompi/mca/coll/base/coll_tags.h"#include "ompi/mca/pml/pml.h"#include "ompi/op/op.h"#include "coll_tuned.h"#include "coll_tuned_topo.h"/** * This is a generic implementation of the reduce protocol. It used the tree * provided as an argument and execute all operations using a segment of * count times a datatype. * For the last communication it will update the count in order to limit * th number of datatype to the original count (original_count) */int ompi_coll_tuned_reduce_generic( void* sendbuf, void* recvbuf, int original_count,                                    ompi_datatype_t* datatype, ompi_op_t* op,                                    int root, ompi_communicator_t* comm,                                    ompi_coll_tree_t* tree, int count_by_segment ){    char *inbuf[2] = {(char*)NULL, (char*)NULL};    char *local_op_buffer = NULL, *accumbuf = NULL, *sendtmpbuf = NULL;    ptrdiff_t extent, lower_bound;    size_t typelng, realsegsize;    ompi_request_t* reqs[2] = {MPI_REQUEST_NULL, MPI_REQUEST_NULL};    int num_segments, line, ret, segindex, i, rank;    int recvcount, prevcount, inbi, previnbi;    /**     * Determine number of segments and number of elements     * sent per operation     */    ompi_ddt_get_extent( datatype, &lower_bound, &extent );    ompi_ddt_type_size( datatype, &typelng );    num_segments = (original_count + count_by_segment - 1) / count_by_segment;    realsegsize = count_by_segment * extent;    sendtmpbuf = (char*) sendbuf;     if( sendbuf == MPI_IN_PLACE ) {         sendtmpbuf = (char *)recvbuf;     }    rank = ompi_comm_rank(comm);    /* non-leaf nodes - wait for children to send me data & forward up (if needed) */    if( tree->tree_nextsize > 0 ) {        /* handle non existant recv buffer (i.e. its NULL) and            protect the recv buffer on non-root nodes */        accumbuf = (char*)recvbuf;        if( (NULL == accumbuf) || (root != rank) ) {            accumbuf = (char*)malloc(realsegsize * num_segments);  /* TO BE OPTIMIZED */	    if (accumbuf == NULL) { line = __LINE__; ret = -1; goto error_hndl; }	}         /* Allocate two buffers for incoming segments */        inbuf[0] = (char*) malloc(realsegsize);        if( inbuf[0] == NULL ) { line = __LINE__; ret = -1; goto error_hndl; }        /* if there is chance to overlap communication -           allocate second buffer */        if( (num_segments > 1) || (tree->tree_nextsize > 1) ) {            inbuf[1] = (char*) malloc(realsegsize);            if( inbuf[1] == NULL ) { line = __LINE__; ret = -1; goto error_hndl;}        } else {            inbuf[1] = NULL;        }        /* reset input buffer index and receive count */        inbi = 0;        recvcount = 0;        /* for each segment */        for( segindex = 0; segindex <= num_segments; segindex++ ) {            prevcount = recvcount;            /* recvcount - number of elements in current segment */            recvcount = count_by_segment;            if( segindex == (num_segments-1) )                recvcount = original_count - count_by_segment * segindex;            /* for each child */            for( i = 0; i < tree->tree_nextsize; i++ ) {                /**                 * We try to overlap communication:                 * either with next segment or with the next child                 */                /* post irecv for current segindex on current child */                if( segindex < num_segments ) {                    void* local_recvbuf = inbuf[inbi];                    if( 0 == i ) {                        /* for the first step (1st child per segment) we might be able to                         * irecv directly into the accumulate buffer so that we can                         * reduce(op) this with our sendbuf in one step as ompi_op_reduce                         * only has two buffer pointers, this avoids an extra memory copy.                         *                         * BUT if we are root and are USING MPI_IN_PLACE this is wrong ek!                         * check for root might not be needed as it should be checked higher up                         */                        if( !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {                            local_recvbuf = accumbuf + segindex * realsegsize;                        }                    }                    ret = MCA_PML_CALL(irecv(local_recvbuf, recvcount, datatype, tree->tree_next[i],                                             MCA_COLL_BASE_TAG_REDUCE, comm, &reqs[inbi]));                    if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl;  }                }                /* wait for previous req to complete, if any */                previnbi = (inbi+1) % 2;                /* wait on data from last child for previous segment */                ret = ompi_request_wait_all( 1, &reqs[previnbi], MPI_STATUSES_IGNORE );                if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl;  }                local_op_buffer = inbuf[previnbi];                if( i > 0 ) {                    /* our first operation is to combine our own [sendbuf] data with the data                     * we recvd from down stream (but only if we are not root and not using                     * MPI_IN_PLACE)                     */                    if( 1 == i ) {                        if( !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {                            local_op_buffer = sendtmpbuf + segindex * realsegsize;                        }                    }                    /* apply operation */                    ompi_op_reduce(op, local_op_buffer, accumbuf+segindex*realsegsize, recvcount, datatype );                } else if ( segindex > 0 ) {                    void* accumulator = accumbuf + (segindex-1) * realsegsize;                    if( tree->tree_nextsize <= 1 ) {                        if( !((MPI_IN_PLACE == sendbuf) && (rank == tree->tree_root)) ) {                                        local_op_buffer = sendtmpbuf+(segindex-1)*realsegsize;                        }                    }                    ompi_op_reduce(op, local_op_buffer, accumulator, prevcount, datatype );                    /* all reduced on available data this step (i) complete, pass to                     * the next process unless your the root                     */                    if (rank != tree->tree_root) {                        /* send combined/accumulated data to parent */                        ret = MCA_PML_CALL( send( accumulator, prevcount, datatype,                                                  tree->tree_prev, MCA_COLL_BASE_TAG_REDUCE,                                                  MCA_PML_BASE_SEND_STANDARD, comm) );                        if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl;  }                    }                    /* we stop when segindex = number of segments (i.e. we do num_segment+1 steps to allow for pipelining */                    if (segindex == num_segments) break;                }                /* update input buffer index */                inbi = previnbi;            } /* end of for each child */        } /* end of for each segment */        /* clean up */        if( inbuf[0] != NULL) free(inbuf[0]);        if( inbuf[1] != NULL) free(inbuf[1]);        if( (NULL == recvbuf) || (root != rank) ) free(accumbuf);    }    /* leaf nodes */    else {        /* Send segmented data to parents */        segindex = 0;        while( original_count > 0 ) {            if( original_count < count_by_segment ) count_by_segment = original_count;            ret = MCA_PML_CALL( send((char*)sendbuf + segindex * realsegsize, count_by_segment,                                     datatype, tree->tree_prev,                                     MCA_COLL_BASE_TAG_REDUCE, MCA_PML_BASE_SEND_STANDARD, comm) );            if (ret != MPI_SUCCESS) { line = __LINE__; goto error_hndl;  }            segindex++;            original_count -= count_by_segment;        }    }    return OMPI_SUCCESS; error_hndl:  /* error handler */    OPAL_OUTPUT (( ompi_coll_tuned_stream, "ERROR_HNDL: node %d file %s line %d error %d\n", rank, __FILE__, line, ret ));    if( inbuf[0] != NULL ) free(inbuf[0]);    if( inbuf[1] != NULL ) free(inbuf[1]);    if( (NULL == recvbuf) && (NULL != accumbuf) ) free(accumbuf);    return ret;}/* Attention: this version of the reduce operations does not   work for:   - non-commutative operations   - segment sizes which are not multiplies of the extent of the datatype     meaning that at least one datatype must fit in the segment !*/int ompi_coll_tuned_reduce_intra_chain( void *sendbuf, void *recvbuf, int count,                                        ompi_datatype_t* datatype, ompi_op_t* op,                                        int root, ompi_communicator_t* comm, uint32_t segsize,                                        int fanout){    int segcount = count;    size_t typelng;    OPAL_OUTPUT((ompi_coll_tuned_stream,"coll:tuned:reduce_intra_chain rank %d fo %d ss %5d", ompi_comm_rank(comm), fanout, segsize));    COLL_TUNED_UPDATE_CHAIN( comm, root, fanout );    /**     * Determine number of segments and number of elements     * sent per operation     */    ompi_ddt_type_size( datatype, &typelng );    COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );    return ompi_coll_tuned_reduce_generic( sendbuf, recvbuf, count, datatype, op, root, comm,                                           comm->c_coll_selected_data->cached_chain, segcount );}int ompi_coll_tuned_reduce_intra_pipeline( void *sendbuf, void *recvbuf,                                           int count, ompi_datatype_t* datatype,                                           ompi_op_t* op, int root,                                           ompi_communicator_t* comm, uint32_t segsize ){    int segcount = count;    size_t typelng;    OPAL_OUTPUT((ompi_coll_tuned_stream,"coll:tuned:reduce_intra_pipeline rank %d ss %5d",                 ompi_comm_rank(comm), segsize));    COLL_TUNED_UPDATE_PIPELINE( comm, root );    /**     * Determine number of segments and number of elements     * sent per operation     */    ompi_ddt_type_size( datatype, &typelng );    COLL_TUNED_COMPUTED_SEGCOUNT( segsize, typelng, segcount );    return ompi_coll_tuned_reduce_generic( sendbuf, recvbuf, count, datatype, op, root, comm,                                           comm->c_coll_selected_data->cached_pipeline, segcount );}int ompi_coll_tuned_reduce_intra_binary( void *sendbuf, void *recvbuf,                                         int count, ompi_datatype_t* datatype,                                         ompi_op_t* op, int root,                                         ompi_communicator_t* comm, uint32_t segsize ){    int segcount = count;    size_t typelng;    OPAL_OUTPUT((ompi_coll_tuned_stream,"coll:tuned:reduce_intra_binary rank %d ss %5d",                 ompi_comm_rank(comm), segsize));    COLL_TUNED_UPDATE_BINTREE( comm, root );    /**     * Determine number of segments and number of elements     * sent per operation     */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?