Skip to content

Commit

Permalink
hip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Nov 26, 2023
1 parent 6ed5ce4 commit ab12afd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

description = (Path(__file__).resolve().parent / "README.md").read_text()
setup(name='gpuctypes',
version='0.1.1',
version='0.1.2',
description='ctypes wrappers for HIP, CUDA, and OpenCL',
author='George Hotz',
long_description=description,
Expand Down
40 changes: 21 additions & 19 deletions test/test_hip.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ def to_char_p_p(options: List[str]) -> c_char_p_p:
c_options[:] = [ctypes.create_string_buffer(o.encode("utf-8")) for o in options]
return c_options

def get_hip_bytes(arg, get_sz, get_str) -> bytes:
sz = ctypes.c_size_t()
check(get_sz(arg, ctypes.byref(sz)))
mstr = ctypes.create_string_buffer(sz.value)
check(get_str(arg, mstr))
return ctypes.string_at(mstr, size=sz.value)

class TestHIP(unittest.TestCase):
def test_malloc(self):
ptr = ctypes.c_void_p()
Expand All @@ -27,29 +34,24 @@ def test_get_device_properties(self) -> hip.hipDeviceProp_t:
print(device_properties.gcnArchName)
return device_properties

def test_compile_fail(self):
prg = "void test() { {"
prog = hip.hiprtcProgram()
check(hip.hiprtcCreateProgram(ctypes.pointer(prog), prg.encode(), "<null>".encode(), 0, None, None))
status = hip.hiprtcCompileProgram(prog, 0, None)
assert status != 0
log = get_hip_bytes(prog, hip.hiprtcGetProgramLogSize, hip.hiprtcGetProgramLog).decode()
assert len(log) > 10
print(log)

def test_compile(self):
hip.hipSetDevice(0)
prg = "void test() { }"
prog = hip.hiprtcProgram()
check(hip.hiprtcCreateProgram(ctypes.pointer(prog), prg.encode(), "<null>".encode(), 0, None, None))
# NOTE: this is segfaulting with options
#options = [f'--offload-arch={self.test_get_device_properties().gcnArchName}']
options = []
status = hip.hiprtcCompileProgram(prog, len(options), to_char_p_p(options))
if status != 0:
log_size = ctypes.c_size_t()
check(hip.hiprtcGetProgramLogSize(prog, ctypes.byref(log_size)))
logstr = ctypes.create_string_buffer(log_size.value)
check(hip.hiprtcGetProgramLog(prog, logstr))
print(ctypes.string_at(logstr))
code_size = ctypes.c_size_t()
check(hip.hiprtcGetCodeSize(prog, ctypes.byref(code_size)))
assert code_size.value > 10
#c_options = (ctypes.POINTER(ctypes.c_char) * 2)()
#c_options[0] = ctypes.create_string_buffer(f'--offload-arch={self.test_get_device_properties().gcnArchName}'.encode())
#c_options[1] = None
#hip.hiprtcCompileProgram(prog, 1, ctypes.cast(c_options, ctypes.POINTER(ctypes.POINTER(ctypes.c_char))))
#return hip.hiprtcGetCode(prog)
options = [f'--offload-arch={self.test_get_device_properties().gcnArchName.decode()}']
check(hip.hiprtcCompileProgram(prog, len(options), to_char_p_p(options)))
code = get_hip_bytes(prog, hip.hiprtcGetCodeSize, hip.hiprtcGetCode)
assert len(code) > 10

if __name__ == '__main__':
unittest.main()

0 comments on commit ab12afd

Please sign in to comment.