MCPcopy
hub / github.com/tinygrad/tinygrad / alloc_sysmem

Method alloc_sysmem

tinygrad/runtime/support/system.py:189–194  ·  view source on GitHub ↗
(self, size:int, vaddr:int=0, contiguous:bool=False)

Source from the content-addressed store, hash-verified

187 self.cfg_fd = FileIOInterface(f"/sys/bus/pci/devices/{self.pcibus}/config", os.O_RDWR | os.O_SYNC | os.O_CLOEXEC)
188
189 def alloc_sysmem(self, size:int, vaddr:int=0, contiguous:bool=False) -> tuple[MMIOInterface, list[int]]:
190 assert not contiguous or size <= (2 << 20), "Contiguous allocation is only supported for sizes up to 2MB"
191 flags = (libc.MAP_HUGETLB if contiguous and (size:=round_up(size, mmap.PAGESIZE)) > mmap.PAGESIZE else 0) | (MAP_FIXED if vaddr else 0)
192 va = FileIOInterface.anon_mmap(vaddr, size, mmap.PROT_READ|mmap.PROT_WRITE, mmap.MAP_SHARED|mmap.MAP_ANONYMOUS|MAP_POPULATE|MAP_LOCKED|flags, 0)
193 sysmem_view, paddrs = MMIOInterface(va, size), [(x, mmap.PAGESIZE) for x in System.system_paddrs(va, size)]
194 return sysmem_view, [p + i for p, sz in paddrs for i in range(0, sz, 0x1000)][:ceildiv(size, 0x1000)]
195
196 def reset(self): os.system(f"sudo sh -c 'echo 1 > /sys/bus/pci/devices/{self.pcibus}/reset'")
197 def read_config(self, offset:int, size:int): return int.from_bytes(self.cfg_fd.read(size, binary=True, offset=offset), byteorder='little')

Callers 10

allocMethod · 0.45
init_swMethod · 0.45
__init__Method · 0.45
init_hwMethod · 0.45
provide_pagesMethod · 0.45
create_queueMethod · 0.45
_alloc_boot_memMethod · 0.45
handleFunction · 0.45
bench.pyFile · 0.45
connect.pyFile · 0.45

Calls 5

round_upFunction · 0.90
MMIOInterfaceClass · 0.90
ceildivFunction · 0.90
system_paddrsMethod · 0.80
anon_mmapMethod · 0.45

Tested by

no test coverage detected