mirror of
https://github.com/exaloop/codon.git
synced 2025-06-03 15:03:52 +08:00
* Add nvptx pass * Fix spaces * Don't change name * Add runtime support * Add init call * Add more runtime functions * Add launch function * Add intrinsics * Fix codegen * Run GPU pass between general opt passes * Set data layout * Create context * Link libdevice * Add function remapping * Fix linkage * Fix libdevice link * Fix linking * Fix personality * Fix linking * Fix linking * Fix linking * Add internalize pass * Add more math conversions * Add more re-mappings * Fix conversions * Fix __str__ * Add decorator attribute for any decorator * Update kernel decorator * Fix kernel decorator * Fix kernel decorator * Fix kernel decorator * Fix kernel decorator * Remove old decorator * Fix pointer calc * Fix fill-in codegen * Fix linkage * Add comment * Update list conversion * Add more conversions * Add dict and set conversions * Add float32 type to IR/LLVM * Add float32 * Add float32 stdlib * Keep required global values in PTX module * Fix PTX module pruning * Fix malloc * Set will-return * Fix name cleanup * Fix access * Fix name cleanup * Fix function renaming * Update dimension API * Fix args * Clean up API * Move GPU transformations to end of opt pipeline * Fix alloc replacements * Fix naming * Target PTX 4.2 * Fix global renaming * Fix early return in static blocks; Add __realized__ function * Format * Add __llvm_name__ for functions * Add vector type to IR * SIMD support [wip] * Update kernel naming * Fix early returns; Fix SIMD calls * Fix kernel naming * Fix IR matcher * Remove module print * Update realloc * Add overloads for 32-bit float math ops * Add gpu.Pointer type for working with raw pointers * Add float32 conversion * Add to_gpu and from_gpu * clang-format * Add f32 reduction support to OpenMP * Fix automatic GPU class conversions * Fix conversion functions * Fix conversions * Rename self * Fix tuple conversion * Fix conversions * Fix conversions * Update PTX filename * Fix filename * Add raw function * Add GPU docs * Allow nested object conversions * Add tests (WIP) * Update SIMD * Add staticrange and statictuple loop support * SIMD updates * Add new Vec constructors * Fix UInt conversion * Fix size-0 allocs * Add more tests * Add matmul test * Rename gpu test file * Add more tests * Add alloc cache * Fix object_to_gpu * Fix frees * Fix str conversion * Fix set conversion * Fix conversions * Fix class conversion * Fix str conversion * Fix byte conversion * Fix list conversion * Fix pointer conversions * Fix conversions * Fix conversions * Update tests * Fix conversions * Fix tuple conversion * Fix tuple conversion * Fix auto conversions * Fix conversion * Fix magics * Update tests * Support GPU in JIT mode * Fix GPU+JIT * Fix kernel filename in JIT mode * Add __static_print__; Add earlyDefines; Various domination bugfixes; SimplifyContext RAII base handling * Fix global static handling * Fix float32 tests * FIx gpu module * Support OpenMP "collapse" option * Add more collapse tests * Capture generics and statics * TraitVar handling * Python exceptions / isinstance [wip; no_ci] * clang-format * Add list comparison operators * Support empty raise in IR * Add dict 'or' operator * Fix repr * Add copy module * Fix spacing * Use sm_30 * Python exceptions * TypeTrait support; Fix defaultDict * Fix earlyDefines * Add defaultdict * clang-format * Fix invalid canonicalizations * Fix empty raise * Fix copyright * Add Python numerics option * Support py-numerics in math module * Update docs * Add static Python division / modulus * Add static py numerics tests * Fix staticrange/tuple; Add KwTuple.__getitem__ * clang-format * Add gpu parameter to par * Fix globals * Don't init loop vars on loop collapse * Add par-gpu tests * Update gpu docs * Fix isinstance check * Remove invalid test * Add -libdevice to set custom path [skip ci] * Add release notes; bump version [skip ci] * Add libdevice docs [skip ci] Co-authored-by: Ibrahim Numanagić <ibrahimpasa@gmail.com>
238 lines
6.0 KiB
Python
238 lines
6.0 KiB
Python
# (c) 2022 Exaloop Inc. All rights reserved.
|
|
|
|
from internal.gc import realloc, free
|
|
|
|
|
|
class File:
|
|
sz: int
|
|
buf: Ptr[byte]
|
|
fp: cobj
|
|
|
|
def __init__(self, fp: cobj):
|
|
self.fp = fp
|
|
self._reset()
|
|
|
|
def __init__(self, path: str, mode: str):
|
|
self.fp = _C.fopen(path.c_str(), mode.c_str())
|
|
if not self.fp:
|
|
raise IOError(f"file {path} could not be opened")
|
|
self._reset()
|
|
|
|
def _errcheck(self, msg: str):
|
|
err = int(_C.ferror(self.fp))
|
|
if err:
|
|
raise IOError(f"file I/O error: {msg}")
|
|
|
|
def __enter__(self):
|
|
pass
|
|
|
|
def __exit__(self):
|
|
self.close()
|
|
|
|
def __iter__(self) -> Generator[str]:
|
|
for a in self._iter():
|
|
yield a.__ptrcopy__()
|
|
|
|
def readlines(self) -> List[str]:
|
|
return [l for l in self]
|
|
|
|
def write(self, s: str):
|
|
self._ensure_open()
|
|
_C.fwrite(s.ptr, 1, len(s), self.fp)
|
|
self._errcheck("error in write")
|
|
|
|
def __file_write_gen__(self, g: Generator[T], T: type):
|
|
for s in g:
|
|
self.write(str(s))
|
|
|
|
def read(self, sz: int) -> str:
|
|
self._ensure_open()
|
|
buf = Ptr[byte](sz)
|
|
ret = _C.fread(buf, 1, sz, self.fp)
|
|
self._errcheck("error in read")
|
|
return str(buf, ret)
|
|
|
|
def tell(self) -> int:
|
|
ret = _C.ftell(self.fp)
|
|
self._errcheck("error in tell")
|
|
return ret
|
|
|
|
def seek(self, offset: int, whence: int):
|
|
_C.fseek(self.fp, offset, i32(whence))
|
|
self._errcheck("error in seek")
|
|
|
|
def flush(self):
|
|
_C.fflush(self.fp)
|
|
|
|
def close(self):
|
|
if self.fp:
|
|
_C.fclose(self.fp)
|
|
self.fp = cobj()
|
|
if self.buf:
|
|
_C.free(self.buf)
|
|
self._reset()
|
|
|
|
def _ensure_open(self):
|
|
if not self.fp:
|
|
raise IOError("I/O operation on closed file")
|
|
|
|
def _reset(self):
|
|
self.buf = Ptr[byte]()
|
|
self.sz = 0
|
|
|
|
def _iter(self) -> Generator[str]:
|
|
self._ensure_open()
|
|
while True:
|
|
rd = _C.getline(
|
|
Ptr[Ptr[byte]](self.__raw__() + 8), Ptr[int](self.__raw__()), self.fp
|
|
)
|
|
if rd != -1:
|
|
yield str(self.buf, rd)
|
|
else:
|
|
break
|
|
|
|
def _iter_trim_newline(self) -> Generator[str]:
|
|
self._ensure_open()
|
|
while True:
|
|
rd = _C.getline(
|
|
Ptr[Ptr[byte]](self.__raw__() + 8), Ptr[int](self.__raw__()), self.fp
|
|
)
|
|
if rd != -1:
|
|
if self.buf[rd - 1] == byte(10):
|
|
rd -= 1
|
|
yield str(self.buf, rd)
|
|
else:
|
|
break
|
|
|
|
|
|
def _gz_errcheck(stream: cobj):
|
|
errnum = i32(0)
|
|
msg = _C.gzerror(stream, __ptr__(errnum))
|
|
if msg and msg[0]:
|
|
raise IOError(f"zlib error: {str(msg, _C.strlen(msg))}")
|
|
|
|
|
|
class gzFile:
|
|
sz: int
|
|
buf: Ptr[byte]
|
|
fp: cobj
|
|
|
|
def __init__(self, fp: cobj):
|
|
self.fp = fp
|
|
self._reset()
|
|
|
|
def __init__(self, path: str, mode: str):
|
|
self.fp = _C.gzopen(path.c_str(), mode.c_str())
|
|
if not self.fp:
|
|
raise IOError(f"file {path} could not be opened")
|
|
self._reset()
|
|
|
|
def _getline(self) -> int:
|
|
if not self.buf:
|
|
self.sz = 128
|
|
self.buf = Ptr[byte](self.sz)
|
|
|
|
offset = 0
|
|
while True:
|
|
if not _C.gzgets(self.fp, self.buf + offset, i32(self.sz - offset)):
|
|
_gz_errcheck(self.fp)
|
|
if offset == 0:
|
|
return -1
|
|
break
|
|
|
|
offset += _C.strlen(self.buf + offset)
|
|
|
|
if self.buf[offset - 1] == byte(10): # '\n'
|
|
break
|
|
|
|
oldsz = self.sz
|
|
self.sz *= 2
|
|
self.buf = realloc(self.buf, self.sz, oldsz)
|
|
|
|
return offset
|
|
|
|
def __iter__(self) -> Generator[str]:
|
|
for a in self._iter():
|
|
yield a.__ptrcopy__()
|
|
|
|
def __enter__(self):
|
|
pass
|
|
|
|
def __exit__(self):
|
|
self.close()
|
|
|
|
def close(self):
|
|
if self.fp:
|
|
_C.gzclose(self.fp)
|
|
self.fp = cobj()
|
|
if self.buf:
|
|
free(self.buf)
|
|
self._reset()
|
|
|
|
def readlines(self) -> List[str]:
|
|
return [l for l in self]
|
|
|
|
def write(self, s: str):
|
|
self._ensure_open()
|
|
_C.gzwrite(self.fp, s.ptr, u32(len(s)))
|
|
_gz_errcheck(self.fp)
|
|
|
|
def __file_write_gen__(self, g: Generator[T], T: type):
|
|
for s in g:
|
|
self.write(str(s))
|
|
|
|
def tell(self) -> int:
|
|
ret = _C.gztell(self.fp)
|
|
_gz_errcheck(self.fp)
|
|
return ret
|
|
|
|
def seek(self, offset: int, whence: int):
|
|
_C.gzseek(self.fp, offset, i32(whence))
|
|
_gz_errcheck(self.fp)
|
|
|
|
def _iter(self) -> Generator[str]:
|
|
self._ensure_open()
|
|
while True:
|
|
rd = self._getline()
|
|
if rd != -1:
|
|
yield str(self.buf, rd)
|
|
else:
|
|
break
|
|
|
|
def _iter_trim_newline(self) -> Generator[str]:
|
|
self._ensure_open()
|
|
while True:
|
|
rd = self._getline()
|
|
if rd != -1:
|
|
if self.buf[rd - 1] == byte(10):
|
|
rd -= 1
|
|
yield str(self.buf, rd)
|
|
else:
|
|
break
|
|
|
|
def _ensure_open(self):
|
|
if not self.fp:
|
|
raise IOError("I/O operation on closed file")
|
|
|
|
def _reset(self):
|
|
self.buf = cobj()
|
|
self.sz = 0
|
|
|
|
|
|
def open(path: str, mode: str = "r") -> File:
|
|
return File(path, mode)
|
|
|
|
|
|
def gzopen(path: str, mode: str = "r") -> gzFile:
|
|
return gzFile(path, mode)
|
|
|
|
|
|
def is_binary(path: str) -> bool:
|
|
# https://stackoverflow.com/questions/898669/how-can-i-detect-if-a-file-is-binary-non-text-in-python/7392391#7392391
|
|
# Can get both false positive and false negatives, but still is a
|
|
# clever approach that works for the large majority of files
|
|
textchars = {7, 8, 9, 10, 12, 13, 27} | set(iter(range(0x20, 0x100))) - {0x7F}
|
|
with open(path, "rb") as f:
|
|
header = f.read(1024)
|
|
return any(ord(c) not in textchars for c in header)
|