# Accelerate computations using GPU?

**URL:** <https://community.wolfram.com/t/accelerate-computations-using-gpu/11618>\
**Category:** Questions\
**Tags:** mathematica, graphics-and-visualization, high-performance-computing\
**Created:** [December 3, 2017, 1:43am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618 "2017-12-03T01:43:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![raphaelpetrovich](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/raphaelpetrovich/32/40560_2.png) [@raphaelpetrovich](https://community.wolfram.com/u/raphaelpetrovich)\
**Post date:** [December 3, 2017, 1:43am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/1 "2017-12-03T01:43:49Z")

</div>

Hi,

I am trying to accelerate computation using GPU. I started with the textbook example

```
Needs["CUDALink`"]

ListLinePlot[
 Thread[List[CUDAFoldList[Plus, 0, RandomReal[{-1, 1}, 500000]], 
   CUDAFoldList[Plus, 0, RandomReal[{-1, 1}, 500000]]]]]

```

This code should use GPU to accelerate the computation.  
Then for comparison, I tried to generate similar result with

```
ListLinePlot[
 Thread[List[FoldList[Plus, 0, RandomReal[{-1, 1}, 500000]], 
   FoldList[Plus, 0, RandomReal[{-1, 1}, 500000]]]]]

```

In both cases it took about 4 sec. to finish the computation i.e. there was no significant difference in time required to get the result. Why?

---

<div class="post-metadata">

**Author:** ![kapioletto](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kapioletto/32/10262_2.png) [@kapioletto](https://community.wolfram.com/u/kapioletto)\
**Post date:** [December 3, 2017, 6:17am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/2 "2017-12-03T06:17:22Z")

</div>

Why did not you show the timing code? Are you using `AbsoluteTiming`? Are you also timing ( unnecessarily ) `ListLinePlot`?

---

<div class="post-metadata">

**Author:** ![raphaelpetrovich](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/raphaelpetrovich/32/40560_2.png) [@raphaelpetrovich](https://community.wolfram.com/u/raphaelpetrovich)\
**Post date:** [December 3, 2017, 6:25am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/3 "2017-12-03T06:25:45Z")

</div>

I have the timing displayed on my window. You can do that by following the link below.

> **[Display the Timing of an Evaluation in a Notebook Window—Wolfram Documentation](https://reference.wolfram.com/language/howto/DisplayTheTimingOfAnEvaluationInANotebookWindow.html)**
>
> The time it takes the Wolfram Language to perform a computation is important information that can help you write efficient programs. Conveniently, you can display the time elapsed for your most recent computation in the lower-left corner of your...

---

<div class="post-metadata">

**Author:** ![kapioletto](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kapioletto/32/10262_2.png) [@kapioletto](https://community.wolfram.com/u/kapioletto)\
**Post date:** [December 3, 2017, 6:31am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/4 "2017-12-03T06:31:05Z")

</div>

Then yes indeed you timed `ListLinePlot` and `Thread ` which you should not have done, as you should time only parallelized computation. Timings on window are not useful on the forum as you cannot post actual numbers. Please use `AbsoluteTiming` around only parallelized code and post actual times. Also click “Reply” to a specific post so responses are nested.

---

<div class="post-metadata">

**Author:** ![raphaelpetrovich](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/raphaelpetrovich/32/40560_2.png) [@raphaelpetrovich](https://community.wolfram.com/u/raphaelpetrovich)\
**Post date:** [December 3, 2017, 6:47am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/5 "2017-12-03T06:47:37Z")

</div>

OK, so the GPU acceleration in the above example is applied only for generating the random reals and not for showing them on the plot.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [December 3, 2017, 12:05pm UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/6 "2017-12-03T12:05:14Z")

</div>

Why do you think that anything else than `CUDAFoldList` itself would run on the GPU? It’s the only function you are using with CUDA in its name.

The random numbers are not generated on the GPU. Only the FoldList operation runs there. I can’t test on a GPU, but with a list of that size, the operation should take a tiny fraction of a second even on a CPU (0.04 s on my machine if I replace `0` with `0.` as the starting value).

Accurate benchmarking is difficult, but for best results try to ensure that the calculation takes at least on the order of 0.1-1 seconds and use `AbsouteTiming`.

---

<div class="post-metadata">

**Author:** ![raphaelpetrovich](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/raphaelpetrovich/32/40560_2.png) [@raphaelpetrovich](https://community.wolfram.com/u/raphaelpetrovich)\
**Post date:** [December 3, 2017, 6:48pm UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/7 "2017-12-03T18:48:19Z")

</div>

Thanks. Is there a way to use GPU acceleration for

```
 RandomFunction and ParallelTable 

```

?

---

<div class="post-metadata">

**Author:** ![raphaelpetrovich](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/raphaelpetrovich/32/40560_2.png) [@raphaelpetrovich](https://community.wolfram.com/u/raphaelpetrovich)\
**Post date:** [December 4, 2017, 4:48am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/8 "2017-12-04T04:48:36Z")

</div>

I was unaware that most of the computation time in the above example was spent on generating the graph and not for FoldList operation, this caused the confusion.

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/szhorvat/32/3531_2.png) [@szhorvat](https://community.wolfram.com/u/szhorvat)\
**Post date:** [December 4, 2017, 11:58am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/9 "2017-12-04T11:58:01Z")

</div>

I do not think there is a way to do this, unless you write the code from scratch in C. This seems to be the main purpose of CUDALink and OpenCLLink: send your data (packed arrays) to the GPU, run code on them that you developed separately in C (not in Mathematica), copy the result back.

Currently, there is no functionality to run general Mathematica code on the GPU. Even those functions that _appear_ general, such as `CUDAFoldList`, are in reality restricted to a few specific applications: it can only take Max, Min, Plus, Minus, or Times.

In principle, it should be possible to have a restricted version of Table run on the GPU. Currently, Mathematica can’t do this. Let’s see if the new compiler framework brings improvements here.

---

<div class="post-metadata">

**Author:** ![raphaelpetrovich](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/raphaelpetrovich/32/40560_2.png) [@raphaelpetrovich](https://community.wolfram.com/u/raphaelpetrovich)\
**Post date:** [December 7, 2017, 4:16am UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/10 "2017-12-07T04:16:27Z")

</div>

That is sad. Thanks for the information.

---

<div class="post-metadata">

**Author:** ![anon21378091](https://avatars.discourse-cdn.com/v4/letter/a/bcef8e/32.png) [@anon21378091](https://community.wolfram.com/u/anon21378091)\
**Post date:** [October 19, 2021, 4:26pm UTC](https://community.wolfram.com/t/accelerate-computations-using-gpu/11618/11 "2021-10-19T16:26:58Z")

</div>

Hello Szabolcs,

RE: “In principle, it should be possible to have a restricted version of Table run on the GPU. Currently, Mathematica can’t do this. Let’s see if the new compiler framework brings improvements here.”

A question: I am not a heavy compiler user but should be; did you get the impression from WTC2021 that things have developed in this respect? Or is it something we need to go find out? Thanks if you can answer.
