|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +# cython: profile=False |
| 19 | +# distutils: language = c++ |
| 20 | +# cython: embedsignature = True |
| 21 | + |
| 22 | + |
| 23 | +cpdef enum DeviceAllocationType: |
| 24 | + CPU = <char> CDeviceAllocationType_kCPU |
| 25 | + CUDA = <char> CDeviceAllocationType_kCUDA |
| 26 | + CUDA_HOST = <char> CDeviceAllocationType_kCUDA_HOST |
| 27 | + OPENCL = <char> CDeviceAllocationType_kOPENCL |
| 28 | + VULKAN = <char> CDeviceAllocationType_kVULKAN |
| 29 | + METAL = <char> CDeviceAllocationType_kMETAL |
| 30 | + VPI = <char> CDeviceAllocationType_kVPI |
| 31 | + ROCM = <char> CDeviceAllocationType_kROCM |
| 32 | + ROCM_HOST = <char> CDeviceAllocationType_kROCM_HOST |
| 33 | + EXT_DEV = <char> CDeviceAllocationType_kEXT_DEV |
| 34 | + CUDA_MANAGED = <char> CDeviceAllocationType_kCUDA_MANAGED |
| 35 | + ONEAPI = <char> CDeviceAllocationType_kONEAPI |
| 36 | + WEBGPU = <char> CDeviceAllocationType_kWEBGPU |
| 37 | + HEXAGON = <char> CDeviceAllocationType_kHEXAGON |
| 38 | + |
| 39 | + |
| 40 | +cdef object _wrap_device_allocation_type(CDeviceAllocationType device_type): |
| 41 | + return DeviceAllocationType(<char> device_type) |
| 42 | + |
| 43 | + |
| 44 | +cdef class Device(_Weakrefable): |
| 45 | + """ |
| 46 | + Abstract interface for hardware devices |
| 47 | +
|
| 48 | + This object represents a device with access to some memory spaces. |
| 49 | + When handling a Buffer or raw memory address, it allows deciding in which |
| 50 | + context the raw memory address should be interpreted |
| 51 | + (e.g. CPU-accessible memory, or embedded memory on some particular GPU). |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__(self): |
| 55 | + raise TypeError("Do not call Device's constructor directly, " |
| 56 | + "use the device attribute of the MemoryManager instead.") |
| 57 | + |
| 58 | + cdef void init(self, const shared_ptr[CDevice]& device): |
| 59 | + self.device = device |
| 60 | + |
| 61 | + @staticmethod |
| 62 | + cdef wrap(const shared_ptr[CDevice]& device): |
| 63 | + cdef Device self = Device.__new__(Device) |
| 64 | + self.init(device) |
| 65 | + return self |
| 66 | + |
| 67 | + def __eq__(self, other): |
| 68 | + if not isinstance(other, Device): |
| 69 | + return False |
| 70 | + return self.device.get().Equals(deref((<Device>other).device.get())) |
| 71 | + |
| 72 | + def __repr__(self): |
| 73 | + return "<pyarrow.Device: {}>".format(frombytes(self.device.get().ToString())) |
| 74 | + |
| 75 | + @property |
| 76 | + def type_name(self): |
| 77 | + """ |
| 78 | + A shorthand for this device's type. |
| 79 | + """ |
| 80 | + return frombytes(self.device.get().type_name()) |
| 81 | + |
| 82 | + @property |
| 83 | + def device_id(self): |
| 84 | + """ |
| 85 | + A device ID to identify this device if there are multiple of this type. |
| 86 | +
|
| 87 | + If there is no "device_id" equivalent (such as for the main CPU device on |
| 88 | + non-numa systems) returns -1. |
| 89 | + """ |
| 90 | + return self.device.get().device_id() |
| 91 | + |
| 92 | + @property |
| 93 | + def is_cpu(self): |
| 94 | + """ |
| 95 | + Whether this device is the main CPU device. |
| 96 | +
|
| 97 | + This shorthand method is very useful when deciding whether a memory address |
| 98 | + is CPU-accessible. |
| 99 | + """ |
| 100 | + return self.device.get().is_cpu() |
| 101 | + |
| 102 | + @property |
| 103 | + def device_type(self): |
| 104 | + """ |
| 105 | + Return the DeviceAllocationType of this device. |
| 106 | + """ |
| 107 | + return _wrap_device_allocation_type(self.device.get().device_type()) |
| 108 | + |
| 109 | + |
| 110 | +cdef class MemoryManager(_Weakrefable): |
| 111 | + """ |
| 112 | + An object that provides memory management primitives. |
| 113 | +
|
| 114 | + A MemoryManager is always tied to a particular Device instance. |
| 115 | + It can also have additional parameters (such as a MemoryPool to |
| 116 | + allocate CPU memory). |
| 117 | +
|
| 118 | + """ |
| 119 | + |
| 120 | + def __init__(self): |
| 121 | + raise TypeError("Do not call MemoryManager's constructor directly, " |
| 122 | + "use pyarrow.default_cpu_memory_manager() instead.") |
| 123 | + |
| 124 | + cdef void init(self, const shared_ptr[CMemoryManager]& mm): |
| 125 | + self.memory_manager = mm |
| 126 | + |
| 127 | + @staticmethod |
| 128 | + cdef wrap(const shared_ptr[CMemoryManager]& mm): |
| 129 | + cdef MemoryManager self = MemoryManager.__new__(MemoryManager) |
| 130 | + self.init(mm) |
| 131 | + return self |
| 132 | + |
| 133 | + def __repr__(self): |
| 134 | + return "<pyarrow.MemoryManager device: {}>".format( |
| 135 | + frombytes(self.memory_manager.get().device().get().ToString()) |
| 136 | + ) |
| 137 | + |
| 138 | + @property |
| 139 | + def device(self): |
| 140 | + """ |
| 141 | + The device this MemoryManager is tied to. |
| 142 | + """ |
| 143 | + return Device.wrap(self.memory_manager.get().device()) |
| 144 | + |
| 145 | + @property |
| 146 | + def is_cpu(self): |
| 147 | + """ |
| 148 | + Whether this MemoryManager is tied to the main CPU device. |
| 149 | +
|
| 150 | + This shorthand method is very useful when deciding whether a memory |
| 151 | + address is CPU-accessible. |
| 152 | + """ |
| 153 | + return self.memory_manager.get().is_cpu() |
| 154 | + |
| 155 | + |
| 156 | +def default_cpu_memory_manager(): |
| 157 | + """ |
| 158 | + Return the default CPU MemoryManager instance. |
| 159 | +
|
| 160 | + The returned singleton instance uses the default MemoryPool. |
| 161 | + """ |
| 162 | + return MemoryManager.wrap(c_default_cpu_memory_manager()) |
0 commit comments