# 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) # module Enumerable PLATFORM_IS_JAVA=!(RUBY_PLATFORM.index('java').nil?) THREAD_NUMBER= ENV['ONTOMDE_THREADS'].nil? ? (PLATFORM_IS_JAVA ? 4 : 1) : ENV['ONTOMDE_THREADS'].to_int 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 end