Date: 2024-04-26T10:22:41+00:00

Actual source code: svdelemen.cxx
slepc-3.21.1 2024-04-26
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 Elemental SVD solver
     12: */

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

     17: typedef struct {
     18:   Mat Ae;        /* converted matrix */
     19: } SVD_Elemental;

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

     26:   PetscFunctionBegin;
     27:   SVDCheckStandard(svd);
     28:   SVDCheckDefinite(svd);
     29:   PetscCall(MatGetSize(svd->A,&M,&N));
     30:   PetscCheck(M==N,PetscObjectComm((PetscObject)svd),PETSC_ERR_SUP,"Not implemented for rectangular matrices");
     31:   svd->ncv = N;
     32:   if (svd->mpd!=PETSC_DEFAULT) PetscCall(PetscInfo(svd,"Warning: parameter mpd ignored\n"));
     33:   if (svd->max_it==PETSC_DEFAULT) svd->max_it = 1;
     34:   svd->leftbasis = PETSC_TRUE;
     35:   SVDCheckUnsupported(svd,SVD_FEATURE_STOPPING);
     36:   PetscCall(SVDAllocateSolution(svd,0));

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

     44: static PetscErrorCode SVDSolve_Elemental(SVD svd)
     45: {
     46:   SVD_Elemental  *ctx = (SVD_Elemental*)svd->data;
     47:   Mat            A = ctx->Ae,Z,Q,U,V;
     48:   Mat_Elemental  *a = (Mat_Elemental*)A->data,*q,*z;
     49:   PetscInt       i,rrank,ridx,erow;

     51:   PetscFunctionBegin;
     52:   El::DistMatrix<PetscReal,El::STAR,El::VC> sigma(*a->grid);
     53:   PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&Z));
     54:   PetscCall(MatDuplicate(A,MAT_DO_NOT_COPY_VALUES,&Q));
     55:   z = (Mat_Elemental*)Z->data;
     56:   q = (Mat_Elemental*)Q->data;

     58:   El::SVD(*a->emat,*z->emat,sigma,*q->emat);

     60:   for (i=0;i<svd->ncv;i++) {
     61:     P2RO(A,1,i,&rrank,&ridx);
     62:     RO2E(A,1,rrank,ridx,&erow);
     63:     svd->sigma[i] = sigma.Get(erow,0);
     64:   }
     65:   PetscCall(BVGetMat(svd->U,&U));
     66:   PetscCall(MatConvert(Z,MATDENSE,MAT_REUSE_MATRIX,&U));
     67:   PetscCall(BVRestoreMat(svd->U,&U));
     68:   PetscCall(MatDestroy(&Z));
     69:   PetscCall(BVGetMat(svd->V,&V));
     70:   PetscCall(MatConvert(Q,MATDENSE,MAT_REUSE_MATRIX,&V));
     71:   PetscCall(BVRestoreMat(svd->V,&V));
     72:   PetscCall(MatDestroy(&Q));

     74:   svd->nconv  = svd->ncv;
     75:   svd->its    = 1;
     76:   svd->reason = SVD_CONVERGED_TOL;
     77:   PetscFunctionReturn(PETSC_SUCCESS);
     78: }

     80: static PetscErrorCode SVDDestroy_Elemental(SVD svd)
     81: {
     82:   PetscFunctionBegin;
     83:   PetscCall(PetscFree(svd->data));
     84:   PetscFunctionReturn(PETSC_SUCCESS);
     85: }

     87: static PetscErrorCode SVDReset_Elemental(SVD svd)
     88: {
     89:   SVD_Elemental  *ctx = (SVD_Elemental*)svd->data;

     91:   PetscFunctionBegin;
     92:   PetscCall(MatDestroy(&ctx->Ae));
     93:   PetscFunctionReturn(PETSC_SUCCESS);
     94: }

     96: SLEPC_EXTERN PetscErrorCode SVDCreate_Elemental(SVD svd)
     97: {
     98:   SVD_Elemental  *ctx;

    100:   PetscFunctionBegin;
    101:   PetscCall(PetscNew(&ctx));
    102:   svd->data = (void*)ctx;

    104:   svd->ops->solve          = SVDSolve_Elemental;
    105:   svd->ops->setup          = SVDSetUp_Elemental;
    106:   svd->ops->destroy        = SVDDestroy_Elemental;
    107:   svd->ops->reset          = SVDReset_Elemental;
    108:   PetscFunctionReturn(PETSC_SUCCESS);
    109: }