R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

Parallel Extensions(並列プログラミング)

.NET Framework 4.0 の Parallel Extensions(並列プログラミング)を使うと、マルチスレッドなプログラムが簡単に作れるんですね。
以下は、重たい処理を 2 回実行する従来の処理方法です。

日本語でメソッドっぽくないけど、メソッドですw


重い処理(0);
重い処理(1);


お馴染みの書き方ですね。
重たい処理(); が終わると、次の重い処理(); が実行されます。


using System.Threading.Tasks;

Task t0 = Task.StartNew(() => 重い処理(0));
Task t1 = Task.StartNew(() => 重い処理(1));


こう書くと、最初の重い処理の終了を待たずに、次の重い処理が実行されるらしいです。
簡単だなー。

for や foreach や LINQ などでも、並列処理が可能です。


通常
for (int i = 0; i < 10; ++i) 重たい処理(i);
foreach(var x in collection) 重たい処理(x);
var q = from x in collection select x;

並列
Parallel.For(0, 10, i => 重たい処理(i));
Parallel.Foreach(collection, x => 重たい処理(x));
var q = from x in collection.AsParallel() select x;


素晴しき並列処理

実装処理も、それを実現するテクノロジも全く違うものですが、これらを使って、従来の構文に似せることで敷居をさげているのですね。

#ちなみにシングルコアなプロセッサだと、何か意味があるのだろうか?

投稿日時 : 2009年3月17日 14:22

Feedback

# re: Parallel Extensions(並列プログラミング) 2009/03/17 14:43 επιστημη

> シングルコアなプロセッサだと

その重たい処理がfileやsocketなどのIOを待っているのであれば、その間他のスレッドが動けるならば全体のパフォーマンスを上げることができるます。

# re: Parallel Extensions(並列プログラミング) 2009/03/17 15:17 aetos

もちろん、それぞれの重い処理は独立していなきゃならんという点は従来通りなわけですが。
この辺、簡単に書けちゃうがために、マルチスレッドに起因するバグが出てきそうな気もしますね(そういう批判は間違いなく出てくると思います)。

# re: Parallel Extensions(並列プログラミング) 2009/03/17 20:30 ちゃっぴ

将来は single core な場合、並列化しないとかやってくれないかなぁと。

# re: Parallel Extensions(並列プログラミング) 2009/03/18 14:59 R・田中一郎

επιστημη さん

>その重たい処理がfileやsocketなどのIOを待っているのであれば、その間他のスレッドが動けるならば全体のパフォーマンスを上げることができるます。

なるほど、本来マルチスレッド化を検討すべき場面では有効ってことですよね。

--------------------------------------------------------------------------------
aetos さん

>この辺、簡単に書けちゃうがために、マルチスレッドに起因するバグが出てきそうな気もしますね

使い慣れている人にとっては、簡単に書けることはメリットではあるけれど、そうでない人はハマることになりますね。
そういえば、インテルさんで、この手の処理のデバッグツールとか提供していたような記憶があります。

--------------------------------------------------------------------------------
ちゃっぴ さん

>将来は single core な場合、並列化しないとかやってくれないかなぁと。

逆にオーバーヘッドかかって遅くなっちゃってもアレですもんね。

# re: Parallel Extensions(並列プログラミング) 2009/03/18 22:58 Jitta

> もちろん、それぞれの重い処理は独立していなきゃならんという点は従来通りなわけですが。
> この辺、簡単に書けちゃうがために、マルチスレッドに起因するバグが出てきそうな気もしますね(そういう批判は間違いなく出てくると思います)。

げ!怖い妄想しちゃいました。
→バブル ソートは重いから、並列化しよう。

# re: Parallel Extensions(並列プログラミング) 2009/03/19 13:03 R・田中一郎

バブルだけに、簡単にはじけそうな仕様ですねw

# re: Parallel Extensions(並列プログラミング) 2009/05/12 15:35 koie

かたちが昔futureといっていたものににていますね。
これのメリットは結果待ち構文が特に必要ないので
逐次プログラムを自然に並行プログラムに拡張できることです。

# re: Parallel Extensions(並列プログラミング) 2009/05/13 8:58 R・田中一郎

意味がよく理解できなかったのですが・・・・

>これのメリットは結果待ち構文が特に必要ないので

何と比較した話なのかはわかりませんが、現在のマルチスレッドも結果待ちが必要でないマルチスレッドの実現方法もありますし、随時プログラムを自然にマルチスレッド化したら、とんでもないことになる際の懸念についての話題が、直前のコメントで取り上げられていますよね?

# each time i used to read smaller content which as well clear their motive, and that is also happening with this article which I am reading at this time. 2018/10/05 13:57 each time i used to read smaller content which as

each time i used to read smaller content which as well clear their motive, and that is also happening with this article which I am reading at this time.

# A person necessarily help to make critically articles I'd state. That is the very first time I frequented your web page and to this point? I amazed with the analysis you made to make this particular post incredible. Wonderful activity! 2019/04/07 20:49 A person necessarily help to make critically artic

A person necessarily help to make critically articles I'd state.
That is the very first time I frequented your web page
and to this point? I amazed with the analysis you made to make this particular post incredible.
Wonderful activity!

# I feel this is one of the so much significant information for me. And i'm glad reading your article. However should commentary on some basic issues, The website taste is perfect, the articles is actually excellent : D. Excellent process, cheers 2019/07/31 23:30 I feel this is one of the so much significant info

I feel this is one of the so much significant information for me.
And i'm glad reading your article. However should commentary on some basic issues,
The website taste is perfect, the articles is actually excellent : D.
Excellent process, cheers

# I feel this is one of the so much significant information for me. And i'm glad reading your article. However should commentary on some basic issues, The website taste is perfect, the articles is actually excellent : D. Excellent process, cheers 2019/07/31 23:31 I feel this is one of the so much significant info

I feel this is one of the so much significant information for me.
And i'm glad reading your article. However should commentary on some basic issues,
The website taste is perfect, the articles is actually excellent : D.
Excellent process, cheers

# I feel this is one of the so much significant information for me. And i'm glad reading your article. However should commentary on some basic issues, The website taste is perfect, the articles is actually excellent : D. Excellent process, cheers 2019/07/31 23:32 I feel this is one of the so much significant info

I feel this is one of the so much significant information for me.
And i'm glad reading your article. However should commentary on some basic issues,
The website taste is perfect, the articles is actually excellent : D.
Excellent process, cheers

# I feel this is one of the so much significant information for me. And i'm glad reading your article. However should commentary on some basic issues, The website taste is perfect, the articles is actually excellent : D. Excellent process, cheers 2019/07/31 23:33 I feel this is one of the so much significant info

I feel this is one of the so much significant information for me.
And i'm glad reading your article. However should commentary on some basic issues,
The website taste is perfect, the articles is actually excellent : D.
Excellent process, cheers

タイトル
名前
Url
コメント