xref: /petsc/src/mat/tests/ex217.c (revision b2ccae6bdc8edea944f1c160ca3b2eb32c69ecb2)
1 static char help[] = "Tests MatGetCurrentMemType for gpu type matrices both bound and unbound to cpu";
2 
3 #include <petscmat.h>
4 #include <../src/mat/impls/aij/mpi/mpiaij.h>
5 
6 int main(int argc, char **argv)
7 {
8   Mat          A;
9   PetscMemType memtype;
10   MatType      mattype;
11   PetscBool    ishypre, iskokkos, iscuda, iship;
12 
13   PetscFunctionBeginUser;
14   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
15   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
16   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, 1, 1));
17   PetscCall(MatSetFromOptions(A));
18   PetscCall(MatGetType(A, &mattype));
19   PetscCall(PetscObjectTypeCompareAny((PetscObject)A, &iscuda, MATMPIAIJCUSPARSE, MATSEQAIJCUSPARSE, ""));
20   PetscCall(PetscObjectTypeCompareAny((PetscObject)A, &iship, MATMPIAIJHIPSPARSE, MATSEQAIJHIPSPARSE, ""));
21   PetscCall(PetscObjectTypeCompareAny((PetscObject)A, &iskokkos, MATMPIAIJKOKKOS, MATSEQAIJKOKKOS, ""));
22   PetscCall(PetscObjectTypeCompare((PetscObject)A, MATHYPRE, &ishypre));
23 #if defined(PETSC_HAVE_HYPRE)
24   PetscCall(MatHYPRESetPreallocation(A, 1, NULL, 1, NULL));
25 #endif
26   PetscCall(MatSeqAIJSetPreallocation(A, 1, NULL));
27   PetscCall(MatMPIAIJSetPreallocation(A, 1, NULL, 1, NULL));
28 
29   PetscCall(MatGetCurrentMemType(A, &memtype));
30   if (iscuda) PetscCheck(memtype == PETSC_MEMTYPE_CUDA, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wrong memory type");
31   else if (iship) PetscCheck(memtype == PETSC_MEMTYPE_HIP, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wrong memory type");
32   else if (iskokkos) PetscCheck(memtype == PETSC_MEMTYPE_KOKKOS, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wrong memory type");
33   else if (ishypre) PetscCheck(PetscDefined(HAVE_HYPRE_DEVICE) ? memtype == PETSC_MEMTYPE_DEVICE : memtype == PETSC_MEMTYPE_HOST, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wrong memory type");
34   else PetscCheck(memtype == PETSC_MEMTYPE_HOST, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wrong memory type");
35 
36   // Kokkos doesn't currently implement MatBindToCPU
37   if (!iskokkos) {
38     PetscCall(MatBindToCPU(A, PETSC_TRUE));
39     PetscCall(MatGetCurrentMemType(A, &memtype));
40     PetscCheck(memtype == PETSC_MEMTYPE_HOST, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "wrong memory type");
41   }
42   PetscCall(MatDestroy(&A));
43   PetscCall(PetscFinalize());
44   return 0;
45 }
46 
47 /*TEST
48 
49    test:
50      suffix: seqaij
51      args: -mat_type aij
52      output_file: output/empty.out
53 
54    test:
55      suffix: mpiaij
56      nsize: 2
57      args: -mat_type aij
58      output_file: output/empty.out
59 
60    test:
61      requires: cuda
62      suffix: seqaijcusparse
63      args: -mat_type aijcusparse
64      output_file: output/empty.out
65 
66    test:
67      requires: cuda
68      suffix: mpiaijcusparse
69      nsize: 2
70      args: -mat_type aijcusparse
71      output_file: output/empty.out
72 
73    test:
74      requires: hip
75      suffix: seqaijhipsparse
76      args: -mat_type aijhipsparse
77      output_file: output/empty.out
78 
79    test:
80      requires: hip
81      suffix: mpiaijhipsparse
82      nsize: 2
83      args: -mat_type aijhipsparse
84      output_file: output/empty.out
85 
86    test:
87      requires: kokkos_kernels
88      suffix: seqaijkokkos
89      args: -mat_type aijkokkos
90      output_file: output/empty.out
91 
92    test:
93      requires: kokkos_kernels
94      suffix: mpiaijkokkos
95      nsize: 2
96      args: -mat_type aijkokkos
97      output_file: output/empty.out
98 
99    test:
100      requires: hypre
101      suffix: hypre
102      args: -mat_type hypre
103      output_file: output/empty.out
104 
105    test:
106      requires: hypre
107      suffix: hypre_parallel
108      nsize: 2
109      args: -mat_type hypre
110      output_file: output/empty.out
111 
112 TEST*/
113