Chris -
Thanks for your response. I have a few follow up questions:

toddg=> (show (range 10))
===  public final clojure.lang.LazySeq  ===

... so range returns a lazy seq

toddg=> (show (seq (range 10)))
===  public final clojure.lang.ChunkedCons  ===

... but seq'ing it returns a ChunkedCons

Q: Why does range return a LazySeq whereas seq returns a ChunkedCons? I can see that these two classes are very different...but I don't really understand what's going on here.

toddg=> (show (doall (range 10)))
===  public final clojure.lang.LazySeq  ===

Q: Since doall walks the entire seq, it almost seems like the lazy seq isn't a lazy seq anymore... I mean, if seq'ing a LazySeq returns a different type, then I'd almost expect doall to return a different type, too. One that's not lazy.

toddg=> (show list)
===  static clojure.lang.PersistentList$1  ===

toddg=> (show (list (range 10)))
===  public clojure.lang.PersistentList  ===

Q: So, is this the idiomatic way to convert the LazySeq or ChunkedCons to a Persistent (list|vector)?

toddg=> (show (into [] (range 10)))
===  public clojure.lang.PersistentVector  ===

toddg=> (show (into () (range 10)))
===  public clojure.lang.PersistentList  ===

Q: Where are the clojure.lang classes documented? I see the classes in the source, but they do not appear to have javadocs.

BTW - I really enjoyed your book.

-Todd

On 6/28/10 6:45 AM, Chouser wrote:
On Sat, Jun 26, 2010 at 7:01 PM, toddg<t.greenwoodg...@gmail.com>  wrote:
(running clojure 1.2 snapshot)

Q1: Why does pmap use the number of available processors + 2? I would
have thought it would just use the number of avail processors...

I'm not entirely sure, but I think the idea is to prevent too
much context-switching on each core.  Perhaps +2 helps fill in
gaps in the processor's pipelines or something.  The agent pool
for 'send' has a similar limit formula.

Q2: Could someone clear up my misunderstanding of pmap w/ respect to
the code snippets below? Pmap doesn't seem to be limiting the number
of threads to the number of processors + 2...

I've created an anon func that does't return, so I wouldn't expect the
pmap step function to advance beyond 4 (I have 2 processors):

#1: Limit pmap to # of processors
----------------------------------

[snip]

-->  just two threads running, as expected


#2: Limit pmap to # of processors * 10
--------------------------------------

user=>  (pmap #(while true (do (println "Thread: " (.getId (Thread/
currentThread)) "item: " %)(Thread/sleep 500))) (range (* 10
(.availableProcessors (Runtime/getRuntime)))))
Thread: Thread:   12 item:  0
(Thread:  25 item:  13
Thread:  24 item:  12
Thread:  23 item:  11
Thread:  22 item:  10
Thread:  21 item:  9
Thread:  20 item:  8
Thread:  19 item:  7
Thread:  18 item:  6
Thread:  17 item:  5
Thread:  16 item:  4
Thread:  15 item:  3

-->  In this short snippet, you can see>  4 threads running...expected?

Range produces a chunked seq, which if my answer to Q1 is
correct, I guess pmap isn't expecting.  So pmap creates one
'future' per element, but instead of only doing this for procs+2
elements at a time, it does so for every element of the first
chunk.  If you use range of more than 32 elements, you'll see
your example doesn't go beyond the first 32 (range's chunk size).
...or you can use a PersistentList instead of a range, which
produces unchunked seqs, and see that it only uses procs+2
threads.

--Chouser
http://joyofclojure.com/


--
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
Note that posts from new members are moderated - please be patient with your 
first post.
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