Actual source code: ex19.c

  1: const char help[] = "Test MatCreateDenseFromVecType()\n\n";

  3: #include <petscdevice_cuda.h>
  4: #include <petscmat.h>
  5: #include <assert.h>

  7: int main(int argc, char **args)
  8: {
  9:   Mat      A;
 10:   Vec      X;
 11:   VecType  vtype;
 12:   PetscInt n = 20, lda = PETSC_DECIDE;

 14:   PetscFunctionBeginUser;
 15:   PetscCall(PetscInitialize(&argc, &args, NULL, help));

 17:   PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Creating Mat from Vec type example", NULL);
 18:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-lda", &lda, NULL));
 19:   PetscOptionsEnd();
 20:   if (lda > 0) lda += n;

 22:   PetscCall(VecCreate(PETSC_COMM_WORLD, &X));
 23:   PetscCall(VecSetSizes(X, n, PETSC_DECIDE));
 24:   PetscCall(VecSetFromOptions(X));
 25:   PetscCall(VecSetUp(X));
 26:   PetscCall(VecGetType(X, &vtype));

 28:   PetscCall(MatCreateDenseFromVecType(PETSC_COMM_WORLD, vtype, n, n, PETSC_DECIDE, PETSC_DECIDE, lda, NULL, &A));
 29:   PetscCall(MatSetFromOptions(A));
 30:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 31:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

 33:   PetscMemType       X_memtype, A_memtype;
 34:   const PetscScalar *array;
 35:   PetscCall(VecGetArrayReadAndMemType(X, &array, &X_memtype));
 36:   PetscCall(VecRestoreArrayReadAndMemType(X, &array));
 37:   PetscCall(MatDenseGetArrayReadAndMemType(A, &array, &A_memtype));
 38:   PetscCall(MatDenseRestoreArrayReadAndMemType(A, &array));
 39:   PetscAssert(A_memtype == X_memtype, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Failed memtype guarantee in MatCreateDenseFromVecType");

 41:   /* test */
 42:   PetscCall(MatViewFromOptions(A, NULL, "-ex19_mat_view"));
 43:   PetscCall(MatDestroy(&A));
 44:   PetscCall(VecDestroy(&X));
 45:   PetscCall(PetscFinalize());
 46:   return 0;
 47: }

 49: /*TEST
 50:   testset:
 51:     args: -lda {{0 1}} -ex19_mat_view
 52:     filter: grep -v -i type
 53:     output_file: output/ex19.out

 55:     test:
 56:       suffix: cuda
 57:       requires: cuda
 58:       args: -vec_type {{cuda mpicuda}}

 60:     test:
 61:       suffix: hip
 62:       requires: hip
 63:       args: -vec_type hip

 65:     test:
 66:       suffix: standard
 67:       args: -vec_type standard

 69:     test:
 70:       suffix: kokkos
 71:       # we don't have MATDENSESYCL yet
 72:       requires: kokkos_kernels !sycl
 73:       args: -vec_type kokkos
 74: TEST*/