Skip to content

GPU: give Metal an IEEE-754 binary64 in software - #15866

Open
ktf wants to merge 1 commit into
AliceO2Group:devfrom
ktf:pr15866
Open

ktf wants to merge 1 commit into
AliceO2Group:devfrom
ktf:pr15866

Conversation

@ktf

@ktf ktf commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Metal Shading Language has no double, and the tracking needs one: the track
parametrisation, the propagator and the material budget all do double
arithmetic. The Metal entry point in dev already includes
GPUCommonDoubleBinary64.h; this adds it.

The class implements binary64 over a 64-bit integer. Round to nearest even,
with subnormals, infinities and NaNs, and NaN propagation in the ARM64 order,
so an Apple host is a bit-exact reference down to the payload. Addition,
subtraction, multiplication, division and the conversions to and from float
and the 32-bit integers are exact; sin and cos are fdlibm's and land within
2 ulp of libm. There is no fused multiply-add and no square root. The entry
point aliases the double keyword to the class, so the shared headers go on
saying double, and the header refuses to build where a real double exists.

Abs needs a Metal specialisation of its own, since fabs would
otherwise resolve to metal::fabs(float) through the implicit conversion and
round the mantissa away at 17 call sites, with no error and no warning, among
them the SMatrixGPU pivot selection. A static_assert keeps it that way, since
metal::fabs is not constant-evaluable. SinCosd goes the other way: nothing on
the device path needs it, so math_utils::sincosd is excluded for Metal as it
already is for OpenCL, and the stale GPUCommonDouble.h include goes with it.
The one remaining line is a ternary that needs an explicit value_t to stay
unambiguous once value_t is a class.

It costs of the order of a hundred times plain float on an M-series GPU, which
the tracking can afford because double is a small fraction of its floating
point work.

@alibuild

Copy link
Copy Markdown
Collaborator

Error while checking build/O2/fullCI_slc9 for 20a92b7 at 2026-09-29 21:01:

No log files found

Full log here.

@ktf

ktf commented Sep 30, 2026

Copy link
Copy Markdown
Member Author

@davidrohr any comments on this?

Comment thread DataFormats/Reconstruction/src/TrackParametrizationWithError.cxx Outdated
Comment thread DataFormats/Reconstruction/src/TrackParametrizationWithError.cxx Outdated
Comment thread GPU/Common/GPUCommonMath.h Outdated
Comment thread GPU/Common/GPUCommonDoubleBinary64.h
@ktf

ktf commented Sep 30, 2026

Copy link
Copy Markdown
Member Author

Ok, updated and cleaned up. notice the value_t cast is needed because the ternary will not work with two different types.

@ktf
ktf requested a review from davidrohr September 30, 2026 10:20
@alibuild

Copy link
Copy Markdown
Collaborator

Error while checking build/O2/fullCI_slc9 for 554ec70 at 2026-09-30 15:48:

No log files found

Full log here.

@ktf

ktf commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

@davidrohr any further objections?

@vkucera I see a bunch of spurious:

/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:115:21: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:166:17: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:174:22: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:179:25: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:187:21: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:222:25: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:227:30: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]

can you turn off the check, please? We routinely use VLAs for performance reason.

Comment thread GPU/Common/GPUCommonMath.h Outdated
Metal Shading Language has no double, and the tracking needs one: the track
parametrisation, the propagator and the material budget all do double
arithmetic. The Metal entry point in dev already includes
GPUCommonDoubleBinary64.h; this adds it.

The class implements binary64 over a 64-bit integer. Round to nearest even,
with subnormals, infinities and NaNs, and NaN propagation in the ARM64 order,
so an Apple host is a bit-exact reference down to the payload. Addition,
subtraction, multiplication, division and the conversions to and from float
and the 32-bit integers are exact; sin and cos are fdlibm's and land within
2 ulp of libm. There is no fused multiply-add and no square root. The entry
point aliases the double keyword to the class, so the shared headers go on
saying double, and the header refuses to build where a real double exists.

Abs<double> needs a Metal specialisation of its own, since fabs would
otherwise resolve to metal::fabs(float) through the implicit conversion and
round the mantissa away at 17 call sites, with no error and no warning, among
them the SMatrixGPU pivot selection. A static_assert keeps it that way, since
metal::fabs is not constant-evaluable. SinCosd goes the other way: nothing on
the device path needs it, so math_utils::sincosd is excluded for Metal as it
already is for OpenCL, and the stale GPUCommonDouble.h include goes with it.
The one remaining line is a ternary that needs an explicit value_t to stay
unambiguous once value_t is a class.

It costs of the order of a hundred times plain float on an M-series GPU, which
the tracking can afford because double is a small fraction of its floating
point work.
@vkucera

vkucera commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

@davidrohr any further objections?

@vkucera I see a bunch of spurious:

/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:115:21: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:166:17: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:174:22: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:179:25: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:187:21: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:222:25: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]
/sw/SOURCES/O2/slc9_aarch64-slc9_aarch64/0/GPU/Utils/SplineHelper.cxx:227:30: error: variable length arrays in C++ are a Clang extension [clang-diagnostic-vla-cxx-extension]

can you turn off the check, please? We routinely use VLAs for performance reason.

It's already off.

https://github.com/alisw/alidist/blob/dea51bc9ac4b9322d5377d31df8837964610aa69/o2checkcode.sh#L67

Isn't this the misbehaviour investigated by @sawenzel ?

@alibuild

alibuild commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator

Error while checking build/O2/fullCI_slc9 for dd0671c at 2026-10-01 11:17:

No log files found

Full log here.

@ktf

ktf commented Oct 1, 2026

Copy link
Copy Markdown
Member Author

@davidrohr ok, updated.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants