Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve readability of some assigments #551

Closed
wants to merge 2 commits into from
Closed

Conversation

Sbozzolo
Copy link
Member

@Sbozzolo Sbozzolo commented Oct 7, 2024

A pattern like

map!(x -> x, view(flux_up_sw, :, gcol), view(flux_up, :, gcol))

is quite hard to understand for what it does. It takes a couple of minutes to understand that it is just an assignment and it equivalent to

flux_up_sw[:, gcol] = flux_up[:, gcol]

I removed these instances and replaced them with a clear assignment.

On CPU, this is 1 % slower, but I think that well worth for the added readability. (It is also not clear if has any impact in the overall performance)

julia> @benchmark map!(x -> x, view(A, :, 1), view(B, :, 1))
BenchmarkTools.Trial: 10000 samples with 6 evaluations.
 Range (min … max):  4.966 μs …  12.451 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.037 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.063 μs ± 141.562 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

     ▂▇█▅▁
  ▁▂▅█████▆▄▃▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  4.97 μs         Histogram: frequency by time        5.69 μs <

 Memory estimate: 128 bytes, allocs estimate: 4.

julia> @benchmark view(A, :, 1) .= view(B, :, 1)
BenchmarkTools.Trial: 10000 samples with 6 evaluations.
 Range (min … max):  4.913 μs …  13.299 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.086 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.121 μs ± 194.785 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

        ▄▇██▇▅▃▁                                              ▂
  ▅▃▃▁▄████████████▇▆▄▃▄▅▆▇▇▆▆▆▆▇▆▇▇▆▅▆▆▆▆▇▇▅▆▅▅▆▅▆▆▅▆▇▄▆▆▅▄▆ █
  4.91 μs      Histogram: log(frequency) by time      6.04 μs <

 Memory estimate: 224 bytes, allocs estimate: 6.

Prior to this commit, `flux_dn_dir` was not set in the two stream case
for non-gray radiation, leading to garbage values (whatever was in the
memory at init). Now, the variable is accumulated over for ever g-point.
A pattern like
```
map!(x -> x, view(flux_up_sw, :, gcol), view(flux_up, :, gcol))
```
is quite hard to understand for what it does. It takes a couple of
minutes to understand that it is just an assignment and it equivalent to
```
flux_up_sw[:, gcol] = flux_up[:, gcol]
```

I removed these instances and replaced them with a clear assignment.

On CPU, this is 1 % slower, but I think that well worth for the added
readability.

```
julia> @benchmark map!(x -> x, view(A, :, 1), view(B, :, 1))
BenchmarkTools.Trial: 10000 samples with 6 evaluations.
 Range (min … max):  4.966 μs …  12.451 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.037 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.063 μs ± 141.562 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

     ▂▇█▅▁
  ▁▂▅█████▆▄▃▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  4.97 μs         Histogram: frequency by time        5.69 μs <

 Memory estimate: 128 bytes, allocs estimate: 4.

julia> @benchmark view(A, :, 1) .= view(B, :, 1)
BenchmarkTools.Trial: 10000 samples with 6 evaluations.
 Range (min … max):  4.913 μs …  13.299 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     5.086 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.121 μs ± 194.785 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

        ▄▇██▇▅▃▁                                              ▂
  ▅▃▃▁▄████████████▇▆▄▃▄▅▆▇▇▆▆▆▆▇▆▇▇▆▅▆▆▆▆▇▇▅▆▅▅▆▅▆▆▅▆▇▄▆▆▅▄▆ █
  4.91 μs      Histogram: log(frequency) by time      6.04 μs <

 Memory estimate: 224 bytes, allocs estimate: 6.
```
@sriharshakandala
Copy link
Member

@Sbozzolo : These have already been merged in PR #538
This is duplication of effort. Its best to just add back the reverted PRs at the earliest!

@Sbozzolo
Copy link
Member Author

Sbozzolo commented Oct 7, 2024

@Sbozzolo : These have already been merged in PR #538 This is duplication of effort. Its best to just add back the reverted PRs at the earliest!

Ok!

@Sbozzolo Sbozzolo closed this Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants