caffe icon indicating copy to clipboard operation
caffe copied to clipboard

prediction in multi-thread has problems?

Open KangBing opened this issue 10 years ago • 3 comments

I use caffe/examples/cpp_classification to predict. I meet a strange problem in multi-thread. When I create Classifier in thread A and use Classifier->Classify() in thread A, it predicts very fast. But when I create Classifier in thread A and use Classifier->Classify() in thread B, it predicts very slow. Is that a feature of caffe, or I use prediction in a wrong way?

KangBing avatar Aug 15 '16 14:08 KangBing

Hello @KangBing

This is because the Caffe context is a TLS variable: https://github.com/BVLC/caffe/blob/master/src/caffe/common.cpp#L15-L20 So, if a new thread tries to use Caffe, it will initialize a new context (and initialize cublas/curand), making it slow the first time. I published a small code sample implementing an heavily multithreaded inference server using the Go programming language: https://github.com/NVIDIA/gpu-rest-engine My solution in this case is to patch caffe to make the context settable for any thread, then any thread can perform inference efficiently: https://github.com/flx42/caffe/commit/1a5187a259a5cb31fef0e091bfe4795b268b1238

flx42 avatar Aug 16 '16 01:08 flx42

@flx42 Thanks for the answer.

KangBing avatar Aug 16 '16 07:08 KangBing

你好,@KangBing,我也遇到这个问题,请问您是怎么解决的。

xiaoluojun avatar Sep 04 '17 09:09 xiaoluojun