Actual source code: ex1.c

      2: static char help[] = "Basic vector routines.\n\n";

      4: /*
      5:   Include "petscvec.h" so that we can use vectors.  Note that this file
      6:   automatically includes:
      7:      petscsys.h       - base PETSc routines   petscis.h     - index sets
      8:      petscviewer.h - viewers
      9: */

     11: #include <petscvec.h>

     13: int main(int argc,char **argv)
     14: {
     15:   Vec            x,y,w;               /* vectors */
     16:   Vec            *z;                    /* array of vectors */
     17:   PetscReal      norm,v,v1,v2,maxval;
     18:   PetscInt       n = 20,maxind;
     19:   PetscScalar    one = 1.0,two = 2.0,three = 3.0,dots[3],dot;

     21:   PetscInitialize(&argc,&argv,(char*)0,help);
     22:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

     24:   /*
     25:      Create a vector, specifying only its global dimension.
     26:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector format
     27:      (currently parallel, shared, or sequential) is determined at runtime.  Also, the
     28:      parallel partitioning of the vector is determined by PETSc at runtime.

     30:      Routines for creating particular vector types directly are:
     31:         VecCreateSeq() - uniprocessor vector
     32:         VecCreateMPI() - distributed vector, where the user can
     33:                          determine the parallel partitioning
     34:         VecCreateShared() - parallel vector that uses shared memory
     35:                             (available only on the SGI); otherwise,
     36:                             is the same as VecCreateMPI()

     38:      With VecCreate(), VecSetSizes() and VecSetFromOptions() the option -vec_type mpi or
     39:      -vec_type shared causes the particular type of vector to be formed.

     41:   */
     42:   VecCreate(PETSC_COMM_WORLD,&x);
     43:   VecSetSizes(x,PETSC_DECIDE,n);
     44:   VecSetFromOptions(x);

     46:   /*
     47:      Duplicate some work vectors (of the same format and
     48:      partitioning as the initial vector).
     49:   */
     50:   VecDuplicate(x,&y);
     51:   VecDuplicate(x,&w);

     53:   /*
     54:      Duplicate more work vectors (of the same format and
     55:      partitioning as the initial vector).  Here we duplicate
     56:      an array of vectors, which is often more convenient than
     57:      duplicating individual ones.
     58:   */
     59:   VecDuplicateVecs(x,3,&z);
     60:   /*
     61:      Set the vectors to entries to a constant value.
     62:   */
     63:   VecSet(x,one);
     64:   VecSet(y,two);
     65:   VecSet(z[0],one);
     66:   VecSet(z[1],two);
     67:   VecSet(z[2],three);
     68:   /*
     69:      Demonstrate various basic vector routines.
     70:   */
     71:   VecDot(x,y,&dot);
     72:   VecMDot(x,3,z,dots);

     74:   /*
     75:      Note: If using a complex numbers version of PETSc, then
     76:      PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
     77:      (when using real numbers) it is undefined.
     78:   */

     80:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %" PetscInt_FMT "\n",n);
     81:   VecMax(x,&maxind,&maxval);
     82:   PetscPrintf(PETSC_COMM_WORLD,"VecMax %g, VecInd %" PetscInt_FMT "\n",(double)maxval,maxind);

     84:   VecMin(x,&maxind,&maxval);
     85:   PetscPrintf(PETSC_COMM_WORLD,"VecMin %g, VecInd %" PetscInt_FMT "\n",(double)maxval,maxind);
     86:   PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zero\n");

     88:   VecScale(x,two);
     89:   VecNorm(x,NORM_2,&norm);
     90:   v    = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
     91:   PetscPrintf(PETSC_COMM_WORLD,"VecScale %g\n",(double)v);

     93:   VecCopy(x,w);
     94:   VecNorm(w,NORM_2,&norm);
     95:   v    = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
     96:   PetscPrintf(PETSC_COMM_WORLD,"VecCopy  %g\n",(double)v);

     98:   VecAXPY(y,three,x);
     99:   VecNorm(y,NORM_2,&norm);
    100:   v    = norm-8.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    101:   PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %g\n",(double)v);

    103:   VecAYPX(y,two,x);
    104:   VecNorm(y,NORM_2,&norm);
    105:   v    = norm-18.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    106:   PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %g\n",(double)v);

    108:   VecSwap(x,y);
    109:   VecNorm(y,NORM_2,&norm);
    110:   v    = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    111:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);
    112:   VecNorm(x,NORM_2,&norm);
    113:   v = norm-18.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    114:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);

    116:   VecWAXPY(w,two,x,y);
    117:   VecNorm(w,NORM_2,&norm);
    118:   v    = norm-38.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    119:   PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %g\n",(double)v);

    121:   VecPointwiseMult(w,y,x);
    122:   VecNorm(w,NORM_2,&norm);
    123:   v    = norm-36.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    124:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %g\n",(double)v);

    126:   VecPointwiseDivide(w,x,y);
    127:   VecNorm(w,NORM_2,&norm);
    128:   v    = norm-9.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    129:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %g\n",(double)v);

    131:   dots[0] = one;
    132:   dots[1] = three;
    133:   dots[2] = two;

    135:   VecSet(x,one);
    136:   VecMAXPY(x,3,dots,z);
    137:   VecNorm(z[0],NORM_2,&norm);
    138:   v    = norm-PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    139:   VecNorm(z[1],NORM_2,&norm);
    140:   v1   = norm-2.0*PetscSqrtReal((PetscReal)n); if (v1 > -PETSC_SMALL && v1 < PETSC_SMALL) v1 = 0.0;
    141:   VecNorm(z[2],NORM_2,&norm);
    142:   v2   = norm-3.0*PetscSqrtReal((PetscReal)n); if (v2 > -PETSC_SMALL && v2 < PETSC_SMALL) v2 = 0.0;
    143:   PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g \n",(double)v,(double)v1,(double)v2);

    145:   /*
    146:      Free work space.  All PETSc objects should be destroyed when they
    147:      are no longer needed.
    148:   */
    149:   VecDestroy(&x);
    150:   VecDestroy(&y);
    151:   VecDestroy(&w);
    152:   VecDestroyVecs(3,&z);
    153:   PetscFinalize();
    154:   return 0;
    155: }

    157: /*TEST

    159:   testset:
    160:     output_file: output/ex1_1.out
    161:     # This is a test where the exact numbers are critical
    162:     diff_args: -j

    164:     test:

    166:     test:
    167:         suffix: cuda
    168:         args: -vec_type cuda
    169:         requires: cuda

    171:     test:
    172:         suffix: kokkos
    173:         args: -vec_type kokkos
    174:         requires: kokkos_kernels

    176:     test:
    177:         suffix: hip
    178:         args: -vec_type hip
    179:         requires: hip

    181:     test:
    182:         suffix: 2
    183:         nsize: 2

    185:     test:
    186:         suffix: 2_cuda
    187:         nsize: 2
    188:         args: -vec_type cuda
    189:         requires: cuda

    191:     test:
    192:         suffix: 2_kokkos
    193:         nsize: 2
    194:         args: -vec_type kokkos
    195:         requires: kokkos_kernels

    197:     test:
    198:         suffix: 2_hip
    199:         nsize: 2
    200:         args: -vec_type hip
    201:         requires: hip

    203: TEST*/