-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathsycl_sgemm.cpp
More file actions
128 lines (107 loc) · 3.92 KB
/
sycl_sgemm.cpp
File metadata and controls
128 lines (107 loc) · 3.92 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/***************************************************************************
*
* Copyright (C) Codeplay Software Ltd.
*
* 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
*
* 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.
*
* Codeplay's SYCL-For-CUDA-Examples
*
* sycl_sgemm.cpp
*
* Description:
* SGEMM operation in SYCL with buffers
**************************************************************************/
#include <algorithm>
#include <iostream>
#include <vector>
#include <sycl/sycl.hpp>
#include <cublas_v2.h>
#include <cuda.h>
#define CHECK_ERROR(FUNC) checkCudaErrorMsg(FUNC, " " #FUNC)
void inline checkCudaErrorMsg(cublasStatus_t status, const char *msg) {
if (status != CUBLAS_STATUS_SUCCESS) {
std::cout << "ERROR CUBLAS:" << msg << " - " << status << std::endl;
exit(EXIT_FAILURE);
}
}
void inline checkCudaErrorMsg(cudaError status, const char *msg) {
if (status != cudaSuccess) {
std::cout << "ERROR CUDA: " << msg << " - " << status << std::endl;
exit(EXIT_FAILURE);
}
}
int main() {
using namespace sycl;
constexpr size_t WIDTH = 1024;
constexpr size_t HEIGHT = 1024;
constexpr float ALPHA = 1.0f;
constexpr float BETA = 0.0f;
std::vector<float> h_A(WIDTH * HEIGHT), h_B(WIDTH * HEIGHT),
h_C(WIDTH * HEIGHT);
std::cout << "Size: " << h_C.size() << std::endl;
// A is an identity matrix
std::fill(std::begin(h_A), std::end(h_A), 0.0f);
for (size_t i = 0; i < WIDTH; i++) {
h_A[i * WIDTH + i] = 1.0f;
}
// B is a matrix fill with 1
std::fill(std::begin(h_B), std::end(h_B), 1.0f);
sycl::queue q{[](auto &d) {
return (d.get_platform().get_backend() == sycl::backend::ext_oneapi_cuda);
}};
cublasHandle_t handle;
CHECK_ERROR(cublasCreate(&handle));
{
buffer<float, 2> b_A{h_A.data(), range<2>{WIDTH, HEIGHT}};
buffer<float, 2> b_B{h_B.data(), range<2>{WIDTH, HEIGHT}};
buffer<float, 2> b_C{h_C.data(), range<2>{WIDTH, HEIGHT}};
q.submit([&](handler &h) {
auto d_A = b_A.get_access<sycl::access::mode::read>(h);
auto d_B = b_B.get_access<sycl::access::mode::read>(h);
auto d_C = b_C.get_access<sycl::access::mode::write>(h);
h.host_task([=](sycl::interop_handle ih) {
// Set the correct cuda context & stream
cuCtxSetCurrent(ih.get_native_context<backend::ext_oneapi_cuda>());
auto cuStream = ih.get_native_queue<backend::ext_oneapi_cuda>();
cublasSetStream(handle, cuStream);
auto cuA = reinterpret_cast<float *>(
ih.get_native_mem<backend::ext_oneapi_cuda>(d_A));
auto cuB = reinterpret_cast<float *>(
ih.get_native_mem<backend::ext_oneapi_cuda>(d_B));
auto cuC = reinterpret_cast<float *>(
ih.get_native_mem<backend::ext_oneapi_cuda>(d_C));
CHECK_ERROR(cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N, WIDTH, HEIGHT,
WIDTH, &ALPHA, cuA, WIDTH, cuB, WIDTH, &BETA,
cuC, WIDTH));
cuStreamSynchronize(cuStream);
});
});
}
// C must be all ones
int i = 0;
const bool allEqual =
std::all_of(std::begin(h_C), std::end(h_C), [&i](float num) {
++i;
if (num != 1) {
std::cout << i << " Not one : " << num << std::endl;
}
return num == 1;
});
if (!allEqual) {
std::cout << " Incorrect result " << std::endl;
} else {
std::cout << " Correct! " << std::endl;
}
CHECK_ERROR(cublasDestroy(handle));
return allEqual ? EXIT_SUCCESS : EXIT_FAILURE;
}