-4

Assume i have a really basic script, that requires a lot of calculation:

c = 2
result = 0
for i in range(0,10000):
    c += 5
    c = i*c
print(c) //just added this, sorry for confusion!

...takes about 15 seconds in IDLE on my mac book pro. How can I get this exact script to run on the gpu not cpu, for faster results? Also, wondering how code (if at all) would need to change in order to work for gpu?

UPDATE: sorry, meant 15 seconds with the print statement at the end there. Turns out this is a bad example because IDLE executes this unusually slow - just tried in Terminal and it was instant.

harry lakins
  • 803
  • 1
  • 13
  • 28
  • 1
    Surely this does not take 15 seconds? It take me 0.1 s. – jmd_dk Nov 30 '16 at 19:30
  • 1
    I'm not 100% familiar with macbook pro hardware, but perhaps [PyCuda](https://mathema.tician.de/software/pycuda/) could be what you are looking for. – Spencer D Nov 30 '16 at 19:30
  • 7
    I would recommend reading up on (GP)GPU programming in Python as well as [this question](http://stackoverflow.com/questions/5957554/python-gpu-programming#5957647) which focuses on PyCUDA. On a sidenote, I'm somewhat concerned that this program takes 15 seconds to run on your hardware. – Jules Nov 30 '16 at 19:30
  • 2
    This took my Note 4 (a phone) 2.5 seconds, and it's under clocked. I pray for Mac users if this took 15 seconds. – Carcigenicate Nov 30 '16 at 19:34
  • @jmd_dk I'm not sure if it applies here but in python 2 runs some programs that use range must slower than python 3. OP might be using 2 – Whud Nov 30 '16 at 19:39
  • MY MISTAKE. It took 15 seconds to also print it out at the end - without that - under a second! Nonetheless, i appreciate the concerns, and @Carcigenicate, no need for the prays :) – harry lakins Nov 30 '16 at 19:40
  • @harrylakins I'm not sure what Python interpreter you're using, but even with a `print` invocation, it's well under a second for me (but this is off-topic for this question). – Jules Nov 30 '16 at 19:48
  • IDLE - it's really slow. – harry lakins Nov 30 '16 at 19:49
  • Well my quick example really hasn't worked out well!! Your right- in terminal it's much quicker. Hope either way my actual question is understood :) – harry lakins Nov 30 '16 at 19:50

1 Answers1

0

I would recommend looking into using something like gnumpy if you're mainly looking to do simple but repetitive mathematical calculations.

  • Would this mean altering existing code? With my actual projects, which are already up and working, would i have to restructure it to work for gpu? – harry lakins Nov 30 '16 at 19:42
  • @harrylakins while many structures (like loops) can be (in some cases) fairly easily refactored to take advantage of GPU offloading, a more complex code base can require _loads_ of work to benefit from, or even use, a GPU. Again, I recommend you learn at least the fundamentals of the CUDA (or OpenCL if you're using AMD) architecture, even if that requires understanding some CUDA C code. – Jules Nov 30 '16 at 19:51
  • So I'm probably going to have to do quite a lot of code restructuring? My code is a neural network which involves a lot of arithmetic between matrix and list values – harry lakins Nov 30 '16 at 19:53
  • How much restructuring you'll have to do is entirely dependent on the current structure of your code. If your ANN is complex and already fairly complete, this could mean several weeks of dedicated work. – Jules Nov 30 '16 at 19:54
  • Just out of curiosity, in your actual code do you have the print function inside or outside the for loop? I ask because the only way I could replicate this taking 15ish seconds was with the print inside the for loop. It took maybe a second to actually complete the for loop without the print inside it. Printing to the console is really costly for the speed the program runs at because, and correct me if I'm wrong, it won't reiterate through the loop until the text has been printed to the console. – Matt Kyles Nov 30 '16 at 19:56
  • My NN is completely flexible and as a user you can choose it's structure ... so the code is very abstract in a sense (nothing hardcoded). – harry lakins Nov 30 '16 at 19:57
  • Regarding the 15 second code - i am also rather intrigued. I am defiantly printing after (out of) the loop, in IDLE. It takes a good 10 seconds, and freezes up IDLE for a second or two. Whereas in Terminal, it prints instantly. Clearly using IDLE was a bad idea to supply an accurate execution time...! I have no clue why IDLE makes it so much slower. – harry lakins Nov 30 '16 at 19:59