http://webgpu.hwu.crhc.illinois.edu/

// MP 1
#include <wb.h> __global__ void vecAdd(float * in1, float * in2, float * out, int len) {
//@@ Insert code to implement vector addition here
int i = blockIdx.x * blockDim.x + threadIdx.x ;
if( i < len )
out[i] = in1[i] + in2[i] ;
} int main(int argc, char ** argv) {
wbArg_t args;
int inputLength;
float * hostInput1;
float * hostInput2;
float * hostOutput;
float * deviceInput1;
float * deviceInput2;
float * deviceOutput; args = wbArg_read(argc, argv); wbTime_start(Generic, "Importing data and creating memory on host");
hostInput1 = (float *) wbImport(wbArg_getInputFile(args, ), &inputLength);
hostInput2 = (float *) wbImport(wbArg_getInputFile(args, ), &inputLength);
hostOutput = (float *) malloc(inputLength * sizeof(float));
wbTime_stop(Generic, "Importing data and creating memory on host"); wbLog(TRACE, "The input length is ", inputLength); wbTime_start(GPU, "Allocating GPU memory.");
//@@ Allocate GPU memory here cudaMalloc((void**)&deviceInput1 , sizeof(float) * inputLength);
cudaMalloc((void**)&deviceInput2 , sizeof(float) * inputLength);
cudaMalloc((void**)&deviceOutput , sizeof(float) * inputLength); wbTime_stop(GPU, "Allocating GPU memory."); wbTime_start(GPU, "Copying input memory to the GPU.");
//@@ Copy memory to the GPU here
cudaMemcpy(deviceInput1,hostInput1,sizeof(float) * inputLength , cudaMemcpyHostToDevice) ;
cudaMemcpy(deviceInput2,hostInput2,sizeof(float) * inputLength , cudaMemcpyHostToDevice) ; wbTime_stop(GPU, "Copying input memory to the GPU."); //@@ Initialize the grid and block dimensions here dim3 DimGrid( (inputLength - )/ + , , ) ;
dim3 DimBlock( , , ) ; wbTime_start(Compute, "Performing CUDA computation");
//@@ Launch the GPU Kernel here vecAdd<<<DimGrid,DimBlock>>>(deviceInput1,deviceInput2,deviceOutput,inputLength); cudaThreadSynchronize();
wbTime_stop(Compute, "Performing CUDA computation"); wbTime_start(Copy, "Copying output memory to the CPU");
//@@ Copy the GPU memory back to the CPU here cudaMemcpy(hostOutput,deviceOutput,sizeof(float)*inputLength,cudaMemcpyDeviceToHost); wbTime_stop(Copy, "Copying output memory to the CPU"); wbTime_start(GPU, "Freeing GPU Memory");
//@@ Free the GPU memory here cudaFree(deviceInput1);
cudaFree(deviceInput2);
cudaFree(deviceOutput); wbTime_stop(GPU, "Freeing GPU Memory"); wbSolution(args, hostOutput, inputLength); free(hostInput1);
free(hostInput2);
free(hostOutput); return ;
}

cuda vector addition的更多相关文章

  1. CUDA Samples:Vector Add

    以下CUDA sample是分别用C++和CUDA实现的两向量相加操作,参考CUDA 8.0中的sample:C:\ProgramData\NVIDIA Corporation\CUDA Sample ...

  2. 6.2 CUDA streams

    stream是什么 nivdia给出的解释是:A sequence of operations that execute in issue-order on the GPU.  可以理解成在GPU上执 ...

  3. Vector Math for 3D Computer Graphics (Bradley Kjell 著)

    https://chortle.ccsu.edu/VectorLessons/index.html Chapter0 Points and Lines (已看) Chapter1 Vectors, P ...

  4. 【读书笔记】:MIT线性代数(2):Vector Spaces and Subspaces

    Vector Space: R1, R2, R3,R4 , .... Each space Rn consists of a whole collection of vectors. R5 conta ...

  5. 2.1CUDA-Thread

    在HOST端我们会分配block的dimension, grid的dimension.但是对应到实际的硬件是如何执行这些硬件的呢? 如下图: lanuch kernel 执行一个grid. 一个Gri ...

  6. 8.3 MPI

    MPI 模型 如图MPI的各个运算节点是分布式的.每一个节点可以视为是一个“Thread”,但这里的不同之处在于这些节点没有所谓的共享内存,或者说Global Memory.所以,在后面也会看到,一般 ...

  7. QuantStart量化交易文集

    Over the last seven years more than 200 quantitative finance articles have been written by members o ...

  8. Kubernetes 教程:在 Containerd 容器中使用 GPU

    原文链接:https://fuckcloudnative.io/posts/add-nvidia-gpu-support-to-k8s-with-containerd/ 前两天闹得沸沸扬扬的事件不知道 ...

  9. 现代3D图形编程学习-基础简介(1) (译)

    本书系列 现代3D图形编程学习 基础简介 并不像本书的其他章节,这章内容没有相关的源代码或是项目.本章,我们将讨论向量,图形渲染理论,以及OpenGL. 向量 在阅读这本书的时候,你需要熟悉代数和几何 ...

随机推荐

  1. Android4.4 Framework分析——Zygote进程的启动过程

    Android启动过程中的第一个进程init.在启动过程中会启动两个关键的系统服务进程ServiceManager和Zygote. 本文要介绍的就是Zygote进程的启动,Zygote俗称孵化器,专门 ...

  2. JavaScript 内存

    JavaScript 中对内存的一些了解 在使用JavaScript进行开发的过程中,了解JavaScript内存机制有助于开发人员能够清晰的认识到自己写的代码在执行的过程中发生过什么,也能够提高项目 ...

  3. Java EE (4) -- Java EE 6 Java Persistence API Developer Certified Expert(1z0-898)

    Overview of the Java Persistence API Describe the basics of Object Relational Mapping (ORM) Define t ...

  4. 重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom

    原文:重新想象 Windows 8 Store Apps (10) - 控件之 ScrollViewer 特性: Chaining, Rail, Inertia, Snap, Zoom [源码下载] ...

  5. C# Windows Phone App 开发,修改【锁定画面】,从【Assets】、【UI】、【网路图片】,并解决失灵问题。

    原文:C# Windows Phone App 开发,修改[锁定画面],从[Assets].[UI].[网路图片],并解决失灵问题. 一般我们在开发Windows Phone App,有时会希望透过应 ...

  6. Asp.net获取用户名和IP

    1. 在ASP.NET中专用属性:    获取服务器电脑名:Page.Server.ManchineName    获取用户信息:Page.User 获取客户端电脑名:Page.Request.Use ...

  7. Struts的ONGL

    1.什么是OGNL OGNL它是Object Graphic Navigation Language(对象图导航语言)缩写,    它是一个开源项目. Struts2框架使用OGNL作为默认的表达式语 ...

  8. WPF绘制党徽(立体效果,Cool)

    原文:WPF绘制党徽(立体效果,Cool) 前面用WPF方式绘制了党旗(WPF制作的党旗) ,去年3月份利用C# 及GDI+绘制过党徽,这次使用WPF来绘制党徽. ------------------ ...

  9. ODPS 下一个map / reduce 准备

    阿里接到一个电话说练习和比赛智能二选一, 真的很伤心, 练习之前积极老龄化的权利. 要总结ODPS下一个 写map / reduce 并进行购买预测过程. 首先这里的hadoop输入输出都是表的形式, ...

  10. 从头开始编写项目Makefile(八):型号规则

    [版权声明:转载请保留源:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com]     上创建的文件夹谈及.没有生产目标文件到相应的文件夹,在这里 ...