7
7
import warnings
8
8
import math
9
9
import numpy as np
10
- from .ndarray import ndarray_device_allocate_head , ndarray_populate_head
10
+ from .ndarray import ( ndarray_populate_head , ArrayHeaderManager )
11
11
from . import driver as _driver
12
12
from . import devices
13
13
from numba import dummyarray
@@ -54,15 +54,23 @@ class DeviceNDArrayBase(object):
54
54
def __init__ (self , shape , strides , dtype , stream = 0 , writeback = None ,
55
55
gpu_head = None , gpu_data = None ):
56
56
"""
57
- Arguments
58
-
59
- shape: array shape.
60
- strides: array strides.
61
- dtype: data type as numpy.dtype.
62
- stream: cuda stream.
63
- writeback: Deprecated.
64
- gpu_head: user provided device memory for the ndarray head structure
65
- gpu_data: user provided device memory for the ndarray data buffer
57
+ Args
58
+ ----
59
+
60
+ shape
61
+ array shape.
62
+ strides
63
+ array strides.
64
+ dtype
65
+ data type as numpy.dtype.
66
+ stream
67
+ cuda stream.
68
+ writeback
69
+ Deprecated.
70
+ gpu_head
71
+ user provided device memory for the ndarray head structure
72
+ gpu_data
73
+ user provided device memory for the ndarray data buffer
66
74
"""
67
75
if isinstance (shape , (int , long )):
68
76
shape = (shape ,)
@@ -86,27 +94,22 @@ def __init__(self, shape, strides, dtype, stream=0, writeback=None,
86
94
else :
87
95
self .alloc_size = _driver .device_memory_size (gpu_data )
88
96
97
+ self .gpu_mem = ArrayHeaderManager (devices .get_context ())
98
+
89
99
if gpu_head is None :
90
- gpu_head = ndarray_device_allocate_head (self .ndim )
100
+ gpu_head = self . gpu_mem . allocate (self .ndim )
91
101
ndarray_populate_head (gpu_head , gpu_data , self .shape ,
92
102
self .strides , stream = stream )
93
103
self .gpu_head = gpu_head
94
104
self .gpu_data = gpu_data
95
105
96
106
self .__writeback = writeback # should deprecate the use of this
97
107
98
- # define the array interface to work with numpy
99
- #
100
- # XXX: problem with data being accessed.
101
- # is NULL pointer alright?
102
- #
103
- # self.__array_interface__ = {
104
- # 'shape' : self.shape,
105
- # 'typestr' : self.dtype.str,
106
- # 'data' : (0, True),
107
- # 'version' : 3,
108
- # }
109
-
108
+ def __del__ (self ):
109
+ try :
110
+ self .gpu_mem .free (self .gpu_head )
111
+ except :
112
+ pass
110
113
111
114
@property
112
115
def device_ctypes_pointer (self ):
@@ -183,11 +186,11 @@ def split(self, section, stream=0):
183
186
end = min (begin + section , self .size )
184
187
shape = (end - begin ,)
185
188
gpu_data = self .gpu_data .view (begin * itemsize , end * itemsize )
186
- gpu_head = ndarray_device_allocate_head (1 )
187
- ndarray_populate_head (gpu_head , gpu_data , shape , strides ,
188
- stream = stream )
189
+ # gpu_head = _allocate_head (1)
190
+ # ndarray_populate_head(gpu_head, gpu_data, shape, strides,
191
+ # stream=stream)
189
192
yield DeviceNDArray (shape , strides , dtype = self .dtype , stream = stream ,
190
- gpu_head = gpu_head , gpu_data = gpu_data )
193
+ gpu_data = gpu_data )
191
194
192
195
def as_cuda_arg (self ):
193
196
"""Returns a device memory object that is used as the argument.
@@ -251,14 +254,21 @@ def __getitem__(self, item):
251
254
252
255
class MappedNDArray (DeviceNDArrayBase , np .ndarray ):
253
256
def device_setup (self , gpu_data , stream = 0 ):
254
- gpu_head = ndarray_device_allocate_head ( self . ndim )
257
+ self . gpu_mem = ArrayHeaderManager ( devices . get_context () )
255
258
259
+ gpu_head = self .gpu_mem .allocate (self .ndim )
256
260
ndarray_populate_head (gpu_head , gpu_data , self .shape ,
257
261
self .strides , stream = stream )
258
262
259
263
self .gpu_data = gpu_data
260
264
self .gpu_head = gpu_head
261
265
266
+ def __del__ (self ):
267
+ try :
268
+ self .gpu_mem .free (self .gpu_head )
269
+ except :
270
+ pass
271
+
262
272
263
273
def from_array_like (ary , stream = 0 , gpu_head = None , gpu_data = None ):
264
274
"Create a DeviceNDArray object that is like ary."
0 commit comments