Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experimental using openMP for encode #257

Closed
wants to merge 1 commit into from
Closed

Conversation

vrancurel
Copy link
Contributor

FIX #249

I was playing with openMP and encode speed, and just by doing this, on my 2-core VirtualBox I was able to double the speed of the encode basically as shown in those 2 measures:

Without:

54: rs-fnt_W1_N50_M50_D_C_NON_SYSTEMATIC,GEN,enc,11682,dec,0,
54: rs-fnt_W1_N3_M3_D_C_NON_SYSTEMATIC,GEN,enc,446,dec,0,REP,enc,371,dec,1748777,
54: rs-fnt_W1_N3_M3_D0-1_C0_NON_SYSTEMATIC,GEN,enc,531,dec,0,REP,enc,345,dec,1683961,
54: rs-fnt_W1_N3_M5_D0-1_C0_NON_SYSTEMATIC,GEN,enc,415,dec,0,REP,enc,306,dec,1859003,
54: rs-fnt_W1_N3_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,342,dec,0,REP,enc,282,dec,1702904,
54: rs-fnt_W1_N9_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,1015,dec,0,REP,enc,974,dec,7980070,
54: rs-fnt_W1_N9_M3_D2-3_C2_NON_SYSTEMATIC,GEN,enc,1140,dec,0,REP,enc,970,dec,7672327,
54: rs-fnt_W1_N9_M5_D2-3-4_C2-3_NON_SYSTEMATIC,GEN,enc,1014,dec,0,REP,enc,1021,dec,7717734,
54: rs-fnt_W1_N9_M5_D1-3-5_C1-3_NON_SYSTEMATIC,GEN,enc,1050,dec,0,REP,enc,961,dec,7620345,
54: rs-fnt_W1_N9_M5_D1-3-5-7-8_C_NON_SYSTEMATIC,GEN,enc,1058,dec,0,REP,enc,1003,dec,7513444,
54: rs-fnt_W1_N9_M5_D_C0-1-2-3-4_NON_SYSTEMATIC,GEN,enc,1030,dec,0,REP,enc,1053,dec,7828909,
54: rs-fnt_W2_N50_M50_D_C_NON_SYSTEMATIC,GEN,enc,5331,dec,0,
54: rs-fnt_W2_N3_M3_D_C_NON_SYSTEMATIC,GEN,enc,160,dec,0,REP,enc,119,dec,623762,
54: rs-fnt_W2_N3_M3_D0-1_C0_NON_SYSTEMATIC,GEN,enc,137,dec,0,REP,enc,121,dec,622016,
54: rs-fnt_W2_N3_M5_D0-1_C0_NON_SYSTEMATIC,GEN,enc,311,dec,0,REP,enc,132,dec,626148,
54: rs-fnt_W2_N3_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,119,dec,0,REP,enc,117,dec,636176,
54: rs-fnt_W2_N9_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,530,dec,0,REP,enc,457,dec,3159750,
54: rs-fnt_W2_N9_M3_D2-3_C2_NON_SYSTEMATIC,GEN,enc,454,dec,0,REP,enc,487,dec,3222815,
54: rs-fnt_W2_N9_M5_D2-3-4_C2-3_NON_SYSTEMATIC,GEN,enc,474,dec,0,REP,enc,473,dec,3207778,
54: rs-fnt_W2_N9_M5_D1-3-5_C1-3_NON_SYSTEMATIC,GEN,enc,486,dec,0,REP,enc,456,dec,3105525,
54: rs-fnt_W2_N9_M5_D1-3-5-7-8_C_NON_SYSTEMATIC,GEN,enc,471,dec,0,REP,enc,450,dec,3158596,
54: rs-fnt_W2_N9_M5_D_C0-1-2-3-4_NON_SYSTEMATIC,GEN,enc,458,dec,0,REP,enc,463,dec,3284101,

With:

54: rs-fnt_W1_N50_M50_D_C_NON_SYSTEMATIC,GEN,enc,6379,dec,0,
54: rs-fnt_W1_N3_M3_D_C_NON_SYSTEMATIC,GEN,enc,292,dec,0,REP,enc,218,dec,1513485,
54: rs-fnt_W1_N3_M3_D0-1_C0_NON_SYSTEMATIC,GEN,enc,536,dec,0,REP,enc,205,dec,1622537,
54: rs-fnt_W1_N3_M5_D0-1_C0_NON_SYSTEMATIC,GEN,enc,570,dec,0,REP,enc,224,dec,1536442,
54: rs-fnt_W1_N3_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,521,dec,0,REP,enc,235,dec,1527373,
54: rs-fnt_W1_N9_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,946,dec,0,REP,enc,804,dec,5863211,
54: rs-fnt_W1_N9_M3_D2-3_C2_NON_SYSTEMATIC,GEN,enc,769,dec,0,REP,enc,736,dec,6125558,
54: rs-fnt_W1_N9_M5_D2-3-4_C2-3_NON_SYSTEMATIC,GEN,enc,767,dec,0,REP,enc,751,dec,5942816,
54: rs-fnt_W1_N9_M5_D1-3-5_C1-3_NON_SYSTEMATIC,GEN,enc,784,dec,0,REP,enc,737,dec,5957581,
54: rs-fnt_W1_N9_M5_D1-3-5-7-8_C_NON_SYSTEMATIC,GEN,enc,786,dec,0,REP,enc,728,dec,5824002,
54: rs-fnt_W1_N9_M5_D_C0-1-2-3-4_NON_SYSTEMATIC,GEN,enc,791,dec,0,REP,enc,735,dec,5762718,
54: rs-fnt_W2_N50_M50_D_C_NON_SYSTEMATIC,GEN,enc,4052,dec,0,
54: rs-fnt_W2_N3_M3_D_C_NON_SYSTEMATIC,GEN,enc,306,dec,0,REP,enc,141,dec,576822,
54: rs-fnt_W2_N3_M3_D0-1_C0_NON_SYSTEMATIC,GEN,enc,134,dec,0,REP,enc,84,dec,581018,
54: rs-fnt_W2_N3_M5_D0-1_C0_NON_SYSTEMATIC,GEN,enc,148,dec,0,REP,enc,83,dec,520250,
54: rs-fnt_W2_N3_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,88,dec,0,REP,enc,84,dec,521601,
54: rs-fnt_W2_N9_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,326,dec,0,REP,enc,332,dec,2191755,
54: rs-fnt_W2_N9_M3_D2-3_C2_NON_SYSTEMATIC,GEN,enc,338,dec,0,REP,enc,313,dec,2350386,
54: rs-fnt_W2_N9_M5_D2-3-4_C2-3_NON_SYSTEMATIC,GEN,enc,349,dec,0,REP,enc,326,dec,2253370,
54: rs-fnt_W2_N9_M5_D1-3-5_C1-3_NON_SYSTEMATIC,GEN,enc,362,dec,0,REP,enc,325,dec,2271510,
54: rs-fnt_W2_N9_M5_D1-3-5-7-8_C_NON_SYSTEMATIC,GEN,enc,348,dec,0,REP,enc,355,dec,2175632,
54: rs-fnt_W2_N9_M5_D_C0-1-2-3-4_NON_SYSTEMATIC,GEN,enc,345,dec,0,REP,enc,328,dec,2279095,

I assume using it on 4 cores with quadruple the speed, etc

There is a little bit more work due to breaks that will require using shared conditions, but parallelizing the decode is feasible because there are a lot of for loops.

I think more and more this is the right approach for threading this lib (indeed using threads will involve using thread pools and can complexify the code, etc).

We nevertheless need to have a clean implem in case the pragma is not supported by the compiler.

@vrancurel
Copy link
Contributor Author

Apparently cmake has issues, I don't know why, I just added -fopenmp

@vrancurel
Copy link
Contributor Author

54: rs-fnt_W1_N50_M50_D_C_NON_SYSTEMATIC,GEN,enc,1062,dec,0,
54: rs-fnt_W1_N3_M3_D_C_NON_SYSTEMATIC,GEN,enc,825,dec,0,REP,enc,176,dec,229342,
54: rs-fnt_W1_N3_M3_D0-1_C0_NON_SYSTEMATIC,GEN,enc,59,dec,0,REP,enc,65,dec,216543,
54: rs-fnt_W1_N3_M5_D0-1_C0_NON_SYSTEMATIC,GEN,enc,689,dec,0,REP,enc,77,dec,209777,
54: rs-fnt_W1_N3_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,180,dec,0,REP,enc,39,dec,340361,
54: rs-fnt_W1_N9_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,395,dec,0,REP,enc,77,dec,666570,
54: rs-fnt_W1_N9_M3_D2-3_C2_NON_SYSTEMATIC,GEN,enc,204,dec,0,REP,enc,79,dec,667393,
54: rs-fnt_W1_N9_M5_D2-3-4_C2-3_NON_SYSTEMATIC,GEN,enc,102,dec,0,REP,enc,87,dec,686840,
54: rs-fnt_W1_N9_M5_D1-3-5_C1-3_NON_SYSTEMATIC,GEN,enc,137,dec,0,REP,enc,92,dec,680534,
54: rs-fnt_W1_N9_M5_D1-3-5-7-8_C_NON_SYSTEMATIC,GEN,enc,785,dec,0,REP,enc,82,dec,2902603,
54: rs-fnt_W1_N9_M5_D_C0-1-2-3-4_NON_SYSTEMATIC,GEN,enc,210,dec,0,REP,enc,82,dec,641399,
54: rs-fnt_W2_N50_M50_D_C_NON_SYSTEMATIC,GEN,enc,3438,dec,0,
54: rs-fnt_W2_N3_M3_D_C_NON_SYSTEMATIC,GEN,enc,35,dec,0,REP,enc,11,dec,50672,
54: rs-fnt_W2_N3_M3_D0-1_C0_NON_SYSTEMATIC,GEN,enc,609,dec,0,REP,enc,11,dec,59649,
54: rs-fnt_W2_N3_M5_D0-1_C0_NON_SYSTEMATIC,GEN,enc,16,dec,0,REP,enc,16,dec,63751,
54: rs-fnt_W2_N3_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,326,dec,0,REP,enc,12,dec,66271,
54: rs-fnt_W2_N9_M3_D1-2_C2_NON_SYSTEMATIC,GEN,enc,68,dec,0,REP,enc,32,dec,178869,
54: rs-fnt_W2_N9_M3_D2-3_C2_NON_SYSTEMATIC,GEN,enc,40,dec,0,REP,enc,37,dec,156572,
54: rs-fnt_W2_N9_M5_D2-3-4_C2-3_NON_SYSTEMATIC,GEN,enc,44,dec,0,REP,enc,31,dec,164454,
54: rs-fnt_W2_N9_M5_D1-3-5_C1-3_NON_SYSTEMATIC,GEN,enc,153,dec,0,REP,enc,32,dec,165296,
54: rs-fnt_W2_N9_M5_D1-3-5-7-8_C_NON_SYSTEMATIC,GEN,enc,77,dec,0,REP,enc,31,dec,147815,
54: rs-fnt_W2_N9_M5_D_C0-1-2-3-4_NON_SYSTEMATIC,GEN,enc,397,dec,0,REP,enc,32,dec,167849,

On the following HW:

% cat /proc/cpuinfo                                                                                                                                                                                                                                                  -- INSERT --
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 142
model name	: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
stepping	: 9
microcode	: 0x8e
cpu MHz		: 800.003
cache size	: 4096 KB
physical id	: 0
siblings	: 4
core id		: 0
cpu cores	: 2
apicid		: 0
initial apicid	: 0
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips	: 5808.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
processor	: 1
vendor_id	: GenuineIntel
cpu family	: 6
model		: 142
model name	: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
stepping	: 9
microcode	: 0x8e
cpu MHz		: 800.000
cache size	: 4096 KB
physical id	: 0
siblings	: 4
core id		: 1
cpu cores	: 2
apicid		: 2
initial apicid	: 2
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips	: 5808.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
processor	: 2
vendor_id	: GenuineIntel
cpu family	: 6
model		: 142
model name	: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
stepping	: 9
microcode	: 0x8e
cpu MHz		: 800.007
cache size	: 4096 KB
physical id	: 0
siblings	: 4
core id		: 0
cpu cores	: 2
apicid		: 1
initial apicid	: 1
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips	: 5808.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:
processor	: 3
vendor_id	: GenuineIntel
cpu family	: 6
model		: 142
model name	: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz
stepping	: 9
microcode	: 0x8e
cpu MHz		: 800.006
cache size	: 4096 KB
physical id	: 0
siblings	: 4
core id		: 1
cpu cores	: 2
apicid		: 3
initial apicid	: 3
fpu		: yes
fpu_exception	: yes
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp flush_l1d
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf
bogomips	: 5808.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:

@@ -121,6 +121,7 @@ include(CheckCXXCompilerFlag)
set(COMMON_CXX_FLAGS
-pipe
-Wall
-fopenmp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To do it in a portable way, we should use use find_package and use the populated variable.

An example is shown here :

cmake_minimum_required(VERSION 3.9)
project(solver LANGUAGES CXX)

find_package(OpenMP REQUIRED)
add_executable(solver solver.cc)
target_link_libraries(solver PRIVATE OpenMP::OpenMP_CXX)

Note that we may need to bump our minimum version for CMake, because OpenMP support got reworked in 3.9 (which is only one year old: I don't know if it's too recent to be easily used from most of Linux distribution).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative way:

find_package(OpenMP)
if (OPENMP_FOUND)
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()

If OpenMP is not found, its acceleration #pragma will be disabled.

Copy link
Contributor

@slaperche-scality slaperche-scality Oct 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't always work (hence the revamp in 3.9), see https://cmake.org/Bug/view.php?id=15393

@slaperche-scality
Copy link
Contributor

We nevertheless need to have a clean implem in case the pragma is not supported by the compiler.

Yes, OpenMP is nice and easy to use, that's for sure and thus it can be a useful tool to quickly test which part we want to parallelize and check if it's worth it or not.

Now, as you mentionned, I'm not sure if we do want to use it in "production" because:

  • it seems not supported by WebAssembly.
  • it seems to be supported on Android, at some degree at least, but not by default (from what I have seen after a quick search, you need, at least, to switch the NDK toolchain from Clang to GCC…).

So, if we have to implement our own parallelization solution as fallback for these cases, why not only use our own implementation? Using both OpenMP/homemade fallback will make us maintain and test these two codebase/codepath?

We already have 3 differents build to test now (scalar, SSE and AVX (and we may get AVX512 and NEON in the future)), if we add another variation point (OpenMP/fallback), the number of combinations to test will grows

Now, if OpenMP is well supported on all our targets then we should go for it instead of using an homemade solution, of course.

@vrancurel
Copy link
Contributor Author

@catid what do you think ? shall we use OpenMP or threads in this lib ? Is it interesting to provide the threading facility for the caller instead of providing a library operating on a single core ?

@slaperche-scality
Copy link
Contributor

Is it interesting to provide the threading facility for the caller instead of providing a library operating on a single core ?

My opinion on this part is that implementing multithreading into the lib (if it's worth it, and your little experiment with OpenMP seems to show that it's worth it!) then we give our users more choice/flexibility.

If we support multithreading in the library, then our users have the choice between:

  1. fine-grained parallelism (parallelism when processing a single file/buffer of data), implemented by us in the internals of the lib => you can get a speedup even when processing a single file/buffer of data
  2. coarse-grained parallelism (parallelism when processing several files/buffer of data), implemented by the user itself => the user can disable our parallelism (if we support multithreading, the number of thread to use should be configurable and setting it to 1 should disable multithreading) so that the encoding of a file only use a single thread, but then the user can implement something to encode batch of files at once (no speedup when processing a single file/buffer of data though)
  3. use a mix of both

If we don't implement multithreading, well then only the approach 2 is possible for the user, no choice.

@scality scality deleted a comment from slaperche-scality Oct 23, 2018
@vrancurel
Copy link
Contributor Author

Please continue the discussion on issue #249

@vrancurel vrancurel closed this Oct 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants