NVIDIA / NVIDIA/cccl

[DOC]: How do I share the semaphores amongst threads?

Open
#1,464 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
2.5k
Forks
486
Avg merge
2d 6h
Merged PRs (30d)
295

Description

### Is this a duplicate?

- [X] I confirmed there appear to be no [duplicate issues](https://github.com/NVIDIA/cccl/issues) for this bug and that I agree to the [Code of Conduct](CODE_OF_CONDUCT.md)

### Is this for new documentation, or an update to existing docs?

Update

### Describe the incorrect/future/missing documentation

I want to use semaphores to block all the threads in the system until the current one is done printing, but it appears that the objects aren't being shared amongst threads even if they have the system scope.

Here is an [example](https://github.com/mrakgr/The-Spiral-Language/blob/24607739a0d19242d5e78542d1ae2519e27b082b/Spiral%20Compilation%20Tests/cuda_experiments/tensor9/semaphore.spi#L56) in Spiral that I am trying to get to work.

```
inl lock : ref (_ _ thread_scope_system) = create_binary_semaphore 0
if grid_group_thread_rank()+1 = grid_group_num_threads() then release lock
acquire lock
console.write_ln "hello"
console.write_ln {id=grid_group_thread_rank()}
release lock
```

Only the last Cuda threads executes the write statements, and the rest deadlock. This tells me that the rest of the threads do not have access to the same `binary_semaphore` object.

[Here](https://github.com/mrakgr/The-Spiral-Language/blob/24607739a0d19242d5e78542d1ae2519e27b082b/Spiral%20Compilation%20Tests/cuda_experiments/tensor9/semaphore.py#L1) is the complete compiled output.

The documentation examples are very sparse regarding this. Just how are these objects supposed to be shared amongst the threads? I thought the `cuda::thread_scope_system` being [passed](https://github.com/mrakgr/The-Spiral-Language/blob/24607739a0d19242d5e78542d1ae2519e27b082b/Spiral%20Compilation%20Tests/cuda_experiments/tensor9/semaphore.py#L10C28-L10C53) as a template argument would be responsible for that, but it seems I was wrong.

### If this is a correction, please provide a link to the incorrect documentation. If this is a new documentation request, please link to where you have looked.

https://nvidia.github.io/cccl/libcudacxx/extended_api/synchronization_primitives/binary_semaphore.html

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.