Changeset a573c22 for doc/papers
- Timestamp:
- Feb 6, 2020, 10:27:01 AM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 53a49cc
- Parents:
- b0795be
- Location:
- doc/papers/concurrency/examples
- Files:
-
- 1 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/papers/concurrency/examples/Fib.py
rb0795be ra573c22 4 4 while True: 5 5 fn = fn1 + fn2; fn2 = fn1; fn1 = fn; yield fn 6 7 8 6 9 7 f1 = Fib() … … 14 12 # Local Variables: # 15 13 # tab-width: 4 # 16 # compile-command: "python3. 5Fib.py" #14 # compile-command: "python3.7 Fib.py" # 17 15 # End: # -
doc/papers/concurrency/examples/Fib2.c
rb0795be ra573c22 1 1 #include <stdio.h> 2 2 3 void mary() {4 printf( "MARY\n" );5 }6 7 3 #define FIB_INIT { 0 } 8 typedef struct { int next; int fn1, fn2; } Fib;4 typedef struct { int restart; int fn1, fn2; } Fib; 9 5 int fib( Fib * f ) { 10 static void * states[] = { &&s1, &&s2, &&s3 }; 11 goto *states[f->next]; 6 static void * states[] = { &&s0, &&s1, &&s2 }; 7 goto *states[f->restart]; 8 s0: 9 f->fn1 = 0; 10 f->restart = 1; 11 return f->fn1; 12 12 s1: 13 mary();14 f->fn1 = 0;15 f->next = 1;16 return f->fn1;17 s2:18 mary();19 13 f->fn2 = f->fn1; 20 14 f->fn1 = 1; 21 f-> next = 2;15 f->restart = 2; 22 16 return f->fn1; 23 s3:; 24 mary(); 17 s2:; 25 18 int fn = f->fn1 + f->fn2; 26 19 f->fn2 = f->fn1; -
doc/papers/concurrency/examples/Fib2.py
rb0795be ra573c22 1 1 def Fib(): 2 fn1, fn = 0, 12 fn1, fn = 1, 0 3 3 while True: 4 yield fn 14 yield fn 5 5 fn1, fn = fn, fn1 + fn 6 6 … … 12 12 # Local Variables: # 13 13 # tab-width: 4 # 14 # compile-command: "python3. 5Fib2.py" #14 # compile-command: "python3.7 Fib2.py" # 15 15 # End: # -
doc/papers/concurrency/examples/Fib3.c
rb0795be ra573c22 2 2 3 3 typedef struct { 4 int fn1, fn; 5 void * next; 4 int restart, fn1, fn; 6 5 } Fib; 7 #define FibCtor { 1, 0, NULL}6 #define FibCtor { 0, 1, 0 } 8 7 9 8 Fib * comain( Fib * f ) { 10 if ( __builtin_expect(f->next != 0, 1) ) goto *f->next; 11 f->next = &&s1; 9 static void * states[] = {&&s0, &&s1}; 10 goto *states[f->restart]; 11 s0: f->restart = 1; 12 12 for ( ;; ) { 13 13 return f; -
doc/papers/concurrency/examples/FibRefactor.py
rb0795be ra573c22 22 22 # Local Variables: # 23 23 # tab-width: 4 # 24 # compile-command: "python3. 5FibRefactor.py" #24 # compile-command: "python3.7 FibRefactor.py" # 25 25 # End: # -
doc/papers/concurrency/examples/Format.c
rb0795be ra573c22 2 2 3 3 typedef struct { 4 void * next;4 int restart, g, b; 5 5 char ch; 6 int g, b;7 6 } Fmt; 8 7 9 8 void comain( Fmt * f ) { 10 if ( __builtin_expect(f->next != 0, 1) ) goto *f->next; 11 f->next = &&s1; 9 static void * states[] = {&&s0, &&s1}; 10 goto *states[f->restart]; 11 s0: f->restart = 1; 12 12 for ( ;; ) { 13 13 for ( f->g = 0; f->g < 5; f->g += 1 ) { // groups 14 14 for ( f->b = 0; f->b < 4; f->b += 1 ) { // blocks 15 return; 16 s1:; while ( f->ch == '\n' ) return; // ignore 15 do { 16 return; s1: ; 17 } while ( f->ch == '\n' ); // ignore 17 18 printf( "%c", f->ch ); // print character 18 19 } … … 24 25 25 26 int main() { 26 Fmt fmt = { NULL};27 Fmt fmt = { 0 }; 27 28 comain( &fmt ); // prime 28 29 for ( ;; ) { -
doc/papers/concurrency/examples/Format.cc
rb0795be ra573c22 6 6 for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks 7 7 for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters 8 //for ( ;; ) { // for newline characters8 for ( ;; ) { // for newline characters 9 9 suspend(); 10 //if ( ch != '\n' ) break; // ignore newline11 //}10 if ( ch != '\n' ) break; // ignore newline 11 } 12 12 // cout << ch; // print character 13 13 } … … 31 31 // Local Variables: // 32 32 // tab-width: 4 // 33 // compile-command: "u++-work -O2 -nodebu bg Format.cc" //33 // compile-command: "u++-work -O2 -nodebug Format.cc" // 34 34 // End: // -
doc/papers/concurrency/examples/Format.cfa
rb0795be ra573c22 11 11 for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks 12 12 for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters 13 //do {13 do { 14 14 suspend(); 15 //} while ( ch == '\n' || ch == '\t' );15 } while ( ch == '\n' || ch == '\t' ); 16 16 sout | ch; // print character 17 17 } -
doc/papers/concurrency/examples/Format.data
rb0795be ra573c22 1 abcdefghijklmnopqrstuvwxyzxxxxxxxxxxxxxx 1 abcdefghijklmnop 2 qrstuvwxyzx 3 xxxxxxxxxxxxx -
doc/papers/concurrency/examples/Format.py
rb0795be ra573c22 4 4 for g in range( 5 ): # groups of 5 blocks 5 5 for b in range( 4 ): # blocks of 4 characters 6 print( (yield), end='' ) # receive from send 6 while True: 7 ch = (yield) # receive from send 8 if '\n' not in ch: 9 break 10 print( ch, end='' ) # receive from send 7 11 print( ' ', end='' ) # block separator 8 12 print() # group separator … … 11 15 print() 12 16 17 input = "abcdefghijklmnop\nqrstuvwx\nyzxxxxxxxxxxxxxx\n" 18 13 19 fmt = Format() 14 20 next( fmt ) # prime generator 15 for i in range( 41 ):16 fmt.send( 'a'); # send to yield21 for i in input: 22 fmt.send( i ); # send to yield 17 23 18 24 # Local Variables: # 19 25 # tab-width: 4 # 20 # compile-command: "python3. 5Format.py" #26 # compile-command: "python3.7 Format.py" # 21 27 # End: # -
doc/papers/concurrency/examples/Format1.c
rb0795be ra573c22 2 2 3 3 typedef struct { 4 void * next;4 int restart, g, b; 5 5 char ch; 6 int g, b;7 6 } Fmt; 8 7 9 8 void format( Fmt * f ) { 10 if ( __builtin_expect(f->next != 0, 1) ) goto *f->next; 11 f->next = &&s1; 9 static void * states[] = {&&s0, &&s1}; 10 goto *states[f->restart]; 11 s0: f->restart = 1; 12 12 for ( ;; ) { 13 13 for ( f->g = 0; f->g < 5; f->g += 1 ) { // groups 14 14 for ( f->b = 0; f->b < 4; f->b += 1 ) { // blocks 15 15 return; 16 s1: ; 17 if ( f->ch == '\0' ) goto fini; // EOF ? 16 s1: if ( f->ch == '\0' ) goto fini; // EOF ? 18 17 while ( f->ch == '\n' ) return; // ignore 19 printf( "%c", f->ch ); // print character18 // printf( "%c", f->ch ); // print character 20 19 } 21 printf( " " ); // block separator20 // printf( " " ); // block separator 22 21 } 23 printf( "\n" ); // group separator22 // printf( "\n" ); // group separator 24 23 } 25 fini: 26 if ( f->g != 0 || f->b != 0 ) printf( "\n" );24 fini:; 25 // if ( f->g != 0 || f->b != 0 ) printf( "\n" ); 27 26 } 28 27 29 28 int main() { 30 Fmt fmt = { NULL};29 Fmt fmt = { 0 }; 31 30 format( &fmt ); // prime 32 for ( ;; ) { 33 scanf( "%c", &fmt.ch ); // direct read into communication variable 34 if ( feof( stdin ) ) break; 31 fmt.ch = 'a'; 32 for ( long int i = 0; i < 1000000000; i += 1 ) { 33 // scanf( "%c", &fmt.ch ); // direct read into communication variable 34 // if ( feof( stdin ) ) break; 35 35 format( &fmt ); 36 36 } 37 fmt.ch = '\0'; 37 fmt.ch = '\0'; // sentential (EOF) 38 38 format( &fmt ); 39 39 } -
doc/papers/concurrency/examples/PingPong.c
rb0795be ra573c22 2 2 3 3 typedef struct PingPong { 4 int restart; // style 1 5 int N, i; 4 6 const char * name; 5 int N, i;6 7 struct PingPong * partner; 7 void * next; 8 void * next; // style 2 8 9 } PingPong; 9 #define PPCtor( name, N ) { name, N, 0, NULL, NULL } 10 #define PPCtor( name, N ) { 0, N, 0, name, NULL, NULL } 11 10 12 void comain( PingPong * pp ) __attribute__(( noinline )); 11 13 void comain( PingPong * pp ) { 14 #if 0 12 15 if ( __builtin_expect(pp->next != 0, 1) ) goto *pp->next; 13 #if 014 pp->next = &&here;15 asm( "mov %0,%%rdi" : "=m" (pp) );16 asm( "mov %rdi,%rax" );17 #ifndef OPT18 #ifdef PRINT19 asm( "add $16, %rsp" );20 #endif // PRINT21 asm( "popq %rbp" );22 #endif // ! OPT23 24 #ifdef OPT25 #ifdef PRINT26 asm( "popq %rbx" );27 #endif // PRINT28 #endif // OPT29 asm( "jmp comain" );30 here: ;31 #endif // 032 33 16 pp->next = &&cycle; 34 17 for ( ; pp->i < pp->N; pp->i += 1 ) { … … 53 36 cycle: ; 54 37 } // for 38 #endif // 0 39 40 #if 1 41 static void * states[] = {&&s0, &&s1}; 42 goto *states[pp->restart]; 43 s0: pp->restart = 1; 44 for ( ; pp->i < pp->N; pp->i += 1 ) { 45 #ifdef PRINT 46 printf( "%s %d\n", pp->name, pp->i ); 47 #endif // PRINT 48 asm( "mov %0,%%rdi" : "=m" (pp->partner) ); 49 asm( "mov %rdi,%rax" ); 50 #ifndef OPT 51 #ifdef PRINT 52 asm( "add $16, %rsp" ); 53 #endif // PRINT 54 asm( "popq %rbp" ); 55 #endif // ! OPT 56 57 #ifdef OPT 58 #ifdef PRINT 59 asm( "popq %rbx" ); 60 #endif // PRINT 61 #endif // OPT 62 asm( "jmp comain" ); 63 s1: ; 64 } // for 65 #endif // 0 55 66 } 56 67 … … 70 81 // Local Variables: // 71 82 // tab-width: 4 // 72 // compile-command: "gcc- 8-g -DPRINT PingPong.c" //83 // compile-command: "gcc-9 -g -DPRINT PingPong.c" // 73 84 // End: // -
doc/papers/concurrency/examples/Pingpong.py
rb0795be ra573c22 1 1 def PingPong( name, N ): 2 partner = (yield)# get partner3 yield 2 partner = yield # get partner 3 yield # resume scheduler 4 4 for i in range( N ): 5 5 print( name ) 6 yield partner 6 yield partner # execute next 7 7 print( "end", name ) 8 8 9 9 def Scheduler(): 10 n = (yield) # starting coroutine 11 while True: 12 n = next( n ) # schedule coroutine 10 n = yield # starting coroutine 11 try: 12 while True: 13 n = next( n ) # schedule coroutine 14 except StopIteration: 15 pass 13 16 14 17 pi = PingPong( "ping", 5 ) 15 18 po = PingPong( "pong", 5 ) 16 next( pi ) 17 pi.send( po ) 18 next( po ) 19 po.send( pi ) 19 next( pi ) # prime 20 pi.send( po ) # send partner 21 next( po ) # prime 22 po.send( pi ) # send partner 20 23 21 24 s = Scheduler(); 22 next( s ) 25 next( s ) # prime 23 26 try: 24 27 s.send( pi ) # start cycle 25 except StopIteration: 26 p rint( "scheduler stop" )28 except StopIteration: # scheduler stopped 29 pass 27 30 print( "stop" ) 28 31 29 32 # Local Variables: # 30 33 # tab-width: 4 # 31 # compile-command: "python3. 5Pingpong.py" #34 # compile-command: "python3.7 Pingpong.py" # 32 35 # End: # -
doc/papers/concurrency/examples/ProdCons.py
rb0795be ra573c22 1 1 def Prod( N ): 2 cons = (yield)# get cons3 yield 2 cons = yield # get cons 3 yield # resume scheduler 4 4 for i in range( N ): 5 5 print( "prod" ) 6 yield cons 6 yield cons # execute next 7 7 print( "end", "prod" ) 8 8 9 9 def Cons( N ): 10 prod = (yield)# get prod11 yield 10 prod = yield # get prod 11 yield # resume scheduler 12 12 for i in range( N ): 13 13 print( "cons" ) 14 yield prod 14 yield prod # execute next 15 15 print( "end", "cons" ) 16 16 17 17 def Scheduler(): 18 n = (yield) # starting coroutine 19 while True: 20 n = next( n ) # schedule coroutine 18 n = yield # starting coroutine 19 try: 20 while True: 21 n = next( n ) # schedule coroutine 22 except StopIteration: 23 pass 21 24 22 25 prod = Prod( 5 ) 23 26 cons = Cons( 5 ) 24 next( prod ) 25 prod.send( cons ) 26 next( cons ) 27 cons.send( prod ) 27 next( prod ) # prime 28 prod.send( cons ) # send cons 29 next( cons ) # prime 30 cons.send( prod ) # send prod 28 31 29 32 s = Scheduler(); 30 next( s ) 33 next( s ) # prime 31 34 try: 32 35 s.send( prod ) # start cycle 33 except StopIteration: 34 p rint( "scheduler stop" )36 except StopIteration: # scheduler stopped 37 pass 35 38 print( "stop" ) 36 39 37 40 # Local Variables: # 38 41 # tab-width: 4 # 39 # compile-command: "python3. 5ProdCons.py" #42 # compile-command: "python3.7 ProdCons.py" # 40 43 # End: # -
doc/papers/concurrency/examples/Refactor.py
rb0795be ra573c22 26 26 # Local Variables: # 27 27 # tab-width: 4 # 28 # compile-command: "python3. 5Refactor.py" #28 # compile-command: "python3.7 Refactor.py" # 29 29 # End: #
Note: See TracChangeset
for help on using the changeset viewer.