Actual source code: test1.c

slepc-3.19.0 2023-03-31
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: */

 11: static char help[] = "Test BV operations.\n\n";

 13: #include <slepcbv.h>

 15: int main(int argc,char **argv)
 16: {
 17:   Vec               t,v;
 18:   Mat               Q=NULL,M=NULL;
 19:   BV                X,Y;
 20:   PetscInt          i,j,n=10,k=5,l=3,nloc,lda;
 21:   PetscMPIInt       rank;
 22:   PetscScalar       *q,*z;
 23:   const PetscScalar *pX;
 24:   PetscReal         nrm;
 25:   PetscViewer       view;
 26:   PetscBool         verbose,matcuda,testlda=PETSC_FALSE;

 28:   PetscFunctionBeginUser;
 29:   PetscCall(SlepcInitialize(&argc,&argv,(char*)0,help));
 30:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL));
 31:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL));
 32:   PetscCall(PetscOptionsGetInt(NULL,NULL,"-l",&l,NULL));
 33:   PetscCall(PetscOptionsHasName(NULL,NULL,"-verbose",&verbose));
 34:   PetscCall(PetscOptionsHasName(NULL,NULL,"-matcuda",&matcuda));
 35:   PetscCall(PetscOptionsHasName(NULL,NULL,"-testlda",&testlda));
 36:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Test BV with %" PetscInt_FMT " columns of dimension %" PetscInt_FMT ".\n",k,n));

 38:   /* Create template vector */
 39:   PetscCall(VecCreate(PETSC_COMM_WORLD,&t));
 40:   PetscCall(VecSetSizes(t,PETSC_DECIDE,n));
 41:   PetscCall(VecSetFromOptions(t));
 42:   PetscCall(VecGetLocalSize(t,&nloc));

 44:   /* Create BV object X */
 45:   PetscCall(BVCreate(PETSC_COMM_WORLD,&X));
 46:   PetscCall(PetscObjectSetName((PetscObject)X,"X"));
 47:   PetscCall(BVSetSizesFromVec(X,t,k));
 48:   PetscCall(BVSetFromOptions(X));

 50:   /* Set up viewer */
 51:   PetscCall(PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&view));
 52:   PetscCall(PetscViewerPushFormat(view,PETSC_VIEWER_ASCII_INFO_DETAIL));
 53:   PetscCall(BVView(X,view));
 54:   PetscCall(PetscViewerPopFormat(view));

 56:   /* Fill X entries */
 57:   for (j=0;j<k;j++) {
 58:     PetscCall(BVGetColumn(X,j,&v));
 59:     PetscCall(VecSet(v,0.0));
 60:     for (i=0;i<4;i++) {
 61:       if (i+j<n) PetscCall(VecSetValue(v,i+j,(PetscScalar)(3*i+j-2),INSERT_VALUES));
 62:     }
 63:     PetscCall(VecAssemblyBegin(v));
 64:     PetscCall(VecAssemblyEnd(v));
 65:     PetscCall(BVRestoreColumn(X,j,&v));
 66:   }
 67:   if (verbose) PetscCall(BVView(X,view));

 69:   /* Create BV object Y */
 70:   PetscCall(BVCreate(PETSC_COMM_WORLD,&Y));
 71:   PetscCall(PetscObjectSetName((PetscObject)Y,"Y"));
 72:   PetscCall(BVSetSizesFromVec(Y,t,l));
 73:   PetscCall(BVSetFromOptions(Y));

 75:   /* Fill Y entries */
 76:   for (j=0;j<l;j++) {
 77:     PetscCall(BVGetColumn(Y,j,&v));
 78:     PetscCall(VecSet(v,(PetscScalar)(j+1)/4.0));
 79:     PetscCall(BVRestoreColumn(Y,j,&v));
 80:   }
 81:   if (verbose) PetscCall(BVView(Y,view));

 83:   /* Create Mat */
 84:   PetscCall(MatCreate(PETSC_COMM_SELF,&Q));
 85:   if (matcuda && PetscDefined(HAVE_CUDA)) PetscCall(MatSetType(Q,MATSEQDENSECUDA));
 86:   else PetscCall(MatSetType(Q,MATSEQDENSE));
 87:   PetscCall(MatSetSizes(Q,k,l,k,l));
 88:   if (testlda) PetscCall(MatDenseSetLDA(Q,k+2));
 89:   PetscCall(MatSeqDenseSetPreallocation(Q,NULL));
 90:   PetscCall(PetscObjectSetName((PetscObject)Q,"Q"));
 91:   PetscCall(MatDenseGetArrayWrite(Q,&q));
 92:   PetscCall(MatDenseGetLDA(Q,&lda));
 93:   for (i=0;i<k;i++)
 94:     for (j=0;j<l;j++)
 95:       q[i+j*lda] = (i<j)? 2.0: -0.5;
 96:   PetscCall(MatDenseRestoreArrayWrite(Q,&q));
 97:   if (verbose) PetscCall(MatView(Q,NULL));

 99:   /* Test BVMult */
100:   PetscCall(BVMult(Y,2.0,1.0,X,Q));
101:   if (verbose) {
102:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMult - - - - - - - - -\n"));
103:     PetscCall(BVView(Y,view));
104:   }

106:   /* Test BVMultVec */
107:   PetscCall(BVGetColumn(Y,0,&v));
108:   PetscCall(PetscMalloc1(k,&z));
109:   z[0] = 2.0;
110:   for (i=1;i<k;i++) z[i] = -0.5*z[i-1];
111:   PetscCall(BVMultVec(X,-1.0,1.0,v,z));
112:   PetscCall(PetscFree(z));
113:   PetscCall(BVRestoreColumn(Y,0,&v));
114:   if (verbose) {
115:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMultVec - - - - - - -\n"));
116:     PetscCall(BVView(Y,view));
117:   }

119:   /* Test BVDot */
120:   PetscCall(MatCreate(PETSC_COMM_SELF,&M));
121:   if (matcuda && PetscDefined(HAVE_CUDA)) PetscCall(MatSetType(M,MATSEQDENSECUDA));
122:   else PetscCall(MatSetType(M,MATSEQDENSE));
123:   PetscCall(MatSetSizes(M,l,k,l,k));
124:   if (testlda) PetscCall(MatDenseSetLDA(M,l+2));
125:   PetscCall(MatSeqDenseSetPreallocation(M,NULL));
126:   PetscCall(PetscObjectSetName((PetscObject)M,"M"));
127:   PetscCall(BVDot(X,Y,M));
128:   if (verbose) {
129:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVDot - - - - - - - - -\n"));
130:     PetscCall(MatView(M,NULL));
131:   }

133:   /* Test BVDotVec */
134:   PetscCall(BVGetColumn(Y,0,&v));
135:   PetscCall(PetscMalloc1(k,&z));
136:   PetscCall(BVDotVec(X,v,z));
137:   PetscCall(BVRestoreColumn(Y,0,&v));
138:   if (verbose) {
139:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVDotVec - - - - - - -\n"));
140:     PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF,1,k,z,&v));
141:     PetscCall(PetscObjectSetName((PetscObject)v,"z"));
142:     PetscCall(VecView(v,view));
143:     PetscCall(VecDestroy(&v));
144:   }
145:   PetscCall(PetscFree(z));

147:   /* Test BVMultInPlace and BVScale */
148:   PetscCall(BVMultInPlace(X,Q,1,l));
149:   PetscCall(BVScale(X,2.0));
150:   if (verbose) {
151:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"After BVMultInPlace - - - - -\n"));
152:     PetscCall(BVView(X,view));
153:   }

155:   /* Test BVNorm */
156:   PetscCall(BVNormColumn(X,0,NORM_2,&nrm));
157:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"2-Norm of X[0] = %g\n",(double)nrm));
158:   PetscCall(BVNorm(X,NORM_FROBENIUS,&nrm));
159:   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Frobenius Norm of X = %g\n",(double)nrm));

161:   /* Test BVGetArrayRead */
162:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
163:   if (!rank) {
164:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"First row of X =\n"));
165:     PetscCall(BVGetArrayRead(X,&pX));
166:     for (i=0;i<k;i++) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"%g ",(double)PetscRealPart(pX[i*nloc])));
167:     PetscCall(PetscPrintf(PETSC_COMM_WORLD,"\n"));
168:     PetscCall(BVRestoreArrayRead(X,&pX));
169:   }

171:   PetscCall(BVDestroy(&X));
172:   PetscCall(BVDestroy(&Y));
173:   PetscCall(MatDestroy(&Q));
174:   PetscCall(MatDestroy(&M));
175:   PetscCall(VecDestroy(&t));
176:   PetscCall(SlepcFinalize());
177:   return 0;
178: }

180: /*TEST

182:    test:
183:       args: -bv_type {{vecs contiguous svec mat}separate output} -verbose
184:       suffix: 1
185:       filter: sed -e 's/-0[.]/0./g'

187:    testset:
188:       args: -bv_type svec -vec_type cuda -verbose
189:       requires: cuda
190:       output_file: output/test1_1_cuda.out
191:       test:
192:          suffix: 1_cuda
193:       test:
194:          suffix: 1_cuda_mat
195:          args: -matcuda
196:          filter: sed -e "s/seqdensecuda/seqdense/"

198:    test:
199:       args: -bv_type {{vecs contiguous svec mat}separate output} -verbose -testlda
200:       suffix: 2
201:       filter: sed -e 's/-0[.]/0./g'

203:    testset:
204:       args: -bv_type svec -vec_type cuda -verbose -testlda
205:       requires: cuda
206:       output_file: output/test1_1_cuda.out
207:       test:
208:          suffix: 2_cuda
209:       test:
210:          suffix: 2_cuda_mat
211:          args: -matcuda
212:          filter: sed -e "s/seqdensecuda/seqdense/"

214: TEST*/