NVIDIA CUDA 学习 (2) CUDA Parallel Programming

这篇博客介绍了NVIDIA CUDA的并行编程,从CPU并发编程到GPU并发编程的转换。内容包括尖括号的含义,解释了并行块的个数以及块索引的概念。同时,文章提到了CUDA中__global__和__device__的区别,确保代码只在GPU上运行。
摘要由CSDN通过智能技术生成

CPU并发编程

在这里插入图片描述

#include "../common/book.h"
#define N 10
void add( int *a, int *b, int *c ) {
   
	int tid = 0; // this is CPU zero, so we start at zero
	while (tid < N) {
   
		c[tid] = a[tid] + b[tid];
		tid += 1; // we have one CPU, so we increment by one
	}
}
int main( void ) {
   
	int a[N], b[N], c[N];
	// fill the arrays 'a' and 'b' on the CPU
	for (int i=0; i<N; i++) {
   
		a[i] = -i;
		b[i] = i * i;
	}
	add( a, b, c );
	// display the results
	for (int i=0; i<N; i++) {
   
		printf( "%d + %d = %d\n", a[i], b[i], c[i] );
	}
	return 0;
}

在这里插入图片描述

GPU并发编程

#include "../common/book.h"
#define N 10
int main( void ) {
   
	int a[N], b[N], c[N];
	int *dev_a, *dev_b, *dev_c;
	// allocate the memory on the GPU
	HANDLE_ERROR( cudaMalloc( (void**)&dev_a, N * 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值