当前位置:网站首页>CUDA编程(一):实现两个数组相加
CUDA编程(一):实现两个数组相加
2022-06-10 16:53:00 【武乐乐~】
前言
由于最近忙着秋招,本系列博客最近只记录下自己学CUDA的例子,不会做出注释,后期有空会补上基础知识和代码详解。
#include <stdio.h>
const double a = 1.23;
const double b = 2.34;
void __global__ add(const double *x, const double *y, const double *z,const int N );
void __global__ add(const double *x, const double *y, double *z, const int N)
{
const int tid = blockDim.x * blockIdx.x + threadIdx.x;
if(tid < N)
{
z[tid] = x[tid] + y[tid];
}
}
int main()
{
const int N = 1000;
const int M = sizeof(double) * N;
double *ha = (double *)malloc(M);
double *hb = (double *)malloc(M);
double *hc = (double *)malloc(M);
// assignment
for(int i=0; i < N; ++i)
{
ha[i] = a;
hb[i] = b;
}
//
double *da, *db, *dc;
cudaMalloc((void**)&da, M);
cudaMalloc((void**)&db, M);
cudaMalloc((void**)&dc, M);
cudaMemcpy(da,ha,M,cudaMemcpyHostToDevice);
cudaMemcpy(db,hb,M,cudaMemcpyHostToDevice);
// kernel fun
const int block_size = 128;
const int grid_size = (N + block_size -1)/ block_size;
add<<<grid_size,block_size>>>(da,db,dc,N);
cudaMemcpy(hc,dc,M,cudaMemcpyDeviceToHost);
free(ha);
free(hb);
free(hc);
cudaFree(da);
cudaFree(db);
cudaFree(dc);
return 0;
}
编译指令
nvcc -arch=sm_75 add.cu -o add
边栏推荐
- 当v-if和v-for需要同时使用的时候
- 开源项目 PM 浅谈如何设计官网
- What should be done to improve the service level of the park and optimize the business environment
- 2022G1工业锅炉司炉考题及在线模拟考试
- 5年后,你将如何融入20万亿美元的「项目经济」
- Fabric.js 缩放画布
- Station B doesn't want to be a "conscience aiyouteng"
- Mapbox GL development tutorial (11): loading line layers
- Why 0.1+0.2=0.3000000000000004
- Fabric. JS activation input box
猜你喜欢

品牌难立,IPO难行,中国茶企困于“传统”?

mapbox-gl开发教程(十一):加载线图层

2022年G2电站锅炉司炉考试模拟100题及模拟考试

2022 version of idea graphical interface GUI garbled code solution super detailed simple version

2022年茶艺师(中级)操作证考试题库及模拟考试

自定义视图:图形与图像的处理(一):使用简单图片

It has become a unicorn since its establishment one year ago. Tencent didi is the "backer". This year's new unicorn is not simple

What should be done to improve the service level of the park and optimize the business environment

vscode常用快捷键

Brands are difficult to establish, IPO is difficult, and Chinese tea enterprises are trapped in "tradition"?
随机推荐
PrimeKG : 构建知识图谱以实现精确医疗
基于Feign远程调用
Nacos注册中心
2022年G2电站锅炉司炉考试模拟100题及模拟考试
Fabric. JS zoom canvas
KDD 2021 | MoCl: comparative learning of molecular graphs using multi-level domain knowledge
域名备案和icp备案有哪些区别?
二十多年了,安全套市场还只有杜蕾斯、冈本、杰士邦
安全感
Thread interview related questions
Daily question -1287 Elements that appear more than 25% in an ordered array
厉害了,工信部推出 “一键解绑” 手机号绑定的互联网账号,堪称神器
How MySQL modifies field type and field length
Mapbox GL development tutorial (11): loading line layers
软件项目管理 6.10.成本预算
OpenJudge NOI 1.13 15:求序列中的众数
Leetcode String to integer(Atoi)
pands pd. Detailed parsing of dataframe() function
蓝桥杯_挑选子串_组合数学_乘法原理_ / 尺取法
Leetcode String to integer(Atoi)