Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Half to Single in .NET with hardware acceleration?

I have a .NET app where the half precision type is an excellent fit. There is an "old" C# project to handle this data type. However, Intel CPUs offer now hardware acceleration from half to single conversion. Does anyone know how to leverage such hardware acceleration in .NET?

like image 247
Joannes Vermorel Avatar asked Oct 19 '22 21:10

Joannes Vermorel


1 Answers

To be able to use the built in support from the CPU for float16 in C#, you would have to use an unmanaged assembly (i.e. C/C++ dll) to handle either the data type itself and/or the algorithms that depend on the float16 type. Calling into unmanaged code is easy enough using PInvoke, but unless you have the Intel Compiler, you would have to code parts of the unmanaged library in assembly code because MSVC don't directly support the pragmas necessary for the compiler to generate the x86-64 assembly code required for the float16 type.

Assuming you do all this, you might still be disappointed with the result since preparing your data structures and calling into unmanaged code can have significant CPU overhead which might counter the performance you would gain from float16.

like image 157
syazdani Avatar answered Nov 01 '22 08:11

syazdani