libvips / pyvips

python binding for libvips using cffi
MIT License
649 stars 50 forks source link

vips_concurrency_set analog #425

Open q3cpma opened 1 year ago

q3cpma commented 1 year ago

Hello, is there a way to limit concurrency from within a pyvips using process? I'll probably need it to do process based SMP instead of using internal threads.

Sorry in advance if it's right under my eyes, but perusing the documentation didn't help.

jcupitt commented 1 year ago

You can set the env var VIPS_CONCURRENCY to set the threadpool size.

Though libvips will automatically size down threadpools if the host machine becomes overloaded (ie. VIPS_CONCURRENCY really sets the maximum threadpool size), so it may be unnecessary.

q3cpma commented 1 year ago

I see, the variable is read only during module import, right? At least my experimentation says so.

About the self-balancing, does it use load average or something else? Interesting feature.

jcupitt commented 1 year ago

That's true, just on import. It's a single per-process setting.

You can also set concurrency on an image, then downstream processing on that image will use that setting.

The adaptive pool sizing works by counting blocked threads. If a thread stops on a mutex, it looks up its threadpool and increments an "n-blocked-threads" counter on the pool. The thread work unit dispatcher watches this variable and (with a bit of hysteresis) sizes the pool up and down so that only a few threads are blocked. Pools starts off with concurrency / 2 workers, then either grow or shrink depending on the workload.

Pools generally size on IO, but they'll size on system load too, since a heavily loaded system will tend to leave workers blocked.

jcupitt commented 1 year ago

You can see the sizing like this:

$ vips copy st-francis.jpg x.jpg --vips-info
VIPS-INFO: 12:47:28.607: threadpool completed with 4 workers

That's very IO bound, so the pool is small (this PC has 32 hardware threads).

If I do something with more CPU load, the pool sizes larger:

$ vips gaussblur st-francis.jpg x.jpg 20 --vips-info
VIPS-INFO: 12:49:28.429: gaussblur mask width 71
VIPS-INFO: 12:49:28.429: threadpool completed with 2 workers
VIPS-INFO: 12:49:28.429: threadpool completed with 2 workers
VIPS-INFO: 12:49:28.429: threadpool completed with 2 workers
VIPS-INFO: 12:49:28.430: threadpool completed with 2 workers
VIPS-INFO: 12:49:28.432: convi: using C path
VIPS-INFO: 12:49:28.432: threadpool completed with 2 workers
VIPS-INFO: 12:49:28.432: threadpool completed with 1 workers
VIPS-INFO: 12:49:28.432: threadpool completed with 1 workers
VIPS-INFO: 12:49:28.433: threadpool completed with 1 workers
VIPS-INFO: 12:49:28.435: convi: using vector path
VIPS-INFO: 12:50:06.403: threadpool completed with 32 workers
q3cpma commented 1 year ago

Thanks for the detail.