How to use OpenGL funtions in CUDA

Hi all… I am new to CUDA. I tried to use the OpenGL functions in CUDA Device function. Its giving error

calling a host function from device/global function is not allowed.

I tried to compile the program with nvcc program.cu -arch=sm_20 . Then also its showing same error. Can any one please give a sample code describing how to use OpenGL functions in CUDA ?

You can’t do it.

oh…Thanks for reply.

I am working on Terrain Rendering project. This is my requirement:

  1. I allocated memory dynamically using malloc() in device function. (This is required for making a quadtree structure. We dont know how much memory it will take as if we try to allocate using CUDAMalloc() before calling global function ). When i am using that in host function its giving segmentation fault . But I have to use that data for rendering. Thats why i asked how to use OpenGL functions in CUDA for rendering purpose.

  2. Is there any way to use that dynamically allocated memory back in host ? Or can U please tell any other way to proceed ?

Hi avidday… Frankly, I am waiting for U’r reply. I have the CPU running code of my project. I have to convert that into CUDA and have to parallelize that. At first, we are trying for converting that into CUDA which runs on single thread (like CPU main thread). We are unable to do this only, because we are new to CUDA.

Can you please suggest any way to proceed ?

Sorry, no I can’t - rendering is something I know nothing about.

I don’t have any experience with rendering either, but you may want to read section 3.2.8 “Graphics Interoperability” of the Programming Guide. The Programming Guide also is a good read in general and essential for any project.

For the data structure, I would recommend to build it on the CPU and then copy it to the device in one go. It’s best to avoid building complicated data structures on the device because (i) arrays often give better memory access patterns compared to data structures using pointers, and (ii) the locking necessary to protect the integrity of the data structure when building it in parallel is difficult to achieve on the device.

Ok… We will try it. Please clarify this doubt.

I allocated memory dynamically using malloc() in device function. Can we reuse that back in host function ? Is there any way ?

No there is not. Device pointers can’t be dereferenced in host memory. It sounds like you might be better off building the quadtree using indexing into linear memory, rather pointers. Indices and linear memory are much more portable than pointers and structures assembled from many nested dynamic memory allocations.