MATLAB Answers

Why is the new "half" data type an opaque class?

7 views (last 30 days)
James Tursa
James Tursa on 25 Apr 2020
Commented: James Tursa on 6 May 2020
The good news:
The new half precision data type was introduced in R2019b. Graphics cards have been using this for quite some time, so this is a welcome addition to MATLAB.
The bad news:
Unfortunately, TMW has implemented half as an opaque class (like a classdef OOP object) instead of a simple numeric class like double or single.
Why is this bad?
With simple numeric classes like double or single, the data is not hidden. It is immediately accessable to a multitude of functions such as typecast( ), and is immediately available inside of mex routines. But with an opaque class, the data is hidden. Many of the MATLAB functions that work with simple numeric classes such as double and single will not work with the new half class. And you can't get at the data inside a mex routine at all, so forget about calling any library code (3rd party or yours) for any data manipulation from a mex routine. You don't see any mxHALF_CLASS or mxGetHalfs listed in the mex doc because they aren't there and getting pointers to your half data isn't supported.
@TMW: What was the point of hiding the half numeric data from the user? Why was half implemented this way? Is it too late to reverse this decision and turn half into a simple numeric class like double and single in future versions of MATLAB? The current opaque design hamstrings the user, particularly for conversing with GPU cards. At least provide mex read access to the data areas with mxGetHalfs and mxGetComplexHalfs routines.
E.g., even for simple stuff such as fwrite( ), you have to do funky workarounds:

  1 Comment

Daniel M
Daniel M on 26 Apr 2020
Interesting topic, looking forward to the discussion. Followed and voted!

Sign in to comment.

Answers (1)

Krishna Bindumadhavan
Krishna Bindumadhavan on 6 May 2020
Hi James,
Appreciate your feedback regarding treating half as a native type in MATLAB (similar to single/double) and support for half with MEX API's. We will consider this in future releases as adoption of half precision increases amongst the user base.
Regarding GPU compute with half datatype, we have recently added support for half in the GPU Coder toolbox in 20a for supported NVIDIA Cards (like Volta). See https://www.mathworks.com/help/gpucoder/examples/sobel-edge-detection-in-half-precision.html for an example.
In addition, deep learning code generation supports inference with fp16 compute (using mixed precision Tensor Cores) with the TensorRT library .
Hope this helps.
Best Regards,
Krishna.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by