# \[beginner\] memory access

**URL:** <https://forums.developer.nvidia.com/t/beginner-memory-access/19505>\
**Category:** CUDA Programming and Performance\
**Created:** [October 31, 2010, 11:48am UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505 "2010-10-31T11:48:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vaglienti](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vaglienti](https://forums.developer.nvidia.com/u/vaglienti)\
**Post date:** [October 31, 2010, 11:48am UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/1 "2010-10-31T11:48:45Z")

</div>

if I put width = 65535 and height = 1 execution time ~ 0.47 sec  
if I put width = 1 and height =65535 execution time ~ 0.06 sec WHY?

and then…also this:

if I put nThreads(16,16) and dimBlockW = (sizeW+15)/16; dimBlockH = (sizeH+15)/16; is more fast than : nThreads(32,16) and dimBlockW = (sizeW+31)/32 dimBlockH = (sizeH+15)/16; WHY? with nThreads(16,16) is not memory access coalescing! or not? [External Image](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/wallbash.gif "Image hosted on another site. Click to open in a new tab.")

for example :

**global** void kernel(float \*dataIn,uchar4 \*dataOut,int w,int h,int step)  
{

```
float data;

int x = (blockIdx.x) * blockDim.x + threadIdx.x;		
int y = (blockIdx.y) * blockDim.y + threadIdx.y;	

if ((x<w) && (y<h))
{
	for (int k=0; k<step; k++)
		  data += __fmul_rn(dataIn[y*w +x],0.2);

	dataOut[y*w +x] = make_uchar4(data,44,44,44);
}

```

}

int main(int argc, char\* argv)  
{

```
float timeCuda,timeHost;

    int sizeW = 65535;

    int sizeH = 1;

    int step = 15000;

    float *dataIn;
uchar4 *dataOut;

cudaMalloc(&dataOut,sizeW*sizeH*sizeof(uchar4));
cudaMalloc(&dataIn,sizeW*sizeH*sizeof(float));

dim3 nThreads(32,16);

int dimBlockW = (sizeW+31)/32;

int dimBlockH = (sizeH+15)/16;

dim3 nBlocks(dimBlockW,dimBlockH);

unsigned int timer = 0;

    printf("\n CUDA start\n");

cutilCheckError( cutCreateTimer( &timer));

cutilCheckError( cutStartTimer( timer));
				
kernel<<< nBlocks, nThreads >>> (dataIn,dataOut,sizeW,sizeH,step);				

cutilSafeCall(cudaThreadSynchronize());
				
cutilCheckError( cutStopTimer( timer));

timeCuda = cutGetTimerValue( timer)/1000;

printf("\n CUDA end time: %f (s)\n", timeCuda);

cutilCheckError( cutDeleteTimer( timer));

cudaFree(dataOut);
cudaFree(dataIn);

```

}

---

<div class="post-metadata">

**Author:** ![vaglienti](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vaglienti](https://forums.developer.nvidia.com/u/vaglienti)\
**Post date:** [October 31, 2010, 11:48am UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/2 "2010-10-31T11:48:45Z")

</div>

if I put width = 65535 and height = 1 execution time ~ 0.47 sec  
if I put width = 1 and height =65535 execution time ~ 0.06 sec WHY?

and then…also this:

if I put nThreads(16,16) and dimBlockW = (sizeW+15)/16; dimBlockH = (sizeH+15)/16; is more fast than : nThreads(32,16) and dimBlockW = (sizeW+31)/32 dimBlockH = (sizeH+15)/16; WHY? with nThreads(16,16) is not memory access coalescing! or not? [External Image](http://hqnveipbwb20/public/style_emoticons/<#EMO_DIR#>/wallbash.gif "Image hosted on another site. Click to open in a new tab.")

for example :

**global** void kernel(float \*dataIn,uchar4 \*dataOut,int w,int h,int step)  
{

```
float data;

int x = (blockIdx.x) * blockDim.x + threadIdx.x;		
int y = (blockIdx.y) * blockDim.y + threadIdx.y;	

if ((x<w) && (y<h))
{
	for (int k=0; k<step; k++)
		  data += __fmul_rn(dataIn[y*w +x],0.2);

	dataOut[y*w +x] = make_uchar4(data,44,44,44);
}

```

}

int main(int argc, char\* argv)  
{

```
float timeCuda,timeHost;

    int sizeW = 65535;

    int sizeH = 1;

    int step = 15000;

    float *dataIn;
uchar4 *dataOut;

cudaMalloc(&dataOut,sizeW*sizeH*sizeof(uchar4));
cudaMalloc(&dataIn,sizeW*sizeH*sizeof(float));

dim3 nThreads(32,16);

int dimBlockW = (sizeW+31)/32;

int dimBlockH = (sizeH+15)/16;

dim3 nBlocks(dimBlockW,dimBlockH);

unsigned int timer = 0;

    printf("\n CUDA start\n");

cutilCheckError( cutCreateTimer( &timer));

cutilCheckError( cutStartTimer( timer));
				
kernel<<< nBlocks, nThreads >>> (dataIn,dataOut,sizeW,sizeH,step);				

cutilSafeCall(cudaThreadSynchronize());
				
cutilCheckError( cutStopTimer( timer));

timeCuda = cutGetTimerValue( timer)/1000;

printf("\n CUDA end time: %f (s)\n", timeCuda);

cutilCheckError( cutDeleteTimer( timer));

cudaFree(dataOut);
cudaFree(dataIn);

```

}

---

<div class="post-metadata">

**Author:** ![LSChien](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LSChien](https://forums.developer.nvidia.com/u/LSChien)\
**Post date:** [November 1, 2010, 1:13pm UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/3 "2010-11-01T13:13:28Z")

</div>

> [@](#):
>
> if I put width = 65535 and height = 1 execution time ~ 0.47 sec
> 
> if I put width = 1 and height =65535 execution time ~ 0.06 sec WHY?

I think this is problem of effective bandwidth.

for example, width = 1, then according to your setting

```auto
int x = (blockIdx.x) * blockDim.x + threadIdx.x; 

int y = (blockIdx.y) * blockDim.y + threadIdx.y;

```

first half-warp is threadIdx.y =0, threadIdx.x = 0,1,2,3,…,15

However only threadIdx.x=0 is not out-of-array bound, that is,

first half-warp loads a cache line to L2 cache, but only one thread fetches the data,

so effective bandwidth is 1/16.

> [@](#):
>
> with nThreads(16,16) is not memory access coalescing

this is not correct, global memory accessing is based on half-warp, even on Fermi.

---

<div class="post-metadata">

**Author:** ![LSChien](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LSChien](https://forums.developer.nvidia.com/u/LSChien)\
**Post date:** [November 1, 2010, 1:13pm UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/4 "2010-11-01T13:13:28Z")

</div>

> [@](#):
>
> if I put width = 65535 and height = 1 execution time ~ 0.47 sec
> 
> if I put width = 1 and height =65535 execution time ~ 0.06 sec WHY?

I think this is problem of effective bandwidth.

for example, width = 1, then according to your setting

```auto
int x = (blockIdx.x) * blockDim.x + threadIdx.x; 

int y = (blockIdx.y) * blockDim.y + threadIdx.y;

```

first half-warp is threadIdx.y =0, threadIdx.x = 0,1,2,3,…,15

However only threadIdx.x=0 is not out-of-array bound, that is,

first half-warp loads a cache line to L2 cache, but only one thread fetches the data,

so effective bandwidth is 1/16.

> [@](#):
>
> with nThreads(16,16) is not memory access coalescing

this is not correct, global memory accessing is based on half-warp, even on Fermi.

---

<div class="post-metadata">

**Author:** ![vaglienti](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vaglienti](https://forums.developer.nvidia.com/u/vaglienti)\
**Post date:** [November 1, 2010, 6:29pm UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/5 "2010-11-01T18:29:41Z")

</div>

I have a gtx 460, and then compute with  
capacity 2.1.  
I read the SDK (with difficulty)  
help me to clarify basic concepts.

A silly example, which only serves as an example to understand how to maximize access to global memory  
(Setting the number of threads per block, grid size for access coalescing):

**global** void kernel (uchar4 dataOut \*, int width, int height)  
{  
float red;

```
int x = (blockIdx.x) * blockDim.x threadIdx.x;
int y = (blockIdx.y) * blockDim.y threadIdx.y;

if ((x <width) & & (y <height))
{
   ....................
    ....................
    calculating the variable "red" that depends on the indices x and y

    ....................
    ....................

    dataOut [y * width x] = make_uchar4 (red, 0,0,255);
}

```

}

int main (int argc, char \* argv )  
{  
int sizeW = atoi (argv [1]); \<------ (any values … so do not necessarily multiples of a number set)

```
int sizeH = atoi (argv [2]); <------ (any values .... so do not necessarily multiples of a number set)

uchar4 *dataOut;

cudaMalloc (& dataOut, sizeW * sizeH * sizeof (uchar4));

dim3 nThreads (?,?);

dim3 nBlocks (?,?);

kernel <<<nBlocks, nThreads>>> (dataOut, sizeW, sizeH);
..............

...............

..................

cudaFree (dataOut);

```

}

then having a video card with compute capacity 2.1 what is the best setting (again for  
coalescence)? What are the values to take the place of question marks? And you can also motivate the choice?

---

<div class="post-metadata">

**Author:** ![vaglienti](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vaglienti](https://forums.developer.nvidia.com/u/vaglienti)\
**Post date:** [November 1, 2010, 6:29pm UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/6 "2010-11-01T18:29:41Z")

</div>

I have a gtx 460, and then compute with  
capacity 2.1.  
I read the SDK (with difficulty)  
help me to clarify basic concepts.

A silly example, which only serves as an example to understand how to maximize access to global memory  
(Setting the number of threads per block, grid size for access coalescing):

**global** void kernel (uchar4 dataOut \*, int width, int height)  
{  
float red;

```
int x = (blockIdx.x) * blockDim.x threadIdx.x;
int y = (blockIdx.y) * blockDim.y threadIdx.y;

if ((x <width) & & (y <height))
{
   ....................
    ....................
    calculating the variable "red" that depends on the indices x and y

    ....................
    ....................

    dataOut [y * width x] = make_uchar4 (red, 0,0,255);
}

```

}

int main (int argc, char \* argv )  
{  
int sizeW = atoi (argv [1]); \<------ (any values … so do not necessarily multiples of a number set)

```
int sizeH = atoi (argv [2]); <------ (any values .... so do not necessarily multiples of a number set)

uchar4 *dataOut;

cudaMalloc (& dataOut, sizeW * sizeH * sizeof (uchar4));

dim3 nThreads (?,?);

dim3 nBlocks (?,?);

kernel <<<nBlocks, nThreads>>> (dataOut, sizeW, sizeH);
..............

...............

..................

cudaFree (dataOut);

```

}

then having a video card with compute capacity 2.1 what is the best setting (again for  
coalescence)? What are the values to take the place of question marks? And you can also motivate the choice?

---

<div class="post-metadata">

**Author:** ![LSChien](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LSChien](https://forums.developer.nvidia.com/u/LSChien)\
**Post date:** [November 3, 2010, 2:03am UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/7 "2010-11-03T02:03:38Z")

</div>

> [@](#):
>
> **global** void kernel (uchar4 dataOut \*, int width, int height)
> 
> {
> 
> ```
> float red;
> 
> int x = (blockIdx.x) * blockDim.x + threadIdx.x;
> 
> int y = (blockIdx.y) * blockDim.y + threadIdx.y;
> 
> if ((x <width) & & (y <height))
> 
> {
> 
> ....................
> 
> ....................
> 
> ```

Suppose that you follow page 8 of programming guide, that is, threadIdx.x sweeps columns of the matrix, and you matrix is row-major, then you should set first element of thread block as multiple of 16, for example, dim block(16,16) or block(32,8) or …

however width of the matrix should be multiple of 16 or much bigger than 16.

---

<div class="post-metadata">

**Author:** ![LSChien](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@LSChien](https://forums.developer.nvidia.com/u/LSChien)\
**Post date:** [November 3, 2010, 2:03am UTC](https://forums.developer.nvidia.com/t/beginner-memory-access/19505/8 "2010-11-03T02:03:38Z")

</div>

> [@](#):
>
> **global** void kernel (uchar4 dataOut \*, int width, int height)
> 
> {
> 
> ```
> float red;
> 
> int x = (blockIdx.x) * blockDim.x + threadIdx.x;
> 
> int y = (blockIdx.y) * blockDim.y + threadIdx.y;
> 
> if ((x <width) & & (y <height))
> 
> {
> 
> ....................
> 
> ....................
> 
> ```

Suppose that you follow page 8 of programming guide, that is, threadIdx.x sweeps columns of the matrix, and you matrix is row-major, then you should set first element of thread block as multiple of 16, for example, dim block(16,16) or block(32,8) or …

however width of the matrix should be multiple of 16 or much bigger than 16.
