1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CUSOLVER_CONTEXT_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CUSOLVER_CONTEXT_H_ 18 19 #include <complex> 20 21 #if !TENSORFLOW_USE_ROCM 22 #include "third_party/gpus/cuda/include/cusolverDn.h" 23 using gpusolverHandle_t = cusolverDnHandle_t; 24 #else 25 #include "tensorflow/stream_executor/rocm/rocsolver_wrapper.h" 26 using gpusolverHandle_t = rocblas_handle; 27 #endif 28 29 #include "tensorflow/compiler/xla/statusor.h" 30 #include "tensorflow/compiler/xla/types.h" 31 #include "tensorflow/compiler/xla/util.h" 32 #include "tensorflow/core/lib/core/status.h" 33 #include "tensorflow/core/platform/stream_executor_no_cuda.h" 34 #include "tensorflow/stream_executor/blas.h" 35 36 namespace xla { 37 namespace gpu { 38 39 class GpuSolverContext { 40 public: 41 // stream may be nullptr, in which case the context can only be used for 42 // buffer size queries. 43 static StatusOr<GpuSolverContext> Create(se::Stream* stream); 44 GpuSolverContext() = default; 45 ~GpuSolverContext(); 46 47 GpuSolverContext(const GpuSolverContext&) = delete; 48 GpuSolverContext(GpuSolverContext&&); 49 GpuSolverContext& operator=(const GpuSolverContext&) = delete; 50 GpuSolverContext& operator=(GpuSolverContext&&); 51 52 // Computes the Cholesky factorization A = L * L^T for a single matrix. 53 // Returns Status::OK() if the kernel was launched successfully. See: 54 // http://docs.nvidia.com/cuda/cusolver/#cuds-lt-t-gt-potrf 55 template <typename T, typename = std::enable_if_t< 56 std::is_same<T, float>::value || 57 std::is_same<T, double>::value || 58 std::is_same<T, std::complex<float>>::value || 59 std::is_same<T, std::complex<double>>::value>> 60 Status Potrf(se::blas::UpperLower uplo, int n, se::DeviceMemory<T> dev_A, 61 int lda, se::DeviceMemory<int> dev_lapack_info, 62 se::DeviceMemory<T> workspace) = delete; 63 64 // Returns the size of the `workspace` required by Potrf, in number of 65 // elements of `type`. 66 StatusOr<int64> PotrfBufferSize(PrimitiveType type, se::blas::UpperLower uplo, 67 int n, int lda); 68 69 private: 70 GpuSolverContext(se::Stream* stream, gpusolverHandle_t handle); 71 handle()72 gpusolverHandle_t handle() const { return handle_; } 73 74 se::Stream* stream_ = nullptr; 75 gpusolverHandle_t handle_ = nullptr; 76 }; 77 78 #define CALL_LAPACK_TYPES(m) \ 79 m(float, S) m(double, D) m(std::complex<float>, C) m(std::complex<double>, Z) 80 #define POTRF_INSTANCE(T, type_prefix) \ 81 template <> \ 82 Status GpuSolverContext::Potrf<T>( \ 83 se::blas::UpperLower uplo, int n, se::DeviceMemory<T> A, int lda, \ 84 se::DeviceMemory<int> lapack_info, se::DeviceMemory<T> workspace); 85 CALL_LAPACK_TYPES(POTRF_INSTANCE); 86 #undef POTRF_INSTANCE 87 #undef CALL_LAPACK_TYPES 88 89 } // namespace gpu 90 } // namespace xla 91 92 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_CUSOLVER_CONTEXT_H_ 93