Ignore:
Timestamp:
02/17/21 09:25:47 (4 years ago)
Author:
bulthuis
Message:

CGH: adding elementwise multiplication and power for vectors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/toolkits/issm/IssmMpiVec.h

    r25336 r25995  
    11/*!\file:  IssmMpiVec.h
    2  * \brief implementation of parallel dense ISSM vector. Internally, the parallel dense vector is 
    3  * split in rows across each cpu. Each vector (representing a subset of rows) on each cpu is fully 
    4  * dense, and is represented by a linear buffer of type doubletype. 
    5  * This object needs to answer the API defined by the virtual functions in IssmAbsVec, 
     2 * \brief implementation of parallel dense ISSM vector. Internally, the parallel dense vector is
     3 * split in rows across each cpu. Each vector (representing a subset of rows) on each cpu is fully
     4 * dense, and is represented by a linear buffer of type doubletype.
     5 * This object needs to answer the API defined by the virtual functions in IssmAbsVec,
    66 * and the contructors required by IssmVec (see IssmVec.h)
    7  */ 
     7 */
    88
    99#ifndef _ISSM_MPI_VEC_H_
     
    2626/*}}}*/
    2727
    28 /*We need to template this class, in case we want to create vectors that hold IssmDouble* vector or IssmPDouble* vector. 
    29   Such vectors would be useful for use without or with the matlab or python interface (which do not care for IssmDouble types, 
     28/*We need to template this class, in case we want to create vectors that hold IssmDouble* vector or IssmPDouble* vector.
     29  Such vectors would be useful for use without or with the matlab or python interface (which do not care for IssmDouble types,
    3030  but only rely on IssmPDouble types)*/
    3131template <class doubletype> class IssmAbsVec;
    3232
    33 template <class doubletype> 
     33template <class doubletype>
    3434class IssmMpiVec:public IssmAbsVec<doubletype>{
    3535
     
    206206
    207207                        /*Recap, each cpu has num_procs datasets of buckets. For a certain cpu j, for a given dataset i, the buckets this  {{{
    208                          * dataset owns correspond to rows that are owned by cpu i, not j!. Out of all the buckets we own, make row,col,value,insert_mode 
     208                         * dataset owns correspond to rows that are owned by cpu i, not j!. Out of all the buckets we own, make row,col,value,insert_mode
    209209                         * vectors that will be shipped around the cluster: */
    210210                        this->BucketsBuildScatterBuffers(&numvalues_forcpu,&row_indices_forcpu,&values_forcpu,&modes_forcpu,bucketsforcpu,num_procs);
     
    244244
    245245                        /*Scatter values around: {{{*/
    246                         /*Now, to scatter values across the cluster, we need sendcnts and displs. Our sendbufs have been built by BucketsBuildScatterBuffers, with a stride given 
    247                          * by numvalues_forcpu. Get this ready to go before starting the scatter itslef. For reference, here is the ISSM_MPI_Scatterv prototype: 
     246                        /*Now, to scatter values across the cluster, we need sendcnts and displs. Our sendbufs have been built by BucketsBuildScatterBuffers, with a stride given
     247                         * by numvalues_forcpu. Get this ready to go before starting the scatter itslef. For reference, here is the ISSM_MPI_Scatterv prototype:
    248248                         * int ISSM_MPI_Scatterv( void *sendbuf, int *sendcnts, int *displs, ISSM_MPI_Datatype sendtype, void *recvbuf, int recvcnt, ISSM_MPI_Datatype recvtype, int root, ISSM_MPI_Comm comm) :*/
    249249                        sendcnts=xNew<int>(num_procs);
     
    308308                        xDelete<int>(sendcnts);
    309309                        xDelete<int>(displs);
    310                        
     310
    311311                        /*Get rid of all buckets, as we have already added them to the matrix!: */
    312312                        delete this->buckets;
     
    318318                void SetValues(int ssize, int* list, doubletype* values, InsMode mode){/*{{{*/
    319319
    320                         /*we need to store all the values we collect here in order to Assemble later. 
    321                          * Indeed, the values we are collecting here most of the time will not belong 
     320                        /*we need to store all the values we collect here in order to Assemble later.
     321                         * Indeed, the values we are collecting here most of the time will not belong
    322322                         * to us, but to another part of the vector on another cpu: */
    323323                        _assert_(buckets);
     
    329329                void SetValue(int dof, doubletype value, InsMode mode){/*{{{*/
    330330
    331                         /*we need to store the value we collect here in order to Assemble later. 
    332                          * Indeed, the value we are collecting here most of the time will not belong 
     331                        /*we need to store the value we collect here in order to Assemble later.
     332                         * Indeed, the value we are collecting here most of the time will not belong
    333333                         * to us, but to another part of the vector on another cpu: */
    334334                        _assert_(buckets);
     
    365365
    366366                        /*Get Ownership range*/
    367                         int lower_row,upper_row; 
     367                        int lower_row,upper_row;
    368368                        GetOwnershipBoundariesFromRange(&lower_row,&upper_row,m,IssmComm::GetComm());
    369                         int range=upper_row-lower_row;   
     369                        int range=upper_row-lower_row;
    370370
    371371                        /*return NULL if no range*/
     
    377377
    378378                        /*Build indices*/
    379                         int* indices=xNew<int>(range); 
     379                        int* indices=xNew<int>(range);
    380380                        for(int i=0;i<range;i++) indices[i]=lower_row+i;
    381381
     
    518518                                        break;
    519519                                case NORM_TWO:
    520                                         local_norm=0.; 
     520                                        local_norm=0.;
    521521                                        for(i=0;i<this->m;i++)local_norm+=this->vector[i]*this->vector[i];
    522522                                        ISSM_MPI_Reduce(&local_norm, &norm, 1, ISSM_MPI_DOUBLE, ISSM_MPI_SUM, 0, IssmComm::GetComm());
     
    570570                        /*pointwise w=x/y where this->vector is w: */
    571571                        for(i=0;i<this->m;i++)this->vector[i]=x->vector[i]/y->vector[i];
     572                }
     573                /*}}}*/
     574                void PointwiseMult(IssmAbsVec<doubletype>* xin,IssmAbsVec<doubletype>* yin){/*{{{*/
     575
     576                        int i;
     577
     578                        /*Assume xin and yin are of the correct type, and downcast: */
     579                        IssmMpiVec* x=NULL;
     580                        IssmMpiVec* y=NULL;
     581
     582                        x=(IssmMpiVec<doubletype>*)xin;
     583                        y=(IssmMpiVec<doubletype>*)yin;
     584
     585                        /*pointwise w=x*y where this->vector is w: */
     586                        for(i=0;i<this->m;i++)this->vector[i]=x->vector[i]*y->vector[i];
     587                }
     588                /*}}}*/
     589                void Pow(doubletype scale_factor){/*{{{*/
     590
     591                        int i;
     592                        for(i=0;i<this->M;i++)this->vector[i]=pow(this->vector[i],scale_factor);
     593
    572594                }
    573595                /*}}}*/
     
    623645
    624646                        /*we are going to march through the buffers, and marshall data onto them, so in order to not
    625                          *lose track of where these buffers are located in memory, we are going to work using copies 
     647                         *lose track of where these buffers are located in memory, we are going to work using copies
    626648                         of them: */
    627649                        temp_row_indices_forcpu=row_indices_forcpu;
     
    649671                        *pmodes_forcpu       = modes_forcpu;
    650672                }
    651                 /*}}}*/         
     673                /*}}}*/
    652674};
    653 #endif //#ifndef _ISSM_MPI_VEC_H_       
     675#endif //#ifndef _ISSM_MPI_VEC_H_
Note: See TracChangeset for help on using the changeset viewer.