CUDA UVA क्या है? Pointer से memory पहचानना और copy direction का अनुमान लगाना
CUDA Unified Virtual Addressing (UVA) एक process के भीतर host और समर्थित GPU allocations को एक virtual address space में रखता है। इससे CUDA pointer की memory location पहचान सकता है और `cudaMemcpyDefault` copy की direction का अनुमान लगा सकता है। साझा address space physical memory को एक नहीं करता, हर pointer को हर GPU kernel के लिए accessible नहीं बनाता और data को अपने आप move नहीं करता।
CUDA UVA क्या है? Pointer से memory पहचानना और copy direction का अनुमान लगाना
CUDA Unified Virtual Addressing (UVA) एक process के भीतर host और समर्थित GPU allocations को एक virtual address space में रखता है। इससे CUDA pointer की memory location पहचान सकता है और cudaMemcpyDefault copy की direction का अनुमान लगा सकता है। साझा address space physical memory को एक नहीं करता, हर pointer को हर GPU kernel के लिए accessible नहीं बनाता और data को अपने आप move नहीं करता।
यह लेख NVIDIA CUDA C++ में UVA पर केंद्रित है। Memory type, device accessibility और managed-memory behavior allocation, current device और platform features पर निर्भर रहते हैं; इन्हें संबंधित APIs से जाँचें।
UVA address space में क्या एक करता है?
एक process में CPU memory और हर GPU की global memory साझा virtual address space में अलग-अलग ranges लेती हैं। इसमें cudaMalloc जैसे CUDA allocations और new, malloc या mmap से बने host allocations शामिल हैं। Pointer value का अर्थ यह नहीं कि bytes एक ही physical memory में हैं। Pointer और CUDA की allocation या mapping जानकारी से पता लगाया जा सकता है कि address host का है या किसी खास GPU memory का।
एक OS process की CUDA virtual address space
┌────────────────────────────────────────────────────────┐
│ Host range │ GPU 0 range │ GPU 1 range │
│ new/malloc/mmap │ cudaMalloc │ cudaMalloc │
└────────────────────────────────────────────────────────┘
│ pointer value + cudaPointerGetAttributes()
├─ memory type और owner device पहचानें
└─ cudaMemcpyDefault: pointers से copy direction निकालें
एक virtual address space ≠ एक physical memory या automatic page migrationCUDA Programming Guide बताता है कि UVA space में pointer values से memory location पहचानी जा सकती है और cudaMemcpyDefault copy kind निकाल सकता है। Runtime API का cudaPointerGetAttributes memory type, allocation device और current device पर उपलब्ध host/device aliases देता है। इसलिए UVA manual pointer tracking घटाता और copy calls सरल कर सकता है, पर address पहचानने से access permission नहीं मिलती और memory move नहीं होती। CUDA Programming Guide — Unified Virtual Address Space CUDA Runtime API — Unified Addressing
UVA, Unified Memory और mapped memory में क्या अंतर है?
इन सुविधाओं का साथ उपयोग हो सकता है, पर ये अलग समस्याएँ हल करती हैं। UVA pointers और memory location पहचानने की addressing scheme है। Unified Memory CPU और GPU code द्वारा उपयोग किए जा सकने वाले allocations को manage करता है; placement, access और movement platform support पर निर्भर हैं। Mapped pinned host memory kernel को host allocation सीधे access करने देती है। केवल unified pointer address से हर host allocation को mapped access नहीं मिलता।
| सुविधा | मुख्य समस्या | Pointer या data पर असर | अकेले क्या सुनिश्चित नहीं करती |
|---|---|---|---|
| UVA | Pointer address space और memory location पहचानना | समर्थित ranges process virtual space में होती हैं; pointer queries और cudaMemcpyDefault संभव | Physical memory का मिलना, kernel का मनमाना access या automatic page migration |
| Unified Memory | CPU/GPU managed allocations का उपयोग और प्रबंधन | cudaMallocManaged जैसी managed memory CPU और GPU code उपयोग कर सकते हैं; placement और movement system पर निर्भर | हर system पर एक जैसी migration policy या performance |
| Mapped pinned host memory | Kernel का host memory तक direct access | Page-locked host allocation GPU address space में map होती है | Host memory का GPU local memory जितना तेज़ होना |
UVA-supported devices पर cudaHostAlloc या cudaMallocHost से allocated pinned host memory GPU को directly accessible होती है और सामान्यतः host/device पर समान pointer value रखती है। cudaHostAllocWriteCombined अपवाद है। बाद में cudaHostRegister से registered मौजूदा memory का अलग device pointer हो सकता है; cudaHostGetDevicePointer से device alias लें। यह behavior UVA के साथ allocation या mapping API से मिलता है, हर host allocation का universal गुण नहीं है। CUDA Runtime API — Unified Addressing
HMM या ATS support वाले कुछ systems पर Unified Memory सामान्य system memory को implicitly managed मान सकता है। यह system के managed-memory और coherency features से आता है, UVA द्वारा automatic data movement से नहीं। Pointer usability, page migration और concurrent access जानने के लिए device attributes और संबंधित memory feature अलग से जाँचें। CUDA Programming Guide — Unified Memory
Copy से पहले device और pointer कैसे जाँचें?
पहले cudaDeviceProp::unifiedAddressing से process और target device का UVA support जाँचें। फिर pointer attributes देखकर device allocation का owner device पता करें और cudaMemcpyDefault से runtime को source तथा destination addresses से copy direction infer करने दें। cudaMemcpyDefault केवल UVA-supporting systems पर valid है।
| API या property | क्या जाँचता या करता है | ध्यान दें |
|---|---|---|
cudaDeviceProp::unifiedAddressing | cudaGetDeviceProperties द्वारा लौटाए device का UVA support | Target device की property जाँचें; pointers की equality से अनुमान न लगाएँ |
cudaPointerGetAttributes(&attr, ptr) | type, allocation device और current device पर उपलब्ध host/device aliases देता है | devicePointer NULL हो तो current device के पास उस pointer का direct-access alias नहीं है |
cudaMemcpyDefault | Pointer values से copy direction infer करता है | UVA के बिना invalid; placement या access permissions नहीं बदलता |
cudaDeviceCanAccessPeer / |
नीचे का उदाहरण pinned host allocation से device allocation में copy करता है और device pointer का memory type तथा owner device query करता है। मुख्य बात direction constant के बजाय cudaMemcpyDefault का उपयोग है। Pointer numbers print करना या equality जाँचना kernel accessibility या physical location साबित नहीं करता।
#include <cuda_runtime.h>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#define CUDA_CHECK(call) do { \
cudaError_t err = (call); \
if (err != cudaSuccess) { \
std::fprintf(stderr, "%s\n", cudaGetErrorString(err)); \
std::exit(EXIT_FAILURE); \
} \
} while (0)
int main() {
constexpr int device = 0;
constexpr std::size_t bytes = sizeof(float);
cudaDeviceProp prop{};
CUDA_CHECK(cudaGetDeviceProperties(&prop, device));
if (!prop.unifiedAddressing) {
std::fprintf(stderr, "UVA is unavailable on this device.\n");
return EXIT_FAILURE;
}
CUDA_CHECK(cudaSetDevice(device));
float* host = nullptr;
float* deviceBuffer = nullptr;
CUDA_CHECK(cudaHostAlloc(&host, bytes, cudaHostAllocDefault));
CUDA_CHECK(cudaMalloc(&deviceBuffer, bytes));
host[0] = 42.0f;
cudaPointerAttributes attr{};
CUDA_CHECK(cudaPointerGetAttributes(&attr, deviceBuffer));
if (attr.type != cudaMemoryTypeDevice || attr.device != device) {
std::fprintf(stderr, "Pointer does not describe the expected device allocation.\n");
CUDA_CHECK(cudaFree(deviceBuffer));
CUDA_CHECK(cudaFreeHost(host));
return EXIT_FAILURE;
}
CUDA_CHECK(cudaMemcpy(deviceBuffer, host, bytes, cudaMemcpyDefault));
float result = 0.0f;
CUDA_CHECK(cudaMemcpy(&result, deviceBuffer, bytes, cudaMemcpyDefault));
std::printf("result = %.1f\n", result);
CUDA_CHECK(cudaFree(deviceBuffer));
CUDA_CHECK(cudaFreeHost(host));
}यह listing API क्रम समझाने के लिए है; यह दावा नहीं कि इसे किसी खास CUDA version या GPU पर compile/run किया गया। Production code में RAII जैसी lifetime management रखें ताकि error path पर भी allocation मुक्त हो। Attribute query विफल हो तो जाँचें कि pointer current CUDA context में visible और अभी allocated है। cudaMemcpyDefault विफल हो या UVA न हो तो source और destination type ज्ञात होने पर explicit direction constant लें, और चुने path की accessibility भी जाँचें। CUDA Runtime API — Memory Management
Peer access और pointer failures का निदान कैसे करें?
UVA कई GPU memory ranges को अलग पहचानने का address आधार देता है। फिर भी एक GPU को दूसरी GPU की memory सीधे access कराने के लिए device pair का peer access support जाँचें और उसे enable करें। यदि cudaDeviceCanAccessPeer support नहीं बताता, तो UVA pointer उस सीमा को पार नहीं करता। Peer copy या host staging जैसे supported transfer path चुनें।
| लक्षण | क्या जाँचें | सुरक्षित कार्रवाई |
|---|---|---|
cudaMemcpyDefault error देता है | Target का unifiedAddressing, pointer validity/lifetime और copy API की address requirements | पहले UVA जाँचें; जरूरत हो तो explicit direction से समस्या सीमित करें |
| Pointer value ठीक लगता है पर attribute query विफल है | Current CUDA context में allocation/registration/mapping और pointer मुक्त तो नहीं | Allocation API और owner trace करें; valid lifetime में ही query करें |
| एक GPU का kernel दूसरी GPU के pointer को access नहीं कर पाता | cudaDeviceCanAccessPeer, peer enablement और device pair/topology | Support confirm करने के बाद ही cudaDeviceEnablePeerAccess; अन्यथा supported copy path चुनें |
| CPU या kernel accessibility को लेकर भ्रम | cudaPointerAttributes |
**Execution environment जाँचें।** 64-bit process और target device के लिए `unifiedAddressing` query करें। कई GPU हों तो हर device जाँचें।
**Pointer का स्रोत और lifetime दर्ज करें।** पता करें कि वह `cudaMalloc`, `cudaHostAlloc`, `cudaHostRegister`, `cudaMallocManaged` या system allocator से आया है। Free/unregister के बाद उपयोग न करें।
**जरूरत पड़ने पर attributes query करें।** Type, owner device और उपलब्ध aliases देखें। Query address classify करता है, काम synchronize नहीं करता।
**Copy direction सत्यापित करें।** UVA होने पर `cudaMemcpyDefault` से copy करें और data compare करें। जरूरत पर `cudaMemcpyHostToDevice` जैसे explicit call से तुलना करें।
**Device-to-device access अलग से जाँचें।** P2P के लिए हर pair की support query करें और enablement confirm करें। Support न हो तो host staging या supported peer copy पर लौटें।
संक्षेप में, UVA pointer द्वारा दर्शाई memory पहचानने की address प्रणाली है। cudaPointerGetAttributes और cudaMemcpyDefault उस जानकारी का उपयोग करते हैं, पर physical memories नहीं मिलाते और kernels को मनमाना access नहीं देते। Unified Memory page management, mapped host access और peer permissions अलग-अलग verify करें।
स्रोत
[NVIDIA CUDA Programming Guide — Unified and System Memory](https://docs.nvidia.com/cuda/cuda-programming-guide/02-basics/understanding-memory.html)
[NVIDIA CUDA Runtime API 13.5 — Unified Addressing](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__UNIFIED.html)
[NVIDIA CUDA Runtime API 13.5 — Memory Management](https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html)
[NVIDIA CUDA C++ Best Practices Guide 13.5 — Unified Virtual Addressing](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/)