Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of using cudaStreamAddCallback #309

Closed
wensimin opened this issue Dec 17, 2024 · 1 comment
Closed

Example of using cudaStreamAddCallback #309

wensimin opened this issue Dec 17, 2024 · 1 comment

Comments

@wensimin
Copy link

I try to register a callback on a stream, and the following code reports an error

def stream_over(*argv):
    print(f"over !")

 cudart.cudaStreamAddCallback(stream, stream_over, None, 0)
nvidia-dev-1  |   cudart.cudaStreamAddCallback(stream._stream, stream_over, None, 0)
nvidia-dev-1  |   File "cuda/bindings/runtime.pyx", line 15344, in cuda.bindings.runtime.cudaStreamAddCallback
nvidia-dev-1  |   File "cuda/bindings/runtime.pyx", line 4054, in cuda.bindings.runtime.cudaStreamCallback_t.__cinit__
nvidia-dev-1  | TypeError: an integer is required

In the documentation I found this class cudaStreamCallback_t,but I don't know how to use it correctly
Any help?

@leofang
Copy link
Member

leofang commented Dec 17, 2024

Hi @wensimin this is how cudaStreamAddCallback (deprecated) and cudaLaunchHostFunc are tested:

def cudart_func_stream_callback(use_host_api):
class testStruct(ctypes.Structure):
_fields_ = [
("a", ctypes.c_int),
("b", ctypes.c_int),
("c", ctypes.c_int),
]
def task_callback_host(userData):
data = testStruct.from_address(userData)
assert data.a == 1
assert data.b == 2
assert data.c == 3
return 0
def task_callback_stream(stream, status, userData):
data = testStruct.from_address(userData)
assert data.a == 1
assert data.b == 2
assert data.c == 3
return 0
if use_host_api:
callback_type = ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_void_p)
target_task = task_callback_host
else:
callback_type = ctypes.PYFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p)
target_task = task_callback_stream
# Construct ctype data
c_callback = callback_type(target_task)
c_data = testStruct(1, 2, 3)
# ctypes is managing the pointer value for us
if use_host_api:
callback = cudart.cudaHostFn_t(_ptr=ctypes.addressof(c_callback))
else:
callback = cudart.cudaStreamCallback_t(_ptr=ctypes.addressof(c_callback))
# Run
err, stream = cudart.cudaStreamCreate()
assertSuccess(err)
if use_host_api:
(err,) = cudart.cudaLaunchHostFunc(stream, callback, ctypes.addressof(c_data))
assertSuccess(err)
else:
(err,) = cudart.cudaStreamAddCallback(stream, callback, ctypes.addressof(c_data), 0)
assertSuccess(err)
(err,) = cudart.cudaDeviceSynchronize()
assertSuccess(err)

@NVIDIA NVIDIA locked and limited conversation to collaborators Dec 17, 2024
@leofang leofang converted this issue into discussion #310 Dec 17, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants