Date: 2024-12-02T08:57:46+00:00

Actual source code: svdscalap.c
slepc-3.22.2 2024-12-02
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain

      6:    This file is part of SLEPc.
      7:    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
      8:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      9: */
     10: /*
     11:    This file implements a wrapper to the ScaLAPACK SVD solver
     12: */

     14: #include <slepc/private/svdimpl.h>
     15: #include <slepc/private/slepcscalapack.h>

     17: typedef struct {
     18:   Mat As;        /* converted matrix */
     19: } SVD_ScaLAPACK;

     21: static PetscErrorCode SVDSetUp_ScaLAPACK(SVD svd)
     22: {
     23:   SVD_ScaLAPACK  *ctx = (SVD_ScaLAPACK*)svd->data;
     24:   PetscInt       M,N;

     26:   PetscFunctionBegin;
     27:   SVDCheckStandard(svd);
     28:   SVDCheckDefinite(svd);
     29:   PetscCall(MatGetSize(svd->A,&M,&N));
     30:   svd->ncv = N;
     31:   if (svd->mpd!=PETSC_DETERMINE) PetscCall(PetscInfo(svd,"Warning: parameter mpd ignored\n"));
     32:   if (svd->max_it==PETSC_DETERMINE) svd->max_it = 1;
     33:   svd->leftbasis = PETSC_TRUE;
     34:   SVDCheckUnsupported(svd,SVD_FEATURE_STOPPING);
     35:   PetscCall(SVDAllocateSolution(svd,0));

     37:   /* convert matrix */
     38:   PetscCall(MatDestroy(&ctx->As));
     39:   PetscCall(MatConvert(svd->OP,MATSCALAPACK,MAT_INITIAL_MATRIX,&ctx->As));
     40:   PetscFunctionReturn(PETSC_SUCCESS);
     41: }

     43: static PetscErrorCode SVDSolve_ScaLAPACK(SVD svd)
     44: {
     45:   SVD_ScaLAPACK  *ctx = (SVD_ScaLAPACK*)svd->data;
     46:   Mat            A = ctx->As,Z,Q,QT,U,V;
     47:   Mat_ScaLAPACK  *a = (Mat_ScaLAPACK*)A->data,*q,*z;
     48:   PetscScalar    *work,minlwork;
     49:   PetscBLASInt   info,lwork=-1,one=1;
     50:   PetscInt       M,N,m,n,mn;
     51: #if defined(PETSC_USE_COMPLEX)
     52:   PetscBLASInt   lrwork;
     53:   PetscReal      *rwork,dummy;
     54: #endif

     56:   PetscFunctionBegin;
     57:   PetscCall(MatGetSize(A,&M,&N));
     58:   PetscCall(MatGetLocalSize(A,&m,&n));
     59:   mn = (M>=N)? n: m;
     60:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A),&Z));
     61:   PetscCall(MatSetSizes(Z,m,mn,PETSC_DECIDE,PETSC_DECIDE));
     62:   PetscCall(MatSetType(Z,MATSCALAPACK));
     63:   PetscCall(MatAssemblyBegin(Z,MAT_FINAL_ASSEMBLY));
     64:   PetscCall(MatAssemblyEnd(Z,MAT_FINAL_ASSEMBLY));
     65:   z = (Mat_ScaLAPACK*)Z->data;
     66:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A),&QT));
     67:   PetscCall(MatSetSizes(QT,mn,n,PETSC_DECIDE,PETSC_DECIDE));
     68:   PetscCall(MatSetType(QT,MATSCALAPACK));
     69:   PetscCall(MatAssemblyBegin(QT,MAT_FINAL_ASSEMBLY));
     70:   PetscCall(MatAssemblyEnd(QT,MAT_FINAL_ASSEMBLY));
     71:   q = (Mat_ScaLAPACK*)QT->data;

     73:   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
     74: #if !defined(PETSC_USE_COMPLEX)
     75:   /* allocate workspace */
     76:   PetscCallBLAS("SCALAPACKgesvd",SCALAPACKgesvd_("V","V",&a->M,&a->N,a->loc,&one,&one,a->desc,svd->sigma,z->loc,&one,&one,z->desc,q->loc,&one,&one,q->desc,&minlwork,&lwork,&info));
     77:   PetscCheckScaLapackInfo("gesvd",info);
     78:   PetscCall(PetscBLASIntCast((PetscInt)minlwork,&lwork));
     79:   PetscCall(PetscMalloc1(lwork,&work));
     80:   /* call computational routine */
     81:   PetscCallBLAS("SCALAPACKgesvd",SCALAPACKgesvd_("V","V",&a->M,&a->N,a->loc,&one,&one,a->desc,svd->sigma,z->loc,&one,&one,z->desc,q->loc,&one,&one,q->desc,work,&lwork,&info));
     82:   PetscCheckScaLapackInfo("gesvd",info);
     83:   PetscCall(PetscFree(work));
     84: #else
     85:   /* allocate workspace */
     86:   PetscCallBLAS("SCALAPACKgesvd",SCALAPACKgesvd_("V","V",&a->M,&a->N,a->loc,&one,&one,a->desc,svd->sigma,z->loc,&one,&one,z->desc,q->loc,&one,&one,q->desc,&minlwork,&lwork,&dummy,&info));
     87:   PetscCheckScaLapackInfo("gesvd",info);
     88:   PetscCall(PetscBLASIntCast((PetscInt)PetscRealPart(minlwork),&lwork));
     89:   lrwork = 1+4*PetscMax(a->M,a->N);
     90:   PetscCall(PetscMalloc2(lwork,&work,lrwork,&rwork));
     91:   /* call computational routine */
     92:   PetscCallBLAS("SCALAPACKgesvd",SCALAPACKgesvd_("V","V",&a->M,&a->N,a->loc,&one,&one,a->desc,svd->sigma,z->loc,&one,&one,z->desc,q->loc,&one,&one,q->desc,work,&lwork,rwork,&info));
     93:   PetscCheckScaLapackInfo("gesvd",info);
     94:   PetscCall(PetscFree2(work,rwork));
     95: #endif
     96:   PetscCall(PetscFPTrapPop());

     98:   PetscCall(MatHermitianTranspose(QT,MAT_INITIAL_MATRIX,&Q));
     99:   PetscCall(MatDestroy(&QT));
    100:   PetscCall(BVGetMat(svd->U,&U));
    101:   PetscCall(BVGetMat(svd->V,&V));
    102:   if (M>=N) {
    103:     PetscCall(MatConvert(Z,MATDENSE,MAT_REUSE_MATRIX,&U));
    104:     PetscCall(MatConvert(Q,MATDENSE,MAT_REUSE_MATRIX,&V));
    105:   } else {
    106:     PetscCall(MatConvert(Q,MATDENSE,MAT_REUSE_MATRIX,&U));
    107:     PetscCall(MatConvert(Z,MATDENSE,MAT_REUSE_MATRIX,&V));
    108:   }
    109:   PetscCall(BVRestoreMat(svd->U,&U));
    110:   PetscCall(BVRestoreMat(svd->V,&V));
    111:   PetscCall(MatDestroy(&Z));
    112:   PetscCall(MatDestroy(&Q));

    114:   svd->nconv  = svd->ncv;
    115:   svd->its    = 1;
    116:   svd->reason = SVD_CONVERGED_TOL;
    117:   PetscFunctionReturn(PETSC_SUCCESS);
    118: }

    120: static PetscErrorCode SVDDestroy_ScaLAPACK(SVD svd)
    121: {
    122:   PetscFunctionBegin;
    123:   PetscCall(PetscFree(svd->data));
    124:   PetscFunctionReturn(PETSC_SUCCESS);
    125: }

    127: static PetscErrorCode SVDReset_ScaLAPACK(SVD svd)
    128: {
    129:   SVD_ScaLAPACK  *ctx = (SVD_ScaLAPACK*)svd->data;

    131:   PetscFunctionBegin;
    132:   PetscCall(MatDestroy(&ctx->As));
    133:   PetscFunctionReturn(PETSC_SUCCESS);
    134: }

    136: SLEPC_EXTERN PetscErrorCode SVDCreate_ScaLAPACK(SVD svd)
    137: {
    138:   SVD_ScaLAPACK  *ctx;

    140:   PetscFunctionBegin;
    141:   PetscCall(PetscNew(&ctx));
    142:   svd->data = (void*)ctx;

    144:   svd->ops->solve          = SVDSolve_ScaLAPACK;
    145:   svd->ops->setup          = SVDSetUp_ScaLAPACK;
    146:   svd->ops->destroy        = SVDDestroy_ScaLAPACK;
    147:   svd->ops->reset          = SVDReset_ScaLAPACK;
    148:   PetscFunctionReturn(PETSC_SUCCESS);
    149: }