Skip to content

core: POWER/VSX Improvements - #9907

Merged
opencv-pushbot merged 2 commits into
opencv:masterfrom
seiko2plus:vsxFixesImproves
Nov 16, 2017
Merged

opencv-pushbot merged 2 commits into
opencv:masterfrom
seiko2plus:vsxFixesImproves

Conversation

@seiko2plus

@seiko2plus seiko2plus commented Oct 23, 2017 •

Copy link
Copy Markdown
Contributor

POWER/VSX Improvements

Improvements are related to #7207

  • VSX compatibility in vsx_utils.hpp among compilers gcc, clang, xlc becomes more mature and organized.

  • As result of supporting IBM Advance toolchain, now we can build and test Opencv for powerpc in x86_64,
    For more information about IBM Advance toolchain and how to install it checkout this link.

    Options that are provided by toolchain

    • AT_PATH : Advance Toolchain directory, i.e. -DAT_PATH=/opt/at11.0

    • AT_RPATH : Add new directories to runtime search path, i.e. -DAT_RPATH=/path/to/opencv/lib,/path/to/opencl/runtime.
      Note:

      By default, the Advance Toolchain will prepend its own lib directory to the RPATH/RUNPATH.
      However, in case the user changes this behavior, he or she must ensure the Advance Toolchain
      lib directory will always appear before a directory with another libc, i.e. /usr/lib, /usr/lib64, /lib or /lib64.
      This will guarantee you won't mix parts of different glibc versions.

    • AT_HOST_LINK: instead of testing opencv on the target machine,
      you can turn this option ON to link against host advance toolchain runtime
      so you can perform tests directly in the host using QEMU user emulation.
      i.e. -DAT_HOST_LINK=ON , qemu-ppc64le ./bin/opencv_test_core

    • AT_NO_AUTOVEC: For some kind of reasons QEMU instruction emulation
      fails to load unaligend addresses correctly especially with floating-point if vectorization optimization is enabled.
      Vectorization in GCC is enabled by the flag -ftree-vectorize and by default at -O3,
      so you should enable this option if you going to build opencv in release mode.
      Note: Auto vectorization optimization works without any issues in PowerKVM/QEMU.

Current issues:

  • xlc fails to compile googletest
  • clang 5 aborted during parse googletest
  • There are several issues on big-endian mode and
    I'm willing to fix them later but I still have no intention to support vsx on it.
force_builders=Custom
docker_image:Custom=powerpc64le

@alalek

alalek commented Oct 26, 2017 •

Copy link
Copy Markdown
Member

Looks good to me +1

@seiko2plus

seiko2plus commented Oct 26, 2017 •

Copy link
Copy Markdown
Contributor Author

@alalek I implemented the new universal intrinsics and I think it's ready for merge
but I prefer to wait until @tomoaki0705 finishes #9753

@tomoaki0705

Copy link
Copy Markdown
Contributor

Thank you very much @seiko2plus . I really appreciate your kind help offer.
However ,the #9753 didin't end happily, I had to revert the dotprod version back to pure NEON version. (the story is written on #9753 )
This ends up in removing "accumulate" version of v_dotprod.
Still, v_rotate_left, v_rotate_right and v_matmuladd are there, but I had to remove new version of v_dotprod

looking in your PR, it seems you already implemented this new version of v_dotprod
I really appreciate your work, and I do respect your work, but since this intrinsic doesn't help, I removed it.

I'm sorry but to say this, but could you follow my modification, please ?

Again, nice work and sorry for disturbing because of my mistake.

- changed behavior of vec_ctf, vec_ctu, vec_cts
  in gcc and clang to make them compatible with XLC
- implemented most of missing conversion intrinsics in gcc and clang
- implemented conversions intrinsics of odd-numbered elements
- ignored gcc bug warning that caused by -Wunused-but-set-variable in rare cases
- replaced right shift with algebraic right shift for signed vectors
  to shift in the sign bit.
- added new universal intrinsics v_matmuladd, v_rotate_left/right
- avoid using floating multiply-add in RNG
@seiko2plus seiko2plus changed the title POWER/VSX Improvements core: POWER/VSX Improvements Oct 28, 2017
@seiko2plus

Copy link
Copy Markdown
Contributor Author

@tomoaki0705 It's alright, I removed the accumulate version of v_dotprod and I kept v_matmuladd, v_rotate_left/right.

@seiko2plus

Copy link
Copy Markdown
Contributor Author

@alalek I changed my mind, please leave this PR open until I finish the implementation of universal intrinsics (exp, log, sin, cos) for VSX and @tomoaki0705 finishes #9753 in case if he is gonna need any new intrinsics.
thank you.

@mshabunin

Copy link
Copy Markdown
Contributor

I've been able to build OpenCV using toolchain provided by Ubuntu 17.10.

Config:

FROM ubuntu:17.10

RUN apt-get update && apt-get install -y \
    curl git python3 cmake ninja-build \
    gcc-7-powerpc64le-linux-gnu \
    g++-7-powerpc64le-linux-gnu \
    binutils-powerpc64le-linux-gnu \
    libc6-ppc64-powerpc-cross \
    pkg-config-powerpc64le-linux-gnu

Toolchain file:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR powerpc64le)

set(CMAKE_C_COMPILER /usr/bin/powerpc64le-linux-gnu-gcc-7)
set(CMAKE_CXX_COMPILER /usr/bin/powerpc64le-linux-gnu-g++-7)

set(CMAKE_FIND_ROOT_PATH /usr/powerpc64le-linux-gnu)

SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

@mshabunin

Copy link
Copy Markdown
Contributor

I tried to run the core test in same container (need to install qemu-user package):

qemu-ppc64le -L /usr/powerpc64le-linux-gnu -E OPENCV_TEST_DATA_PATH=/testdata /build/bin/opencv_test_core

Note: options -fno-tree-vectorize -fno-tree-slp-vectorize were needed to fix some tests.

Unfortunately the hal_intrin.int8x16 test crashes (added --gtest_filter=*:-hal_intrin.8x16 to filter it out):

$ qemu-ppc64le -L /usr/powerpc64le-linux-gnu -E OPENCV_TEST_DATA_PATH=/testdata /build/bin/opencv_test_core
 --gtest_filter=*intrin*                                                                                                           
CTEST_FULL_OUTPUT                                                                                                                  
OpenCV version: 3.3.1-dev                                                                                      
OpenCV VCS version: unknown                                                                                                        
Build type: debug                                                                                          
Parallel framework: pthreads                                                                                                       
CPU features: VSX                                                                                              
OpenCL is disabled                                                                                                                 
Note: Google Test filter = *intrin*                                                                                       
[==========] Running 11 tests from 1 test case.                                                                                    
[----------] Global test environment set-up.                                                                                       
[----------] 11 tests from hal_intrin                                                                                              
[ RUN      ] hal_intrin.uint8x16                                                                                                   
[       OK ] hal_intrin.uint8x16 (8 ms)                                                                    
[ RUN      ] hal_intrin.int8x16                                                                                                    
Segmentation fault (core dumped) 

@alalek

alalek commented Nov 13, 2017

Copy link
Copy Markdown
Member

@mshabunin Add "-cpu POWER9" option to QEMU (it is "Illegal instruction" fault).

@seiko2plus

Copy link
Copy Markdown
Contributor Author

@mshabunin Did you used this current patch during tests?

if not, please use this current patch for the following reasons

when we use one of these VSX instructions that truncate double-precision to fixed-point word or
round doubleword to single-precision

Qemu instruction emulation return:
even-numbered elements of the result vector are zero and the other elements contains conversion result
ex: vec_ctu((vec_double2){1, 2}) return (vec_uint4) {0, 1, 0, 2}

PowerKVM/QEMU return: even and odd numbered elements contains conversion result
ex: vec_ctu((vec_double2){1, 2}) return (vec_uint4) {1, 1, 2, 2}

I didn't notice this issue because all my previous tests was only on PowerKVM.
I worked around this issue by use odd-numbered elements instead of even-numbered elements.

@alalek

alalek commented Nov 14, 2017

Copy link
Copy Markdown
Member

This says about vec_ctu (page 385):

Note: Elements 1 and 3 of the result vector are undefined when a is a double
vector

@mshabunin

mshabunin commented Nov 14, 2017 •

Copy link
Copy Markdown
Contributor

@alalek , yes, -cpu POWER9 helped, now tests are passing. Thank you.

@seiko2plus , I used this patch + #10078 + master

@seiko2plus

seiko2plus commented Nov 15, 2017 •

Copy link
Copy Markdown
Contributor Author

@alalek

Note: Elements 1 and 3 of the result vector are undefined when a is a double
vector

That was my thought before and ISA2.07 docs confirms the same.

but it seems that gcc and clang have different behavior than xlc.

I'm launching a full investigation into VSX conversions, please wait for my next reply.


@mshabunin old versions of qemu use power7 as default cpu and newest versions use power8 as default cpu.
That gives us two possibilities:

  • The default optimization of your gcc cross compiler is targeting power9, you can change default using -mtune option
    i.e. -mtune=power8

  • Qemu version is old, starting from version 1.6 the default cpu is power8, so changing default cpu to power8 should fix the issue
    i.e. -cpu power8

Anyway I prefer to use IBM Advance toolchain which I support in this patch, Did you try using it?

@mshabunin

Copy link
Copy Markdown
Contributor

@seiko2plus , thanks for clarification. I did not try IBM Advance toolchain, the way with standard Ubuntu packages looks easier and works well enough, probably we will use it for testing on the buildbot.

As IBM documentation says:

In some scenarios you will not get many advantages using the Advance Toolchain, and it will be easier to use the distro's toolchain that usually performs quite well. Such cases include when you are building applications that are not CPU-bound and on I/O constrained environments.

@mshabunin mshabunin self-assigned this Nov 16, 2017
@opencv-pushbot
opencv-pushbot merged commit 2dc76d5 into opencv:master Nov 16, 2017
@seiko2plus

Copy link
Copy Markdown
Contributor Author

@alalek

Results of VSX conversion instructions are affected by endianness:

  • any VSX conversion instruction that converts 4 to 2 elements on big-endian mode it returns as following
    {A, B, C, D} -> {A, C}
    and vice versa on little-endian
    {A, B, C, D} -> {B, D}

  • any VSX conversions instruction that convert 2 to 4 elements on big-endian mode it returns as following
    {A, B} -> {A, undefined, B, undefined}
    and vice versa on little-endian
    {A, B} -> {undefined, A, undefined, B}

On the other hand, xlc(little-endian) prefer the compatibility with big-endian
over the performance, since supports this behavior cost an extra instruction in little-endian.

I ended up with implemented conversions intrinsics of odd-numbered elements which it maps directly to VSX instructions,
and also we still support conversion of even-numbered elements for gcc, clang.
for more explanation please check following intrinsics in vsx_utils.hpp
vec_cvfo, vec_ctdo, vec_ctslo, vec_ctulo.

note:
current compatibility in xlc is broken with conversions between 2 to 4 elements,
I was going to fix it in this patch but since it had merge,
I'm going to release another patch fix this problem by the same way I did with 4 to 2 conversions,
also it will contain a fix for googletest issue in xlc.

@mshabunin

Copy link
Copy Markdown
Contributor

@seiko2plus , I thought we want to support only LE platforms:

#if defined(__VSX__) && defined(__PPC64__) && defined(__LITTLE_ENDIAN__)
#  define CV_VSX 1
#endif

@seiko2plus

seiko2plus commented Nov 18, 2017 •

Copy link
Copy Markdown
Contributor Author

@mshabunin Sorry for not being clear enough, you misunderstood me
I didn't mean that, I was just trying to explain the effects of little/big endian mode on the results of VSX conversion instructions in general.
However the next patch is going to add further clarification.

SavyaSanchi-Sharma pushed a commit to SavyaSanchi-Sharma/opencv that referenced this pull request Jul 30, 2026
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.

5 participants