Halide icon indicating copy to clipboard operation
Halide copied to clipboard

Reuse compiled pipeline on multiple threads.

Open mcourteaux opened this issue 8 years ago • 10 comments

I'm building a scientific academic research application, that uses Halide. I have access to a beefy 40-core machine. Is it possible to reuse a compiled Halide function/pipeline on multiple threads in parallel? In terms of: setting pipeline parameters, setting input buffers, setting output buffers, making sure internal temporary buffers are not shared.

I would like to save on compilation time, as I'm now literally JIT compiling the same pipeline 40 times before the application starts it's actual work.

mcourteaux avatar Oct 18 '17 13:10 mcourteaux

tl;dr We need to add some support to do this, though it isn't too difficult.

While multithreaded calls are fully supported for AOT compilation, there are two obstacles to making them work for JIT. (Note I'm going to use Pipeline instead of Func below as the actual implementation of functionality is at the Pipeline level. There should be pass through methods from Func to Pipeline for everything mentioned and thus the advice should apply to both.)

First, the Pipeline has to be compiled before it is called. The easiest way to do this is to arrange to call compile_jit() on the Pipeline on a single thread before doing realizations on other threads.

Second, JIT calls typically take their arguments from global Param and ImageParam objects and these are not threadsafe. The solution is to call the argv function directly with values taken from some passed in data structure, such as a map that has named entries corresponding to the names of the Param and ImageParam objects. If you need it in a hurry and are willing to make an array of pointers to the raw arguments, all this would require is making an accessor on Pipeline to the argv_function on the compiled module. But we should be able to make a threadsafe method that ensures the compilation has happened and takes all the args from passed in values...

zvookin avatar Oct 18 '17 17:10 zvookin

I am most definitely not familiar with the internals of Halide. The argv_function you are referring to, is that the function that is returned by compile_jit()? If so, does it take the same parameters as if I would generate a AOT header file?

The return value of compile_jit() could be documented better, I think.

mcourteaux avatar Oct 22 '17 11:10 mcourteaux

That would be a very helpful functionality, are there any plans on doing this? Just curious.

edubois avatar Nov 08 '17 17:11 edubois

Yes, it is on my TODO list. Probably not tomorrow, but probably within a month.

zvookin avatar Nov 08 '17 17:11 zvookin

Hi, any update on this? I would also be very interested. Thanks!

eldakms avatar Dec 05 '17 10:12 eldakms

Hi, Any news on this topic? Many thanks,

edubois avatar Dec 18 '17 03:12 edubois

Please take a look at https://github.com/halide/Halide/pull/2657 and comment if it does not meet your needs per this issue. I expect this will get merged within a week.

zvookin avatar Dec 20 '17 22:12 zvookin

Looks good to me, many thanks!

edubois avatar Dec 20 '17 23:12 edubois

Fixed by https://github.com/halide/Halide/pull/2657 . Pass a ParamMap as the last argument of realize to access the new functionality.

zvookin avatar Jan 09 '18 22:01 zvookin

I'm reopening this (5 years later!) because there is a proposed PR (https://github.com/halide/Halide/pull/6777) that may address this in a more flexible and useful way (and may eventually allow us to deprecate ParamMap entirely).

Could any of the original commenters (@mcourteaux and @edubois specifically) comment on whether they are (still?) using ParamMap (or Halide in general), and/or take a look at #6777 to see how it looks for their purposes?

steven-johnson avatar May 26 '22 16:05 steven-johnson