# Multiplication takes large time?

**URL:** <https://forums.developer.nvidia.com/t/multiplication-takes-large-time/9616>\
**Category:** CUDA Programming and Performance\
**Created:** [May 21, 2009, 9:43am UTC](https://forums.developer.nvidia.com/t/multiplication-takes-large-time/9616 "2009-05-21T09:43:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manjunath\_Gudisi](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Manjunath\_Gudisi](https://forums.developer.nvidia.com/u/Manjunath_Gudisi)\
**Post date:** [May 21, 2009, 9:43am UTC](https://forums.developer.nvidia.com/t/multiplication-takes-large-time/9616/1 "2009-05-21T09:43:03Z")

</div>

Hi All,

```auto
#define d_coeff 1024

__global__ void foo( unsigned char* array_1, unsigned char* array_2 )

{

	long k = blockDim.x * blockIdx.x + threadIdx.x;

	

	if(k < 4320000 )

	{

		int ind = 0;

		// some operations here. ind is updated here.

		ind = array_2[k];

		

		unsigned short int coeff = 0;

		 coeff = array_2[ind << 8];

		for( int i = 0; i < 3; i++ )

		{

			int p_value = ( __mul24( array_1[i], coeff ) + d_coeff ) >> 8;

			array_1[i] = (unsigned char)p_value;

		}

	}

}

```

this kernel is called as:

foo\<\<\<8438, 512\>\>\>(array\_1, array\_2);

and array\_1 and array\_2 are of size 1728000.

Here whole execution time for this function is **14ms**. If I comment **array\_1[i]\* coeff** in

inner **for** loop then executon time is **4ms**.

My questions are :

(1)Does mutiplication take so much time?

(2)If Yes, then how to reduce the execution time of this multiplication?

```
I have used <b>__mul24(array_1[i],coeff)</b> instead of <b>array_1[i]* coeff</b>, but 

didnot get any improvement in execution time time.

```

---

<div class="post-metadata">

**Author:** ![x248](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@x248](https://forums.developer.nvidia.com/u/x248)\
**Post date:** [May 21, 2009, 10:01am UTC](https://forums.developer.nvidia.com/t/multiplication-takes-large-time/9616/2 "2009-05-21T10:01:28Z")

</div>

> [@](#):
>
> Hi All,
> 
> ```auto
> #define d_coeff 1024
> 
> __global__ void foo( unsigned char* array_1, unsigned char* array_2 )
> 
> {
> 
> long k = blockDim.x * blockIdx.x + threadIdx.x;
> 
> 	
> 
> if(k < 4320000 )
> 
> {
> 
> int ind = 0;
> 
> // some operations here. ind is updated here.
> 
> ind = array_2[k];
> 
> 		
> 
> unsigned short int coeff = 0;
> 
> coeff = array_2[ind << 8];
> 
> for( int i = 0; i < 3; i++ )
> 
> {
> 
> int p_value = ( __mul24( array_1[i], coeff ) + d_coeff ) >> 8;
> 
> array_1[i] = (unsigned char)p_value;
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> this kernel is called as:
> 
> foo\<\<\<8438, 512\>\>\>(array\_1, array\_2);
> 
> and array\_1 and array\_2 are of size 1728000.
> 
> Here whole execution time for this function is **14ms**. If I comment **array\_1[i]\* coeff** in
> 
> inner **for** loop then executon time is **4ms**.
> 
> My questions are :
> 
> (1)Does mutiplication take so much time?
> 
> (2)If Yes, then how to reduce the execution time of this multiplication?
> 
> ```
> I have used <b>__mul24(array_1[i],coeff)</b> instead of <b>array_1[i]* coeff</b>, but 
> 
> didnot get any improvement in execution time time.
> 
> ```

You are using array.

see http://forums.nvidia.com/index.php?showtopic=97222&st=20

with array you are in local memory and not the register.

Try wihout array, and you will see the performance…

---

<div class="post-metadata">

**Author:** ![Jamie\_K](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Jamie\_K](https://forums.developer.nvidia.com/u/Jamie_K)\
**Post date:** [May 21, 2009, 2:03pm UTC](https://forums.developer.nvidia.com/t/multiplication-takes-large-time/9616/3 "2009-05-21T14:03:53Z")

</div>

You also have an obvious race condition on array\_1. It won’t hurt performance but your program will not work correctly. Your main problem is you are in the wrong line of work.
