LCOV - code coverage report
Current view: top level - sys/classes/bv/tests - test1.c (source / functions) Hit Total Coverage
Test: SLEPc Lines: 122 122 100.0 %
Date: 2024-05-02 00:43:15 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.\n\n";
      12             : 
      13             : #include <slepcbv.h>
      14             : 
      15           8 : int main(int argc,char **argv)
      16             : {
      17           8 :   Vec               t,v;
      18           8 :   Mat               Q=NULL,M=NULL;
      19           8 :   BV                X,Y;
      20           8 :   PetscInt          i,j,n=10,k=5,l=3,ldx,lda;
      21           8 :   PetscMPIInt       rank;
      22           8 :   PetscScalar       *q,*z;
      23           8 :   const PetscScalar *pX;
      24           8 :   PetscReal         nrm;
      25           8 :   PetscViewer       view;
      26           8 :   PetscBool         verbose,matcuda,testlda=PETSC_FALSE;
      27             : 
      28           8 :   PetscFunctionBeginUser;
      29           8 :   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
      30           8 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
      31           8 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL));
      32           8 :   PetscCall(PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL));
      33           8 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
      34           8 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-matcuda",&matcuda));
      35           8 :   PetscCall(PetscOptionsHasName(NULL,NULL,"-testlda",&testlda));
      36           8 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Test BV with %" PetscInt_FMT " columns of dimension %" PetscInt_FMT ".\n",k,n));
      37             : 
      38             :   /* Create template vector */
      39           8 :   PetscCall(VecCreate(PETSC_COMM_WORLD,&t));
      40           8 :   PetscCall(VecSetSizes(t,PETSC_DECIDE,n));
      41           8 :   PetscCall(VecSetFromOptions(t));
      42             : 
      43             :   /* Create BV object X */
      44           8 :   PetscCall(BVCreate(PETSC_COMM_WORLD,&X));
      45           8 :   PetscCall(PetscObjectSetName((PetscObject)X,"X"));
      46           8 :   PetscCall(BVSetSizesFromVec(X,t,k));
      47           8 :   PetscCall(BVSetFromOptions(X));
      48             : 
      49             :   /* Set up viewer */
      50           8 :   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&view));
      51           8 :   PetscCall(PetscViewerPushFormat(view,PETSC_VIEWER_ASCII_INFO_DETAIL));
      52           8 :   PetscCall(BVView(X,view));
      53           8 :   PetscCall(PetscViewerPopFormat(view));
      54             : 
      55             :   /* Fill X entries */
      56          48 :   for (j=0;j<k;j++) {
      57          40 :     PetscCall(BVGetColumn(X,j,&v));
      58          40 :     PetscCall(VecSet(v,0.0));
      59         200 :     for (i=0;i<4;i++) {
      60         160 :       if (i+j<n) PetscCall(VecSetValue(v,i+j,(PetscScalar)(3*i+j-2),INSERT_VALUES));
      61             :     }
      62          40 :     PetscCall(VecAssemblyBegin(v));
      63          40 :     PetscCall(VecAssemblyEnd(v));
      64          40 :     PetscCall(BVRestoreColumn(X,j,&v));
      65             :   }
      66           8 :   if (verbose) PetscCall(BVView(X,view));
      67             : 
      68             :   /* Create BV object Y */
      69           8 :   PetscCall(BVCreate(PETSC_COMM_WORLD,&Y));
      70           8 :   PetscCall(PetscObjectSetName((PetscObject)Y,"Y"));
      71           8 :   PetscCall(BVSetSizesFromVec(Y,t,l));
      72           8 :   PetscCall(BVSetFromOptions(Y));
      73             : 
      74             :   /* Fill Y entries */
      75          32 :   for (j=0;j<l;j++) {
      76          24 :     PetscCall(BVGetColumn(Y,j,&v));
      77          24 :     PetscCall(VecSet(v,(PetscScalar)(j+1)/4.0));
      78          24 :     PetscCall(BVRestoreColumn(Y,j,&v));
      79             :   }
      80           8 :   if (verbose) PetscCall(BVView(Y,view));
      81             : 
      82             :   /* Create Mat */
      83           8 :   PetscCall(MatCreate(PETSC_COMM_SELF,&Q));
      84           8 :   if (matcuda && PetscDefined(HAVE_CUDA)) PetscCall(MatSetType(Q,MATSEQDENSECUDA));
      85           8 :   else PetscCall(MatSetType(Q,MATSEQDENSE));
      86           8 :   PetscCall(MatSetSizes(Q,k,l,k,l));
      87           8 :   if (testlda) PetscCall(MatDenseSetLDA(Q,k+2));
      88           8 :   PetscCall(MatSeqDenseSetPreallocation(Q,NULL));
      89           8 :   PetscCall(PetscObjectSetName((PetscObject)Q,"Q"));
      90           8 :   PetscCall(MatDenseGetArrayWrite(Q,&q));
      91           8 :   PetscCall(MatDenseGetLDA(Q,&lda));
      92          48 :   for (i=0;i<k;i++)
      93         160 :     for (j=0;j<l;j++)
      94         216 :       q[i+j*lda] = (i<j)? 2.0: -0.5;
      95           8 :   PetscCall(MatDenseRestoreArrayWrite(Q,&q));
      96           8 :   if (verbose) PetscCall(MatView(Q,NULL));
      97             : 
      98             :   /* Test BVMult */
      99           8 :   PetscCall(BVMult(Y,2.0,1.0,X,Q));
     100           8 :   if (verbose) {
     101           8 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMult - - - - - - - - -\n"));
     102           8 :     PetscCall(BVView(Y,view));
     103             :   }
     104             : 
     105             :   /* Test BVMultVec */
     106           8 :   PetscCall(BVGetColumn(Y,0,&v));
     107           8 :   PetscCall(PetscMalloc1(k,&z));
     108           8 :   z[0] = 2.0;
     109          40 :   for (i=1;i<k;i++) z[i] = -0.5*z[i-1];
     110           8 :   PetscCall(BVMultVec(X,-1.0,1.0,v,z));
     111           8 :   PetscCall(PetscFree(z));
     112           8 :   PetscCall(BVRestoreColumn(Y,0,&v));
     113           8 :   if (verbose) {
     114           8 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMultVec - - - - - - -\n"));
     115           8 :     PetscCall(BVView(Y,view));
     116             :   }
     117             : 
     118             :   /* Test BVDot */
     119           8 :   PetscCall(MatCreate(PETSC_COMM_SELF,&M));
     120           8 :   if (matcuda && PetscDefined(HAVE_CUDA)) PetscCall(MatSetType(M,MATSEQDENSECUDA));
     121           8 :   else PetscCall(MatSetType(M,MATSEQDENSE));
     122           8 :   PetscCall(MatSetSizes(M,l,k,l,k));
     123           8 :   if (testlda) PetscCall(MatDenseSetLDA(M,l+2));
     124           8 :   PetscCall(MatSeqDenseSetPreallocation(M,NULL));
     125           8 :   PetscCall(PetscObjectSetName((PetscObject)M,"M"));
     126           8 :   PetscCall(BVDot(X,Y,M));
     127           8 :   if (verbose) {
     128           8 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVDot - - - - - - - - -\n"));
     129           8 :     PetscCall(MatView(M,NULL));
     130             :   }
     131             : 
     132             :   /* Test BVDotVec */
     133           8 :   PetscCall(BVGetColumn(Y,0,&v));
     134           8 :   PetscCall(PetscMalloc1(k,&z));
     135           8 :   PetscCall(BVDotVec(X,v,z));
     136           8 :   PetscCall(BVRestoreColumn(Y,0,&v));
     137           8 :   if (verbose) {
     138           8 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVDotVec - - - - - - -\n"));
     139           8 :     PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF,1,k,z,&v));
     140           8 :     PetscCall(PetscObjectSetName((PetscObject)v,"z"));
     141           8 :     PetscCall(VecView(v,view));
     142           8 :     PetscCall(VecDestroy(&v));
     143             :   }
     144           8 :   PetscCall(PetscFree(z));
     145             : 
     146             :   /* Test BVMultInPlace and BVScale */
     147           8 :   PetscCall(BVMultInPlace(X,Q,1,l));
     148           8 :   PetscCall(BVScale(X,2.0));
     149           8 :   if (verbose) {
     150           8 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMultInPlace - - - - -\n"));
     151           8 :     PetscCall(BVView(X,view));
     152             :   }
     153             : 
     154             :   /* Test BVNorm */
     155           8 :   PetscCall(BVNormColumn(X,0,NORM_2,&nrm));
     156           8 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"2-Norm of X[0] = %g\n",(double)nrm));
     157           8 :   PetscCall(BVNorm(X,NORM_FROBENIUS,&nrm));
     158           8 :   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Frobenius Norm of X = %g\n",(double)nrm));
     159             : 
     160             :   /* Test BVGetArrayRead */
     161           8 :   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
     162           8 :   if (!rank) {
     163           8 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"First row of X =\n"));
     164           8 :     PetscCall(BVGetLeadingDimension(X,&ldx));
     165           8 :     PetscCall(BVGetArrayRead(X,&pX));
     166          48 :     for (i=0;i<k;i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"%g ",(double)PetscRealPart(pX[i*ldx])));
     167           8 :     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
     168           8 :     PetscCall(BVRestoreArrayRead(X,&pX));
     169             :   }
     170             : 
     171           8 :   PetscCall(BVDestroy(&X));
     172           8 :   PetscCall(BVDestroy(&Y));
     173           8 :   PetscCall(MatDestroy(&Q));
     174           8 :   PetscCall(MatDestroy(&M));
     175           8 :   PetscCall(VecDestroy(&t));
     176           8 :   PetscCall(SlepcFinalize());
     177             :   return 0;
     178             : }
     179             : 
     180             : /*TEST
     181             : 
     182             :    test:
     183             :       args: -bv_type {{vecs contiguous svec mat}separate output} -verbose
     184             :       suffix: 1
     185             :       filter: sed -e 's/-0[.]/0./g'
     186             : 
     187             :    testset:
     188             :       args: -bv_type svec -vec_type cuda -verbose
     189             :       requires: cuda
     190             :       output_file: output/test1_1_svec_cuda.out
     191             :       test:
     192             :          suffix: 1_svec_cuda
     193             :       test:
     194             :          suffix: 1_svec_cuda_mat
     195             :          args: -matcuda
     196             :          filter: sed -e "s/seqdensecuda/seqdense/"
     197             : 
     198             :    testset:
     199             :       args: -bv_type mat -vec_type cuda -verbose
     200             :       requires: cuda
     201             :       output_file: output/test1_1_mat_cuda.out
     202             :       filter: sed -e "s/seqdensecuda/seqdense/"
     203             :       test:
     204             :          suffix: 1_mat_cuda
     205             :       test:
     206             :          suffix: 1_mat_cuda_mat
     207             :          args: -matcuda
     208             : 
     209             :    test:
     210             :       args: -bv_type {{vecs contiguous svec mat}separate output} -verbose -testlda
     211             :       suffix: 2
     212             :       filter: sed -e 's/-0[.]/0./g'
     213             : 
     214             :    testset:
     215             :       args: -bv_type svec -vec_type cuda -verbose -testlda
     216             :       requires: cuda
     217             :       output_file: output/test1_1_svec_cuda.out
     218             :       test:
     219             :          suffix: 2_svec_cuda
     220             :       test:
     221             :          suffix: 2_svec_cuda_mat
     222             :          args: -matcuda
     223             :          filter: sed -e "s/seqdensecuda/seqdense/"
     224             : 
     225             :    testset:
     226             :       args: -bv_type mat -vec_type cuda -verbose -testlda
     227             :       requires: cuda
     228             :       output_file: output/test1_1_mat_cuda.out
     229             :       filter: sed -e "s/seqdensecuda/seqdense/"
     230             :       test:
     231             :          suffix: 2_mat_cuda
     232             :       test:
     233             :          suffix: 2_mat_cuda_mat
     234             :          args: -matcuda
     235             : 
     236             : TEST*/

Generated by: LCOV version 1.14