mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Does c_predict_api support multiple threads?

Open nightrank opened this issue 10 years ago • 2 comments

I have a function like this: void Predict(string file) { MXPredCreate(...); MXPredGetOutputShape(...); MXPredGetOutput(...); MXPredFree(...); }

This function will take 1 second to run (In CPU mode). It takes 100 seconds for 100 times when running in one thread (obviously) But it still takes 100 seconds when running in 10 threads, the CPU load keeps about 15%, which is almost the same as one thread.

So I can assume the predict API does not support multiple threads?

Since I compiled mxnet with USE_OPENMP=1, I have tried to set OMP_NUM_THREADS environment variable and use goto_set_num_threads function to control the number of threads, both not work.

https://github.com/xianyi/OpenBLAS/blob/1f217a6175f9a2e3528caee32d4e6fd982995af6/README.md

If you compile this lib with USE_OPENMP=1, you should set OMP_NUM_THREADS environment variable. OpenBLAS ignores OPENBLAS_NUM_THREADS and GOTO_NUM_THREADS with USE_OPENMP=1.

We provided the below functions to control the number of threads on runtime. void goto_set_num_threads(int num_threads); void openblas_set_num_threads(int num_threads);

How can I make the prediction function support multi-threading? Any comments are appreciated, thanks.

nightrank avatar Aug 13 '16 14:08 nightrank

This issue looks similar with this: https://github.com/dmlc/mxnet/issues/937

think it depends on how the cifar example uses the library. If it does not use matrix multiplication much, you will not see much speedup when using multiple threads. I think for Cifar, most of the time should be spent on the convolution layers? Then it comes to how convolution is implemented on CPU

But for prediction, each prediction from different thread should be independent. Prediction in multiple threads are really needed.

nightrank avatar Aug 13 '16 14:08 nightrank

Some update: when batch size > 1, it works great. But if multiple threads invoke with batch size = 1, it runs really slow. Seems due to OpenMP's design. I have implemented multi-process framework as a workaround, close this.

nightrank avatar Aug 18 '16 01:08 nightrank