Date: 2022-03-31T04:46:29+00:00

Actual source code: ex1.c

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

      4: /*T
      5:    Concepts: vectors^basic routines;
      6:    Processors: n
      7: T*/

      9: /*
     10:   Include "petscvec.h" so that we can use vectors.  Note that this file
     11:   automatically includes:
     12:      petscsys.h       - base PETSc routines   petscis.h     - index sets
     13:      petscviewer.h - viewers
     14: */

     16: #include <petscvec.h>

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

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

     29:   /*
     30:      Create a vector, specifying only its global dimension.
     31:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector format
     32:      (currently parallel, shared, or sequential) is determined at runtime.  Also, the
     33:      parallel partitioning of the vector is determined by PETSc at runtime.

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

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

     46:   */
     47:   VecCreate(PETSC_COMM_WORLD,&x);
     48:   VecSetSizes(x,PETSC_DECIDE,n);
     49:   VecSetFromOptions(x);

     51:   /*
     52:      Duplicate some work vectors (of the same format and
     53:      partitioning as the initial vector).
     54:   */
     55:   VecDuplicate(x,&y);
     56:   VecDuplicate(x,&w);

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

     79:   /*
     80:      Note: If using a complex numbers version of PETSc, then
     81:      PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
     82:      (when using real numbers) it is undefined.
     83:   */

     85:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %" PetscInt_FMT "\n",n);
     86:   VecMax(x,&maxind,&maxval);
     87:   PetscPrintf(PETSC_COMM_WORLD,"VecMax %g, VecInd %" PetscInt_FMT "\n",(double)maxval,maxind);

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

     93:   VecScale(x,two);
     94:   VecNorm(x,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,"VecScale %g\n",(double)v);

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

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

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

    113:   VecSwap(x,y);
    114:   VecNorm(y,NORM_2,&norm);
    115:   v    = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    116:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);
    117:   VecNorm(x,NORM_2,&norm);
    118:   v = norm-18.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
    119:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);

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

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

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

    136:   dots[0] = one;
    137:   dots[1] = three;
    138:   dots[2] = two;

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

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

    162: /*TEST

    164:   testset:
    165:     output_file: output/ex1_1.out
    166:     # This is a test where the exact numbers are critical
    167:     diff_args: -j

    169:     test:

    171:     test:
    172:         suffix: cuda
    173:         args: -vec_type cuda
    174:         requires: cuda

    176:     test:
    177:         suffix: kokkos
    178:         args: -vec_type kokkos
    179:         requires: kokkos_kernels

    181:     test:
    182:         suffix: hip
    183:         args: -vec_type hip
    184:         requires: hip

    186:     test:
    187:         suffix: 2
    188:         nsize: 2

    190:     test:
    191:         suffix: 2_cuda
    192:         nsize: 2
    193:         args: -vec_type cuda
    194:         requires: cuda

    196:     test:
    197:         suffix: 2_kokkos
    198:         nsize: 2
    199:         args: -vec_type kokkos
    200:         requires: kokkos_kernels

    202:     test:
    203:         suffix: 2_hip
    204:         nsize: 2
    205:         args: -vec_type hip
    206:         requires: hip

    208: TEST*/