6.2.15 · HinglishGPU Architecture

ROCm - OpenCL alternatives

2,701 words12 min readRead in English

6.2.15 · Hardware › GPU Architecture

Overview

Jab ki CUDA GPU computing mein dominant hai, ROCm (Radeon Open Compute) aur OpenCL (Open Computing Language) heterogeneous parallel computing ke liye vendor-agnostic alternatives provide karte hain. In platforms ko samajhna critical hai kyunki vendor lock-in innovation ko rok deta hai aur portability broader hardware access enable karti hai.

Figure — ROCm - OpenCL alternatives

Core Concepts

First Principles Se: Vendor Abstraction Kaise Kaam Karta Hai

The Hardware Abstraction Stack

Kyun chahiye layers? Har GPU vendor ke paas alag hote hain:

  • Instruction sets (NVIDIA: PTX, AMD: GCN/RDNA ISA)
  • Memory hierarchies (L1/L2 cache sizes, shared memory)
  • Thread organization (NVIDIA warps vs AMD wavefronts)

Kya hai solution? Ek compilation pipeline jo high-level code → intermediate representation → vendor-specific machine code mein translate karta hai.

Kaise kaam karta hai step-by-step?

Step 1: Portable kernel code likho

// OpenCL kernel (vendor-agnostic)
__kernel void vector_add(__global float* A, 
                         __global float* B,
                         __global float* C) {
    int i = get_global_id(0);  // Abstract work-item ID
    C[i] = A[i] + B[i];
}

Yeh step kyun? Hum standardized built-ins (get_global_id) use karte hain hardware-specific registers ki jagah. OpenCL runtime is cheez ko jo bhi hardware provide karta hai usse map kar dega.

Step 2: Runtime compilation

Source code → LLVM IR → Device-specific ISA → Execution

Yeh step kyun? Just-in-time (JIT) compilation driver ko allow karta hai ki woh actual hardware present ke liye optimize kare. Agar tumhare paas AMD RX 7900 hai, toh woh RDNA 3 ke liye compile karta hai. Agar Intel Arc hai, toh Xe-HPG ke liye compile karta hai.

Step 3: Memory space mapping

OpenCL Memory NVIDIA Mapping AMD Mapping
__global Global memory VRAM
__local Shared memory LDS (Local Data Share)
__private Registers VGPRs (Vector GPUs)

Yeh kyun matter karta hai? Abstraction chhupa deta hai ki AMD ke paas 64 KB LDS per CU hai jabki NVIDIA ke paas 48-128 KB shared memory per SM hai (48 KB ya 96 KB on Fermi–Pascal, up to 128 KB on Ampere). Tumhara code automatically adapt kar leta hai.

ROCm ka HIP: CUDA Compatibility Layer

Derivation: HIP portability kaise achieve karta hai

Start: CUDA kernel

__global__ void saxpy(float a, float* x, float* y, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) y[i] = a * x[i] + y[i];
}

Step 1: HIP par port karo (modern HIP mein same blockIdx.x/threadIdx.x built-ins use hote hain)

// HIP kernel (nearly identical to CUDA)
__global__ void saxpy(float a, float* x, float* y, int n) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) y[i] = a * x[i] + y[i];
}

Yeh kyun kaam karta hai? HIP same built-in names define karta hai (blockIdx, threadIdx, blockDim) aur API functions (hipMalloc, hipMemcpy) jo appropriate backend se map hote hain:

// When compiling for NVIDIA (HIP-Clang targeting NVPTX):
//   hipMalloc  -> cudaMalloc
//   blockIdx.x -> CUDA blockIdx.x
 
// When compiling for AMD (HIP-Clang targeting AMDGPU):
//   hipMalloc  -> native ROCm allocation
//   blockIdx.x -> AMDGPU workgroup/workitem intrinsics

Step 2: Compilation routes

HIP source → [HIP-Clang → NVPTX] → NVIDIA binary
HIP source → [HIP-Clang → AMDGPU] → ROCm binary

Do paths kyun? Alag instruction sets ke liye alag code-generation targets ki zaroorat hoti hai, lekin source code identical rehta hai. Note karo ki HIP NVCC use nahi karta — yeh HIP-Clang use karta hai, jo NVIDIA GPUs ke liye NVPTX (NVIDIA ka virtual ISA) emit karta hai aur AMD GPUs ke liye AMDGPU ISA.

Common Mistakes & Misconceptions

Architecture Comparison

Concept Map

creates

breaks

breaks

provides

uses

translates 80-95%

maintained by

enables

enables

via

compiles to

targets

CUDA NVIDIA-only

Vendor Lock-in

ROCm AMD Platform

OpenCL Cross-platform

HIP CUDA-like API

HSA Memory Access

Khronos Group

Hardware Abstraction

JIT Compilation

Vendor-specific ISA