# Running For loops on GPU

**URL:** <https://discourse.julialang.org/t/running-for-loops-on-gpu/1553>\
**Category:** GPU\
**Tags:** first-steps\
**Created:** [January 17, 2017, 10:44pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553 "2017-01-17T22:44:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![deepakagrawal](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@deepakagrawal](https://discourse.julialang.org/u/deepakagrawal)\
**Post date:** [January 17, 2017, 10:44pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/1 "2017-01-17T22:44:05Z")

</div>

I am trying to do simulation in which

1. I am repeating same function multiple times
2. Changing several parameters

Is there a way I can do any of them via a for loop or some other way on GPU?

I am new to Julia. Any help or guidance will be great.

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [January 18, 2017, 4:09am UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/2 "2017-01-18T04:09:50Z")

</div>

You are asking this question at a very interesting time (for us). Since you are new to Julia this might not be the easiest topic to dive right into and things are still very much under development.

Broadly speaking you have three options if you want to work with GPU programming and Julia currently

1. [OpenCL.jl](https://github.com/JuliaGPU/OpenCL.jl) and [CUDArt.jl](https://github.com/JuliaGPU/CUDArt.jl) provide access to the OpenCL and CUDA environment and you can write kernels in CUDA/OpenCL C and execute them on the GPU, but do the management in Julia
2. [ArrayFire.jl](https://github.com/JuliaComputing/ArrayFire.jl) provides a high-level interface to the ArrayFire library allowing you to run specific functions on the GPU
3. Julia 0.6 comes with the necessary support to compile Julia code directly to the GPU. The GPU compiler is implemented in [CUDAnative.jl](https://github.com/JuliaGPU/CUDAnative.jl)

### CUDAnative.jl and GPUArrays.jl

CUDAnative provides a CUDA-like programming environment but it has a certain set of limitations (due to the fact that GPU programming is quite different from programming for the CPU). To try it out you will need to build Julia v0.6 from source and follow the instructions in the CUDAnative.jl readme.

We eventually aim to provide a higher-level interface in the form of [GPUArrays.jl](https://github.com/JuliaGPU/GPUArrays.jl), but both CUDAnative and GPUArrays are still experimental (although we welcome early adopters).

I hope this gives you a good enough introduction to what you can currently do.

---

<div class="post-metadata">

**Author:** ![Tem\_Pl](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@Tem\_Pl](https://discourse.julialang.org/u/Tem_Pl)\
**Post date:** [January 18, 2017, 2:15pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/3 "2017-01-18T14:15:39Z")

</div>

These seem to provide low level or functional programming. Will it be possible to compile an arbitrary for loop at any point? I think not, due to the GPU programming model and that many loops cannot be parallelized . Is this correct?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 18, 2017, 2:28pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/4 "2017-01-18T14:28:14Z")

</div>

That is correct. Although `programming model` is a bit misleading, because the constraining factor is really the hardware.  
In theory you could compile arbitrary code to the GPU, but obviously no one is really motivated to implement the missing bits when they end up running slower than on the CPU.  
In GPUArrays, I’m working on abstractions, that make it a bit easier to run the body of a for loop on the GPU 😉  
If you have a NVIDIA GPU and quite a bit of patience, we can try to figure things out!

---

<div class="post-metadata">

**Author:** ![deepakagrawal](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@deepakagrawal](https://discourse.julialang.org/u/deepakagrawal)\
**Post date:** [January 18, 2017, 2:51pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/5 "2017-01-18T14:51:12Z")

</div>

I like this idea. and I have both 😛

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 18, 2017, 2:56pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/6 "2017-01-18T14:56:40Z")

</div>

Damn it! 😃

Well first you need to get CUDAnative running.  
Install instructions are in the README [https://github.com/JuliaGPU/CUDAnative.jl](https://github.com/JuliaGPU/CUDAnative.jl) .  
If that works, feel free to open a documentation issue on GPUArrays, and I will try to respond to that 😉

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [January 18, 2017, 3:04pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/7 "2017-01-18T15:04:54Z")

</div>

> [@sdanisch](#):
>
> Well first you need to get CUDAnative running.Install instructions are in the README [GitHub - JuliaGPU/CUDAnative.jl: Julia support for native CUDA programming](https://github.com/JuliaGPU/CUDAnative.jl) .

I’ve updated the README, nowadays it just involves building julia/master from source and using the package manager. Expect a blogpost with more details very soon (say, next week).

---

<div class="post-metadata">

**Author:** ![deepakagrawal](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@deepakagrawal](https://discourse.julialang.org/u/deepakagrawal)\
**Post date:** [January 18, 2017, 9:25pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/8 "2017-01-18T21:25:56Z")

</div>

Can you guide me on how to build julia 0.6? If the steps are similar to 0.5. Where can I find the source code. The github version seems to be 0.5

Also will I need to build llvm separately or I can do that by Pkg.add(“llvm”)?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [January 18, 2017, 11:11pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/9 "2017-01-18T23:11:18Z")

</div>

On what platform are you? The build steps should be documented in Julia’s README, even how to get the source.

---

<div class="post-metadata">

**Author:** ![deepakagrawal](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@deepakagrawal](https://discourse.julialang.org/u/deepakagrawal)\
**Post date:** [January 19, 2017, 2:44am UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/10 "2017-01-19T02:44:27Z")

</div>

Thank you. I was doing git checkout 0.5. which converted the version to 0.6.

I read it carefully this time. Thank you very much. CUDAnative installed and tested. Passed.

---

<div class="post-metadata">

**Author:** ![AlgoQ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algoq/32/27312_2.png) [@AlgoQ](https://discourse.julialang.org/u/AlgoQ)\
**Post date:** [July 19, 2021, 2:45pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/11 "2021-07-19T14:45:35Z")

</div>

Hi, are there any better options for this case now? I’m also new to Julia 😅 .  
I’m willing to create my own basic backtesting framework where I do the same computation multiple times but with different parameters.

---

<div class="post-metadata">

**Author:** ![Akatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akatz/32/15164_2.png) [@Akatz](https://discourse.julialang.org/u/Akatz)\
**Post date:** [July 19, 2021, 5:39pm UTC](https://discourse.julialang.org/t/running-for-loops-on-gpu/1553/12 "2021-07-19T17:39:41Z")

</div>

Welcome! See:

> **[GitHub - JuliaGPU/KernelAbstractions.jl: Heterogeneous programming in Julia](https://github.com/JuliaGPU/KernelAbstractions.jl)**
>
> Heterogeneous programming in Julia. Contribute to JuliaGPU/KernelAbstractions.jl development by creating an account on GitHub.

and tullio.jl

Both will be covered in tomorrow’s JuliaCon [GPU workshop](https://juliacon.org/2021/workshops/)
