项目打包下载

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

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

 /*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property and
* proprietary rights in and to this software and related documentation.
* Any use, reproduction, disclosure, or distribution of this software
* and related documentation without an express license agreement from
* NVIDIA Corporation is strictly prohibited.
*
* Please refer to the applicable NVIDIA end user license agreement (EULA)
* associated with this source code for terms and conditions that govern
* your use of this NVIDIA software.
*
*/ #include "../common/book.h"
#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h" #define N (33 * 1024) __global__ void add(int *a, int *b, int *c) {
int tid = threadIdx.x + blockIdx.x * blockDim.x;
while (tid < N) {
c[tid] = a[tid] + b[tid];
tid += blockDim.x * gridDim.x;
}
} int main(void) {
int *a, *b, *c;
int *dev_a, *dev_b, *dev_c; // allocate the memory on the CPU
a = (int*)malloc(N * sizeof(int));
b = (int*)malloc(N * sizeof(int));
c = (int*)malloc(N * sizeof(int)); // allocate the memory on the GPU
HANDLE_ERROR(cudaMalloc((void**)&dev_a, N * sizeof(int)));
HANDLE_ERROR(cudaMalloc((void**)&dev_b, N * sizeof(int)));
HANDLE_ERROR(cudaMalloc((void**)&dev_c, N * sizeof(int))); // fill the arrays 'a' and 'b' on the CPU
for (int i = ; i<N; i++) {
a[i] = i;
b[i] = * i;
} // copy the arrays 'a' and 'b' to the GPU
HANDLE_ERROR(cudaMemcpy(dev_a, a, N * sizeof(int),
cudaMemcpyHostToDevice));
HANDLE_ERROR(cudaMemcpy(dev_b, b, N * sizeof(int),
cudaMemcpyHostToDevice)); /*
当向量元素超过线程个数时的情况
向量元素个数为(33 * 1024)/(128 * 128)=2.x倍
*/
add << <, >> >(dev_a, dev_b, dev_c); // copy the array 'c' back from the GPU to the CPU
HANDLE_ERROR(cudaMemcpy(c, dev_c, N * sizeof(int),
cudaMemcpyDeviceToHost)); // verify that the GPU did the work we requested
bool success = true;
for (int i = ; i<N; i++) {
if ((a[i] + b[i]) != c[i]) {
printf("Error: %d + %d != %d\n", a[i], b[i], c[i]);
success = false;
}
}
if (success) printf("We did it!\n"); // free the memory we allocated on the GPU
HANDLE_ERROR(cudaFree(dev_a));
HANDLE_ERROR(cudaFree(dev_b));
HANDLE_ERROR(cudaFree(dev_c)); // free the memory we allocated on the CPU
free(a);
free(b);
free(c); return ;
}

cuda中当元素个数超过线程个数时的处理案例的更多相关文章

  1. cuda中当数组数大于线程数的处理方法

    参考stackoverflow一篇帖子的处理方法:https://stackoverflow.com/questions/26913683/different-way-to-index-threads ...

  2. 统计js数组中奇数元素的个数

    如何统计一个JS数组中奇数元素的个数呢? 这是群友提出的一个问题,大部分群友给出的是遍历 然后对2取模,得到最终结果. 这样的写法是最容易想得到的,那么有没有其他思路呢? 这里我提供另外一种思路,我们 ...

  3. [JavaScript] 获取数组中相同元素的个数

    /** * 获取数组中相同元素的个数 * @param val 相同的元素 * @param arr 传入数组 */ function getSameNum(val,arr){ processArr ...

  4. matlab求一个矩阵中各元素出现的个数(归一化)

    function [m,n] = stamatrix(a) %网上找到的方法,感觉很巧妙 x=a(:); x=sort(x); d=diff([x;max(x)+1]); count = diff(f ...

  5. javaScript获取文档中所有元素节点的个数

    HTML+JS 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  6. 查看java中的线程个数名称

    查看java中的线程个数名称 package com.stono.thread2; import java.lang.management.ManagementFactory; import java ...

  7. 海量数据处理 - 10亿个数中找出最大的10000个数(top K问题)

    前两天面试3面学长问我的这个问题(想说TEG的3个面试学长都是好和蔼,希望能完成最后一面,各方面原因造成我无比想去鹅场的心已经按捺不住了),这个问题还是建立最小堆比较好一些. 先拿10000个数建堆, ...

  8. 求二叉树中第K层结点的个数

    一,问题描述 构建一棵二叉树(不一定是二叉查找树),求出该二叉树中第K层中的结点个数(根结点为第0层) 二,二叉树的构建 定义一个BinaryTree类来表示二叉树,二叉树BinaryTree 又是由 ...

  9. Linux查看进程线程个数

    1.根据进程号进行查询: # pstree -p 进程号 # top -Hp 进程号 2.根据进程名字进行查询: # pstree -p `ps -e | grep server | awk '{pr ...

随机推荐

  1. ios json转model的简单现实

    在android开发中,可用第三方的转换库如gson等.当然在ios也有一些库如MJExtensiond等.在这里,我简单实现一下. 一.先建一个model并且继承NSObject,代码如下: cla ...

  2. 今天来记录一下关于ajax跨域的一些问题。以备不时之需。

    今天来记录一下关于ajax跨域的一些问题.以备不时之需. 跨域 同源策略限制 同源策略阻止从一个域上加载的脚本获取或操作另一个域上的文档属性.也就是说,受到请求的 URL 的域必须与当前 Web 页面 ...

  3. 系统:Centos 7.2 内核3.10.0-327.el7.x86_64 # 内核需要高于2.6.32

    系统:Centos 7.2 内核3.10.0-327.el7.x86_64 # 内核需要高于2.6.32 Drbd : 192.168.8.111:node1/dev/drdb0 /mydeta 19 ...

  4. Tomcat底层通过全类名创建对象的实现

    示例: //com.neuedu.baier.entity.User为User类的全类名 //要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段 Class<?> user ...

  5. PAT天梯赛L3-005 垃圾箱分布

    题目链接:点击打开链接 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住.所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方,同时还要保证每个居民点都在距离它一个不太远的 ...

  6. 操作iframe的一些方法

    //父页面操作iframe里的内容 oInput.onclick=function(){ var oBox = oIframe.contentWindow.document.getElementByI ...

  7. 1、python简单介绍

    写在前面:曾经与java擦肩而过,现在懊悔很深,希望自己通过学习python,熟练掌握python,来弥补曾经的愚蠢.python简单介绍 python 1989年年底诞生,截止2017年,已经是IT ...

  8. 项目 06 Bootstrap

    项目班 06 Bootstrap 一.介绍 #基于HTML,CSS,JS的简洁灵活的流行前端框架及交互组件集 #为快速WEB开发提供了一套前端工具包,包括布局.网格.表格.按钮.表单.导航.提示等等 ...

  9. Nim && Grundy (基础博弈游戏 )

    通常的Nim游戏的定义是这样的:有若干堆石子,每堆石子的数量都是有限的,合法的移动是“选择一堆石子并拿走若干颗(不能不拿)”,如果轮到某个人时所有的石子堆都已经被拿空了,则判负(因为他此刻没有任何合法 ...

  10. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...