]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/nbody-simd/nbody-simd.factor
Specialized array overhaul
[factor.git] / extra / benchmark / nbody-simd / nbody-simd.factor
1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors fry kernel locals math math.constants
4 math.functions math.vectors math.vectors.simd prettyprint
5 combinators.smart sequences hints classes.struct
6 specialized-arrays ;
7 IN: benchmark.nbody-simd
8
9 : solar-mass ( -- x ) 4 pi sq * ; inline
10 CONSTANT: days-per-year 365.24
11
12 STRUCT: body
13 { location double-4 }
14 { velocity double-4 }
15 { mass double } ;
16
17 SPECIALIZED-ARRAY: body
18
19 : <body> ( location velocity mass -- body )
20     [ days-per-year v*n ] [ solar-mass * ] bi* body <struct-boa> ; inline
21
22 : <jupiter> ( -- body )
23     double-4{ 4.84143144246472090e+00 -1.16032004402742839e+00 -1.03622044471123109e-01 0.0 }
24     double-4{ 1.66007664274403694e-03 7.69901118419740425e-03 -6.90460016972063023e-05 0.0 }
25     9.54791938424326609e-04
26     <body> ;
27
28 : <saturn> ( -- body )
29     double-4{ 8.34336671824457987e+00 4.12479856412430479e+00 -4.03523417114321381e-01 0.0 }
30     double-4{ -2.76742510726862411e-03 4.99852801234917238e-03 2.30417297573763929e-05 0.0 }
31     2.85885980666130812e-04
32     <body> ;
33
34 : <uranus> ( -- body )
35     double-4{ 1.28943695621391310e+01 -1.51111514016986312e+01 -2.23307578892655734e-01 0.0 }
36     double-4{ 2.96460137564761618e-03 2.37847173959480950e-03 -2.96589568540237556e-05 0.0 }
37     4.36624404335156298e-05
38     <body> ;
39
40 : <neptune> ( -- body )
41     double-4{ 1.53796971148509165e+01 -2.59193146099879641e+01 1.79258772950371181e-01 0.0 }
42     double-4{ 2.68067772490389322e-03 1.62824170038242295e-03 -9.51592254519715870e-05 0.0 }
43     5.15138902046611451e-05
44     <body> ;
45
46 : <sun> ( -- body )
47     double-4{ 0 0 0 0 } double-4{ 0 0 0 0 } 1 <body> ;
48     
49 : offset-momentum ( body offset -- body )
50     vneg solar-mass v/n >>velocity ; inline
51
52 : init-bodies ( bodies -- )
53     [ first ] [ [ [ velocity>> ] [ mass>> ] bi v*n ] [ v+ ] map-reduce ] bi
54     offset-momentum drop ; inline
55
56 : <nbody-system> ( -- system )
57     [ <sun> <jupiter> <saturn> <uranus> <neptune> ]
58     body-array{ } output>sequence
59     dup init-bodies ; inline
60
61 :: each-pair ( bodies pair-quot: ( other-body body -- ) each-quot: ( body -- ) -- )
62     bodies [| body i |
63         body each-quot call
64         bodies i 1 + tail-slice [
65             body pair-quot call
66         ] each
67     ] each-index ; inline
68
69 : update-position ( body dt -- )
70     [ dup velocity>> ] dip '[ _ _ v*n v+ ] change-location drop ; inline
71
72 : mag ( dt body other-body -- mag d )
73     [ location>> ] bi@ v- [ norm-sq dup sqrt * / ] keep ; inline
74
75 :: update-velocity ( other-body body dt -- )
76     dt body other-body mag
77     [ [ body ] 2dip '[ other-body mass>> _ * _ n*v v- ] change-velocity drop ]
78     [ [ other-body ] 2dip '[ body mass>> _ * _ n*v v+ ] change-velocity drop ] 2bi ; inline
79
80 : advance ( system dt -- )
81     [ '[ _ update-velocity ] [ drop ] each-pair ]
82     [ '[ _ update-position ] each ]
83     2bi ; inline
84
85 : inertia ( body -- e )
86     [ mass>> ] [ velocity>> norm-sq ] bi * 0.5 * ; inline
87
88 : newton's-law ( other-body body -- e )
89     [ [ mass>> ] bi@ * ] [ [ location>> ] bi@ distance ] 2bi / ; inline
90
91 : energy ( system -- x )
92     [ 0.0 ] dip [ newton's-law - ] [ inertia + ] each-pair ; inline
93
94 : nbody ( n -- )
95     >fixnum
96     <nbody-system>
97     [ energy . ] [ '[ _ 0.01 advance ] times ] [ energy . ] tri ;
98
99 : nbody-main ( -- ) 1000000 nbody ;
100
101 MAIN: nbody-main