oneDNN icon indicating copy to clipboard operation
oneDNN copied to clipboard

how cound i know how many threads mkldnn application use

Open arthurhi opened this issue 7 years ago • 17 comments

hi, As the title , i wanna know it, the max thread number the appilication use when using mkldnn lib. how can i know it ? can the MKLDNN_VERBOSE show it? thanks a lot.

arthurhi avatar Jan 29 '19 07:01 arthurhi

i use icc to compile mkl-dnn.

arthurhi avatar Jan 29 '19 07:01 arthurhi

Hi @arthurhi,

Intel MKL-DNN relies on OpenMP (if built with it). So the library should use no more than omp_get_max_threads() threads. However, implementations may use less than this number of threads for performance reasons.

emfomenk avatar Jan 29 '19 17:01 emfomenk

thanks. now i can get max thread numbers 8 from omp_get_max_threads() interface. and my board has 8 cores, so it means less than 8 threads when implementing. and how can i set it up to 16 when compile mkl-dnn.

arthurhi avatar Jan 30 '19 02:01 arthurhi

@arthurhi Please let me know if this link answers your question: https://software.intel.com/en-us/mkl-windows-developer-guide-setting-the-number-of-threads-using-an-openmp-environment-variable

nathan-greeneltch-intel avatar Jan 30 '19 18:01 nathan-greeneltch-intel

@arthurhi, that the number of threads is a run-time setting, not a build-time setting, so you don't have to think about that when compiling the application.

You can choose the number of threads the library will use by setting OMP_NUM_THREADS environment variable or omp_set_num_threads() function call.

For the 8 core system we recommend using 8 threads to achieve the best throughput. If you are using multiple instances of an application the total number of threads should be not more than the total number of physical cores.

vpirogov avatar Jan 30 '19 20:01 vpirogov

thanks a lot . now i know it . and after my test appilication(lenet 10000 samples test), i find a strange result.
and if i set omp_set_num_threads(4); it will cost 508ms.

but if i set omp_set_num_threads(8); it will cost 580ms. and if i have not set the thread number, and call omp_get_max_threads to get value 8. why 4 threads will be faster than 8 threads? my computer is Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz x 8.

another question confused me, i use intel-mkl cblas interface and mkl-dnn to optimize my appilication as i said before. but now i find mkl-dnn cost 580ms with default thread numbers 8. and mkl cbass interface cost 1157ms, is it normal in theory ?

arthurhi avatar Jan 31 '19 01:01 arthurhi

@arthurhi From ark.intel.com, it looks like your CPU has 4 (physical) cores and 8 (logical) threads. For compute-heavy workloads it is reasonable to see best performance with OMP_NUM_THREADS equal to the number of physical cores.

kwiersch avatar Jan 31 '19 03:01 kwiersch

@arthurhi the "spin and overhead time" from _kmp_for_barrier increases a lot if you use all the CPU threads. In your platform, you have 8 CPU threads, so the "spin and overhead time" will increase dramatically if you set OMP_NUM_THREADS=8

if you can share your MKLDNN_VERBOSE log, we can verify it on a similar platform with benchdnn.

For the mkl cblas vs mkl-dnn question, can you share us your OpenMP thread affinity log for both cases with KMP verbose mode? you can set get OpenMP log by below command. export KMP_AFFINITY=verbose

you should get related CPU thread information like below diagram. hyper_threading

louie-tsai avatar Feb 09 '19 01:02 louie-tsai

@ louie-tsai thanks, as you say, i set the OpenMP log: root@ccm-64-uefi:/home/libin/lenet/test/linux_test# ./lenet_test_mkldnn 8
-------omp_set_num_threads = 8 OMP: Info #212: KMP_AFFINITY: decoding x2APIC ids. OMP: Info #210: KMP_AFFINITY: Affinity capable, using global cpuid leaf 11 info OMP: Info #154: KMP_AFFINITY: Initial OS proc set respected: 0-7 OMP: Info #156: KMP_AFFINITY: 8 available OS procs OMP: Info #157: KMP_AFFINITY: Uniform topology OMP: Info #179: KMP_AFFINITY: 1 packages x 8 cores/pkg x 1 threads/core (8 total cores) OMP: Info #250: KMP_AFFINITY: pid 943 tid 943 thread 0 bound to OS proc set 0-7 -------omp_get_max_threads = 8 -------omp_get_num_threads = 1 OMP: Info #250: KMP_AFFINITY: pid 943 tid 944 thread 1 bound to OS proc set 0-7 OMP: Info #250: KMP_AFFINITY: pid 943 tid 945 thread 2 bound to OS proc set 0-7 OMP: Info #250: KMP_AFFINITY: pid 943 tid 946 thread 3 bound to OS proc set 0-7 OMP: Info #250: KMP_AFFINITY: pid 943 tid 947 thread 4 bound to OS proc set 0-7 OMP: Info #250: KMP_AFFINITY: pid 943 tid 948 thread 5 bound to OS proc set 0-7 OMP: Info #250: KMP_AFFINITY: pid 943 tid 949 thread 6 bound to OS proc set 0-7 OMP: Info #250: KMP_AFFINITY: pid 943 tid 950 thread 7 bound to OS proc set 0-7 [test_single_file] filename ../out_3_28.yuv , ret = 3 ======[test_single_file 28x28 single sample] using mkl_dnn is completed===== ==============at 4090 milliseconds==========

inference cost total 4089857 us, avg 408 us

arthurhi avatar Feb 11 '19 05:02 arthurhi

Can you give me some advice about cpu tflops about Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz(4 cores 8 threads) VS Intel(R) Atom(TM) CPU C3734 @ 1.70GHz(8 cores).
Because i run lenet on both platform and get the time cost is so different, one reason is AVX2 vs SSE, and i don't know other reasons. and PC cost 0.5s, C3734 cost 4s.

arthurhi avatar Feb 11 '19 05:02 arthurhi

Assuming that both processors are running at base frequency theoretical peaks for single precision are the following

Processor cores instr/clock ops/instr fp32/simd GHz GFlops/s
Intel(R) Core(TM) i5-8250U 4 1 2 8 1.6 102.4
Intel(R) Atom(TM) C3734 8 1 1 4 1.7 54.4

Note, that in reality frequency for the i5-8250U system will likely to be higher due to Intel (R) Turbo Boost Technology.

vpirogov avatar Feb 12 '19 17:02 vpirogov

To see what is going on with performance on both systems you might want to collect the logs with MKLDNN_VERBOSE=1.

vpirogov avatar Feb 12 '19 17:02 vpirogov

@vpirogov Thank you very much! i collect the logs. c3734 log: mkldnn_verbose,exec,convolution,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic1oc20_ih28oh24kh5sh1dh0ph0_iw28ow24kw5sw1dw0pw0,0.143066 mkldnn_verbose,exec,pooling,nchw_pooling:any,forward_inference,fdata:nchw fws:undef,alg:pooling_max,mb1ic20_ih24oh12kh2sh2ph0_iw24ow12kw2sw2pw0,0.0888672 mkldnn_verbose,exec,convolution,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic20oc50_ih12oh8kh5sh1dh0ph0_iw12ow8kw5sw1dw0pw0,0.25 mkldnn_verbose,exec,pooling,nchw_pooling:any,forward_inference,fdata:nchw fws:undef,alg:pooling_max,mb1ic50_ih8oh4kh2sh2ph0_iw8ow4kw2sw2pw0,0.059082 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nc,,mb1ic800oc500,0.0769043 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nc fwei:oi fbia:x fdst:nc,,mb1ic500oc10,0.0090332 mkldnn_verbose,exec,softmax,ref:any,forward_inference,fdata:nc fdiff:undef,,mb1ic10,0.0090332

i5 log: mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_oihw out:f32_Ohwi8o,num:1,20x1x5x5,0.00292969 mkldnn_verbose,exec,convolution,jit:avx2,forward_inference,fsrc:nchw fwei:Ohwi8o fbia:x fdst:nChw8c,alg:convolution_direct,mb1_ic1oc20_ih28oh24kh5sh1dh0ph0_iw28ow24kw5sw1dw0pw0,0.0109863 mkldnn_verbose,exec,pooling,jit:avx,forward_inference,fdata:nChw8c fws:undef,alg:pooling_max,mb1ic20_ih24oh12kh2sh2ph0_iw24ow12kw2sw2pw0,0.00390625 mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_oihw out:f32_OIhw8i8o,num:1,50x20x5x5,0.0109863 mkldnn_verbose,exec,convolution,jit:avx2,forward_inference,fsrc:nChw8c fwei:OIhw8i8o fbia:x fdst:nChw8c,alg:convolution_direct,mb1_ic20oc50_ih12oh8kh5sh1dh0ph0_iw12ow8kw5sw1dw0pw0,0.0209961 mkldnn_verbose,exec,pooling,jit:avx,forward_inference,fdata:nChw8c fws:undef,alg:pooling_max,mb1ic50_ih8oh4kh2sh2ph0_iw8ow4kw2sw2pw0,0.00292969 mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_nChw8c out:f32_nchw,num:1,1x50x4x4,0.00390625 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nc,,mb1ic800oc500,0.0991211 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nc fwei:oi fbia:x fdst:nc,,mb1ic500oc10,0.00488281 mkldnn_verbose,exec,softmax,ref:any,forward_inference,fdata:nc fdiff:undef,,mb1ic10,0.00585938

arthurhi avatar Feb 13 '19 01:02 arthurhi

Thanks @arthurhi, performance is indeed as expected based on the logs. One thing to improve is to avoid weight reorders for convolutions, as these do not change from iteration to iteration on inference.

The reason why you see relatively lower performance on C3734 is because Intel MKL-DNN does not yet supports direct convolution algorithm (jit:sse42) on Intel SSE4.2 for the convolutions with the number of channels not being a multiple of 8. Due to this fact the convolutions are executed using GEMM-based implementation (gemm:blas), which has somewhat lower efficiency.

You can try padding the number of input and output channels to multiple of 8 to see performance comparison that is more representative of hardware capabilities.

vpirogov avatar Feb 13 '19 21:02 vpirogov

Thanks @vpirogov , as you say, i try it and a little performance improvement. and i can't pad the first convolution becasue the input channle is 1, if i pad the size ,i will pad the kernel size and source image. so i have not pad the first convolution. And now the log is :

mkldnn_verbose,exec,convolution,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic1oc24_ih28oh24kh5sh1dh0ph0_iw28ow24kw5sw1dw0pw0,0.440186 mkldnn_verbose,exec,pooling,nchw_pooling:any,forward_inference,fdata:nchw fws:undef,alg:pooling_max,mb1ic24_ih24oh12kh2sh2ph0_iw24ow12kw2sw2pw0,0.166016 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nChw8c,num:1,1x24x12x12,0.0700684 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_oihw out:f32_OIhw8i8o,num:1,56x24x5x5,0.0839844 mkldnn_verbose,exec,convolution,jit:sse42,forward_inference,fsrc:nChw8c fwei:OIhw8i8o fbia:x fdst:nChw8c,alg:convolution_direct,mb1_ic24oc56_ih12oh8kh5sh1dh0ph0_iw12ow8kw5sw1dw0pw0,0.273926 mkldnn_verbose,exec,pooling,jit:sse42,forward_inference,fdata:nChw8c fws:undef,alg:pooling_max,mb1ic56_ih8oh4kh2sh2ph0_iw8ow4kw2sw2pw0,0.0698242 mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nChw8c out:f32_nchw,num:1,1x56x4x4,0.00219727 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nc,,mb1ic896oc504,0.185791 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nc fwei:oi fbia:x fdst:nc,,mb1ic504oc10,0.0078125 mkldnn_verbose,exec,softmax,ref:any,forward_inference,fdata:nc fdiff:undef,,mb1ic10,0.00878906

i can see sse42 in the second convolution but inner_product still use gemm:blas although the number of input and output channel is 896,504, it is a multiple of 8.

arthurhi avatar Feb 14 '19 07:02 arthurhi

Hi @arthurhi, this seems right. There's definitely no need to pad the input channels for the first convolution. Inner product will always use GEMM algorithm, as this is the best algorithm for the operation in question.

vpirogov avatar Feb 25 '19 21:02 vpirogov

Closing the issue due to lack of activity. Feel free to reopen if you have any further questions.

vpirogov avatar Mar 06 '19 17:03 vpirogov