Skip to content

Commit ea4e321

Browse files
bpecsekhanabi1224
andauthored
Slight optimization of nbody 5.cl (#203)
* more lru impls * Slight optimization od nbody 5.cl * compilation switces modified for gfortran * nmody 5.cl amended * LRU 2.py corrected * Trying to speed up gfortran Co-authored-by: hanabi1224 <harlowmoo@gmail.com>
1 parent 8a04d72 commit ea4e321

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

bench/algorithm/nbody/5.cl

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
;;; Based on 2.zig
22
;;; Converted to Common Lisp by Bela Pecsek - 2021-12-04
3-
;;; * Code cleanup and refactoring - 2021-12-06
3+
;;; * Code cleanup and refactoring - 2021-12-06
4+
;;; * Slight optimization - 2021-12-07
45
(declaim (optimize (speed 3) (safety 0) (debug 0)))
56

67
(eval-when (:compile-toplevel :load-toplevel :execute)
@@ -15,11 +16,11 @@
1516
(declaim (inline x y z vx vy vz mass make-body))
1617
(defstruct (body (:type (vector f64))
1718
(:conc-name nil)
18-
(:constructor make-body (x y z fill1 vx vy vz fill2 mass)))
19-
x y z fill1 vx vy vz fill2 mass)
19+
(:constructor make-body (x y z fill1 vx vy vz fill2 mas)))
20+
x y z fill1 vx vy vz fill2 mas)
2021

2122
(declaim (ftype (function (body) f64.4) pos vel)
22-
(inline pos vel mass set-pos (setf pos) set-vel (setf vel) (setf mass)))
23+
(inline pos vel mass set-pos (setf pos) set-vel (setf vel) set-mass (setf mass)))
2324
(defun pos (body)
2425
(f64.4-aref body 0))
2526
(defun vel (body)
@@ -81,15 +82,14 @@
8182

8283
;; Helper functions
8384
(declaim (ftype (function (f64.4 f64.4) f64) dot)
84-
(inline dot scale length-sq length_))
85+
(inline dot length-sq length_))
8586
(defun dot (a b)
8687
(f64.4-hsum (f64.4* a b)))
8788

88-
(declaim (ftype (function (f64.4) f64) length-sq))
89+
(declaim (ftype (function (f64.4) f64) length-sq length_))
8990
(defun length-sq (a)
9091
(dot a a))
9192

92-
(declaim (ftype (function (f64.4) f64) length_))
9393
(defun length_ (a)
9494
(sqrt (length-sq a)))
9595

@@ -115,28 +115,28 @@
115115
(loop for (bi . rest) on system do
116116
(dolist (bj rest)
117117
(let* ((pd (f64.4- (pos bi) (pos bj)))
118-
(dsq (f64.4 (dot pd pd)))
118+
(dsq (f64.4 (length-sq pd)))
119119
(dst (f64.4-sqrt dsq))
120-
(mag (f64.4/ (f64.4* dsq dst))))
121-
(declare (type f64.4 dsq dst mag))
122-
(f64.4-decf (vel bi) (f64.4* pd (f64.4* (mass bj) mag)))
123-
(f64.4-incf (vel bj) (f64.4* pd (f64.4* (mass bi) mag))))))
120+
(mag (f64.4/ (f64.4* dsq dst)))
121+
(pd-mag (f64.4* pd mag)))
122+
(f64.4-decf (vel bi) (f64.4* pd-mag (mass bj)))
123+
(f64.4-incf (vel bj) (f64.4* pd-mag (mass bi))))))
124124
(loop for b in system do
125125
(f64.4-incf (pos b) (vel b)))))
126126

127127
;; Output the total energy of the system.
128128
(declaim (ftype (function (list) null) energy))
129129
(defun energy (system)
130-
(loop with e of-type f64 = 0d00
131-
for (bi . rest) on system do
132-
;; Add the kinetic energy for each body.
133-
(incf e (f64* 0.5d0 (mass bi) (length-sq (vel bi))))
134-
(dolist (bj rest)
135-
(declare (type body bj))
136-
;; Add the potential energy between this body and every other bodies
137-
(decf e (f64/ (f64* (mass bi) (mass bj))
138-
(length_ (f64.4- (pos bi) (pos bj))))))
139-
finally (format t "~,9f~%" e))) ;; Output the total energy of the system.
130+
(loop for (bi . rest) on system
131+
with e of-type f64 = 0d0
132+
;; Add the kinetic energy for each body.
133+
do (incf e (f64* 0.5d0 (mass bi) (length-sq (vel bi))))
134+
(dolist (bj rest)
135+
(declare (type body bj))
136+
;; Add the potential energy between this body and every other bodies
137+
(decf e (f64/ (f64* (mass bi) (mass bj))
138+
(length_ (f64.4- (pos bi) (pos bj))))))
139+
finally (format t "~,9f~%" e)))
140140

141141
;; Rescale certain properties of bodies. That allows doing
142142
;; consequential advances as if dt were equal to 1.0d0.
@@ -155,7 +155,7 @@
155155
(let ((system *system*))
156156
(offset-momentum system)
157157
(energy system) ;; Output initial energy of the system
158-
(scale-bodies system +DT+) ;; Scale bodies to use time step of 1.0d0
158+
(scale-bodies system +DT+) ;; Scale bodies to use unity time step
159159
(advance system n-times) ;; Advance system n times
160160
(scale-bodies system +RECIP-DT+) ;; Rescale bodies back to original
161161
(energy system))) ;; Output final energy of the system

bench/bench_fortran.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ environments:
2828
version: 10
2929
docker:
3030
include:
31-
build: gfortran-10 -O3 -march=broadwell -fomit-frame-pointer -fopenmp -lpthread -lgmp -o out/app app.f90
31+
build: gfortran-10 -O3 -march=broadwell -dalign -fopenmp -fopenmp-simd -lgmp -o out/app app.f90
3232
out_dir: out
3333
run_cmd: app
3434
- os: linux

0 commit comments

Comments
 (0)