Lab 4 Feb 7, 2014

Robert Morehead

1a.

Let's use Eric's code and the crummy code written by the clearly untalented student.


In [13]:
include("CLASS_REPO/Astro585/lab4/HW4_leapfrog.jl")
include("CLASS_REPO/Astro585/lab4/HW4_leapfrog_student.jl")


Out[13]:
test_leapfrog_student (generic function with 2 methods)

1b.

I suspect the memory re-allocation for the log array due to the vcat() calls each loop is what made this so terrible.

1c.


In [14]:
Profile.clear()
for i in 0:50
    @profile orbit = integrate_leapfrog!([1.0,0.0,00.0,1.0],2pi/200,6pi);
    #println("X:",orbit[1][end]," Y:",orbit[2][end]," Vx:",orbit[3][end]," Vy:",orbit[4][end])
end
Profile.print()


WARNING: the profile data buffer is full; profiling probably terminated
before your program finished. To profile for longer runs, call Profile.init()
with a larger buffer and/or larger delay.
1     ...rc/execute_request.jl; execute_request_0x535c5df2; line: 132
  1 loading.jl; include_string; line: 83
      1 no file; anonymous; line: 14
       1 ...lab4/HW4_leapfrog.jl; integrate_leapfrog!; line: 95
        1 deepcopy.jl; deepcopy_internal; line: 45
24    .../lab4/HW4_leapfrog.jl; advance_leapfrog!; line: 46
45    .../lab4/HW4_leapfrog.jl; update_derivs_pos!; line: 6
60    .../lab4/HW4_leapfrog.jl; update_derivs_vel!; line: 18
21    abstractarray.jl; checkbounds; line: 63
27    abstractarray.jl; checkbounds; line: 74
12    abstractarray.jl; checkbounds; line: 95
226   abstractarray.jl; trailingsize; line: 54
9     array.jl; copy!; line: 48
26    array.jl; getindex; line: 342
13    array.jl; setindex!; line: 453
25    array.jl; unsafe_copy!; line: 31
11    array.jl; unsafe_copy!; line: 38
24    deepcopy.jl; _deepcopy_array_t; line: 52
68    deepcopy.jl; deepcopy_internal; line: 45
16    int.jl; <; line: 228
12    operators.jl; >; line: 19
21    range.jl; Range1; line: 27
131   range.jl; colon; line: 38
12    range.jl; maximum; line: 101
5     range.jl; minimum; line: 100
7     reflection.jl; isbits; line: 36
    43235 multi.jl; anonymous; line: 1308
      43235 ...Julia/src/IJulia.jl; eventloop; line: 92
       43235 ...Julia/src/IJulia.jl; eventloop; line: 68
          43235 ...xecute_request.jl; execute_request_0x535c5df2; line: 132
            43235 loading.jl; include_string; line: 83
                6     no file; anonymous; line: 10
                43229 no file; anonymous; line: 14
                 5     ...HW4_leapfrog.jl; integrate_leapfrog!; line: 67
                 8     ...HW4_leapfrog.jl; integrate_leapfrog!; line: 70
                 12    ...HW4_leapfrog.jl; integrate_leapfrog!; line: 73
                 1     ...HW4_leapfrog.jl; integrate_leapfrog!; line: 74
                  1 .../HW4_leapfrog.jl; update_derivs!; line: 36
                 75    ...HW4_leapfrog.jl; integrate_leapfrog!; line: 78
                  4  deepcopy.jl; deepcopy_internal; line: 46
                   2 deepcopy.jl; _deepcopy_array_t; line: 52
                   2 deepcopy.jl; _deepcopy_array_t; line: 53
                    2 array.jl; copy!; line: 51
                     2 array.jl; unsafe_copy!; line: 38
                      2 array.jl; unsafe_copy!; line: 31
                    27 array.jl; setindex!; line: 465
                 8     ...HW4_leapfrog.jl; integrate_leapfrog!; line: 82
                 17    ...HW4_leapfrog.jl; integrate_leapfrog!; line: 84
                 21591 ...HW4_leapfrog.jl; integrate_leapfrog!; line: 87
                       183  ...4_leapfrog.jl; advance_leapfrog!; line: 46
                       58   ...4_leapfrog.jl; advance_leapfrog!; line: 49
                       3987 ...4_leapfrog.jl; advance_leapfrog!; line: 51
                        132  range.jl; colon; line: 38
                         61 range.jl; Range1; line: 27
                         50 range.jl; Range1; line: 28
                       877  ...4_leapfrog.jl; advance_leapfrog!; line: 52
                        119 ...4_leapfrog.jl; update_derivs_vel!; line: 18
                        11  ...4_leapfrog.jl; update_derivs_vel!; line: 19
                        99  ...4_leapfrog.jl; update_derivs_vel!; line: 21
                        3   ...4_leapfrog.jl; update_derivs_vel!; line: 22
                        19  ...4_leapfrog.jl; update_derivs_vel!; line: 23
                        111 ...4_leapfrog.jl; update_derivs_vel!; line: 24
                        79  ...4_leapfrog.jl; update_derivs_vel!; line: 25
                        105 ...4_leapfrog.jl; update_derivs_vel!; line: 26
                        77  ...4_leapfrog.jl; update_derivs_vel!; line: 27
                        65  ...4_leapfrog.jl; update_derivs_vel!; line: 28
                        60  ...4_leapfrog.jl; update_derivs_vel!; line: 29
                        1   ...4_leapfrog.jl; update_derivs_vel!; line: 30
                       3181 ...4_leapfrog.jl; advance_leapfrog!; line: 53
                        43   range.jl; colon; line: 38
                         37 range.jl; Range1; line: 27
                       313  ...4_leapfrog.jl; advance_leapfrog!; line: 54
                        202 ...4_leapfrog.jl; update_derivs_pos!; line: 6
                        1   ...4_leapfrog.jl; update_derivs_pos!; line: 7
                        101 ...4_leapfrog.jl; update_derivs_pos!; line: 8
                        3   ...4_leapfrog.jl; update_derivs_pos!; line: 11
                       2825 ...4_leapfrog.jl; advance_leapfrog!; line: 55
                        22   range.jl; colon; line: 38
                         15 range.jl; Range1; line: 27
                       6    range.jl; colon; line: 38
                 20    ...HW4_leapfrog.jl; integrate_leapfrog!; line: 88
                 103   ...HW4_leapfrog.jl; integrate_leapfrog!; line: 91
                 1     ...HW4_leapfrog.jl; integrate_leapfrog!; line: 93
                 3880  ...HW4_leapfrog.jl; integrate_leapfrog!; line: 94
                  250  abstractarray.jl; trailingsize; line: 54
                  3594 array.jl; getindex; line: 342
                  1    range.jl; Range1; line: 28
                  12   range.jl; colon; line: 38
                   5 range.jl; Range1; line: 27
                   4 range.jl; Range1; line: 28
                 17419 ...HW4_leapfrog.jl; integrate_leapfrog!; line: 95
                  127  abstractarray.jl; trailingsize; line: 54
                  2344 deepcopy.jl; deepcopy_internal; line: 45
                  4422 deepcopy.jl; deepcopy_internal; line: 46
                   2870 deepcopy.jl; _deepcopy_array_t; line: 52
                      160 reflection.jl; isbits; line: 36
                   330  deepcopy.jl; _deepcopy_array_t; line: 53
                    92  array.jl; copy!; line: 48
                    207 array.jl; copy!; line: 51
                     199 array.jl; unsafe_copy!; line: 38
                      77 array.jl; unsafe_copy!; line: 31
                      61 array.jl; unsafe_copy!; line: 33
                     3   array.jl; unsafe_copy!; line: 44
                   1060 deepcopy.jl; _deepcopy_array_t; line: 55
                   51   deepcopy.jl; _deepcopy_array_t; line: 62
                  36   deepcopy.jl; deepcopy_internal; line: 48
                  43   range.jl; colon; line: 38
                   36 range.jl; Range1; line: 27
                   1  range.jl; Range1; line: 28
                    26   array.jl; setindex!; line: 453
                    4923 array.jl; setindex!; line: 454
                     9    abstractarray.jl; checkbounds; line: 63
                     10   abstractarray.jl; checkbounds; line: 75
                     192  abstractarray.jl; checkbounds; line: 95
                      95 abstractarray.jl; checkbounds; line: 63
                     4648 abstractarray.jl; checkbounds; line: 96
                      4615 abstractarray.jl; checkbounds; line: 74
                       184  range.jl; maximum; line: 101
                       136  range.jl; minimum; line: 100
                        1    int.jl; <; line: 228
                         48 int.jl; <; line: 228
                         51 operators.jl; >; line: 19
                      10   abstractarray.jl; checkbounds; line: 75
                      5    range.jl; maximum; line: 101
                      3    range.jl; minimum; line: 100
                    29   array.jl; setindex!; line: 462
                    17   array.jl; setindex!; line: 464
                    1180 array.jl; setindex!; line: 465
                    24   array.jl; setindex!; line: 469
                 4     deepcopy.jl; deepcopy_internal; line: 46
                 11    range.jl; colon; line: 38

In [15]:
Profile.clear()
for i in 0:50
    @profile orbit = integrate_leapfrog_student([1.0,0.0,0.0,1.0],2pi/200,6pi);
    #println("X:",orbit[1][end]," Y:",orbit[2][end]," Vx:",orbit[3][end]," Vy:",orbit[4][end])
end
Profile.print()


WARNING: the profile data buffer is full; profiling probably terminated
before your program finished. To profile for longer runs, call Profile.init()
with a larger buffer and/or larger delay.
    6466 multi.jl; anonymous; line: 1308
      6466 ...Julia/src/IJulia.jl; eventloop; line: 92
       6466 ...Julia/src/IJulia.jl; eventloop; line: 68
          6466 ...xecute_request.jl; execute_request_0x535c5df2; line: 132
            6466 loading.jl; include_string; line: 83
                6466 no file; anonymous; line: 14
                 6384 ...frog_student.jl; integrate_leapfrog_student; line: 31
                  1    ...frog_student.jl; leapfrog_update_pos; line: 11
                  6383 ...frog_student.jl; leapfrog_update_pos; line: 13
                   4    abstractarray.jl; vcat; line: 706
                 17   ...frog_student.jl; integrate_leapfrog_student; line: 32
                  2  ...pfrog_student.jl; derrivatives; line: 4
                  15 ...pfrog_student.jl; derrivatives; line: 7
                   2  abstractarray.jl; vcat; line: 706
                 2    ...frog_student.jl; integrate_leapfrog_student; line: 33
                  2 ...pfrog_student.jl; leapfrog_update_both; line: 20
                   1 abstractarray.jl; vcat; line: 706
                 22   ...frog_student.jl; integrate_leapfrog_student; line: 36
                  1  abstractarray.jl; vcat; line: 706
                   4  abstractarray.jl; vcat; line: 925
                     1 abstractarray.jl; cat_t; line: 875
                     1 abstractarray.jl; cat_t; line: 893
                        1 base.jl; Array; line: 149
                     1 abstractarray.jl; cat_t; line: 918
                     1 abstractarray.jl; cat_t; line: 919
                        1 base.jl; append_any; line: 110
                   16 array.jl; vcat; line: 1164
                 15   ...frog_student.jl; integrate_leapfrog_student; line: 37
                  1  abstractarray.jl; vcat; line: 706
                   6 abstractarray.jl; vcat; line: 925
                     6 abstractarray.jl; cat_t; line: 912
                   5 array.jl; vcat; line: 1164
                   3 array.jl; vcat; line: 1172
                 15   ...frog_student.jl; integrate_leapfrog_student; line: 38
                  2  abstractarray.jl; vcat; line: 706
                   6 array.jl; vcat; line: 1164
                 11   ...frog_student.jl; integrate_leapfrog_student; line: 39
                  2 abstractarray.jl; vcat; line: 706
                   4 array.jl; vcat; line: 1164

1d.

Yep it was the vcat calls, interesting how the increase by ~an order of magnitude in each successive line. Your code spends most of it's time on the deepcopy at line 95

1e.

Pre-allocating the log array might help, changing a value should be a much cheaper process.

2


2a.


In [29]:
function f(x::Vector{Float64})
  return exp(-0.5.*x.^2)/sqrt(2pi)
end


Out[29]:
f (generic function with 2 methods)

In [30]:
x = linspace(-3,3,1000)
y = f(x)
using PyPlot
plot(x,y)


Out[30]:
1-element Array{Any,1}:
 PyObject <matplotlib.lines.Line2D object at 0x110d90e10>

Looks normal to me!

2b. -loop


In [83]:
function loop_interate(f, a::Float64, b::Float64, integration_number::Int=1000)

  @assert a < b
  interval_bounds = linspace(a,b,integration_number+1)
  value_array = zeros(Float64,integration_number) 

  for i = 2:integration_number
    #trapizoid rule, cause that is enough for a homework 
    value_array[i] = (interval_bounds[i] - interval_bounds[i-1]) *.5*(f(interval_bounds[i-1] )+f(interval_bounds[i] ) ) 
  end
 
  return sum(value_array)

end


Out[83]:
loop_interate (generic function with 3 methods)

In [118]:
#Does it obey the 68–95–99.7 rule?
println(loop_interate(f,-1.0,1.0))
println(loop_interate(f,-2.0,2.0))
println(loop_interate(f,-3.0,3.0))
println(loop_interate(f,-100.0,100.0))


0.6822049054334063
0.9542826178365158
0.9972732918302741
1.0000000000000004

2c. -vector


In [108]:
function vector_interate(f, a::Float64, b::Float64, integration_number::Int=1000)

  @assert a < b
  bounds = linspace(a,b,integration_number+1)
  lower_bounds = bounds[1:integration_number]
  upper_bounds = bounds[2:end]

  value_array = (upper_bounds .- lower_bounds) *.5.*(f(lower_bounds ).+f(upper_bounds)) 

  return sum(value_array)

end


Out[108]:
vector_interate (generic function with 2 methods)

In [120]:
#Does it obey the 68–95–99.7 rule?
println(vector_interate(f,-1.0,1.0))
println(vector_interate(f,-2.0,2.0))
println(vector_interate(f,-3.0,3.0))
println(vector_interate(f,-100.0,100.0))


0.682689330823248
0.9544994481518971
0.9973001241637558
1.0

2d. -map + reduce


In [143]:
function trapazoid_rule(a::Float64,b::Float64)
  #If f is not alrady complied you are out of luck here. 
  return (b - a) * .5 * (f(a) + f(b))
  
end

function map_plus_reduce_interate(f, a::Float64, b::Float64, integration_number::Int=1000)

  #Don't actually call f anymore
  @assert a < b
  bounds = linspace(a,b,integration_number+1)
  lower_bounds = bounds[1:integration_number]
  upper_bounds = bounds[2:end]

  value_array = map(trapazoid_rule,lower_bounds,upper_bounds)

  return reduce(+,value_array)

end


Out[143]:
map_plus_reduce_interate (generic function with 2 methods)

In [144]:
#Does it obey the 68–95–99.7 rule?
println(map_plus_reduce_interate(f,-1.0,1.0))
println(map_plus_reduce_interate(f,-2.0,2.0))
println(map_plus_reduce_interate(f,-3.0,3.0))
println(map_plus_reduce_interate(f,-100.0,100.0))


0.682689330823248
0.9544994481518971
0.9973001241637558
1.0

2e. -mapreduce


In [159]:
#mapreduce can't handle multiple itr, so just tweaking map_plus_reduce wont do it. errgh, tired and grumpy, 
#don't want to code no more today

function trapazoid_rule(bounds)
  #If f is not alrady complied you are out of luck here. 
  return (b - a) * .5 * (f(a) + f(b))
  
end

function mapreduce_interate(f, a::Float64, b::Float64, integration_number::Int=1000)

  #Don't actually call f anymore 
  @assert a < b
  bounds = linspace(a,b,integration_number+1)
  #lower_bounds = bounds[1:integration_number]
  #upper_bounds = bounds[2:end]

  return mapreduce(trapazoid_rule,+,bounds)


end


Out[159]:
mapreduce_interate (generic function with 2 methods)

In [149]:
#Does it obey the 68–95–99.7 rule?
println(mapreduce_interate(f,-1.0,1.0))
println(mapreduce_interate(f,-2.0,2.0))
println(mapreduce_interate(f,-3.0,3.0))
println(mapreduce_interate(f,-100.0,100.0))


no method trapazoid_rule(Float64,)
at In[149]:2
 in mr_pairwise at reduce.jl:135
 in mr_pairwise at reduce.jl:153 (repeats 3 times)
 in mapreduce at reduce.jl:162
 in mapreduce_interate at In[148]:15
 in mapreduce_interate at In[148]:10

2f. -devectorize


In [151]:
#blah! looks interesting...loops faster than vectors? Strange.

2g.


In [160]:
println("Loop: ",@elapsed loop_interate(f,-1.0,1.0,1000000))
println("Vector: ",@elapsed vector_interate(f,-1.0,1.0,1000000))
println("Map+Reduce: ",@elapsed map_plus_reduce_interate(f,-1.0,1.0,1000000))


Loop: 0.440510142
Vector: 0.188881352
Map+Reduce: 0.216344906

2h.

Since I only made three versions, let's profile all of them.


In [161]:
Profile.clear()
@profile loop_interate(f,-1.0,1.0,1000000)
Profile.print()


1   array.jl; setindex!; line: 412
    372 multi.jl; anonymous; line: 1308
      372 ...IJulia/src/IJulia.jl; eventloop; line: 92
       372 ...Julia/src/IJulia.jl; eventloop; line: 68
          372 ...execute_request.jl; execute_request_0x535c5df2; line: 132
            372 loading.jl; include_string; line: 83
                371 profile.jl; anonymous; line: 14
                 14  In[83]; loop_interate; line: 4
                  14 array.jl; linspace; line: 238
                 2   In[83]; loop_interate; line: 5
                  2 array.jl; fill!; line: 187
                 1   In[83]; loop_interate; line: 8
                 353 In[83]; loop_interate; line: 10
                    53 In[17]; f; line: 2
                    1  array.jl; setindex!; line: 412
                    3  float.jl; *; line: 136
                    2  float.jl; +; line: 132
                 1   In[83]; loop_interate; line: 13
                  1 abstractarray.jl; sum; line: 1487
                   1 abstractarray.jl; sum_pairwise; line: 1481
                    1 abstractarray.jl; sum_pairwise; line: 1481
                     1 abstractarray.jl; sum_pairwise; line: 1481
                      1 abstractarray.jl; sum_pairwise; line: 1481
                       1 abstractarray.jl; sum_pairwise; line: 1481
                        1 abstractarray.jl; sum_pairwise; line: 1481
                         1 ...ractarray.jl; sum_pairwise; line: 1481
                          1 ...ractarray.jl; sum_pairwise; line: 1481
                           1 ...actarray.jl; sum_pairwise; line: 1481
                            1 ...actarray.jl; sum_pairwise; line: 1481
                             1 ...actarray.jl; sum_pairwise; line: 1481
                              1 ...ctarray.jl; sum_pairwise; line: 1481
                               1 ...ctarray.jl; sum_pairwise; line: 1481
                                1 ...ctarray.jl; sum_pairwise; line: 135

In [162]:
Profile.clear()
@profile vector_interate(f,-1.0,1.0,1000000)
Profile.print()


    160 multi.jl; anonymous; line: 1308
      160 ...IJulia/src/IJulia.jl; eventloop; line: 92
       160 ...Julia/src/IJulia.jl; eventloop; line: 68
          160 ...execute_request.jl; execute_request_0x535c5df2; line: 132
            160 loading.jl; include_string; line: 83
                160 profile.jl; anonymous; line: 14
                 15  In[108]; vector_interate; line: 4
                  15 array.jl; linspace; line: 238
                 22  In[108]; vector_interate; line: 6
                  22 array.jl; getindex; line: 294
                 122 In[108]; vector_interate; line: 13
                  3   array.jl; .*; line: 135
                  3   broadcast.jl; ##broadcast_T_-#215; line: 189
                      3 broadcast.jl; ##-_inner!#217; line: 166
                   92 In[29]; f; line: 2
                    4  array.jl; .*; line: 135
                    14 array.jl; ./; line: 135
                    46 array.jl; .^; line: 920
                    28 operators.jl; exp; line: 236
                   20 broadcast.jl; .*; line: 277
                    20 broadcast.jl; ##broadcast_T_*#221; line: 189
                      17 broadcast.jl; broadcast_args; line: 87
                       17 broadcast.jl; calc_loop_strides; line: 63
                        1 broadcast.jl; ##*_inner!#223; line: 136
                        2 broadcast.jl; ##*_inner!#223; line: 166
                   4  broadcast.jl; .+; line: 276
                    4 broadcast.jl; ##broadcast_T_+#209; line: 189
                        1 broadcast.jl; ##+_inner!#211; line: 136
                        3 broadcast.jl; ##+_inner!#211; line: 166
                 1   In[108]; vector_interate; line: 15
                    1 abstractarray.jl; sum; line: 1487
                     1 abstractarray.jl; sum_pairwise; line: 1481
                      1 abstractarray.jl; sum_pairwise; line: 1481
                       1 abstractarray.jl; sum_pairwise; line: 1481
                        1 abstractarray.jl; sum_pairwise; line: 1481
                         1 ...ractarray.jl; sum_pairwise; line: 1481
                          1 ...ractarray.jl; sum_pairwise; line: 1481
                           1 ...actarray.jl; sum_pairwise; line: 1481
                            1 ...actarray.jl; sum_pairwise; line: 1481
                             1 ...actarray.jl; sum_pairwise; line: 1481
                              1 ...ctarray.jl; sum_pairwise; line: 1481
                               1 ...ctarray.jl; sum_pairwise; line: 1481
                                1 ...tarray.jl; sum_pairwise; line: 1481
                                 1 ...tarray.jl; sum_pairwise; line: 1481
                                  1 ...tarray.jl; sum_pairwise; line: 135

In [163]:
Profile.clear()
@profile map_plus_reduce_interate(f,-1.0,1.0,1000000) 
Profile.print()


    263 multi.jl; anonymous; line: 1308
      263 ...IJulia/src/IJulia.jl; eventloop; line: 92
       263 ...Julia/src/IJulia.jl; eventloop; line: 68
          263 ...execute_request.jl; execute_request_0x535c5df2; line: 132
            263 loading.jl; include_string; line: 83
                262 profile.jl; anonymous; line: 14
                 14  In[143]; map_plus_reduce_interate; line: 11
                  14 array.jl; linspace; line: 238
                 189 In[143]; map_plus_reduce_interate; line: 15
                  19  abstractarray.jl; map; line: 1696
                  170 abstractarray.jl; map; line: 1702
                     170 abstractarray.jl; map_to!; line: 1690
                        52 In[148]; trapazoid_rule; line: 3
                         49 In[17]; f; line: 2
                        1  array.jl; setindex!; line: 412
                 59  In[143]; map_plus_reduce_interate; line: 17
                   59 reduce.jl; reduce; line: 179
                    59 reduce.jl; r_pairwise; line: 174
                     59 reduce.jl; r_pairwise; line: 174
                      59 reduce.jl; r_pairwise; line: 174
                       59 reduce.jl; r_pairwise; line: 174
                        59 reduce.jl; r_pairwise; line: 174
                         59 reduce.jl; r_pairwise; line: 174
                          59 reduce.jl; r_pairwise; line: 174
                           59 reduce.jl; r_pairwise; line: 174
                            59 reduce.jl; r_pairwise; line: 174
                             59 reduce.jl; r_pairwise; line: 174
                              59 reduce.jl; r_pairwise; line: 174
                               59 reduce.jl; r_pairwise; line: 174
                                59 reduce.jl; r_pairwise; line: 174
                                 59 reduce.jl; r_pairwise; line: 135

In [ ]: