Click here to Skip to main content
16,015,969 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dear all,
any one can share a simple cuda sample program. It will be very helpful to me, since i am supposed to start the cuda study.

thanks & regards
_rps
Posted

You could try the manufacturers: http://www.nvidia.com/object/cuda_get_samples.html[^]
 
Share this answer
 
C#
__global__ void Addition(int *a,int *b,int *c)
{

   *c = *a + *b;
}
int main()
{
  int a,b,c;
  int *dev_a,*dev_b,*dev_c;
  int size = sizeof(int);



  cudaMalloc((void**)&dev_a, size);
  cudaMalloc((void**)&dev_b, size);
  cudaMalloc((void**)&dev_c, size);

  a=12,b=4;

  cudaMemcpy(dev_a, &a,sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(dev_b, &b,sizeof(int), cudaMemcpyHostToDevice);

  Addition<<< 1,1 >>>(dev_a,dev_b,dev_c);
  cudaMemcpy(&c, dev_c,size, cudaMemcpyDeviceToHost);
   //printf("%d + %d is %d\n",a,b,c);

   cudaFree(&dev_a);
   cudaFree(&dev_b);
   cudaFree(&dev_c);

   printf("%d\n", c);
   getch();
   return 0;


}



use this header files

C#
__global__ void Addition(int *a,int *b,int *c)
{

   *c = *a + *b;
}
int main()
{
  int a,b,c;
  int *dev_a,*dev_b,*dev_c;
  int size = sizeof(int);



  cudaMalloc((void**)&dev_a, size);
  cudaMalloc((void**)&dev_b, size);
  cudaMalloc((void**)&dev_c, size);

  a=12,b=4;

  cudaMemcpy(dev_a, &a,sizeof(int), cudaMemcpyHostToDevice);
  cudaMemcpy(dev_b, &b,sizeof(int), cudaMemcpyHostToDevice);

  Addition<<< 1,1 >>>(dev_a,dev_b,dev_c);
  cudaMemcpy(&c, dev_c,size, cudaMemcpyDeviceToHost);
   //printf("%d + %d is %d\n",a,b,c);

   cudaFree(&dev_a);
   cudaFree(&dev_b);
   cudaFree(&dev_c);

   printf("%d\n", c);
   getch();
   return 0;


}

use this header files
XML
#include <stdio.h>
#include <conio.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
 
Share this answer
 
v3
Comments
CHill60 8-May-14 4:21am    
> 2 years too late for the OP
Member 13371952 11-Oct-17 2:03am    
please give any other solutions in cuda

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900