-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_zgemm.jl
207 lines (191 loc) · 8.13 KB
/
test_zgemm.jl
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#=**
* Ported from OpenCLBLAS zGEMM example (example_sgemm.c)
* Qijia (Michael) Jin
* @version 0.0.1
*
* Copyright (C) 2016 Qijia (Michael) Jin
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*=#
const libclblas = Libdl.find_library(["clBLAS","libclBLAS"],["C:\\AMD\\clBLA-2.10.0\\bin","C:\\AMD\\acml6.1.0.33\\ifort64\\lib\\"])
#const libopencl = Libdl.find_library(["libOpenCL","OpenCL"],["."])
const libopencl = Libdl.find_library(["OpenCL64","OpenCL"],["C:\\Program Files\\NVIDIA Corporation\\OpenCL\\","C:\\Program Files (x86)\\AMD APP SDK\\2.9-1\\bin\\x86_64"])
if (isempty(libclblas))
print("clBLAS can't be found!")
end
include("cl_typedef.jl")
include("clblas_typedef.jl")
include("cl_functions.jl")
include("clblas_functions.jl")
#ccall((:function, “library”), return_type, (argtype,),arg)
function clblasZgemm(o,tA,tB,M,N,K,alpha,A,offA,lda,B,offB,ldb,beta,C,offC,ldc,ncq,cq,ne,wle,e)
return ccall((:clblasZgemm, libclblas), cl_int, (clblasOrder,
clblasTranspose,
clblasTranspose,
Csize_t,
Csize_t,
Csize_t,
Ref{clblasDoubleComplex},#treating this as a pointer fixed a segmentation fault
#clblasDoubleComplex,
cl_mem,
Csize_t,
Csize_t,
cl_mem,
Csize_t,
Csize_t,
Ref{clblasDoubleComplex},#treating this as a pointer fixed a segmentation fault
#clblasDoubleComplex,
#Base.cconvert(Ptr{Void}, Ref{cl_mem}),
#Ref{cl_mem},
cl_mem,
Csize_t,
Csize_t,
cl_uint,
Ptr{cl_command_queue},
cl_uint,
#Ref{cl_event},
#AMD's OpenCL driver (Windows 7 x64) throws invalid event if argument type is Ref{cl_event}
Ptr{cl_event},
Ptr{cl_event}),
#Ptr{cl_event_info},
#Ptr{cl_event_info}),
o,tA,tB,M,N,K,alpha,A,offA,lda,B,offB,ldb,beta,C,offC,ldc,ncq,cq,ne,wle,e)
end
function main()
local props = vec(convert(Array{cl_context_properties, 2}, [CL_CONTEXT_PLATFORM 0 0]))
devs = Array(cl_device_id, 1)
devs[1] = clGetFirstGPU()
local platform = clGetGPUPlatform(devs[1])
println(string("Selected GPU: ",clGetDeviceVendor(devs[1])), " ", clGetDeviceName(devs[1]))
props[2] = Base.cconvert(cl_context_properties,platform)
err = Array(cl_int, 1)
local ctx = clCreateContext(props,1,devs[1],C_NULL,C_NULL,err)
statusCheck(err[1])
err = Array(cl_int, 1)
local queue = Array(cl_command_queue, 1)
queue[1] = clCreateCommandQueue(ctx, devs[1], cl_command_queue_properties(0), err)
statusCheck(err[1])
################################ create arrays
A = convert(Array{clblasDoubleComplex,2}, [[11, 12, 13, 14, 15]';[21, 22, 23, 24, 25]';[31, 32, 33, 34, 35]';[41, 42, 43, 44, 45]'])
B = convert(Array{clblasDoubleComplex,2}, [[11, 12, 13]';[21, 22, 23]';[31, 32, 33]';[41, 42, 43]';[51, 52, 53]'])
C = convert(Array{clblasDoubleComplex,2}, [[11, 12, 13]';[21, 22, 23]';[31, 32, 33]';[41, 42, 43]'])
##A = convert(Array{cl_double2,2}, convert(Array{clblasDoubleComplex,2}, [[11, 12, 13, 14, 15]';[21, 22, 23, 24, 25]';[31, 32, 33, 34, 35]';[41, 42, 43, 44, 45]']))
##B = convert(Array{cl_double2,2}, convert(Array{clblasDoubleComplex,2}, [[11, 12, 13]';[21, 22, 23]';[31, 32, 33]';[41, 42, 43]';[51, 52, 53]']))
##C = convert(Array{cl_double2,2}, convert(Array{clblasDoubleComplex,2}, [[11, 12, 13]';[21, 22, 23]';[31, 32, 33]';[41, 42, 43]']))
A1 = vec(A)
B1 = vec(B)
C1 = vec(C)
M = Csize_t(length(A[:,1]))
K = Csize_t(length(B[:,1]))
N = Csize_t(length(B[1,:]))
order = clblasColumnMajor ##julia uses column major
alpha = Array(clblasDoubleComplex, 1)
alpha[1] = convert(clblasDoubleComplex, 10)
#println(string("alpha: ",alpha))
beta = Array(clblasDoubleComplex, 1)
beta[1] = convert(clblasDoubleComplex, 20)
#println(string("beta: ",beta))
transA = clblasNoTrans;
transB = clblasNoTrans;
off = convert(Csize_t, 0)
offA = convert(Csize_t, 0)
offB = convert(Csize_t, 0)
offC = convert(Csize_t, 0)
#Now initialize OpenCLBLAS and buffers
statusCheck(clblasSetup())
statusCheck(clFlush(queue[1]))
err = Array(cl_int, 1)
bufA = clCreateBuffer(ctx, CL_MEM_READ_ONLY, M * K * sizeof(clblasDoubleComplex), C_NULL, err)
statusCheck(err[1])
err = Array(cl_int, 1)
bufB = clCreateBuffer(ctx, CL_MEM_READ_ONLY, K * N * sizeof(clblasDoubleComplex), C_NULL, err)
statusCheck(err[1])
err = Array(cl_int, 1)
bufC = clCreateBuffer(ctx, CL_MEM_READ_WRITE, M * N * sizeof(clblasDoubleComplex), C_NULL, err)
statusCheck(err[1])
statusCheck(clFlush(queue[1]))
event = Array(cl_event, 1)
event[1] = C_NULL
statusCheck(clEnqueueWriteBuffer(queue[1], bufA, CL_TRUE, Csize_t(0), M * K * sizeof(clblasDoubleComplex), A1, cl_uint(0), C_NULL, event))
statusCheck(clWaitForEvents(1,event))
statusCheck(clReleaseEvent(event[1])) #free the memory
event[1] = C_NULL
statusCheck(clEnqueueWriteBuffer(queue[1], bufB, CL_TRUE, Csize_t(0), K * N * sizeof(clblasDoubleComplex), B1, cl_uint(0), C_NULL, event))
statusCheck(clWaitForEvents(1,event))
statusCheck(clReleaseEvent(event[1])) #free the memory
event[1] = C_NULL
statusCheck(clEnqueueWriteBuffer(queue[1], bufC, CL_TRUE, Csize_t(0), M * N * sizeof(clblasDoubleComplex), C1, cl_uint(0), C_NULL, event))
statusCheck(clWaitForEvents(1,event))
statusCheck(clReleaseEvent(event[1])) #free the memory
#=================Check respective buffer sizes in GPU
ref_count = Array(Csize_t, 1)
statusCheck(clGetMemObjectInfo(bufA, CL_MEM_SIZE, Csize_t(sizeof(ref_count)), ref_count, C_NULL))
println(string("bufA memory object size: ", Int32(ref_count[1])))
ref_count = 0
ref_count = Array(Csize_t, 1)
statusCheck(clGetMemObjectInfo(bufB, CL_MEM_SIZE, Csize_t(sizeof(ref_count)), ref_count, C_NULL))
println(string("bufB memory object size: ", Int32(ref_count[1])))
ref_count = 0
ref_count = Array(Csize_t, 1)
statusCheck(clGetMemObjectInfo(bufC, CL_MEM_SIZE, Csize_t(sizeof(ref_count)), ref_count, C_NULL))
println(string("bufC memory object size: ", Int32(ref_count[1])))
ref_count = 0
=====#
event[1] = C_NULL
#=
statusCheck(clblasSgemm(clblasRowMajor, clblasNoTrans, clblasNoTrans, M, N, K,
alpha, bufA, 0, K,
bufB, 0, N, beta,
bufC, 0, N,
1, queue, 0, C_NULL, event))
=#
statusCheck(clblasZgemm(clblasColumnMajor, clblasNoTrans, clblasNoTrans, M, N, K,
alpha, bufA, 0, M,
bufB, 0, K, beta,
bufC, 0, M,
1, queue, 0, C_NULL, event))
statusCheck(clFlush(queue[1]))
statusCheck(clWaitForEvents(1,event))
statusCheck(clReleaseEvent(event[1])) #free the memory
C2=Array(clblasDoubleComplex,length(C1))
event[1] = C_NULL
statusCheck(clEnqueueReadBuffer(queue[1], bufC, CL_TRUE, Csize_t(0), length(C1)*sizeof(clblasDoubleComplex), C2, cl_uint(0), C_NULL, event))
statusCheck(clWaitForEvents(1,event))
statusCheck(clReleaseEvent(event[1])) #free the memory
statusCheck(clFlush(queue[1]))
statusCheck(clReleaseMemObject(bufC))
statusCheck(clFlush(queue[1]))
statusCheck(clReleaseMemObject(bufB))
statusCheck(clFlush(queue[1]))
statusCheck(clReleaseMemObject(bufA))
statusCheck(clFlush(queue[1]))
#statusCheck(clGetMemObjectInfo(bufA, CL_MEM_REFERENCE_COUNT, Csize_t(sizeof(ref_count)), ref_count, C_NULL))
#bufA = C_NULL
#bufB = C_NULL
#bufC = C_NULL
clblasTeardown()
statusCheck(clFlush(queue[1]))
statusCheck(clReleaseCommandQueue(queue[1]))
statusCheck(clReleaseContext(ctx))
bufC = C_NULL
bufB = C_NULL
bufA = C_NULL
queue[1] = C_NULL
event[1] = C_NULL
ctx = C_NULL
devs[1] = C_NULL
Base.gc() ##not sure if julia has been garbage collecting, now is a good time though
return reshape(C2, Int(M), Int(N))
end
if (!isempty(libclblas) && !isempty(libopencl))
main()
end