# my speedy SGEMM

**URL:** <https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584>\
**Category:** CUDA Programming and Performance\
**Created:** [October 5, 2007, 11:50pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584 "2007-10-05T23:50:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [October 5, 2007, 11:50pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/1 "2007-10-05T23:50:31Z")

</div>

I’ve tried implementing a matrix multiply that’s as fast as I could make it. On my 8600GT OC, it gets 38 Glfops vs 23 with CUBLAS. Could somebody who has an 8800GTX get some numbers?

I tried very hard to optimize it, and experimented with many techniques. I’m afraid, however, that further improvement is unlikely without direct access to cubin. Minor changes give rather chaotic swings in performance (up-down by 30-200%), even with ptxas run at -O0. Turning on ptxas optimizations usually hurts performance.

---

<div class="post-metadata">

**Author:** ![e.ping](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@e.ping](https://forums.developer.nvidia.com/u/e.ping)\
**Post date:** [October 6, 2007, 12:28am UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/2 "2007-10-06T00:28:13Z")

</div>

> [@](#):
>
> Could somebody who has an 8800GTX get some numbers?
> 
> [snapback]261174[/snapback]

1. Nice Alex!

-dh

---

<div class="post-metadata">

**Author:** ![vvolkov](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vvolkov](https://forums.developer.nvidia.com/u/vvolkov)\
**Post date:** [October 6, 2007, 4:14pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/3 "2007-10-06T16:14:17Z")

</div>

I get 137.9 Gflop/s on 8800 GTX. CUBLAS runs at 120.

---

<div class="post-metadata">

**Author:** ![nutti](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nutti](https://forums.developer.nvidia.com/u/nutti)\
**Post date:** [October 7, 2007, 7:41am UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/4 "2007-10-07T07:41:18Z")

</div>

> [@](#):
>
> Could somebody who has an 8800GTX get some numbers?

I get 171.68 Gflops, using a 8800 GTX ULTRA.

Nice work!

---

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [October 7, 2007, 5:34pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/5 "2007-10-07T17:34:09Z")

</div>

> [@](#):
>
> I get 171.68 Gflops, using a 8800 GTX ULTRA.
> 
> Nice work!
> 
> [snapback]261572[/snapback]

wow! how can such a difference be explained?

see, something about the compiiling/optimizing is just so chaotic.

---

<div class="post-metadata">

**Author:** ![e.ping](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@e.ping](https://forums.developer.nvidia.com/u/e.ping)\
**Post date:** [October 7, 2007, 5:44pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/6 "2007-10-07T17:44:50Z")

</div>

> [@](#):
>
> wow! how can such a difference be explained?
> 
> see, something about the compiiling/optimizing is just so chaotic.
> 
> [snapback]261705[/snapback]

I doubt he even recompiled, probably used your binary.

This is a nice example how your excellent program scaled, without recompiling, across the 8-series family with different numbers of processors and different clocks.

The Ultra has faster shader, memory clocks and bandwidth than the GTX, so the 137 to 171 is just straight clock scaling.

---

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [October 7, 2007, 8:39pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/7 "2007-10-07T20:39:49Z")

</div>

> [@](#):
>
> I doubt he even recompiled, probably used your binary.
> 
> This is a nice example how your excellent program scaled, without recompiling, across the 8-series family with different numbers of processors and different clocks.
> 
> The Ultra has faster shader, memory clocks and bandwidth&nbsp; than the GTX, so the 137 to 171 is just straight clock scaling.
> 
> [snapback]261711[/snapback]

172/138 = 25%, while the ultra has 6.5% faster shaders and 20% memory. something else must be going on.

btw, the code gets recompiled automatically because the executable only has cm\_10 ptx and sm\_11 cubin embedded (it’s an artifact of my optimizing).

---

<div class="post-metadata">

**Author:** ![seb](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seb/32/151566_2.png) [@seb](https://forums.developer.nvidia.com/u/seb)\
**Post date:** [October 7, 2007, 9:14pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/8 "2007-10-07T21:14:54Z")

</div>

I’ve also got the Ultra.

When I don’t recompile the project (just launch the binary in the Release folder) I get 155 GFlops. Recompiling the Project in Release Mode gives me the same number.

What’s going on, why is my card slower?  
I use it not only for the computation but it’s also my display adapter so it also renders my desktop. Might this impact performance?

Btw, nice work Alex!

---

<div class="post-metadata">

**Author:** ![vvolkov](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vvolkov](https://forums.developer.nvidia.com/u/vvolkov)\
**Post date:** [October 7, 2007, 9:39pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/9 "2007-10-07T21:39:27Z")

</div>

So, this code is 65% faster than CUBLAS when run on 8600GT OC, but only 14% faster on 8800 GTX. I wonder how it compares to CUBLAS on 8800 GTX Ultra. Any info?

Also, I am not sure that the timing used in this code is fair. I’d call cudaThreadSynchronize() after each kernel invocation, inside the loop. If you invoke kernel again before previous pass is not over, it may go wrong. This could be the cause for the 171 vs 155 case.

---

<div class="post-metadata">

**Author:** ![paulius](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@paulius](https://forums.developer.nvidia.com/u/paulius)\
**Post date:** [October 8, 2007, 5:22pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/10 "2007-10-08T17:22:39Z")

</div>

> [@](#):
>
> …
> 
> Also, I am not sure that the timing used in this code is fair. I’d call cudaThreadSynchronize() after each kernel invocation, inside the loop. If you invoke kernel again before previous pass is not over, it may go wrong. This could be the cause for the 171 vs 155 case.
> 
> [snapback]261801[/snapback]

Calling cudaThreadSynchronize() in CUDA 1.0 after each kernel call isn’t necessary. Subsequent kernel calls get serialized by the driver. So, it suffices to call cudaThreadSynchronize() once, after the timing loop.

Paulius

---

<div class="post-metadata">

**Author:** ![mfatica](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mfatica](https://forums.developer.nvidia.com/u/mfatica)\
**Post date:** [October 9, 2007, 5:11am UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/11 "2007-10-09T05:11:41Z")

</div>

Alex,  
nice code optimization but what you have coded is not a real SGEMM.  
SGEMM performs C=alpha_A_B+beta\*C.

CUBLAS achieves 120Gflops in CUDA 1.0 for SGEMM and it will improve in the upcoming release.

---

<div class="post-metadata">

**Author:** ![vvolkov](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vvolkov](https://forums.developer.nvidia.com/u/vvolkov)\
**Post date:** [October 9, 2007, 11:29am UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/12 "2007-10-09T11:29:01Z")

</div>

> [@](#):
>
> Calling cudaThreadSynchronize() in CUDA 1.0 after each kernel call isn’t necessary.&nbsp; Subsequent kernel calls get serialized by the driver.&nbsp; So, it suffices to call cudaThreadSynchronize() once, after the timing loop.
> 
> Paulius
> 
> [snapback]262084[/snapback]

This contradicts with my observations with CUDA 1.0.

To be more specific, I took the bandwidth benchmark posted in another thread ([The Official NVIDIA Forums | NVIDIA](http://forums.nvidia.com/index.php?showtopic=44152&view=findpost&p=246974)), and inserted cutGetTimerValue() in the timing loop to measure the time taken by each iteration. First ~20 iterations take ~13 us each which is just the kernel invocation overhead, later iterations take ~2.9 ms each which is the expected time and is the same as when using cudaThreadSynchronize().

---

<div class="post-metadata">

**Author:** ![seb](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/seb/32/151566_2.png) [@seb](https://forums.developer.nvidia.com/u/seb)\
**Post date:** [October 9, 2007, 4:06pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/13 "2007-10-09T16:06:33Z")

</div>

> [@](#):
>
> This contradicts with my observations with CUDA 1.0.
> 
> To be more specific, I took the bandwidth benchmark posted in another thread ([The Official NVIDIA Forums | NVIDIA](http://forums.nvidia.com/index.php?showtopic=44152&view=findpost&p=246974)), and inserted cutGetTimerValue() in the timing loop to measure the time taken by each iteration. First ~20 iterations take ~13 us each which is just the kernel invocation overhead, later iterations take ~2.9 ms each which is the expected time and is the same as when using cudaThreadSynchronize().[snapback]262461[/snapback]

There seems to be a misunderstanding. When you want to time each kernel run you have to make sure the kernel finished. You do that by calling cudaThreadSynchronize().

What I think Paulius is saying is that you can call say 10 kernels without the cudaThreadSynchronize and CUDA will queue the calls and execute them one by one in a FIFO manner (I guess). You don’t have to call cudaThreadSynchronize() between each kernel invocation to make sure it is executed properly etc. You just have to call it once right before you take the time to make sure everything is finished. That’s if you time more kernels per run.

So you are right: basically I think one can say before taking the time there should be a call to cudaThreadSynchronize(). I think it would actually make sense to implicitly invoke cudaThreadSynchronize() functionality when calling cutGetTimerValue(). So no one would get confused. A lot of people have measured wrong times because of the obscure asynchronous behavior of CUDA, including myself.

---

<div class="post-metadata">

**Author:** ![vvolkov](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@vvolkov](https://forums.developer.nvidia.com/u/vvolkov)\
**Post date:** [October 9, 2007, 4:30pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/14 "2007-10-09T16:30:43Z")

</div>

OK, i’ve got it. They are queued. And in my case I get 10 kernel calls in a queue.

I though Paulius meant that there is an implicit call to cudaThreadSynchronize() before each kernel invocation.

Thanks!

---

<div class="post-metadata">

**Author:** ![alex\_dubinsky](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@alex\_dubinsky](https://forums.developer.nvidia.com/u/alex_dubinsky)\
**Post date:** [October 9, 2007, 10:08pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/15 "2007-10-09T22:08:39Z")

</div>

> [@](#):
>
> Alex,
> 
> nice code optimization but what you have coded is not a real SGEMM.
> 
> SGEMM performs C=alpha_A_B+beta\*C.
> 
> CUBLAS achieves 120Gflops in CUDA 1.0 for SGEMM and it will improve in the upcoming release.
> 
> [snapback]262369[/snapback]

You’re right. A real sgemm includes alpha and beta, and supports various transpose modes. Alphas and betas are trivial, but the transpose modes will require more work. In a proper implementation, however, the extra features will have little effect on performance (especially when they’re not used).

I’ll work on expanding my function to be a full sgemm.

---

<div class="post-metadata">

**Author:** ![CBD](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@CBD](https://forums.developer.nvidia.com/u/CBD)\
**Post date:** [November 12, 2007, 6:30pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/16 "2007-11-12T18:30:12Z")

</div>

> [@](#):
>
> Alex,
> 
> nice code optimization but what you have coded is not a real SGEMM.
> 
> SGEMM performs C=alpha_A_B+beta\*C.
> 
> CUBLAS achieves 120Gflops in CUDA 1.0 for SGEMM and it will improve in the upcoming release.
> 
> [snapback]262369[/snapback]

I have an innocent question about SGEMM. You mention 120GFlops. Is this sustained or is there a performance hit when the constants are changed ?

Obviously there will be a drop in performance for a short time but what I’m interested in is aggregate computation speed (ie over many operations) and I’m trying to ascertain how often I could change the constants on-the-fly without a major penalty (cache flushes etc).

Thanks.

---

<div class="post-metadata">

**Author:** ![mfatica](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mfatica](https://forums.developer.nvidia.com/u/mfatica)\
**Post date:** [November 12, 2007, 6:39pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/17 "2007-11-12T18:39:29Z")

</div>

This is sustained on the card for any alpha and beta.

---

<div class="post-metadata">

**Author:** ![BeauPaisley](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@BeauPaisley](https://forums.developer.nvidia.com/u/BeauPaisley)\
**Post date:** [November 20, 2007, 11:41pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/18 "2007-11-20T23:41:19Z")

</div>

> [@](#):
>
> This is sustained on the card for any alpha and beta.
> 
> [snapback]278520[/snapback]

We ran a CUBLAS/sgemm test on an 8500GT and get about 5gflops which is less than the 8 gflops that intel MKL provides on the cpu (3gHz duo core).

We got this rather low-end card just to try out cublas and see if it was feasible for using instead of/in addition to MKL blas in our application. From these tests it would not be fruitful. Can we expect that stepping up to something like an 8800 will provide significant gains? Is it probably that the gap between cublas performance and mkl blas will widen in future cublas releases and future nvidia card generations?

Our users are not graphics types so we can’t expect for our prospects to typically have 8800 level and beyond boards for some time so we are still trying to evaluate if perhaps it is too soon to supplement routines such as sggemm/fft in our applications with cuda equivalents.

Beau Paisley

[www.omatrix.com](http://www.omatrix.com)

---

<div class="post-metadata">

**Author:** ![mfatica](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mfatica](https://forums.developer.nvidia.com/u/mfatica)\
**Post date:** [November 21, 2007, 12:51am UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/19 "2007-11-21T00:51:11Z")

</div>

The 8500GT has 16 processors clocked at 900Mhz and a 128bit memory interface. I am impressed that it is achieving 8 Gflops… :-)

If you are looking for a good entry level card (around $250), try the 8800GT. It has 112 processors clocked at 1.5Ghz and a 256bit memory interface.

Could you tell us what was the size of the matrices?

---

<div class="post-metadata">

**Author:** ![e.ping](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@e.ping](https://forums.developer.nvidia.com/u/e.ping)\
**Post date:** [December 5, 2007, 5:24pm UTC](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584/20 "2007-12-05T17:24:48Z")

</div>

I am trying to run the code on a Quadro FX 5600 but under Linux and I am not sure how to compile it.  
I used the standard Makefile that was provided for the matrixMul example but the performance I  
get is max ~110 Gflop/s (with CUBLAS I get 120 Gflop/s). Can somebody help with this. Thanks,  
Stan

[Next page](https://forums.developer.nvidia.com/t/my-speedy-sgemm/1584.md?page=2)
