博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cuda中当元素个数超过线程个数时的处理案例
阅读量:4929 次
发布时间:2019-06-11

本文共 1966 字,大约阅读时间需要 6 分钟。

当向量元素超过线程个数时的情况

向量元素个数为(33 * 1024)/(128 * 128)=2.x倍

1 /* 2 * Copyright 1993-2010 NVIDIA Corporation.  All rights reserved. 3 * 4 * NVIDIA Corporation and its licensors retain all intellectual property and 5 * proprietary rights in and to this software and related documentation. 6 * Any use, reproduction, disclosure, or distribution of this software 7 * and related documentation without an express license agreement from 8 * NVIDIA Corporation is strictly prohibited. 9 *10 * Please refer to the applicable NVIDIA end user license agreement (EULA)11 * associated with this source code for terms and conditions that govern12 * your use of this NVIDIA software.13 *14 */15 16 17 #include "../common/book.h"18 #include "cuda.h"19 #include "cuda_runtime.h"20 #include "device_launch_parameters.h"21 22 #define N   (33 * 1024)23 24 __global__ void add(int *a, int *b, int *c) {25     int tid = threadIdx.x + blockIdx.x * blockDim.x;26     while (tid < N) {27         c[tid] = a[tid] + b[tid];28         tid += blockDim.x * gridDim.x;29     }30 }31 32 int main(void) {33     int *a, *b, *c;34     int *dev_a, *dev_b, *dev_c;35 36     // allocate the memory on the CPU37     a = (int*)malloc(N * sizeof(int));38     b = (int*)malloc(N * sizeof(int));39     c = (int*)malloc(N * sizeof(int));40 41     // allocate the memory on the GPU42     HANDLE_ERROR(cudaMalloc((void**)&dev_a, N * sizeof(int)));43     HANDLE_ERROR(cudaMalloc((void**)&dev_b, N * sizeof(int)));44     HANDLE_ERROR(cudaMalloc((void**)&dev_c, N * sizeof(int)));45 46     // fill the arrays 'a' and 'b' on the CPU47     for (int i = 0; i
> >(dev_a, dev_b, dev_c);63 64 // copy the array 'c' back from the GPU to the CPU65 HANDLE_ERROR(cudaMemcpy(c, dev_c, N * sizeof(int),66 cudaMemcpyDeviceToHost));67 68 // verify that the GPU did the work we requested69 bool success = true;70 for (int i = 0; i

 

转载于:https://www.cnblogs.com/liangliangdetianxia/p/3985040.html

你可能感兴趣的文章
前端那些事之React篇--helloword
查看>>
ios的google解析XML框架GDataXML的配置及使用
查看>>
netty-当一个客户端连接到来的时候发生了什么
查看>>
PHP_5.3.20 源码编译安装PHP-FPM
查看>>
在51CTO三年年+了,你也来晒晒
查看>>
js控制图片等比例缩放
查看>>
Java高级开发工程师面试考纲
查看>>
FreeMarker表达式
查看>>
Debian9.2 下使用vnstat查看服务器带宽流量统计
查看>>
NGINX + PHP-FPM 502
查看>>
mysql数据备份与恢复
查看>>
Openstack API常用命令
查看>>
OpenSSL漏洞凶猛来袭 慧眼恶意代码监测应对有方
查看>>
C语言 喝汽水问题
查看>>
LINUX中搭建DNS服务器,实现正向、反向以及访问不同DNS解析
查看>>
SCCM2012 R2实战系列之十:解决WDS服务无法启动问题(错误1067:进程意外终止)...
查看>>
ubuntu 下安装 mysql
查看>>
关于k-means聚类算法的matlab实现
查看>>
Git分支2
查看>>
一键安装Gitlab后的备份、迁移与恢复
查看>>