# small bench accessing global mem vs shared

**URL:** <https://forums.developer.nvidia.com/t/small-bench-accessing-global-mem-vs-shared/19935>\
**Category:** CUDA Programming and Performance\
**Created:** [November 24, 2010, 5:15pm UTC](https://forums.developer.nvidia.com/t/small-bench-accessing-global-mem-vs-shared/19935 "2010-11-24T17:15:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitsu\_DunDee](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Mitsu\_DunDee](https://forums.developer.nvidia.com/u/Mitsu_DunDee)\
**Post date:** [November 24, 2010, 5:15pm UTC](https://forums.developer.nvidia.com/t/small-bench-accessing-global-mem-vs-shared/19935/1 "2010-11-24T17:15:42Z")

</div>

Hi, I am starting cuda programming on a quadro FX 2700M on a hp notebook,  
after reading some tutorials and some cuda sdk, i’ve tested the following code (  
here is just the important func). The obtained results are puzzling me.  
The 3rd approach obviously gives the best results (on my computer at least) but the  
high price operation are : 1 load from global and 1 write to global, that is the very same  
load/write than the number 1 and 2 approaches.

Any explanation ?  
Regards.

# _RESULTS_

# = Testing complex conjugate =

# = approach1 gives : 97.6781

# = approach2 gives : 97.5815

# = approach3 gives : 21.838

# = approach4 gives : 813.004

_CODE_  
void testConjugate(int argc, char\*\* argv){  
// set timers  
double time\_spent\_std = 0.0;  
double time\_spent\_special = 0.0;  
double time\_spent\_special2 = 0.0;  
double time\_spent\_special3 = 0.0;  
unsigned int timer\_std;  
unsigned int timer\_special;  
unsigned int timer\_special2;  
unsigned int timer\_special3;  
cutilCheckError(cutCreateTimer(&timer\_std));  
cutilCheckError(cutCreateTimer(&timer\_special));  
cutilCheckError(cutCreateTimer(&timer\_special2));  
cutilCheckError(cutCreateTimer(&timer\_special3));  
cutilCheckError(cutResetTimer(timer\_std));  
cutilCheckError(cutResetTimer(timer\_special));  
cutilCheckError(cutResetTimer(timer\_special2));  
cutilCheckError(cutResetTimer(timer\_special3));

unsigned int dim = 128;  
unsigned int xp\_number = 10;  
unsigned int lim = 20;

cufftComplex \*tmp1\_var;

// alloc KERNEL CONJUGATES ON DEVICE  
cutilSafeCall(cudaMalloc( (void \*\*) &tmp1\_var, dim_dim_dim\*sizeof(cufftComplex)));

for( unsigned int j = 0; j \< xp\_number; ++j) {

```
/////////////////////////////////////////////////////
// method 1
cutStartTimer(timer_std);  
for (unsigned int i = 0; i < lim ; ++i) {
  //compute spectral conjugate
  ComplexConjugate<<<4096,512>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_std);
time_spent_std += cutGetTimerValue(timer_std);
cutResetTimer(timer_std);
cutilCheckMsg("Something went wrong");
/////////////////////////////////////////////////////
// method 2   
cutStartTimer(timer_special);  
for (unsigned int i = 0; i < lim; ++i) {
  //compute spectral conjugate
  ComplexConjugatev2<<<4096,512>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_special);
time_spent_special += cutGetTimerValue(timer_special);
cutResetTimer(timer_special);

cutilCheckMsg("Something went wrong");
/////////////////////////////////////////////////////
// method 3
cutStartTimer(timer_special2);  
for (unsigned int i = 0; i < lim; ++i) {
  //compute spectral conjugate
  ComplexConjugatev3<<<4*4096,512/4>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_special2);
time_spent_special2 += cutGetTimerValue(timer_special2);
cutResetTimer(timer_special2);
cutilCheckMsg("Something went wrong");
/////////////////////////////////////////////////////
// method 4
cutStartTimer(timer_special3);  
for (unsigned int i = 0; i < lim; ++i) {
  //compute spectral conjugate
  ComplexConjugatev4<<<4*4096,128>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_special3);
time_spent_special3 += cutGetTimerValue(timer_special3);
cutResetTimer(timer_special3);
cutilCheckMsg("Something went wrong");

```

}

std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< “= Testing complex conjugate =” \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach1 gives : " \<\< time\_spent\_std/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach2 gives : " \<\< time\_spent\_special/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach3 gives : " \<\< time\_spent\_special2/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach4 gives : " \<\< time\_spent\_special3/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;

cudaFree(tmp1\_var);

}

// Complex conjugate  
static **global** void ComplexConjugate(Complex\* a)  
{  
const int threadID = blockIdx.x \* blockDim.x + threadIdx.x;  
a[threadID].y = - a[threadID].y;  
}

// Complex conjugate  
static **global** void ComplexConjugatev2(Complex\* a)  
{  
const int threadID = blockIdx.x \* blockDim.x + threadIdx.x;  
a[threadID].y \*= -1.0;  
}

static **global** void ComplexConjugatev3(Complex\* a)  
{  
const int threadID = threadIdx.x;  
**shared** cufftComplex tab[512];  
tab[threadID] = a[blockIdx.x \* blockDim.x + threadID];  
tab[threadID].y \*= - 1.0;  
a[blockIdx.x \* blockDim.x + threadID] = tab[threadID];  
}

static **global** void ComplexConjugatev4(Complex\* a)  
{  
const int threadID = threadIdx.x;  
**shared** cufftComplex tab[128];  
if ( threadID == 0 ){  
for( unsigned int i = 0; i \< 128; ++i) {  
tab[i] = a[blockIdx.x \* blockDim.x + i];  
}  
}  
\_\_syncthreads();

tab[threadID].y \*= - 1.0;  
\_\_syncthreads();

if ( threadID == 0) {  
for( unsigned int j = 0; j \< 128; ++j) {  
a[blockIdx.x \* blockDim.x + j] = tab[j];  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Mitsu\_DunDee](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Mitsu\_DunDee](https://forums.developer.nvidia.com/u/Mitsu_DunDee)\
**Post date:** [November 24, 2010, 5:15pm UTC](https://forums.developer.nvidia.com/t/small-bench-accessing-global-mem-vs-shared/19935/2 "2010-11-24T17:15:42Z")

</div>

Hi, I am starting cuda programming on a quadro FX 2700M on a hp notebook,  
after reading some tutorials and some cuda sdk, i’ve tested the following code (  
here is just the important func). The obtained results are puzzling me.  
The 3rd approach obviously gives the best results (on my computer at least) but the  
high price operation are : 1 load from global and 1 write to global, that is the very same  
load/write than the number 1 and 2 approaches.

Any explanation ?  
Regards.

# _RESULTS_

# = Testing complex conjugate =

# = approach1 gives : 97.6781

# = approach2 gives : 97.5815

# = approach3 gives : 21.838

# = approach4 gives : 813.004

_CODE_  
void testConjugate(int argc, char\*\* argv){  
// set timers  
double time\_spent\_std = 0.0;  
double time\_spent\_special = 0.0;  
double time\_spent\_special2 = 0.0;  
double time\_spent\_special3 = 0.0;  
unsigned int timer\_std;  
unsigned int timer\_special;  
unsigned int timer\_special2;  
unsigned int timer\_special3;  
cutilCheckError(cutCreateTimer(&timer\_std));  
cutilCheckError(cutCreateTimer(&timer\_special));  
cutilCheckError(cutCreateTimer(&timer\_special2));  
cutilCheckError(cutCreateTimer(&timer\_special3));  
cutilCheckError(cutResetTimer(timer\_std));  
cutilCheckError(cutResetTimer(timer\_special));  
cutilCheckError(cutResetTimer(timer\_special2));  
cutilCheckError(cutResetTimer(timer\_special3));

unsigned int dim = 128;  
unsigned int xp\_number = 10;  
unsigned int lim = 20;

cufftComplex \*tmp1\_var;

// alloc KERNEL CONJUGATES ON DEVICE  
cutilSafeCall(cudaMalloc( (void \*\*) &tmp1\_var, dim_dim_dim\*sizeof(cufftComplex)));

for( unsigned int j = 0; j \< xp\_number; ++j) {

```
/////////////////////////////////////////////////////
// method 1
cutStartTimer(timer_std);  
for (unsigned int i = 0; i < lim ; ++i) {
  //compute spectral conjugate
  ComplexConjugate<<<4096,512>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_std);
time_spent_std += cutGetTimerValue(timer_std);
cutResetTimer(timer_std);
cutilCheckMsg("Something went wrong");
/////////////////////////////////////////////////////
// method 2   
cutStartTimer(timer_special);  
for (unsigned int i = 0; i < lim; ++i) {
  //compute spectral conjugate
  ComplexConjugatev2<<<4096,512>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_special);
time_spent_special += cutGetTimerValue(timer_special);
cutResetTimer(timer_special);

cutilCheckMsg("Something went wrong");
/////////////////////////////////////////////////////
// method 3
cutStartTimer(timer_special2);  
for (unsigned int i = 0; i < lim; ++i) {
  //compute spectral conjugate
  ComplexConjugatev3<<<4*4096,512/4>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_special2);
time_spent_special2 += cutGetTimerValue(timer_special2);
cutResetTimer(timer_special2);
cutilCheckMsg("Something went wrong");
/////////////////////////////////////////////////////
// method 4
cutStartTimer(timer_special3);  
for (unsigned int i = 0; i < lim; ++i) {
  //compute spectral conjugate
  ComplexConjugatev4<<<4*4096,128>>> (tmp1_var);
}

cudaThreadSynchronize();
cutStopTimer(timer_special3);
time_spent_special3 += cutGetTimerValue(timer_special3);
cutResetTimer(timer_special3);
cutilCheckMsg("Something went wrong");

```

}

std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< “= Testing complex conjugate =” \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach1 gives : " \<\< time\_spent\_std/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach2 gives : " \<\< time\_spent\_special/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach3 gives : " \<\< time\_spent\_special2/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;  
std::cout \<\< "= approach4 gives : " \<\< time\_spent\_special3/((double) xp\_number) \<\< std::endl;  
std::cout \<\< “=============================” \<\< std::endl;

cudaFree(tmp1\_var);

}

// Complex conjugate  
static **global** void ComplexConjugate(Complex\* a)  
{  
const int threadID = blockIdx.x \* blockDim.x + threadIdx.x;  
a[threadID].y = - a[threadID].y;  
}

// Complex conjugate  
static **global** void ComplexConjugatev2(Complex\* a)  
{  
const int threadID = blockIdx.x \* blockDim.x + threadIdx.x;  
a[threadID].y \*= -1.0;  
}

static **global** void ComplexConjugatev3(Complex\* a)  
{  
const int threadID = threadIdx.x;  
**shared** cufftComplex tab[512];  
tab[threadID] = a[blockIdx.x \* blockDim.x + threadID];  
tab[threadID].y \*= - 1.0;  
a[blockIdx.x \* blockDim.x + threadID] = tab[threadID];  
}

static **global** void ComplexConjugatev4(Complex\* a)  
{  
const int threadID = threadIdx.x;  
**shared** cufftComplex tab[128];  
if ( threadID == 0 ){  
for( unsigned int i = 0; i \< 128; ++i) {  
tab[i] = a[blockIdx.x \* blockDim.x + i];  
}  
}  
\_\_syncthreads();

tab[threadID].y \*= - 1.0;  
\_\_syncthreads();

if ( threadID == 0) {  
for( unsigned int j = 0; j \< 128; ++j) {  
a[blockIdx.x \* blockDim.x + j] = tab[j];  
}  
}  
}
