forked from JuliaGPU/CUDA.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multigpu.jl
338 lines (316 loc) · 13.2 KB
/
multigpu.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
## wrappers for multi-gpu functionality in cusolverMg
# auxiliary functionality
# NOTE: in the cublasMg preview, which also relies on this functionality, a separate library
# called 'cudalibmg' is introduced. factor this out when we actually ship that.
mutable struct MatrixDescriptor
desc::cudaLibMgMatrixDesc_t
function MatrixDescriptor(a, grid; rowblocks = size(a, 1), colblocks = size(a, 2), elta=eltype(a) )
desc = Ref{cudaLibMgMatrixDesc_t}()
cusolverMgCreateMatrixDesc(desc, size(a, 1), size(a, 2), rowblocks, colblocks, elta, grid)
return new(desc[])
end
end
Base.unsafe_convert(::Type{cudaLibMgMatrixDesc_t}, obj::MatrixDescriptor) = obj.desc
mutable struct DeviceGrid
desc::cudaLibMgGrid_t
function DeviceGrid(num_row_devs, num_col_devs, deviceIds, mapping)
@assert num_row_devs == 1 "Only 1-D column block cyclic is supported, so numRowDevices must be equal to 1."
desc = Ref{cudaLibMgGrid_t}()
cusolverMgCreateDeviceGrid(desc, num_row_devs, num_col_devs, deviceIds, mapping)
return new(desc[])
end
end
Base.unsafe_convert(::Type{cudaLibMgGrid_t}, obj::DeviceGrid) = obj.desc
function allocateBuffers(n_row_devs, n_col_devs, mat::Matrix)
mat_row_block_size = div(size(mat, 1), n_row_devs)
mat_col_block_size = div(size(mat, 2), n_col_devs)
mat_buffers = Vector{CuMatrix{eltype(mat)}}(undef, ndevices())
mat_numRows = Vector{Int64}(undef, ndevices())
mat_numCols = Vector{Int64}(undef, ndevices())
typesize = sizeof(eltype(mat))
ldas = Vector{Int64}(undef, ndevices())
current_dev = device()
mat_cpu_bufs = Vector{Matrix{eltype(mat)}}(undef, ndevices())
for (di, dev) in enumerate(devices())
ldas[di] = mat_col_block_size
dev_row = mod(di - 1, n_row_devs) + 1
dev_col = div(di - 1, n_row_devs) + 1
mat_row_inds = ((dev_row-1)*mat_row_block_size+1):min(dev_row*mat_row_block_size, size(mat, 1))
mat_col_inds = ((dev_col-1)*mat_col_block_size+1):min(dev_col*mat_col_block_size, size(mat, 2))
mat_cpu_bufs[di] = Array(mat[mat_row_inds, mat_col_inds])
end
for (di, dev) in enumerate(devices())
device!(dev)
mat_gpu_buf = CuMatrix{eltype(mat)}(undef, size(mat_cpu_bufs[di]))
copyto!(mat_gpu_buf, mat_cpu_bufs[di])
mat_buffers[di] = mat_gpu_buf
end
for (di, dev) in enumerate(devices())
device!(dev)
synchronize()
end
device!(current_dev)
return mat_buffers
end
function returnBuffers(n_row_devs, n_col_devs, row_block_size, col_block_size, dDs, D)
row_block_size = div(size(D, 1), n_row_devs)
col_block_size = div(size(D, 2), n_col_devs)
numRows = [row_block_size for dev in 1:ndevices()]
numCols = [col_block_size for dev in 1:ndevices()]
typesize = sizeof(eltype(D))
current_dev = device()
cpu_bufs = Vector{Matrix{eltype(D)}}(undef, ndevices())
for (di, dev) in enumerate(devices())
device!(dev)
dev_row = mod(di - 1, n_row_devs) + 1
dev_col = div(di - 1, n_row_devs) + 1
row_inds = ((dev_row-1)*row_block_size+1):min(dev_row*row_block_size, size(D, 1))
col_inds = ((dev_col-1)*col_block_size+1):min(dev_col*col_block_size, size(D, 2))
cpu_bufs[di] = Matrix{eltype(D)}(undef, length(row_inds), length(col_inds))
copyto!(cpu_bufs[di], dDs[di])
end
for (di, dev) in enumerate(devices())
device!(dev)
synchronize()
dev_row = mod(di - 1, n_row_devs) + 1
dev_col = div(di - 1, n_row_devs) + 1
row_inds = ((dev_row-1)*row_block_size+1):min(dev_row*row_block_size, size(D, 1))
col_inds = ((dev_col-1)*col_block_size+1):min(dev_col*col_block_size, size(D, 2))
D[row_inds, col_inds] = cpu_bufs[di]
end
device!(current_dev)
return D
end
## wrappers
function mg_syevd!(jobz::Char, uplo::Char, A; dev_rows=1, dev_cols=ndevices()) # one host-side array A
dev = device()
grid = DeviceGrid(1, ndevices(), devices(), CUDALIBMG_GRID_MAPPING_COL_MAJOR)
if uplo != 'L'
throw(ArgumentError("only lower fill mode (uplo='L') supported"))
end
m, n = size(A)
N = div(size(A, 2), ndevices()) # dimension of the sub-matrix
lwork = Ref{Int64}(0)
workspace = Vector{CuArray}(undef, ndevices())
W = Vector{real(eltype(A))}(undef, n)
desc = MatrixDescriptor(A, grid; colblocks=N)
A_arr = allocateBuffers(dev_rows, dev_cols, A)
IA = 1 # for now
JA = 1
GC.@preserve A_arr begin
cusolverMgSyevd_bufferSize(mg_handle(), jobz, uplo, n, pointer.(A_arr), IA, JA, desc, W, real(eltype(A)), eltype(A), lwork)
end
for (di, dev) in enumerate(devices())
device!(dev)
workspace[di] = CUDA.zeros(eltype(A), lwork[])
synchronize()
end
device!(dev)
info = Ref{Cint}(C_NULL)
GC.@preserve A_arr workspace begin
cusolverMgSyevd(mg_handle(), jobz, uplo, n, pointer.(A_arr), IA, JA, desc, W, real(eltype(A)), eltype(A), pointer.(workspace), lwork[], info)
end
if info[] < 0
throw(ArgumentError("The $(info[])th parameter is wrong"))
end
A = returnBuffers(dev_rows, dev_cols, div(size(A, 1), dev_rows), div(size(A, 2), dev_cols), A_arr, A)
if jobz == 'N'
return W
elseif jobz == 'V'
return W, A
end
end
function mg_potrf!(uplo::Char, A; dev_rows=1, dev_cols=ndevices()) # one host-side array A
dev = device()
grid = DeviceGrid(1, ndevices(), devices(), CUDALIBMG_GRID_MAPPING_COL_MAJOR)
if uplo != 'L'
throw(ArgumentError("only lower fill mode (uplo='L') supported"))
end
m, n = size(A)
N = div(size(A, 2), ndevices()) # dimension of the sub-matrix
lwork = Ref{Int64}(0)
workspace = Vector{CuArray}(undef, ndevices())
desc = MatrixDescriptor(A, grid; colblocks=N)
A_arr = allocateBuffers(dev_rows, dev_cols, A)
IA = 1 # for now
JA = 1
GC.@preserve A_arr begin
cusolverMgPotrf_bufferSize(mg_handle(), uplo, n, pointer.(A_arr), IA, JA, desc, eltype(A), lwork)
end
for (di, dev) in enumerate(devices())
device!(dev)
workspace[di] = CUDA.zeros(eltype(A), lwork[])
synchronize()
end
device!(dev)
info = Ref{Cint}(C_NULL)
GC.@preserve A_arr workspace begin
cusolverMgPotrf(mg_handle(), uplo, n, pointer.(A_arr), IA, JA, desc, eltype(A), pointer.(workspace), lwork[], info)
end
if info[] < 0
throw(ArgumentError("The $(info[])th parameter is wrong"))
end
A = returnBuffers(dev_rows, dev_cols, div(size(A, 1), dev_rows), div(size(A, 2), dev_cols), A_arr, A)
return A
end
function mg_potri!(uplo::Char, A; dev_rows=1, dev_cols=ndevices()) # one host-side array A
dev = device()
grid = DeviceGrid(1, ndevices(), devices(), CUDALIBMG_GRID_MAPPING_COL_MAJOR)
if uplo != 'L'
throw(ArgumentError("only lower fill mode (uplo='L') supported"))
end
m, n = size(A)
N = div(size(A, 2), ndevices()) # dimension of the sub-matrix
lwork = Ref{Int64}(0)
workspace = Vector{CuArray}(undef, ndevices())
desc = MatrixDescriptor(A, grid; colblocks=N)
A_arr = allocateBuffers(dev_rows, dev_cols, A)
IA = 1 # for now
JA = 1
GC.@preserve A_arr begin
cusolverMgPotri_bufferSize(mg_handle(), uplo, n, pointer.(A_arr), IA, JA, desc, eltype(A), lwork)
end
for (di, dev) in enumerate(devices())
device!(dev)
workspace[di] = CUDA.zeros(eltype(A), lwork[])
synchronize()
end
device!(dev)
info = Ref{Cint}(C_NULL)
GC.@preserve A_arr workspace begin
cusolverMgPotri(mg_handle(), uplo, n, pointer.(A_arr), IA, JA, desc, eltype(A), pointer.(workspace), lwork[], info)
end
if info[] < 0
throw(ArgumentError("The $(info[])th parameter is wrong"))
end
A = returnBuffers(dev_rows, dev_cols, div(size(A, 1), dev_rows), div(size(A, 2), dev_cols), A_arr, A)
return A
end
function mg_potrs!(uplo::Char, A, B; dev_rows=1, dev_cols=ndevices()) # one host-side array A
dev = device()
grid = DeviceGrid(1, ndevices(), devices(), CUDALIBMG_GRID_MAPPING_COL_MAJOR)
if uplo != 'L'
throw(ArgumentError("only lower fill mode (uplo='L') supported"))
end
ma, na = size(A)
mb, nb = size(B)
NA = div(size(A, 2), ndevices()) # dimension of the sub-matrix
NB = div(size(B, 2), ndevices()) # dimension of the sub-matrix
lwork = Ref{Int64}(0)
workspace = Vector{CuArray}(undef, ndevices())
descA = MatrixDescriptor(A, grid; colblocks=NA)
descB = MatrixDescriptor(A, grid; colblocks=NB)
A_arr = allocateBuffers(dev_rows, dev_cols, A)
B_arr = allocateBuffers(dev_rows, dev_cols, B)
IA = 1 # for now
JA = 1
IB = 1 # for now
JB = 1
GC.@preserve A_arr B_arr begin
cusolverMgPotrs_bufferSize(mg_handle(), uplo, na, nb, pointer.(A_arr), IA, JA, descA, pointer.(B_arr), IB, JB, descB, eltype(A), lwork)
end
for (di, dev) in enumerate(devices())
device!(dev)
workspace[di] = CUDA.zeros(eltype(A), lwork[])
synchronize()
end
device!(dev)
info = Ref{Cint}(C_NULL)
GC.@preserve A_arr B_arr workspace begin
cusolverMgPotrs(mg_handle(), uplo, na, nb, pointer.(A_arr), IA, JA, descA, pointer.(B_arr), IB, JB, descB, eltype(A), pointer.(workspace), lwork[], info)
end
if info[] < 0
throw(ArgumentError("The $(info[])th parameter is wrong"))
end
B = returnBuffers(dev_rows, dev_cols, div(size(B, 1), dev_rows), div(size(B, 2), dev_cols), B_arr, B)
return B
end
function mg_getrf!(A; dev_rows=1, dev_cols=ndevices()) # one host-side array A
dev = device()
grid = DeviceGrid(1, ndevices(), devices(), CUDALIBMG_GRID_MAPPING_COL_MAJOR)
m, n = size(A)
N = div(size(A, 2), ndevices()) # dimension of the sub-matrix
lwork = Ref{Int64}(0)
ipivs = Vector{CuVector{Cint}}(undef, ndevices())
workspace = Vector{CuArray}(undef, ndevices())
desc = MatrixDescriptor(A, grid; colblocks=N)
A_arr = allocateBuffers(dev_rows, dev_cols, A)
IA = 1 # for now
JA = 1
for (di, dev) in enumerate(devices())
device!(dev)
ipivs[di] = CUDA.zeros(Cint, N)
synchronize()
end
device!(dev)
GC.@preserve A_arr ipivs begin
cusolverMgGetrf_bufferSize(mg_handle(), m, n, pointer.(A_arr), IA, JA, desc, pointer.(ipivs), eltype(A), lwork)
end
device_synchronize()
for (di, dev) in enumerate(devices())
device!(dev)
workspace[di] = CUDA.zeros(eltype(A), lwork[])
synchronize()
end
device!(dev)
info = Ref{Cint}(C_NULL)
GC.@preserve A_arr ipivs workspace begin
cusolverMgGetrf(mg_handle(), m, n, pointer.(A_arr), IA, JA, desc, pointer.(ipivs), eltype(A), pointer.(workspace), lwork[], info)
end
device_synchronize()
if info[] < 0
throw(ArgumentError("The $(info[])th parameter is wrong"))
end
A = returnBuffers(dev_rows, dev_cols, div(size(A, 1), dev_rows), div(size(A, 2), dev_cols), A_arr, A)
ipiv = Vector{Int}(undef, n)
for (di, dev) in enumerate(devices())
device!(dev)
ipiv[((di-1)*N + 1):min((di*N), n)] = collect(ipivs[di])
end
device!(dev)
return A, ipiv
end
function mg_getrs!(trans, A, ipiv, B; dev_rows=1, dev_cols=ndevices()) # one host-side array A
dev = device()
grid = DeviceGrid(1, ndevices(), devices(), CUDALIBMG_GRID_MAPPING_COL_MAJOR)
ma, na = size(A)
mb, nb = size(B)
NA = div(size(A, 2), ndevices()) # dimension of the sub-matrix
NB = div(size(B, 2), ndevices()) # dimension of the sub-matrix
lwork = Ref{Int64}(0)
ipivs = Vector{CuVector{Cint}}(undef, ndevices())
workspace = Vector{CuArray}(undef, ndevices())
descA = MatrixDescriptor(A, grid; colblocks=NA)
descB = MatrixDescriptor(A, grid; colblocks=NB)
A_arr = allocateBuffers(dev_rows, dev_cols, A)
B_arr = allocateBuffers(dev_rows, dev_cols, B)
IA = 1 # for now
JA = 1
IB = 1 # for now
JB = 1
for (di, dev) in enumerate(devices())
device!(dev)
local_ipiv = Cint.(ipiv[(di-1)*NA+1:min(di*NA,length(ipiv))])
ipivs[di] = CuArray(local_ipiv)
synchronize()
end
device!(dev)
GC.@preserve A_arr B_arr ipivs begin
cusolverMgGetrs_bufferSize(mg_handle(), trans, na, nb, pointer.(A_arr), IA, JA, descA, pointer.(ipivs), pointer.(B_arr), IB, JB, descB, eltype(A), lwork)
end
for (di, dev) in enumerate(devices())
device!(dev)
workspace[di] = CUDA.zeros(eltype(A), lwork[])
synchronize()
end
device!(dev)
info = Ref{Cint}(C_NULL)
GC.@preserve A_arr B_arr ipivs workspace begin
cusolverMgGetrs(mg_handle(), trans, na, nb, pointer.(A_arr), IA, JA, descA, pointer.(ipivs), pointer.(B_arr), IB, JB, descB, eltype(A), pointer.(workspace), lwork[], info)
end
if info[] < 0
throw(ArgumentError("The $(info[])th parameter is wrong"))
end
B = returnBuffers(dev_rows, dev_cols, div(size(B, 1), dev_rows), div(size(B, 2), dev_cols), B_arr, B)
return B
end