Module: Enumerable

Defined in:
lib/ontomde-core/threads.rb

Overview

Multi-threading is necessary for taking advantage of multi cpu/core

  • MRI does not take advantage of multi core so you will not see any gain

  • JRUBY does use native thread and thus will benefit of threading.

I have personnaly gainned 15% of global ontomde execution time on a dual CPU with anti-virus using between 25% and 90% of one core. Benefit should be more noticeable with a quad core.

(may 2009)

Constant Summary collapse

PLATFORM_IS_JAVA =
!(RUBY_PLATFORM.index('java').nil?)
THREAD_NUMBER =
ENV['ONTOMDE_THREADS'].nil? ? (PLATFORM_IS_JAVA ? 4 : 1) : ENV['ONTOMDE_THREADS'].to_int

Instance Method Summary collapse

Instance Method Details

#eachInThread(res, nbThread = THREAD_NUMBER, &proc) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ontomde-core/threads.rb', line 15

def eachInThread(res,nbThread=THREAD_NUMBER,&proc)
	if(res.context[:mtkThread,false])
puts "!! WARNING: recursive multi-thread request ignored" 
nbThread=1
	end	
	if(nbThread<=1 || (size < (nbThread*2)) ) 
each &proc
return 
	end	

	t=Array.new
	(0..(nbThread-1)).each { |i|
t[i]=Array.new
	}
	# split collection in nbThread collections
	i=0
	each { |elt|
t[i].push(elt)
i=(i+1)%nbThread
	}
	
	# run one thread for each sub-collection
	rt=Array.new
	(0..(nbThread-1)).each { |i|
puts "!Starting thread #{i} for #{t[i].size} elements"
rt.push(Thread.new(i,t[i],res.context) {  |ii,tas,ctx|
	Thread.current["@@context"]=ctx
	res.mtk_context(:mtkThread  => true) {
		tas.each &proc
	#puts "!thread #{ii} done."
	}})

	}

	puts "!waiting for unfinished parallel tasks"
	rt.each { |th|
th.join
	}
end