Math Function Vectorization
Nobody has claimed this yet.
- 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:
SLEEFis a promising candidate. However, there is no new release since 2020.SVMLis the most famous one. However, it is designed for Intel CPU and it is a proprietary product.LIBMVECis 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.cmakeand related macros in source. - add
unary_math_vectorization = falseto tiflash's configuration. - add unit tests for unary math functions.
- vectorized the following functions on
x86-64usinglibmvec(libmvec'sexpperforms slower, so we do not use it):cossintanacosasinatanloglog2cbrt
- vectorize the following functions on
aarch64usingoptimized-routines:cossinlogexplog10atan
- update doc?
Auxiliary
How to check libmvec is available?

Vector ABI
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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
