anchor.diff:          (velocity-scale 2.64)
           (delta-time 0.016)
           (damping 1.0)
  -        (p 256))
  -      (with-cuda-context (dev-id)
  -        (cffi:with-foreign-objects ((start-event 'cu-event)
  -                                    (stop-event 'cu-event))
  -          (cu-event-create start-event cu-event-default)
  -          (cu-event-create stop-event cu-event-default)
  -          (with-memory-blocks ((new-pos 'float4 num-bodies)
  -                               (old-pos 'float4 num-bodies)
  -                               (vel 'float4 num-bodies))
  -            (reset old-pos vel cluster-scale velocity-scale num-bodies)
  -            (memcpy-host-to-device old-pos vel)
  -            (let ((window (make-instance 'nbody-window)))
  -              (setf (slot-value window 'new-pos) new-pos)
  -              (setf (slot-value window 'old-pos) old-pos)
  -              (setf (slot-value window 'vel) vel)
  -              (setf (slot-value window 'delta-time) delta-time)
  -              (setf (slot-value window 'damping) damping)
  -              (setf (slot-value window 'num-bodies) num-bodies)
  -              (setf (slot-value window 'p) p)
  -              (setf (slot-value window 'start-event) start-event)
  -              (setf (slot-value window 'stop-event) stop-event)
  -              (cu-event-record (cffi:mem-ref start-event 'cu-event)
  -                               (cffi:null-pointer))
  -              (glut:display-window window)))))))
  +        (p 256)
  +        (window (make-instance 'nbody-window)))
  +    (with-cuda-context (dev-id)
  +      (with-memory-blocks ((new-pos 'float4 num-bodies)
  +                           (old-pos 'float4 num-bodies)
  +                           (vel 'float4 num-bodies))
  +      (with-cuda-timer
  +        ;; set values to slots of the nbody window
  +        (setf (slot-value window 'new-pos) new-pos
  +              (slot-value window 'old-pos) old-pos
  +              (slot-value window 'vel) vel
  +              (slot-value window 'delta-time) delta-time
  +              (slot-value window 'damping) damping
  +              (slot-value window 'num-bodies) num-bodies
  +              (slot-value window 'p) p)
  +        ;; reset position and velocity of bodies
  +        (reset old-pos vel cluster-scale velocity-scale num-bodies)
  +        ;; copy position and velocity on host memory to device memory
  +        (memcpy-host-to-device old-pos vel)
  +        ;; start CUDA timer
  +        (start-timer)
  +        ;; start glut main loop
  +        (glut:display-window window))))))
  
positive.code: (let ((fps-count 0)
        (fps-limit 5)
        (template "CUDA N-Body (~A bodies): ~,1F fps | ~,1F BIPS | ~,1F GFLOP/s | single precision~%"))
    (defun display-frame-rate (num-bodies start-event stop-event)
      (incf fps-count)
      (when (>= fps-count fps-limit)
        (cffi:with-foreign-object (pmilliseconds :float)
          (cu-event-record (cffi:mem-ref stop-event 'cu-event)
                           (cffi:null-pointer))
          (cu-event-synchronize (cffi:mem-ref stop-event 'cu-event))
          (cu-event-elapsed-time pmilliseconds
                                 (cffi:mem-ref start-event 'cu-event)
                                 (cffi:mem-ref stop-event 'cu-event))
          (let* ((milliseconds (/ (cffi:mem-ref pmilliseconds :float)
                                  fps-count))
                 (ifps (/ 1.0 (/ milliseconds 1000.0))))
            (multiple-value-bind (interactions-per-second gflops)
                (compute-perf-stats num-bodies milliseconds 1)
              (glut:set-window-title (format nil template 
                                             num-bodies ifps
                                             interactions-per-second gflops)))
            (setf fps-count 0)
            (setf fps-limit (if (> ifps 1.0) ifps 1.0)))
          (cu-event-record (cffi:mem-ref start-event 'cu-event)
                           (cffi:null-pointer))))))
  
  (defmethod glut:reshape ((w nbody-window) width height)
    (gl:matrix-mode :projection)
    (gl:load-identity)
    (glu:perspective 60.0 (/ width height) 0.1 1000.0)
    
    (gl:matrix-mode :modelview)
    (gl:viewport 0 0 width height))
  
  (defmethod glut:idle ((w nbody-window))
    (update (slot-value w 'new-pos)
            (slot-value w 'old-pos)
            (slot-value w 'vel)
            (slot-value w 'delta-time)
            (slot-value w 'damping)
            (slot-value w 'num-bodies)
            (slot-value w 'p))
    (rotatef (slot-value w 'new-pos) (slot-value w 'old-pos))
    (glut:post-redisplay))
  
  (defun main ()
    (let ((dev-id 0)
  
negative.code: (defun update (new-pos old-pos vel delta-time damping num-bodies p)
    (integrate-nbody-system new-pos old-pos vel delta-time damping num-bodies p))
  
  (defclass nbody-window (glut:window)
    ((new-pos :initform nil)
     (old-pos :initform nil)
  