Date: 2021-09-20T09:43:45+00:00

Actual source code: test7.c
slepc-3.15.2 2021-09-20
Report Typos and Errors

      1: /*
      2:    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      3:    SLEPc - Scalable Library for Eigenvalue Problem Computations
      4:    Copyright (c) 2002-2021, 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 multiplication of a Mat times a BV.\n\n";

     13: #include <slepcbv.h>

     15: int main(int argc,char **argv)
     16: {
     18:   Vec            t,r,v;
     19:   Mat            B,Ymat;
     20:   BV             X,Y,Z,Zcopy;
     21:   PetscInt       i,j,m=10,n,k=5,rep=1,Istart,Iend;
     22:   PetscScalar    *pZ;
     23:   PetscReal      norm;
     24:   PetscViewer    view;
     25:   PetscBool      flg,verbose,fromfile;
     26:   char           filename[PETSC_MAX_PATH_LEN];
     27:   PetscViewer    viewer;
     28:   BVMatMultType  vmm;

     30:   SlepcInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
     31:   PetscOptionsGetInt(NULL,NULL,"-k",&k,NULL);
     32:   PetscOptionsGetInt(NULL,NULL,"-rep",&rep,NULL);
     33:   PetscOptionsHasName(NULL,NULL,"-verbose",&verbose);
     34:   PetscOptionsGetString(NULL,NULL,"-file",filename,sizeof(filename),&fromfile);
     35:   MatCreate(PETSC_COMM_WORLD,&B);
     36:   PetscObjectSetName((PetscObject)B,"B");
     37:   if (fromfile) {
     38: #if defined(PETSC_USE_COMPLEX)
     39:     PetscPrintf(PETSC_COMM_WORLD," Reading COMPLEX matrix from a binary file...\n");
     40: #else
     41:     PetscPrintf(PETSC_COMM_WORLD," Reading REAL matrix from a binary file...\n");
     42: #endif
     43:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);
     44:     MatSetFromOptions(B);
     45:     MatLoad(B,viewer);
     46:     PetscViewerDestroy(&viewer);
     47:     MatGetSize(B,&m,&n);
     48:     MatGetOwnershipRange(B,&Istart,&Iend);
     49:   } else {
     50:     /* Create 1-D Laplacian matrix */
     51:     PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
     52:     PetscOptionsGetInt(NULL,NULL,"-n",&n,&flg);
     53:     if (!flg) n = m;
     54:     MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,m,n);
     55:     MatSetFromOptions(B);
     56:     MatSetUp(B);
     57:     MatGetOwnershipRange(B,&Istart,&Iend);
     58:     for (i=Istart;i<Iend;i++) {
     59:       if (i>0 && i-1<n) { MatSetValue(B,i,i-1,-1.0,INSERT_VALUES); }
     60:       if (i+1<n) { MatSetValue(B,i,i+1,-1.0,INSERT_VALUES); }
     61:       if (i<n) { MatSetValue(B,i,i,2.0,INSERT_VALUES); }
     62:     }
     63:     MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
     64:     MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
     65:   }

     67:   PetscPrintf(PETSC_COMM_WORLD,"Test BVMatMult (m=%D, n=%D, k=%D).\n",m,n,k);
     68:   MatCreateVecs(B,&t,&r);

     70:   /* Create BV object X */
     71:   BVCreate(PETSC_COMM_WORLD,&X);
     72:   PetscObjectSetName((PetscObject)X,"X");
     73:   BVSetSizesFromVec(X,t,k);
     74:   BVSetMatMultMethod(X,BV_MATMULT_VECS);
     75:   BVSetFromOptions(X);
     76:   BVGetMatMultMethod(X,&vmm);
     77:   PetscPrintf(PETSC_COMM_WORLD,"Using method: %s\n",BVMatMultTypes[vmm]);

     79:   /* Set up viewer */
     80:   PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&view);
     81:   if (verbose) {
     82:     PetscViewerPushFormat(view,PETSC_VIEWER_ASCII_MATLAB);
     83:   }

     85:   /* Fill X entries */
     86:   for (j=0;j<k;j++) {
     87:     BVGetColumn(X,j,&v);
     88:     VecSet(v,0.0);
     89:     for (i=Istart;i<PetscMin(j+1,Iend);i++) {
     90:       VecSetValue(v,i,1.0,INSERT_VALUES);
     91:     }
     92:     VecAssemblyBegin(v);
     93:     VecAssemblyEnd(v);
     94:     BVRestoreColumn(X,j,&v);
     95:   }
     96:   if (verbose) {
     97:     MatView(B,view);
     98:     BVView(X,view);
     99:   }

    101:   /* Create BV object Y */
    102:   BVCreate(PETSC_COMM_WORLD,&Y);
    103:   PetscObjectSetName((PetscObject)Y,"Y");
    104:   BVSetSizesFromVec(Y,r,k+4);
    105:   BVSetMatMultMethod(Y,BV_MATMULT_VECS);
    106:   BVSetFromOptions(Y);
    107:   BVSetActiveColumns(Y,2,k+2);

    109:   /* Test BVMatMult */
    110:   for (i=0;i<rep;i++) {
    111:     BVMatMult(X,B,Y);
    112:   }
    113:   if (verbose) {
    114:     BVView(Y,view);
    115:   }

    117:   /* Test BVGetMat/RestoreMat */
    118:   BVGetMat(Y,&Ymat);
    119:   PetscObjectSetName((PetscObject)Ymat,"Ymat");
    120:   if (verbose) {
    121:     MatView(Ymat,view);
    122:   }
    123:   BVRestoreMat(Y,&Ymat);

    125:   if (!fromfile) {
    126:     /* Create BV object Z */
    127:     BVDuplicateResize(Y,k,&Z);
    128:     PetscObjectSetName((PetscObject)Z,"Z");

    130:     /* Fill Z entries */
    131:     for (j=0;j<k;j++) {
    132:       BVGetColumn(Z,j,&v);
    133:       VecSet(v,0.0);
    134:       if (!Istart) { VecSetValue(v,0,1.0,ADD_VALUES); }
    135:       if (j<n && j>=Istart && j<Iend) { VecSetValue(v,j,1.0,ADD_VALUES); }
    136:       if (j+1<n && j>=Istart && j<Iend) { VecSetValue(v,j+1,-1.0,ADD_VALUES); }
    137:       VecAssemblyBegin(v);
    138:       VecAssemblyEnd(v);
    139:       BVRestoreColumn(Z,j,&v);
    140:     }
    141:     if (verbose) {
    142:       BVView(Z,view);
    143:     }

    145:     /* Save a copy of Z */
    146:     BVDuplicate(Z,&Zcopy);
    147:     BVCopy(Z,Zcopy);

    149:     /* Test BVMult, check result of previous operations */
    150:     BVMult(Z,-1.0,1.0,Y,NULL);
    151:     BVNorm(Z,NORM_FROBENIUS,&norm);
    152:     PetscPrintf(PETSC_COMM_WORLD,"Norm of error: %g\n",(double)norm);
    153:   }

    155:   /* Test BVMatMultColumn, multiply Y(:,2), result in Y(:,3) */
    156:   if (m==n) {
    157:     BVMatMultColumn(Y,B,2);
    158:     if (verbose) {
    159:       BVView(Y,view);
    160:     }

    162:     if (!fromfile) {
    163:       /* Test BVGetArray, modify Z to match Y */
    164:       BVCopy(Zcopy,Z);
    165:       BVGetArray(Z,&pZ);
    166:       if (Istart==0) {
    167:         if (Iend<3) SETERRQ(PETSC_COMM_WORLD,1,"First process must have at least 3 rows");
    168:         pZ[Iend]   = 5.0;   /* modify 3 first entries of second column */
    169:         pZ[Iend+1] = -4.0;
    170:         pZ[Iend+2] = 1.0;
    171:       }
    172:       BVRestoreArray(Z,&pZ);
    173:       if (verbose) {
    174:         BVView(Z,view);
    175:       }

    177:       /* Check result again with BVMult */
    178:       BVMult(Z,-1.0,1.0,Y,NULL);
    179:       BVNorm(Z,NORM_FROBENIUS,&norm);
    180:       PetscPrintf(PETSC_COMM_WORLD,"Norm of error: %g\n",(double)norm);
    181:     }
    182:   }

    184:   BVDestroy(&Z);
    185:   BVDestroy(&Zcopy);
    186:   BVDestroy(&X);
    187:   BVDestroy(&Y);
    188:   MatDestroy(&B);
    189:   VecDestroy(&t);
    190:   VecDestroy(&r);
    191:   SlepcFinalize();
    192:   return ierr;
    193: }

    195: /*TEST

    197:    testset:
    198:       output_file: output/test7_1.out
    199:       filter: grep -v "Using method"
    200:       test:
    201:          suffix: 1
    202:          args: -bv_type {{vecs contiguous svec mat}shared output} -bv_matmult vecs
    203:       test:
    204:          suffix: 1_cuda
    205:          args: -bv_type svec -mat_type aijcusparse -bv_matmult vecs
    206:          requires: cuda
    207:       test:
    208:          suffix: 1_mat
    209:          args: -bv_type {{vecs contiguous svec mat}shared output} -bv_matmult mat

    211:    testset:
    212:       output_file: output/test7_2.out
    213:       filter: grep -v "Using method"
    214:       args: -m 34 -n 38 -k 9
    215:       nsize: 2
    216:       test:
    217:          suffix: 2
    218:          args: -bv_type {{vecs contiguous svec mat}shared output} -bv_matmult vecs
    219:       test:
    220:          suffix: 2_cuda
    221:          args: -bv_type svec -mat_type aijcusparse -bv_matmult vecs
    222:          requires: cuda
    223:       test:
    224:          suffix: 2_mat
    225:          args: -bv_type {{vecs contiguous svec mat}shared output} -bv_matmult mat

    227: TEST*/