Actual source code: ex34.c
petsc-3.12.4 2020-02-04
Report Typos and Errors

      1: static char help[] = "Tests for norm caching\n";

      3:  #include <petscvec.h>
      4: #include <petsc/private/petscimpl.h>  /* to gain access to the private PetscObjectStateIncrease() */

      6: int main(int argc,char **argv)
      7: {
      8:   Vec            V,W;
      9:   MPI_Comm       comm;
     10:   PetscScalar    one=1,e=2.7181;
     11:   PetscReal      nrm1,nrm2,nrm3,nrm4;
     12:   PetscInt       ione=1;

     16:   PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr;
     17:   comm = MPI_COMM_SELF;

     19:   VecCreate(comm,&V);
     20:   VecSetSizes(V,10,PETSC_DECIDE);
     21:   VecSetFromOptions(V);
     22:   VecSetRandom(V,NULL);
     23:   VecAssemblyBegin(V);
     24:   VecAssemblyEnd(V);

     26:   /*
     27:    * Initial
     28:    */
     29:   /* display norm 1 & 2 */
     30:   VecNorm(V,NORM_1,&nrm1);
     31:   VecNorm(V,NORM_2,&nrm2);
     32:   PetscPrintf(comm,"Original: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     34:   /* display cached norm 1 & 2 */
     35:   VecNorm(V,NORM_1,&nrm1);
     36:   VecNorm(V,NORM_2,&nrm2);
     37:   PetscPrintf(comm,"cached: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     39:   /*
     40:    * Alter an element
     41:    */
     42:   VecSetValues(V,1,&ione,&one,INSERT_VALUES);

     44:   /* display norm 1 & 2 */
     45:   VecNorm(V,NORM_1,&nrm1);
     46:   VecNorm(V,NORM_2,&nrm2);
     47:   PetscPrintf(comm,"Altered: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     49:   /* display cached norm 1 & 2 */
     50:   VecNorm(V,NORM_1,&nrm1);
     51:   VecNorm(V,NORM_2,&nrm2);
     52:   PetscPrintf(comm,"recomputed: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     54:   /*
     55:    * Scale the vector a little
     56:    */
     57:   VecScale(V,e);

     59:   /* display updated cached norm 1 & 2 */
     60:   VecNorm(V,NORM_1,&nrm1);
     61:   VecNorm(V,NORM_2,&nrm2);
     62:   PetscPrintf(comm,"Scale: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     64:   /* display forced norm 1 & 2 */
     65:   PetscObjectStateIncrease((PetscObject)V);
     66:   VecNorm(V,NORM_1,&nrm1);
     67:   VecNorm(V,NORM_2,&nrm2);
     68:   PetscPrintf(comm,"recompute: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     70:   /*
     71:    * Normalize the vector a little
     72:    */
     73:   VecNormalize(V,&nrm1);

     75:   /* display updated cached norm 1 & 2 */
     76:   VecNorm(V,NORM_1,&nrm1);
     77:   VecNorm(V,NORM_2,&nrm2);
     78:   PetscPrintf(comm,"Normalize: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     80:   /* display forced norm 1 & 2 */
     81:   PetscObjectStateIncrease((PetscObject)V);
     82:   VecNorm(V,NORM_1,&nrm1);
     83:   VecNorm(V,NORM_2,&nrm2);
     84:   PetscPrintf(comm,"recompute: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     86:   /*
     87:    * Copy to another vector
     88:    */
     89:   VecDuplicate(V,&W);
     90:   VecCopy(V,W);

     92:   /* display norm 1 & 2 */
     93:   VecNorm(V,NORM_1,&nrm1);
     94:   VecNorm(V,NORM_2,&nrm2);
     95:   PetscPrintf(comm,"Original: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

     97:   /* display cached norm 1 & 2 */
     98:   VecNorm(W,NORM_1,&nrm1);
     99:   VecNorm(W,NORM_2,&nrm2);
    100:   PetscPrintf(comm,"copied: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

    102:   /*
    103:    * Copy while data is invalid
    104:    */
    105:   VecSetValues(V,1,&ione,&one,INSERT_VALUES);
    106:   VecCopy(V,W);

    108:   /* display norm 1 & 2 */
    109:   VecNorm(V,NORM_1,&nrm1);
    110:   VecNorm(V,NORM_2,&nrm2);
    111:   PetscPrintf(comm,"Invalidated: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

    113:   /* display norm 1 & 2 */
    114:   VecNorm(W,NORM_1,&nrm1);
    115:   VecNorm(W,NORM_2,&nrm2);
    116:   PetscPrintf(comm,"copied: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

    118:   /*
    119:    * Constant vector
    120:    */
    121:   VecSet(V,e);

    123:   /* display updated cached norm 1 & 2 */
    124:   VecNorm(V,NORM_1,&nrm1);
    125:   VecNorm(V,NORM_2,&nrm2);
    126:   PetscPrintf(comm,"Constant: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

    128:   /* display forced norm 1 & 2 */
    129:   PetscObjectStateIncrease((PetscObject)V);
    130:   VecNorm(V,NORM_1,&nrm1);
    131:   VecNorm(V,NORM_2,&nrm2);
    132:   PetscPrintf(comm,"recomputed: norm1=%e,norm2=%e\n",(double)nrm1,(double)nrm2);

    134:   /*
    135:    * Swap vectors
    136:    */
    137:   VecNorm(V,NORM_1,&nrm1);
    138:   VecNorm(W,NORM_1,&nrm2);
    139:   PetscPrintf(comm,"Orig: norm_V=%e,norm_W=%e\n",(double)nrm1,(double)nrm2);
    140:   /* store inf norm */
    141:   VecNorm(V,NORM_INFINITY,&nrm3);
    142:   VecNorm(W,NORM_INFINITY,&nrm4);

    144:   VecSwap(V,W);

    146:   PetscObjectStateIncrease((PetscObject)V);
    147:   PetscObjectStateIncrease((PetscObject)W);
    148:   VecNorm(V,NORM_1,&nrm1);
    149:   VecNorm(W,NORM_1,&nrm2);
    150:   PetscPrintf(comm,"swapped: norm_V=%e,norm_W=%e\n",(double)nrm2,(double)nrm1);
    151:   PetscPrintf(comm,"orig: F-norm_V=%e,F-norm_W=%e\n",(double)nrm3,(double)nrm4);
    152:   VecNorm(V,NORM_INFINITY,&nrm3);
    153:   VecNorm(W,NORM_INFINITY,&nrm4);
    154:   PetscPrintf(comm,"swapped: F-norm_V=%e,F-norm_W=%e\n",(double)nrm4,(double)nrm3);

    156:   VecDestroy(&V);
    157:   VecDestroy(&W);
    158:   PetscFinalize();
    159:   return ierr;
    160: }

    163: /*TEST

    165:    testset:
    166:       output_file: output/ex34_1.out
    167:       test:
    168:         suffix: standard
    169:       test:
    170:         requires: cuda
    171:         args: -vec_type cuda
    172:         suffix: cuda
    173:       test:
    174:         requires: viennacl
    175:         args: -vec_type viennacl
    176:         suffix: viennacl

    178: TEST*/