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