DFT-EFE
 
Loading...
Searching...
No Matches
LinAlgOpContext.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2021. *
3 * The Regents of the University of Michigan and DFT-EFE developers. *
4 * *
5 * This file is part of the DFT-EFE code. *
6 * *
7 * DFT-EFE is free software: you can redistribute it and/or modify *
8 * it under the terms of the Lesser GNU General Public License as *
9 * published by the Free Software Foundation, either version 3 of *
10 * the License, or (at your option) any later version. *
11 * *
12 * DFT-EFE is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
15 * See the Lesser GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License at the top level of DFT-EFE distribution. If not, see *
19 * <https://www.gnu.org/licenses/>. *
20 ******************************************************************************/
21
22/*
23 * @author Ian C. Lin, Sambit Das
24 */
25
26#ifndef dftefeLinAlgOpContext_h
27#define dftefeLinAlgOpContext_h
28
30#include <memory>
31#include <utils/DeviceUtils.h>
38namespace dftefe
39{
40 namespace linearAlgebra
41 {
43 {
44 FP32,
45 TF32,
46 BF16,
47 FP16
48 };
49
50 template <utils::MemorySpace memorySpace>
52 {
53 public:
55
56 ~LinAlgOpContext() = default;
57
58 void
60 {
61 d_opType = opType;
62 }
63
66 {
67 return d_opType;
68 }
69
72
75 {
76#if defined(DFTEFE_WITH_DEVICE_LANG_SYCL)
77 enforceDefaultStreamExclusivity();
78#endif
79 return d_defaultStream;
80 }
81
84 {
85#if defined(DFTEFE_WITH_DEVICE_LANG_SYCL)
86 enforceDefaultStreamExclusivity();
87#endif
88 return d_deviceBlasHandle;
89 }
90
93 {
94 return d_numBlasStreams;
95 }
96
99 {
100#if defined(DFTEFE_WITH_DEVICE_LANG_SYCL)
101 enforceNonDefaultStreamExclusivity();
102#endif
103 return d_streams.data();
104 }
105
108 {
109#if defined(DFTEFE_WITH_DEVICE_LANG_SYCL)
110 enforceNonDefaultStreamExclusivity();
111#endif
112 return d_deviceBlasHandles.data();
113 }
114
115 private:
117 inline static std::vector<utils::deviceBlasHandle_t> d_deviceBlasHandles;
118 inline static std::vector<utils::deviceStream_t> d_streams;
119
122
125
126#if defined(DFTEFE_WITH_DEVICE_LANG_SYCL)
127 // SYCL queues (unlike CUDA/HIP streams) have no implicit ordering
128 // relationship with one another, so the default-stream/non-default-
129 // stream mutual exclusivity that CUDA/HIP get for free from the driver
130 // (stream 0 implicitly synchronizes with every other blocking stream)
131 // has to be enforced here explicitly for callers that go through
132 // getBlasStream()/getDeviceBlasHandle()/getBlasStreamsVec()/
133 // getDeviceBlasHandlesVec().
134 enum class ActiveStreamSide
135 {
136 Default,
137 NonDefault
138 };
139
140 inline static ActiveStreamSide d_activeStreamSide =
141 ActiveStreamSide::Default;
142
143 static void
144 enforceDefaultStreamExclusivity()
145 {
146 if (d_activeStreamSide == ActiveStreamSide::NonDefault)
147 {
148 for (auto &stream : d_streams)
149 utils::deviceStreamSynchronize(stream);
150 d_activeStreamSide = ActiveStreamSide::Default;
151 }
152 }
153
154 static void
155 enforceNonDefaultStreamExclusivity()
156 {
157 if (d_activeStreamSide == ActiveStreamSide::Default)
158 {
159 utils::deviceStreamSynchronize(d_defaultStream);
160 d_activeStreamSide = ActiveStreamSide::NonDefault;
161 }
162 }
163#endif
164
167 utils::deviceStream_t & streamId);
168
171
174
175 }; // end of LinAlgOpContext
176 } // end of namespace linearAlgebra
177} // end of namespace dftefe
179#endif // end of dftefeLinAlgOpContext_h
Definition: LinAlgOpContext.h:52
static utils::deviceStream_t * getBlasStreamsVec()
Definition: LinAlgOpContext.h:98
static std::vector< utils::deviceStream_t > d_streams
Definition: LinAlgOpContext.h:118
static utils::deviceStream_t & getBlasStream()
Definition: LinAlgOpContext.h:74
TensorOpDataType getTensorOpDataType()
Definition: LinAlgOpContext.h:65
TensorOpDataType d_opType
storage for deviceblas handle
Definition: LinAlgOpContext.h:124
static utils::deviceStream_t d_defaultStream
Definition: LinAlgOpContext.h:121
void setTensorOpDataType(TensorOpDataType opType)
Definition: LinAlgOpContext.h:59
static std::vector< utils::deviceBlasHandle_t > d_deviceBlasHandles
Definition: LinAlgOpContext.h:117
static utils::deviceBlasStatus_t setBlasStream(utils::deviceStream_t &streamId)
Definition: LinAlgOpContext.t.cpp:104
static utils::deviceBlasHandle_t & getDeviceBlasHandle()
Definition: LinAlgOpContext.h:83
size_type numBlasStreams() const
Definition: LinAlgOpContext.h:92
size_type d_numBlasStreams
Definition: LinAlgOpContext.h:116
utils::deviceBlasStatus_t create(utils::deviceBlasHandle_t &handleId)
Definition: LinAlgOpContext.t.cpp:129
static utils::deviceBlasHandle_t * getDeviceBlasHandlesVec()
Definition: LinAlgOpContext.h:107
utils::deviceBlasStatus_t destroy(utils::deviceBlasHandle_t &handleId)
Definition: LinAlgOpContext.t.cpp:145
static utils::deviceBlasHandle_t d_deviceBlasHandle
Definition: LinAlgOpContext.h:120
TensorOpDataType
Definition: LinAlgOpContext.h:43
cublasHandle_t deviceBlasHandle_t
Definition: DeviceTypeConfig.cu.h:36
cudaStream_t deviceStream_t
Definition: DeviceTypeConfig.cu.h:27
cublasStatus_t deviceBlasStatus_t
Definition: DeviceTypeConfig.cu.h:38
dealii includes
Definition: AtomFieldDataSpherical.cpp:31
std::uint64_t size_type
Definition: TypeConfig.h:9