pingcap / pingcap/tiflash

Math Function Vectorization

Open
#5,352 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type/feature-request
Dominant language
C++
Stars
1k
Forks
423
Avg merge
1d 15h
Merged PRs (30d)
24

Description

Feature Design Document

Background
Current Code Base

TiFlash currently has an relatively old math function vectorization mechanism inheritated from ClickHouse's old code base:
https://github.com/pingcap/tiflash/blob/34d7570a34c0daec5ca9c705296470b31d743d91/dbms/src/Functions/FunctionsMath.h#L30

However, when distributing the products, we never tried to enable it. Actually, at ClickHouse's master, this library has already been removed:
https://github.com/ClickHouse/ClickHouse/blob/d0e6f8ebc120a682bb09231fbbba1a904c8c29bb/src/Functions/FunctionMathUnary.h#L171

It seems that vectorclass has already become somehow inactive since 2021. Clickhouse quit using vectorized math functions anymore.

Vectorization Solution

Besides vectorclass, there are several alternatives providing math function vectorization:

  • SLEEF is a promising candidate. However, there is no new release since 2020.
  • SVML is the most famous one. However, it is designed for Intel CPU and it is a proprietary product.
  • LIBMVEC is a part of glibc licenced under LGPL. TiFlash is now mostly used on GNU/Linux envrionment. This can be considered as a suitable choice.

For aarch64 platform, tiflash has already introduced optimized-routines as one of its contrib library; which can provide vectorized variants for log, cos, sin, exp.

Proposed Solution

libmvec is generally available in environment with glibc 2.22+. However, as we still need to support some ancient platform like CentOS 7, it is possible that the deployed env does not have libmvec.

Therefore, we can use dlopen with dlsym to dynamically detect whether libmvec is available.

Another consideration is that vectorized math function will have a lower precision. Therefore, there are potential changes in behavior when vectorization is enabled. To avoid trouble, we should provide a switch for the user and disable vectorization on default.

Summary
  • remove dbms/cmake/find_vectorclass.cmake and related macros in source.
  • add unary_math_vectorization = false to tiflash's configuration.
  • add unit tests for unary math functions.
  • vectorized the following functions on x86-64 using libmvec(libmvec's exp performs slower, so we do not use it):
    • cos
    • sin
    • tan
    • acos
    • asin
    • atan
    • log
    • log2
    • cbrt
  • vectorize the following functions on aarch64 using optimized-routines:
    • cos
    • sin
    • log
    • exp
    • log10
    • atan
  • update doc?
Auxiliary
How to check libmvec is available?

image

Vector ABI

image
more details

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with dbms/src/Functions/FunctionsMath.h and dbms/cmake/find_vectorclass.cmake, then compare the linked ClickHouse implementation and existing source macros. Done means removing the vectorclass machinery, adding the configuration switch and architecture-specific vectorization described here, and adding unary math unit tests; documentation is updated if required.

Written by the indexing model from the issue text.

Assessment

Tech stack
cmake, cpp
Domain
build-system, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.