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进行计算时与CPU主要进行以下交互:

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


 

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

例子:

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

相关实践学习
在云上部署ChatGLM2-6B大模型(GPU版)
ChatGLM2-6B是由智谱AI及清华KEG实验室于2023年6月发布的中英双语对话开源大模型。通过本实验,可以学习如何配置AIGC开发环境,如何部署ChatGLM2-6B大模型。
相关文章
|
机器学习/深度学习 并行计算 图形学
CPU、GPU、TPU、NPU等到底是什么?
CPU、GPU、TPU、NPU等到底是什么?
5583 3
|
5月前
|
存储 JSON API
TOON:专为 LLM 设计的轻量级数据格式
TOON(Token-Oriented Object Notation)是一种专为降低LLM输入token消耗设计的数据格式。它通过省略JSON中冗余的括号、引号和重复键名,用类似CSV与YAML结合的方式表达结构化数据,显著减少token数量,适合向模型高效传参,但不替代JSON用于存储或复杂嵌套场景。
1136 2
TOON:专为 LLM 设计的轻量级数据格式
|
存储 缓存 异构计算
大语言模型量化方法对比:GPTQ、GGUF、AWQ
在过去的一年里,大型语言模型(llm)有了飞速的发展,在本文中,我们将探讨几种(量化)的方式,除此以外,还会介绍分片及不同的保存和压缩策略。
6519 0
|
7月前
|
人工智能 JSON 前端开发
Agentic AI崛起:九大核心技术定义未来人机交互模式​
本文系统梳理AI智能体架构设计的九大核心技术,涵盖智能体基础、多智能体协作、知识增强、模型优化、工具调用、协议标准化及人机交互等关键领域,助力构建高效、智能、协同的AI应用体系。建议点赞收藏,持续关注AI架构前沿技术。
1725 1
|
12月前
|
存储 人工智能 监控
一键部署 Dify + MCP Server,高效开发 AI 智能体应用
本文将着重介绍如何通过 SAE 快速搭建 Dify AI 研发平台,依托 Serverless 架构提供全托管、免运维的解决方案,高效开发 AI 智能体应用。
7017 65
|
SQL 存储 HIVE
鹰角基于 Flink + Paimon + Trino 构建湖仓一体化平台实践项目
鹰角基于 Flink + Paimon + Trino 构建湖仓一体化平台实践项目
909 2