How to separate CUDA code into multiple files

后端 未结 4 846
南笙
南笙 2020-12-13 07:25

I am trying separate a CUDA program into two separate .cu files in effort to edge closer to writing a real app in C++. I have a simple little program that:

Allocate

4条回答
  •  醉梦人生
    2020-12-13 08:17

    If you look at the CUDA SDK code examples, they have extern C defines that reference functions compiled from .cu files. This way, the .cu files are compiled by nvcc and only linked into the main program while the .cpp files are compiled normally.

    For example, in marchingCubes_kernel.cu has the function body:

    extern "C" void
    launch_classifyVoxel( dim3 grid, dim3 threads, uint* voxelVerts, uint *voxelOccupied, uchar *volume,
                          uint3 gridSize, uint3 gridSizeShift, uint3 gridSizeMask, uint numVoxels,
                          float3 voxelSize, float isoValue)
    {
        // calculate number of vertices need per voxel
        classifyVoxel<<>>(voxelVerts, voxelOccupied, volume, 
                                         gridSize, gridSizeShift, gridSizeMask, 
                                         numVoxels, voxelSize, isoValue);
        cutilCheckMsg("classifyVoxel failed");
    }
    

    While in marchingCubes.cpp (where main() resides) just has a definition:

    extern "C" void
    launch_classifyVoxel( dim3 grid, dim3 threads, uint* voxelVerts, uint *voxelOccupied, uchar *volume,
                          uint3 gridSize, uint3 gridSizeShift, uint3 gridSizeMask, uint numVoxels,
                          float3 voxelSize, float isoValue);
    

    You can put these in a .h file too.

提交回复
热议问题