CUDA实例练习(五):两数相加

 1 #include <stdio.h>
 2 #include <cuda_runtime.h>
 3 #include <device_launch_parameters.h>
 4 #include "book.h"
 5 #include "gputimer.h"
 6 
 7 __global__ void add(int a, int b, int *c){
 8     *c = a + b;
 9 }
10 
11 int main(void){
12     GpuTimer timer;
13     int out;
14 
15     int *d_in;//在GPU上定义指针并分配内存
16     HANDLE_ERROR(cudaMalloc((void **)&d_in, sizeof(int)));
17     
18     timer.Start();//计时
19     add << <1, 1 >> >(2, 7, d_in);//调用设备函数
20     timer.Stop();
21     //将GPU中计算出的结果返回给CPU
22     HANDLE_ERROR(cudaMemcpy(&out, d_in, sizeof(int), cudaMemcpyDeviceToHost));
23     printf("2 + 7 = %d.\n", out);
24     printf("add time is: %g ms.\n", timer.Elapsed());//打印计算时间
25 
26     cudaFree(d_in);//释放在GPU上分配的内存
27 
28     return 0;
29     
30 }

posted @ 2017-07-31 17:31  Jason&Hymer  阅读(342)  评论(0编辑  收藏  举报