Skip to content

Commit 042e292

Browse files
authored
Merge pull request #27 from comeyrd/master
Updating Texture implementation after CUDA 12
2 parents 3812c80 + 7b1aa83 commit 042e292

1 file changed

Lines changed: 41 additions & 26 deletions

File tree

src/cuda/level0/devicememory/DeviceMemory.cu

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@ __device__ int getRand(int seed, int mod);
128128
/// <param name="n"> An int to process. </param>
129129
/// <param name="d_out"> [in,out] If non-null, the out. </param>
130130
/// <param name="width"> The width. </param>
131+
/// <param name="texA"> The texture Object. </param>
131132
////////////////////////////////////////////////////////////////////////////////////////////////////
132133

133-
__global__ void readTexels(int n, float *d_out, int width);
134+
__global__ void readTexels(int n, float *d_out, int width, cudaTextureObject_t texA);
134135

135136
////////////////////////////////////////////////////////////////////////////////////////////////////
136137
/// <summary> Reads texels in cache. </summary>
@@ -139,9 +140,10 @@ __global__ void readTexels(int n, float *d_out, int width);
139140
///
140141
/// <param name="n"> An int to process. </param>
141142
/// <param name="d_out"> [in,out] If non-null, the out. </param>
143+
/// <param name="texA"> The texture Object. </param>
142144
////////////////////////////////////////////////////////////////////////////////////////////////////
143145

144-
__global__ void readTexelsInCache(int n, float *d_out);
146+
__global__ void readTexelsInCache(int n, float *d_out, cudaTextureObject_t texA);
145147

146148
////////////////////////////////////////////////////////////////////////////////////////////////////
147149
/// <summary> Reads texels random. </summary>
@@ -152,12 +154,11 @@ __global__ void readTexelsInCache(int n, float *d_out);
152154
/// <param name="d_out"> [in,out] If non-null, the out. </param>
153155
/// <param name="width"> The width. </param>
154156
/// <param name="height"> The height. </param>
157+
/// <param name="texA"> The texture Object. </param>
155158
////////////////////////////////////////////////////////////////////////////////////////////////////
156159

157-
__global__ void readTexelsRandom(int n, float *d_out, int width, int height);
158-
// Texture to use for the benchmarks
159-
/// <summary> The tex a. </summary>
160-
texture<float4, 2, cudaReadModeElementType> texA;
160+
__global__ void readTexelsRandom(int n, float *d_out, int width, int height, cudaTextureObject_t texA);
161+
161162

162163
// ****************************************************************************
163164
// Function: addBenchmarkSpecOptions (From SHOC)
@@ -455,11 +456,8 @@ void TestTextureMem(ResultDatabase &resultDB, OptionParser &op, double scalet) {
455456
checkCudaErrors(cudaEventCreate(&start));
456457
checkCudaErrors(cudaEventCreate(&stop));
457458

458-
// make sure our texture behaves like we want....
459-
texA.normalized = false;
460-
texA.addressMode[0] = cudaAddressModeClamp;
461-
texA.addressMode[1] = cudaAddressModeClamp;
462-
texA.filterMode = cudaFilterModePoint;
459+
//Channel Description
460+
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc<float4>();
463461

464462
for (int j = 0; j < nsizes; j++) {
465463
if(!quiet) {
@@ -499,24 +497,38 @@ void TestTextureMem(ResultDatabase &resultDB, OptionParser &op, double scalet) {
499497

500498
// Allocate a cuda array
501499
cudaArray *cuArray;
502-
checkCudaErrors(cudaMallocArray(&cuArray, &texA.channelDesc, width, height));
500+
checkCudaErrors(cudaMallocArray(&cuArray, &channelDesc, width, height));
503501

504502
// Copy in source data
505503
//checkCudaErrors(cudaMemcpyToArray(cuArray, 0, 0, h_in, size, cudaMemcpyHostToDevice));
506504

507505
checkCudaErrors(cudaMemcpy2DToArray(cuArray, 0, 0, h_in, width, width, height, cudaMemcpyDefault));
508-
509-
// Bind texture to the array
510-
checkCudaErrors(cudaBindTextureToArray(texA, cuArray));
511-
506+
//creating ressource descriptor
507+
cudaResourceDesc resDesc = {};
508+
memset(&resDesc, 0, sizeof(resDesc));
509+
resDesc.resType = cudaResourceTypeArray;
510+
resDesc.res.array.array = cuArray;
511+
512+
//creating Texture Descriptor
513+
cudaTextureDesc texDesc = {};
514+
memset(&texDesc, 0, sizeof(texDesc));
515+
texDesc.addressMode[0] = cudaAddressModeClamp;
516+
texDesc.addressMode[1] = cudaAddressModeClamp;
517+
texDesc.filterMode = cudaFilterModePoint;
518+
texDesc.readMode = cudaReadModeElementType;
519+
texDesc.normalizedCoords = 0;
520+
521+
//creating the texture object :
522+
cudaTextureObject_t texObjA = 0;
523+
checkCudaErrors(cudaCreateTextureObject(&texObjA, &resDesc, &texDesc, NULL));
512524
for (int p = 0; p < passes; p++) {
513525
// Test 1: Repeated Linear Access
514526
float t = 0.0f;
515527

516528
checkCudaErrors(cudaEventRecord(start, 0));
517529
// read texels from texture
518530
for (int iter = 0; iter < iterations; iter++) {
519-
readTexels<<<gridSize, blockSize>>>(kernelRepFactor, d_out, width);
531+
readTexels<<<gridSize, blockSize>>>(kernelRepFactor, d_out, width, texObjA);
520532
}
521533
checkCudaErrors(cudaEventRecord(stop, 0));
522534
checkCudaErrors(cudaEventSynchronize(stop));
@@ -540,7 +552,7 @@ void TestTextureMem(ResultDatabase &resultDB, OptionParser &op, double scalet) {
540552
// Test 2 Repeated Cache Access
541553
checkCudaErrors(cudaEventRecord(start, 0));
542554
for (int iter = 0; iter < iterations; iter++) {
543-
readTexelsInCache<<<gridSize, blockSize>>>(kernelRepFactor, d_out);
555+
readTexelsInCache<<<gridSize, blockSize>>>(kernelRepFactor, d_out, texObjA);
544556
}
545557
cudaEventRecord(stop, 0);
546558
cudaEventSynchronize(stop);
@@ -564,7 +576,7 @@ void TestTextureMem(ResultDatabase &resultDB, OptionParser &op, double scalet) {
564576

565577
// read texels from texture
566578
for (int iter = 0; iter < iterations; iter++) {
567-
readTexelsRandom<<<gridSize, blockSize>>>(kernelRepFactor, d_out, width, height);
579+
readTexelsRandom<<<gridSize, blockSize>>>(kernelRepFactor, d_out, width, height, texObjA);
568580
}
569581

570582
checkCudaErrors(cudaEventRecord(stop, 0));
@@ -585,7 +597,7 @@ void TestTextureMem(ResultDatabase &resultDB, OptionParser &op, double scalet) {
585597
delete[] h_out;
586598
checkCudaErrors(cudaFree(d_out));
587599
checkCudaErrors(cudaFreeArray(cuArray));
588-
checkCudaErrors(cudaUnbindTexture(texA));
600+
checkCudaErrors(cudaDestroyTextureObject(texObjA));
589601
}
590602
checkCudaErrors(cudaEventDestroy(start));
591603
checkCudaErrors(cudaEventDestroy(stop));
@@ -836,16 +848,17 @@ __global__ void writeLocalMemory(float *output, int size, int repeat) {
836848
/// <param name="n"> An int to process. </param>
837849
/// <param name="d_out"> [in,out] If non-null, the out. </param>
838850
/// <param name="width"> The width. </param>
851+
/// <param name="texA"> The texture Object. </param>
839852
////////////////////////////////////////////////////////////////////////////////////////////////////
840853

841-
__global__ void readTexels(int n, float *d_out, int width) {
854+
__global__ void readTexels(int n, float *d_out, int width, cudaTextureObject_t texA) {
842855
int idx_x = (blockIdx.x * blockDim.x) + threadIdx.x;
843856
int idx_y = (blockIdx.y * blockDim.y) + threadIdx.y;
844857
int out_idx = idx_y * gridDim.x + idx_x;
845858
float sum = 0.0f;
846859
int width_bits = width - 1;
847860
for (int i = 0; i < n; i++) {
848-
float4 v = tex2D(texA, float(idx_x), float(idx_y));
861+
float4 v = tex2D<float4>(texA, float(idx_x), float(idx_y));
849862
idx_x = (idx_x + 1) & width_bits;
850863
sum += v.x;
851864
}
@@ -861,15 +874,16 @@ __global__ void readTexels(int n, float *d_out, int width) {
861874
///
862875
/// <param name="n"> An int to process. </param>
863876
/// <param name="d_out"> [in,out] If non-null, the out. </param>
877+
/// <param name="texA"> The texture Object. </param>
864878
////////////////////////////////////////////////////////////////////////////////////////////////////
865879

866-
__global__ void readTexelsInCache(int n, float *d_out) {
880+
__global__ void readTexelsInCache(int n, float *d_out, cudaTextureObject_t texA) {
867881
int idx_x = (blockIdx.x * blockDim.x) + threadIdx.x;
868882
int idx_y = (blockIdx.y * blockDim.y) + threadIdx.y;
869883
int out_idx = idx_y * gridDim.x + idx_x;
870884
float sum = 0.0f;
871885
for (int i = 0; i < n; i++) {
872-
float4 v = tex2D(texA, float(idx_x), float(idx_y));
886+
float4 v = tex2D<float4>(texA, float(idx_x), float(idx_y));
873887
sum += v.x;
874888
}
875889
d_out[out_idx] = sum;
@@ -886,17 +900,18 @@ __global__ void readTexelsInCache(int n, float *d_out) {
886900
/// <param name="d_out"> [in,out] If non-null, the out. </param>
887901
/// <param name="width"> The width. </param>
888902
/// <param name="height"> The height. </param>
903+
/// <param name="texA"> The texture Object. </param>
889904
////////////////////////////////////////////////////////////////////////////////////////////////////
890905

891-
__global__ void readTexelsRandom(int n, float *d_out, int width, int height) {
906+
__global__ void readTexelsRandom(int n, float *d_out, int width, int height, cudaTextureObject_t texA) {
892907
int idx_x = (blockIdx.x * blockDim.x) + threadIdx.x;
893908
int idx_y = (blockIdx.y * blockDim.y) + threadIdx.y;
894909
int out_idx = idx_y * gridDim.x + idx_x;
895910
float sum = 0.0f;
896911
int width_bits = width - 1;
897912
int height_bits = height - 1;
898913
for (int i = 0; i < n; i++) {
899-
float4 v = tex2D(texA, float(idx_x), float(idx_y));
914+
float4 v = tex2D<float4>(texA, float(idx_x), float(idx_y));
900915
idx_x = (idx_x * 3 + 29) & (width_bits);
901916
idx_y = (idx_y * 5 + 11) & (height_bits);
902917
sum += v.x;

0 commit comments

Comments
 (0)