13

In general, can Mathematica automatically (i.e. without writing code specifically for this) exploit GPU hardware and/or parallelize built-in operations across multiple cores?

For example, for drawing a single very CPU-intensive plot or solving a very CPU-intensive equation, would upgrading the graphics hardware result in speed-up? Would upgrading to a CPU with more cores speed things up? (I realize that more cores mean I could solve more equations in parallel but I'm curious about the single-equation case)

Just trying to get a handle on how Mathematica exploits hardware.

8
  • 1
    I would imagine it depends upon the operation and if it utilizes the GPU, and in what way. I would daresay that most compound operations do not utilize the GPU, as GPU programming is significantly different from CPU programming. For instance, a FFT fits well into a GPU model, but a set of equations that can't be dealt with via linear algebra is likely an entirely different story. Commented Dec 26, 2011 at 20:31
  • I guess I'm asking if built-in Mathematica functions use the GPU... Commented Dec 26, 2011 at 20:32
  • You stated/implied that it does .. missing a word? :) The Mathematica formums/brochure would be the places to find that out: definitely not here, unless the question was about writing such operations. Commented Dec 26, 2011 at 20:33
  • my short experience with doing basic simulations in M, is that the computation in M is VERY fast, but what seems to me to slow things, is the rendering of plots and graphics. So anything you can do to optimize this part (smarter way of making plots/graphics, using options such as PerformanceGoal->"Speed" and MaxPlotPoints and many other things like this would help. So I would imagine a faster graphics card, would help. As for other aspects, M can utilize GPU's with CUDA. I do not use this part of it. Commented Dec 26, 2011 at 20:40
  • 1
    @Nasser, much the same for me. Rendering Histograms and DateListPlots is often the rate determining step -- even with PerformanceGoal->"Speed". DateListPlot is slow because date and time functions in Mma are very slow. I'd like to see a 50 times speed improvement to make them competitive with e.g. VBA. Commented Dec 26, 2011 at 22:01

3 Answers 3

15

I wouldn't say Mathematica does automatically GPU or Paralell-CPU computing, at least in general. Since you need do something with paralell kernels, then you should initialize more kernels and/or upload CUDALink or OpenCLLink and use specific Mathematica functionality to exploit the potential of CPU and/or GPU.

For example, I haven't got very powerful graphics card (NVIDIA GeForce 9400 GT) but we can test how CUDALink works. First I have to upload CUDALink :

Needs["CUDALink`"] 

I am going to test multiplication of large matrices. I choose a random matrix 5000 x 5000 of real numbers in range (-1,1) :

M = RandomReal[{-1,1}, {5000, 5000}];

Now we can check the computing times without GPU support

 In[4]:= AbsoluteTiming[ Dot[M,M]; ] Out[4]= {26.3780000, Null} 

and with GPU support

In[5]:= AbsoluteTiming[ CUDADot[M, M]; ] Out[5]= {6.6090000, Null} 

In this case we obtained a performance speed-up roughly of factor 4, by using CUDADot instead of Dot.

Edit

To add an example of parallel CPU acceleration (on a dual-core machine) I choose all prime numbers in range [2^300, 2^300 +10^6]. First without parallelizing :

In[139]:= AbsoluteTiming[ Select[ Range[ 2^300, 2^300 + 10^6], PrimeQ ]; ] Out[139]= {121.0860000, Null} 

while using Parallelize[expr], which evaluates expr using automatic parallelization

In[141]:= AbsoluteTiming[ Parallelize[ Select[ Range[ 2^300, 2^300 + 10^6], PrimeQ ] ]; ] Out[141]= {63.8650000, Null} 

As one could expect we've got almost two times faster evaluation.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Artes, I have a related question. I am getting a new laptop mainly to use with Mathematica. Are you saying that I should focus on CPU only and not put much emphasis on the GPU then?
@Y2H It's a bit too general problem, basically you can get a lot from your GPU nevertheless I'd say it's of secondary importance, first mind the CPU and RAM, even more important will be style of programming in Mathematica.
8

Generally no, a faster GPU will not accelerate normal Mathematica computations.

You must be using Cuda/OpenCL supported functions to use the GPU. You can get an overview of the options and some samples of their use here: CUDA and OpenCL Support.

Comments

4

I can't comment much on how Mathematica uses the GPU (as I never had the chance to try), but I don't believe it does it by default (i.e without you writing code specifically to exploit the GPU)

Adding more cores will help if you explicitly parallelize your calculations (see Parallelize and related functions).

If you don't parallelize explicitly, I believe there are still certain numerical calculations that take advantage of multiple cores. I'm not sure which one, but I do know that some linear algebra related functions (LinearSolve, Det, etc.) use multiple cores by default.

8 Comments

I know some of the image processing operations will also use multiple cores by default.
Solving an eigensystem (dense or sparse) also uses many cores automatically
@acl I tried Eigenvalues on a dual core CPU, but it only used one core. Maybe Eigensystem does but Eigenvalues doesn't (that'd be unusual)? I can't test right now.
So, I just tried With[{upN = 10000, sites = 2000}, sp = SparseArray[Thread[RandomInteger[{1, sites}, {upN, 2}] -> RandomReal[{0, 1}, upN]], {sites, sites}]]; {evals, evecs} = Eigensystem[sp, -500]; (which creates a sparse array and then obtains the eigenvalues and eigenvectors) and it uses both cores of my macbook. I also ran it on one of my office machines via ssh and it uses all 4 cores there (linux).
@Searke The license limits the number of parallel kernels you may start; however, what I am describing is a single mathkernel taking 200% (or 400%). I am not sure if that is limited by licensing or not, and have no way of checking.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.