【AI实战】CUDA 编程入门及开源项目代码分享

【AI实战】CUDA 编程入门

  • CUDA 简介
  • 官方文档
  • 简单的代码
  • 开源学习的代码
  • 参考

【AI实战】CUDA 编程入门及开源项目代码分享_第1张图片

CUDA 简介

CUDA(Compute Unified Device Architecture),是显卡厂商NVIDIA推出的运算平台。

CUDA™是一种由NVIDIA推出的通用并行计算架构, 该架构使GPU能够解决复杂的计算问题。 它包含了 CUDA指令集架构(ISA)以及GPU内部的并行计算 引擎。

开发人员可以使用C语言来为CUDA™架构编 写程序,所编写出的程序可以在支持CUDA™的处理 器上以超高性能运行。(来源:百度百科)

随着显卡的发展,GPU越来越强大,而且GPU为显示图像做了优化。在计算上已经超越了通用的CPU。

如此强大的芯片如果只是作为显卡就太浪费了,因此NVIDIA推出CUDA,让显卡可以用于图像计算以外的目的。

CUDA(Compute Unified Device Architecture)是一个新的基础架构,这个架构可以使用GPU来解决商业、工业以及科学方面的复杂计算问题。

CUDA的SDK中的编译器和开发平台支持Windows、Linux系统,可以与Visual Studio2005,2008,2010集成在一起。

官方文档

  • NVIDIA CUDA C++ Programming Guide
    地址:https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html

  • CUDA C++ Best Practices Guide

    地址:https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/index.html

  • CUDA C编程权威指南

    英文原版叫《Professional CUDA C Programming》
    pdf文件地址:http://www.hds.bme.hu/~fhegedus/C++/Professional%20CUDA%20C%20Programming.pdf

简单的代码

将大小为N的两个向量 A 和 B 相加,并将结果存储到向量 C 中:

// Kernel definition
__global__ void VecAdd(float* A, float* B, float* C)
{
    int i = threadIdx.x;
    C[i] = A[i] + B[i];
}

int main()
{
    ...
    // Kernel invocation with N threads
    VecAdd<<<1, N>>>(A, B, C);
    ...
}

以下代码将大小为 N x N 的两个矩阵 A 和 B 相加,并将结果存储到矩阵 C 中:

// Kernel definition
__global__ void MatAdd(float A[N][N], float B[N][N],
                       float C[N][N])
{
    int i = threadIdx.x;
    int j = threadIdx.y;
    C[i][j] = A[i][j] + B[i][j];
}

int main()
{
    ...
    // Kernel invocation with one block of N * N * 1 threads
    int numBlocks = 1;
    dim3 threadsPerBlock(N, N);
    MatAdd<<<numBlocks, threadsPerBlock>>>(A, B, C);
    ...
}

开源学习的代码

这里分享一些开源项目,以便更好的学习。

根据各自的需求阅读对应的源代码。

能自己动手写就动手写,多写才是王道。

  • FasterTransformer
    FasterTransformer:https://github.com/NVIDIA/DeepLearningExamples/tree/master/FasterTransformer

    这是英伟达开源的Transformer推理加速引擎。

  • TurboTransformers

    TurboTransformers:https://github.com/Tencent/TurboTransformers

    这是腾讯开源的Transformer推理加速引擎。

  • DeepSpeed

    DeepSpeed: https://github.com/microsoft/DeepSpeed

    这是微软开源的深度学习分布式训练加速引擎

  • cuda-samples

    https://github.com/NVIDIA/cuda-samples

  • CUDALibrarySamples

    https://github.com/NVIDIA/CUDALibrarySamples

  • CUDA-Programming

    https://github.com/brucefan1983/CUDA-Programming

  • cuda-convnet2

    https://github.com/akrizhevsky/cuda-convnet2

  • tiny-cuda-nn

    https://github.com/NVlabs/tiny-cuda-nn

  • tsne-cuda

    https://github.com/CannyLab/tsne-cuda

  • CUDA_Freshman

    https://github.com/Tony-Tan/CUDA_Freshman

  • lightseq

    LightSeq:https://github.com/bytedance/lightseq

参考

  • https://docs.nvidia.com/cuda

你可能感兴趣的:(人工智能,深度学习,CUDA)