[1ed5e9e] | 1 | #include <thread.hfa> |
---|
| 2 | |
---|
| 3 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 4 | // cofor ( uC++ COFOR ) |
---|
| 5 | typedef void (*cofor_body_t)( long ); |
---|
| 6 | thread co_runner { |
---|
| 7 | long lo, hi; |
---|
| 8 | cofor_body_t loop_body; |
---|
| 9 | }; |
---|
| 10 | |
---|
| 11 | void ?{}( co_runner & this, long lo, long hi, cofor_body_t loop_body ) { |
---|
| 12 | this.lo = lo; |
---|
| 13 | this.hi = hi; |
---|
| 14 | this.loop_body = loop_body; |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | void main( co_runner & this ) with( this ) { |
---|
| 18 | for ( long i = lo; i < hi; i++ ) |
---|
| 19 | loop_body(i); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | void cofor( long lo, long hi, cofor_body_t loop_body ) { |
---|
| 23 | long range = hi - lo; |
---|
| 24 | if ( range <= 0 ) return; |
---|
| 25 | long nprocs = get_proc_count( *active_cluster() ); |
---|
| 26 | if ( nprocs == 0 ) return; |
---|
| 27 | long threads = range < nprocs ? range : nprocs; |
---|
| 28 | long stride = range / threads + 1, extras = range % threads; |
---|
| 29 | long i = 0; |
---|
| 30 | long stride_iter = lo; |
---|
| 31 | co_runner * runners[ threads ]; |
---|
| 32 | for ( i; threads ) { |
---|
| 33 | runners[i] = alloc(); |
---|
| 34 | } |
---|
| 35 | for ( i = 0; i < extras; i += 1, stride_iter += stride ) { |
---|
| 36 | (*runners[i]){ stride_iter, stride_iter + stride, loop_body }; |
---|
| 37 | } |
---|
| 38 | stride -= 1; |
---|
| 39 | for ( ; i < threads; i += 1, stride_iter += stride ) { |
---|
| 40 | (*runners[i]){ stride_iter, stride_iter + stride, loop_body }; |
---|
| 41 | } |
---|
| 42 | for ( i; threads ) { |
---|
| 43 | delete( runners[i] ); |
---|
| 44 | } |
---|
| 45 | } |
---|
| 46 | |
---|
[334e0cf2] | 47 | #define COFOR( lidname, low, high, body ) \ |
---|
| 48 | { \ |
---|
| 49 | void loopBody( long lidname ) { \ |
---|
| 50 | body \ |
---|
| 51 | } \ |
---|
| 52 | cofor( low, high, loopBody ); \ |
---|
| 53 | } |
---|
| 54 | |
---|
[1ed5e9e] | 55 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 56 | // parallel (COBEGIN/COEND) |
---|
| 57 | typedef void (*parallel_stmt_t)( void * ); |
---|
| 58 | thread para_runner { |
---|
| 59 | parallel_stmt_t body; |
---|
| 60 | void * arg; |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | void ?{}( para_runner & this, parallel_stmt_t body, void * arg ) { |
---|
| 64 | this.body = body; |
---|
| 65 | this.arg = arg; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void main( para_runner & this ) with( this ) { body( arg ); } |
---|
| 69 | |
---|
| 70 | void parallel( parallel_stmt_t * stmts, void ** args, size_t num ) { |
---|
| 71 | para_runner * runners[ num ]; |
---|
| 72 | for ( i; num ) |
---|
| 73 | (*(runners[i] = malloc())){ stmts[i], args[i] }; |
---|
| 74 | for ( i; num ) |
---|
| 75 | delete( runners[i] ); |
---|
| 76 | } |
---|
| 77 | |
---|