# Dealing with optional CUDA in package development

**URL:** <https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981>\
**Category:** General Usage\
**Tags:** package, gpu, cuda\
**Created:** [July 20, 2017, 6:16pm UTC](https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981 "2017-07-20T18:16:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pearcemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pearcemc/32/3109_2.png) [@pearcemc](https://discourse.julialang.org/u/pearcemc)\
**Post date:** [July 20, 2017, 6:16pm UTC](https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981/1 "2017-07-20T18:16:55Z")

</div>

I’m developing a package with both GPU routines and a CPU fall-back. I have two related queries:

1. How should I best determine if a CUDA device is available?
2. What would be the best way to avoid forcing CUDA dependencies on a user without a GPU?

My current solution would be to define the CUDA-requiring functions conditionally on readstring(`deviceQuery`) not failing. This feels a bit dirty though - is there a better way?

Thoughts much appreciated.

---

<div class="post-metadata">

**Author:** ![adamslc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adamslc/32/3452_2.png) [@adamslc](https://discourse.julialang.org/u/adamslc)\
**Post date:** [July 20, 2017, 6:23pm UTC](https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981/2 "2017-07-20T18:23:12Z")

</div>

Take a look at this:  
[https://github.com/timholy/ComputationalResources.jl](https://github.com/timholy/ComputationalResources.jl)

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 20, 2017, 10:49pm UTC](https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981/3 "2017-07-20T22:49:27Z")

</div>

@tim.holy I was checking the package the other day, and from what I understood we still don’t have a way to let’s say pass in an option `GPU=true` to a function and get the code branching automatically for the user, correct? Please correct if I am wrong, but with `ComputationalResources.jl` we expect the users of our packages to type in the resources they want to use explicitly.

Given that the users of my package don’t have a clue of what CPUThreads, ArrayFire, etc. means, I decided to live with no precompilation in ImageQuilting.jl.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [July 21, 2017, 12:25am UTC](https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981/4 "2017-07-21T00:25:32Z")

</div>

I don’t think this needs to affect precompilation; that’s more an issue for Requires.jl.

ComputationalResources.jl is more about controlling dispatch in a way that lets you (in combination with Requires.jl) write implementations for, e.g., GPUs without introducing hard dependencies. If you don’t need compile-time dispatch, of course you can use a keyword + Requires.jl rather than ComputationalResources.jl + Requires.jl.

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [July 21, 2017, 1:08am UTC](https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981/5 "2017-07-21T01:08:37Z")

</div>

@tim.holy does Requires.jl solves the issue with optional dependencies and precompilation? In my case, I have this function: [https://github.com/juliohm/ImageQuilting.jl/blob/master/src/utils.jl#L19-L25](https://github.com/juliohm/ImageQuilting.jl/blob/master/src/utils.jl#L19-L25)

If OpenCL and CLFFT are installed, I can pass in the option `gpu=true` and everything works. Could I just wrap the entire GPU-related source code in a @require block and be able to activate precompilation?

---

<div class="post-metadata">

**Author:** ![pearcemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pearcemc/32/3109_2.png) [@pearcemc](https://discourse.julialang.org/u/pearcemc)\
**Post date:** [July 21, 2017, 10:20am UTC](https://discourse.julialang.org/t/dealing-with-optional-cuda-in-package-development/4981/6 "2017-07-21T10:20:49Z")

</div>

`Requires.jl` looks interesting for my purposes. Might give it a go based on requiring `CUDArt`. Thanks,
