Logo

Programming-Idioms

  • Fortran
  • Rust
  • Python

Idiom #56 Launch 1000 parallel tasks and wait for completion

Fork-join : launch the concurrent execution of procedure f with parameter i from 1 to 1000.
Tasks are independent and f(i) doesn't return any value.
Tasks need not run all at the same time, so you may use a pool.
Wait for the completion of the 1000 tasks and then print "Finished".

from multiprocessing import Pool
def f(i):
	i * i

with Pool(1000) as p:
	p.map(func=f, iterable=range(1, 1001))

print('Finished')
  integer :: tasks, n, t, i
  tasks = 1000
  n = num_images()
  t = this_image()
  do i = t, tasks, n
     call f(i)
  end do
  sync all
  print *,"Finished"

This uses the parallel features of Fortran, running as many taks in parallel as there are images.
use std::thread;
let threads: Vec<_> = (0..1000).map(|i| thread::spawn(move || f(i))).collect();

for t in threads {
	t.join();
}
#include<latch>
#include<thread>
size_t taskCount = 1000;
auto remainingTasks = std::latch(static_cast<std::ptrdiff_t>(taskCount));
auto f = [&remainingTasks](int i) {
  // do work
  remainingTasks.count_down();
};

std::vector<std::jthread> threads{ 10 };
for (auto i = 0; i < taskCount; i++) {
  auto& workerThread = threads[i % 10];
  if (workerThread.joinable()) {
    workerThread.join();
  }
  workerThread = std::jthread(f, i);
}

remainingTasks.wait();
std::cout << "Finished";

Latch is used to wait until all tasks are completed. Simulated thread pool with vector of jthreads.

New implementation...