# C[omp]ute

Welcome to my blog, which was once a mailing list of the same name and is still generated by mail. Please reply via the "comment" links.

Always interested in offers/projects/new ideas. Eclectic experience in fields like: numerical computing; Python web; Java enterprise; functional languages; GPGPU; SQL databases; etc. Based in Santiago, Chile; telecommute worldwide. CV; email.

© 2006-2015 Andrew Cooke (site) / post authors (content).

## Clojure macro example - dopar

From: andrew cooke <andrew@...>

Date: Sun, 10 Jun 2012 14:28:20 -0400

I just wrote my first "worthwhile" macro in Clojure (by which I mean, does
something non-trivial I expected the language to do for me).

I have some code I want to run multiple times.  The only things that changes
each time is an integer, but each run takes 10 hours.  So I had a loop that
took 10 hours per iteration.  If you know Clojure then you'll know that my
code looked like this:

(doseq [i (range 4)]
(do-stuff-with i))

which is basically a "for loop" that changes "i".

To save time I wanted to run those things in parallel.  So I wanted something
like Python's multiprocessing - a simple way to make "do-stuff-with i" run on
multiple cores.

Solving this went through three phases:

- Looking for an obvious existing solution.  Couldn't find one.

- Trying to write an implementation using basic concepts from Java like
semaphores or ThreadPoolExecutor.  This was complicated and "felt wrong".

- Working out how to do it the "Clojure way".  Understanding that I needed
agents, and then writing a macro to manage the agents.

At first I was confused by agents - I thought they would be something like
processes.  But really they are just values that a process can access.  So in
my code above.  So my original idea to put "do-stuff-with" in an agent was
wrong - it is the "i" that "goes inside" the agent.

So the basic way to solve this problem in Clojure is to:

- Create an agent for each "i"

- Use "send" to send the function "do-stuff-with" to each agent.

- Wait for all the agents to finish.

Behind the scenes, Clojure is careful to only let a few agents run at a time
(this is what "send" takes care of).

So a solution would look something like:

(apply await
(for [i (range 4)]
(let [a (agent i)]
(send a do-stuff-with)
a)))

which:

- Generates an agent for each i

- Sends the work to the agent (which is queued to run in a thread from a

- Creates a list of agents (the result of the "for")

- Waits for all the agents to finish.

OK, so where does the macro come into this?  Well, this code:

(defmacro dopar [seq-expr & body]
(assert (= 2 (count seq-expr)) "single pair of forms in sequence
expression")
(let [[k v] seq-expr]
(apply await
(for [k# ~v]
(let [a# (agent k#)]
(send a# (fn [~k] ~@body))
a#)))))

lets me write the (apply....) above as:

(dopar [i (range 4)]
(do-stuff-with i))

which is, I think, pretty awesome.

Andrew`