1 #pragma once 2 3 #include <petsc/private/matdensecupmimpl.h> /*I <petscmat.h> I*/ 4 #include <../src/mat/impls/dense/seq/dense.h> 5 6 #include <petsc/private/deviceimpl.h> // PetscDeviceContextGetOptionalNullContext_Internal() 7 #include <petsc/private/randomimpl.h> // _p_PetscRandom 8 #include <petsc/private/vecimpl.h> // _p_Vec 9 #include <petsc/private/cupmobject.hpp> 10 #include <petsc/private/cupmsolverinterface.hpp> 11 12 #include <petsc/private/cpp/type_traits.hpp> // PetscObjectCast() 13 #include <petsc/private/cpp/utility.hpp> // util::exchange() 14 15 #include <../src/vec/vec/impls/seq/cupm/vecseqcupm.hpp> // for VecSeq_CUPM 16 17 namespace Petsc 18 { 19 20 namespace mat 21 { 22 23 namespace cupm 24 { 25 26 namespace impl 27 { 28 29 template <device::cupm::DeviceType T> 30 class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL MatDense_Seq_CUPM : MatDense_CUPM<T, MatDense_Seq_CUPM<T>> { 31 public: 32 MATDENSECUPM_HEADER(T, MatDense_Seq_CUPM<T>); 33 34 private: 35 struct Mat_SeqDenseCUPM { 36 PetscScalar *d_v; // pointer to the matrix on the GPU 37 PetscScalar *unplacedarray; // if one called MatCUPMDensePlaceArray(), this is where it stashed the original 38 bool d_user_alloc; 39 bool d_unplaced_user_alloc; 40 // factorization support 41 cupmBlasInt_t *d_fact_ipiv; // device pivots 42 cupmScalar_t *d_fact_tau; // device QR tau vector 43 cupmBlasInt_t *d_fact_info; // device info 44 cupmScalar_t *d_fact_work; // device workspace 45 cupmBlasInt_t d_fact_lwork; // size of device workspace 46 // workspace 47 Vec workvec; 48 }; 49 50 static PetscErrorCode SetPreallocation_(Mat, PetscDeviceContext, PetscScalar *) noexcept; 51 52 static PetscErrorCode HostToDevice_(Mat, PetscDeviceContext) noexcept; 53 static PetscErrorCode DeviceToHost_(Mat, PetscDeviceContext) noexcept; 54 55 static PetscErrorCode CheckCUPMSolverInfo_(const cupmBlasInt_t *, cupmStream_t) noexcept; 56 57 template <typename Derived> 58 struct SolveCommon; 59 struct SolveQR; 60 struct SolveCholesky; 61 struct SolveLU; 62 63 template <typename Solver, bool transpose> 64 static PetscErrorCode MatSolve_Factored_Dispatch_(Mat, Vec, Vec) noexcept; 65 template <typename Solver, bool transpose> 66 static PetscErrorCode MatMatSolve_Factored_Dispatch_(Mat, Mat, Mat) noexcept; 67 template <bool transpose, bool hermitian> 68 static PetscErrorCode MatMultAddColumnRange_Dispatch_(Mat, Vec, Vec, Vec, PetscInt, PetscInt) noexcept; 69 template <bool transpose, bool hermitian> 70 static PetscErrorCode MatMultColumnRange_Dispatch_(Mat, Vec, Vec, PetscInt, PetscInt) noexcept; 71 template <bool transpose, bool hermitian> 72 static PetscErrorCode MatMultAdd_Dispatch_(Mat, Vec, Vec, Vec) noexcept; 73 74 template <bool to_host> 75 static PetscErrorCode Convert_Dispatch_(Mat, MatType, MatReuse, Mat *) noexcept; 76 77 PETSC_NODISCARD static constexpr MatType MATIMPLCUPM_() noexcept; 78 PETSC_NODISCARD static constexpr Mat_SeqDense *MatIMPLCast_(Mat) noexcept; 79 80 public: 81 PETSC_NODISCARD static constexpr Mat_SeqDenseCUPM *MatCUPMCast(Mat) noexcept; 82 83 // define these by hand since they don't fit the above mold 84 PETSC_NODISCARD static constexpr const char *MatConvert_seqdensecupm_seqdense_C() noexcept; 85 PETSC_NODISCARD static constexpr const char *MatProductSetFromOptions_seqaij_seqdensecupm_C() noexcept; 86 87 static PetscErrorCode Create(Mat) noexcept; 88 static PetscErrorCode Destroy(Mat) noexcept; 89 static PetscErrorCode SetUp(Mat) noexcept; 90 static PetscErrorCode Reset(Mat) noexcept; 91 92 static PetscErrorCode BindToCPU(Mat, PetscBool) noexcept; 93 static PetscErrorCode Convert_SeqDense_SeqDenseCUPM(Mat, MatType, MatReuse, Mat *) noexcept; 94 static PetscErrorCode Convert_SeqDenseCUPM_SeqDense(Mat, MatType, MatReuse, Mat *) noexcept; 95 96 template <PetscMemType, PetscMemoryAccessMode> 97 static PetscErrorCode GetArray(Mat, PetscScalar **, PetscDeviceContext) noexcept; 98 template <PetscMemType, PetscMemoryAccessMode> 99 static PetscErrorCode RestoreArray(Mat, PetscScalar **, PetscDeviceContext) noexcept; 100 template <PetscMemoryAccessMode> 101 static PetscErrorCode GetArrayAndMemType(Mat, PetscScalar **, PetscMemType *, PetscDeviceContext) noexcept; 102 template <PetscMemoryAccessMode> 103 static PetscErrorCode RestoreArrayAndMemType(Mat, PetscScalar **, PetscDeviceContext) noexcept; 104 105 private: 106 template <PetscMemType mtype, PetscMemoryAccessMode mode> 107 static PetscErrorCode GetArrayC_(Mat m, PetscScalar **p) noexcept 108 { 109 PetscDeviceContext dctx; 110 111 PetscFunctionBegin; 112 PetscCall(GetHandles_(&dctx)); 113 PetscCall(GetArray<mtype, mode>(m, p, dctx)); 114 PetscFunctionReturn(PETSC_SUCCESS); 115 } 116 117 template <PetscMemType mtype, PetscMemoryAccessMode mode> 118 static PetscErrorCode RestoreArrayC_(Mat m, PetscScalar **p) noexcept 119 { 120 PetscDeviceContext dctx; 121 122 PetscFunctionBegin; 123 PetscCall(GetHandles_(&dctx)); 124 PetscCall(RestoreArray<mtype, mode>(m, p, dctx)); 125 PetscFunctionReturn(PETSC_SUCCESS); 126 } 127 128 template <PetscMemoryAccessMode mode> 129 static PetscErrorCode GetArrayAndMemTypeC_(Mat m, PetscScalar **p, PetscMemType *tp) noexcept 130 { 131 PetscDeviceContext dctx; 132 133 PetscFunctionBegin; 134 PetscCall(GetHandles_(&dctx)); 135 PetscCall(GetArrayAndMemType<mode>(m, p, tp, dctx)); 136 PetscFunctionReturn(PETSC_SUCCESS); 137 } 138 139 template <PetscMemoryAccessMode mode> 140 static PetscErrorCode RestoreArrayAndMemTypeC_(Mat m, PetscScalar **p) noexcept 141 { 142 PetscDeviceContext dctx; 143 144 PetscFunctionBegin; 145 PetscCall(GetHandles_(&dctx)); 146 PetscCall(RestoreArrayAndMemType<mode>(m, p, dctx)); 147 PetscFunctionReturn(PETSC_SUCCESS); 148 } 149 150 public: 151 static PetscErrorCode PlaceArray(Mat, const PetscScalar *) noexcept; 152 static PetscErrorCode ReplaceArray(Mat, const PetscScalar *) noexcept; 153 static PetscErrorCode ResetArray(Mat) noexcept; 154 155 template <bool transpose_A, bool transpose_B> 156 static PetscErrorCode MatMatMult_Numeric_Dispatch(Mat, Mat, Mat) noexcept; 157 static PetscErrorCode Copy(Mat, Mat, MatStructure) noexcept; 158 static PetscErrorCode ZeroEntries(Mat) noexcept; 159 static PetscErrorCode Conjugate(Mat) noexcept; 160 static PetscErrorCode Scale(Mat, PetscScalar) noexcept; 161 static PetscErrorCode AXPY(Mat, PetscScalar, Mat, MatStructure) noexcept; 162 static PetscErrorCode Duplicate(Mat, MatDuplicateOption, Mat *) noexcept; 163 static PetscErrorCode SetRandom(Mat, PetscRandom) noexcept; 164 165 static PetscErrorCode GetColumnVector(Mat, Vec, PetscInt) noexcept; 166 template <PetscMemoryAccessMode> 167 static PetscErrorCode GetColumnVec(Mat, PetscInt, Vec *) noexcept; 168 template <PetscMemoryAccessMode> 169 static PetscErrorCode RestoreColumnVec(Mat, PetscInt, Vec *) noexcept; 170 171 static PetscErrorCode GetFactor(Mat, MatFactorType, Mat *) noexcept; 172 static PetscErrorCode InvertFactors(Mat) noexcept; 173 174 static PetscErrorCode GetSubMatrix(Mat, PetscInt, PetscInt, PetscInt, PetscInt, Mat *) noexcept; 175 static PetscErrorCode RestoreSubMatrix(Mat, Mat *) noexcept; 176 }; 177 178 } // namespace impl 179 180 namespace 181 { 182 183 // Declare this here so that the functions below can make use of it 184 template <device::cupm::DeviceType T> 185 inline PetscErrorCode MatCreateSeqDenseCUPM(MPI_Comm comm, PetscInt m, PetscInt n, PetscScalar *data, Mat *A, PetscDeviceContext dctx = nullptr, bool preallocate = true) noexcept 186 { 187 PetscFunctionBegin; 188 PetscCall(impl::MatDense_Seq_CUPM<T>::CreateIMPLDenseCUPM(comm, m, n, m, n, data, A, dctx, preallocate)); 189 PetscFunctionReturn(PETSC_SUCCESS); 190 } 191 192 } // anonymous namespace 193 194 namespace impl 195 { 196 197 // ========================================================================================== 198 // MatDense_Seq_CUPM - Private API - Utility 199 // ========================================================================================== 200 201 template <device::cupm::DeviceType T> 202 inline PetscErrorCode MatDense_Seq_CUPM<T>::SetPreallocation_(Mat m, PetscDeviceContext dctx, PetscScalar *user_device_array) noexcept 203 { 204 const auto mcu = MatCUPMCast(m); 205 const auto nrows = m->rmap->n; 206 const auto ncols = m->cmap->n; 207 auto &lda = MatIMPLCast(m)->lda; 208 cupmStream_t stream; 209 210 PetscFunctionBegin; 211 PetscCheckTypeName(m, MATSEQDENSECUPM()); 212 PetscValidDeviceContext(dctx, 2); 213 PetscCall(checkCupmBlasIntCast(nrows)); 214 PetscCall(checkCupmBlasIntCast(ncols)); 215 PetscCall(GetHandlesFrom_(dctx, &stream)); 216 if (lda <= 0) lda = nrows; 217 if (!mcu->d_user_alloc) PetscCallCUPM(cupmFreeAsync(mcu->d_v, stream)); 218 if (user_device_array) { 219 mcu->d_user_alloc = PETSC_TRUE; 220 mcu->d_v = user_device_array; 221 } else { 222 std::size_t size; 223 224 mcu->d_user_alloc = PETSC_FALSE; 225 size = lda * ncols; 226 PetscCall(PetscCUPMMallocAsync(&mcu->d_v, size, stream)); 227 PetscCall(PetscCUPMMemsetAsync(mcu->d_v, 0, size, stream)); 228 } 229 m->offloadmask = PETSC_OFFLOAD_GPU; 230 PetscFunctionReturn(PETSC_SUCCESS); 231 } 232 233 template <device::cupm::DeviceType T> 234 inline PetscErrorCode MatDense_Seq_CUPM<T>::HostToDevice_(Mat m, PetscDeviceContext dctx) noexcept 235 { 236 const auto nrows = m->rmap->n; 237 const auto ncols = m->cmap->n; 238 const auto copy = m->offloadmask == PETSC_OFFLOAD_CPU || m->offloadmask == PETSC_OFFLOAD_UNALLOCATED; 239 240 PetscFunctionBegin; 241 PetscCheckTypeName(m, MATSEQDENSECUPM()); 242 if (m->boundtocpu) PetscFunctionReturn(PETSC_SUCCESS); 243 PetscCall(PetscInfo(m, "%s matrix %" PetscInt_FMT " x %" PetscInt_FMT "\n", copy ? "Copy" : "Reusing", nrows, ncols)); 244 if (copy) { 245 const auto mcu = MatCUPMCast(m); 246 cupmStream_t stream; 247 248 // Allocate GPU memory if not present 249 if (!mcu->d_v) PetscCall(SetPreallocation(m, dctx, nullptr)); 250 PetscCall(GetHandlesFrom_(dctx, &stream)); 251 PetscCall(PetscLogEventBegin(MAT_DenseCopyToGPU, m, 0, 0, 0)); 252 { 253 const auto mimpl = MatIMPLCast(m); 254 const auto lda = mimpl->lda; 255 const auto src = mimpl->v; 256 const auto dest = mcu->d_v; 257 258 if (lda > nrows) { 259 PetscCall(PetscCUPMMemcpy2DAsync(dest, lda, src, lda, nrows, ncols, cupmMemcpyHostToDevice, stream)); 260 } else { 261 PetscCall(PetscCUPMMemcpyAsync(dest, src, lda * ncols, cupmMemcpyHostToDevice, stream)); 262 } 263 } 264 PetscCall(PetscLogEventEnd(MAT_DenseCopyToGPU, m, 0, 0, 0)); 265 // order important, ensure that offloadmask is PETSC_OFFLOAD_BOTH 266 m->offloadmask = PETSC_OFFLOAD_BOTH; 267 } 268 PetscFunctionReturn(PETSC_SUCCESS); 269 } 270 271 template <device::cupm::DeviceType T> 272 inline PetscErrorCode MatDense_Seq_CUPM<T>::DeviceToHost_(Mat m, PetscDeviceContext dctx) noexcept 273 { 274 const auto nrows = m->rmap->n; 275 const auto ncols = m->cmap->n; 276 const auto copy = m->offloadmask == PETSC_OFFLOAD_GPU; 277 278 PetscFunctionBegin; 279 PetscCheckTypeName(m, MATSEQDENSECUPM()); 280 PetscCall(PetscInfo(m, "%s matrix %" PetscInt_FMT " x %" PetscInt_FMT "\n", copy ? "Copy" : "Reusing", nrows, ncols)); 281 if (copy) { 282 const auto mimpl = MatIMPLCast(m); 283 cupmStream_t stream; 284 285 // MatCreateSeqDenseCUPM may not allocate CPU memory. Allocate if needed 286 if (!mimpl->v) PetscCall(MatSeqDenseSetPreallocation(m, nullptr)); 287 PetscCall(GetHandlesFrom_(dctx, &stream)); 288 PetscCall(PetscLogEventBegin(MAT_DenseCopyFromGPU, m, 0, 0, 0)); 289 { 290 const auto lda = mimpl->lda; 291 const auto dest = mimpl->v; 292 const auto src = MatCUPMCast(m)->d_v; 293 294 if (lda > nrows) { 295 PetscCall(PetscCUPMMemcpy2DAsync(dest, lda, src, lda, nrows, ncols, cupmMemcpyDeviceToHost, stream)); 296 } else { 297 PetscCall(PetscCUPMMemcpyAsync(dest, src, lda * ncols, cupmMemcpyDeviceToHost, stream)); 298 } 299 } 300 PetscCall(PetscLogEventEnd(MAT_DenseCopyFromGPU, m, 0, 0, 0)); 301 // order is important, MatSeqDenseSetPreallocation() might set offloadmask 302 m->offloadmask = PETSC_OFFLOAD_BOTH; 303 } 304 PetscFunctionReturn(PETSC_SUCCESS); 305 } 306 307 template <device::cupm::DeviceType T> 308 inline PetscErrorCode MatDense_Seq_CUPM<T>::CheckCUPMSolverInfo_(const cupmBlasInt_t *fact_info, cupmStream_t stream) noexcept 309 { 310 PetscFunctionBegin; 311 if (PetscDefined(USE_DEBUG)) { 312 cupmBlasInt_t info = 0; 313 314 PetscCall(PetscCUPMMemcpyAsync(&info, fact_info, 1, cupmMemcpyDeviceToHost, stream)); 315 if (stream) PetscCallCUPM(cupmStreamSynchronize(stream)); 316 static_assert(std::is_same<decltype(info), int>::value, ""); 317 PetscCheck(info <= 0, PETSC_COMM_SELF, PETSC_ERR_MAT_CH_ZRPVT, "Bad factorization: zero pivot in row %d", info - 1); 318 PetscCheck(info >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Wrong argument to cupmSolver %d", -info); 319 } 320 PetscFunctionReturn(PETSC_SUCCESS); 321 } 322 323 // ========================================================================================== 324 // MatDense_Seq_CUPM - Private API - Solver Dispatch 325 // ========================================================================================== 326 327 // specific solvers called through the dispatch_() family of functions 328 template <device::cupm::DeviceType T> 329 template <typename Derived> 330 struct MatDense_Seq_CUPM<T>::SolveCommon { 331 using derived_type = Derived; 332 333 template <typename F> 334 static PetscErrorCode ResizeFactLwork(Mat_SeqDenseCUPM *mcu, cupmStream_t stream, F &&cupmSolverComputeFactLwork) noexcept 335 { 336 cupmBlasInt_t lwork; 337 338 PetscFunctionBegin; 339 PetscCallCUPMSOLVER(cupmSolverComputeFactLwork(&lwork)); 340 if (lwork > mcu->d_fact_lwork) { 341 mcu->d_fact_lwork = lwork; 342 PetscCallCUPM(cupmFreeAsync(mcu->d_fact_work, stream)); 343 PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_work, lwork, stream)); 344 } 345 PetscFunctionReturn(PETSC_SUCCESS); 346 } 347 348 static PetscErrorCode FactorPrepare(Mat A, cupmStream_t stream) noexcept 349 { 350 const auto mcu = MatCUPMCast(A); 351 352 PetscFunctionBegin; 353 PetscCall(PetscInfo(A, "%s factor %" PetscInt_FMT " x %" PetscInt_FMT " on backend\n", derived_type::NAME(), A->rmap->n, A->cmap->n)); 354 A->factortype = derived_type::MATFACTORTYPE(); 355 A->ops->solve = MatSolve_Factored_Dispatch_<derived_type, false>; 356 A->ops->solvetranspose = MatSolve_Factored_Dispatch_<derived_type, true>; 357 A->ops->matsolve = MatMatSolve_Factored_Dispatch_<derived_type, false>; 358 A->ops->matsolvetranspose = MatMatSolve_Factored_Dispatch_<derived_type, true>; 359 360 PetscCall(PetscStrFreeAllocpy(MATSOLVERCUPM(), &A->solvertype)); 361 if (!mcu->d_fact_info) PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_info, 1, stream)); 362 PetscFunctionReturn(PETSC_SUCCESS); 363 } 364 }; 365 366 template <device::cupm::DeviceType T> 367 struct MatDense_Seq_CUPM<T>::SolveLU : SolveCommon<SolveLU> { 368 using base_type = SolveCommon<SolveLU>; 369 370 static constexpr const char *NAME() noexcept { return "LU"; } 371 static constexpr MatFactorType MATFACTORTYPE() noexcept { return MAT_FACTOR_LU; } 372 373 static PetscErrorCode Factor(Mat A, IS, IS, const MatFactorInfo *) noexcept 374 { 375 const auto m = static_cast<cupmBlasInt_t>(A->rmap->n); 376 const auto n = static_cast<cupmBlasInt_t>(A->cmap->n); 377 cupmStream_t stream; 378 cupmSolverHandle_t handle; 379 PetscDeviceContext dctx; 380 381 PetscFunctionBegin; 382 if (!m || !n) PetscFunctionReturn(PETSC_SUCCESS); 383 PetscCall(GetHandles_(&dctx, &handle, &stream)); 384 PetscCall(base_type::FactorPrepare(A, stream)); 385 { 386 const auto mcu = MatCUPMCast(A); 387 const auto da = DeviceArrayReadWrite(dctx, A); 388 const auto lda = static_cast<cupmBlasInt_t>(MatIMPLCast(A)->lda); 389 390 // clang-format off 391 PetscCall( 392 base_type::ResizeFactLwork( 393 mcu, stream, 394 [&](cupmBlasInt_t *fact_lwork) 395 { 396 return cupmSolverXgetrf_bufferSize(handle, m, n, da.cupmdata(), lda, fact_lwork); 397 } 398 ) 399 ); 400 // clang-format on 401 if (!mcu->d_fact_ipiv) PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_ipiv, n, stream)); 402 403 PetscCall(PetscLogGpuTimeBegin()); 404 PetscCallCUPMSOLVER(cupmSolverXgetrf(handle, m, n, da.cupmdata(), lda, mcu->d_fact_work, mcu->d_fact_lwork, mcu->d_fact_ipiv, mcu->d_fact_info)); 405 PetscCall(PetscLogGpuTimeEnd()); 406 PetscCall(CheckCUPMSolverInfo_(mcu->d_fact_info, stream)); 407 } 408 PetscCall(PetscLogGpuFlops(2.0 * n * n * m / 3.0)); 409 PetscFunctionReturn(PETSC_SUCCESS); 410 } 411 412 template <bool transpose> 413 static PetscErrorCode Solve(Mat A, cupmScalar_t *x, cupmBlasInt_t ldx, cupmBlasInt_t m, cupmBlasInt_t nrhs, cupmBlasInt_t k, PetscDeviceContext dctx, cupmStream_t stream) noexcept 414 { 415 const auto mcu = MatCUPMCast(A); 416 const auto fact_info = mcu->d_fact_info; 417 const auto fact_ipiv = mcu->d_fact_ipiv; 418 cupmSolverHandle_t handle; 419 420 PetscFunctionBegin; 421 PetscCall(GetHandlesFrom_(dctx, &handle)); 422 PetscCall(PetscInfo(A, "%s solve %d x %d on backend\n", NAME(), m, k)); 423 PetscCall(PetscLogGpuTimeBegin()); 424 { 425 constexpr auto op = transpose ? CUPMSOLVER_OP_T : CUPMSOLVER_OP_N; 426 const auto da = DeviceArrayRead(dctx, A); 427 const auto lda = static_cast<cupmBlasInt_t>(MatIMPLCast(A)->lda); 428 429 // clang-format off 430 PetscCall( 431 base_type::ResizeFactLwork( 432 mcu, stream, 433 [&](cupmBlasInt_t *lwork) 434 { 435 return cupmSolverXgetrs_bufferSize( 436 handle, op, m, nrhs, da.cupmdata(), lda, fact_ipiv, x, ldx, lwork 437 ); 438 } 439 ) 440 ); 441 // clang-format on 442 PetscCallCUPMSOLVER(cupmSolverXgetrs(handle, op, m, nrhs, da.cupmdata(), lda, fact_ipiv, x, ldx, mcu->d_fact_work, mcu->d_fact_lwork, fact_info)); 443 PetscCall(CheckCUPMSolverInfo_(fact_info, stream)); 444 } 445 PetscCall(PetscLogGpuTimeEnd()); 446 PetscCall(PetscLogGpuFlops(nrhs * (2.0 * m * m - m))); 447 PetscFunctionReturn(PETSC_SUCCESS); 448 } 449 }; 450 451 template <device::cupm::DeviceType T> 452 struct MatDense_Seq_CUPM<T>::SolveCholesky : SolveCommon<SolveCholesky> { 453 using base_type = SolveCommon<SolveCholesky>; 454 455 static constexpr const char *NAME() noexcept { return "Cholesky"; } 456 static constexpr MatFactorType MATFACTORTYPE() noexcept { return MAT_FACTOR_CHOLESKY; } 457 458 static PetscErrorCode Factor(Mat A, IS, const MatFactorInfo *) noexcept 459 { 460 const auto n = static_cast<cupmBlasInt_t>(A->rmap->n); 461 PetscDeviceContext dctx; 462 cupmSolverHandle_t handle; 463 cupmStream_t stream; 464 465 PetscFunctionBegin; 466 if (!n || !A->cmap->n) PetscFunctionReturn(PETSC_SUCCESS); 467 PetscCheck(A->spd == PETSC_BOOL3_TRUE, PETSC_COMM_SELF, PETSC_ERR_SUP, "%ssytrs unavailable. Use MAT_FACTOR_LU", cupmSolverName()); 468 PetscCall(GetHandles_(&dctx, &handle, &stream)); 469 PetscCall(base_type::FactorPrepare(A, stream)); 470 { 471 const auto mcu = MatCUPMCast(A); 472 const auto da = DeviceArrayReadWrite(dctx, A); 473 const auto lda = static_cast<cupmBlasInt_t>(MatIMPLCast(A)->lda); 474 475 // clang-format off 476 PetscCall( 477 base_type::ResizeFactLwork( 478 mcu, stream, 479 [&](cupmBlasInt_t *fact_lwork) 480 { 481 return cupmSolverXpotrf_bufferSize( 482 handle, CUPMSOLVER_FILL_MODE_LOWER, n, da.cupmdata(), lda, fact_lwork 483 ); 484 } 485 ) 486 ); 487 // clang-format on 488 PetscCall(PetscLogGpuTimeBegin()); 489 PetscCallCUPMSOLVER(cupmSolverXpotrf(handle, CUPMSOLVER_FILL_MODE_LOWER, n, da.cupmdata(), lda, mcu->d_fact_work, mcu->d_fact_lwork, mcu->d_fact_info)); 490 PetscCall(PetscLogGpuTimeEnd()); 491 PetscCall(CheckCUPMSolverInfo_(mcu->d_fact_info, stream)); 492 } 493 PetscCall(PetscLogGpuFlops(1.0 * n * n * n / 3.0)); 494 495 #if 0 496 // At the time of writing this interface (cuda 10.0), cusolverDn does not implement *sytrs 497 // and *hetr* routines. The code below should work, and it can be activated when *sytrs 498 // routines will be available 499 if (!mcu->d_fact_ipiv) PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_ipiv, n, stream)); 500 if (!mcu->d_fact_lwork) { 501 PetscCallCUPMSOLVER(cupmSolverDnXsytrf_bufferSize(handle, n, da.cupmdata(), lda, &mcu->d_fact_lwork)); 502 PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_work, mcu->d_fact_lwork, stream)); 503 } 504 if (mcu->d_fact_info) PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_info, 1, stream)); 505 PetscCall(PetscLogGpuTimeBegin()); 506 PetscCallCUPMSOLVER(cupmSolverXsytrf(handle, CUPMSOLVER_FILL_MODE_LOWER, n, da, lda, mcu->d_fact_ipiv, mcu->d_fact_work, mcu->d_fact_lwork, mcu->d_fact_info)); 507 PetscCall(PetscLogGpuTimeEnd()); 508 #endif 509 PetscFunctionReturn(PETSC_SUCCESS); 510 } 511 512 template <bool transpose> 513 static PetscErrorCode Solve(Mat A, cupmScalar_t *x, cupmBlasInt_t ldx, cupmBlasInt_t m, cupmBlasInt_t nrhs, cupmBlasInt_t k, PetscDeviceContext dctx, cupmStream_t stream) noexcept 514 { 515 const auto mcu = MatCUPMCast(A); 516 const auto fact_info = mcu->d_fact_info; 517 cupmSolverHandle_t handle; 518 519 PetscFunctionBegin; 520 PetscAssert(!mcu->d_fact_ipiv, PETSC_COMM_SELF, PETSC_ERR_LIB, "%ssytrs not implemented", cupmSolverName()); 521 PetscCall(GetHandlesFrom_(dctx, &handle)); 522 PetscCall(PetscInfo(A, "%s solve %d x %d on backend\n", NAME(), m, k)); 523 PetscCall(PetscLogGpuTimeBegin()); 524 { 525 const auto da = DeviceArrayRead(dctx, A); 526 const auto lda = static_cast<cupmBlasInt_t>(MatIMPLCast(A)->lda); 527 528 // clang-format off 529 PetscCall( 530 base_type::ResizeFactLwork( 531 mcu, stream, 532 [&](cupmBlasInt_t *lwork) 533 { 534 return cupmSolverXpotrs_bufferSize( 535 handle, CUPMSOLVER_FILL_MODE_LOWER, m, nrhs, da.cupmdata(), lda, x, ldx, lwork 536 ); 537 } 538 ) 539 ); 540 // clang-format on 541 PetscCallCUPMSOLVER(cupmSolverXpotrs(handle, CUPMSOLVER_FILL_MODE_LOWER, m, nrhs, da.cupmdata(), lda, x, ldx, mcu->d_fact_work, mcu->d_fact_lwork, fact_info)); 542 PetscCall(CheckCUPMSolverInfo_(fact_info, stream)); 543 } 544 PetscCall(PetscLogGpuTimeEnd()); 545 PetscCall(PetscLogGpuFlops(nrhs * (2.0 * m * m - m))); 546 PetscFunctionReturn(PETSC_SUCCESS); 547 } 548 }; 549 550 template <device::cupm::DeviceType T> 551 struct MatDense_Seq_CUPM<T>::SolveQR : SolveCommon<SolveQR> { 552 using base_type = SolveCommon<SolveQR>; 553 554 static constexpr const char *NAME() noexcept { return "QR"; } 555 static constexpr MatFactorType MATFACTORTYPE() noexcept { return MAT_FACTOR_QR; } 556 557 static PetscErrorCode Factor(Mat A, IS, const MatFactorInfo *) noexcept 558 { 559 const auto m = static_cast<cupmBlasInt_t>(A->rmap->n); 560 const auto n = static_cast<cupmBlasInt_t>(A->cmap->n); 561 const auto min = std::min(m, n); 562 const auto mimpl = MatIMPLCast(A); 563 cupmStream_t stream; 564 cupmSolverHandle_t handle; 565 PetscDeviceContext dctx; 566 567 PetscFunctionBegin; 568 if (!m || !n) PetscFunctionReturn(PETSC_SUCCESS); 569 PetscCall(GetHandles_(&dctx, &handle, &stream)); 570 PetscCall(base_type::FactorPrepare(A, stream)); 571 mimpl->rank = min; 572 { 573 const auto mcu = MatCUPMCast(A); 574 const auto da = DeviceArrayReadWrite(dctx, A); 575 const auto lda = static_cast<cupmBlasInt_t>(mimpl->lda); 576 577 if (!mcu->workvec) PetscCall(vec::cupm::VecCreateSeqCUPMAsync<T>(PetscObjectComm(PetscObjectCast(A)), m, &mcu->workvec)); 578 if (!mcu->d_fact_tau) PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_tau, min, stream)); 579 // clang-format off 580 PetscCall( 581 base_type::ResizeFactLwork( 582 mcu, stream, 583 [&](cupmBlasInt_t *fact_lwork) 584 { 585 return cupmSolverXgeqrf_bufferSize(handle, m, n, da.cupmdata(), lda, fact_lwork); 586 } 587 ) 588 ); 589 // clang-format on 590 PetscCall(PetscLogGpuTimeBegin()); 591 PetscCallCUPMSOLVER(cupmSolverXgeqrf(handle, m, n, da.cupmdata(), lda, mcu->d_fact_tau, mcu->d_fact_work, mcu->d_fact_lwork, mcu->d_fact_info)); 592 PetscCall(PetscLogGpuTimeEnd()); 593 PetscCall(CheckCUPMSolverInfo_(mcu->d_fact_info, stream)); 594 } 595 PetscCall(PetscLogGpuFlops(2.0 * min * min * (std::max(m, n) - min / 3.0))); 596 PetscFunctionReturn(PETSC_SUCCESS); 597 } 598 599 template <bool transpose> 600 static PetscErrorCode Solve(Mat A, cupmScalar_t *x, cupmBlasInt_t ldx, cupmBlasInt_t m, cupmBlasInt_t nrhs, cupmBlasInt_t k, PetscDeviceContext dctx, cupmStream_t stream) noexcept 601 { 602 const auto mimpl = MatIMPLCast(A); 603 const auto rank = static_cast<cupmBlasInt_t>(mimpl->rank); 604 const auto mcu = MatCUPMCast(A); 605 const auto fact_info = mcu->d_fact_info; 606 const auto fact_tau = mcu->d_fact_tau; 607 const auto fact_work = mcu->d_fact_work; 608 const auto fact_lwork = mcu->d_fact_lwork; 609 cupmSolverHandle_t solver_handle; 610 cupmBlasHandle_t blas_handle; 611 612 PetscFunctionBegin; 613 PetscCall(GetHandlesFrom_(dctx, &blas_handle, &solver_handle)); 614 PetscCall(PetscInfo(A, "%s solve %d x %d on backend\n", NAME(), m, k)); 615 PetscCall(PetscLogGpuTimeBegin()); 616 { 617 const auto da = DeviceArrayRead(dctx, A); 618 const auto one = cupmScalarCast(1.0); 619 const auto lda = static_cast<cupmBlasInt_t>(mimpl->lda); 620 621 if (transpose) { 622 PetscCallCUPMBLAS(cupmBlasXtrsm(blas_handle, CUPMBLAS_SIDE_LEFT, CUPMBLAS_FILL_MODE_UPPER, CUPMBLAS_OP_T, CUPMBLAS_DIAG_NON_UNIT, rank, nrhs, &one, da.cupmdata(), lda, x, ldx)); 623 PetscCallCUPMSOLVER(cupmSolverXormqr(solver_handle, CUPMSOLVER_SIDE_LEFT, CUPMSOLVER_OP_N, m, nrhs, rank, da.cupmdata(), lda, fact_tau, x, ldx, fact_work, fact_lwork, fact_info)); 624 PetscCall(CheckCUPMSolverInfo_(fact_info, stream)); 625 } else { 626 constexpr auto op = PetscDefined(USE_COMPLEX) ? CUPMSOLVER_OP_C : CUPMSOLVER_OP_T; 627 628 PetscCallCUPMSOLVER(cupmSolverXormqr(solver_handle, CUPMSOLVER_SIDE_LEFT, op, m, nrhs, rank, da.cupmdata(), lda, fact_tau, x, ldx, fact_work, fact_lwork, fact_info)); 629 PetscCall(CheckCUPMSolverInfo_(fact_info, stream)); 630 PetscCallCUPMBLAS(cupmBlasXtrsm(blas_handle, CUPMBLAS_SIDE_LEFT, CUPMBLAS_FILL_MODE_UPPER, CUPMBLAS_OP_N, CUPMBLAS_DIAG_NON_UNIT, rank, nrhs, &one, da.cupmdata(), lda, x, ldx)); 631 } 632 } 633 PetscCall(PetscLogGpuTimeEnd()); 634 PetscCall(PetscLogFlops(nrhs * (4.0 * m * rank - (rank * rank)))); 635 PetscFunctionReturn(PETSC_SUCCESS); 636 } 637 }; 638 639 template <device::cupm::DeviceType T> 640 template <typename Solver, bool transpose> 641 inline PetscErrorCode MatDense_Seq_CUPM<T>::MatSolve_Factored_Dispatch_(Mat A, Vec x, Vec y) noexcept 642 { 643 using namespace vec::cupm; 644 const auto pobj_A = PetscObjectCast(A); 645 const auto m = static_cast<cupmBlasInt_t>(A->rmap->n); 646 const auto k = static_cast<cupmBlasInt_t>(A->cmap->n); 647 auto &workvec = MatCUPMCast(A)->workvec; 648 PetscScalar *y_array = nullptr; 649 PetscDeviceContext dctx; 650 PetscBool xiscupm, yiscupm, aiscupm; 651 bool use_y_array_directly; 652 cupmStream_t stream; 653 654 PetscFunctionBegin; 655 PetscCheck(A->factortype != MAT_FACTOR_NONE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Matrix must be factored to solve"); 656 PetscCall(PetscObjectTypeCompare(PetscObjectCast(x), VecSeq_CUPM::VECSEQCUPM(), &xiscupm)); 657 PetscCall(PetscObjectTypeCompare(PetscObjectCast(y), VecSeq_CUPM::VECSEQCUPM(), &yiscupm)); 658 PetscCall(PetscObjectTypeCompare(pobj_A, MATSEQDENSECUPM(), &aiscupm)); 659 PetscAssert(aiscupm, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Matrix A is somehow not CUPM?????????????????????????????"); 660 PetscCall(GetHandles_(&dctx, &stream)); 661 use_y_array_directly = yiscupm && (k >= m); 662 { 663 const PetscScalar *x_array; 664 const auto xisdevice = xiscupm && PetscOffloadDevice(x->offloadmask); 665 const auto copy_mode = xisdevice ? cupmMemcpyDeviceToDevice : cupmMemcpyHostToDevice; 666 667 if (!use_y_array_directly && !workvec) PetscCall(VecCreateSeqCUPMAsync<T>(PetscObjectComm(pobj_A), m, &workvec)); 668 // The logic here is to try to minimize the amount of memory copying: 669 // 670 // If we call VecCUPMGetArrayRead(X, &x) every time xiscupm and the data is not offloaded 671 // to the GPU yet, then the data is copied to the GPU. But we are only trying to get the 672 // data in order to copy it into the y array. So the array x will be wherever the data 673 // already is so that only one memcpy is performed 674 if (xisdevice) { 675 PetscCall(VecCUPMGetArrayReadAsync<T>(x, &x_array, dctx)); 676 } else { 677 PetscCall(VecGetArrayRead(x, &x_array)); 678 } 679 PetscCall(VecCUPMGetArrayWriteAsync<T>(use_y_array_directly ? y : workvec, &y_array, dctx)); 680 PetscCall(PetscCUPMMemcpyAsync(y_array, x_array, m, copy_mode, stream)); 681 if (xisdevice) { 682 PetscCall(VecCUPMRestoreArrayReadAsync<T>(x, &x_array, dctx)); 683 } else { 684 PetscCall(VecRestoreArrayRead(x, &x_array)); 685 } 686 } 687 688 if (!aiscupm) PetscCall(MatConvert(A, MATSEQDENSECUPM(), MAT_INPLACE_MATRIX, &A)); 689 PetscCall(Solver{}.template Solve<transpose>(A, cupmScalarPtrCast(y_array), m, m, 1, k, dctx, stream)); 690 if (!aiscupm) PetscCall(MatConvert(A, MATSEQDENSE, MAT_INPLACE_MATRIX, &A)); 691 692 if (use_y_array_directly) { 693 PetscCall(VecCUPMRestoreArrayWriteAsync<T>(y, &y_array, dctx)); 694 } else { 695 const auto copy_mode = yiscupm ? cupmMemcpyDeviceToDevice : cupmMemcpyDeviceToHost; 696 PetscScalar *yv; 697 698 // The logic here is that the data is not yet in either y's GPU array or its CPU array. 699 // There is nothing in the interface to say where the user would like it to end up. So we 700 // choose the GPU, because it is the faster option 701 if (yiscupm) { 702 PetscCall(VecCUPMGetArrayWriteAsync<T>(y, &yv, dctx)); 703 } else { 704 PetscCall(VecGetArray(y, &yv)); 705 } 706 PetscCall(PetscCUPMMemcpyAsync(yv, y_array, k, copy_mode, stream)); 707 if (yiscupm) { 708 PetscCall(VecCUPMRestoreArrayWriteAsync<T>(y, &yv, dctx)); 709 } else { 710 PetscCall(VecRestoreArray(y, &yv)); 711 } 712 PetscCall(VecCUPMRestoreArrayWriteAsync<T>(workvec, &y_array)); 713 } 714 PetscFunctionReturn(PETSC_SUCCESS); 715 } 716 717 template <device::cupm::DeviceType T> 718 template <typename Solver, bool transpose> 719 inline PetscErrorCode MatDense_Seq_CUPM<T>::MatMatSolve_Factored_Dispatch_(Mat A, Mat B, Mat X) noexcept 720 { 721 const auto m = static_cast<cupmBlasInt_t>(A->rmap->n); 722 const auto k = static_cast<cupmBlasInt_t>(A->cmap->n); 723 cupmBlasInt_t nrhs, ldb, ldx, ldy; 724 PetscScalar *y; 725 PetscBool biscupm, xiscupm, aiscupm; 726 PetscDeviceContext dctx; 727 cupmStream_t stream; 728 729 PetscFunctionBegin; 730 PetscCheck(A->factortype != MAT_FACTOR_NONE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Matrix must be factored to solve"); 731 PetscCall(PetscObjectTypeCompare(PetscObjectCast(B), MATSEQDENSECUPM(), &biscupm)); 732 PetscCall(PetscObjectTypeCompare(PetscObjectCast(X), MATSEQDENSECUPM(), &xiscupm)); 733 PetscCall(PetscObjectTypeCompare(PetscObjectCast(A), MATSEQDENSECUPM(), &aiscupm)); 734 PetscCall(GetHandles_(&dctx, &stream)); 735 { 736 PetscInt n; 737 738 PetscCall(MatGetSize(B, nullptr, &n)); 739 PetscCall(PetscCUPMBlasIntCast(n, &nrhs)); 740 PetscCall(MatDenseGetLDA(B, &n)); 741 PetscCall(PetscCUPMBlasIntCast(n, &ldb)); 742 PetscCall(MatDenseGetLDA(X, &n)); 743 PetscCall(PetscCUPMBlasIntCast(n, &ldx)); 744 } 745 { 746 // The logic here is to try to minimize the amount of memory copying: 747 // 748 // If we call MatDenseCUPMGetArrayRead(B, &b) every time biscupm and the data is not 749 // offloaded to the GPU yet, then the data is copied to the GPU. But we are only trying to 750 // get the data in order to copy it into the y array. So the array b will be wherever the 751 // data already is so that only one memcpy is performed 752 const auto bisdevice = biscupm && PetscOffloadDevice(B->offloadmask); 753 const auto copy_mode = bisdevice ? cupmMemcpyDeviceToDevice : cupmMemcpyHostToDevice; 754 const PetscScalar *b; 755 756 if (bisdevice) { 757 b = DeviceArrayRead(dctx, B); 758 } else if (biscupm) { 759 b = HostArrayRead(dctx, B); 760 } else { 761 PetscCall(MatDenseGetArrayRead(B, &b)); 762 } 763 764 if (ldx < m || !xiscupm) { 765 // X's array cannot serve as the array (too small or not on device), B's array cannot 766 // serve as the array (const), so allocate a new array 767 ldy = m; 768 PetscCall(PetscCUPMMallocAsync(&y, nrhs * m)); 769 } else { 770 // X's array should serve as the array 771 ldy = ldx; 772 y = DeviceArrayWrite(dctx, X); 773 } 774 PetscCall(PetscCUPMMemcpy2DAsync(y, ldy, b, ldb, m, nrhs, copy_mode, stream)); 775 if (!bisdevice && !biscupm) PetscCall(MatDenseRestoreArrayRead(B, &b)); 776 } 777 778 // convert to CUPM twice?????????????????????????????????? 779 // but A should already be CUPM?????????????????????????????????????? 780 if (!aiscupm) PetscCall(MatConvert(A, MATSEQDENSECUPM(), MAT_INPLACE_MATRIX, &A)); 781 PetscCall(Solver{}.template Solve<transpose>(A, cupmScalarPtrCast(y), ldy, m, nrhs, k, dctx, stream)); 782 if (!aiscupm) PetscCall(MatConvert(A, MATSEQDENSECUPM(), MAT_INPLACE_MATRIX, &A)); 783 784 if (ldx < m || !xiscupm) { 785 const auto copy_mode = xiscupm ? cupmMemcpyDeviceToDevice : cupmMemcpyDeviceToHost; 786 PetscScalar *x; 787 788 // The logic here is that the data is not yet in either X's GPU array or its CPU 789 // array. There is nothing in the interface to say where the user would like it to end up. 790 // So we choose the GPU, because it is the faster option 791 if (xiscupm) { 792 x = DeviceArrayWrite(dctx, X); 793 } else { 794 PetscCall(MatDenseGetArray(X, &x)); 795 } 796 PetscCall(PetscCUPMMemcpy2DAsync(x, ldx, y, ldy, k, nrhs, copy_mode, stream)); 797 if (!xiscupm) PetscCall(MatDenseRestoreArray(X, &x)); 798 PetscCallCUPM(cupmFreeAsync(y, stream)); 799 } 800 PetscFunctionReturn(PETSC_SUCCESS); 801 } 802 803 template <device::cupm::DeviceType T> 804 template <bool transpose, bool hermitian> 805 inline PetscErrorCode MatDense_Seq_CUPM<T>::MatMultAddColumnRange_Dispatch_(Mat A, Vec xx, Vec yy, Vec zz, PetscInt c_start, PetscInt c_end) noexcept 806 { 807 const auto m = static_cast<cupmBlasInt_t>(A->rmap->n); 808 const auto n = static_cast<cupmBlasInt_t>(c_end - c_start); 809 const auto lda = static_cast<cupmBlasInt_t>(MatIMPLCast(A)->lda); 810 PetscBool xiscupm, yiscupm, ziscupm; 811 cupmBlasHandle_t handle; 812 Vec x = xx, y = yy, z = zz; 813 PetscDeviceContext dctx; 814 815 PetscFunctionBegin; 816 PetscCall(PetscObjectTypeCompareAny(PetscObjectCast(xx), &xiscupm, VecSeq_CUPM::VECSEQCUPM(), VecSeq_CUPM::VECMPICUPM(), VecSeq_CUPM::VECCUPM(), "")); 817 if (!xiscupm || xx->boundtocpu) { 818 PetscCall(VecCreate(PetscObjectComm(PetscObjectCast(xx)), &x)); 819 PetscCall(VecSetLayout(x, xx->map)); 820 PetscCall(VecSetType(x, VecSeq_CUPM::VECCUPM())); 821 PetscCall(VecCopy(xx, x)); 822 } 823 824 if (yy) { 825 PetscCall(PetscObjectTypeCompareAny(PetscObjectCast(yy), &yiscupm, VecSeq_CUPM::VECSEQCUPM(), VecSeq_CUPM::VECMPICUPM(), VecSeq_CUPM::VECCUPM(), "")); 826 if (!yiscupm || yy->boundtocpu) { 827 PetscCall(VecCreate(PetscObjectComm(PetscObjectCast(yy)), &y)); 828 PetscCall(VecSetLayout(y, yy->map)); 829 PetscCall(VecSetType(y, VecSeq_CUPM::VECCUPM())); 830 PetscCall(VecCopy(yy, y)); 831 } 832 } 833 834 if (zz != yy) { 835 PetscCall(PetscObjectTypeCompareAny(PetscObjectCast(zz), &ziscupm, VecSeq_CUPM::VECSEQCUPM(), VecSeq_CUPM::VECMPICUPM(), VecSeq_CUPM::VECCUPM(), "")); 836 if (!ziscupm || zz->boundtocpu) { 837 PetscCall(VecCreate(PetscObjectComm(PetscObjectCast(zz)), &z)); 838 PetscCall(VecSetLayout(z, zz->map)); 839 PetscCall(VecSetType(z, VecSeq_CUPM::VECCUPM())); 840 } 841 } else { 842 z = y; 843 } 844 845 if (y && y != z) PetscCall(VecSeq_CUPM::Copy(y, z)); // mult add 846 if (!m || !n) { 847 // mult only 848 if (!y) PetscCall(VecSeq_CUPM::Set(z, 0.0)); 849 PetscFunctionReturn(PETSC_SUCCESS); 850 } 851 PetscCall(PetscInfo(A, "Matrix-vector product %" PetscBLASInt_FMT " x %" PetscBLASInt_FMT " on backend\n", m, n)); 852 PetscCall(GetHandles_(&dctx, &handle)); 853 { 854 constexpr auto op = transpose ? (hermitian ? CUPMBLAS_OP_C : CUPMBLAS_OP_T) : CUPMBLAS_OP_N; 855 const auto one = cupmScalarCast(1.0); 856 const auto zero = cupmScalarCast(0.0); 857 const auto da = DeviceArrayRead(dctx, A); 858 const auto dxx = VecSeq_CUPM::DeviceArrayRead(dctx, x); 859 const auto dzz = VecSeq_CUPM::DeviceArrayReadWrite(dctx, z); 860 861 PetscCall(PetscLogGpuTimeBegin()); 862 PetscCallCUPMBLAS(cupmBlasXgemv(handle, op, m, n, &one, da.cupmdata() + c_start * lda, lda, dxx.cupmdata() + (transpose ? 0 : c_start), 1, y ? &one : &zero, dzz.cupmdata() + (transpose ? c_start : 0), 1)); 863 PetscCall(PetscLogGpuTimeEnd()); 864 } 865 PetscCall(PetscLogGpuFlops(2.0 * m * n - (yy ? 0 : m))); 866 if (z != zz) { 867 PetscCall(VecCopy(z, zz)); 868 if (z != y) PetscCall(VecDestroy(&z)); 869 } 870 if (y != yy) PetscCall(VecDestroy(&y)); 871 if (x != xx) PetscCall(VecDestroy(&x)); 872 PetscFunctionReturn(PETSC_SUCCESS); 873 } 874 875 template <device::cupm::DeviceType T> 876 template <bool transpose, bool hermitian> 877 inline PetscErrorCode MatDense_Seq_CUPM<T>::MatMultColumnRange_Dispatch_(Mat A, Vec xx, Vec yy, PetscInt c_start, PetscInt c_end) noexcept 878 { 879 PetscFunctionBegin; 880 PetscCall(MatMultAddColumnRange_Dispatch_<transpose, hermitian>(A, xx, nullptr, yy, c_start, c_end)); 881 PetscFunctionReturn(PETSC_SUCCESS); 882 } 883 884 template <device::cupm::DeviceType T> 885 template <bool transpose, bool hermitian> 886 inline PetscErrorCode MatDense_Seq_CUPM<T>::MatMultAdd_Dispatch_(Mat A, Vec xx, Vec yy, Vec zz) noexcept 887 { 888 PetscFunctionBegin; 889 PetscCall(MatMultAddColumnRange_Dispatch_<transpose, hermitian>(A, xx, yy, zz, 0, A->cmap->n)); 890 PetscFunctionReturn(PETSC_SUCCESS); 891 } 892 893 // ========================================================================================== 894 // MatDense_Seq_CUPM - Private API - Conversion Dispatch 895 // ========================================================================================== 896 897 template <device::cupm::DeviceType T> 898 template <bool to_host> 899 inline PetscErrorCode MatDense_Seq_CUPM<T>::Convert_Dispatch_(Mat M, MatType type, MatReuse reuse, Mat *newmat) noexcept 900 { 901 PetscFunctionBegin; 902 if (reuse == MAT_REUSE_MATRIX || reuse == MAT_INITIAL_MATRIX) { 903 // TODO these cases should be optimized 904 PetscCall(MatConvert_Basic(M, type, reuse, newmat)); 905 } else { 906 const auto B = *newmat; 907 const auto pobj = PetscObjectCast(B); 908 909 if (to_host) { 910 PetscCall(BindToCPU(B, PETSC_TRUE)); 911 PetscCall(Reset(B)); 912 } else { 913 PetscCall(PetscDeviceInitialize(PETSC_DEVICE_CUPM())); 914 } 915 916 PetscCall(PetscStrFreeAllocpy(to_host ? VECSTANDARD : VecSeq_CUPM::VECCUPM(), &B->defaultvectype)); 917 PetscCall(PetscObjectChangeTypeName(pobj, to_host ? MATSEQDENSE : MATSEQDENSECUPM())); 918 // cvec might be the wrong VecType, destroy and rebuild it if necessary 919 // REVIEW ME: this is possibly very inefficient 920 PetscCall(VecDestroy(&MatIMPLCast(B)->cvec)); 921 922 MatComposeOp_CUPM(to_host, pobj, MatConvert_seqdensecupm_seqdense_C(), nullptr, Convert_SeqDenseCUPM_SeqDense); 923 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMGetArray_C(), nullptr, GetArrayC_<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_READ_WRITE>); 924 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMGetArrayRead_C(), nullptr, GetArrayC_<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_READ>); 925 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMGetArrayWrite_C(), nullptr, GetArrayC_<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_WRITE>); 926 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMRestoreArray_C(), nullptr, RestoreArrayC_<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_READ_WRITE>); 927 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMRestoreArrayRead_C(), nullptr, RestoreArrayC_<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_READ>); 928 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMRestoreArrayWrite_C(), nullptr, RestoreArrayC_<PETSC_MEMTYPE_DEVICE, PETSC_MEMORY_ACCESS_WRITE>); 929 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMPlaceArray_C(), nullptr, PlaceArray); 930 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMResetArray_C(), nullptr, ResetArray); 931 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMReplaceArray_C(), nullptr, ReplaceArray); 932 MatComposeOp_CUPM(to_host, pobj, MatProductSetFromOptions_seqaij_seqdensecupm_C(), nullptr, MatProductSetFromOptions_SeqAIJ_SeqDense); 933 MatComposeOp_CUPM(to_host, pobj, MatDenseCUPMSetPreallocation_C(), nullptr, SetPreallocation); 934 935 if (to_host) { 936 B->offloadmask = PETSC_OFFLOAD_CPU; 937 } else { 938 Mat_SeqDenseCUPM *mcu; 939 940 PetscCall(PetscNew(&mcu)); 941 B->spptr = mcu; 942 B->offloadmask = PETSC_OFFLOAD_UNALLOCATED; // REVIEW ME: why not offload host?? 943 PetscCall(BindToCPU(B, PETSC_FALSE)); 944 } 945 946 MatSetOp_CUPM(to_host, B, bindtocpu, nullptr, BindToCPU); 947 MatSetOp_CUPM(to_host, B, destroy, MatDestroy_SeqDense, Destroy); 948 } 949 PetscFunctionReturn(PETSC_SUCCESS); 950 } 951 952 // ========================================================================================== 953 // MatDense_Seq_CUPM - Public API 954 // ========================================================================================== 955 956 template <device::cupm::DeviceType T> 957 inline constexpr MatType MatDense_Seq_CUPM<T>::MATIMPLCUPM_() noexcept 958 { 959 return MATSEQDENSECUPM(); 960 } 961 962 template <device::cupm::DeviceType T> 963 inline constexpr typename MatDense_Seq_CUPM<T>::Mat_SeqDenseCUPM *MatDense_Seq_CUPM<T>::MatCUPMCast(Mat m) noexcept 964 { 965 return static_cast<Mat_SeqDenseCUPM *>(m->spptr); 966 } 967 968 template <device::cupm::DeviceType T> 969 inline constexpr Mat_SeqDense *MatDense_Seq_CUPM<T>::MatIMPLCast_(Mat m) noexcept 970 { 971 return static_cast<Mat_SeqDense *>(m->data); 972 } 973 974 template <device::cupm::DeviceType T> 975 inline constexpr const char *MatDense_Seq_CUPM<T>::MatConvert_seqdensecupm_seqdense_C() noexcept 976 { 977 return T == device::cupm::DeviceType::CUDA ? "MatConvert_seqdensecuda_seqdense_C" : "MatConvert_seqdensehip_seqdense_C"; 978 } 979 980 template <device::cupm::DeviceType T> 981 inline constexpr const char *MatDense_Seq_CUPM<T>::MatProductSetFromOptions_seqaij_seqdensecupm_C() noexcept 982 { 983 return T == device::cupm::DeviceType::CUDA ? "MatProductSetFromOptions_seqaij_seqdensecuda_C" : "MatProductSetFromOptions_seqaij_seqdensehip_C"; 984 } 985 986 // ========================================================================================== 987 988 // MatCreate_SeqDenseCUPM() 989 template <device::cupm::DeviceType T> 990 inline PetscErrorCode MatDense_Seq_CUPM<T>::Create(Mat A) noexcept 991 { 992 PetscFunctionBegin; 993 PetscCall(PetscDeviceInitialize(PETSC_DEVICE_CUPM())); 994 PetscCall(MatCreate_SeqDense(A)); 995 PetscCall(Convert_SeqDense_SeqDenseCUPM(A, MATSEQDENSECUPM(), MAT_INPLACE_MATRIX, &A)); 996 PetscFunctionReturn(PETSC_SUCCESS); 997 } 998 999 template <device::cupm::DeviceType T> 1000 inline PetscErrorCode MatDense_Seq_CUPM<T>::Destroy(Mat A) noexcept 1001 { 1002 PetscFunctionBegin; 1003 // prevent copying back data if we own the data pointer 1004 if (!MatIMPLCast(A)->user_alloc) A->offloadmask = PETSC_OFFLOAD_CPU; 1005 PetscCall(Convert_SeqDenseCUPM_SeqDense(A, MATSEQDENSE, MAT_INPLACE_MATRIX, &A)); 1006 PetscCall(MatDestroy_SeqDense(A)); 1007 PetscFunctionReturn(PETSC_SUCCESS); 1008 } 1009 1010 // obj->ops->setup() 1011 template <device::cupm::DeviceType T> 1012 inline PetscErrorCode MatDense_Seq_CUPM<T>::SetUp(Mat A) noexcept 1013 { 1014 PetscFunctionBegin; 1015 PetscCall(PetscLayoutSetUp(A->rmap)); 1016 PetscCall(PetscLayoutSetUp(A->cmap)); 1017 if (!A->preallocated) { 1018 PetscDeviceContext dctx; 1019 1020 PetscCall(GetHandles_(&dctx)); 1021 PetscCall(SetPreallocation(A, dctx, nullptr)); 1022 } 1023 PetscFunctionReturn(PETSC_SUCCESS); 1024 } 1025 1026 template <device::cupm::DeviceType T> 1027 inline PetscErrorCode MatDense_Seq_CUPM<T>::Reset(Mat A) noexcept 1028 { 1029 PetscFunctionBegin; 1030 if (const auto mcu = MatCUPMCast(A)) { 1031 cupmStream_t stream; 1032 1033 PetscCheck(!mcu->unplacedarray, PETSC_COMM_SELF, PETSC_ERR_ORDER, "MatDense%sResetArray() must be called first", cupmNAME()); 1034 PetscCall(GetHandles_(&stream)); 1035 if (!mcu->d_user_alloc) PetscCallCUPM(cupmFreeAsync(mcu->d_v, stream)); 1036 PetscCallCUPM(cupmFreeAsync(mcu->d_fact_tau, stream)); 1037 PetscCallCUPM(cupmFreeAsync(mcu->d_fact_ipiv, stream)); 1038 PetscCallCUPM(cupmFreeAsync(mcu->d_fact_info, stream)); 1039 PetscCallCUPM(cupmFreeAsync(mcu->d_fact_work, stream)); 1040 PetscCall(VecDestroy(&mcu->workvec)); 1041 PetscCall(PetscFree(A->spptr /* mcu */)); 1042 } 1043 PetscFunctionReturn(PETSC_SUCCESS); 1044 } 1045 1046 // ========================================================================================== 1047 1048 template <device::cupm::DeviceType T> 1049 inline PetscErrorCode MatDense_Seq_CUPM<T>::BindToCPU(Mat A, PetscBool to_host) noexcept 1050 { 1051 const auto mimpl = MatIMPLCast(A); 1052 const auto pobj = PetscObjectCast(A); 1053 1054 PetscFunctionBegin; 1055 PetscCheck(!mimpl->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first"); 1056 PetscCheck(!mimpl->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first"); 1057 A->boundtocpu = to_host; 1058 PetscCall(PetscStrFreeAllocpy(to_host ? PETSCRANDER48 : PETSCDEVICERAND(), &A->defaultrandtype)); 1059 if (to_host) { 1060 PetscDeviceContext dctx; 1061 1062 // make sure we have an up-to-date copy on the CPU 1063 PetscCall(GetHandles_(&dctx)); 1064 PetscCall(DeviceToHost_(A, dctx)); 1065 } else { 1066 PetscBool iscupm; 1067 1068 if (auto &cvec = mimpl->cvec) { 1069 PetscCall(PetscObjectTypeCompare(PetscObjectCast(cvec), VecSeq_CUPM::VECSEQCUPM(), &iscupm)); 1070 if (!iscupm) PetscCall(VecDestroy(&cvec)); 1071 } 1072 if (auto &cmat = mimpl->cmat) { 1073 PetscCall(PetscObjectTypeCompare(PetscObjectCast(cmat), MATSEQDENSECUPM(), &iscupm)); 1074 if (!iscupm) PetscCall(MatDestroy(&cmat)); 1075 } 1076 } 1077 1078 // ============================================================ 1079 // Composed ops 1080 // ============================================================ 1081 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetArray_C", MatDenseGetArray_SeqDense, GetArrayC_<PETSC_MEMTYPE_HOST, PETSC_MEMORY_ACCESS_READ_WRITE>); 1082 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetArrayRead_C", MatDenseGetArray_SeqDense, GetArrayC_<PETSC_MEMTYPE_HOST, PETSC_MEMORY_ACCESS_READ>); 1083 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetArrayWrite_C", MatDenseGetArray_SeqDense, GetArrayC_<PETSC_MEMTYPE_HOST, PETSC_MEMORY_ACCESS_WRITE>); 1084 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetArrayAndMemType_C", nullptr, GetArrayAndMemTypeC_<PETSC_MEMORY_ACCESS_READ_WRITE>); 1085 MatComposeOp_CUPM(to_host, pobj, "MatDenseRestoreArrayAndMemType_C", nullptr, RestoreArrayAndMemTypeC_<PETSC_MEMORY_ACCESS_READ_WRITE>); 1086 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetArrayReadAndMemType_C", nullptr, GetArrayAndMemTypeC_<PETSC_MEMORY_ACCESS_READ>); 1087 MatComposeOp_CUPM(to_host, pobj, "MatDenseRestoreArrayReadAndMemType_C", nullptr, RestoreArrayAndMemTypeC_<PETSC_MEMORY_ACCESS_READ>); 1088 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetArrayWriteAndMemType_C", nullptr, GetArrayAndMemTypeC_<PETSC_MEMORY_ACCESS_WRITE>); 1089 MatComposeOp_CUPM(to_host, pobj, "MatDenseRestoreArrayWriteAndMemType_C", nullptr, RestoreArrayAndMemTypeC_<PETSC_MEMORY_ACCESS_WRITE>); 1090 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetColumnVec_C", MatDenseGetColumnVec_SeqDense, GetColumnVec<PETSC_MEMORY_ACCESS_READ_WRITE>); 1091 MatComposeOp_CUPM(to_host, pobj, "MatDenseRestoreColumnVec_C", MatDenseRestoreColumnVec_SeqDense, RestoreColumnVec<PETSC_MEMORY_ACCESS_READ_WRITE>); 1092 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetColumnVecRead_C", MatDenseGetColumnVecRead_SeqDense, GetColumnVec<PETSC_MEMORY_ACCESS_READ>); 1093 MatComposeOp_CUPM(to_host, pobj, "MatDenseRestoreColumnVecRead_C", MatDenseRestoreColumnVecRead_SeqDense, RestoreColumnVec<PETSC_MEMORY_ACCESS_READ>); 1094 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetColumnVecWrite_C", MatDenseGetColumnVecWrite_SeqDense, GetColumnVec<PETSC_MEMORY_ACCESS_WRITE>); 1095 MatComposeOp_CUPM(to_host, pobj, "MatDenseRestoreColumnVecWrite_C", MatDenseRestoreColumnVecWrite_SeqDense, RestoreColumnVec<PETSC_MEMORY_ACCESS_WRITE>); 1096 MatComposeOp_CUPM(to_host, pobj, "MatDenseGetSubMatrix_C", MatDenseGetSubMatrix_SeqDense, GetSubMatrix); 1097 MatComposeOp_CUPM(to_host, pobj, "MatDenseRestoreSubMatrix_C", MatDenseRestoreSubMatrix_SeqDense, RestoreSubMatrix); 1098 MatComposeOp_CUPM(to_host, pobj, "MatQRFactor_C", MatQRFactor_SeqDense, SolveQR::Factor); 1099 MatComposeOp_CUPM(to_host, pobj, "MatMultAddColumnRange_C", MatMultAddColumnRange_SeqDense, MatMultAddColumnRange_Dispatch_</* transpose */ false, /* hermitian */ false>); 1100 MatComposeOp_CUPM(to_host, pobj, "MatMultHermitianTransposeColumnRange_C", MatMultHermitianTransposeColumnRange_SeqDense, MatMultColumnRange_Dispatch_</* transpose */ true, /* hermitian */ true>); 1101 MatComposeOp_CUPM(to_host, pobj, "MatMultHermitianTransposeAddColumnRange_C", MatMultHermitianTransposeAddColumnRange_SeqDense, MatMultAddColumnRange_Dispatch_</* transpose */ true, /* hermitian */ true>); 1102 // always the same 1103 PetscCall(PetscObjectComposeFunction(pobj, "MatDenseSetLDA_C", MatDenseSetLDA_SeqDense)); 1104 1105 // ============================================================ 1106 // Function pointer ops 1107 // ============================================================ 1108 MatSetOp_CUPM(to_host, A, duplicate, MatDuplicate_SeqDense, Duplicate); 1109 MatSetOp_CUPM(to_host, A, mult, MatMult_SeqDense, [](Mat A, Vec xx, Vec yy) { return MatMultAdd_Dispatch_</* transpose */ false, /* hermitian */ false>(A, xx, nullptr, yy); }); 1110 MatSetOp_CUPM(to_host, A, multtranspose, MatMultTranspose_SeqDense, [](Mat A, Vec xx, Vec yy) { return MatMultAdd_Dispatch_</* transpose */ true, /* hermitian */ false>(A, xx, nullptr, yy); }); 1111 MatSetOp_CUPM(to_host, A, multhermitiantranspose, MatMultTranspose_SeqDense, [](Mat A, Vec xx, Vec yy) { return MatMultAdd_Dispatch_</* transpose */ true, /* hermitian */ true>(A, xx, nullptr, yy); }); 1112 MatSetOp_CUPM(to_host, A, multadd, MatMultAdd_SeqDense, MatMultAdd_Dispatch_</* transpose */ false, /* hermitian */ false>); 1113 MatSetOp_CUPM(to_host, A, multtransposeadd, MatMultTransposeAdd_SeqDense, MatMultAdd_Dispatch_</* transpose */ true, /* hermitian */ false>); 1114 MatSetOp_CUPM(to_host, A, multhermitiantransposeadd, MatMultHermitianTransposeAdd_SeqDense, MatMultAdd_Dispatch_</* transpose */ true, /* hermitian */ true>); 1115 MatSetOp_CUPM(to_host, A, matmultnumeric, MatMatMultNumeric_SeqDense_SeqDense, MatMatMult_Numeric_Dispatch</* transpose_A */ false, /* transpose_B */ false>); 1116 MatSetOp_CUPM(to_host, A, mattransposemultnumeric, MatMatTransposeMultNumeric_SeqDense_SeqDense, MatMatMult_Numeric_Dispatch</* transpose_A */ false, /* transpose_B */ true>); 1117 MatSetOp_CUPM(to_host, A, transposematmultnumeric, MatTransposeMatMultNumeric_SeqDense_SeqDense, MatMatMult_Numeric_Dispatch</* transpose_A */ true, /* transpose_B */ false>); 1118 MatSetOp_CUPM(to_host, A, axpy, MatAXPY_SeqDense, AXPY); 1119 MatSetOp_CUPM(to_host, A, choleskyfactor, MatCholeskyFactor_SeqDense, SolveCholesky::Factor); 1120 MatSetOp_CUPM(to_host, A, lufactor, MatLUFactor_SeqDense, SolveLU::Factor); 1121 MatSetOp_CUPM(to_host, A, getcolumnvector, MatGetColumnVector_SeqDense, GetColumnVector); 1122 MatSetOp_CUPM(to_host, A, conjugate, MatConjugate_SeqDense, Conjugate); 1123 MatSetOp_CUPM(to_host, A, scale, MatScale_SeqDense, Scale); 1124 MatSetOp_CUPM(to_host, A, shift, MatShift_SeqDense, Shift); 1125 MatSetOp_CUPM(to_host, A, copy, MatCopy_SeqDense, Copy); 1126 MatSetOp_CUPM(to_host, A, zeroentries, MatZeroEntries_SeqDense, ZeroEntries); 1127 MatSetOp_CUPM(to_host, A, setup, MatSetUp_SeqDense, SetUp); 1128 MatSetOp_CUPM(to_host, A, setrandom, MatSetRandom_SeqDense, SetRandom); 1129 MatSetOp_CUPM(to_host, A, getdiagonal, MatGetDiagonal_SeqDense, GetDiagonal); 1130 // seemingly always the same 1131 A->ops->productsetfromoptions = MatProductSetFromOptions_SeqDense; 1132 1133 if (const auto cmat = mimpl->cmat) PetscCall(MatBindToCPU(cmat, to_host)); 1134 PetscFunctionReturn(PETSC_SUCCESS); 1135 } 1136 1137 template <device::cupm::DeviceType T> 1138 inline PetscErrorCode MatDense_Seq_CUPM<T>::Convert_SeqDenseCUPM_SeqDense(Mat M, MatType type, MatReuse reuse, Mat *newmat) noexcept 1139 { 1140 PetscFunctionBegin; 1141 PetscCall(Convert_Dispatch_</* to host */ true>(M, type, reuse, newmat)); 1142 PetscFunctionReturn(PETSC_SUCCESS); 1143 } 1144 1145 template <device::cupm::DeviceType T> 1146 inline PetscErrorCode MatDense_Seq_CUPM<T>::Convert_SeqDense_SeqDenseCUPM(Mat M, MatType type, MatReuse reuse, Mat *newmat) noexcept 1147 { 1148 PetscFunctionBegin; 1149 PetscCall(Convert_Dispatch_</* to host */ false>(M, type, reuse, newmat)); 1150 PetscFunctionReturn(PETSC_SUCCESS); 1151 } 1152 1153 // ========================================================================================== 1154 1155 template <device::cupm::DeviceType T> 1156 template <PetscMemType mtype, PetscMemoryAccessMode access> 1157 inline PetscErrorCode MatDense_Seq_CUPM<T>::GetArray(Mat m, PetscScalar **array, PetscDeviceContext dctx) noexcept 1158 { 1159 constexpr auto hostmem = PetscMemTypeHost(mtype); 1160 constexpr auto read_access = PetscMemoryAccessRead(access); 1161 1162 PetscFunctionBegin; 1163 static_assert((mtype == PETSC_MEMTYPE_HOST) || (mtype == PETSC_MEMTYPE_DEVICE), ""); 1164 PetscCall(PetscDeviceContextGetOptionalNullContext_Internal(&dctx)); 1165 if (hostmem) { 1166 if (read_access) { 1167 PetscCall(DeviceToHost_(m, dctx)); 1168 } else if (!MatIMPLCast(m)->v) { 1169 // MatCreateSeqDenseCUPM may not allocate CPU memory. Allocate if needed 1170 PetscCall(MatSeqDenseSetPreallocation(m, nullptr)); 1171 } 1172 *array = MatIMPLCast(m)->v; 1173 } else { 1174 if (read_access) { 1175 PetscCall(HostToDevice_(m, dctx)); 1176 } else if (!MatCUPMCast(m)->d_v) { 1177 // write-only 1178 PetscCall(SetPreallocation(m, dctx, nullptr)); 1179 } 1180 *array = MatCUPMCast(m)->d_v; 1181 } 1182 if (PetscMemoryAccessWrite(access)) { 1183 m->offloadmask = hostmem ? PETSC_OFFLOAD_CPU : PETSC_OFFLOAD_GPU; 1184 PetscCall(PetscObjectStateIncrease(PetscObjectCast(m))); 1185 } 1186 PetscFunctionReturn(PETSC_SUCCESS); 1187 } 1188 1189 template <device::cupm::DeviceType T> 1190 template <PetscMemType mtype, PetscMemoryAccessMode access> 1191 inline PetscErrorCode MatDense_Seq_CUPM<T>::RestoreArray(Mat m, PetscScalar **array, PetscDeviceContext) noexcept 1192 { 1193 PetscFunctionBegin; 1194 static_assert((mtype == PETSC_MEMTYPE_HOST) || (mtype == PETSC_MEMTYPE_DEVICE), ""); 1195 if (PetscMemoryAccessWrite(access)) { 1196 // WRITE or READ_WRITE 1197 m->offloadmask = PetscMemTypeHost(mtype) ? PETSC_OFFLOAD_CPU : PETSC_OFFLOAD_GPU; 1198 PetscCall(PetscObjectStateIncrease(PetscObjectCast(m))); 1199 } 1200 if (array) { 1201 PetscCall(CheckPointerMatchesMemType_(*array, mtype)); 1202 *array = nullptr; 1203 } 1204 PetscFunctionReturn(PETSC_SUCCESS); 1205 } 1206 1207 template <device::cupm::DeviceType T> 1208 template <PetscMemoryAccessMode access> 1209 inline PetscErrorCode MatDense_Seq_CUPM<T>::GetArrayAndMemType(Mat m, PetscScalar **array, PetscMemType *mtype, PetscDeviceContext dctx) noexcept 1210 { 1211 PetscFunctionBegin; 1212 PetscCall(GetArray<PETSC_MEMTYPE_DEVICE, access>(m, array, dctx)); 1213 if (mtype) *mtype = PETSC_MEMTYPE_CUPM(); 1214 PetscFunctionReturn(PETSC_SUCCESS); 1215 } 1216 1217 template <device::cupm::DeviceType T> 1218 template <PetscMemoryAccessMode access> 1219 inline PetscErrorCode MatDense_Seq_CUPM<T>::RestoreArrayAndMemType(Mat m, PetscScalar **array, PetscDeviceContext dctx) noexcept 1220 { 1221 PetscFunctionBegin; 1222 PetscCall(RestoreArray<PETSC_MEMTYPE_DEVICE, access>(m, array, dctx)); 1223 PetscFunctionReturn(PETSC_SUCCESS); 1224 } 1225 1226 // ========================================================================================== 1227 1228 template <device::cupm::DeviceType T> 1229 inline PetscErrorCode MatDense_Seq_CUPM<T>::PlaceArray(Mat A, const PetscScalar *array) noexcept 1230 { 1231 const auto mimpl = MatIMPLCast(A); 1232 const auto mcu = MatCUPMCast(A); 1233 1234 PetscFunctionBegin; 1235 PetscCheck(!mimpl->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first"); 1236 PetscCheck(!mimpl->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first"); 1237 PetscCheck(!mcu->unplacedarray, PETSC_COMM_SELF, PETSC_ERR_ORDER, "MatDense%sResetArray() must be called first", cupmNAME()); 1238 if (mimpl->v) { 1239 PetscDeviceContext dctx; 1240 1241 PetscCall(GetHandles_(&dctx)); 1242 PetscCall(HostToDevice_(A, dctx)); 1243 } 1244 mcu->unplacedarray = util::exchange(mcu->d_v, const_cast<PetscScalar *>(array)); 1245 mcu->d_unplaced_user_alloc = util::exchange(mcu->d_user_alloc, PETSC_TRUE); 1246 PetscFunctionReturn(PETSC_SUCCESS); 1247 } 1248 1249 template <device::cupm::DeviceType T> 1250 inline PetscErrorCode MatDense_Seq_CUPM<T>::ReplaceArray(Mat A, const PetscScalar *array) noexcept 1251 { 1252 const auto mimpl = MatIMPLCast(A); 1253 const auto mcu = MatCUPMCast(A); 1254 1255 PetscFunctionBegin; 1256 PetscCheck(!mimpl->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first"); 1257 PetscCheck(!mimpl->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first"); 1258 PetscCheck(!mcu->unplacedarray, PETSC_COMM_SELF, PETSC_ERR_ORDER, "MatDense%sResetArray() must be called first", cupmNAME()); 1259 if (!mcu->d_user_alloc) { 1260 cupmStream_t stream; 1261 1262 PetscCall(GetHandles_(&stream)); 1263 PetscCallCUPM(cupmFreeAsync(mcu->d_v, stream)); 1264 } 1265 mcu->d_v = const_cast<PetscScalar *>(array); 1266 mcu->d_user_alloc = PETSC_FALSE; 1267 PetscFunctionReturn(PETSC_SUCCESS); 1268 } 1269 1270 template <device::cupm::DeviceType T> 1271 inline PetscErrorCode MatDense_Seq_CUPM<T>::ResetArray(Mat A) noexcept 1272 { 1273 const auto mimpl = MatIMPLCast(A); 1274 const auto mcu = MatCUPMCast(A); 1275 1276 PetscFunctionBegin; 1277 PetscCheck(!mimpl->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first"); 1278 PetscCheck(!mimpl->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first"); 1279 if (mimpl->v) { 1280 PetscDeviceContext dctx; 1281 1282 PetscCall(GetHandles_(&dctx)); 1283 PetscCall(HostToDevice_(A, dctx)); 1284 } 1285 mcu->d_v = util::exchange(mcu->unplacedarray, nullptr); 1286 mcu->d_user_alloc = mcu->d_unplaced_user_alloc; 1287 PetscFunctionReturn(PETSC_SUCCESS); 1288 } 1289 1290 // ========================================================================================== 1291 1292 template <device::cupm::DeviceType T> 1293 template <bool transpose_A, bool transpose_B> 1294 inline PetscErrorCode MatDense_Seq_CUPM<T>::MatMatMult_Numeric_Dispatch(Mat A, Mat B, Mat C) noexcept 1295 { 1296 cupmBlasInt_t m, n, k; 1297 PetscBool Aiscupm, Biscupm; 1298 PetscDeviceContext dctx; 1299 cupmBlasHandle_t handle; 1300 1301 PetscFunctionBegin; 1302 PetscCall(PetscCUPMBlasIntCast(C->rmap->n, &m)); 1303 PetscCall(PetscCUPMBlasIntCast(C->cmap->n, &n)); 1304 PetscCall(PetscCUPMBlasIntCast(transpose_A ? A->rmap->n : A->cmap->n, &k)); 1305 if (!m || !n || !k) PetscFunctionReturn(PETSC_SUCCESS); 1306 1307 // we may end up with SEQDENSE as one of the arguments 1308 // REVIEW ME: how? and why is it not B and C???????? 1309 PetscCall(PetscObjectTypeCompare(PetscObjectCast(A), MATSEQDENSECUPM(), &Aiscupm)); 1310 PetscCall(PetscObjectTypeCompare(PetscObjectCast(B), MATSEQDENSECUPM(), &Biscupm)); 1311 if (!Aiscupm) PetscCall(MatConvert(A, MATSEQDENSECUPM(), MAT_INPLACE_MATRIX, &A)); 1312 if (!Biscupm) PetscCall(MatConvert(B, MATSEQDENSECUPM(), MAT_INPLACE_MATRIX, &B)); 1313 PetscCall(PetscInfo(C, "Matrix-Matrix product %" PetscBLASInt_FMT " x %" PetscBLASInt_FMT " x %" PetscBLASInt_FMT " on backend\n", m, k, n)); 1314 PetscCall(GetHandles_(&dctx, &handle)); 1315 1316 PetscCall(PetscLogGpuTimeBegin()); 1317 { 1318 const auto one = cupmScalarCast(1.0); 1319 const auto zero = cupmScalarCast(0.0); 1320 const auto da = DeviceArrayRead(dctx, A); 1321 const auto db = DeviceArrayRead(dctx, B); 1322 const auto dc = DeviceArrayWrite(dctx, C); 1323 PetscInt alda, blda, clda; 1324 1325 PetscCall(MatDenseGetLDA(A, &alda)); 1326 PetscCall(MatDenseGetLDA(B, &blda)); 1327 PetscCall(MatDenseGetLDA(C, &clda)); 1328 PetscCallCUPMBLAS(cupmBlasXgemm(handle, transpose_A ? CUPMBLAS_OP_T : CUPMBLAS_OP_N, transpose_B ? CUPMBLAS_OP_T : CUPMBLAS_OP_N, m, n, k, &one, da.cupmdata(), alda, db.cupmdata(), blda, &zero, dc.cupmdata(), clda)); 1329 } 1330 PetscCall(PetscLogGpuTimeEnd()); 1331 1332 PetscCall(PetscLogGpuFlops(1.0 * m * n * k + 1.0 * m * n * (k - 1))); 1333 if (!Aiscupm) PetscCall(MatConvert(A, MATSEQDENSE, MAT_INPLACE_MATRIX, &A)); 1334 if (!Biscupm) PetscCall(MatConvert(B, MATSEQDENSE, MAT_INPLACE_MATRIX, &B)); 1335 PetscFunctionReturn(PETSC_SUCCESS); 1336 } 1337 1338 template <device::cupm::DeviceType T> 1339 inline PetscErrorCode MatDense_Seq_CUPM<T>::Copy(Mat A, Mat B, MatStructure str) noexcept 1340 { 1341 const auto m = A->rmap->n; 1342 const auto n = A->cmap->n; 1343 1344 PetscFunctionBegin; 1345 PetscAssert(m == B->rmap->n && n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "size(B) != size(A)"); 1346 // The two matrices must have the same copy implementation to be eligible for fast copy 1347 if (A->ops->copy == B->ops->copy) { 1348 PetscDeviceContext dctx; 1349 cupmStream_t stream; 1350 1351 PetscCall(GetHandles_(&dctx, &stream)); 1352 PetscCall(PetscLogGpuTimeBegin()); 1353 { 1354 const auto va = DeviceArrayRead(dctx, A); 1355 const auto vb = DeviceArrayWrite(dctx, B); 1356 // order is important, DeviceArrayRead/Write() might call SetPreallocation() which sets 1357 // lda! 1358 const auto lda_a = MatIMPLCast(A)->lda; 1359 const auto lda_b = MatIMPLCast(B)->lda; 1360 1361 if (lda_a > m || lda_b > m) { 1362 PetscAssert(lda_b > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "B lda (%" PetscBLASInt_FMT ") must be > 0 at this point, this indicates Mat%sSetPreallocation() was not called when it should have been!", lda_b, cupmNAME()); 1363 PetscAssert(lda_a > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "A lda (%" PetscBLASInt_FMT ") must be > 0 at this point, this indicates Mat%sSetPreallocation() was not called when it should have been!", lda_a, cupmNAME()); 1364 PetscCall(PetscCUPMMemcpy2DAsync(vb.data(), lda_b, va.data(), lda_a, m, n, cupmMemcpyDeviceToDevice, stream)); 1365 } else { 1366 PetscCall(PetscCUPMMemcpyAsync(vb.data(), va.data(), m * n, cupmMemcpyDeviceToDevice, stream)); 1367 } 1368 } 1369 PetscCall(PetscLogGpuTimeEnd()); 1370 } else { 1371 PetscCall(MatCopy_Basic(A, B, str)); 1372 } 1373 PetscFunctionReturn(PETSC_SUCCESS); 1374 } 1375 1376 template <device::cupm::DeviceType T> 1377 inline PetscErrorCode MatDense_Seq_CUPM<T>::ZeroEntries(Mat m) noexcept 1378 { 1379 PetscDeviceContext dctx; 1380 cupmStream_t stream; 1381 1382 PetscFunctionBegin; 1383 PetscCall(GetHandles_(&dctx, &stream)); 1384 PetscCall(PetscLogGpuTimeBegin()); 1385 { 1386 const auto va = DeviceArrayWrite(dctx, m); 1387 const auto lda = MatIMPLCast(m)->lda; 1388 const auto ma = m->rmap->n; 1389 const auto na = m->cmap->n; 1390 1391 if (lda > ma) { 1392 PetscCall(PetscCUPMMemset2DAsync(va.data(), lda, 0, ma, na, stream)); 1393 } else { 1394 PetscCall(PetscCUPMMemsetAsync(va.data(), 0, ma * na, stream)); 1395 } 1396 } 1397 PetscCall(PetscLogGpuTimeEnd()); 1398 PetscFunctionReturn(PETSC_SUCCESS); 1399 } 1400 1401 namespace detail 1402 { 1403 1404 // ========================================================================================== 1405 // SubMatIndexFunctor 1406 // 1407 // Iterator which permutes a linear index range into matrix indices for am nrows x ncols 1408 // submat with leading dimension lda. Essentially SubMatIndexFunctor(i) returns the index for 1409 // the i'th sequential entry in the matrix. 1410 // ========================================================================================== 1411 template <typename T> 1412 struct SubMatIndexFunctor { 1413 PETSC_HOSTDEVICE_INLINE_DECL T operator()(T x) const noexcept { return ((x / nrows) * lda) + (x % nrows); } 1414 1415 PetscInt nrows; 1416 PetscInt ncols; 1417 PetscInt lda; 1418 }; 1419 1420 template <typename Iterator> 1421 struct SubMatrixIterator : MatrixIteratorBase<Iterator, SubMatIndexFunctor<typename thrust::iterator_difference<Iterator>::type>> { 1422 using base_type = MatrixIteratorBase<Iterator, SubMatIndexFunctor<typename thrust::iterator_difference<Iterator>::type>>; 1423 1424 using iterator = typename base_type::iterator; 1425 1426 constexpr SubMatrixIterator(Iterator first, Iterator last, PetscInt nrows, PetscInt ncols, PetscInt lda) noexcept : 1427 base_type{ 1428 std::move(first), std::move(last), {nrows, ncols, lda} 1429 } 1430 { 1431 } 1432 1433 PETSC_NODISCARD iterator end() const noexcept { return this->begin() + (this->func.nrows * this->func.ncols); } 1434 }; 1435 1436 namespace 1437 { 1438 1439 template <typename T> 1440 PETSC_NODISCARD inline SubMatrixIterator<typename thrust::device_vector<T>::iterator> make_submat_iterator(PetscInt rstart, PetscInt rend, PetscInt cstart, PetscInt cend, PetscInt lda, T *ptr) noexcept 1441 { 1442 const auto nrows = rend - rstart; 1443 const auto ncols = cend - cstart; 1444 const auto dptr = thrust::device_pointer_cast(ptr); 1445 1446 return {dptr + (rstart * lda) + cstart, dptr + ((rstart + nrows) * lda) + cstart, nrows, ncols, lda}; 1447 } 1448 1449 } // namespace 1450 1451 struct conjugate { 1452 PETSC_NODISCARD PETSC_HOSTDEVICE_INLINE_DECL PetscScalar operator()(const PetscScalar &x) const noexcept { return PetscConj(x); } 1453 }; 1454 1455 } // namespace detail 1456 1457 template <device::cupm::DeviceType T> 1458 inline PetscErrorCode MatDense_Seq_CUPM<T>::Conjugate(Mat A) noexcept 1459 { 1460 const auto m = A->rmap->n; 1461 const auto n = A->cmap->n; 1462 const auto N = m * n; 1463 PetscDeviceContext dctx; 1464 cupmStream_t stream; 1465 1466 PetscFunctionBegin; 1467 if (PetscDefined(USE_COMPLEX)) { 1468 PetscCall(GetHandles_(&dctx, &stream)); 1469 PetscCall(PetscLogGpuTimeBegin()); 1470 { 1471 const auto da = DeviceArrayReadWrite(dctx, A); 1472 const auto lda = MatIMPLCast(A)->lda; 1473 cupmStream_t stream; 1474 PetscCall(GetHandlesFrom_(dctx, &stream)); 1475 1476 if (lda > m) { 1477 // clang-format off 1478 PetscCallThrust( 1479 const auto sub_mat = detail::make_submat_iterator(0, m, 0, n, lda, da.data()); 1480 1481 THRUST_CALL( 1482 thrust::transform, 1483 stream, 1484 sub_mat.begin(), sub_mat.end(), sub_mat.begin(), 1485 detail::conjugate{} 1486 ) 1487 ); 1488 // clang-format on 1489 } else { 1490 // clang-format off 1491 PetscCallThrust( 1492 const auto aptr = thrust::device_pointer_cast(da.data()); 1493 1494 THRUST_CALL( 1495 thrust::transform, 1496 stream, 1497 aptr, aptr + N, aptr, 1498 detail::conjugate{} 1499 ) 1500 ); 1501 // clang-format on 1502 } 1503 } 1504 PetscCall(PetscLogGpuTimeEnd()); 1505 } 1506 PetscFunctionReturn(PETSC_SUCCESS); 1507 } 1508 1509 template <device::cupm::DeviceType T> 1510 inline PetscErrorCode MatDense_Seq_CUPM<T>::Scale(Mat A, PetscScalar alpha) noexcept 1511 { 1512 const auto m = A->rmap->n; 1513 const auto n = A->cmap->n; 1514 const auto N = m * n; 1515 PetscDeviceContext dctx; 1516 1517 PetscFunctionBegin; 1518 PetscCall(PetscInfo(A, "Performing Scale %" PetscInt_FMT " x %" PetscInt_FMT " on backend\n", m, n)); 1519 PetscCall(GetHandles_(&dctx)); 1520 { 1521 const auto da = DeviceArrayReadWrite(dctx, A); 1522 const auto lda = MatIMPLCast(A)->lda; 1523 1524 if (lda > m) { 1525 cupmStream_t stream; 1526 1527 PetscCall(GetHandlesFrom_(dctx, &stream)); 1528 // clang-format off 1529 PetscCallThrust( 1530 const auto sub_mat = detail::make_submat_iterator(0, m, 0, n, lda, da.data()); 1531 1532 THRUST_CALL( 1533 thrust::transform, 1534 stream, 1535 sub_mat.begin(), sub_mat.end(), sub_mat.begin(), 1536 device::cupm::functors::make_times_equals(alpha) 1537 ) 1538 ); 1539 // clang-format on 1540 } else { 1541 const auto cu_alpha = cupmScalarCast(alpha); 1542 cupmBlasHandle_t handle; 1543 1544 PetscCall(GetHandlesFrom_(dctx, &handle)); 1545 PetscCall(PetscLogGpuTimeBegin()); 1546 PetscCallCUPMBLAS(cupmBlasXscal(handle, N, &cu_alpha, da.cupmdata(), 1)); 1547 PetscCall(PetscLogGpuTimeEnd()); 1548 } 1549 } 1550 PetscCall(PetscLogGpuFlops(N)); 1551 PetscFunctionReturn(PETSC_SUCCESS); 1552 } 1553 1554 template <device::cupm::DeviceType T> 1555 inline PetscErrorCode MatDense_Seq_CUPM<T>::AXPY(Mat Y, PetscScalar alpha, Mat X, MatStructure) noexcept 1556 { 1557 const auto m_x = X->rmap->n, m_y = Y->rmap->n; 1558 const auto n_x = X->cmap->n, n_y = Y->cmap->n; 1559 const auto N = m_x * n_x; 1560 PetscDeviceContext dctx; 1561 1562 PetscFunctionBegin; 1563 if (!m_x || !n_x || alpha == (PetscScalar)0.0) PetscFunctionReturn(PETSC_SUCCESS); 1564 PetscCall(PetscInfo(Y, "Performing AXPY %" PetscInt_FMT " x %" PetscInt_FMT " on backend\n", m_y, n_y)); 1565 PetscCall(GetHandles_(&dctx)); 1566 { 1567 const auto dx = DeviceArrayRead(dctx, X); 1568 const auto dy = DeviceArrayReadWrite(dctx, Y); 1569 const auto lda_x = MatIMPLCast(X)->lda; 1570 const auto lda_y = MatIMPLCast(Y)->lda; 1571 1572 if (lda_x > m_x || lda_y > m_x) { 1573 cupmStream_t stream; 1574 1575 PetscCall(GetHandlesFrom_(dctx, &stream)); 1576 // clang-format off 1577 PetscCallThrust( 1578 const auto sub_mat_y = detail::make_submat_iterator(0, m_y, 0, n_y, lda_y, dy.data()); 1579 const auto sub_mat_x = detail::make_submat_iterator(0, m_x, 0, n_x, lda_x, dx.data()); 1580 1581 THRUST_CALL( 1582 thrust::transform, 1583 stream, 1584 sub_mat_x.begin(), sub_mat_x.end(), sub_mat_y.begin(), sub_mat_y.begin(), 1585 device::cupm::functors::make_axpy(alpha) 1586 ); 1587 ); 1588 // clang-format on 1589 } else { 1590 const auto cu_alpha = cupmScalarCast(alpha); 1591 cupmBlasHandle_t handle; 1592 1593 PetscCall(GetHandlesFrom_(dctx, &handle)); 1594 PetscCall(PetscLogGpuTimeBegin()); 1595 PetscCallCUPMBLAS(cupmBlasXaxpy(handle, N, &cu_alpha, dx.cupmdata(), 1, dy.cupmdata(), 1)); 1596 PetscCall(PetscLogGpuTimeEnd()); 1597 } 1598 } 1599 PetscCall(PetscLogGpuFlops(PetscMax(2 * N - 1, 0))); 1600 PetscFunctionReturn(PETSC_SUCCESS); 1601 } 1602 1603 template <device::cupm::DeviceType T> 1604 inline PetscErrorCode MatDense_Seq_CUPM<T>::Duplicate(Mat A, MatDuplicateOption opt, Mat *B) noexcept 1605 { 1606 const auto hopt = (opt == MAT_COPY_VALUES && A->offloadmask != PETSC_OFFLOAD_CPU) ? MAT_DO_NOT_COPY_VALUES : opt; 1607 PetscDeviceContext dctx; 1608 1609 PetscFunctionBegin; 1610 PetscCall(GetHandles_(&dctx)); 1611 // do not call SetPreallocation() yet, we call it afterwards?? 1612 PetscCall(MatCreateSeqDenseCUPM<T>(PetscObjectComm(PetscObjectCast(A)), A->rmap->n, A->cmap->n, nullptr, B, dctx, /* preallocate */ false)); 1613 PetscCall(MatDuplicateNoCreate_SeqDense(*B, A, hopt)); 1614 if (opt == MAT_COPY_VALUES && hopt != MAT_COPY_VALUES) PetscCall(Copy(A, *B, SAME_NONZERO_PATTERN)); 1615 // allocate memory if needed 1616 if (opt != MAT_COPY_VALUES && !MatCUPMCast(*B)->d_v) PetscCall(SetPreallocation(*B, dctx, nullptr)); 1617 PetscFunctionReturn(PETSC_SUCCESS); 1618 } 1619 1620 template <device::cupm::DeviceType T> 1621 inline PetscErrorCode MatDense_Seq_CUPM<T>::SetRandom(Mat A, PetscRandom rng) noexcept 1622 { 1623 PetscBool device_rand_is_rander48; 1624 PetscBool device = PETSC_FALSE; 1625 1626 PetscFunctionBegin; 1627 // CUPMObject<hip>::PETSCDEVICERAD() is PETSCRANDER48 until PetscRandom is implemented for hiprand 1628 PetscCall(PetscStrncmp(PETSCDEVICERAND(), PETSCRANDER48, sizeof(PETSCRANDER48), &device_rand_is_rander48)); 1629 if (!device_rand_is_rander48) PetscCall(PetscObjectTypeCompare(PetscObjectCast(rng), PETSCDEVICERAND(), &device)); 1630 if (device) { 1631 const auto m = A->rmap->n; 1632 const auto n = A->cmap->n; 1633 PetscDeviceContext dctx; 1634 1635 PetscCall(GetHandles_(&dctx)); 1636 { 1637 const auto a = DeviceArrayWrite(dctx, A); 1638 PetscInt lda; 1639 1640 PetscCall(MatDenseGetLDA(A, &lda)); 1641 if (lda > m) { 1642 for (PetscInt i = 0; i < n; i++) PetscCall(PetscRandomGetValues(rng, m, a.data() + i * lda)); 1643 } else { 1644 PetscInt mn; 1645 1646 PetscCall(PetscIntMultError(m, n, &mn)); 1647 PetscCall(PetscRandomGetValues(rng, mn, a)); 1648 } 1649 } 1650 } else { 1651 PetscCall(MatSetRandom_SeqDense(A, rng)); 1652 } 1653 PetscFunctionReturn(PETSC_SUCCESS); 1654 } 1655 1656 // ========================================================================================== 1657 1658 template <device::cupm::DeviceType T> 1659 inline PetscErrorCode MatDense_Seq_CUPM<T>::GetColumnVector(Mat A, Vec v, PetscInt col) noexcept 1660 { 1661 const auto offloadmask = A->offloadmask; 1662 const auto n = A->rmap->n; 1663 const auto col_offset = [&](const PetscScalar *ptr) { return ptr + col * MatIMPLCast(A)->lda; }; 1664 PetscBool viscupm; 1665 PetscDeviceContext dctx; 1666 cupmStream_t stream; 1667 1668 PetscFunctionBegin; 1669 PetscCall(PetscObjectTypeCompareAny(PetscObjectCast(v), &viscupm, VecSeq_CUPM::VECSEQCUPM(), VecSeq_CUPM::VECMPICUPM(), VecSeq_CUPM::VECCUPM(), "")); 1670 PetscCall(GetHandles_(&dctx, &stream)); 1671 if (viscupm && !v->boundtocpu) { 1672 const auto x = VecSeq_CUPM::DeviceArrayWrite(dctx, v); 1673 1674 // update device data 1675 if (PetscOffloadDevice(offloadmask)) { 1676 PetscCall(PetscCUPMMemcpyAsync(x.data(), col_offset(DeviceArrayRead(dctx, A)), n, cupmMemcpyDeviceToDevice, stream)); 1677 } else { 1678 PetscCall(PetscCUPMMemcpyAsync(x.data(), col_offset(HostArrayRead(dctx, A)), n, cupmMemcpyHostToDevice, stream)); 1679 } 1680 } else { 1681 PetscScalar *x; 1682 1683 // update host data 1684 PetscCall(VecGetArrayWrite(v, &x)); 1685 if (PetscOffloadUnallocated(offloadmask) || PetscOffloadHost(offloadmask)) { 1686 PetscCall(PetscArraycpy(x, col_offset(HostArrayRead(dctx, A)), n)); 1687 } else if (PetscOffloadDevice(offloadmask)) { 1688 PetscCall(PetscCUPMMemcpyAsync(x, col_offset(DeviceArrayRead(dctx, A)), n, cupmMemcpyDeviceToHost, stream)); 1689 } 1690 PetscCall(VecRestoreArrayWrite(v, &x)); 1691 } 1692 PetscFunctionReturn(PETSC_SUCCESS); 1693 } 1694 1695 template <device::cupm::DeviceType T> 1696 template <PetscMemoryAccessMode access> 1697 inline PetscErrorCode MatDense_Seq_CUPM<T>::GetColumnVec(Mat A, PetscInt col, Vec *v) noexcept 1698 { 1699 using namespace vec::cupm; 1700 const auto mimpl = MatIMPLCast(A); 1701 PetscDeviceContext dctx; 1702 1703 PetscFunctionBegin; 1704 PetscCheck(!mimpl->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first"); 1705 PetscCheck(!mimpl->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first"); 1706 mimpl->vecinuse = col + 1; 1707 if (!mimpl->cvec) PetscCall(MatDenseCreateColumnVec_Private(A, &mimpl->cvec)); 1708 PetscCall(GetHandles_(&dctx)); 1709 PetscCall(GetArray<PETSC_MEMTYPE_DEVICE, access>(A, const_cast<PetscScalar **>(&mimpl->ptrinuse), dctx)); 1710 PetscCall(VecCUPMPlaceArrayAsync<T>(mimpl->cvec, mimpl->ptrinuse + static_cast<std::size_t>(col) * static_cast<std::size_t>(mimpl->lda))); 1711 if (access == PETSC_MEMORY_ACCESS_READ) PetscCall(VecLockReadPush(mimpl->cvec)); 1712 *v = mimpl->cvec; 1713 PetscFunctionReturn(PETSC_SUCCESS); 1714 } 1715 1716 template <device::cupm::DeviceType T> 1717 template <PetscMemoryAccessMode access> 1718 inline PetscErrorCode MatDense_Seq_CUPM<T>::RestoreColumnVec(Mat A, PetscInt, Vec *v) noexcept 1719 { 1720 using namespace vec::cupm; 1721 const auto mimpl = MatIMPLCast(A); 1722 const auto cvec = mimpl->cvec; 1723 PetscDeviceContext dctx; 1724 1725 PetscFunctionBegin; 1726 PetscCheck(mimpl->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetColumnVec() first"); 1727 PetscCheck(cvec, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column vector"); 1728 mimpl->vecinuse = 0; 1729 if (access == PETSC_MEMORY_ACCESS_READ) PetscCall(VecLockReadPop(cvec)); 1730 PetscCall(VecCUPMResetArrayAsync<T>(cvec)); 1731 PetscCall(GetHandles_(&dctx)); 1732 PetscCall(RestoreArray<PETSC_MEMTYPE_DEVICE, access>(A, const_cast<PetscScalar **>(&mimpl->ptrinuse), dctx)); 1733 if (v) *v = nullptr; 1734 PetscFunctionReturn(PETSC_SUCCESS); 1735 } 1736 1737 // ========================================================================================== 1738 1739 template <device::cupm::DeviceType T> 1740 inline PetscErrorCode MatDense_Seq_CUPM<T>::GetFactor(Mat A, MatFactorType ftype, Mat *fact_out) noexcept 1741 { 1742 Mat fact = nullptr; 1743 PetscDeviceContext dctx; 1744 1745 PetscFunctionBegin; 1746 PetscCall(GetHandles_(&dctx)); 1747 PetscCall(MatCreateSeqDenseCUPM<T>(PetscObjectComm(PetscObjectCast(A)), A->rmap->n, A->cmap->n, nullptr, &fact, dctx, /* preallocate */ false)); 1748 fact->factortype = ftype; 1749 switch (ftype) { 1750 case MAT_FACTOR_LU: 1751 case MAT_FACTOR_ILU: // fall-through 1752 fact->ops->lufactorsymbolic = MatLUFactorSymbolic_SeqDense; 1753 fact->ops->ilufactorsymbolic = MatLUFactorSymbolic_SeqDense; 1754 break; 1755 case MAT_FACTOR_CHOLESKY: 1756 case MAT_FACTOR_ICC: // fall-through 1757 fact->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqDense; 1758 break; 1759 case MAT_FACTOR_QR: { 1760 const auto pobj = PetscObjectCast(fact); 1761 1762 PetscCall(PetscObjectComposeFunction(pobj, "MatQRFactor_C", MatQRFactor_SeqDense)); 1763 PetscCall(PetscObjectComposeFunction(pobj, "MatQRFactorSymbolic_C", MatQRFactorSymbolic_SeqDense)); 1764 } break; 1765 case MAT_FACTOR_NONE: 1766 case MAT_FACTOR_ILUDT: // fall-through 1767 case MAT_FACTOR_NUM_TYPES: // fall-through 1768 SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatFactorType %s not supported", MatFactorTypes[ftype]); 1769 } 1770 PetscCall(PetscStrFreeAllocpy(MATSOLVERCUPM(), &fact->solvertype)); 1771 PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, const_cast<char **>(fact->preferredordering) + MAT_FACTOR_LU)); 1772 PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, const_cast<char **>(fact->preferredordering) + MAT_FACTOR_ILU)); 1773 PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, const_cast<char **>(fact->preferredordering) + MAT_FACTOR_CHOLESKY)); 1774 PetscCall(PetscStrallocpy(MATORDERINGEXTERNAL, const_cast<char **>(fact->preferredordering) + MAT_FACTOR_ICC)); 1775 *fact_out = fact; 1776 PetscFunctionReturn(PETSC_SUCCESS); 1777 } 1778 1779 template <device::cupm::DeviceType T> 1780 inline PetscErrorCode MatDense_Seq_CUPM<T>::InvertFactors(Mat A) noexcept 1781 { 1782 const auto mimpl = MatIMPLCast(A); 1783 const auto mcu = MatCUPMCast(A); 1784 const auto n = static_cast<cupmBlasInt_t>(A->cmap->n); 1785 cupmSolverHandle_t handle; 1786 PetscDeviceContext dctx; 1787 cupmStream_t stream; 1788 1789 PetscFunctionBegin; 1790 #if PetscDefined(HAVE_CUDA) && PetscDefined(USING_NVCC) 1791 // HIP appears to have this by default?? 1792 PetscCheck(PETSC_PKG_CUDA_VERSION_GE(10, 1, 0), PETSC_COMM_SELF, PETSC_ERR_SUP, "Upgrade to CUDA version 10.1.0 or higher"); 1793 #endif 1794 if (!n || !A->rmap->n) PetscFunctionReturn(PETSC_SUCCESS); 1795 PetscCheck(A->factortype == MAT_FACTOR_CHOLESKY, PETSC_COMM_SELF, PETSC_ERR_LIB, "Factor type %s not implemented", MatFactorTypes[A->factortype]); 1796 // spd 1797 PetscCheck(!mcu->d_fact_ipiv, PETSC_COMM_SELF, PETSC_ERR_LIB, "%sDnsytri not implemented", cupmSolverName()); 1798 1799 PetscCall(GetHandles_(&dctx, &handle, &stream)); 1800 { 1801 const auto da = DeviceArrayReadWrite(dctx, A); 1802 const auto lda = static_cast<cupmBlasInt_t>(mimpl->lda); 1803 cupmBlasInt_t il; 1804 1805 PetscCallCUPMSOLVER(cupmSolverXpotri_bufferSize(handle, CUPMSOLVER_FILL_MODE_LOWER, n, da.cupmdata(), lda, &il)); 1806 if (il > mcu->d_fact_lwork) { 1807 mcu->d_fact_lwork = il; 1808 PetscCallCUPM(cupmFreeAsync(mcu->d_fact_work, stream)); 1809 PetscCall(PetscCUPMMallocAsync(&mcu->d_fact_work, il, stream)); 1810 } 1811 PetscCall(PetscLogGpuTimeBegin()); 1812 PetscCallCUPMSOLVER(cupmSolverXpotri(handle, CUPMSOLVER_FILL_MODE_LOWER, n, da.cupmdata(), lda, mcu->d_fact_work, mcu->d_fact_lwork, mcu->d_fact_info)); 1813 PetscCall(PetscLogGpuTimeEnd()); 1814 } 1815 PetscCall(CheckCUPMSolverInfo_(mcu->d_fact_info, stream)); 1816 // TODO (write cuda kernel) 1817 PetscCall(MatSeqDenseSymmetrize_Private(A, PETSC_TRUE)); 1818 PetscCall(PetscLogGpuFlops(1.0 * n * n * n / 3.0)); 1819 1820 A->ops->solve = nullptr; 1821 A->ops->solvetranspose = nullptr; 1822 A->ops->matsolve = nullptr; 1823 A->factortype = MAT_FACTOR_NONE; 1824 1825 PetscCall(PetscFree(A->solvertype)); 1826 PetscFunctionReturn(PETSC_SUCCESS); 1827 } 1828 1829 // ========================================================================================== 1830 1831 template <device::cupm::DeviceType T> 1832 inline PetscErrorCode MatDense_Seq_CUPM<T>::GetSubMatrix(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *mat) noexcept 1833 { 1834 const auto mimpl = MatIMPLCast(A); 1835 const auto array_offset = [&](PetscScalar *ptr) { return ptr + rbegin + static_cast<std::size_t>(cbegin) * mimpl->lda; }; 1836 const auto n = rend - rbegin; 1837 const auto m = cend - cbegin; 1838 auto &cmat = mimpl->cmat; 1839 PetscDeviceContext dctx; 1840 1841 PetscFunctionBegin; 1842 PetscCheck(!mimpl->vecinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreColumnVec() first"); 1843 PetscCheck(!mimpl->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseRestoreSubMatrix() first"); 1844 mimpl->matinuse = cbegin + 1; 1845 1846 PetscCall(GetHandles_(&dctx)); 1847 PetscCall(HostToDevice_(A, dctx)); 1848 1849 if (cmat && ((m != cmat->cmap->N) || (n != cmat->rmap->N))) PetscCall(MatDestroy(&cmat)); 1850 { 1851 const auto device_array = array_offset(MatCUPMCast(A)->d_v); 1852 1853 if (cmat) { 1854 PetscCall(PlaceArray(cmat, device_array)); 1855 } else { 1856 PetscCall(MatCreateSeqDenseCUPM<T>(PetscObjectComm(PetscObjectCast(A)), n, m, device_array, &cmat, dctx)); 1857 } 1858 } 1859 PetscCall(MatDenseSetLDA(cmat, mimpl->lda)); 1860 // place CPU array if present but do not copy any data 1861 if (const auto host_array = mimpl->v) { 1862 cmat->offloadmask = PETSC_OFFLOAD_GPU; 1863 PetscCall(MatDensePlaceArray(cmat, array_offset(host_array))); 1864 } 1865 1866 cmat->offloadmask = A->offloadmask; 1867 *mat = cmat; 1868 PetscFunctionReturn(PETSC_SUCCESS); 1869 } 1870 1871 template <device::cupm::DeviceType T> 1872 inline PetscErrorCode MatDense_Seq_CUPM<T>::RestoreSubMatrix(Mat A, Mat *m) noexcept 1873 { 1874 const auto mimpl = MatIMPLCast(A); 1875 const auto cmat = mimpl->cmat; 1876 const auto reset = static_cast<bool>(mimpl->v); 1877 bool copy, was_offload_host; 1878 1879 PetscFunctionBegin; 1880 PetscCheck(mimpl->matinuse, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Need to call MatDenseGetSubMatrix() first"); 1881 PetscCheck(cmat, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing internal column matrix"); 1882 PetscCheck(*m == cmat, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not the matrix obtained from MatDenseGetSubMatrix()"); 1883 mimpl->matinuse = 0; 1884 1885 // calls to ResetArray may change it, so save it here 1886 was_offload_host = cmat->offloadmask == PETSC_OFFLOAD_CPU; 1887 if (was_offload_host && !reset) { 1888 copy = true; 1889 PetscCall(MatSeqDenseSetPreallocation(A, nullptr)); 1890 } else { 1891 copy = false; 1892 } 1893 1894 PetscCall(ResetArray(cmat)); 1895 if (reset) PetscCall(MatDenseResetArray(cmat)); 1896 if (copy) { 1897 PetscDeviceContext dctx; 1898 1899 PetscCall(GetHandles_(&dctx)); 1900 PetscCall(DeviceToHost_(A, dctx)); 1901 } else { 1902 A->offloadmask = was_offload_host ? PETSC_OFFLOAD_CPU : PETSC_OFFLOAD_GPU; 1903 } 1904 1905 cmat->offloadmask = PETSC_OFFLOAD_UNALLOCATED; 1906 *m = nullptr; 1907 PetscFunctionReturn(PETSC_SUCCESS); 1908 } 1909 1910 // ========================================================================================== 1911 1912 namespace 1913 { 1914 1915 template <device::cupm::DeviceType T> 1916 inline PetscErrorCode MatMatMultNumeric_SeqDenseCUPM_SeqDenseCUPM(Mat A, Mat B, Mat C, PetscBool TA, PetscBool TB) noexcept 1917 { 1918 PetscFunctionBegin; 1919 if (TA) { 1920 if (TB) { 1921 PetscCall(MatDense_Seq_CUPM<T>::template MatMatMult_Numeric_Dispatch<true, true>(A, B, C)); 1922 } else { 1923 PetscCall(MatDense_Seq_CUPM<T>::template MatMatMult_Numeric_Dispatch<true, false>(A, B, C)); 1924 } 1925 } else { 1926 if (TB) { 1927 PetscCall(MatDense_Seq_CUPM<T>::template MatMatMult_Numeric_Dispatch<false, true>(A, B, C)); 1928 } else { 1929 PetscCall(MatDense_Seq_CUPM<T>::template MatMatMult_Numeric_Dispatch<false, false>(A, B, C)); 1930 } 1931 } 1932 PetscFunctionReturn(PETSC_SUCCESS); 1933 } 1934 1935 template <device::cupm::DeviceType T> 1936 inline PetscErrorCode MatSolverTypeRegister_DENSECUPM() noexcept 1937 { 1938 PetscFunctionBegin; 1939 for (auto ftype : util::make_array(MAT_FACTOR_LU, MAT_FACTOR_CHOLESKY, MAT_FACTOR_QR)) { 1940 PetscCall(MatSolverTypeRegister(MatDense_Seq_CUPM<T>::MATSOLVERCUPM(), MATSEQDENSE, ftype, MatDense_Seq_CUPM<T>::GetFactor)); 1941 PetscCall(MatSolverTypeRegister(MatDense_Seq_CUPM<T>::MATSOLVERCUPM(), MatDense_Seq_CUPM<T>::MATSEQDENSECUPM(), ftype, MatDense_Seq_CUPM<T>::GetFactor)); 1942 } 1943 PetscFunctionReturn(PETSC_SUCCESS); 1944 } 1945 1946 } // anonymous namespace 1947 1948 } // namespace impl 1949 1950 } // namespace cupm 1951 1952 } // namespace mat 1953 1954 } // namespace Petsc 1955