Converting default Float type to Float32

I have noticed on my system that whenever I define CuArrays, I get Float64 as default. Is there a way to change the default to Float32, i.e. without specifying the type every time I define CuArrays? I want to get Float32, say, when I am using GPU.

Can you give an example of what you mean by default? In Julia, floating point literals are 64-bit. Is that causing the “default” behavior you observe or something else?

1 Like

For instance, if I just define

A = CuArray([Inf for i = 1:20])
typeof(A) gives
CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}

unless I do
A=CuArray{Float32}([Inf for i = 1:20])
typeof(A) gives
CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}

I am looking for a way to do
A = CuArray([Inf for i = 1:20])
so that
typeof(A) gives
CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}

Thank you for the response. Sorry, I am new here and I am not sure how I can convert to code formats.

If you want Float32 literals you can enter them in scientific notation with f instead of e. For the special case of Inf and NaN there are 32-bit versions with a 32 suffix.

julia> typeof(3.2f0)
Float32

julia> typeof(Inf32)
Float32

julia> typeof(NaN32)
Float32
4 Likes

@GunnarFarneback already answered your question, but to be more explicit, the chain of events you need to understand is the following:

  1. Inf is a Float64 value.
  2. [Inf for i = 1:20] creates an Array{Float64, 1} since each item in the array is a Float64 value.
  3. CuArray([Inf for i = 1:20]) creates a CuArray{Float64, 1, CUDA.Mem.DeviceBuffer} since it’s taking in an Array{Float64, 1}.

Sorry, I am new here

You don’t ever need to apologize for being new.

4 Likes

You might be looking for cu, which copies a Vector{Float64} to a CuArray{Float32, 1, ...}, changing the element type on the assumption that this was just the default, and the GPU default should be Float32 (as Float64 is usually much much slower there).

3 Likes

I am not aware of the cu , I would be grateful if you have the link to the same or some minimal working example. Thank you.

Thank you everyone for the great suggestions. Just for the reference if anyone should have a similar question in the future I just found out that a similar question was asked on here on the stackoverflow with satisfactory answers.