Ignore:
Timestamp:
09/16/13 09:43:55 (12 years ago)
Author:
Mathieu Morlighem
Message:

merged trunk-jpl and trunk for revision 16135

Location:
issm/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk

  • issm/trunk/src

  • issm/trunk/src/c

    • Property svn:ignore
      •  

        old new  
        1414probe.results
        1515stXXXX*
        16 
         16.deps
         17.dirstamp
  • issm/trunk/src/c/toolkits

    • Property svn:ignore set to
      .deps
      .dirstamp
  • issm/trunk/src/c/toolkits/mumps

    • Property svn:ignore set to
      .deps
      .dirstamp
  • issm/trunk/src/c/toolkits/mumps/MpiDenseMumpsSolve.cpp

    r14950 r16137  
    1313#include "../../shared/MemOps/MemOps.h"
    1414#include "../../shared/Exceptions/exceptions.h"
    15 #include "../../shared/io/Comm/Comm.h"
    16 #include "../mpi/patches/mpipatches.h"
     15#include "../../shared/io/Comm/IssmComm.h"
     16#include "../../classes/Params/GenericParam.h"
     17#include "../../classes/Params/Parameters.h"
     18#include "../mpi/issmmpi.h"
     19#include "../adolc/adolcincludes.h"
     20#include "./mumpsincludes.h"
    1721
    1822/*Mumps header files: */
    1923#include "dmumps_c.h"
    20 
    21 #define JOB_INIT -1
    22 #define JOB_SOLVE 6
    23 #define JOB_END  -2
    2424/*}}}*/
    2525
    26 void MpiDenseMumpsSolve( /*output: */ IssmPDouble* uf, int uf_M, int uf_m, /*matrix input: */ IssmPDouble* Kff, int Kff_M, int Kff_N, int Kff_m, /*right hand side vector: */ IssmPDouble* pf, int pf_M, int pf_m){ /*{{{*/
     26void MumpsInit(DMUMPS_STRUC_C &theMumpsStruc) {
     27        theMumpsStruc.par          = 1; 
     28        theMumpsStruc.sym          = 0;
     29        theMumpsStruc.comm_fortran = MPI_Comm_c2f(IssmComm::GetComm());
     30        theMumpsStruc.job          = -1;
     31        dmumps_c(&theMumpsStruc);
     32}
     33
     34// must be preceded by a call to MumpsInit
     35void MumpsSettings(DMUMPS_STRUC_C &theMumpsStruc) {
     36        /*Control statements:{{{ */
     37        theMumpsStruc.icntl[1-1] = 6; //error verbose
     38        theMumpsStruc.icntl[2-1] = 1; //std verbose
     39        theMumpsStruc.icntl[4-1] = 4; //verbose everything
     40        theMumpsStruc.icntl[5-1] = 0;
     41        theMumpsStruc.icntl[18-1] = 3;
     42
     43        theMumpsStruc.icntl[20-1] = 0;
     44        theMumpsStruc.icntl[21-1] = 0;
     45        theMumpsStruc.icntl[30-1] = 0;
     46        /*}}}*/
     47}
     48
     49// must be preceded by a call to MumpsInit
     50void MumpsAnalyze(DMUMPS_STRUC_C &theMumpsStruc) {
     51        theMumpsStruc.job          = 1;
     52        dmumps_c(&theMumpsStruc);
     53}
     54
     55// must be preceded by a call to MumpsAnalyze
     56void MumpsFactorize(DMUMPS_STRUC_C &theMumpsStruc) {
     57        theMumpsStruc.job          = 2;
     58        dmumps_c(&theMumpsStruc);
     59}
     60
     61// must be preceded by a call to MumpsFactorize
     62void MumpsBacksubstitute(DMUMPS_STRUC_C &theMumpsStruc) {
     63        theMumpsStruc.job          = 3;
     64        dmumps_c(&theMumpsStruc);
     65}
     66
     67// must be preceded at least  by a call to MumpsInit
     68void MumpsFinalize(DMUMPS_STRUC_C &theMumpsStruc) {
     69        theMumpsStruc.job          = -2;
     70        dmumps_c(&theMumpsStruc);
     71}
     72
     73void MumpsSolve(int n,
     74                int nnz,
     75                int local_nnz,
     76                int* irn_loc,
     77                int* jcn_loc,
     78                IssmPDouble *a_loc,
     79                IssmPDouble *rhs,
     80                Parameters* parameters=0 /*unused here*/) {
     81        /*Initialize mumps: {{{*/
     82        DMUMPS_STRUC_C theMumpsStruc;
     83        MumpsInit(theMumpsStruc);
     84        MumpsSettings(theMumpsStruc);
     85        /*}}}*/
     86        // now setup the rest of theMumpsStruc
     87        theMumpsStruc.n=n;
     88        theMumpsStruc.nz=nnz;
     89        theMumpsStruc.nz_loc=local_nnz;
     90        theMumpsStruc.irn_loc=irn_loc;
     91        theMumpsStruc.jcn_loc=jcn_loc;
     92        theMumpsStruc.a_loc=a_loc;
     93        theMumpsStruc.rhs=rhs;
     94        theMumpsStruc.nrhs=1;
     95        theMumpsStruc.lrhs=1;
     96        /*Solve system: {{{*/
     97        MumpsAnalyze(theMumpsStruc);
     98        MumpsFactorize(theMumpsStruc);
     99        MumpsBacksubstitute(theMumpsStruc);
     100        /*}}}*/
     101        MumpsFinalize(theMumpsStruc);
     102}
     103
     104#ifdef _HAVE_ADOLC_
     105// prototype for active variant
     106void MumpsSolve(int n,
     107                int nnz,
     108                int local_nnz,
     109                int* irn_loc,
     110                int* jcn_loc,
     111                IssmDouble *a_loc,
     112                IssmDouble *rhs,
     113                Parameters* parameters);
     114#endif
     115
     116void MpiDenseMumpsSolve( /*output: */ IssmDouble* uf, int uf_M, int uf_m, /*matrix input: */ IssmDouble* Kff, int Kff_M, int Kff_N, int Kff_m, /*right hand side vector: */ IssmDouble* pf, int pf_M, int pf_m, Parameters* parameters){ /*{{{*/
    27117
    28118        /*Variables: {{{*/
    29119
    30         MPI_Comm   comm;
     120        ISSM_MPI_Comm   comm;
    31121        int        my_rank;
    32122        int        num_procs;
     
    36126        int        *irn_loc = NULL;
    37127        int        *jcn_loc = NULL;
    38         IssmPDouble *a_loc   = NULL;
     128        IssmDouble *a_loc   = NULL;
    39129        int         count;
    40130        int         lower_row;
    41131        int         upper_row;
    42         IssmPDouble* rhs=NULL;
     132        IssmDouble* rhs=NULL;
    43133        int*        recvcounts=NULL;
    44134        int*        displs=NULL;
     
    54144        if (uf_m!=Kff_m | uf_m!=pf_m)_error_("solution vector should be locally the same size as stiffness matrix Kff and load vector pf");
    55145        /*}}}*/
    56         /*Initialize mumps: {{{*/
    57         DMUMPS_STRUC_C id;
    58         id.job          = JOB_INIT;
    59         id.par          = 1; 
    60         id.sym          = 0;
    61         id.comm_fortran = MPI_Comm_c2f(comm);
    62         dmumps_c(&id);
    63         /*}}}*/
    64         /*Control statements:{{{ */
    65         id.icntl[1-1] = 6; //error verbose
    66         id.icntl[2-1] = 1; //std verbose
    67         id.icntl[4-1] = 4; //verbose everything
    68         id.icntl[5-1] = 0;
    69         id.icntl[18-1] = 3;
    70 
    71         id.icntl[20-1] = 0;
    72         id.icntl[21-1] = 0;
    73         id.icntl[30-1] = 0;
    74         /*}}}*/
    75146        /*Initialize matrix:{{{ */
    76         id.n=Kff_M;
    77147
    78148        /*figure out number of non-zero entries: */
     
    84154        }
    85155
    86         MPI_Reduce(&local_nnz,&nnz,1,MPI_INT,MPI_SUM,0,comm);
    87         MPI_Bcast(&nnz,1,MPI_INT,0,comm);
    88         id.nz=nnz;
    89         id.nz_loc=local_nnz;
     156        ISSM_MPI_Reduce(&local_nnz,&nnz,1,ISSM_MPI_INT,ISSM_MPI_SUM,0,comm);
     157        ISSM_MPI_Bcast(&nnz,1,ISSM_MPI_INT,0,comm);
    90158
    91159        /*Allocate: */
     
    93161                irn_loc=xNew<int>(local_nnz);
    94162                jcn_loc=xNew<int>(local_nnz);
    95                 a_loc=xNew<IssmPDouble>(local_nnz);
     163                a_loc=xNew<IssmDouble>(local_nnz);
    96164        }
    97165
     
    109177                }
    110178        }
    111         id.irn_loc=irn_loc;
    112         id.jcn_loc=jcn_loc;
    113         id.a_loc=a_loc;
    114 
    115         /*Deal with right hand side. We need to MPI_Gather it onto cpu 0: */
    116         rhs=xNew<IssmPDouble>(pf_M);
     179        /*Deal with right hand side. We need to ISSM_MPI_Gather it onto cpu 0: */
     180        rhs=xNew<IssmDouble>(pf_M);
    117181
    118182        recvcounts=xNew<int>(num_procs);
     
    120184
    121185        /*recvcounts:*/
    122         MPI_Allgather(&pf_m,1,MPI_INT,recvcounts,1,MPI_INT,comm);
     186        ISSM_MPI_Allgather(&pf_m,1,ISSM_MPI_INT,recvcounts,1,ISSM_MPI_INT,comm);
    123187
    124188        /*displs: */
    125         MPI_Allgather(&lower_row,1,MPI_INT,displs,1,MPI_INT,comm);
     189        ISSM_MPI_Allgather(&lower_row,1,ISSM_MPI_INT,displs,1,ISSM_MPI_INT,comm);
    126190
    127191        /*Gather:*/
    128         MPI_Gatherv(pf, pf_m, MPI_DOUBLE, rhs, recvcounts, displs, MPI_DOUBLE,0,comm);
    129         id.rhs=rhs;
    130         id.nrhs=1;
    131         id.lrhs=1;
    132 
    133         /*}}}*/
    134         /*Solve system: {{{*/
    135         id.job = JOB_SOLVE;
    136         dmumps_c (&id);
    137         /*}}}*/
     192        ISSM_MPI_Gatherv(pf, pf_m, ISSM_MPI_DOUBLE, rhs, recvcounts, displs, ISSM_MPI_DOUBLE,0,comm);
     193
     194        MumpsSolve(Kff_M,
     195                   nnz,
     196                   local_nnz,
     197                   irn_loc,
     198                   jcn_loc,
     199                   a_loc,
     200                   rhs,
     201                   parameters);
     202
    138203        /*Now scatter from cpu 0 to all other cpus: {{{*/
    139         MPI_Scatterv( rhs, recvcounts, displs, MPI_DOUBLE, uf, uf_m, MPI_DOUBLE, 0, comm);
     204        ISSM_MPI_Scatterv( rhs, recvcounts, displs, ISSM_MPI_DOUBLE, uf, uf_m, ISSM_MPI_DOUBLE, 0, comm);
    140205
    141206        /*}}}*/
    142207        /*Cleanup: {{{*/
    143         id.job = JOB_END;
    144         dmumps_c (&id);
    145 
    146208        xDelete<int>(irn_loc);
    147209        xDelete<int>(jcn_loc);
    148         xDelete<IssmPDouble>(a_loc);
    149         xDelete<IssmPDouble>(rhs);
     210        xDelete<IssmDouble>(a_loc);
     211        xDelete<IssmDouble>(rhs);
    150212        xDelete<int>(recvcounts);
    151213        xDelete<int>(displs);
    152 
    153214        /*}}}*/
    154215} /*}}}*/
    155216
    156217#ifdef _HAVE_ADOLC_
    157 void MpiDenseMumpsSolve( /*output: */ IssmDouble* uf, int uf_M, int uf_m, /*matrix input: */ IssmDouble* Kff, int Kff_M, int Kff_N, int Kff_m, /*right hand side vector: */ IssmDouble* pf, int pf_M, int pf_m){ /*{{{*/
    158         _error_("not supported yet!");
    159 } /*}}}*/
     218
     219int mumpsSolveEDF(int iArrLength, int* iArr, int /* ignored */, IssmPDouble* dp_x, int /* ignored */, IssmPDouble* dp_y) {
     220  // unpack parameters
     221  int n=iArr[0];
     222  int nnz=iArr[1];
     223  int local_nnz=iArr[2];
     224  int *local_irn=xNew<int>(local_nnz);
     225  int *local_jcn=xNew<int>(local_nnz);
     226  IssmPDouble *A=xNew<IssmPDouble>(local_nnz);
     227  for (int i=0;i<local_nnz;++i) {
     228    local_irn[i]=iArr[3+i];
     229    local_jcn[i]=iArr[3+local_nnz+i];
     230    A[i]=dp_x[i];
     231  }
     232  IssmPDouble *rhs_sol=xNew<IssmPDouble>(n);
     233  for (int i=0;i<n;++i) {
     234    rhs_sol[i]=dp_x[local_nnz+i];
     235  }
     236  MumpsSolve(n,nnz,local_nnz,local_irn,local_jcn,A,rhs_sol);
     237  for (int i=0;i<n;++i) {
     238    dp_y[i]=rhs_sol[i];
     239  }
     240  xDelete(rhs_sol);
     241  xDelete(A);
     242  xDelete(local_jcn);
     243  xDelete(local_irn);
     244  return 0;
     245}
     246
     247void MumpsSolve(int n,
     248                int nnz,
     249                int local_nnz,
     250                int* irn_loc,
     251                int* jcn_loc,
     252                IssmDouble *a_loc,
     253                IssmDouble *rhs,
     254                Parameters* parameters) {
     255  int packedDimsSparseArrLength=1+1+1+local_nnz+local_nnz;
     256  int *packedDimsSparseArr=xNew<int>(packedDimsSparseArrLength);
     257  packedDimsSparseArr[0]=n;
     258  packedDimsSparseArr[1]=nnz;
     259  packedDimsSparseArr[2]=local_nnz;
     260  for (int i=0;i<local_nnz;++i) {
     261    packedDimsSparseArr[3+i]=irn_loc[i];
     262    packedDimsSparseArr[3+local_nnz+i]=jcn_loc[i];
     263  }
     264  IssmDouble *pack_A_rhs=xNew<IssmDouble>(local_nnz+n);
     265  for (int i=0;i<local_nnz;++i) {
     266    pack_A_rhs[i]=a_loc[i];
     267  }
     268  for (int i=0;i<n;++i) {
     269    pack_A_rhs[local_nnz+i]=rhs[i];
     270  }
     271  IssmPDouble *passivePack_A_rhs=xNew<IssmPDouble>(local_nnz+n);
     272  IssmPDouble *passiveSol=xNew<IssmPDouble>(n);
     273  IssmDouble *sol=xNew<IssmDouble>(n);
     274  call_ext_fct(dynamic_cast<GenericParam<Adolc_edf> * >(parameters->FindParamObject(AdolcParamEnum))->GetParameterValue().myEDF_for_solverx_p,
     275               packedDimsSparseArrLength, packedDimsSparseArr,
     276               local_nnz+n, passivePack_A_rhs, pack_A_rhs,
     277               n, passiveSol,sol);
     278  for (int i=0;i<n;++i) {
     279    rhs[i]=sol[i];
     280  }
     281  xDelete(sol);
     282  xDelete(passiveSol);
     283  xDelete(passivePack_A_rhs);
     284  xDelete(pack_A_rhs);
     285  xDelete(packedDimsSparseArr);
     286}
     287
     288int fos_reverse_mumpsSolveEDF(int iArrLength, int* iArr,
     289                              int m, IssmPDouble *dp_U,
     290                              int nPlusNz, IssmPDouble *dp_Z,
     291                              IssmPDouble *dp_x, IssmPDouble *dp_y) {
     292  // unpack parameters
     293  int n=iArr[0];
     294  int nnz=iArr[1];
     295  int local_nnz=iArr[2];
     296  int *local_irn=xNew<int>(local_nnz);
     297  int *local_jcn=xNew<int>(local_nnz);
     298  IssmPDouble *a_loc=xNew<IssmPDouble>(local_nnz);
     299  for (int i=0;i<local_nnz;++i) {
     300          local_irn[i]=iArr[3+i];
     301          local_jcn[i]=iArr[3+local_nnz+i];
     302          a_loc[i]=dp_x[i];
     303  }
     304  IssmPDouble *rhs_sol=xNew<IssmPDouble>(n);
     305  for (int i=0;i<n;++i) {
     306    rhs_sol[i]=dp_U[i];
     307  }
     308  DMUMPS_STRUC_C theMumpsStruc;
     309  MumpsInit(theMumpsStruc);
     310  MumpsSettings(theMumpsStruc);
     311  theMumpsStruc.n=n;
     312  theMumpsStruc.nz=nnz;
     313  theMumpsStruc.nz_loc=local_nnz;
     314  theMumpsStruc.irn_loc=local_irn;
     315  theMumpsStruc.jcn_loc=local_jcn;
     316  theMumpsStruc.a_loc=a_loc;
     317  theMumpsStruc.rhs=rhs_sol;
     318  theMumpsStruc.nrhs=1;
     319  theMumpsStruc.lrhs=1;
     320  theMumpsStruc.icntl[9-1] = 0; //solve for the transpose
     321  MumpsAnalyze(theMumpsStruc);
     322  MumpsFactorize(theMumpsStruc);
     323  MumpsBacksubstitute(theMumpsStruc);
     324  MumpsFinalize(theMumpsStruc);
     325  // update the adjoint of the rhs:
     326  for (int i=0;i<n;++i) {
     327    dp_Z[local_nnz+i]+=rhs_sol[i];
     328  }
     329  // bcast dp_y (the solution of the forward system)
     330  ISSM_MPI_Bcast(dp_y,n,ISSM_MPI_PDOUBLE,0,IssmComm::GetComm());
     331  // bcast the adjoint of the right-hand-side, i.e. this solution
     332  ISSM_MPI_Bcast(rhs_sol,n,ISSM_MPI_PDOUBLE,0,IssmComm::GetComm());
     333  // update the adjoint of the matrix with the outer product of right-hand-side adjoint and the original solution
     334  for (int i=0;i<local_nnz;++i) {
     335    dp_Z[i]-=rhs_sol[iArr[3+i]-1]*dp_y[iArr[3+local_nnz+i]-1];
     336  }
     337  xDelete(rhs_sol);
     338  xDelete(a_loc);
     339  xDelete(local_jcn);
     340  xDelete(local_irn);
     341  return 3;
     342}
     343
    160344#endif
Note: See TracChangeset for help on using the changeset viewer.