Actual source code: ex41.c

  1: static char help[] = "Nest vector set subvector functionality.\n\n";

  3: #include <petscvec.h>

  5: PetscErrorCode test_vec_ops(void)
  6: {
  7:   Vec         X, Y, a, b;
  8:   Vec         c, d, e, f, g, h;
  9:   PetscScalar val;
 10:   PetscInt    tmp_ind[2];
 11:   Vec         tmp_buf[2];

 13:   PetscPrintf(PETSC_COMM_WORLD, "============== %s ==============\n", PETSC_FUNCTION_NAME);

 15:   /* create 4 worker vectors */
 16:   VecCreate(PETSC_COMM_WORLD, &c);
 17:   VecSetSizes(c, PETSC_DECIDE, 4);
 18:   VecSetType(c, VECMPI);
 19:   VecDuplicate(c, &d);
 20:   VecDuplicate(c, &e);
 21:   VecDuplicate(c, &f);

 23:   /* create two more workers of different sizes */
 24:   VecCreate(PETSC_COMM_WORLD, &g);
 25:   VecSetSizes(g, PETSC_DECIDE, 6);
 26:   VecSetType(g, VECMPI);
 27:   VecCreate(PETSC_COMM_WORLD, &h);
 28:   VecSetSizes(h, PETSC_DECIDE, 8);
 29:   VecSetType(h, VECMPI);

 31:   /* set the 6 vectors to some numbers */
 32:   VecSet(c, 1.0);
 33:   VecSet(d, 2.0);
 34:   VecSet(e, 3.0);
 35:   VecSet(f, 4.0);
 36:   VecSet(g, 5.0);
 37:   VecSet(h, 6.0);

 39:   /* assemble a */
 40:   PetscPrintf(PETSC_COMM_WORLD, "a = [c d] \n");
 41:   tmp_buf[0] = c;
 42:   tmp_buf[1] = d;

 44:   VecCreateNest(PETSC_COMM_WORLD, 2, NULL, tmp_buf, &a);
 45:   VecView(a, PETSC_VIEWER_STDOUT_WORLD);
 46:   PetscPrintf(PETSC_COMM_WORLD, "a = [d c] \n");
 47:   VecNestSetSubVec(a, 1, c);
 48:   VecNestSetSubVec(a, 0, d);
 49:   VecAssemblyBegin(a);
 50:   VecAssemblyEnd(a);
 51:   VecView(a, PETSC_VIEWER_STDOUT_WORLD);

 53:   /* assemble b */
 54:   PetscPrintf(PETSC_COMM_WORLD, "b = [e f] \n");
 55:   tmp_buf[0] = e;
 56:   tmp_buf[1] = f;

 58:   VecCreateNest(PETSC_COMM_WORLD, 2, NULL, tmp_buf, &b);
 59:   VecView(b, PETSC_VIEWER_STDOUT_WORLD);
 60:   PetscPrintf(PETSC_COMM_WORLD, "b = [f e] \n");
 61:   VecNestSetSubVec(b, 1, e);
 62:   VecNestSetSubVec(b, 0, f);
 63:   VecAssemblyBegin(b);
 64:   VecAssemblyEnd(b);
 65:   VecView(b, PETSC_VIEWER_STDOUT_WORLD);

 67:   PetscPrintf(PETSC_COMM_WORLD, "X = [a b] \n");
 68:   tmp_buf[0] = a;
 69:   tmp_buf[1] = b;

 71:   VecCreateNest(PETSC_COMM_WORLD, 2, NULL, tmp_buf, &X);
 72:   VecView(X, PETSC_VIEWER_STDOUT_WORLD);
 73:   VecDot(X, X, &val);
 74:   PetscPrintf(PETSC_COMM_WORLD, "X.X = %g \n", (double)PetscRealPart(val));

 76:   PetscPrintf(PETSC_COMM_WORLD, "X = [b a] \n");
 77:   /* re-order components of X */
 78:   VecNestSetSubVec(X, 1, a);
 79:   VecNestSetSubVec(X, 0, b);
 80:   VecAssemblyBegin(X);
 81:   VecAssemblyEnd(X);
 82:   VecView(X, PETSC_VIEWER_STDOUT_WORLD);
 83:   VecDot(X, X, &val);
 84:   PetscPrintf(PETSC_COMM_WORLD, "X.X = %g \n", (double)PetscRealPart(val));

 86:   /* re-assemble X */
 87:   PetscPrintf(PETSC_COMM_WORLD, "X = [g h] \n");
 88:   VecNestSetSubVec(X, 1, g);
 89:   VecNestSetSubVec(X, 0, h);
 90:   VecAssemblyBegin(X);
 91:   VecAssemblyEnd(X);
 92:   VecView(X, PETSC_VIEWER_STDOUT_WORLD);
 93:   VecDot(X, X, &val);
 94:   PetscPrintf(PETSC_COMM_WORLD, "X.X = %g \n", (double)PetscRealPart(val));

 96:   PetscPrintf(PETSC_COMM_WORLD, "Y = X \n");
 97:   VecDuplicate(X, &Y);
 98:   VecCopy(X, Y);
 99:   VecView(Y, PETSC_VIEWER_STDOUT_WORLD);
100:   VecDot(Y, Y, &val);
101:   PetscPrintf(PETSC_COMM_WORLD, "Y.Y = %g \n", (double)PetscRealPart(val));

103:   PetscPrintf(PETSC_COMM_WORLD, "Y = [a b] \n");
104:   tmp_buf[0] = a;
105:   tmp_buf[1] = b;
106:   tmp_ind[0] = 0;
107:   tmp_ind[1] = 1;

109:   VecNestSetSubVecs(Y, 2, tmp_ind, tmp_buf);
110:   VecView(Y, PETSC_VIEWER_STDOUT_WORLD);

112:   VecDestroy(&c);
113:   VecDestroy(&d);
114:   VecDestroy(&e);
115:   VecDestroy(&f);
116:   VecDestroy(&g);
117:   VecDestroy(&h);
118:   VecDestroy(&a);
119:   VecDestroy(&b);
120:   VecDestroy(&X);
121:   VecDestroy(&Y);
122:   return 0;
123: }

125: int main(int argc, char **args)
126: {
128:   PetscInitialize(&argc, &args, (char *)0, help);
129:   test_vec_ops();
130:   PetscFinalize();
131:   return 0;
132: }

134: /*TEST

136:    test:

138: TEST*/