Skip to content

Instantly share code, notes, and snippets.

@pydemo
Last active September 12, 2018 15:22
Show Gist options
  • Select an option

  • Save pydemo/483e4c0e5ffd0d506f6bb030c0480f6c to your computer and use it in GitHub Desktop.

Select an option

Save pydemo/483e4c0e5ffd0d506f6bb030c0480f6c to your computer and use it in GitHub Desktop.
OpenPM prange schedule in Cython

static

Iterations are assigned to threads in a fixed way at compile time. If chunksize is not given, the iterations are distributed in num_threads contiguous blocks, one block per thread. If chunksize is given, each chunk is assigned to threads in a round-robin fashion. This is best when the work is evenly distributed and generally known ahead of time.

dynamic

Threads ask the scheduler for the next chunk dynamically at runtime. The chunksize defaults to 1. A dynamic schedule is best when the workload is unevenly distributed and unknown ahead of time.

guided

Chunks are distributed dynamically, like with dynamic. Unlike with dynamic, the chunksize is not fixed but rather is proportional to the remaining iterations divided by the number of threads.

runtime

The schedule and chunksize are determined by either the openmp.openmp_set_schedule function or the OMP_SCHEDULE environment variable at runtime. This allows exploration of different schedules and chunksizes without recompiling, but may have poorer performance overall as no compile-time optimizations are possible.

	for i in prange(resolution + 1, nogil=True,
					schedule='dynamic', chunksize=1):
		thread_id = threadid()
		fprintf(stdout, "%d\n", <int>thread_id)

prange also accepts a num_threads argument to control the number of threads to use during execution. If num_threads is not provided, prange uses as many threads as there are CPU cores available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment