Asbjxrn,

One thing that leaps out to me performance-wise is the 3 nested loops
(dotimes, dotimes, loop/recur). Whatever's inside the inner loop is
getting run a lot of times! General advice about reducing loop depth
and computation required inside the innermost loop aside... have you
looked at clojure.parallel? Seems like it might be a good fit since
your algorithm is doing a lot of array processing...

-Greg

On Jan 14, 11:27 am, Asbjørn  Bjørnstad <asbj...@gmail.com> wrote:
> I'm not complaining, but I find the topic interesting as I just tried
> translating a fluid dynamics algorithm to clojure.
> (http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/
> GDC03.pdf)
> I got no experience with this kind of stuff, it's just something I
> tried out for fun. So the performance I get may be as expected. Or
> there may be obvious ways of speeding it up that I don't see.
>
> Anyway, here is a core part of the algorithm. It's a diffusion
> step, this gets called 3 times and in total this takes up more than
> one second of cpu time on my machine which makes framerates very
> slow. If anyone got any improvements I'd be happy to hear about it:
>
> (def grid-size 300)
>
> (defn make-grid []
>   (make-array Float/TYPE (* (+ grid-size 2)
>                             (+ grid-size 2))))
>
> (defn diffuse [grid diff-ratio dt]
>   (let [a (float (* dt diff-ratio grid-size grid-size))
>         a4-1 (float (+ 1 (* 4 a)))
>         grid #^floats (deref grid)
>         diffused-grid #^floats (make-grid)
>         line-length (int (+ grid-size 2))]
>     (dotimes [n 20]
>       (dotimes [y grid-size]
>         (let [line-offset (* (inc y) line-length)]
>          (loop [x (int 1)
>                 c (+ x line-offset)]
>            (aset diffused-grid c
>                        (float (/ (+ (aget grid c)
>                                     (*  a
>                                         (+ (+ (aget diffused-grid (dec
> c))
>                                               (aget diffused-grid (inc
> c)))
>                                            (+ (aget diffused-grid (- c
> line-length))
>                                               (aget diffused-grid (+ c
> line-length))))))
>                                  a4-1)))
>            (when (< x grid-size)
>              (recur (inc x)
>                     (inc c)))))))
>     diffused-grid))
>
> (def foo (ref (make-grid)))
>
> (time (diffuse foo 0.00025 0.002))
>
> --
>  -asbjxrn
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to