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