-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathcublas_scope_handle.cpp
73 lines (63 loc) · 2.78 KB
/
cublas_scope_handle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/***************************************************************************
* Copyright (C) Codeplay Software Limited
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* For your convenience, a copy of the License has been included in this
* repository.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/
#include "cublas_scope_handle.hpp"
namespace oneapi {
namespace math {
namespace blas {
namespace cublas {
/**
* Inserts a new element in the map if its key is unique. This new element
* is constructed in place using args as the arguments for the construction
* of a value_type (which is an object of a pair type). The insertion only
* takes place if no other element in the container has a key equivalent to
* the one being emplaced (keys in a map container are unique).
*/
thread_local cublas_handle CublasScopedContextHandler::handle_helper = cublas_handle{};
CublasScopedContextHandler::CublasScopedContextHandler(sycl::interop_handle& ih) : ih(ih) {}
cublasHandle_t CublasScopedContextHandler::get_handle(const sycl::queue& queue) {
CUdevice device = ih.get_native_device<sycl::backend::ext_oneapi_cuda>();
CUstream streamId = get_stream(queue);
cublasStatus_t err;
auto it = handle_helper.cublas_handle_mapper_.find(device);
if (it != handle_helper.cublas_handle_mapper_.end()) {
cublasHandle_t nativeHandle = it->second;
cudaStream_t currentStreamId;
CUBLAS_ERROR_FUNC(cublasGetStream, err, nativeHandle, ¤tStreamId);
if (currentStreamId != streamId) {
CUBLAS_ERROR_FUNC(cublasSetStream, err, nativeHandle, streamId);
}
return nativeHandle;
}
cublasHandle_t nativeHandle;
CUBLAS_ERROR_FUNC(cublasCreate, err, &nativeHandle);
CUBLAS_ERROR_FUNC(cublasSetStream, err, nativeHandle, streamId);
auto insert_iter =
handle_helper.cublas_handle_mapper_.insert(std::make_pair(device, nativeHandle));
return nativeHandle;
}
CUstream CublasScopedContextHandler::get_stream(const sycl::queue& queue) {
return sycl::get_native<sycl::backend::ext_oneapi_cuda>(queue);
}
sycl::context CublasScopedContextHandler::get_context(const sycl::queue& queue) {
return queue.get_context();
}
} // namespace cublas
} // namespace blas
} // namespace math
} // namespace oneapi