/
memory.jl
725 lines (585 loc) · 22.5 KB
/
memory.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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# Raw memory management
export Mem, attribute, attribute!, memory_type, is_managed
module Mem
using ..CUDA
using ..CUDA: @enum_without_prefix, CUstream, CUdevice, CuDim3, CUarray, CUarray_format
using Base: @deprecate_binding
using Printf
#
# buffers
#
# a chunk of memory allocated using the CUDA APIs. this memory can reside on the host, on
# the gpu, or can represent specially-formatted memory (like texture arrays). depending on
# all that, the buffer may be `convert`ed to a Ptr, CuPtr, or CuArrayPtr.
abstract type AbstractBuffer end
Base.convert(T::Type{<:Union{Ptr,CuPtr,CuArrayPtr}}, buf::AbstractBuffer) =
throw(ArgumentError("Illegal conversion of a $(typeof(buf)) to a $T"))
# ccall integration
#
# taking the pointer of a buffer means returning the underlying pointer,
# and not the pointer of the buffer object itself.
Base.unsafe_convert(T::Type{<:Union{Ptr,CuPtr,CuArrayPtr}}, buf::AbstractBuffer) = convert(T, buf)
## device buffer
"""
Mem.DeviceBuffer
Mem.Device
A buffer of device memory residing on the GPU.
"""
struct DeviceBuffer <: AbstractBuffer
ptr::CuPtr{Cvoid}
bytesize::Int
end
Base.pointer(buf::DeviceBuffer) = buf.ptr
Base.sizeof(buf::DeviceBuffer) = buf.bytesize
Base.show(io::IO, buf::DeviceBuffer) =
@printf(io, "DeviceBuffer(%s at %p)", Base.format_bytes(sizeof(buf)), Int(pointer(buf)))
Base.convert(::Type{CuPtr{T}}, buf::DeviceBuffer) where {T} =
convert(CuPtr{T}, pointer(buf))
"""
Mem.alloc(DeviceBuffer, bytesize::Integer)
Allocate `bytesize` bytes of memory on the device. This memory is only accessible on the
GPU, and requires explicit calls to `unsafe_copyto!`, which wraps `cuMemcpy`,
for access on the CPU.
"""
function alloc(::Type{DeviceBuffer}, bytesize::Integer)
bytesize == 0 && return DeviceBuffer(CU_NULL, 0)
ptr_ref = Ref{CUDA.CUdeviceptr}()
CUDA.cuMemAlloc_v2(ptr_ref, bytesize)
return DeviceBuffer(reinterpret(CuPtr{Cvoid}, ptr_ref[]), bytesize)
end
function free(buf::DeviceBuffer)
if pointer(buf) != CU_NULL
CUDA.cuMemFree_v2(buf)
end
end
## host buffer
"""
Mem.HostBuffer
Mem.Host
A buffer of pinned memory on the CPU, possibly accessible on the GPU.
"""
struct HostBuffer <: AbstractBuffer
ptr::Ptr{Cvoid}
bytesize::Int
mapped::Bool
end
Base.pointer(buf::HostBuffer) = buf.ptr
Base.sizeof(buf::HostBuffer) = buf.bytesize
Base.show(io::IO, buf::HostBuffer) =
@printf(io, "HostBuffer(%s at %p)", Base.format_bytes(sizeof(buf)), Int(pointer(buf)))
Base.convert(::Type{Ptr{T}}, buf::HostBuffer) where {T} =
convert(Ptr{T}, pointer(buf))
function Base.convert(::Type{CuPtr{T}}, buf::HostBuffer) where {T}
if buf.mapped
pointer(buf) == C_NULL && return convert(CuPtr{T}, CU_NULL)
ptr_ref = Ref{CuPtr{Cvoid}}()
CUDA.cuMemHostGetDevicePointer_v2(ptr_ref, pointer(buf), #=flags=# 0)
convert(CuPtr{T}, ptr_ref[])
else
throw(ArgumentError("cannot take the GPU address of a pinned but not mapped CPU buffer"))
end
end
@deprecate_binding HOSTALLOC_DEFAULT 0 false
const HOSTALLOC_PORTABLE = CUDA.CU_MEMHOSTALLOC_PORTABLE
const HOSTALLOC_DEVICEMAP = CUDA.CU_MEMHOSTALLOC_DEVICEMAP
const HOSTALLOC_WRITECOMBINED = CUDA.CU_MEMHOSTALLOC_WRITECOMBINED
"""
Mem.alloc(HostBuffer, bytesize::Integer, [flags])
Allocate `bytesize` bytes of page-locked memory on the host. This memory is accessible from
the CPU, and makes it possible to perform faster memory copies to the GPU. Furthermore, if
`flags` is set to `HOSTALLOC_DEVICEMAP` the memory is also accessible from the GPU.
These accesses are direct, and go through the PCI bus.
If `flags` is set to `HOSTALLOC_PORTABLE`, the memory is considered mapped by all CUDA contexts,
not just the one that created the memory, which is useful if the memory needs to be accessed from
multiple devices. Multiple `flags` can be set at one time using a bytewise `OR`:
flags = HOSTALLOC_PORTABLE | HOSTALLOC_DEVICEMAP
"""
function alloc(::Type{HostBuffer}, bytesize::Integer, flags=0)
bytesize == 0 && return HostBuffer(C_NULL, 0, false)
ptr_ref = Ref{Ptr{Cvoid}}()
CUDA.cuMemHostAlloc(ptr_ref, bytesize, flags)
mapped = (flags & HOSTALLOC_DEVICEMAP) != 0
return HostBuffer(ptr_ref[], bytesize, mapped)
end
const HOSTREGISTER_PORTABLE = CUDA.CU_MEMHOSTREGISTER_PORTABLE
const HOSTREGISTER_DEVICEMAP = CUDA.CU_MEMHOSTREGISTER_DEVICEMAP
const HOSTREGISTER_IOMEMORY = CUDA.CU_MEMHOSTREGISTER_IOMEMORY
"""
Mem.register(HostBuffer, ptr::Ptr, bytesize::Integer, [flags])
Page-lock the host memory pointed to by `ptr`. Subsequent transfers to and from devices will
be faster, and can be executed asynchronously. If the `HOSTREGISTER_DEVICEMAP` flag is
specified, the buffer will also be accessible directly from the GPU.
These accesses are direct, and go through the PCI bus.
If the `HOSTREGISTER_PORTABLE` flag is specified, any CUDA context can access the memory.
"""
function register(::Type{HostBuffer}, ptr::Ptr, bytesize::Integer, flags=0)
bytesize == 0 && throw(ArgumentError("Cannot register an empty range of memory."))
CUDA.cuMemHostRegister_v2(ptr, bytesize, flags)
mapped = (flags & HOSTREGISTER_DEVICEMAP) != 0
return HostBuffer(ptr, bytesize, mapped)
end
"""
Mem.unregister(HostBuffer)
Unregisters a memory range that was registered with [`Mem.register`](@ref).
"""
function unregister(buf::HostBuffer)
CUDA.cuMemHostUnregister(buf)
end
function free(buf::HostBuffer)
if pointer(buf) != CU_NULL
CUDA.cuMemFreeHost(buf)
end
end
## unified buffer
"""
Mem.UnifiedBuffer
Mem.Unified
A managed buffer that is accessible on both the CPU and GPU.
"""
struct UnifiedBuffer <: AbstractBuffer
ptr::CuPtr{Cvoid}
bytesize::Int
end
Base.pointer(buf::UnifiedBuffer) = buf.ptr
Base.sizeof(buf::UnifiedBuffer) = buf.bytesize
Base.show(io::IO, buf::UnifiedBuffer) =
@printf(io, "UnifiedBuffer(%s at %p)", Base.format_bytes(sizeof(buf)), Int(pointer(buf)))
Base.convert(::Type{Ptr{T}}, buf::UnifiedBuffer) where {T} =
convert(Ptr{T}, reinterpret(Ptr{Cvoid}, pointer(buf)))
Base.convert(::Type{CuPtr{T}}, buf::UnifiedBuffer) where {T} =
convert(CuPtr{T}, pointer(buf))
@enum_without_prefix CUDA.CUmemAttach_flags CU_MEM_
"""
Mem.alloc(UnifiedBuffer, bytesize::Integer, [flags::CUmemAttach_flags])
Allocate `bytesize` bytes of unified memory. This memory is accessible from both the CPU and
GPU, with the CUDA driver automatically copying upon first access.
"""
function alloc(::Type{UnifiedBuffer}, bytesize::Integer,
flags::CUDA.CUmemAttach_flags=ATTACH_GLOBAL)
bytesize == 0 && return UnifiedBuffer(CU_NULL, 0)
ptr_ref = Ref{CuPtr{Cvoid}}()
CUDA.cuMemAllocManaged(ptr_ref, bytesize, flags)
return UnifiedBuffer(ptr_ref[], bytesize)
end
function free(buf::UnifiedBuffer)
if pointer(buf) != CU_NULL
CUDA.cuMemFree_v2(buf)
end
end
"""
prefetch(::UnifiedBuffer, [bytes::Integer]; [device::CuDevice], [stream::CuStream])
Prefetches memory to the specified destination device.
"""
function prefetch(buf::UnifiedBuffer, bytes::Integer=sizeof(buf);
device::CuDevice=CuCurrentDevice(), stream::CuStream=CuDefaultStream())
bytes > sizeof(buf) && throw(BoundsError(buf, bytes))
CUDA.cuMemPrefetchAsync(buf, bytes, device, stream)
end
@enum_without_prefix CUDA.CUmem_advise CU_MEM_
"""
advise(::UnifiedBuffer, advice::CUDA.CUmem_advise, [bytes::Integer]; [device::CuDevice])
Advise about the usage of a given memory range.
"""
function advise(buf::UnifiedBuffer, advice::CUDA.CUmem_advise, bytes::Integer=sizeof(buf);
device::CuDevice=CuCurrentDevice())
bytes > sizeof(buf) && throw(BoundsError(buf, bytes))
CUDA.cuMemAdvise(buf, bytes, advice, device)
end
## array buffer
mutable struct ArrayBuffer{T,N} <: AbstractBuffer
ptr::CuArrayPtr{T}
dims::Dims{N}
end
Base.pointer(buf::ArrayBuffer) = buf.ptr
Base.sizeof(buf::ArrayBuffer) = error("Opaque array buffers do not have a definite size")
Base.size(buf::ArrayBuffer) = buf.dims
Base.length(buf::ArrayBuffer) = prod(buf.dims)
Base.ndims(buf::ArrayBuffer{<:Any,N}) where {N} = N
Base.show(io::IO, buf::ArrayBuffer{T,1}) where {T} =
@printf(io, "%g-element ArrayBuffer{%s,%g}(%p)", length(buf), string(T), 1, Int(pointer(buf)))
Base.show(io::IO, buf::ArrayBuffer{T}) where {T} =
@printf(io, "%s ArrayBuffer{%s,%g}(%p)", Base.inds2string(size(buf)), string(T), ndims(buf), Int(pointer(buf)))
# array buffers are typed, so refuse arbitrary conversions
Base.convert(::Type{CuArrayPtr{T}}, buf::ArrayBuffer{T}) where {T} =
convert(CuArrayPtr{T}, pointer(buf))
# ... except for CuArrayPtr{Nothing}, which is used to call untyped API functions
Base.convert(::Type{CuArrayPtr{Nothing}}, buf::ArrayBuffer) =
convert(CuArrayPtr{Nothing}, pointer(buf))
function alloc(::Type{<:ArrayBuffer{T}}, dims::Dims{N}) where {T,N}
format = convert(CUarray_format, eltype(T))
if N == 2
width, height = dims
depth = 0
@assert 1 <= width "CUDA 2D array (texture) width must be >= 1"
# @assert witdh <= CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_WIDTH
@assert 1 <= height "CUDA 2D array (texture) height must be >= 1"
# @assert height <= CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_HEIGHT
elseif N == 3
width, height, depth = dims
@assert 1 <= width "CUDA 3D array (texture) width must be >= 1"
# @assert witdh <= CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_WIDTH
@assert 1 <= height "CUDA 3D array (texture) height must be >= 1"
# @assert height <= CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_HEIGHT
@assert 1 <= depth "CUDA 3D array (texture) depth must be >= 1"
# @assert depth <= CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_DEPTH
elseif N == 1
width = dims[1]
height = depth = 0
@assert 1 <= width "CUDA 1D array (texture) width must be >= 1"
# @assert witdh <= CU_DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_WIDTH
else
"CUDA arrays (texture memory) can only have 1, 2 or 3 dimensions"
end
allocateArray_ref = Ref(CUDA.CUDA_ARRAY3D_DESCRIPTOR(
width, # Width::Csize_t
height, # Height::Csize_t
depth, # Depth::Csize_t
format, # Format::CUarray_format
UInt32(CUDA.nchans(T)), # NumChannels::UInt32
0))
handle_ref = Ref{CUarray}()
CUDA.cuArray3DCreate_v2(handle_ref, allocateArray_ref)
ptr = reinterpret(CuArrayPtr{T}, handle_ref[])
return ArrayBuffer{T,N}(ptr, dims)
end
function free(buf::ArrayBuffer)
CUDA.cuArrayDestroy(buf)
end
## convenience aliases
const Device = DeviceBuffer
const Host = HostBuffer
const Unified = UnifiedBuffer
const Array = ArrayBuffer
#
# pointers
#
## initialization
"""
Mem.set!(buf::CuPtr, value::Union{UInt8,UInt16,UInt32}, len::Integer;
async::Bool=false, stream::CuStream)
Initialize device memory by copying `val` for `len` times. Executed asynchronously if
`async` is true, in which case a valid `stream` is required.
"""
set!
for T in [UInt8, UInt16, UInt32]
bits = 8*sizeof(T)
fn_sync = Symbol("cuMemsetD$(bits)_v2")
fn_async = Symbol("cuMemsetD$(bits)Async")
@eval function set!(ptr::CuPtr{$T}, value::$T, len::Integer;
async::Bool=false, stream::Union{Nothing,CuStream}=nothing)
if async
stream===nothing &&
throw(ArgumentError("Asynchronous memory operations require a stream."))
$(getproperty(CUDA, fn_async))(ptr, value, len, stream)
else
stream===nothing ||
throw(ArgumentError("Synchronous memory operations cannot be issues on a stream."))
$(getproperty(CUDA, fn_sync))(ptr, value, len)
end
end
end
## copy operations
for (f, fa, srcPtrTy, dstPtrTy) in (("cuMemcpyDtoH_v2", "cuMemcpyDtoHAsync_v2", CuPtr, Ptr),
("cuMemcpyHtoD_v2", "cuMemcpyHtoDAsync_v2", Ptr, CuPtr),
("cuMemcpyDtoD_v2", "cuMemcpyDtoDAsync_v2", CuPtr, CuPtr),
)
@eval function Base.unsafe_copyto!(dst::$dstPtrTy{T}, src::$srcPtrTy{T}, N::Integer;
stream::Union{Nothing,CuStream}=nothing,
async::Bool=false) where T
if async
stream===nothing &&
throw(ArgumentError("Asynchronous memory operations require a stream."))
$(getproperty(CUDA, Symbol(fa)))(dst, src, N*sizeof(T), stream)
else
stream===nothing ||
throw(ArgumentError("Synchronous memory operations cannot be issued on a stream."))
$(getproperty(CUDA, Symbol(f)))(dst, src, N*sizeof(T))
end
return dst
end
end
function Base.unsafe_copyto!(dst::CuArrayPtr{T}, doffs::Integer, src::Ptr{T}, N::Integer;
stream::Union{Nothing,CuStream}=nothing,
async::Bool=false) where T
if async
stream===nothing &&
throw(ArgumentError("Asynchronous memory operations require a stream."))
CUDA.cuMemcpyHtoAAsync_v2(dst, doffs, src, N*sizeof(T), stream)
else
stream===nothing ||
throw(ArgumentError("Synchronous memory operations cannot be issued on a stream."))
CUDA.cuMemcpyHtoA_v2(dst, doffs, src, N*sizeof(T))
end
end
function Base.unsafe_copyto!(dst::Ptr{T}, src::CuArrayPtr{T}, soffs::Integer, N::Integer;
stream::Union{Nothing,CuStream}=nothing,
async::Bool=false) where T
if async
stream===nothing &&
throw(ArgumentError("Asynchronous memory operations require a stream."))
CUDA.cuMemcpyAtoHAsync_v2(dst, src, soffs, N*sizeof(T), stream)
else
stream===nothing ||
throw(ArgumentError("Synchronous memory operations cannot be issued on a stream."))
CUDA.cuMemcpyAtoH_v2(dst, src, soffs, N*sizeof(T))
end
end
Base.unsafe_copyto!(dst::CuArrayPtr{T}, doffs::Integer, src::CuPtr{T}, N::Integer) where {T} =
CUDA.cuMemcpyDtoA_v2(dst, doffs, src, N*sizeof(T))
Base.unsafe_copyto!(dst::CuPtr{T}, src::CuArrayPtr{T}, soffs::Integer, N::Integer) where {T} =
CUDA.cuMemcpyAtoD_v2(dst, src, soffs, N*sizeof(T))
Base.unsafe_copyto!(dst::CuArrayPtr, src, N::Integer; kwargs...) =
Base.unsafe_copyto!(dst, 0, src, N; kwargs...)
Base.unsafe_copyto!(dst, src::CuArrayPtr, N::Integer; kwargs...) =
Base.unsafe_copyto!(dst, src, 0, N; kwargs...)
function unsafe_copy2d!(dst::Union{Ptr{T},CuPtr{T},CuArrayPtr{T}}, dstTyp::Type{<:AbstractBuffer},
src::Union{Ptr{T},CuPtr{T},CuArrayPtr{T}}, srcTyp::Type{<:AbstractBuffer},
width::Integer, height::Integer=1;
dstPos::CuDim=(1,1), srcPos::CuDim=(1,1),
dstPitch::Integer=0, srcPitch::Integer=0,
async::Bool=false, stream::Union{Nothing,CuStream}=nothing) where T
srcPos = CUDA.CuDim3(srcPos)
@assert srcPos.z == 1
dstPos = CUDA.CuDim3(dstPos)
@assert dstPos.z == 1
srcMemoryType, srcHost, srcDevice, srcArray = if srcTyp == Host
CUDA.CU_MEMORYTYPE_HOST,
src::Ptr,
0,
0
elseif srcTyp == Mem.Device
CUDA.CU_MEMORYTYPE_DEVICE,
0,
src::CuPtr,
0
elseif srcTyp == Mem.Unified
CUDA.CU_MEMORYTYPE_UNIFIED,
0,
reinterpret(CuPtr{Cvoid}, src),
0
elseif srcTyp == Mem.Array
CUDA.CU_MEMORYTYPE_ARRAY,
0,
0,
src::CuArrayPtr
end
dstMemoryType, dstHost, dstDevice, dstArray = if dstTyp == Host
CUDA.CU_MEMORYTYPE_HOST,
dst::Ptr,
0,
0
elseif dstTyp == Mem.Device
CUDA.CU_MEMORYTYPE_DEVICE,
0,
dst::CuPtr,
0
elseif dstTyp == Mem.Unified
CUDA.CU_MEMORYTYPE_UNIFIED,
0,
reinterpret(CuPtr{Cvoid}, dst),
0
elseif dstTyp == Mem.Array
CUDA.CU_MEMORYTYPE_ARRAY,
0,
0,
dst::CuArrayPtr
end
params_ref = Ref(CUDA.CUDA_MEMCPY2D(
# source
(srcPos.x-1)*sizeof(T), srcPos.y-1,
srcMemoryType, srcHost, srcDevice, srcArray,
srcPitch,
# destination
(dstPos.x-1)*sizeof(T), dstPos.y-1,
dstMemoryType, dstHost, dstDevice, dstArray,
dstPitch,
# extent
width*sizeof(T), height
))
if async
stream===nothing &&
throw(ArgumentError("Asynchronous memory operations require a stream."))
CUDA.cuMemcpy2DAsync_v2(params_ref, stream)
else
stream===nothing ||
throw(ArgumentError("Synchronous memory operations cannot be issued on a stream."))
CUDA.cuMemcpy2D_v2(params_ref)
end
end
"""
unsafe_copy3d!(dst, dstTyp, src, srcTyp, width, height=1, depth=1;
dstPos=(1,1,1), dstPitch=0, dstHeight=0,
srcPos=(1,1,1), srcPitch=0, srcHeight=0,
async=false, stream=nothing)
Perform a 3D memory copy between pointers `src` and `dst`, at respectively position `srcPos`
and `dstPos` (1-indexed). Both pitch and destination can be specified for both the source
and destination; consult the CUDA documentation for more details. This call is executed
asynchronously if `async` is set, in which case `stream` needs to be a valid CuStream.
"""
function unsafe_copy3d!(dst::Union{Ptr{T},CuPtr{T},CuArrayPtr{T}}, dstTyp::Type{<:AbstractBuffer},
src::Union{Ptr{T},CuPtr{T},CuArrayPtr{T}}, srcTyp::Type{<:AbstractBuffer},
width::Integer, height::Integer=1, depth::Integer=1;
dstPos::CuDim=(1,1,1), srcPos::CuDim=(1,1,1),
dstPitch::Integer=0, dstHeight::Integer=0,
srcPitch::Integer=0, srcHeight::Integer=0,
async::Bool=false, stream::Union{Nothing,CuStream}=nothing) where T
srcPos = CUDA.CuDim3(srcPos)
dstPos = CUDA.CuDim3(dstPos)
srcMemoryType, srcHost, srcDevice, srcArray = if srcTyp == Host
CUDA.CU_MEMORYTYPE_HOST,
src::Ptr,
0,
0
elseif srcTyp == Mem.Device
CUDA.CU_MEMORYTYPE_DEVICE,
0,
src::CuPtr,
0
elseif srcTyp == Mem.Unified
CUDA.CU_MEMORYTYPE_UNIFIED,
0,
reinterpret(CuPtr{Cvoid}, src),
0
elseif srcTyp == Mem.Array
CUDA.CU_MEMORYTYPE_ARRAY,
0,
0,
src::CuArrayPtr
end
dstMemoryType, dstHost, dstDevice, dstArray = if dstTyp == Host
CUDA.CU_MEMORYTYPE_HOST,
dst::Ptr,
0,
0
elseif dstTyp == Mem.Device
CUDA.CU_MEMORYTYPE_DEVICE,
0,
dst::CuPtr,
0
elseif dstTyp == Mem.Unified
CUDA.CU_MEMORYTYPE_UNIFIED,
0,
reinterpret(CuPtr{Cvoid}, dst),
0
elseif dstTyp == Mem.Array
CUDA.CU_MEMORYTYPE_ARRAY,
0,
0,
dst::CuArrayPtr
end
params_ref = Ref(CUDA.CUDA_MEMCPY3D(
# source
(srcPos.x-1)*sizeof(T), srcPos.y-1, srcPos.z-1,
0, # LOD
srcMemoryType, srcHost, srcDevice, srcArray,
C_NULL, # reserved
srcPitch, srcHeight,
# destination
(dstPos.x-1)*sizeof(T), dstPos.y-1, dstPos.z-1,
0, # LOD
dstMemoryType, dstHost, dstDevice, dstArray,
C_NULL, # reserved
dstPitch, dstHeight,
# extent
width*sizeof(T), height, depth
))
if async
stream===nothing &&
throw(ArgumentError("Asynchronous memory operations require a stream."))
CUDA.cuMemcpy3DAsync_v2(params_ref, stream)
else
stream===nothing ||
throw(ArgumentError("Synchronous memory operations cannot be issued on a stream."))
CUDA.cuMemcpy3D_v2(params_ref)
end
end
#
# auxiliary functionality
#
## memory pinning
# TODO: PerDevice
const __pinned_memory = Dict{Tuple{CuContext,Ptr{Cvoid}}, WeakRef}()
function pin(a::Base.Array, flags=0)
ctx = context()
ptr = convert(Ptr{Cvoid}, pointer(a))
if haskey(__pinned_memory, (ctx,ptr)) && __pinned_memory[(ctx,ptr)].value !== nothing
return
end
buf = Mem.register(Mem.Host, pointer(a), sizeof(a), flags)
finalizer(a) do _
CUDA.isvalid(ctx) || return
context!(ctx) do
Mem.unregister(buf)
end
end
__pinned_memory[(ctx,ptr)] = WeakRef(a)
return
end
## memory info
function info()
free_ref = Ref{Csize_t}()
total_ref = Ref{Csize_t}()
CUDA.cuMemGetInfo_v2(free_ref, total_ref)
return convert(Int, free_ref[]), convert(Int, total_ref[])
end
end # module Mem
"""
available_memory()
Returns the available_memory amount of memory (in bytes), available for allocation by the CUDA context.
"""
available_memory() = Mem.info()[1]
"""
total_memory()
Returns the total amount of memory (in bytes), available for allocation by the CUDA context.
"""
total_memory() = Mem.info()[2]
## pointer attributes
"""
attribute(X, ptr::Union{Ptr,CuPtr}, attr)
Returns attribute `attr` about pointer `ptr`. The type of the returned value depends on the
attribute, and as such must be passed as the `X` parameter.
"""
function attribute(X::Type, ptr::Union{Ptr{T},CuPtr{T}}, attr::CUpointer_attribute) where {T}
ptr = reinterpret(CuPtr{T}, ptr)
data_ref = Ref{X}()
cuPointerGetAttribute(data_ref, attr, ptr)
return data_ref[]
end
"""
attribute!(ptr::Union{Ptr,CuPtr}, attr, val)
Sets attribute` attr` on a pointer `ptr` to `val`.
"""
function attribute!(ptr::Union{Ptr{T},CuPtr{T}}, attr::CUpointer_attribute, val) where {T}
ptr = reinterpret(CuPtr{T}, ptr)
cuPointerSetAttribute(Ref(val), attr, ptr)
return
end
@enum_without_prefix CUpointer_attribute CU_
# some common attributes
@enum_without_prefix CUmemorytype CU_
memory_type(x) = CUmemorytype(attribute(Cuint, x, POINTER_ATTRIBUTE_MEMORY_TYPE))
is_managed(x) = convert(Bool, attribute(Cuint, x, POINTER_ATTRIBUTE_IS_MANAGED))
## shared texture/array stuff
function Base.convert(::Type{CUarray_format}, T::Type)
if T === UInt8
return CU_AD_FORMAT_UNSIGNED_INT8
elseif T === UInt16
return CU_AD_FORMAT_UNSIGNED_INT16
elseif T === UInt32
return CU_AD_FORMAT_UNSIGNED_INT32
elseif T === Int8
return CU_AD_FORMAT_SIGNED_INT8
elseif T === Int16
return CU_AD_FORMAT_SIGNED_INT16
elseif T === Int32
return CU_AD_FORMAT_SIGNED_INT32
elseif T === Float16
return CU_AD_FORMAT_HALF
elseif T === Float32
return CU_AD_FORMAT_FLOAT
else
throw(ArgumentError("CUDA does not support texture arrays for element type $T."))
end
end
nchans(::Type{<:NTuple{C}}) where {C} = C
nchans(::Type) = 1