NVIDIA CUDA library which can be integrated with C/C++

CK CK 3776 
Created at  
375 0 0

The NVIDIA CUDA library is a parallel computing platform and programming model that allows software developers to use NVIDIA GPUs for general-purpose processing – an approach known as GPGPU. It provides a C/C++ API, including header files and libraries, enabling developers to write code that runs on CUDA-enabled GPUs, leveraging their massive parallel processing capabilities to accelerate computationally intensive tasks. This integration allows for relatively straightforward porting of existing C/C++ code to take advantage of GPU acceleration.

 

NVIDIA provides several CUDA libraries that can be integrated with C and C++ programs to accelerate computations on NVIDIA GPUs. These libraries are optimized for high-performance computing and span domains like linear algebra, deep learning, image processing, and more.

Key NVIDIA CUDA Libraries

Here’s a breakdown of commonly used CUDA libraries for various purposes:

1. CUDA Runtime API and Driver API

  • Purpose: Core APIs for managing memory, kernels, and device synchronization.
  • Integration: Provides foundational support for writing GPU programs in C/C++.
  • Key Features:
    • Device management
    • Memory allocation (e.g., cudaMalloc, cudaMemcpy)
    • Kernel launches

Example:

#include <cuda_runtime.h>
#include <iostream>

__global__ void addKernel(int *a, int *b, int *c) {
    int idx = threadIdx.x;
    c[idx] = a[idx] + b[idx];
}

int main() {
    const int size = 256;
    int a[size], b[size], c[size];
    int *d_a, *d_b, *d_c;

    cudaMalloc((void **)&d_a, size * sizeof(int));
    cudaMalloc((void **)&d_b, size * sizeof(int));
    cudaMalloc((void **)&d_c, size * sizeof(int));

    cudaMemcpy(d_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    cudaMemcpy(d_b, b, size * sizeof(int), cudaMemcpyHostToDevice);

    addKernel<<<1, size>>>(d_a, d_b, d_c);

    cudaMemcpy(c, d_c, size * sizeof(int), cudaMemcpyDeviceToHost);

    cudaFree(d_a);
    cudaFree(d_b);
    cudaFree(d_c);

    std::cout << "Addition complete." << std::endl;
    return 0;
}

2. cuBLAS

  • Purpose: GPU-accelerated implementation of BLAS (Basic Linear Algebra Subprograms).
  • Features:
    • Vector and matrix operations like dot product, matrix multiplication, etc.
    • Highly optimized for GPUs.
  • Header: #include <cublas_v2.h>

Example:

#include <cublas_v2.h>
#include <iostream>

int main() {
    cublasHandle_t handle;
    cublasCreate(&handle);

    float alpha = 1.0f, beta = 0.0f;
    float A[6] = {1, 2, 3, 4, 5, 6}; // 2x3 matrix
    float B[3] = {1, 2, 3};          // 3x1 vector
    float C[2] = {0};                // 2x1 result

    float *d_A, *d_B, *d_C;
    cudaMalloc((void **)&d_A, 6 * sizeof(float));
    cudaMalloc((void **)&d_B, 3 * sizeof(float));
    cudaMalloc((void **)&d_C, 2 * sizeof(float));

    cudaMemcpy(d_A, A, 6 * sizeof(float), cudaMemcpyHostToDevice);
    cudaMemcpy(d_B, B, 3 * sizeof(float), cudaMemcpyHostToDevice);

    cublasSgemv(handle, CUBLAS_OP_N, 2, 3, &alpha, d_A, 2, d_B, 1, &beta, d_C, 1);

    cudaMemcpy(C, d_C, 2 * sizeof(float), cudaMemcpyDeviceToHost);

    std::cout << "Result: " << C[0] << ", " << C[1] << std::endl;

    cudaFree(d_A);
    cudaFree(d_B);
    cudaFree(d_C);
    cublasDestroy(handle);

    return 0;
}

3. cuFFT

  • Purpose: Fast Fourier Transform (FFT) on NVIDIA GPUs.
  • Features:
    • Forward and inverse FFTs
    • Single and double precision
  • Header: #include <cufft.h>

Example:

#include <cufft.h>
#include <iostream>

int main() {
    cufftHandle plan;
    cufftComplex *d_data;
    int n = 16;

    cudaMalloc((void **)&d_data, sizeof(cufftComplex) * n);

    cufftPlan1d(&plan, n, CUFFT_C2C, 1);
    cufftExecC2C(plan, d_data, d_data, CUFFT_FORWARD);

    cufftDestroy(plan);
    cudaFree(d_data);

    std::cout << "FFT complete." << std::endl;
    return 0;
}

4. cuDNN

  • Purpose: GPU-accelerated deep learning primitives (used in frameworks like TensorFlow and PyTorch).
  • Features:
    • Convolutions, activation functions, normalization, etc.
  • Header: #include <cudnn.h>

Example: Visit NVIDIA's documentation for in-depth integration examples with neural network layers.


5. NPP (NVIDIA Performance Primitives)

  • Purpose: Image and signal processing.
  • Features:
    • Filters, resizing, morphology, color conversion.
  • Header: #include <nppi.h>

6. Thrust

  • Purpose: High-level parallel programming library with STL-like interfaces.
  • Header: #include <thrust/sort.h>

Example:

#include <thrust/device_vector.h>
#include <thrust/sort.h>
#include <iostream>

int main() {
    thrust::device_vector<int> d_vec(4);
    d_vec[0] = 10;
    d_vec[1] = 3;
    d_vec[2] = 7;
    d_vec[3] = 1;

    thrust::sort(d_vec.begin(), d_vec.end());

    for (int i = 0; i < d_vec.size(); i++)
        std::cout << d_vec[i] << " ";
    return 0;
}

Getting Started

  1. Install CUDA Toolkit: Download from the NVIDIA Developer site.
  2. Set Up Environment: Ensure paths for nvcc compiler and libraries are correctly configured.
  3. Link Libraries:
    • For compilation: Use flags like -lcublas, -lcufft, etc.
    • Example: nvcc -o example example.cu -lcublas

These libraries allow high-performance computing tasks to harness the power of NVIDIA GPUs directly in your C or C++ applications. Let me know if you'd like specific guidance!
 

Tags CUDA Library GPU GPU Library NVIDIA NVIDIA CUDA NVIDIA GPU NVIDIA GPU Library Facebook X
Comments 0
Similar posts
  1. Getting Started with FAISS: A CentOS 7 Installation Guide
    277
  2. CUDA versus OpenCL
    575
  3. Difference between CPU and GPU - Why GPU is faster than CPU?
    801
  4. Rust-GPU-tools for interacting with various GPU APIs
    751
  5. Technical Hierarchy of AI Agents
    806
  6. TensorRT for LLMs: Speed, Scale, and Why Startups Choose It Over TensorFlow
    458
  7. Curiousity if OpenAI Engineers use Cursor.ai
    6561
  8. Building GPU Clusters on GCP, Azure, and AWS: A Practical Guide and Head-to-Head Comparison
    327
  9. GPU & LLM Cluster Infrastructure & Task Scheduling Architecture
    229
  1. SafeFrame is a web standard developed by IAB
    505
  2. CSAI vs SSAI
    574
  3. Ad Marker: SCTE-35 Message Encoder based in Python
    794
  4. Ad marker: SCTE-35 enhanced
    570
  5. FFmpeg compile options based on configure
    1,109
  6. Script checking internet restriction with FFmpeg
    678
  7. FFmpeg compile option to support SCTE-35
    837
  8. HTTP Proxy when your URL access is blocked with FFmpeg
    1,884
  9. Limit Maximum Resolution in HLS.js
    662
  10. Magic line when HLS video manifest is mixed with encrypted / unencrypted video chunks
    1,447
  11. Recovering a crashed HLS.js player
    1,273
  12. How do I create animated background for Google Meet?
    19,000
  13. Secure HLS encoding using AES-128 encryption with FFmpeg
    1,129
  14. Machine Learning Models for Ad Detection from a Video Stream
    184
  15. Unlock Flawless HLS Audio Sync: The Power of #EXT-X-INDEPENDENT-SEGMENTS
    368
  16. Approaches to Scene Detection in Video Processing
    642
  17. FFmpeg CLI to remove audio track from MP4
    540
  18. Building GPU as a Service aka Dockerized GPU
    1,560
  19. IAB Categories (a.k.a. OpenRTB Code)
    1,012
  20. FFmpeg library that can be directly integrated into C/C++/Rust/Python without CLI
    2,046
  21. HTTP Live Streaming (HLS) was developed by Apple as part of QuickTime X in iOS3.0, and became an official standard (RFC 8126) in 2017
    591
  22. Progressive MP4 is an application of streaming techniques using the standardized MP4 container invented by MPEG LA consortium
    1,068
  23. FFmpeg CLI to mix two different audios in a single audio file such as mp3
    598
  24. FFmpeg CLI to merge a video only mp4 and mp3 audio file
    332
  25. FFmpeg CLI to convert a video file to an MP3 audio file
    345
  26. HLS.js options to support subtitle
    511
  27. When HLS.js need to load m3u8 URL redirecting to the different URL (302 Redirection)
    659
  28. HLS.js code to reload when user face bufferfull error
    702
  29. HLS.js code to reload video when levelLoadTimeOut occurs
    703
  30. Change the manifest loading timeout in HLS.js
    1,027
Recently updated
  1. How do I create animated background for Google Meet?
    19,000
  2. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    192
  3. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    75
  4. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    151
  5. Array of arguments passed to script in PHP
    83
  6. Invalid Auction Storms in Ad Tech
    91
  7. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    194
  8. The Two Clocks of Ad Server Architecture
    61
  9. Frequency Cap Architecture Overview
    505
  10. Collecting Proof-of-Play from 100,000 Screens
    139
  11. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    276
  12. What is RedisVL?
    102
  13. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    218
  14. TPU & GPU Cluster Management Tools
    229
  15. The Concept of Scaffolding in AI Agents
    217
  16. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    568
  17. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    246
  18. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    321
  19. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    196
  20. Machine Learning: Training vs. Ranking
    200
  21. The Continuous Learning Machine: Designing a Loop That Actually Turns
    212
  22. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    188
  23. Designing a Data Curation System Around a Vector Database
    212
  24. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    228
  25. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    646
  26. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    179
  27. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    203
  28. Understanding Data Lineage
    251
  29. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    159
  30. GPU & LLM Cluster Infrastructure & Task Scheduling Architecture
    229