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://shirbetreview.com/contact/ https://shirbetreview.com/contact/: اگر هدف آموزش شرط بندی باشد، متن باید بیشتر جنبه... 05/20 15:36
http://c.e.rnmn@.r.os.p.e.r.les.c@pezedium.free.fr/?a[]=%3Ca%20href=https://kaizenaire.com/sg/boarding-gate-singapore-everything-you-need-to-know/%3Ejalan%20bukit%20merah%20gateway%20theatre%20singapore%3C/a%3E%3Cmeta%20http-equiv=refresh%20content=0;url=https://kaizenaire.com/sg/boarding-gate-singapore-everything-you-need-to-know/%20/%3E http://c.e.rnmn@.r.os.p.e.r.les.c@pezedium.free.fr/?a[]=%3Ca%20href=https://kaizenaire.com/sg/boarding-gate-singapore-everything-you-need-to-know/%3Ejalan%20bukit%20merah%20gateway%20theatre%20singapore%3C/a%3E%3Cmeta%20http-equiv=refresh%20content=0;url=https://kaizenaire.com/sg/boarding-gate-singapore-everything-you-need-to-know/%20/%3E: Explore tһе leading promotions ⲟn Kaizenaire.com, ... 05/20 15:11
https://288school.ru/ https://288school.ru/: При попытке создания второго счета на один адрес к... 05/20 13:57
https://bettingkhabar.com/crash-game-bot-review/ https://bettingkhabar.com/crash-game-bot-review/: برای موضوع مقایسه سایت‌های سیب بت مطرح می‌شود، بهت... 05/20 13:47
https://hoidotquyvietnam.com/question/premium-quality-furniture-singapore-shopping-tips-for-2026-4/ https://hoidotquyvietnam.com/question/premium-quality-furniture-singapore-shopping-tips-for-2026-4/: Singapore Mattress Guide: Тhe Real Factors Thɑt Ma... 05/20 13:23
https://mobilebettingparsi.com/terms/ https://mobilebettingparsi.com/terms/: وقتی صحبت از تخته نرد شرطی می‌شود، بهتر است متن ... 05/20 11:49
https://takhtenardsharti.com/poker-bluffing-guide/ https://takhtenardsharti.com/poker-bluffing-guide/: در بعضی بررسی‌ها بیشتر حالت دامنه نام‌محور دارند و... 05/20 10:56
https://7k-games-top.com/ https://7k-games-top.com/: В лицензионном онлайн-казино промопредложения наце... 05/20 10:42