Changeset 14792


Ignore:
Timestamp:
04/29/13 15:36:48 (12 years ago)
Author:
Eric.Larour
Message:

CHG: finished implementation of IssmMpiVec and IssmMpiDenseMat objects. Still need to V&V.
CHG: moved the src/c/classes/matrix/Matrix and Vector objects to src/c/classes/toolkits.
CHG and NEW: offloaded the solver code to the toolkits. To do so, created a Solver class
in the src/c/classes/toolkits, which connects the toolkits to the abstract classes Matrix
and Vector.

Location:
issm/trunk-jpl/src/c
Files:
7 added
3 deleted
10 edited
2 moved

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/Makefile.am

    r14769 r14792  
    119119                                        ./classes/matrix/ElementVector.h\
    120120                                        ./classes/matrix/ElementVector.cpp\
    121                                         ./classes/matrix/Matrix.h\
    122                                         ./classes/matrix/Vector.h\
     121                                        ./classes/toolkits/toolkitobjects.h\
     122                                        ./classes/toolkits/Matrix.h\
     123                                        ./classes/toolkits/Vector.h\
     124                                        ./classes/toolkits/Solver.h\
    123125                                        ./classes/objects/Params/Param.h\
    124126                                        ./classes/objects/Params/GenericParam.h\
     
    229231                                        ./toolkits/issm/IssmSeqVec.h\
    230232                                        ./toolkits/issm/IssmVec.h\
     233                                        ./toolkits/issm/IssmSolver.h\
     234                                        ./toolkits/issm/IssmSolver.cpp\
    231235                                        ./toolkits/triangle/triangleincludes.h\
    232236                                        ./toolkitsenums.h\
     
    320324                                        ./modules/Solverx/Solverx.cpp\
    321325                                        ./modules/Solverx/Solverx.h\
    322                                         ./modules/Solverx/SolverxSeq.cpp\
    323326                                        ./modules/VecMergex/VecMergex.cpp\
    324327                                        ./modules/VecMergex/VecMergex.h\
     
    760763                                        ./toolkits/petsc/objects/PetscVec.h\
    761764                                        ./toolkits/petsc/objects/PetscVec.cpp\
    762                                         ./toolkits/petsc/petscincludes.h\
    763                                         ./modules/Solverx/SolverxPetsc.cpp\
    764                                         ./modules/Solverx/DofTypesToIndexSet.cpp
     765                                        ./toolkits/petsc/objects/PetscSolver.cpp\
     766                                        ./toolkits/petsc/objects/PetscSolver.h\
     767                                        ./toolkits/petsc/petscincludes.h
    765768
    766769#}}}
  • issm/trunk-jpl/src/c/classes/classes.h

    r14734 r14792  
    1111/*matrix: */
    1212#include "./matrix/matrixobjects.h"
     13
     14/*toolkit objects: */
     15#include "./toolkits/toolkitobjects.h"
    1316
    1417/*bamg: */
  • issm/trunk-jpl/src/c/classes/matrix/matrixobjects.h

    r13623 r14792  
    99#include "./ElementMatrix.h"
    1010#include "./ElementVector.h"
    11 #include "./Vector.h"
    12 #include "./Matrix.h"
    1311
    1412#endif
  • issm/trunk-jpl/src/c/classes/toolkits/Matrix.h

    r14782 r14792  
    324324                }
    325325                /*}}}*/
     326
    326327};
    327328
  • issm/trunk-jpl/src/c/modules/Solverx/CMakeLists.txt

    r14284 r14792  
    55# }}}
    66# CORE_SOURCES {{{
    7 set(CORE_SOURCES $ENV{ISSM_DIR}/src/c/modules/Solverx/Solverx.cpp
    8               $ENV{ISSM_DIR}/src/c/modules/Solverx/SolverxSeq.cpp PARENT_SCOPE)
     7set(CORE_SOURCES $ENV{ISSM_DIR}/src/c/modules/Solverx/Solverx.cpp PARENT_SCOPE)
    98# }}}
  • issm/trunk-jpl/src/c/modules/Solverx/Solverx.cpp

    r14665 r14792  
    22 * \brief solver
    33 */
    4 
    5 #include "./Solverx.h"
    6 #include "../../shared/shared.h"
    7 #include "../../include/include.h"
    8 #include "../../io/io.h"
    94
    105#ifdef HAVE_CONFIG_H
     
    149#endif
    1510
     11#include "./Solverx.h"
     12#include "../../shared/shared.h"
     13
    1614void    Solverx(Vector<IssmDouble>** puf, Matrix<IssmDouble>* Kff, Vector<IssmDouble>* pf, Vector<IssmDouble>* uf0,Vector<IssmDouble>* df, Parameters* parameters){
    1715
     16        /*intermediary: */
     17        Solver<IssmDouble> *solver=NULL;
     18       
    1819        /*output: */
    1920        Vector<IssmDouble> *uf=NULL;
    2021
    21         /*In debugging mode, check that stiffness matrix and load vectors are not NULL (they can be empty)*/
    22         _assert_(Kff);
    23         _assert_(pf);
    24        
    2522        if(VerboseModule()) _pprintLine_("   Solving matrix system");
    2623
    27         /*Initialize vector: */
    28         uf=new Vector<IssmDouble>();
     24        /*Initialize solver: */
     25        solver=new Solver<IssmDouble>(Kff,pf,uf0,df,parameters);
    2926
    30         /*According to matrix type, use specific solvers: */
    31         switch(Kff->type){
    32                 #ifdef _HAVE_PETSC_
    33                 case PetscMatType:{
    34                         PetscVec* uf0_vector = NULL;
    35                         PetscVec* df_vector  = NULL;
    36                         if(uf0) uf0_vector = uf0->pvector;
    37                         if(df)  df_vector  = df->pvector;
    38                         SolverxPetsc(&uf->pvector,Kff->pmatrix,pf->pvector,uf0_vector,df_vector,parameters);
    39                         break;}
    40                 #endif
    41                 case IssmMatType:{
    42                         SolverxSeq(&uf->ivector,Kff->imatrix,pf->ivector,parameters);
    43                         break;}
    44                 default:
    45                           _error_("Matrix type: " << Kff->type << " not supported yet!");
    46         }
     27        /*Solve:*/
     28        uf=solver->Solve();
    4729
    4830        /*Assign output pointers:*/
  • issm/trunk-jpl/src/c/modules/Solverx/Solverx.h

    r14656 r14792  
    1212#endif
    1313
    14 #include "../../classes/objects/objects.h"
    15 #include "../../toolkits/toolkits.h"
     14#include "../../classes/toolkits/toolkitobjects.h"
    1615
    1716/* local prototypes: */
    1817void    Solverx(Vector<IssmDouble>** puf, Matrix<IssmDouble>* Kff, Vector<IssmDouble>* pf, Vector<IssmDouble>* uf0,Vector<IssmDouble>* df, Parameters* parameters);
    1918
    20 #ifdef _HAVE_PETSC_
    21 void    SolverxPetsc(PetscVec** puf, PetscMat* Kff, PetscVec* pf, PetscVec* uf0,PetscVec* df, Parameters* parameters);
    22 void    SolverxPetsc(Vec* puf, Mat Kff, Vec pf, Vec uf0,Vec df, Parameters* parameters);
    23 void  DofTypesToIndexSet(IS* pisv, IS* pisp, Vec df,int typeenum);
    24 #endif
    25 
    26 void SolverxSeq(IssmVec<IssmDouble>** puf,IssmMat<IssmDouble>* Kff, IssmVec<IssmDouble>* pf,Parameters* parameters);
    27 void SolverxSeq(IssmPDouble **pX, IssmPDouble *A, IssmPDouble *B,int n);
    28 void SolverxSeq(IssmPDouble *X, IssmPDouble *A, IssmPDouble *B,int n);
    29 
    30 #if defined(_HAVE_ADOLC_) && !defined(_WRAPPERS_)
    31 void SolverxSeq(IssmDouble *X,IssmDouble *A,IssmDouble *B,int n, Parameters* parameters);
    32 // call back functions:
    33 ADOLC_ext_fct EDF_for_solverx;
    34 ADOLC_ext_fct_fos_forward EDF_fos_forward_for_solverx;
    35 ADOLC_ext_fct_fos_reverse EDF_fos_reverse_for_solverx;
    36 ADOLC_ext_fct_fov_forward EDF_fov_forward_for_solverx;
    37 ADOLC_ext_fct_fov_reverse EDF_fov_reverse_for_solverx;
    38 #endif
    39 
    4019#endif  /* _SOLVERX_H */
  • issm/trunk-jpl/src/c/toolkits/issm/IssmMpiDenseMat.h

    r14750 r14792  
    272272                /*FUNCTION Norm{{{*/
    273273                doubletype Norm(NormMode mode){
    274                         _error_("not supported yet!");
     274                       
     275                       
     276                        doubletype norm,local_norm;
     277                        doubletype absolute;
     278                        int i,j;
     279
     280                        switch(mode){
     281                                case NORM_INF:
     282                                        local_norm=0;
     283                                        for(i=0;i<this->M;i++){
     284                                                absolute=0;
     285                                                for(j=0;j<this->N;j++){
     286                                                        absolute+=fabs(this->matrix[N*i+j]);
     287                                                }
     288                                                local_norm=max(local_norm,absolute);
     289                                        }
     290                                        MPI_Reduce(&local_norm, &norm, 1, MPI_DOUBLE, MPI_MAX, 0, IssmComm::GetComm());
     291                                        MPI_Bcast(&norm,1,MPI_DOUBLE,0,IssmComm::GetComm());
     292                                        return norm;
     293                                        break;
     294                                default:
     295                                        _error_("unknown norm !");
     296                                        break;
     297                        }
    275298                }
    276299                /*}}}*/
    277300                /*FUNCTION GetSize{{{*/
    278301                void GetSize(int* pM,int* pN){
    279                         _error_("not supported yet!");
     302                        *pM=M;
     303                        *pN=N;
    280304                }
    281305                /*}}}*/
    282306                /*FUNCTION GetLocalSize{{{*/
    283307                void GetLocalSize(int* pM,int* pN){
    284                         _error_("not supported yet!");
     308                        *pM=m;
     309                        *pN=N;
    285310                }
    286311                /*}}}*/
    287312                /*FUNCTION MatMult{{{*/
    288313                void MatMult(IssmAbsVec<doubletype>* Xin,IssmAbsVec<doubletype>* AXin){
    289                         _error_("not supported yet!");
     314
     315
     316                        int         i,j;
     317                        doubletype *X_serial  = NULL;
     318
     319
     320                        /*A check on the types: */
     321                        if(IssmVecTypeFromToolkitOptions()!=MpiEnum)_error_("MatMult operation only possible with 'mpi' vectors");
     322
     323                        /*Now that we are sure, cast vectors: */
     324                        IssmMpiVec<doubletype>* X=(IssmMpiVec<doubletype>*)Xin;
     325                        IssmMpiVec<doubletype>* AX=(IssmMpiVec<doubletype>*)AXin;
     326
     327                        /*Serialize input Xin: */
     328                        X_serial=X->ToMPISerial();
     329
     330                        /*Every cpu has a serial version of the input vector. Use it to do the Matrix-Vector multiply
     331                         *locally and plug it into AXin: */
     332                        for(i=0;i<this->m;i++){
     333                                for(j=0;j<this->N;j++){
     334                                        AX->vector[i]+=this->matrix[i*N+j]*X_serial[j];
     335                                }
     336                        }
     337
     338                        /*Free ressources: */
     339                        xDelete<doubletype>(X_serial);
    290340                }
    291341                /*}}}*/
    292342                /*FUNCTION Duplicate{{{*/
    293343                IssmMpiDenseMat<doubletype>* Duplicate(void){
    294                         _error_("not supported yet!");
     344
     345                        IssmMpiDenseMat<doubletype>* dup=new IssmMpiDenseMat<doubletype>(this->matrix,this->M,this->N,0);
     346                        return dup;
     347
    295348                }
    296349                /*}}}*/
     
    314367                /*FUNCTION Convert{{{*/
    315368                void Convert(MatrixType type){
    316                         /*do nothing: */
     369                        _error_("not supported yet!");
    317370                }
    318371                /*}}}*/         
  • issm/trunk-jpl/src/c/toolkits/issm/IssmMpiVec.h

    r14750 r14792  
    2222#include "../../shared/Alloc/alloc.h"
    2323#include "../../include/macros.h"
     24#include "../../io/io.h"
    2425#ifdef _HAVE_MPI_
    2526#include "../mpi/mpiincludes.h"
     
    303304                /*FUNCTION AXPY{{{*/
    304305                void AXPY(IssmAbsVec<doubletype>* Xin, doubletype a){
    305                         printf("AXPY not implemented yet!");
    306                         exit(1);
     306                               
     307                        int i;
     308
     309                        /*Assume X is of the correct type, and downcast: */
     310                        IssmMpiVec* X=NULL;
     311
     312                        X=(IssmMpiVec<doubletype>*)Xin;
     313
     314                        /*y=a*x+y where this->vector is y*/
     315                        for(i=0;i<this->m;i++)this->vector[i]=a*X->vector[i]+this->vector[i];
     316
     317
    307318                }
    308319                /*}}}*/
    309320                /*FUNCTION AYPX{{{*/
    310321                void AYPX(IssmAbsVec<doubletype>* Xin, doubletype a){
    311                         printf("AYPX not implemented yet!");
    312                         exit(1);
     322                        int i;
     323
     324                        /*Assume X is of the correct type, and downcast: */
     325                        IssmMpiVec* X=NULL;
     326
     327                        X=(IssmMpiVec<doubletype>*)Xin;
     328
     329                        /*y=x+a*y where this->vector is y*/
     330                        for(i=0;i<this->m;i++)this->vector[i]=X->vector[i]+a*this->vector[i];
     331
    313332                }
    314333                /*}}}*/
    315334                /*FUNCTION ToMPISerial{{{*/
    316335                doubletype* ToMPISerial(void){
    317                        
    318                         printf("IssmMpiVec ToMPISerial not implemented yet!");
    319                         exit(1);
     336
     337                        /*communicator info: */
     338                        MPI_Comm comm;
     339                        int num_procs;
     340
     341                        /*MPI_Allgatherv info: */
     342                        int  lower_row,upper_row;
     343                        int* recvcounts=NULL;
     344                        int* displs=NULL;
     345                       
     346                        /*output: */
     347                        doubletype* buffer=NULL;
     348
     349                        /*initialize comm info: */
     350                        comm=IssmComm::GetComm();
     351                        num_procs=IssmComm::GetSize();
     352
     353                        /*Allocate: */
     354                        buffer=xNew<doubletype>(M);
     355                        recvcounts=xNew<int>(num_procs);
     356                        displs=xNew<int>(num_procs);
     357
     358                        /*recvcounts:*/
     359                        MPI_Allgather(&this->m,1,MPI_INT,recvcounts,1,MPI_INT,comm);
     360                       
     361                        /*get lower_row: */
     362                        GetOwnershipBoundariesFromRange(&lower_row,&upper_row,this->m,comm);
     363
     364                        /*displs: */
     365                        MPI_Allgather(&lower_row,1,MPI_INT,displs,1,MPI_INT,comm);
     366
     367                        /*All gather:*/
     368                        MPI_Allgatherv(this->vector, this->m, MPI_DOUBLE, buffer, recvcounts, displs, MPI_DOUBLE,comm);
     369
     370                        /*free ressources: */
     371                        xDelete<int>(recvcounts);
     372                        xDelete<int>(displs);
     373
     374                        /*return: */
     375                        return buffer;
    320376
    321377                }
     
    338394                /*FUNCTION Norm{{{*/
    339395                doubletype Norm(NormMode mode){
    340 
    341                         printf("IssmMpiVec Norm not implemented yet!");
    342                         exit(1);
     396               
     397                        doubletype local_norm;
     398                        doubletype norm;
     399                        int i;
     400
     401                        switch(mode){
     402                                case NORM_INF:
     403                                        //local_norm=0; for(i=0;i<this->m;i++)local_norm=max(local_norm,fabs(this->vector[i]));
     404                                        local_norm=0; for(i=0;i<this->m;i++)local_norm=max(local_norm,this->vector[i]);
     405                                        MPI_Reduce(&local_norm, &norm, 1, MPI_DOUBLE, MPI_MAX, 0, IssmComm::GetComm());
     406                                        return norm;
     407                                        break;
     408                                case NORM_TWO:
     409                                        local_norm=0;
     410                                        for(i=0;i<this->m;i++)local_norm+=pow(this->vector[i],2);
     411                                        MPI_Reduce(&local_norm, &norm, 1, MPI_DOUBLE, MPI_SUM, 0, IssmComm::GetComm());
     412                                        return sqrt(norm);
     413                                        break;
     414                                default:
     415                                        _error_("unknown norm !");
     416                                        break;
     417                        }
    343418                }
    344419                /*}}}*/
     
    392467                /*}}}*/
    393468};
    394 #endif //#ifndef _ISSM_MPI_VEC_H_
     469#endif //#ifndef _ISSM_MPI_VEC_H_       
  • issm/trunk-jpl/src/c/toolkits/issm/issmtoolkit.h

    r14685 r14792  
    1818#include "./IssmSeqVec.h"
    1919#include "./IssmVec.h"
     20#include "./IssmSolver.h"
    2021
    2122#ifdef _HAVE_MPI_
  • issm/trunk-jpl/src/c/toolkits/petsc/objects/petscobjects.h

    r12850 r14792  
    88#include "./PetscMat.h"
    99#include "./PetscVec.h"
     10#include "./PetscSolver.h"
    1011
    1112#endif
Note: See TracChangeset for help on using the changeset viewer.