Actual source code: ex61.c

  1: static char help[] = "Test VecSetValuesCOO\n\n";

  3: #include <petscmat.h>
  4: int main(int argc, char **args)
  5: {
  6:   Vec            x, y;
  7:   PetscInt       k;
  8:   const PetscInt M = 18;
  9:   PetscMPIInt    rank, size;
 10:   PetscBool      equal;
 11:   PetscScalar   *vals;
 12:   PetscBool      ignoreRemote = PETSC_FALSE;

 14:   PetscInt i0[] = {3, 4, 1, 10, 0, 1, 1, 2, 1, 1, 2, 2, 3, 3, 4, 4, 1, 2, 5, 5, 6, 4, 17, 0, 1, 1, 8, 5, 5, 6, 4, 7, 8, 5};
 15:   PetscInt i1[] = {8, 5, 15, 16, 6, 13, 4, 17, 8, 9, 9, 10, 6, 12, 7, 3, 4, 1, 1, 2, 5, 5, 6, 14, 17, 8, 9, 9, 10, 4, 5, 10, 11, 1, 2};
 16:   PetscInt i2[] = {7, 7, 8, 8, 9, 16, 17, 9, 10, 1, 1, -2, 2, 3, 3, 14, 4, 5, 10, 13, 9, 9, 10, 1, 0, 0, 5, 5, 6, 6, 13, 13, 14, -14, 4, 4, 5, 11, 11, 12, 15, 15, 16};

 18:   struct {
 19:     PetscInt  *i;
 20:     PetscCount n;
 21:   } coo[3] = {
 22:     {i0, sizeof(i0) / sizeof(PetscInt)},
 23:     {i1, sizeof(i1) / sizeof(PetscInt)},
 24:     {i2, sizeof(i2) / sizeof(PetscInt)}
 25:   };

 28:   PetscInitialize(&argc, &args, (char *)0, help);
 29:   PetscOptionsGetBool(NULL, NULL, "-ignore_remote", &ignoreRemote, NULL);
 30:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
 31:   MPI_Comm_size(PETSC_COMM_WORLD, &size);


 35:   VecCreate(PETSC_COMM_WORLD, &x);
 36:   VecSetSizes(x, PETSC_DECIDE, M);
 37:   VecSetType(x, VECSTANDARD);
 38:   VecSetOption(x, VEC_IGNORE_OFF_PROC_ENTRIES, ignoreRemote);
 39:   VecSetOption(x, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);

 41:   for (k = 0; k < coo[rank].n; k++) {
 42:     PetscScalar val = (PetscScalar)coo[rank].i[k];
 43:     VecSetValues(x, 1, &coo[rank].i[k], &val, ADD_VALUES);
 44:   }
 45:   VecAssemblyBegin(x);
 46:   VecAssemblyEnd(x);

 48:   VecCreate(PETSC_COMM_WORLD, &y);
 49:   VecSetSizes(y, PETSC_DECIDE, M);
 50:   VecSetFromOptions(y);
 51:   VecSetOption(y, VEC_IGNORE_OFF_PROC_ENTRIES, ignoreRemote);
 52:   VecSetOption(y, VEC_IGNORE_NEGATIVE_INDICES, PETSC_TRUE);
 53:   VecSetPreallocationCOO(y, coo[rank].n, coo[rank].i);

 55:   PetscMalloc1(coo[rank].n, &vals);
 56:   for (k = 0; k < coo[rank].n; k++) vals[k] = (PetscScalar)coo[rank].i[k];
 57:   VecSetValuesCOO(y, vals, ADD_VALUES);

 59:   VecEqual(x, y, &equal);

 61:   if (!equal) {
 62:     VecView(x, PETSC_VIEWER_STDOUT_WORLD);
 63:     VecView(y, PETSC_VIEWER_STDOUT_WORLD);
 64:     SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "VecSetValuesCOO() failed");
 65:   }

 67:   PetscFree(vals);
 68:   VecDestroy(&x);
 69:   VecDestroy(&y);
 70:   PetscFinalize();
 71:   return 0;
 72: }

 74: /*TEST

 76:   testset:
 77:     output_file: output/empty.out
 78:     nsize: {{1 2 3}}
 79:     args: -ignore_remote {{0 1}}

 81:     test:
 82:       suffix: kokkos
 83:       requires: kokkos_kernels
 84:       args: -vec_type kokkos

 86:     test:
 87:       suffix: cuda
 88:       requires: cuda
 89:       args: -vec_type cuda

 91:     test:
 92:       suffix: std
 93:       args: -vec_type standard

 95: TEST*/