query: tag:algorithm

ワークパイル(Work pile)は、並列処理の為のアルゴリズムの一種です。

lucille 開発日記: マルチスレッド化: ワークパイル

簡単に云ってしまえば、ワークパイルはサーバ/クライアント機構と似たようなもので、ジョブを処理するスレッド部分ではマスタースレッドから仕事(ジョブ)を受け取って処理し、仕事がなくなるまでループするというものです。

いわゆる生産者消費者問題を解決する手法の一つですね。
ちょっとサンプルコードを見てもらったほうがはやいかも。

workpile.rb

ruby>>
require 'thread'

class Workpile
def initialize(num_workers)
@queue = Queue.new
@workers = ThreadGroup.new

# Spawn worker threads
num_workers.times do |i|
  @workers.add(Thread.new do
    puts "Worker #%d is ready." % i
    loop do
      @queue.pop.call # work
      puts " (#%d)" % i
    end
  end)
end

end

def push_task(&block)
@queue.push block
@workers.list.sort_by{rand}.each{|worker| worker.run} # wake up!
end

def start
while input = gets.chomp
break if input == 'exit'
input.split('').each do |i|
push_task{print i}
end
end
end
end

Workpile.new(3).start
<<--

出力結果

shell>>
% ruby workpile.rb
Worker #0 is ready.
Worker #1 is ready.
Worker #2 is ready.
hello!
h (#0)
e (#2)
l (#1)
l (#0)
o (#2)
! (#0)
<<--

3つのワーカースレッドを作成して、投入したタスクを処理しています。
RubyのQueueは同期の面倒を見てくれるので、何も考えずにpush
popでタスクのやり取りを行うことができてしまいます。
しかも、キューが空になったらカレントスレッドを停止するように
なってるんですね。

並列処理というとC/C++でCriticalSectionやmutexのネイティブAPIを
さわるようなのばかりだったので、こんなに簡潔に記述できるのは
良いですね。やはりインターフェイス重要だな。

クリスマスに出る(という噂の)Ruby 1.9でネイティブスレッド化されたら、
もっと楽しいかもしれませんねー。

posted by genki genki on Fri 7 Dec 2007 at 06:14 with 0 comments

MH (Metropolis Hastings) アルゴリズムの拡張。

The Multiple-Try Method and Local Optimization in Metropolis Sampling

Jun S. Liu, Faming Liang, Wing Hung Wong
Journal of the American Statistical Association, Vol. 95, No. 449 (Mar., 2000), pp. 121-134
doi:10.2307/2669532

なかなか楽しそう。

posted by genki genki on Sun 11 Nov 2007 at 07:47 with 0 comments
Contents rssrss
Leading Smart TV App Development Companies | USA | 4 Way Technologies
Apple TV Developer | TVOS Development | USA | 4 Way Technologies
光ファイバーを二次元振動させて走査するAR用ディスプレイ
因果の取り違え
Value Transformer
Swift2's defer for CoffeeScript
mongodb-3.0からcreateIndexのdropDupsが無くなったらしい
mongodb-3.0以降のWiredTigerの設定を動的に変更する方法
一般楕円の高速生成アルゴリズムへの道標
farro mantecatoのレシピ
Tags
algorithm
Comments rssrss
https://rootskitchen.bar/ https://rootskitchen.bar/: Finished a deadly evening session punting on crash... 04/18 12:17
https://www.oyeanuncios.com/profile/margueritecava https://www.oyeanuncios.com/profile/margueritecava: Hat jemand Tipps gegen die leichte Übelkeit in der... 04/17 23:02
https://athworldproperties.com/author/deangelonewhou/ https://athworldproperties.com/author/deangelonewhou/: Mein Stoffwechsel fühlt sich zum ersten Mal seit J... 04/17 23:00
https://hyperharmony.com/author-profile/shannaminifie/ https://hyperharmony.com/author-profile/shannaminifie/: Endlich ist dieses ständige „Food Noise“ im Kopf k... 04/17 22:49
https://rootskitchen.bar/ https://rootskitchen.bar/: Just wrapped up a cracking arvo session grinding c... 04/17 02:19
https://www.legnostyle.ru/catalog/mebel/spalni/sp-3.html https://www.legnostyle.ru/catalog/mebel/spalni/sp-3.html: Мебель из Германии славится на весь мир своими изы... 04/16 15:20
https://www.hampshireairfields.eu.com/ https://www.hampshireairfields.eu.com/: 优质xxx平台 提供高质量的成人娱乐内容。选择 有保障的平台 以获得安全且愉快的观看体验。 04/16 13:16
https://slon8.at-slon6.cc https://slon8.at-slon6.cc: Superb, what a webpage it is! This weblog presents... 04/15 21:27