Our example demonstrates how to implement a worker pool using Chapel’s task parallelism and synchronization primitives.
Our running program shows the 5 jobs being executed by various workers. The program only takes about 2 seconds despite doing about 5 seconds of total work because there are 3 workers operating concurrently.
In this Chapel version:
We use Chapel’s coforall loop to create multiple concurrent tasks, which is similar to launching goroutines in Go.
Instead of channels, we use shared arrays (jobs and results) for communication between tasks. Chapel’s memory model ensures safe concurrent access to these arrays.
The worker procedure takes arrays as arguments instead of channels.
We use Chapel’s sleep() function from the Time module to simulate work.
The main procedure creates the jobs, starts the workers, and then prints the results.
This example showcases Chapel’s support for task parallelism and how it can be used to implement a worker pool pattern, similar to the original Go example.