c# 並行運算
1.parallel.invoke()看例項:
private看結果: 是不是parallel.for在任何時候都比for要快呢?答案當然是「不是」,要不然微軟還留著for幹嘛?static stopwatch watch = new
stopwatch();
private
static
void
run1()
private
static
void
run2()
static
void main(string
args)
}watch.stop();
console.writeline(
"normal run
" + watch.elapsedmilliseconds + "ms"
); watch.restart();
watch.start();
parallel.for(
0, 1000, item =>
});watch.stop();
console.writeline(
"parallefor run
" + watch.elapsedmilliseconds + "ms"
); console.readline();
看例項:
var obj = new結果:(結果不是穩定的,你懂得~)object
();
long num = 0
; concurrentbag
bag = new concurrentbag();
watch.start();
for (int i = 0; i < 10000; i++)
}watch.stop();
console.writeline(
"normal run
"+watch.elapsedmilliseconds+ "ms"
); watch.restart();
parallel.for(
0, 1000, item =>
}});
watch.stop();
console.writeline(
"parallefor run
" + watch.elapsedmilliseconds + "ms"
); console.readline();
再看**:
parallel.for(0, 100, i =>);再看結果:console.readline();
傻孩子,這樣你懂了吧~
3.parallel.foreach
//結果:environment.processorcount能夠獲取到當前的硬體執行緒數,所以這裡也就開了2個區。
console.writeline(environment.processorcount);
console.readline();
//繼續我的併發程式設計;
//可以將資料進行分割槽,每乙個小區內實現序列計算;分割槽採用create實現;
for (int j = 1; j < 4; j++)
次比較", j);
concurrentbag
bag = new concurrentbag();
watch.start();
for (int i = 0; i < 300000; i++)
console.writeline(
"序列計算:集合有,總共耗時:
",bag.count,watch.elapsedmilliseconds);
gc.collect();
bag = new concurrentbag();
watch.restart();
parallel.foreach(partitioner.create(
0, 300000), i =>
});console.writeline(
"平行計算:集合有:,總共耗時:
", bag.count, watch.elapsedmilliseconds);
gc.collect();
watch.stop();
}
console.readline();
4.parallel 中途退出迴圈
break: 當然這個是通知平行計算盡快的退出迴圈,比如平行計算正在迭代100,那麼break後程式還會迭代所有小於100的。
stop:這個就不一樣了,比如正在迭代100突然遇到stop,那它啥也不管了,直接退出。
concurrentbag bag = new concurrentbag();異常處理由於task的start方法是非同步啟動的,所以我們需要額外的技術來完成異常處理watch.start();
parallel.for(
0,1000, (i, state) =>
bag.add(i);
});watch.stop();
console.writeline(
"bag count is
"+bag.count+"
times is
"+watch.elapsedmilliseconds);
trycatch
(exception e)
if (parallelexceptions.count > 0
)
throw
newaggregateexception(parallelexceptions);
});
} catch
(aggregateexception err)
來自:
異常內容:"
, item.innerexception.gettype(),
environment.newline, item.innerexception.source,
environment.newline, item.innerexception.message);
} }
console.writeline(
"主線程馬上結束
");
console.readkey();
static例項二void main(string
args)
catch
(aggregateexception ex)
}console.read();
}static
void
run1()
static
void
run2()
try預設的情況下,底層機制會盡可能多的使用硬體執行緒,然而我們使用手動指定的好處是我們可以在2,4,8個硬體執行緒的情況下來進行測量加速比。catch
(exception ex)
watch.stop();
console.writeline(
"normal run :
" +watch.elapsedmilliseconds);
//尼瑪的,這樣的異常,居然捕獲不到的地呀
classprogram
);console.writeline(
"平行計算:集合有:
", bag.count);}}
fortran並行運算報錯151
mpi並行運算報錯 forrtl severe 151 allocatable array is already allocated parallel loop if condition then allocate x full nel pp some statements deallocate x...
Pig parallel reduce並行執行數
parallel語句可以附加到pig latin中任乙個關係操作符後面,然後它會控制reduce階段的並行,因此只有對與可以觸發reduce過程的操作符才有意義。可以觸發reduce過程的操作符有 group order distinct join cogroup cross 設定parallel的...
CUDA學習 矩陣乘法的並行運算
cpu實現a b c的矩陣乘法 矩陣尺寸是n m的,n和m大於1000 將cpu 移植到cuda。將cpu值傳入gpu,使用cuda計算,與cpu結果對比。優化思路1 將矩陣分塊進行計算 優化思路2 使用share memory進行優化 優化思路3 將資料繫結在texture上 廢話不多說,直接上原...