Forum Navigation
parallel library
Posted: 07/17/08 15:39:08I have been messing around with the idea of a parallel library for a while. Since D doesn't have any way to do this I came up with a way to use the ThreadPool?.
import tango.io.Stdout, tango.core.ThreadPool, tango.time.StopWatch, tango.core.Thread; private ThreadPool!() pool; static this() { setPool; } void setPool(uint size = 12) { pool = new ThreadPool!()(size); } //Parallel block void p_block(void delegate()[] blocks ...) { foreach(blk; blocks) pool.assign(blk); waitThreads; } private void waitThreads() { while(pool.activeJobs || pool.pendingJobs) Thread.sleep(0); }All the magic is in p_block.. You use it as such
p_block( { ..Code A //Process Input }, { ..Code B //Process Game Sound }, { ..Code C //Process AI }); //Process Physics //RenderThus blocks A, B, and C will all run in there own thread and p_block will wait till their done. Of course A, B, and C can't be connected.. and should avoid locking!
My test code..
void main() { int a, b, c, d, e, f, g, h, i, j, k, l; Stdout("testing").newline; StopWatch w1; w1.start; p_block ( { for(a = 0; a < 10003201; ++a){} } , { for(b = 0; b < 80213002; ++b){} } , { for(c = 0; c < 561123423; ++c){} } , { for(d = 0; d < 10003201; ++d){} } , { for(e = 0; e < 80213002; ++e){} } , { for(f = 0; f < 561123423; ++f){} } , { for(g = 0; g < 10003201; ++g){} } , { for(h = 0; h < 80213002; ++h){} } , { for(i = 0; i < 561123423; ++i){} } , { for(j = 0; j < 10003201; ++j){} } , { for(k = 0; k < 80213002; ++k){} } , { for(l = 0; l < 561123423; ++l){} } ); auto t1 = w1.stop; Stdout.format("p_block = {} {} {}", a, b, c).newline; StopWatch w2; w2.start; { for(a = 0; a < 10003201; ++a){} } { for(b = 0; b < 80213002; ++b){} } { for(c = 0; c < 561123423; ++c){} } { for(d = 0; d < 10003201; ++d){} } { for(e = 0; e < 80213002; ++e){} } { for(f = 0; f < 561123423; ++f){} } { for(g = 0; g < 10003201; ++g){} } { for(h = 0; h < 80213002; ++h){} } { for(i = 0; i < 561123423; ++i){} } { for(j = 0; j < 10003201; ++j){} } { for(k = 0; k < 80213002; ++k){} } { for(l = 0; l < 561123423; ++l){} } auto t2 = w2.stop; Stdout.format("linear = {} {} {}", a, b, c).newline; Stdout.format("p_block time: {} Linear time: {} Delta: {}", t1, t2, t2-t1).newline; } /** Output ** testing p_block = 10003201 80213002 561123423 linear = 10003201 80213002 561123423 p_block time: 4.18 Linear time: 6.87 Delta: 2.69 */On my quad core it shows 2.7 seconds faster run time.. Reducing the work for each block reduces the delta.. though I haven't seen it go negative(meaning linear was faster). This seems to be working okay, but wondering if anyone has any ideas to speed this up, look cleaner? Also tried doing a for loop.. much harder... any ideas there?