OptiX (various versions 7.0+) SDK examples float_as_int errors at runtime

I noticed that some of the examples in the SDK, even though they seem to build fine, run into compiling issues when those examples are run (optixHair, optixTriangle, et al). The error message says that float_as_int is not found. This occurs in the runtime compiling. I fixed this in my source code trees by adding the following snippet at the end of SDK/cuda/helpers.h, and then (depending on the particular OptiX SDK version, making sure that file is included in any .cu files that give errors. I also had to create float_as_uint and uint_as_float functions as well, not shown here, but easy to create by just copying the below and change “int” to uint"

===============================
forceinline device int float_as_int(const float f)
{
union {
float f;
int i;
} v1;

v1.f = f;
return v1.i;
}

forceinline device float int_as_float(int i)
{
union {
float f;
int i;
} v1;

v1.i = i;
return v1.f;
}

============================
There may be a better way to do this as there are supposedly built-in cuda functions but this did the trick for me for now. I verified this approach worked in 7.2, 7.3, 7.4. There seems to be other issues for me at least in 7.7 and 8.0, that’s for another day. My goal was to get OptiX anything to work.

Which CUDA toolkit are you using? And can you list a specific 7.x SDK version that has the issue?

I just tried building 7.5 and 8.0 fresh on Linux and I don’t get any errors with float_as_int.

The function call float_as_int() is deprecated as of CUDA 11.5. Please use the intrinsic version with a double-underscore prefix from now on: __float_as_int().

https://docs.nvidia.com/cuda/archive/11.7.0/cuda-toolkit-release-notes/index.html#math-11.5.0

–
David.

NVIDIA-SMI 535.154.05 Driver Version: 535.154.05 CUDA Version: 12.2 (nvidia-smi output)

Ubuntu 22.04

NVIDIA GeForce RTX 4090

Ah, OK! That is the issue
but I am not writing the code, this is the examples in the various OptiX SDKs

apt list --installed | grep cuda-tool

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

cuda-toolkit-12-3-config-common/unknown,now 12.3.101-1 all [installed,automatic]
cuda-toolkit-12-3/unknown,now 12.3.2-1 amd64 [installed]
cuda-toolkit-12-config-common/unknown,now 12.3.101-1 all [installed,automatic]
cuda-toolkit-config-common/unknown,now 12.3.101-1 all [installed,automatic]
cuda-tools-12-3/unknown,now 12.3.2-1 amd64 [installed,automatic]

As an additional note: here is what happens for me on OptiX SDK v 8.0.0. It builds, but when I try to run optixHello, I get:
chris@chris-MS-7C56:~/NVIDIA_Stuff/NVIDIA-OptiX-SDK-8.0.0-linux64-x86_64/SDK/build$ bin/optixHello
[ 4][ KNOBS]: All knobs on default.

[ 4][ DISKCACHE]: Opened database: “/var/tmp/OptixCache_chris/optix7cache.db”
[ 4][ DISKCACHE]: Cache data size: “3.9 MiB”
[ 4][ DISKCACHE]: Cache miss for key: ptx-6584-keyb5ef4d3eaa9b124ce715726298ffb46b-sm_89-rtc1-drv535.154.05

minor Version (60) newer than tool (should be 59)
minor NvvmIRVersion (63) newer than tool (should be 58)
[ 2][ COMPILER]: COMPILE ERROR:

Caught exception: OPTIX_ERROR_INVALID_INPUT: Optix call ‘optixModuleCreate( context, &module_compile_options, &pipeline_compile_options, input, inputSize, LOG, &LOG_SIZE, &module )’ failed: /home/chris/NVIDIA_Stuff/NVIDIA-OptiX-SDK-8.0.0-linux64-x86_64/SDK/optixHello/optixHello.cpp:150)
Log:
COMPILE ERROR:

And for Optix SDK v7.7.0 ditto:
$ bin/optixHello
[ 4][ KNOBS]: All knobs on default.

[ 4][ DISKCACHE]: Opened database: “/var/tmp/OptixCache_chris/optix7cache.db”
[ 4][ DISKCACHE]: Cache data size: “3.9 MiB”
[ 4][ DISKCACHE]: Cache miss for key: ptx-6584-keyf5560cb0a929fdf6e352e689151a88c8-sm_89-rtc1-drv535.154.05

minor Version (60) newer than tool (should be 59)
minor NvvmIRVersion (63) newer than tool (should be 58)
[ 2][ COMPILER]: COMPILE ERROR:

Caught exception: OPTIX_ERROR_INVALID_INPUT: Optix call ‘optixModuleCreate( context, &module_compile_options, &pipeline_compile_options, input, inputSize, LOG, &LOG_SIZE, &module )’ failed: /home/chris/NVIDIA_Stuff/NVIDIA-OptiX-SDK-7.7.0-linux64-x86_64/SDK/optixHello/optixHello.cpp:150)
Log:
COMPILE ERROR:

$ apt list --installed | grep cuda-tool

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

cuda-toolkit-12-3-config-common/unknown,now 12.3.101-1 all [installed,automatic]
cuda-toolkit-12-3/unknown,now 12.3.2-1 amd64 [installed]
cuda-toolkit-12-config-common/unknown,now 12.3.101-1 all [installed,automatic]
cuda-toolkit-config-common/unknown,now 12.3.101-1 all [installed,automatic]
cuda-tools-12-3/unknown,now 12.3.2-1 amd64 [installed,automatic]
nvidia-cuda-toolkit-doc/jammy,jammy,now 11.5.1-1ubuntu1 all [installed,automatic]
nvidia-cuda-toolkit/jammy,now 11.5.1-1ubuntu1 amd64 [installed]

This thread might be helpful: https://forums.developer.nvidia.com/t/optix-8-module-creation-version-mismatch/272885

You may need a newer driver with CUDA 12.3. There are version 545 and 550 drivers for Linux available via the “Beta and Older drivers” link from the Drivers download page.

–
David.

BTW, for posterity, another option is to use the most recent CUDA toolkit that is mentioned in the OptiX SDK release for whichever version of OptiX you want to use. For OptiX 8.0, the release notes mention CUDA 12.0. It’s usually fine to use the latest toolkit, but our general recommendation, if there are any issues, is to pick the last toolkit version that predates the OptiX release, just to be safe.

–
David.

Thank you! That is very helpful! I did get rid of the “cuda-toolkit” packages and now just have the nvidia-cuda-toolkit one. After that, and cleaning up the tree, I was able to build and run Optix SDK v7.7.0 and v8.0.0. Your fast and useful help is much appreciated!

1 Like

As a sidenote, all intersection attribute and optixTrace payload registers inside the OptiX device code are unsigned int, so for these the proper CUDA reinterpretation functions are __float_as_uint() and __uint_as_float() .

The OptiX SDK 8.0.0 examples’ source code corrected most of these occurrences. (Well, optixCustomPrimitive.cu still uses the integer versions.)
That’s not really a functional change since these are all just 32bit bitcasts in the end, but for code cleanliness better use the proper types.

Also strictly speaking, the functions float_as_int() and int_as_float() in the first post with the union are not correct C++ code. It’s undefined behavior to read a different type from a union than was most recently written to, but most compilers do exactly what that code intended and I used such method as well.
See the Explanation section here: https://en.cppreference.com/w/cpp/language/union