# cuda is really slow - even when doing nothing

**URL:** <https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596>\
**Category:** CUDA Programming and Performance\
**Created:** [September 2, 2010, 11:16pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596 "2010-09-02T23:16:35Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![curiousj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@curiousj](https://forums.developer.nvidia.com/u/curiousj)\
**Post date:** [September 2, 2010, 11:16pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/1 "2010-09-02T23:16:35Z")

</div>

After first try cuda to do some kernel computing, I found it is really slow, so I do a little funny testing as below,

float\* m = new float[1000];  
float\* n = new float[1000];  
for(int i=0; i\<200; i++)  
{  
float _md;  
float_ nd;  
cudaMalloc( (void\*\*) &md, 1000 \* sizeof(float));  
cudaMalloc( (void\*\*) &nd, 1000 \* sizeof(float));  
cudaMemcpy(md, m, 1000 \* sizeof(float), cudaMemcpyHostToDevice);  
cudaMemcpy(nd, n, 1000 \* sizeof(float), cudaMemcpyHostToDevice);  
cudaFree(md);  
cudaFree(nd);  
}

This code does no meaningful thing, even does not kernel computing, just copies memory and frees it. Then I use nvcc to compile, run it, it takes 7 seconds. If I do similiar thing in complete host code, malloc and free, it takes no time. My video card is Quadro FX 4000. Not a great one, but it cannot be so slow.

The same thing happens to any of my C code, as long as I rename it to .cu and compile with nvcc, run it, it will be significantly slower than the executable created by visual studio 2008. I expect it will have same speed because they are executed on CPU only.

Any ideas?

---

<div class="post-metadata">

**Author:** ![ONeill](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ONeill](https://forums.developer.nvidia.com/u/ONeill)\
**Post date:** [September 3, 2010, 7:55am UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/2 "2010-09-03T07:55:01Z")

</div>

Lol @ topic title :)

---

<div class="post-metadata">

**Author:** ![ONeill](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@ONeill](https://forums.developer.nvidia.com/u/ONeill)\
**Post date:** [September 3, 2010, 7:55am UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/3 "2010-09-03T07:55:01Z")

</div>

Lol @ topic title :)

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [September 3, 2010, 8:14am UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/4 "2010-09-03T08:14:51Z")

</div>

Would this be on Windows Vista or 7 ?

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [September 3, 2010, 8:14am UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/5 "2010-09-03T08:14:51Z")

</div>

Would this be on Windows Vista or 7 ?

---

<div class="post-metadata">

**Author:** ![dgwsoft](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dgwsoft](https://forums.developer.nvidia.com/u/dgwsoft)\
**Post date:** [September 3, 2010, 1:19pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/6 "2010-09-03T13:19:41Z")

</div>

are you sure you are running on your graphics card not in emulation mode?

Have you installed the SDK and compiled the examples? If so run deviceQuery and see if it reports your card or the emulator device.

Also, there is always some overhead in initialising the CUDA h/w and s/w, especially the first time after a reboot. Not 7 seconds but a

noticeable fraction of a second on the machines I have used. And copying data is an overhead. So for a very small amount of work, yes it is slower on the GPU than on the CPU.

Remember a GPU is only of benefit for specific highly parallelizable problems, it is not just a faster CPU. You need to run a realistic kernel, e.g. the matrix multiplication example, to see the advantages.

> [@](#):
>
> After first try cuda to do some kernel computing, I found it is really slow, so I do a little funny testing as below,
> 
> float\* m = new float[1000];
> 
> float\* n = new float[1000];
> 
> for(int i=0; i\<200; i++)
> 
> {
> 
> float \*md;
> 
> float\* nd;
> 
> cudaMalloc( (void\*\*) &md, 1000 \* sizeof(float));
> 
> cudaMalloc( (void\*\*) &nd, 1000 \* sizeof(float));
> 
> cudaMemcpy(md, m, 1000 \* sizeof(float), cudaMemcpyHostToDevice);
> 
> cudaMemcpy(nd, n, 1000 \* sizeof(float), cudaMemcpyHostToDevice);
> 
> cudaFree(md);
> 
> cudaFree(nd);
> 
> }
> 
> This code does no meaningful thing, even does not kernel computing, just copies memory and frees it. Then I use nvcc to compile, run it, it takes 7 seconds. If I do similiar thing in complete host code, malloc and free, it takes no time. My video card is Quadro FX 4000. Not a great one, but it cannot be so slow.
> 
> The same thing happens to any of my C code, as long as I rename it to .cu and compile with nvcc, run it, it will be significantly slower than the executable created by visual studio 2008. I expect it will have same speed because they are executed on CPU only.
> 
> Any ideas?

---

<div class="post-metadata">

**Author:** ![dgwsoft](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dgwsoft](https://forums.developer.nvidia.com/u/dgwsoft)\
**Post date:** [September 3, 2010, 1:19pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/7 "2010-09-03T13:19:41Z")

</div>

are you sure you are running on your graphics card not in emulation mode?

Have you installed the SDK and compiled the examples? If so run deviceQuery and see if it reports your card or the emulator device.

Also, there is always some overhead in initialising the CUDA h/w and s/w, especially the first time after a reboot. Not 7 seconds but a

noticeable fraction of a second on the machines I have used. And copying data is an overhead. So for a very small amount of work, yes it is slower on the GPU than on the CPU.

Remember a GPU is only of benefit for specific highly parallelizable problems, it is not just a faster CPU. You need to run a realistic kernel, e.g. the matrix multiplication example, to see the advantages.

> [@](#):
>
> After first try cuda to do some kernel computing, I found it is really slow, so I do a little funny testing as below,
> 
> float\* m = new float[1000];
> 
> float\* n = new float[1000];
> 
> for(int i=0; i\<200; i++)
> 
> {
> 
> float \*md;
> 
> float\* nd;
> 
> cudaMalloc( (void\*\*) &md, 1000 \* sizeof(float));
> 
> cudaMalloc( (void\*\*) &nd, 1000 \* sizeof(float));
> 
> cudaMemcpy(md, m, 1000 \* sizeof(float), cudaMemcpyHostToDevice);
> 
> cudaMemcpy(nd, n, 1000 \* sizeof(float), cudaMemcpyHostToDevice);
> 
> cudaFree(md);
> 
> cudaFree(nd);
> 
> }
> 
> This code does no meaningful thing, even does not kernel computing, just copies memory and frees it. Then I use nvcc to compile, run it, it takes 7 seconds. If I do similiar thing in complete host code, malloc and free, it takes no time. My video card is Quadro FX 4000. Not a great one, but it cannot be so slow.
> 
> The same thing happens to any of my C code, as long as I rename it to .cu and compile with nvcc, run it, it will be significantly slower than the executable created by visual studio 2008. I expect it will have same speed because they are executed on CPU only.
> 
> Any ideas?

---

<div class="post-metadata">

**Author:** ![curiousj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@curiousj](https://forums.developer.nvidia.com/u/curiousj)\
**Post date:** [September 3, 2010, 3:10pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/8 "2010-09-03T15:10:37Z")

</div>

I am sure it is running on GPU instead of CPU. It is on Windows XP 32bit. But I figured out what the problem is.  
cudaMalloc is a really expensive job. My code looks doing nothing, just malloc and free for 200 times. It is indeed really slow, hundreds or thousands times slower than CPU/RAM malloc/free. If I just pre-malloc before the loop and then reuse the same cuda memory in the loop, it will be much faster.

---

<div class="post-metadata">

**Author:** ![curiousj](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@curiousj](https://forums.developer.nvidia.com/u/curiousj)\
**Post date:** [September 3, 2010, 3:10pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/9 "2010-09-03T15:10:37Z")

</div>

I am sure it is running on GPU instead of CPU. It is on Windows XP 32bit. But I figured out what the problem is.  
cudaMalloc is a really expensive job. My code looks doing nothing, just malloc and free for 200 times. It is indeed really slow, hundreds or thousands times slower than CPU/RAM malloc/free. If I just pre-malloc before the loop and then reuse the same cuda memory in the loop, it will be much faster.

---

<div class="post-metadata">

**Author:** ![dgwsoft](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dgwsoft](https://forums.developer.nvidia.com/u/dgwsoft)\
**Post date:** [September 3, 2010, 4:55pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/10 "2010-09-03T16:55:37Z")

</div>

> [@](#):
>
> I am sure it is running on GPU instead of CPU. It is on Windows XP 32bit. But I figured out what the problem is.
> 
> cudaMalloc is a really expensive job. My code looks doing nothing, just malloc and free for 200 times. It is indeed really slow, hundreds or thousands times slower than CPU/RAM malloc/free. If I just pre-malloc before the loop and then reuse the same cuda memory in the loop, it will be much faster.

I find it useful to put timers around operations such as cudaMalloc, cudaMemcpy and calls to the kernel, to find out what is really taking all the time. Here is a little class I wrote to help out. (This is for linux - it would need modification for windows).

[font=“Courier New”]

class Timer

{

public:

```
Timer() : clocks_per_sec(sysconf(_SC_CLK_TCK)) { start(); }

void start() { m_start = times(0); }

void stop() { m_stop = times(0); }

double read() const { return double(m_stop - m_start)/clocks_per_sec; }

```

private:

```
long clocks_per_sec;

clock_t m_start, m_stop;

```

};

std::ostream& operator\<\<(std::ostream &os, const Timer& timer)

{

```
os.setf(std::ios_base::fixed, std::ios_base::floatfield);

os << std::setprecision(2) << timer.read();

os.setf((std::_Ios_Fmtflags)0, std::ios_base::floatfield);

return os;

```

}

[/font]

and use like

[font=“Courier New”]Timer t1;

// whatever code

t1.stop();

cout \<\< “\*\*\* whatever code took " \<\< t1 \<\< " seconds” \<\< std::endl;

[/font]

---

<div class="post-metadata">

**Author:** ![dgwsoft](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@dgwsoft](https://forums.developer.nvidia.com/u/dgwsoft)\
**Post date:** [September 3, 2010, 4:55pm UTC](https://forums.developer.nvidia.com/t/cuda-is-really-slow-even-when-doing-nothing/18596/11 "2010-09-03T16:55:37Z")

</div>

> [@](#):
>
> I am sure it is running on GPU instead of CPU. It is on Windows XP 32bit. But I figured out what the problem is.
> 
> cudaMalloc is a really expensive job. My code looks doing nothing, just malloc and free for 200 times. It is indeed really slow, hundreds or thousands times slower than CPU/RAM malloc/free. If I just pre-malloc before the loop and then reuse the same cuda memory in the loop, it will be much faster.

I find it useful to put timers around operations such as cudaMalloc, cudaMemcpy and calls to the kernel, to find out what is really taking all the time. Here is a little class I wrote to help out. (This is for linux - it would need modification for windows).

[font=“Courier New”]

class Timer

{

public:

```
Timer() : clocks_per_sec(sysconf(_SC_CLK_TCK)) { start(); }

void start() { m_start = times(0); }

void stop() { m_stop = times(0); }

double read() const { return double(m_stop - m_start)/clocks_per_sec; }

```

private:

```
long clocks_per_sec;

clock_t m_start, m_stop;

```

};

std::ostream& operator\<\<(std::ostream &os, const Timer& timer)

{

```
os.setf(std::ios_base::fixed, std::ios_base::floatfield);

os << std::setprecision(2) << timer.read();

os.setf((std::_Ios_Fmtflags)0, std::ios_base::floatfield);

return os;

```

}

[/font]

and use like

[font=“Courier New”]Timer t1;

// whatever code

t1.stop();

cout \<\< “\*\*\* whatever code took " \<\< t1 \<\< " seconds” \<\< std::endl;

[/font]
