Emmanuel Hermellin
Emmanuel Hermellin

Reputation: 31

Write result into text file in CUDA/OptiX

I wanted to know if it was possible to write a text file of variables calculated in my program CUDA/OptiX. That's variables are in my .cu files and therefore can not be written by the CPU.

Upvotes: 2

Views: 4717

Answers (6)

Ingo Wald
Ingo Wald

Reputation: 92

No, you cannot open a file in your device code; you can do printf()'s to the console for debugging (in either optix or cuda, doesn't matter), but cannot open or write to files.

Probably the easiest solution is to create some CUDA managed memory (cudaMallocManaged()) that can be read and written on both host and device; write your variable values into that on the device, then afterward open a file and dump them on the host. This also works on both cuda and optix.

Upvotes: 0

jmcarter9t
jmcarter9t

Reputation: 823

I do not know a way to open a file descriptor and write to it directly from the device; however, I have used rtPrintf1 in OptiX to redirect the output of a program to a file. rtPrintf will allow you to print the contents of variables, etc, and if you prefix your prints with a keyword, you can grep for specific items easily. This strategy is a good way to debug early stage OptiX code and even trace what it is doing to some degree.

Upvotes: 0

faizan Siddiqui
faizan Siddiqui

Reputation: 61

in optix, you can transfer all your data back to host using buffer, once it is downloaded back to HOST memory you can easily convert the data in csv or txt format

Upvotes: 0

Alphajet
Alphajet

Reputation: 53

As the previous answers suggest, it's not possible to write data to a file through CUDA kernels. If your code involves multiple loops, you might be thinking how slow your program would be to transfer and write data on each loop; if so, you should make your data transfer after a given number of loops. In other words, write the file in chunks of multiple loops, not on every single loop.

Upvotes: 0

Dongie Agnir
Dongie Agnir

Reputation: 612

As far as I know, it's not possible to perform file I/O from a CUDA kernel. You would need to use cudaMemcpy and copy the data back to host memory, and from there you can write the values to a file.

Upvotes: 6

kroneml
kroneml

Reputation: 687

I have no experience with Optix, but as far as I know, there is no way to write to a file from CUDA. You should download your values to the host in order to store them to a file.

You can use cudaMemcpy( dstPointer, srdPointer, size, cudaMemcpyDeviceToHost); to copy data from the device (GPU) to the host (CPU). See: NVIDIA CUDA Library: cudaMemcpy Be aware that your dstPointer has to be the large enought to store the data!

Upvotes: 0

Related Questions