From: Marc Hoeppner on
Robert Klemme wrote:

> I believe you are not using Process.fork properly. In fact, I am
> surprised that you do not get an exception:
>
> irb(main):001:0> Process.fork("foo")
> ArgumentError: wrong number of arguments (1 for 0)
> from (irb):1:in `fork'
> from (irb):1
> from :0

Yes, quite possible - I didn't really look up the exact code, just wrote
it down from memory, sorry about that..

>
> processes = 4
> count = 0
>
> my_array.each do |task|
> if count == processes
> Process.wait
> count -= 1
> end
>
> fork do
> a_method(task)
> end
> count += 1
> end
>
> Process.waitall
>

That works like a charm, thanks a lot!
--
Posted via http://www.ruby-forum.com/.

From: James M. Lawrence on
Robert Klemme wrote:
> processes = 4
> count = 0
>
> my_array.each do |task|
> if count == processes
> Process.wait
> count -= 1
> end
>
> fork do
> a_method(task)
> end
> count += 1
> end
>
> Process.waitall

Another option,

Tiamat.open_local(4) {
pure do
fun_map :result => my_array do |elem|
a_method(elem)
end
end.compute.result
}

This lets you distribute across N physical machines without a change to
the code.
--
Posted via http://www.ruby-forum.com/.

From: James M. Lawrence on
Tony Arcieri wrote:
> Ruby 1.9 isn't going to help you when using threads to distribute
> computation across CPU cores. The Global VM Lock ensures that
> simultaneous computation is still limited to one core.
>
> JRuby, on the other hand, does not have this limitation. On MRI/1.9
> I would recommend using multiple processes.

I'm not so sure jruby does this effectively.

require 'tiamat/autoconfig'
require 'pure/dsl'
require 'benchmark'

mod = pure do
def total(left, right)
left + right
end

def left
(1..5_000_000).inject(0) { |acc, n| acc + n }
end

def right
(1..5_000_000).inject(0) { |acc, n| acc + n }
end
end

Benchmark.bmbm { |bm|
bm.report("1 thread, 1 interpreter") {
mod.compute(1).total
}
bm.report("2 threads, 1 interpreter") {
mod.compute(2).total
}
# this part removed for jruby bench
bm.report("2 threads, 2 interpreters") {
Tiamat.open_local(2) {
mod.compute.total
}
}
}

== ruby 1.9.2dev (2009-10-18 trunk 25393) [i386-darwin9.8.0]
Rehearsal -------------------------------------------------------------
1 thread, 1 interpreter 4.370000 0.020000 4.390000 ( 4.389990)
2 threads, 1 interpreter 4.360000 0.030000 4.390000 ( 4.385111)
2 threads, 2 interpreters 0.010000 0.010000 4.700000 ( 2.460661)
--------------------------------------------------- total: 13.480000sec

user system total real
1 thread, 1 interpreter 4.360000 0.020000 4.380000 ( 4.376050)
2 threads, 1 interpreter 4.360000 0.030000 4.390000 ( 4.380982)
2 threads, 2 interpreters 0.010000 0.010000 4.710000 ( 2.465925)


== jruby 1.4.0RC3 (ruby 1.8.7 patchlevel 174) (2009-10-30 1d7de2d) (Java
HotSpot(TM) Client VM 1.5.0_20) [i386-java]
Rehearsal ------------------------------------------------------------
1 thread, 1 interpreter 6.060000 0.000000 6.060000 ( 6.060000)
2 threads, 1 interpreter 7.629000 0.000000 7.629000 ( 7.629000)
-------------------------------------------------- total: 13.689000sec

user system total real
1 thread, 1 interpreter 6.080000 0.000000 6.080000 ( 6.080000)
2 threads, 1 interpreter 7.288000 0.000000 7.288000 ( 7.288000)


--
Posted via http://www.ruby-forum.com/.

From: Rajinder Yadav on
On Fri, Oct 30, 2009 at 11:07 AM, James M. Lawrence
<quixoticsycophant(a)gmail.com> wrote:
> Robert Klemme wrote:
>> processes = 4
>> count = 0
>>
>> my_array.each do |task|
>>    if count == processes
>>      Process.wait
>>      count -= 1
>>    end
>>
>>    fork do
>>      a_method(task)
>>    end
>>    count += 1
>> end
>>
>> Process.waitall
>
> Another option,
>
> Tiamat.open_local(4) {
>  pure do
>    fun_map :result => my_array do |elem|
>      a_method(elem)
>    end
>  end.compute.result
> }
>
> This lets you distribute across N physical machines without a change to
> the code.

This is just elegant =) ... it's funny how I observer something then
more of what I observer comes in to the fold! Was hoping you would
reply to the thread ;)
> --
> Posted via http://www.ruby-forum.com/.
>
>

--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives
From: Glen Holcomb on
On Fri, Oct 30, 2009 at 2:06 AM, Robert Klemme
<shortcutter(a)googlemail.com>wrote:

>
>
>
> I figured as much. The thread discussion does not help Marc, because
> he explicitly wanted to use processes for core utilization. Basically
> Glen sent us in the wrong direction though. :-)
>
>
I've always worked best as a diversion.

--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can’t hear a word you’re saying."

-Greg Graffin (Bad Religion)