kunzmi / managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.
Other
440 stars 79 forks source link

How do I convert the following code to managedCuda code #119

Open williamlzw opened 1 year ago

williamlzw commented 1 year ago
__global__ void image_crop(float* image_float, cudaTextureObject_t texture_object, int map_y0, int map_x0)
{
    int cropped_y = blockIdx.x;
    int cropped_x = threadIdx.x;
    int cropped_width = blockDim.x;

    float map_y = float(map_y0 + cropped_y);
    float map_x = float(map_x0 + cropped_x);

    float map_value = tex2D<float>(texture_object, map_x + 0.5, map_y + 0.5);
    image_float[cropped_y * cropped_width + cropped_x] = (map_value - 0.5) / 0.225;
}
kunzmi commented 1 year ago

You don't convert anything, it is the same code. The only thing that changes when porting a C++/Cuda application to ManagedCuda, is the host code that is calling your kernel as it is now written in C#, the kernel code itself remains untouched. Using standard Nvidia Cuda toolchain (nvcc) you compile the kernels to PTX or cubin, and then you load the kernels in C# host code. Even though the samples haven't been updated for a while, you still can use them as the basics didn't evolve over time. Have a look at VectorAdd for the host code and VectorAddKernel for the kernel code.