Date: 2013-07-02T14:40:14+00:00

Actual source code: ex34.c
petsc-3.4.2 2013-07-02

      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() */

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

     18:   PetscInitialize(&argc,&argv,0,help);
     19:   comm = MPI_COMM_SELF;

     21:   VecCreateSeq(comm,10,&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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm1,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",nrm2,nrm1);
    151:   PetscPrintf(comm,"orig: F norm V=%e, F norm W=%e\n",nrm3,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",nrm4,nrm3);

    156:   VecDestroy(&V);
    157:   VecDestroy(&W);

    159:   PetscFinalize();
    160:   return(0);
    161: }