LCOV - code coverage report
Current view: top level - sys/classes/bv/tests - test4.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 96 109 88.1 %
Date: 2024-05-02 01:08:00 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       3             :    SLEPc - Scalable Library for Eigenvalue Problem Computations
       4             :    Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
       5             : 
       6             :    This file is part of SLEPc.
       7             :    SLEPc is distributed under a 2-clause BSD license (see LICENSE).
       8             :    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       9             : */
      10             : 
      11             : static char help[] = "Test BV operations, changing the number of active columns.\n\n";
      12             : 
      13             : #include <slepcbv.h>
      14             : 
      15           9 : int main(int argc,char **argv)
      16             : {
      17           9 :   Vec            t,v;
      18           9 :   Mat            Q,M;
      19           9 :   BV             X,Y;
      20           9 :   PetscInt       i,j,n=10,kx=6,lx=3,ky=5,ly=2;
      21           9 :   PetscScalar    *q,*z;
      22           9 :   PetscReal      nrm;
      23           9 :   PetscViewer    view;
      24           9 :   PetscBool      verbose,trans;
      25             : 
      26           9 :   PetscFunctionBeginUser;
      27           9 :   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
      28           9 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      29           9 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-kx",&kx,NULL));
      30           9 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-lx",&lx,NULL));
      31           9 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-ky",&ky,NULL));
      32           9 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-ly",&ly,NULL));
      33           9 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
      34           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"First BV with %" PetscInt_FMT " active columns (%" PetscInt_FMT " leading columns) of dimension %" PetscInt_FMT ".\n",kx,lx,n));
      35           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Second BV with %" PetscInt_FMT " active columns (%" PetscInt_FMT " leading columns) of dimension %" PetscInt_FMT ".\n",ky,ly,n));
      36             : 
      37             :   /* Create template vector */
      38           9 :   PetscCall(VecCreate(PETSC_COMM_WORLD,&t));
      39           9 :   PetscCall(VecSetSizes(t,PETSC_DECIDE,n));
      40           9 :   PetscCall(VecSetFromOptions(t));
      41             : 
      42             :   /* Create BV object X */
      43           9 :   PetscCall(BVCreate(PETSC_COMM_WORLD,&X));
      44           9 :   PetscCall(PetscObjectSetName((PetscObject)X,"X"));
      45           9 :   PetscCall(BVSetSizesFromVec(X,t,kx+2));  /* two extra columns to test active columns */
      46           9 :   PetscCall(BVSetFromOptions(X));
      47           9 :   PetscCall(BVSetActiveColumns(X,lx,kx));
      48             : 
      49             :   /* Set up viewer */
      50           9 :   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&view));
      51           9 :   if (verbose) PetscCall(PetscViewerPushFormat(view,PETSC_VIEWER_ASCII_MATLAB));
      52             : 
      53             :   /* Fill X entries */
      54         135 :   for (j=0;j<kx+2;j++) {
      55         126 :     PetscCall(BVGetColumn(X,j,&v));
      56         126 :     PetscCall(VecSet(v,0.0));
      57         630 :     for (i=0;i<4;i++) {
      58         504 :       if (i+j<n) PetscCall(VecSetValue(v,i+j,(PetscScalar)(3*i+j-2),INSERT_VALUES));
      59             :     }
      60         126 :     PetscCall(VecAssemblyBegin(v));
      61         126 :     PetscCall(VecAssemblyEnd(v));
      62         126 :     PetscCall(BVRestoreColumn(X,j,&v));
      63             :   }
      64           9 :   if (verbose) PetscCall(BVView(X,view));
      65             : 
      66             :   /* Create BV object Y */
      67           9 :   PetscCall(BVCreate(PETSC_COMM_WORLD,&Y));
      68           9 :   PetscCall(PetscObjectSetName((PetscObject)Y,"Y"));
      69           9 :   PetscCall(BVSetSizesFromVec(Y,t,ky+1));
      70           9 :   PetscCall(BVSetFromOptions(Y));
      71           9 :   PetscCall(BVSetActiveColumns(Y,ly,ky));
      72             : 
      73             :   /* Fill Y entries */
      74          90 :   for (j=0;j<ky+1;j++) {
      75          81 :     PetscCall(BVGetColumn(Y,j,&v));
      76          81 :     PetscCall(VecSet(v,(PetscScalar)(j+1)/4.0));
      77          81 :     PetscCall(BVRestoreColumn(Y,j,&v));
      78             :   }
      79           9 :   if (verbose) PetscCall(BVView(Y,view));
      80             : 
      81             :   /* Create Mat */
      82           9 :   PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,kx,ky,NULL,&Q));
      83           9 :   PetscCall(PetscObjectSetName((PetscObject)Q,"Q"));
      84           9 :   PetscCall(MatDenseGetArray(Q,&q));
      85         117 :   for (i=0;i<kx;i++)
      86         972 :     for (j=0;j<ky;j++)
      87        1476 :       q[i+j*kx] = (i<j)? 2.0: -0.5;
      88           9 :   PetscCall(MatDenseRestoreArray(Q,&q));
      89           9 :   if (verbose) PetscCall(MatView(Q,NULL));
      90             : 
      91             :   /* Test BVResize */
      92           9 :   PetscCall(BVResize(X,kx+4,PETSC_TRUE));
      93             : 
      94             :   /* Test BVMult */
      95           9 :   PetscCall(BVMult(Y,2.0,0.5,X,Q));
      96           9 :   if (verbose) {
      97           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMult - - - - - - - - -\n"));
      98           0 :     PetscCall(BVView(Y,view));
      99             :   }
     100             : 
     101             :   /* Test BVMultVec */
     102           9 :   PetscCall(BVGetColumn(Y,0,&v));
     103           9 :   PetscCall(PetscMalloc1(kx-lx,&z));
     104           9 :   z[0] = 2.0;
     105          81 :   for (i=1;i<kx-lx;i++) z[i] = -0.5*z[i-1];
     106           9 :   PetscCall(BVMultVec(X,-1.0,1.0,v,z));
     107           9 :   PetscCall(PetscFree(z));
     108           9 :   PetscCall(BVRestoreColumn(Y,0,&v));
     109           9 :   if (verbose) {
     110           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMultVec - - - - - - -\n"));
     111           0 :     PetscCall(BVView(Y,view));
     112             :   }
     113             : 
     114             :   /* Test BVDot */
     115           9 :   PetscCall(MatCreateSeqDense(PETSC_COMM_SELF,ky,kx,NULL,&M));
     116           9 :   PetscCall(PetscObjectSetName((PetscObject)M,"M"));
     117           9 :   PetscCall(BVDot(X,Y,M));
     118           9 :   if (verbose) {
     119           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVDot - - - - - - - - -\n"));
     120           0 :     PetscCall(MatView(M,NULL));
     121             :   }
     122             : 
     123             :   /* Test BVDotVec */
     124           9 :   PetscCall(BVGetColumn(Y,0,&v));
     125           9 :   PetscCall(PetscMalloc1(kx-lx,&z));
     126           9 :   PetscCall(BVDotVec(X,v,z));
     127           9 :   PetscCall(BVRestoreColumn(Y,0,&v));
     128           9 :   if (verbose) {
     129           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVDotVec - - - - - - -\n"));
     130           0 :     PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF,1,kx-lx,z,&v));
     131           0 :     PetscCall(PetscObjectSetName((PetscObject)v,"z"));
     132           0 :     PetscCall(VecView(v,view));
     133           0 :     PetscCall(VecDestroy(&v));
     134             :   }
     135           9 :   PetscCall(PetscFree(z));
     136             : 
     137             :   /* Test BVMultInPlace and BVScale */
     138           9 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-trans",&trans));
     139           9 :   if (trans) {
     140           4 :     Mat Qt;
     141           4 :     PetscCall(MatTranspose(Q,MAT_INITIAL_MATRIX,&Qt));
     142           4 :     PetscCall(BVMultInPlaceHermitianTranspose(X,Qt,lx+1,ky));
     143           4 :     PetscCall(MatDestroy(&Qt));
     144           5 :   } else PetscCall(BVMultInPlace(X,Q,lx+1,ky));
     145           9 :   PetscCall(BVScale(X,2.0));
     146           9 :   if (verbose) {
     147           0 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMultInPlace - - - - -\n"));
     148           0 :     PetscCall(BVView(X,view));
     149             :   }
     150             : 
     151             :   /* Test BVNorm */
     152           9 :   PetscCall(BVNormColumn(X,lx,NORM_2,&nrm));
     153           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"2-Norm of X[%" PetscInt_FMT "] = %g\n",lx,(double)nrm));
     154           9 :   PetscCall(BVNorm(X,NORM_FROBENIUS,&nrm));
     155           9 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Frobenius Norm of X = %g\n",(double)nrm));
     156             : 
     157           9 :   PetscCall(BVDestroy(&X));
     158           9 :   PetscCall(BVDestroy(&Y));
     159           9 :   PetscCall(MatDestroy(&Q));
     160           9 :   PetscCall(MatDestroy(&M));
     161           9 :   PetscCall(VecDestroy(&t));
     162           9 :   PetscCall(SlepcFinalize());
     163             :   return 0;
     164             : }
     165             : 
     166             : /*TEST
     167             : 
     168             :    testset:
     169             :       output_file: output/test4_1.out
     170             :       args: -n 18 -kx 12 -ky 8
     171             :       test:
     172             :          suffix: 1
     173             :          args: -bv_type {{vecs contiguous svec mat}shared output}
     174             :       test:
     175             :          suffix: 1_vecs_vmip
     176             :          args: -bv_type vecs -bv_vecs_vmip 1
     177             :       test:
     178             :          suffix: 1_cuda
     179             :          args: -bv_type {{svec mat}} -vec_type cuda
     180             :          requires: cuda
     181             :       test:
     182             :          suffix: 2
     183             :          args: -bv_type {{vecs contiguous svec mat}shared output} -trans
     184             : 
     185             : TEST*/

Generated by: LCOV version 1.14