<commit_before>! Copyright (C) 2008, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors fry kernel locals math math.constants
math.functions math.vectors math.vectors.simd prettyprint
combinators.smart sequences hints struct-arrays classes.struct ;
IN: benchmark.nbody-simd

: solar-mass ( -- x ) 4 pi sq * ; inline
CONSTANT: days-per-year 365.24

STRUCT: body
{ location double-4 }
{ velocity double-4 }
{ mass double } ;

: <body> ( location velocity mass -- body )
    [ days-per-year v*n ] [ solar-mass * ] bi* body <struct-boa> ; inline

: <jupiter> ( -- body )
    double-4{ 4.84143144246472090e+00 -1.16032004402742839e+00 -1.03622044471123109e-01 0.0 }
    double-4{ 1.66007664274403694e-03 7.69901118419740425e-03 -6.90460016972063023e-05 0.0 }
    9.54791938424326609e-04
    <body> ;

: <saturn> ( -- body )
    double-4{ 8.34336671824457987e+00 4.12479856412430479e+00 -4.03523417114321381e-01 0.0 }
    double-4{ -2.76742510726862411e-03 4.99852801234917238e-03 2.30417297573763929e-05 0.0 }
    2.85885980666130812e-04
    <body> ;

: <uranus> ( -- body )
    double-4{ 1.28943695621391310e+01 -1.51111514016986312e+01 -2.23307578892655734e-01 0.0 }
    double-4{ 2.96460137564761618e-03 2.37847173959480950e-03 -2.96589568540237556e-05 0.0 }
    4.36624404335156298e-05
    <body> ;

: <neptune> ( -- body )
    double-4{ 1.53796971148509165e+01 -2.59193146099879641e+01 1.79258772950371181e-01 0.0 }
    double-4{ 2.68067772490389322e-03 1.62824170038242295e-03 -9.51592254519715870e-05 0.0 }
    5.15138902046611451e-05
    <body> ;

: <sun> ( -- body )
    double-4{ 0 0 0 0 } double-4{ 0 0 0 0 } 1 <body> ;
    
: offset-momentum ( body offset -- body )
    vneg solar-mass v/n >>velocity ; inline

TUPLE: nbody-system { bodies struct-array read-only } ;

: init-bodies ( bodies -- )
    [ first ] [ double-4{ 0 0 0 0 } [ [ velocity>> ] [ mass>> ] bi v*n v+ ] reduce ] bi
    offset-momentum drop ; inline

: <nbody-system> ( -- system )
    [ <sun> <jupiter> <saturn> <uranus> <neptune> ]
    struct-array{ body } output>sequence nbody-system boa
    dup bodies>> init-bodies ; inline

:: each-pair ( bodies pair-quot: ( other-body body -- ) each-quot: ( body -- ) -- )
    bodies [| body i |
        body each-quot call
        bodies i 1 + tail-slice [
            body pair-quot call
        ] each
    ] each-index ; inline

: update-position ( body dt -- )
    [ dup velocity>> ] dip '[ _ _ v*n v+ ] change-location drop ; inline
<commit_msg>benchmark.nbody-simd: use map-reduce instead of reduce<commit_after>! Copyright (C) 2008, 2009 <NAME>.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors fry kernel locals math math.constants
math.functions math.vectors math.vectors.simd prettyprint
combinators.smart sequences hints struct-arrays classes.struct ;
IN: benchmark.nbody-simd

: solar-mass ( -- x ) 4 pi sq * ; inline
CONSTANT: days-per-year 365.24

STRUCT: body
{ location double-4 }
{ velocity double-4 }
{ mass double } ;

: <body> ( location velocity mass -- body )
    [ days-per-year v*n ] [ solar-mass * ] bi* body <struct-boa> ; inline

: <jupiter> ( -- body )
    double-4{ 4.84143144246472090e+00 -1.16032004402742839e+00 -1.03622044471123109e-01 0.0 }
    double-4{ 1.66007664274403694e-03 7.69901118419740425e-03 -6.90460016972063023e-05 0.0 }
    9.54791938424326609e-04
    <body> ;

: <saturn> ( -- body )
    double-4{ 8.34336671824457987e+00 4.12479856412430479e+00 -4.03523417114321381e-01 0.0 }
    double-4{ -2.76742510726862411e-03 4.99852801234917238e-03 2.30417297573763929e-05 0.0 }
    2.85885980666130812e-04
    <body> ;

: <uranus> ( -- body )
    double-4{ 1.28943695621391310e+01 -1.51111514016986312e+01 -2.23307578892655734e-01 0.0 }
    double-4{ 2.96460137564761618e-03 2.37847173959480950e-03 -2.96589568540237556e-05 0.0 }
    4.36624404335156298e-05
    <body> ;

: <neptune> ( -- body )
    double-4{ 1.53796971148509165e+01 -2.59193146099879641e+01 1.79258772950371181e-01 0.0 }
    double-4{ 2.68067772490389322e-03 1.62824170038242295e-03 -9.51592254519715870e-05 0.0 }
    5.15138902046611451e-05
    <body> ;

: <sun> ( -- body )
    double-4{ 0 0 0 0 } double-4{ 0 0 0 0 } 1 <body> ;
    
: offset-momentum ( body offset -- body )
    vneg solar-mass v/n >>velocity ; inline

TUPLE: nbody-system { bodies struct-array read-only } ;

: init-bodies ( bodies -- )
    [ first ] [ [ [ velocity>> ] [ mass>> ] bi v*n ] [ v+ ] map-reduce ] bi
    offset-momentum drop ; inline

: <nbody-system> ( -- system )
    [ <sun> <jupiter> <saturn> <uranus> <neptune> ]
    struct-array{ body } output>sequence nbody-system boa
    dup bodies>> init-bodies ; inline

:: each-pair ( bodies pair-quot: ( other-body body -- ) each-quot: ( body -- ) -- )
    bodies [| body i |
        body each-quot call
        bodies i 1 + tail-slice [
            body pair-quot call
        ] each
    ] each-index ; inline

: update-position ( body dt -- )
    [ dup velocity>> ] dip '[ _ _ v*n v+ ] change-location drop ; inline

{'fraction_non_alphanumeric': 0.14021847070506455, 'fraction_numerical': 0.24409136047666335, 'mean_word_length': 2.8828064764841943, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3860460', 'n_tokens_mistral': 2737, 'n_tokens_neox': 2018, 'n_words': 420}