convert a c program to a cuda program

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

convert a c program to a cuda program

opencv-users mailing list
hello evryone , that's an emmergency is there any way to convert a c program to a cuda program.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: convert a c program to a cuda program

keghn

/*
Pure Cuda Programming in C.
https://github.com/parallel-forall/cudacasts/blob/master/ep2-first-cuda-c-program/kernel.cu
http://devblogs.nvidia.com/parallelforall/cudacasts-episode-2-your-first-cuda-c-program/


Compile Using the NVIDIA C Compiler:

nvcc -o vectoradd answer.cu

  Or:
nvcc -o pro pro.cu

 Run it:

./pro

*/



#include <stdio.h>

#define SIZE 1024

__global__ void VectorAdd(int *a, int *b, int *c, int n)
{
        int i = threadIdx.x;

        if (i < n)
                c[i] = a[i] + b[i];
}

int main()
{
        int *a, *b, *c;
        int *d_a, *d_b, *d_c;

        a = (int *)malloc(SIZE*sizeof(int));
        b = (int *)malloc(SIZE*sizeof(int));
        c = (int *)malloc(SIZE*sizeof(int));

        cudaMalloc( &d_a, SIZE*sizeof(int));
        cudaMalloc( &d_b, SIZE*sizeof(int));
        cudaMalloc( &d_c, SIZE*sizeof(int));

        for( int i = 0; i < SIZE; ++i )
        {
                a[i] = i;
                b[i] = i;
                c[i] = 0;
        }

        cudaMemcpy( d_a, a, SIZE*sizeof(int), cudaMemcpyHostToDevice );
        cudaMemcpy( d_b, b, SIZE*sizeof(int), cudaMemcpyHostToDevice );
        cudaMemcpy( d_c, c, SIZE*sizeof(int), cudaMemcpyHostToDevice );

        VectorAdd<<< 1, SIZE >>>(d_a, d_b, d_c, SIZE);
       
        cudaMemcpy( c, d_c, SIZE*sizeof(int), cudaMemcpyDeviceToHost );

        for( int i = 0; i < 10; ++i)
                printf("c[%d] = %d\n", i, c[i]);

        free(a);
        free(b);
        free(c);

        cudaFree(d_a);
        cudaFree(d_b);
        cudaFree(d_c);

        return 0;
}
Loading...