source: libcfa/src/concurrency/cofor.hfa @ a2c2363

Last change on this file since a2c2363 was 334e0cf2, checked in by caparsons <caparson@…>, 14 months ago

added cofor macro

  • Property mode set to 100644
File size: 2.1 KB
RevLine 
[1ed5e9e]1#include <thread.hfa>
2
3//////////////////////////////////////////////////////////////////////////////////////////
4// cofor ( uC++ COFOR )
5typedef void (*cofor_body_t)( long );
6thread co_runner {
7    long lo, hi;
8    cofor_body_t loop_body;
9};
10
11void ?{}( 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
17void main( co_runner & this ) with( this ) {
18    for ( long i = lo; i < hi; i++ )
19        loop_body(i);
20}
21
22void 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)
57typedef void (*parallel_stmt_t)( void * );
58thread para_runner {
59    parallel_stmt_t body;
60    void * arg;
61};
62
63void ?{}( para_runner & this, parallel_stmt_t body, void * arg ) {
64    this.body = body;
65    this.arg = arg;
66}
67
68void main( para_runner & this ) with( this ) { body( arg ); }
69
70void 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
Note: See TracBrowser for help on using the repository browser.