pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

vips_concurrency_set analog

Open q3cpma opened this issue 2 years ago • 5 comments

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.

q3cpma avatar Sep 22 '23 08:09 q3cpma

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.

jcupitt avatar Sep 22 '23 09:09 jcupitt

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.

q3cpma avatar Sep 22 '23 11:09 q3cpma

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 avatar Sep 22 '23 11:09 jcupitt

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

jcupitt avatar Sep 22 '23 11:09 jcupitt

Thanks for the detail.

q3cpma avatar Sep 22 '23 12:09 q3cpma