# @distributed macro in Float64 instead of Float32

**URL:** <https://discourse.julialang.org/t/distributed-macro-in-float64-instead-of-float32/28476>\
**Category:** General Usage\
**Tags:** distributed\
**Created:** [September 6, 2019, 4:11pm UTC](https://discourse.julialang.org/t/distributed-macro-in-float64-instead-of-float32/28476 "2019-09-06T16:11:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [September 6, 2019, 4:11pm UTC](https://discourse.julialang.org/t/distributed-macro-in-float64-instead-of-float32/28476/1 "2019-09-06T16:11:20Z")

</div>

I have the following code in Julia 1.1:

```julia
using Pkg; Pkg.activate(".")
using Distributed
using Distributions
addprocs(3, exeflags="--project=.")
@everywhere using Distributions

samples=map(x->sample(1:5,5),1:5)
prop=(@distributed (+) for j=1:length(samples)
    Int32.(samples[j].==samples[j]')
end) / length(samples)

```

How can I ensure that `prop` comes in `Float32` format? Specifying `Int32` inside of the loop isn’t enough. I tried

```julia
prop=Float32.((@distributed (+) for j=1:length(samples)
    Int32.(samples[j].==samples[j]')
end) / length(samples))

```

but it doesn’t work… I get the error `syntax: expected ")"`.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 7, 2019, 9:34am UTC](https://discourse.julialang.org/t/distributed-macro-in-float64-instead-of-float32/28476/2 "2019-09-07T09:34:13Z")

</div>

I think that the simplest way to achieve what you want is the following:

```julia
prop = (@distributed (+) for j=1:length(samples)
        Int32.(samples[j].==samples[j]')
        end) / Float32(length(samples))

```

which yields:

```julia
5×5 Array{Float32,2}:
 1.0 0.2 0.0 0.2 0.4
 0.2 1.0 0.2 0.2 0.4
 0.0 0.2 1.0 0.0 0.0
 0.2 0.2 0.0 1.0 0.0
 0.4 0.4 0.0 0.0 1.0

```

  

* * *
  

For a more detailed answer, the solution you tried is essentially equivalent to:

```julia
julia> num = @distributed (+) for j=1:length(samples)
           Int32.(samples[j].==samples[j]')
       end
5×5 Array{Int32,2}:
 5 1 0 1 2
 1 5 1 1 2
 0 1 5 0 0
 1 1 0 5 0
 2 2 0 0 5

julia> prop=Float32.(num / length(samples))
5×5 Array{Float32,2}:
 1.0 0.2 0.0 0.2 0.4
 0.2 1.0 0.2 0.2 0.4
 0.0 0.2 1.0 0.0 0.0
 0.2 0.2 0.0 1.0 0.0
 0.4 0.4 0.0 0.0 1.0

```

This does work, but:

- `num` is an array of `Int32` and `length(samples)` is an `Int64` so that their quotient is promoted to `Float64` by default, and then converted manually back to `Float32`. This is a bit wasteful IMHO (but it probably does not really matter; it’s more a question of principles)

- if you put this computation in one expression (like you tried) instead of splitting it into two (like I did above), you get a syntax error because the parser gets confused between macro call syntax (`@mymacro arg1 arg2`) and function call syntax (`myfunction(arg1, arg2)`). Everything could be made to work in one expression by using explicit parentheses for the macro call as well:

```julia
prop = Float32.(
         @distributed(+, for j=1:length(samples)
                           Int32.(samples[j].==samples[j]')
                         end) / length(samples))

```

---

<div class="post-metadata">

**Author:** ![sergevic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergevic/32/9958_2.png) [@sergevic](https://discourse.julialang.org/u/sergevic)\
**Post date:** [September 7, 2019, 6:23pm UTC](https://discourse.julialang.org/t/distributed-macro-in-float64-instead-of-float32/28476/3 "2019-09-07T18:23:20Z")

</div>

Thank you! I just misplaced the `Float32` then 🙂 Your detailed explanation is also a treasure! In my original version, I had the 2 codes separated: one for the sum and one for the division. I tried to combine both and that’s what motivated my question 🙂 You last post explains the `expected ")"` I got 😉
