GPU Parallel Computing

GPU Parallel Computing

 GPU                                                                                                         

  GPU英文全称Graphic Processing Unit,中文翻译为“图形处理器”。GPU是相对于CPU的一个概念,由于在现代的计算机中(特别是家用系统,游戏的发烧友)图形的处理变得越来越重要,需要一个专门的图形的核心处理器。

  GPU有非常多的厂商都生产,和CPU一样,生产的厂商比较多,但大家熟悉的却只有3个,以至于大家以为GPU只有AMD、NVIDIA、Intel3个生产厂商。

nVidia GPU AMD GPU Intel MIC协处理器 nVidia Tegra 4 AMD ARM服务器

CUDA C/C++

CUDA fortran

OpenCL MIC OpenMP CUDA  

GPU 并行计算                                                                                              

  • 可以同CPU或主机进行协同处理
  • 拥有自己的内存
  • 可以同时开启1000个线程
  • 单精度:4.58TFlops 双精度 1.31TFlops

  GPU编程方面主要有一下方法:

GPU Parallel Computing


 

   采用GPU进行计算时与CPU主要进行以下交互:

  • CPU与GPU之间的数据交换
  • 在GPU上进行数据交换

GPU Parallel Computing


 

GPU编程–CUDA                                                                                       

CUDA C/C++: download CUDA drivers & compilers & samples (All In One Package ) free from:

    http://developer.nvidia.com/cuda/cuda-downloads

选择适合的版本~~~~我的下载的是5.0 notebook版本

具体安装方法:可参考这里http://blog.csdn.net/diyoosjtu/article/details/8454253

安装后,打开VS->新建,就会发现一个nVidia,里面有一个CUDA

  主要过程:

  • Hello World
    •   Basic syntax, compile & run
  • GPU memory management
    •   Malloc/free
    •   memcpy
  • Writing parallel kernels
    •    Threads & block
    •      Memory hierachy
复制代码
//hello_world.c:
#include <stdio.h>

void hello_world_kernel(){
    printf(“Hello World\n”);
}
int main(){    hello_world_kernel();}
Compile
& Run: gcc hello_world.c ./a.out
复制代码

CUDA:

复制代码
//hello_world.cu:
#include <stdio.h>
__global__ void hello_world_kernel(){
    printf(“Hello World\n”);
}

int main(){    hello_world_kernel<<<1,1>>>();}

Compile & Run:
nvcc hello_world.cu
./a.out
复制代码


 

GPU计算的主要过程:

  1. Allocate CPU memory for n integers
  2. Allocate GPU memory for n integers
  3. Initialize GPU memory to 0s
  4. Copy from CPU to GPU
  5. call the __global__function, compute   

    Keyword for CUDA kernel

  6. Copy from GPU to CPU
  7. Print the values
  8. free

主要函数:

复制代码
//Host (CPU) manages device (GPU) memory:
cudaMalloc (void ** pointer, size_t nbytes)
cudaMemset (void * pointer, int value, size_t count)
cudaFree (void* pointer)

int nbytes = 1024*sizeof(int);
int * d_a = 0;
cudaMalloc( (void**)&d_a,  nbytes );
cudaMemset( d_a, 0, nbytes);
cudaFree(d_a);

cudaMemcpy( void *dst,   void *src,   size_t nbytes, enum cudaMemcpyKind direction);
//returns after the copy is complete
/*blocks CPU thread until all bytes have been copied
doesn’t start copying until previous CUDA calls complete
enum cudaMemcpyKind
  cudaMemcpyHostToDevice
  cudaMemcpyDeviceToHost
  cudaMemcpyDeviceToDevice*/
复制代码

其中,<<<grid,block>>>

  • 2-level hierarchy: blocks and grid
    •   Block = a group of up to 1024 threads
    •   Grid = all blocks for a given kernel launch
    •   E.g. total 72 threads
      •      blockDim=12, gridDim=6
  • A block can:
    •   Synchronize their execution
    •   Communicate via shared memory
  • Size of grid and blocks are specified during kernel launch

例子:

GPU Parallel Computing
View Code

Thread index computation : 

  idx = blockIdx.x*blockDim.x + threadIdx.x:


 

应用                                                                                                         

High performance math routines for your applications:

  • cuFFT – Fast Fourier Transforms Library
  • cuBLAS – Complete BLAS Library
  • cuSPARSE – Sparse Matrix Library
  • cuRAND – Random Number Generation (RNG) Library
  • NPP – Performance Primitives for Image & Video Processing
  • Thrust – Templated C++ Parallel Algorithms & Data Structures
  • math.h – C99 floating-point Library
 
 

 

知识共享许可协议
本文 由 cococo点点 创作,采用 知识共享 署名-非商业性使用-相同方式共享 3.0 中国大陆 许可协议进行许可。欢迎转载,请注明出处:
转载自:cococo点点 http://www.cnblogs.com/coder2012

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109010.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号