CUDA टेक्सचर मेमोरी क्या है? 2D सैंपलिंग और सामान्य लोड में कैसे चुनें
CUDA texture memory GPU डेटा को निर्देशांकों से sample करने का केवल-पढ़ने वाला तरीका है। Texture object पढ़े जाने वाले resource को coordinate, filter और boundary सेटिंग से जोड़ता है; यह उन warp के लिए उपयोगी हो सकता है जिनके threads 2D में पास-पास के मान पढ़ते हैं। Cache hit DRAM traffic घटा सकता है, लेकिन fetch latency समाप्त नहीं करता। इसलिए texture read सामान्य global memory load से हमेशा तेज़ नहीं होता। [CUDA C++ Best Practices Guide — Texture Memory](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/)
CUDA टेक्सचर मेमोरी क्या है? 2D सैंपलिंग और सामान्य लोड में कैसे चुनें
CUDA texture memory GPU डेटा को निर्देशांकों से sample करने का केवल-पढ़ने वाला तरीका है। Texture object पढ़े जाने वाले resource को coordinate, filter और boundary सेटिंग से जोड़ता है; यह उन warp के लिए उपयोगी हो सकता है जिनके threads 2D में पास-पास के मान पढ़ते हैं। Cache hit DRAM traffic घटा सकता है, लेकिन fetch latency समाप्त नहीं करता। इसलिए texture read सामान्य global memory load से हमेशा तेज़ नहीं होता। CUDA C++ Best Practices Guide — Texture Memory
Texture memory क्या है और सामान्य load से कैसे अलग है?
Texture fetch, tex1D, tex2D या tex3D जैसी device functions से coordinates के आधार पर डेटा पढ़ता है। Texture object डेटा स्रोत (resource descriptor) और sampling नियम (texture descriptor) को जोड़ता है। समर्थित resource type के अनुसार स्रोत CUDA array, device की linear memory या pitch वाली 2D memory हो सकती है। इसे graphics texturing से जुड़े hardware का उपयोग करने वाला CUDA sampling path समझ सकते हैं। CUDA Runtime API — Texture Object Management
| पहलू | Texture fetch | सामान्य global memory load |
|---|---|---|
| पहुँच | coordinates और sampler सेटिंग से 1D, 2D या 3D texel पढ़ता है। | pointer और index से element पढ़ता है। |
| लिखना | Texture read केवल-पढ़ने वाला है; लिखने के लिए अलग path चाहिए। | पढ़ और लिख सकता है। |
| स्थानिक sampling | 2D locality, filtering और address mode के लिए अनुकूल texture cache उपयोग कर सकता है। | indices को coalescing और सामान्य cache behavior के अनुसार बनाते हैं। |
| coordinates और किनारे | normalized coordinates तथा clamp या wrap जैसे mode सेट किए जा सकते हैं। | coordinate conversion और boundary branches code में स्पष्ट होते हैं। |
| समझौता | resource और sampler सेटअप चाहिए; एक kernel में लिखना और texture से पढ़ना coherent नहीं है। | अधिक सीधा है, लेकिन interpolation और boundary handling स्वयं लागू करें। |
NVIDIA के अनुसार texture cache 2D spatial locality के लिए optimize किया गया है: पास-पास के texture addresses पढ़ने वाले threads का warp इस path के अनुकूल है। Cache hit DRAM bandwidth की मांग घटाता है, fetch latency नहीं। GPU और access pattern के आधार पर global loads सामान्य cache का भी उपयोग कर सकते हैं। इसलिए texture path तभी उपयोगी है जब spatial neighbors का पुनः उपयोग या sampling सुविधाएँ वास्तव में काम आएँ। CUDA C++ Best Practices Guide — Texture Memory
2D डेटा पढ़ने के लिए object कैसे बनाएँ?
पहले पुष्टि करें कि डेटा device पर है और उसका आकार, channel format तथा layout texture resource के रूप में समर्थित है। cudaResourceDesc डेटा बताता है; cudaTextureDesc coordinate normalization, filtering और address mode जैसी सेटिंग करता है। cudaCreateTextureObject दोनों को cudaTextureObject_t में जोड़ता है, जिसे kernel को देकर tex2D से पढ़ा जाता है। इसे उपयोग करने वाले सभी kernels पूरे होने के बाद object नष्ट करें और resource मुक्त करें। device support तथा resource के pitch, alignment और dimension limits Runtime API में जाँचें। CUDA Runtime API — Texture Object Management
मान लें कि input image float texel वाले CUDA 2D array में है और हर output pixel के केंद्र के अनुरूप input स्थान को linear तरीके से sample करना है। नीचे का अंश सेटिंग और coordinate नियम दिखाता है, पूरा program नहीं। Array allocation, data transfer और पूर्ण error handling छोड़े गए हैं; हर API परिणाम जाँचें और वास्तविक format तथा dimensions के अनुसार सेटिंग करें।
मौजूदा CUDA 2D array
│ resource descriptor: cudaResourceDesc
▼
cudaCreateTextureObject ◄── sampler सेटिंग: cudaTextureDesc
│ cudaTextureObject_t
▼
kernel tex2D(u, v) बुलाता है
├─ warp पास के 2D coordinates पढ़ता है ─► texture cache locality मदद कर सकती है
└─ filter/address सेटिंग ───────────────► texel, interpolation और boundary चुनती है// inputArray: an existing cudaArray_t containing float texels
__global__ void resize(cudaTextureObject_t tex, float* output,
int outWidth, int outHeight) {
int x = blockIdx.x * blockDim.x + threadIdx.x;
int y = blockIdx.y * blockDim.y + threadIdx.y;
if (x < outWidth && y < outHeight) {
float u = (x + 0.5f) / outWidth;
float v = (y + 0.5f) / outHeight;
output[y * outWidth + x] = tex2D<float>(tex, u, v);
}
}
void runResize(cudaArray_t inputArray, float* output,
int outWidth, int outHeight) {
cudaResourceDesc resource{};
resource.resType = cudaResourceTypeArray;
resource.res.array.array = inputArray;
cudaTextureDesc sampling{};
sampling.addressMode[0] = cudaAddressModeClamp;
sampling.addressMode[1] = cudaAddressModeClamp;
sampling.filterMode = cudaFilterModeLinear;
sampling.readMode = cudaReadModeElementType;
sampling.normalizedCoords = 1;
cudaTextureObject_t image = 0;
cudaCreateTextureObject(&image, &resource, &sampling, nullptr);
// Check API and kernel errors in a complete program.
dim3 block(16, 16);
dim3 grid((outWidth + block.x - 1) / block.x,
(outHeight + block.y - 1) / block.y);
resize<<<grid, block>>>(image, output, outWidth, outHeight);
cudaDeviceSynchronize(); // Wait before releasing the object in this example.
cudaDestroyTextureObject(image);
// Release inputArray only after its final use.
}Normalized coordinates में हर axis texture dimension से scale होती है; (x + 0.5) / width एक texel के center को दर्शाता है। Output coordinate से input coordinate में mapping transformation के नियम पर निर्भर करती है, जैसे pixel centers और boundaries को कैसे align किया गया है। यह एक sampling convention है, हर resize के लिए सार्वभौमिक नियम नहीं। सुनिश्चित करें कि cudaCreateTextureObject सफल हुआ और उपयोग के दौरान object या array नष्ट न हों।
Coordinates, filter और boundary mode परिणाम कैसे बदलते हैं?
cudaFilterModePoint एक texel चुनता है; cudaFilterModeLinear पास के texel के बीच interpolation करता है। 2D में linear filtering चार texel से bilinear interpolation करती है। यह तभी मान्य है जब लौटाया गया मान floating point हो; integer input के साथ उपयोग से पहले data type और read mode जाँचें। CUDA Runtime API — Texture Object Management
बिना normalization के coordinates texel units में होते हैं; normalized coordinates हर axis के अनुसार scale होते हैं। उदाहरण में texel center के लिए (x + 0.5) / width उपयोग करें। आकार बदलने पर यह नियम सुविधाजनक है, पर इच्छित transformation geometry अपने आप तय नहीं करता। Resource dimensions से गणना जाँचें।
cudaAddressModeClamp सीमा से बाहर के coordinate को किनारे पर संभालता है। cudaAddressModeWrap और cudaAddressModeMirror केवल normalized coordinates पर समर्थित हैं; normalization बंद हो तो CUDA इन्हें clamp में बदल देता है। दूसरी तरफ wrap करना, mirror करना और clamp करना अलग image boundaries देते हैं; इच्छित नियम के अनुसार चुनें। CUDA Runtime API — Texture Object Management
cudaReadModeNormalizedFloat से 8-bit और 16-bit integer texel floating point में बदले जा सकते हैं: unsigned मान [0, 1] और signed मान [-1, 1] में map होते हैं। यह setting 32-bit integer texel को promote नहीं करती। Numerical range और image values का अर्थ बदल सकता है, इसलिए input type, sign और आगे की गणना मेल रखें। Linear filtering और normalized read डेटा तथा precision के विकल्प हैं, स्वतः quality सुधार नहीं। CUDA Runtime API — Texture Object Management
Texture कब चुनें और सुरक्षित तुलना कैसे करें?
ऐसे read-only 2D image या grid workload में texture object उपयोगी हो सकता है जहाँ पास के coordinates फिर से उपयोग हों या hardware addressing और linear interpolation मदद करें। यदि threads नियमित स्थानों से पढ़ते हैं और लिखना या custom interpolation चाहिए, तो सामान्य global loads सरल हो सकते हैं। Object creation, cache reuse, address calculation और interpolation accuracy सहित पूरा काम तुलना करें।
उसी kernel invocation में global memory पर लिखे address को texture से न पढ़ें। NVIDIA चेतावनी देता है कि texture cache इन writes के साथ coherent नहीं रहता, इसलिए fetch undefined data लौटा सकता है। सुरक्षित आधार यह है कि producer kernel पूरा हो, आवश्यक stream order या synchronization बनी रहे, फिर अलग consumer kernel texture से पढ़े। CUDA C++ Best Practices Guide — Device Memory Spaces
तय करें कि डेटा read-only है या नहीं और कौन सा समर्थित 1D, 2D या 3D resource type/format उपयुक्त है। Pitched pointer या CUDA array के लिए pitch, alignment और dimension limits जाँचें।
Texel या normalized coordinates चुनें। Pixel center, output scaling और boundary के छोटे उदाहरण रखकर mapping सत्यापित करें।
जाँचें कि cudaTextureDesc के address mode, point/linear filter और read mode वांछित परिणाम तथा input type से मेल खाते हैं। Linear filter की floating-point शर्त और integer conversion की width सीमा पुष्टि करें।
cudaCreateTextureObject का return value जाँचें और object kernel को दें। उपयोग करने वाले सभी kernels पूरे होने तक object और resource जीवित रखें; उसी kernel में लिखे address को texture से न पढ़ें।
समान input, output, boundaries और accuracy conditions के साथ texture और global-load versions की तुलना करें। Representative 2D patterns और target GPUs पर object creation सहित पूरा path मापें।
यदि परिणाम अपेक्षा से अलग हो, पहले coordinate units, texel centers, address modes और filter/read modes जाँचें। अंतर boundaries पर हो तो clamp, wrap या mirror और normalization देखें। Values पुरानी या अनियमित दिखें तो producer/consumer kernel boundaries, stream order और उस address पर writes जाँचें। Performance न बढ़े तो texture cache behavior और DRAM demand देखें; केवल cache hit से कम latency साबित नहीं होती।
Texture memory “image होने के कारण तेज़” नहीं है। यह पढ़ने का path सरल कर सकती है जब पास के 2D coordinates का reuse, boundary handling या interpolation workload के अनुकूल हो। Coordinates और input format परिणाम बदलते हैं; एक kernel की writes और texture reads न मिलाएँ, और समान accuracy पर पूरे path की तुलना करके निर्णय लें।
स्रोत
[NVIDIA CUDA C++ Best Practices Guide — Texture Memory](https://docs.nvidia.com/cuda/cuda-c-best-practices-guide/)
[NVIDIA CUDA Runtime API — Texture Object Management](https://docs.nvidia.com/cuda/cuda-runtime-api/cuda_runtime_api/group__CUDART__TEXTURE__OBJECT.html)