请分享Cuda样本 [英] please share Cuda sample

查看:94
本文介绍了请分享Cuda样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

任何人都可以共享一个简单的cuda示例程序。这对我很有帮助,因为我应该开始cuda研究。



thanks&问候

_rps

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

推荐答案

您可以尝试制造商: http://www.nvidia.com/object/cuda_get_samples.html [ ^ ]
You could try the manufacturers: http://www.nvidia.com/object/cuda_get_samples.html[^]


__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

__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

#include <stdio.h>
#include <conio.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>


这篇关于请分享Cuda样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆