# Performance with memory assigment

**URL:** <https://forums.developer.nvidia.com/t/performance-with-memory-assigment/8161>\
**Category:** CUDA Programming and Performance\
**Created:** [March 7, 2009, 6:44am UTC](https://forums.developer.nvidia.com/t/performance-with-memory-assigment/8161 "2009-03-07T06:44:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fvazquez](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fvazquez](https://forums.developer.nvidia.com/u/fvazquez)\
**Post date:** [March 7, 2009, 6:44am UTC](https://forums.developer.nvidia.com/t/performance-with-memory-assigment/8161/1 "2009-03-07T06:44:41Z")

</div>

Hello, I have a problem with this kernel:

I’m working in a Tesla C870 (compute capability 1.0):

(MAX\_XBLOCK=MAX\_YBLOCK=8)

For example, numVectors=2000, numNeurons=32 and dim=3205.

**global**

void updateUKE(float \*d\_examples,float \*d\_somItems, float \*d\_tmpD,float \*d\_tmpD1,float rr1,

```
                    int numVectors,int numNeurons,int dim)

```

{

int x = blockIdx.x\*MAX\_XBLOCK + threadIdx.x;

int y = blockIdx.y\*MAX\_YBLOCK + threadIdx.y;

if ((x \< numVectors)&&(y \< numNeurons)){

```
  int j;

```

float auxDist=0.0, temp=0.0;

for(j=0;j\<dim;j++) {

```
    auxDist+=powf(d_examples[x*dim+j]-d_somItems[y*dim+j],2);                    

  }

```

d\_tmpD[x\*numNeurons+y]=auxDist;

```
  d_tmpD1[x*numNeurons+y]=-auxDist/rr1;

```

}//if(x\<numVectors)&&(y\<numNeurons)

\_\_syncthreads();

}

The problem is that this kernel is very slow, it spent 0.4 secs but if I change the lines:

d\_tmpD[x\*numNeurons+y]=auxDist;

```
  d_tmpD1[x*numNeurons+y]=-auxDist/rr1;

```

by

d\_tmpD[x\*numNeurons+y]=temp;

```
  d_tmpD1[x*numNeurons+y]=temp;

```

the time now is 0.01 secs. Which is the diference between the register auxDist and temp ?? auxDist is a register that is calculated by each thread before the asignment d\_tmpD[…]=auxDist. I don’t understand this… Can somebody help me??

Thanks, Francisco

---

<div class="post-metadata">

**Author:** ![SPWorley](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@SPWorley](https://forums.developer.nvidia.com/u/SPWorley)\
**Post date:** [March 7, 2009, 7:56am UTC](https://forums.developer.nvidia.com/t/performance-with-memory-assigment/8161/2 "2009-03-07T07:56:34Z")

</div>

It’s doing exactly what it should.

The optimizer is good.

If you replace the lines with temp, the compiler sees that the computation of auxDist isn’t used and therefore isn’t even needed.  
So it removes the auxDist computation entirely, and the kernel runs much faster.

---

<div class="post-metadata">

**Author:** ![diddum](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@diddum](https://forums.developer.nvidia.com/u/diddum)\
**Post date:** [March 7, 2009, 8:38am UTC](https://forums.developer.nvidia.com/t/performance-with-memory-assigment/8161/3 "2009-03-07T08:38:20Z")

</div>

> [@](#):
>
> Hello, I have a problem with this kernel:
> 
> auxDist+=powf(d\_examples[x_dim+j]-d\_somItems[y_dim+j],2);
> 
> The problem is that this kernel is very slow

Well, in the line above you are using the function powf to compute a square.

That’s very bad, because powf is probably 10 times slower than a multiplication.

giovanni

---

<div class="post-metadata">

**Author:** ![fvazquez](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fvazquez](https://forums.developer.nvidia.com/u/fvazquez)\
**Post date:** [March 7, 2009, 8:49am UTC](https://forums.developer.nvidia.com/t/performance-with-memory-assigment/8161/4 "2009-03-07T08:49:52Z")

</div>

> [@](#):
>
> It’s doing exactly what it should.
> 
> The optimizer is good.
> 
> If you replace the lines with temp, the compiler sees that the computation of auxDist isn’t used and therefore isn’t even needed.
> 
> So it removes the auxDist computation entirely, and the kernel runs much faster.

Ok, but i tried the same code in cpu with a secuential loop, that is:

for(i=0;i\<numVectors…

```
 for(j=0;j<numNeurons....

      for(k=0;k<dim......

```

and the compute time is the same that the execution in the gpu… why?? this algorithm can’t be paralelized with cuda??

Thanks,

---

<div class="post-metadata">

**Author:** ![fvazquez](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@fvazquez](https://forums.developer.nvidia.com/u/fvazquez)\
**Post date:** [March 7, 2009, 9:08am UTC](https://forums.developer.nvidia.com/t/performance-with-memory-assigment/8161/5 "2009-03-07T09:08:13Z")

</div>

> [@](#):
>
> Well, in the line above you are using the function powf to compute a square.
> 
> That’s very bad, because powf is probably 10 times slower than a multiplication.
> 
> giovanni

No, with a multiplication the time is the same.

Best regards, Francisco
