-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemat_be_cuda.jl
599 lines (477 loc) · 14.8 KB
/
demat_be_cuda.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
## Delayed Expressions CUDA Backend
# Copyright 2012-2013, Krzysztof Kamieniecki ([email protected])
#TODO A LOT
## setup cuda library
#TODO should auto detect if the library is missing and give up
require("demat_base.jl")
require("libcuda.jl")
import Base.resize # we will add a new resize for our arrays
### CUDA objects, should go to different file
## Context
type jlCUContext
function jlCUContext()
me = new(cuCtxCreate(convert(Uint32,0),convert(Int32,0)));
finalizer(me,reset);
return me;
end
handle::CUcontext
end
function reset(ctxt::jlCUContext)
if ctxt.handle != convert(typeof(ctxt.handle),0)
cuCtxDestroy(ctxt.handle);
ctxt.handle = 0;
end
return ctxt;
end
## Host Memory Buffer - that supports concurrent transfers it is "pinned" (cannot be paged out)
# TODO
## Device Memory Buffer
type jlCUBuffer
function jlCUBuffer()
me = new(0,0);
finalizer(me,clear);
me
end
function jlCUBuffer(nSz::Number)
me = new(0,0);
finalizer(me,clear);
resize(me,nSz);
me
end
sz::CUsize_t
ptr::CUdeviceptr
end
function clear(buffer::jlCUBuffer)
if buffer.ptr != convert(typeof(buffer.ptr),0)
println("CUDA Clearing $buffer");
cuMemFree(buffer.ptr);
buffer.sz = 0;
buffer.ptr = 0;
end
return buffer;
end
function length(buffer::jlCUBuffer)
return buffer.sz;
end
function resize(buffer::jlCUBuffer,nsz)
clear(buffer);
buffer.ptr = cuMemAlloc(nsz);
buffer.sz = nsz;
return buffer;
end
function copyto{T}(dst::jlCUBuffer,src::Array{T,1})
@assert length(dst) == length(src)*sizeof(T);
cuMemcpyHtoD(dst.ptr,src,dst.sz);
return dst;
end
function copyto{T}(dst::Array{T,1},src::jlCUBuffer)
@assert length(dst)*sizeof(T) == length(src);
cuMemcpyDtoH(dst,src.ptr,src.sz);
return dst;
end
## Module
## Kernel
### Delayed matrix backend
type DeBackEndCuda
end
type DeArrCuda{T,N} <: DeArr{DeBackEndCuda,T,N}
function DeArrCuda()
me = new(0,jlCUBuffer());
me
end
function DeArrCuda(a::Array{T,N})
me = new(0,jlCUBuffer());
resize(me,length(a));
me[] = a;
me
end
function DeArrCuda(n::Number)
me = new(0,jlCUBuffer());
resize(me,n);
me
end
sz::CUsize_t
buffer::jlCUBuffer
end
function eltype{T,N}(arr::DeArrCuda{T,N})
return T;
end
function length{T}(arr::DeArrCuda{T,1})
return arr.sz;
end
function size{T}(arr::DeArrCuda{T,1})
return (arr.sz,);
end
function clear(arr::DeArrCuda)
clear(arr.buffer);
arr.sz = 0;
return arr;
end
function resize{T}(arr::DeArrCuda{T,1},nsz)
ncnt = sizeof(T) * nsz;
resize(arr.buffer,ncnt);
arr.sz = nsz;
return arr;
end
function assign{T}(dst::DeArrCuda{T,1},src::Array{T,1})
copyto(dst.buffer,src);
return dst;
end
function assign{T}(dst::Array{T,1},src::DeArrCuda{T,1})
copyto(dst,src.buffer);
return dst;
end
typealias DeVecCu{T} DeArrCuda{T,1}
typealias DeMatCu{T} DeArrCuda{T,2}
# DeCudaEnviroment used to count/allocate registers for PTX generation
const registerTypeToPrefix = {Float32=>"%f32_",Ptr{Float32}=>"%pf32_",Float64=>"%f64_",Ptr{Float64}=>"%pf64_",Uint32=>"%u32_",Ptr{Uint32}=>"%pu32_",Int32=>"%s32_",Ptr{Int32}=>"%ps32_"};
const cudaPtr = "u32"
const juliaTypeToCudaType = {Float32=>"f32",Ptr{Float32}=>cudaPtr,Float64=>"f64",Ptr{Float64}=>cudaPtr,Uint32=>"u32",Ptr{Uint32}=>cudaPtr,Int32=>"s32",Ptr{Int32}=>cudaPtr}
const cudaBPtr = "b32"
const juliaTypeToCudaBType = {Float32=>"b32",Ptr{Float32}=>cudaBPtr,Float64=>"b64",Ptr{Float64}=>cudaBPtr,Uint32=>"b32",Ptr{Uint32}=>cudaBPtr,Int32=>"b32",Ptr{Int32}=>cudaBPtr}
type DePtxEnv
function DePtxEnv()
me = new(Dict(),Array(Any,0),Array(String,0));
return me;
end
registerCounter::Dict;
paramTypes::Array{Any,1};
paramNames::Array{String,1};
end
type PtxRegister{dataType}
id::String;
end
function registerAlloc!(T,env::DePtxEnv)
prefix = registerTypeToPrefix[T];
if !has(env.registerCounter,T)
env.registerCounter[T] = 0;
end
id = env.registerCounter[T];
env.registerCounter[T] += 1;
return PtxRegister{T}("$(prefix)$(id)");
end
function registerType{dataType}(reg::PtxRegister{dataType})
return dataType;
end
function paramAlloc!(T,env::DePtxEnv)
paramIndex = length(env.paramTypes);
paramName = "%p$(paramIndex)";
push!(env.paramTypes,T);
push!(env.paramNames,paramName);
return (paramName,paramIndex);
end
abstract PtxOp;
type msGlobal end
type msParam end
type PtxOpLoad{memorySpace,dataType} <: PtxOp
dst::PtxRegister{dataType};
addr::Union(String,PtxRegister,(PtxRegister,Int32),Uint32); #named address, register, register+offset, absolute
end
type PtxOpStore{memorySpace,dataType} <: PtxOp
addr::Union(String,PtxRegister,(PtxRegister,Int32),Uint32); #named address, register, register+offset, absolute
src::PtxRegister{dataType};
end
type PtxOpBin{op,dataType} <: PtxOp
dst::PtxRegister{dataType};
op0::Union(Int64,Uint64,Float32,Float64,PtxRegister);
op1::Union(Int64,Uint64,Float32,Float64,PtxRegister);
end
# (registerType,register,param setup function, IR Ops)
function de_cuda_eltype(a::DeConst)
ptype = typeof(a.p1);
return ptype;
end
function de_cuda_eltype(a::DeReadOp)
return eltype(a.p1);
end
function de_cuda_eltype(a::DeBinOp)
p1type = de_cuda_eltype(a.p1);
p2type = de_cuda_eltype(a.p2);
if p1type == p2type
return p1type
else
error("DeMat:CUDA: Conversion between $p1type and $p2type not yet supported")
end
end
function de_eltype{T,N}(a::Type{Array{T,N}})
return T
end
function de_eltype{T}(a::Type{Ptr{T}})
return T
end
function de_eltype(a::Type)
return a
end
function de_cuda_eval(a::DeConst,env,paramOut,paramIn,indexReg)
# allocate destination register
rType = de_cuda_eltype(a);
r = registerAlloc!(rType,env);
# allocate space in param structure
(paramName,paramIndex) = paramAlloc!(rType,env);
paramSetup = quote ($paramOut)[$paramIndex+1] = [($paramIn).p1] end
ops = Array(PtxOp,0);
push!(ops,PtxOpLoad{msParam,rType}(r,paramName));
return ( rType , r , paramSetup , ops );
end
function de_cuda_eval(a::DeReadOp,env,paramOut,paramIn,indexReg)
# allocate ptr and destination register
rType = de_cuda_eltype(a);
r = registerAlloc!(rType,env);
srcType = Ptr{rType};
src = registerAlloc!(srcType,env);
offset = registerAlloc!(srcType,env);
srcOffset = registerAlloc!(srcType,env);
# allocate space in param structure
(paramName,paramIndex) = paramAlloc!(srcType,env);
paramSetup = quote ($paramOut)[$paramIndex+1] = [($paramIn).p1.buffer.ptr] end
ops = Array(PtxOp,0);
push!(ops, PtxOpLoad{msParam,srcType}(src,paramName));
push!(ops, PtxOpBin{:<<,srcType}(offset,indexReg,convert(Uint64,log2(sizeof(rType)))));
push!(ops, PtxOpBin{:+,srcType}(srcOffset,src,offset));
push!(ops, PtxOpLoad{msGlobal,rType}(r,srcOffset));
return ( rType , r , paramSetup , ops );
end
function de_cuda_eval{OT}(a::DeBinOp{OT},env,paramOut,paramIn,indexReg)
p1 = de_cuda_eval(a.p1,env,paramOut,quote ($paramIn).p1 end, indexReg)
p2 = de_cuda_eval(a.p2,env,paramOut,quote ($paramIn).p2 end, indexReg)
rType = de_cuda_eltype(a);
r = registerAlloc!(rType,env);
f1 = p1[3];
f2 = p2[3];
paramSetup = quote $(p1[3]); $(p2[3]); end
ops = [ p1[4], p2[4] ];
push!(ops, PtxOpBin{OT,rType}(r,p1[2],p2[2]))
return ( rType , r , paramSetup , ops );
end
const DeOpToCudaOp = {:+ => "add",:<< => "shl",:.* => "mul"}
const CudaMemSpaceToString = {msGlobal=>"global",msParam=>"param"}
function de_cuda_operand(op::PtxRegister)
return op.id;
end
function de_cuda_operand(op)
return "$op"
end
function de_cuda_op_to_string{OP,ST}(op::PtxOpBin{OP,ST})
if OP == :<<
opString = "$(DeOpToCudaOp[OP]).$(juliaTypeToCudaBType[ST])"
else
opString = "$(DeOpToCudaOp[OP]).$(juliaTypeToCudaType[ST])"
end
dst = op.dst.id
op0 = de_cuda_operand(op.op0)
op1 = de_cuda_operand(op.op1)
return "$opString $dst,$op0,$op1"
end
function de_cuda_op_to_string{MS,ST}(op::PtxOpLoad{MS,ST})
opString = "ld.$(CudaMemSpaceToString[MS]).$(juliaTypeToCudaType[ST])"
dst = op.dst.id
addr = de_cuda_operand(op.addr)
return "$opString $dst,[$addr]"
end
# assignement
function assign(lhs::DeVecCu,rhs::DeExpr)
buildTime = @elapsed begin
ltype = eltype(lhs);
env = DePtxEnv();
(lengthName,lengthIndex) = paramAlloc!(Uint32,env)
(dstPtrName,dstPtrIndex) = paramAlloc!(Ptr{ltype},env)
lengthReg = registerAlloc!(Uint32,env)
indexReg = registerAlloc!(Uint32,env)
dstPtrReg = registerAlloc!(Ptr{ltype},env)
@gensym paramOut paramIn
ret = de_cuda_eval(rhs,env,paramOut,paramIn,indexReg);
rtype = ret[1]
rreg = ret[2]
paramSetup = ret[3]
ops = ret[4]
# println("return Type: $rtype");
# println("return Reg: $rreg");
# println("env: $env")
# setup parameter list
paramString = ""
for i = 1:length(env.paramTypes)
pname = env.paramNames[i]
pt = env.paramTypes[i]
pts =""
et = de_eltype(pt)
pts = ".$(juliaTypeToCudaType[et])"
#TODO assume memory allocated by cuMalloc and is 16byte aligned
if pt <: Ptr
pts = "$pts.ptr.global.align 16 "
end
paramString = "$paramString .param $pts $pname"
if i < length(env.paramTypes)
paramString = "$paramString,\n"
end
end
# register allocation
regString = ""
regKeys = keys(env.registerCounter)
for ri = 1:length(regKeys)
rt = regKeys[ri]
rct = juliaTypeToCudaType[rt]
rc = env.registerCounter[rt]
rp = registerTypeToPrefix[rt]
regString = "$regString .reg .$rct $(rp)<$rc>;\n"
end
# setup computation
compString = ""
# println("ops:");
for i = 1:length(ops)
opstring = de_cuda_op_to_string(ops[i])
# println("$(ops[i]) --> $opstring")
compString = "$compString $opstring;\n"
end
# setup result storage
resultString = ""
stcache = ""
if false #sm >= sm_20
stcache = ".cs"
end
ccode =
".version 3.0
.target sm_11
.entry julia_func
(
$paramString
)
{
// Register Allocation
$regString
.reg .u32 %ii,%ix,%nx,%cix,%cnx; //registers for index determination
.reg .pred p;
ld.param.u32 $(lengthReg.id),[$(lengthName)]; //load length
ld.param.u32 $(dstPtrReg.id),[$(dstPtrName)]; //load destination ptr
// Index Setup
//TODO OVERKILL, but it should work for all cases
mov.u32 %ix,%tid.x;
mov.u32 %nx,%ntid.x;
mov.u32 %cix,%ctaid.x;
mov.u32 %cnx,%nctaid.x;
mad.lo.u32 $(indexReg.id),%cix,%nx,%ix;
setp.lt.u32 p,$(indexReg.id),$(lengthReg.id);
@!p bra END;
// Computation
$compString
// Store results
shl.b32 %ii,$(indexReg.id),2;
add.u32 $(dstPtrReg.id),$(dstPtrReg.id),%ii;
st.global$stcache.$(juliaTypeToCudaType[ltype]) [$(dstPtrReg.id)],$(rreg.id);
$resultString
// Jump destination for threads that are beyond data length
END:
}
"
rhsType = typeof(rhs);
infoLogSize = 4096;
infoLog = Array(Uint8,infoLogSize);
infoLog[1:end] = 0
errLogSize = 4096;
errLog = Array(Uint8,errLogSize);
errLog[1:end] = 0
retM = cuModuleLoadDataEx(
ccode,
CU_JIT_WALL_TIME,
CU_JIT_TARGET_FROM_CUCONTEXT,
CU_JIT_THREADS_PER_BLOCK,1024,
CU_JIT_INFO_LOG_BUFFER_SIZE,infoLogSize,
CU_JIT_INFO_LOG_BUFFER,infoLog,
CU_JIT_ERROR_LOG_BUFFER_SIZE,errLogSize,
CU_JIT_ERROR_LOG_BUFFER,errLog)
res = retM[1]
hmod = retM[2]
threadsPerBlock = 0
showInfoLog = false
showErrorLog = false
for i = 1:length(retM[3])
if CU_JIT_THREADS_PER_BLOCK == retM[3][i]
threadsPerBlock = retM[4][i]
elseif CU_JIT_WALL_TIME == retM[3][i]
println("CUDA Build Time: $(retM[4][i]/1000) Seconds")
elseif CU_JIT_INFO_LOG_BUFFER_SIZE == retM[3][i]
showInfoLog = retM[4][i] > 0
elseif CU_JIT_INFO_LOG_BUFFER == retM[3][i]
if showInfoLog
println("CUDA BUILD INFO LOG: $(retM[4][i])")
end
elseif CU_JIT_ERROR_LOG_BUFFER_SIZE == retM[3][i]
showErrorLog = retM[4][i] > 0
elseif CU_JIT_ERROR_LOG_BUFFER == retM[3][i]
if showErrorLog
println("CUDA BUILD ERROR LOG: $(retM[4][i])")
end
end
end
displayResults = false
if 0 != hmod
funcHnd = cuModuleGetFunction(hmod,"julia_func")
@eval function assign1(plhs::DeVecCu,($paramIn)::($rhsType))
rhsSz = de_check_dims($paramIn)
lhsSz = size(plhs)
if rhsSz != lhsSz
error("DeMat:CUDA: src & dst size does not match. NOT IMPLEMENTED FOR SCALARS FIX")
end
N = length(plhs)
$paramOut = Array(Any,$(length(env.paramTypes)))
($paramOut)[$lengthIndex+1] = [uint32(N)]
($paramOut)[$dstPtrIndex+1] = [plhs.buffer.ptr]
$paramSetup
params = $paramOut
paramPtrs = Array(Ptr{Void},$(length(env.paramTypes)))
for i = 1:$(length(env.paramTypes))
paramPtrs[i] = convert(Ptr{Void},params[i])
end
# println("params: $params");
# for i = 1:length(params)
# println("$i: $(typeof(params[i])) $(params[i])")
# end
nxBlock = uint32((N + $threadsPerBlock - 1) / $threadsPerBlock)
nxThread = uint32($threadsPerBlock)
# println("nxb: $nxBlock, nxt: $nxThread")
cuLaunchKernel(
$funcHnd,
nxBlock, uint32(1), uint32(1), # gridDim
nxThread, uint32(1), uint32(1), #blockDim
uint32(0), #sharedMemBytes::Uint32,
convert(Ptr{Void},0), #hStream::CUstream,
paramPtrs,
convert(Ptr{Void},0)) #extra
cuStreamSynchronize(convert(Ptr{Void},0)) # wait for stream to finish, normally you would not do this but for timing tests it is needed
plhs
end
global assign
@eval assign(lhs::DeVecCu,rhs::($rhsType)) = assign1(lhs,rhs)
else
displayResults = true;
end
if displayResults
println("PTX Code:")
println("----------------------------")
println(ccode)
println("----------------------------")
println("Output:")
println("----------------------------")
println("errno: $res")
println("hmod: $hmod")
for i = 1:length(retM[3])
println("$(retM[3][i]) : $(retM[4][i])")
end
println("----------------------------")
error("DeMat:CUDA: Cuda Build Error: $res")
end
end
println("DeMatJulia: Built New Assign (took $buildTime seconds) ... $rhsType");
return assign1(lhs,rhs);
end
assign(lhs::DeArrCuda,rhs::DeEle) = assign(lhs,de_promote(rhs)...)
assign(lhs::DeArrCuda,rhs::Number) = assign(lhs,de_promote(rhs)...)
#initialize cuda only once
if !isdefined(:deCudaCtx)
cuInit();
#list cuda devices, select device zero for now
deCudaDeviceCount = jlcuDeviceList();
if deCudaDeviceCount > 1
println("More than one CUDA device found, using the first one.");
end
deCudaCtx = cuCtxCreate(0,cuDeviceGet(0));
end