Skip to main content

SIMD and threads

You never have to set any of this. libstb picks the fastest SIMD backend your CPU can run when the program starts, and it starts extra threads only for work that is big enough to pay for them. This page is for the day you want control: a library that must not start threads of its own, a server with one worker per CPU, or a bug you want to reproduce on the plain code.

import libstb as stb

big = stb.Image.open("astronaut.png").resize(1200, 800)
overlay = big.resize(1000, 700)


def work():
"""One resize (it can use several threads) and one blend (it has SIMD kernels): the bytes they give."""
return big.resize(300, 200).tobytes(), big.blend(overlay, 10, 10).tobytes()


reference = work() # automatic: the best backend and as many threads as pay for themselves
print("scalar is always available:", "yes" if "scalar" in stb.simd_backends() else "no")

# A block changes this thread only, and the old setting comes back when it ends. Blocks nest.
with stb.threads(1), stb.simd("scalar"): # no worker threads, the portable code
print("inside the block:", stb.simd_name(), "backend,", stb.thread_count(), "thread")
same = work() == reference

print("same pixels with one thread and the scalar code:", "yes" if same else "no")
print("after the block the best backend is back:", "yes" if stb.simd_name() == stb.simd_backends()[0] else "no")
Output
scalar is always available: yes
inside the block: scalar backend, 1 thread
same pixels with one thread and the scalar code: yes
after the block the best backend is back: yes

SIMD​

simd_backends() lists the backends this build can run on this CPU, best first: for example avx2, sse2, scalar on x86-64, or neon, scalar on arm64. scalar, the plain reference code, is always last. simd_name() is the one in use. Every backend gives the same pixels; they differ only in speed, so choosing one never changes a result. That is also what the example checks.

Threads​

thread_count() is the most threads, the calling one included, that one call on this thread may use. It is a ceiling, not a demand: every thread has to get at least about 250 microseconds of work, so a call with little work never starts one. The resize, the filters, tone and the superpixels use threads that way. Results do not depend on the number of threads: they are byte for byte what one thread gives.

Changing them​

There are two scopes of change, and the narrow one is the one to use in a library:

SettingFor the whole processFor one thread, for a block
SIMDstb.set_simd("scalar")with stb.simd("scalar"):
Threadsstb.set_threads(1)with stb.threads(1):

A block changes this thread only. Other threads are not affected, and when it ends the previous setting comes back. Blocks nest. The process-wide calls are for a program that owns the whole process.

For threads, 0 means automatic (the CPUs this process may use), 1 never uses another thread, and n at most n. Set 1 when your program already runs one worker per CPU. A name libstb does not know, or a backend this CPU cannot run, is an error in the block form.

The environment can choose the starting values: LIBSTB_SIMD=<name> and LIBSTB_THREADS=auto|off|<n>.

API at a glance​

Backends, the one in usestb.simd_backends(), stb.simd_name()
One backend for a blockwith stb.simd(name):
One backend for the processstb.set_simd(name)
The thread ceilingstb.thread_count()
A ceiling for a blockwith stb.threads(n):
A ceiling for the processstb.set_threads(n)