Changeset e4c3819 for libcfa/src/concurrency
- Timestamp:
- Sep 19, 2023, 10:26:17 AM (15 months ago)
- Branches:
- master
- Children:
- 60f69f0
- Parents:
- a2c2363
- Location:
- libcfa/src/concurrency
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/cofor.hfa
ra2c2363 re4c3819 3 3 ////////////////////////////////////////////////////////////////////////////////////////// 4 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 }; 5 typedef void (*__cofor_body_t)( ssize_t ); 10 6 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 } 7 void cofor( ssize_t low, ssize_t high, __cofor_body_t loop_body ); 16 8 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 47 #define COFOR( lidname, low, high, body ) \ 48 { \ 49 void loopBody( long lidname ) { \ 50 body \ 51 } \ 52 cofor( low, high, loopBody ); \ 53 } 9 #define COFOR( lidname, low, high, loopbody ) \ 10 { \ 11 void __CFA_loopLambda__( ssize_t lidname ) { \ 12 loopbody \ 13 } \ 14 cofor( low, high, __CFA_loopLambda__ ); \ 15 } 54 16 55 17 ////////////////////////////////////////////////////////////////////////////////////////// 56 18 // parallel (COBEGIN/COEND) 57 19 typedef void (*parallel_stmt_t)( void * ); 58 thread para_runner {59 parallel_stmt_t body;60 void * arg;61 };62 20 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 21 void parallel( parallel_stmt_t * stmts, void ** args, size_t num );
Note: See TracChangeset
for help on using the changeset viewer.