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
|
Built-in Utilities
==================
Automatic Initialization
------------------------
.. module:: pycuda.autoinit
The module :mod:`pycuda.autoinit`, when imported, automatically performs
all the steps necessary to get CUDA ready for submission of compute kernels.
It uses :func:`pycuda.tools.make_default_context` to create a compute context.
.. data:: device
An instance of :class:`pycuda.driver.Device` that was used for automatic
initialization.
.. data:: context
A default-constructed instance of :class:`pycuda.driver.Context`
on :data:`device`. This context is created by calling
:func:`pycuda.tools.make_default_context`.
Choice of Device
----------------
.. module:: pycuda.tools
.. function:: make_default_context()
Return a :class:`pycuda.driver.Context` instance chosen according to the
following rules:
* If the environment variable :envvar:`CUDA_DEVICE` is set, its integer
value is used as the device number.
* If the file :file:`.cuda-device` is present in the user's home directory,
the integer value of its contents is used as the device number.
* Otherwise, all available CUDA devices are tried in a round-robin fashion.
An error is raised if this does not lead to a usable context.
.. versionadded: 0.94
.. function:: get_default_device(default=0)
Deprecated. Use :func:`make_default_context`.
Return a :class:`pycuda.driver.Device` instance chosen according to the
following rules:
* If the environment variable :envvar:`CUDA_DEVICE` is set, its integer
value is used as the device number.
* If the file :file:`.cuda-device` is present in the user's home directory,
the integer value of its contents is used as the device number.
* Otherwise, `default` is used as the device number.
.. versionchanged: 0.94
Deprecated.
Kernel Caching
--------------
.. function:: context_dependent_memoize(func)
This decorator caches the result of the decorated function, *if* a
subsequent occurs in the same :class:`pycuda.driver.Context`.
This is useful for caching of kernels.
.. function:: clear_context_caches()
Empties all context-dependent memoization caches. Also releases
all held reference contexts. If it is important to you that the
program detaches from its context, you might need to call this
function to free all remaining references to your context.
Testing
-------
.. function:: mark_cuda_test(func)
This function, meant for use with :mod:`py.test`, will mark *func* with a
"cuda" tag and make sure it has a CUDA context available when invoked.
Device Metadata and Occupancy
-----------------------------
.. class:: DeviceData(dev=None)
Gives access to more information on a device than is available through
:meth:`pycuda.driver.Device.get_attribute`. If `dev` is `None`, it defaults
to the device returned by :meth:`pycuda.driver.Context.get_device`.
.. attribute:: max_threads
.. attribute:: warp_size
.. attribute:: warps_per_mp
.. attribute:: thread_blocks_per_mp
.. attribute:: registers
.. attribute:: shared_memory
.. attribute:: smem_granularity
The number of threads that participate in banked, simultaneous access
to shared memory.
.. attribute:: smem_alloc_granularity
The size of the smallest possible (non-empty) shared memory allocation.
.. method:: align_bytes(word_size=4)
The distance between global memory base addresses that
allow accesses of word-size `word_size` bytes to get coalesced.
.. method:: align(bytes, word_size=4)
Round up `bytes` to the next alignment boundary as given by :meth:`align_bytes`.
.. method:: align_words(word_size)
Return `self.align_bytes(word_size)/word_size`, while checking that the division
did not yield a remainder.
.. method:: align_dtype(elements, dtype_size)
Round up `elements` to the next alignment boundary
as given by :meth:`align_bytes`, where each element is assumed to be
`dtype_size` bytes large.
.. UNDOC coalesce
.. staticmethod:: make_valid_tex_channel_count(size)
Round up `size` to a valid texture channel count.
.. class:: OccupancyRecord(devdata, threads, shared_mem=0, registers=0)
Calculate occupancy for a given kernel workload characterized by
* thread count of `threads`
* shared memory use of `shared_mem` bytes
* register use of `registers` 32-bit registers
.. attribute:: tb_per_mp
How many thread blocks execute on each multiprocessor.
.. attribute:: limited_by
What :attr:`tb_per_mp` is limited by. One of `"device"`, `"warps"`,
`"regs"`, `"smem"`.
.. attribute:: warps_per_mp
How many warps execute on each multiprocessor.
.. attribute:: occupancy
A `float` value between 0 and 1 indicating how much of each multiprocessor's
scheduling capability is occupied by the kernel.
.. _mempool:
Memory Pools
------------
The functions :func:`pycuda.driver.mem_alloc` and
:func:`pycuda.driver.pagelocked_empty` can consume a fairly large amount of
processing time if they are invoked very frequently. For example, code based on
:class:`pycuda.gpuarray.GPUArray` can easily run into this issue because a
fresh memory area is allocated for each intermediate result. Memory pools are a
remedy for this problem based on the observation that often many of the block
allocations are of the same sizes as previously used ones.
Then, instead of fully returning the memory to the system and incurring the
associated reallocation overhead, the pool holds on to the memory and uses it
to satisfy future allocations of similarly-sized blocks. The pool reacts
appropriately to out-of-memory conditions as long as all memory allocations
are made through it. Allocations performed from outside of the pool may run
into spurious out-of-memory conditions due to the pool owning much or all of
the available memory.
Device-based Memory Pool
^^^^^^^^^^^^^^^^^^^^^^^^
.. class:: PooledDeviceAllocation
An object representing a :class:`DeviceMemoryPool`-based allocation of
linear device memory. Once this object is deleted, its associated device
memory is freed.
:class:`PooledDeviceAllocation` instances can be cast to :class:`int`
(and :class:`long`), yielding the starting address of the device memory
allocated.
.. method:: free
Explicitly return the memory held by *self* to the associated memory pool.
.. method:: __len__
Return the size of the allocated memory in bytes.
.. class:: DeviceMemoryPool
A memory pool for linear device memory as allocated using
:func:`pycuda.driver.mem_alloc`. (see :ref:`mempool`)
.. attribute:: held_blocks
The number of unused blocks being held by this pool.
.. attribute:: active_blocks
The number of blocks in active use that have been allocated
through this pool.
.. method:: allocate(size)
Return a :class:`PooledDeviceAllocation` of *size* bytes.
.. method:: free_held
Free all unused memory that the pool is currently holding.
.. method:: stop_holding
Instruct the memory to start immediately freeing memory returned
to it, instead of holding it for future allocations.
Implicitly calls :meth:`free_held`.
This is useful as a cleanup action when a memory pool falls out
of use.
Memory Pool for pagelocked memory
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. class:: PooledHostAllocation
An object representing a :class:`PageLockedMemoryPool`-based allocation of
linear device memory. Once this object is deleted, its associated device
memory is freed.
.. method:: free
Explicitly return the memory held by *self* to the associated memory pool.
.. method:: __len__
Return the size of the allocated memory in bytes.
.. class:: PageLockedAllocator(flags=0)
Specifies the set of :class:`pycuda.driver.host_alloc_flags` used in its
associated :class:`PageLockedMemoryPool`.
.. class:: PageLockedMemoryPool(allocator=PageLockedAllocator())
A memory pool for pagelocked host memory as allocated using
:func:`pycuda.driver.pagelocked_empty`. (see :ref:`mempool`)
.. attribute:: held_blocks
The number of unused blocks being held by this pool.
.. attribute:: active_blocks
The number of blocks in active use that have been allocated
through this pool.
.. method:: allocate(shape, dtype, order="C")
Return an uninitialized ("empty") :class:`numpy.ndarray` with the given
*shape*, *dtype*, and *order*. This array will be backed by a
:class:`PooledHostAllocation`, which can be found as the ``.base``
attribute of the array.
.. method:: free_held
Free all unused memory that the pool is currently holding.
.. method:: stop_holding
Instruct the memory to start immediately freeing memory returned
to it, instead of holding it for future allocations.
Implicitly calls :meth:`free_held`.
This is useful as a cleanup action when a memory pool falls out
of use.
|