Actual source code: ex69.c
1: static char help[] = "Tests MatCreateDenseCUDA(), MatDenseCUDAPlaceArray(), MatDenseCUDAReplaceArray(), and MatDenseCUDAResetArray()\n\
2: or MatCreateDenseHIP(), MatDenseHIPPlaceArray(), MatDenseHIPReplaceArray(), and MatDenseHIPResetArray(),\n\
3: as well as MatDensePlaceArrayWithMemType(), MatDenseResetArrayWithMemType(), and MatDiagonalScale() on device.\n";
5: #include <petscmat.h>
6: #include <petscpkg_version.h>
8: #if !defined(PETSC_PKG_CUDA_VERSION_GE)
9: #define PETSC_PKG_CUDA_VERSION_GE(MAJ, MIN, PAT) 0
10: #endif
11: #define PetscConcat3(a, b, c) PetscConcat(PetscConcat(a, b), c)
12: #if PetscDefined(HAVE_CUDA)
13: #define DEVICE_TAG CUDA
14: #elif PetscDefined(HAVE_HIP)
15: #define DEVICE_TAG HIP
16: #else
17: #define DEVICE_TAG
18: #endif
19: #define VecDeviceGetArray PetscConcat3(Vec, DEVICE_TAG, GetArray)
20: #define VecDeviceRestoreArray PetscConcat3(Vec, DEVICE_TAG, RestoreArray)
21: #define MatCreateDenseDevice PetscConcat(MatCreateDense, DEVICE_TAG)
22: #define MatDenseDeviceReplaceArray PetscConcat3(MatDense, DEVICE_TAG, ReplaceArray)
23: #define MatDenseDevicePlaceArray PetscConcat3(MatDense, DEVICE_TAG, PlaceArray)
24: #define MatDenseDeviceResetArray PetscConcat3(MatDense, DEVICE_TAG, ResetArray)
26: static PetscErrorCode MatMult_S(Mat S, Vec x, Vec y)
27: {
28: Mat A;
30: PetscFunctionBeginUser;
31: PetscCall(MatShellGetContext(S, &A));
32: PetscCall(MatMult(A, x, y));
33: PetscFunctionReturn(PETSC_SUCCESS);
34: }
36: static PetscBool test_cusparse_transgen = PETSC_FALSE;
38: static PetscErrorCode MatMultTranspose_S(Mat S, Vec x, Vec y)
39: {
40: Mat A;
42: PetscFunctionBeginUser;
43: PetscCall(MatShellGetContext(S, &A));
44: PetscCall(MatMultTranspose(A, x, y));
46: /* alternate transgen true and false to test code logic */
47: PetscCall(MatSetOption(A, MAT_FORM_EXPLICIT_TRANSPOSE, test_cusparse_transgen));
48: test_cusparse_transgen = (PetscBool)!test_cusparse_transgen;
49: PetscFunctionReturn(PETSC_SUCCESS);
50: }
52: int main(int argc, char **argv)
53: {
54: Mat A, B, C, S;
55: Vec t, v, ll, rr;
56: PetscScalar *vv, *aa;
57: // We met a mysterious cudaErrorMisalignedAddress error on some systems with cuda-12.0,1 but not
58: // with prior cuda-11.2,3,7,8 versions. Making nloc an even number somehow 'fixes' the problem.
59: // See more at https://gitlab.com/petsc/petsc/-/merge_requests/6225
60: #if PETSC_PKG_CUDA_VERSION_GE(12, 0, 0)
61: PetscInt n = 32;
62: #else
63: PetscInt n = 30;
64: #endif
65: PetscInt k = 6, l = 0, i, Istart, Iend, nloc, bs, test = 1;
66: PetscBool flg, use_shell = PETSC_FALSE;
67: VecType vtype;
68: PetscMemType mtype;
70: PetscFunctionBeginUser;
71: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
72: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
73: PetscCall(PetscOptionsGetInt(NULL, NULL, "-k", &k, NULL));
74: PetscCall(PetscOptionsGetInt(NULL, NULL, "-l", &l, NULL));
75: PetscCall(PetscOptionsGetInt(NULL, NULL, "-test", &test, NULL));
76: PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_shell", &use_shell, NULL));
77: PetscCheck(k >= 0, PETSC_COMM_WORLD, PETSC_ERR_USER, "k %" PetscInt_FMT " must be positive", k);
78: PetscCheck(l >= 0, PETSC_COMM_WORLD, PETSC_ERR_USER, "l %" PetscInt_FMT " must be positive", l);
79: PetscCheck(l <= k, PETSC_COMM_WORLD, PETSC_ERR_USER, "l %" PetscInt_FMT " must be smaller or equal than k %" PetscInt_FMT, l, k);
81: /* sparse matrix */
82: PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
83: PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n));
84: PetscCall(MatSetType(A, PetscDefined(HAVE_CUDA) ? MATAIJCUSPARSE : MATAIJHIPSPARSE));
85: PetscCall(MatSetOptionsPrefix(A, "A_"));
86: PetscCall(MatSetFromOptions(A));
87: PetscCall(MatSetUp(A));
89: /* test special case for MATSEQAIJCUSPARSE and MATSEQAIJHIPSPARSE to generate explicit transpose (not default) */
90: PetscCall(MatSetOption(A, MAT_FORM_EXPLICIT_TRANSPOSE, PETSC_TRUE));
92: PetscCall(MatGetOwnershipRange(A, &Istart, &Iend));
93: for (i = Istart; i < Iend; i++) {
94: if (i > 0) PetscCall(MatSetValue(A, i, i - 1, -1.0, INSERT_VALUES));
95: if (i < n - 1) PetscCall(MatSetValue(A, i, i + 1, -1.0, INSERT_VALUES));
96: PetscCall(MatSetValue(A, i, i, 2.0, INSERT_VALUES));
97: }
98: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
99: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
101: /* template vector */
102: PetscCall(MatCreateVecs(A, NULL, &t));
103: PetscCall(VecGetType(t, &vtype));
105: /* long vector, contains the stacked columns of an nxk dense matrix */
106: PetscCall(VecGetLocalSize(t, &nloc));
107: PetscCall(VecGetBlockSize(t, &bs));
108: PetscCall(VecCreate(PetscObjectComm((PetscObject)t), &v));
109: PetscCall(VecSetType(v, vtype));
110: PetscCall(VecSetSizes(v, k * nloc, k * n));
111: PetscCall(VecSetBlockSize(v, bs));
112: PetscCall(VecSetRandom(v, NULL));
114: /* dense matrix that contains the columns of v */
115: PetscCall(VecDeviceGetArray(v, &vv));
117: /* test few cases for MATDENSECUDA and MATDENSEHIP handling pointers */
118: switch (test) {
119: case 1:
120: PetscCall(MatCreateDenseDevice(PetscObjectComm((PetscObject)v), nloc, PETSC_DECIDE, n, k - l, vv, &B)); /* pass a pointer to avoid allocation of storage */
121: PetscCall(MatGetCurrentMemType(B, &mtype));
122: PetscCall(MatDenseDeviceReplaceArray(B, NULL)); /* replace with a null pointer, the value after BVRestoreMat */
123: PetscCall(MatDensePlaceArrayWithMemType(B, mtype, vv + l * nloc)); /* set the actual pointer */
124: break;
125: case 2:
126: PetscCall(MatCreateDenseDevice(PetscObjectComm((PetscObject)v), nloc, PETSC_DECIDE, n, k - l, NULL, &B));
127: PetscCall(MatDenseDevicePlaceArray(B, vv + l * nloc)); /* set the actual pointer */
128: break;
129: default:
130: PetscCall(MatCreateDenseDevice(PetscObjectComm((PetscObject)v), nloc, PETSC_DECIDE, n, k - l, vv + l * nloc, &B));
131: break;
132: }
134: /* test MatMatMult() */
135: if (use_shell) {
136: /* we could have called the general converter below, but we explicitly set the operations
137: ourselves to test MatProductSymbolic_X_Dense, MatProductNumeric_X_Dense code */
138: /* PetscCall(MatConvert(A, MATSHELL, MAT_INITIAL_MATRIX, &S)); */
139: PetscCall(MatCreateShell(PetscObjectComm((PetscObject)v), nloc, nloc, n, n, A, &S));
140: PetscCall(MatShellSetOperation(S, MATOP_MULT, (PetscErrorCodeFn *)MatMult_S));
141: PetscCall(MatShellSetOperation(S, MATOP_MULT_TRANSPOSE, (PetscErrorCodeFn *)MatMultTranspose_S));
142: PetscCall(MatShellSetVecType(S, vtype));
143: } else {
144: PetscCall(PetscObjectReference((PetscObject)A));
145: S = A;
146: }
148: PetscCall(MatCreateDenseDevice(PetscObjectComm((PetscObject)v), nloc, PETSC_DECIDE, n, k - l, NULL, &C));
150: /* test MatMatMult() */
151: PetscCall(MatProductCreateWithMat(S, B, NULL, C));
152: PetscCall(MatProductSetType(C, MATPRODUCT_AB));
153: PetscCall(MatProductSetFromOptions(C));
154: PetscCall(MatProductSymbolic(C));
155: PetscCall(MatProductNumeric(C));
156: PetscCall(MatMatMultEqual(S, B, C, 10, &flg));
157: if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error MatMatMult()\n"));
159: /* test MatTransposeMatMult() */
160: PetscCall(MatProductCreateWithMat(S, B, NULL, C));
161: PetscCall(MatProductSetType(C, MATPRODUCT_AtB));
162: PetscCall(MatProductSetFromOptions(C));
163: PetscCall(MatProductSymbolic(C));
164: PetscCall(MatProductNumeric(C));
165: PetscCall(MatTransposeMatMultEqual(S, B, C, 10, &flg));
166: if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error MatTransposeMatMult()\n"));
168: PetscCall(MatDestroy(&C));
169: PetscCall(MatDestroy(&S));
171: /* test MatDiagonalScale() */
172: PetscCall(MatDuplicate(B, MAT_COPY_VALUES, &C));
173: PetscCall(MatBindToCPU(C, PETSC_TRUE));
174: PetscCall(MatCreateVecs(B, &rr, &ll));
175: PetscCall(VecSetRandom(ll, NULL));
176: PetscCall(VecSetRandom(rr, NULL));
177: PetscCall(MatDiagonalScale(C, ll, rr));
178: PetscCall(MatDiagonalScale(B, ll, rr));
179: PetscCall(MatMultEqual(B, C, 10, &flg));
180: if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Error MatDiagonalScale()\n"));
182: PetscCall(VecDestroy(&ll));
183: PetscCall(VecDestroy(&rr));
184: PetscCall(MatDestroy(&C));
186: /* finished using B */
187: PetscCall(MatDenseGetArrayAndMemType(B, &aa, NULL));
188: PetscCheck(vv == aa - l * nloc, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Wrong array");
189: PetscCall(MatDenseRestoreArrayAndMemType(B, &aa));
190: if (test == 1) PetscCall(MatDenseResetArrayWithMemType(B, mtype));
191: else if (test == 2) PetscCall(MatDenseDeviceResetArray(B));
192: PetscCall(VecDeviceRestoreArray(v, &vv));
194: if (test == 1) {
195: PetscCall(MatDenseGetArrayAndMemType(B, &aa, NULL));
196: PetscCheck(!aa, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected a null pointer");
197: PetscCall(MatDenseRestoreArrayAndMemType(B, &aa));
198: }
200: /* free work space */
201: PetscCall(MatDestroy(&B));
202: PetscCall(MatDestroy(&A));
203: PetscCall(VecDestroy(&t));
204: PetscCall(VecDestroy(&v));
205: PetscCall(PetscFinalize());
206: return 0;
207: }
209: /*TEST
211: build:
212: requires: defined(PETSC_DEVICELANGUAGE_CXX)
214: testset:
215: nsize: {{1 2}}
216: args: -test {{0 1 2}} -k 6 -l {{0 5}} -use_shell {{0 1}}
217: output_file: output/empty.out
218: test:
219: requires: cuda
220: suffix: 1_cuda
221: args: -A_mat_type {{aij aijcusparse}}
222: test:
223: requires: hip
224: suffix: 1_hip
225: args: -A_mat_type aijhipsparse
227: TEST*/