Ignore:
Timestamp:
Feb 6, 2020, 10:27:01 AM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
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
Message:

example programs updated for concurrency paper

Location:
doc/papers/concurrency/examples
Files:
1 deleted
15 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/concurrency/examples/Fib.py

    rb0795be ra573c22  
    44        while True:
    55                fn = fn1 + fn2; fn2 = fn1; fn1 = fn; yield fn
    6 
    7 
    86
    97f1 = Fib()
     
    1412# Local Variables: #
    1513# tab-width: 4 #
    16 # compile-command: "python3.5 Fib.py" #
     14# compile-command: "python3.7 Fib.py" #
    1715# End: #
  • doc/papers/concurrency/examples/Fib2.c

    rb0795be ra573c22  
    11#include <stdio.h>
    22
    3 void mary() {
    4         printf( "MARY\n" );
    5 }
    6 
    73#define FIB_INIT { 0 }
    8 typedef struct { int next; int fn1, fn2; } Fib;
     4typedef struct { int restart; int fn1, fn2; } Fib;
    95int 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;
    1212  s1:
    13         mary();
    14         f->fn1 = 0;
    15         f->next = 1;
    16         return f->fn1;
    17   s2:
    18         mary();
    1913        f->fn2 = f->fn1;
    2014        f->fn1 = 1;
    21         f->next = 2;
     15        f->restart = 2;
    2216        return f->fn1;
    23   s3:;
    24         mary();
     17  s2:;
    2518        int fn = f->fn1 + f->fn2;
    2619        f->fn2 = f->fn1;
  • doc/papers/concurrency/examples/Fib2.py

    rb0795be ra573c22  
    11def Fib():
    2     fn1, fn = 0, 1
     2    fn1, fn = 1, 0
    33    while True:
    4         yield fn1
     4        yield fn
    55        fn1, fn = fn, fn1 + fn
    66
     
    1212# Local Variables: #
    1313# tab-width: 4 #
    14 # compile-command: "python3.5 Fib2.py" #
     14# compile-command: "python3.7 Fib2.py" #
    1515# End: #
  • doc/papers/concurrency/examples/Fib3.c

    rb0795be ra573c22  
    22
    33typedef struct {
    4         int fn1, fn;
    5         void * next;
     4        int restart, fn1, fn;
    65} Fib;
    7 #define FibCtor { 1, 0, NULL }
     6#define FibCtor { 0, 1, 0 }
    87
    98Fib * 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;
    1212        for ( ;; ) {
    1313                return f;
  • doc/papers/concurrency/examples/FibRefactor.py

    rb0795be ra573c22  
    2222# Local Variables: #
    2323# tab-width: 4 #
    24 # compile-command: "python3.5 FibRefactor.py" #
     24# compile-command: "python3.7 FibRefactor.py" #
    2525# End: #
  • doc/papers/concurrency/examples/Format.c

    rb0795be ra573c22  
    22
    33typedef struct {
    4         void * next;
     4        int restart, g, b;
    55        char ch;
    6         int g, b;
    76} Fmt;
    87
    98void 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;
    1212        for ( ;; ) {
    1313                for ( f->g = 0; f->g < 5; f->g += 1 ) {                 // groups
    1414                        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
    1718                                printf( "%c", f->ch );                                  // print character
    1819                        }
     
    2425
    2526int main() {
    26         Fmt fmt = { NULL };
     27        Fmt fmt = { 0 };
    2728        comain( &fmt );                                                                         // prime
    2829        for ( ;; ) {
  • doc/papers/concurrency/examples/Format.cc

    rb0795be ra573c22  
    66                        for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks
    77                                for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters
    8 //                                      for ( ;; ) { // for newline characters
     8                                        for ( ;; ) { // for newline characters
    99                                                suspend();
    10 //                                              if ( ch != '\n' ) break; // ignore newline
    11 //                                      }
     10                                                if ( ch != '\n' ) break; // ignore newline
     11                                        }
    1212//                                      cout << ch; // print character
    1313                                }
     
    3131// Local Variables: //
    3232// tab-width: 4 //
    33 // compile-command: "u++-work -O2 -nodebubg Format.cc" //
     33// compile-command: "u++-work -O2 -nodebug Format.cc" //
    3434// End: //
  • doc/papers/concurrency/examples/Format.cfa

    rb0795be ra573c22  
    1111                for ( g = 0; g < 5; g += 1 ) {          // groups of 5 blocks
    1212                        for ( b = 0; b < 4; b += 1 ) {  // blocks of 4 characters
    13 //                              do {
     13                                do {
    1414                                        suspend();
    15 //                              } while ( ch == '\n' || ch == '\t' );
     15                                } while ( ch == '\n' || ch == '\t' );
    1616                                sout | ch;                                      // print character
    1717                        }
  • doc/papers/concurrency/examples/Format.data

    rb0795be ra573c22  
    1 abcdefghijklmnopqrstuvwxyzxxxxxxxxxxxxxx
     1abcdefghijklmnop
     2qrstuvwxyzx
     3xxxxxxxxxxxxx
  • doc/papers/concurrency/examples/Format.py

    rb0795be ra573c22  
    44                        for g in range( 5 ):    # groups of 5 blocks
    55                                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
    711                                print( '  ', end='' ) # block separator
    812                        print()                                 # group separator
     
    1115                        print()
    1216
     17input = "abcdefghijklmnop\nqrstuvwx\nyzxxxxxxxxxxxxxx\n"
     18
    1319fmt = Format()
    1420next( fmt )                                                     # prime generator
    15 for i in range( 41 ):
    16         fmt.send( 'a' );                                # send to yield
     21for i in input:
     22        fmt.send( i );                          # send to yield
    1723
    1824# Local Variables: #
    1925# tab-width: 4 #
    20 # compile-command: "python3.5 Format.py" #
     26# compile-command: "python3.7 Format.py" #
    2127# End: #
  • doc/papers/concurrency/examples/Format1.c

    rb0795be ra573c22  
    22
    33typedef struct {
    4         void * next;
     4        int restart, g, b;
    55        char ch;
    6         int g, b;
    76} Fmt;
    87
    98void 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;
    1212        for ( ;; ) {
    1313                for ( f->g = 0; f->g < 5; f->g += 1 ) {                 // groups
    1414                        for ( f->b = 0; f->b < 4; f->b += 1 ) {         // blocks
    1515                                return;
    16                           s1: ;
    17                                 if ( f->ch == '\0' ) goto fini;                 // EOF ?
     16                          s1: if ( f->ch == '\0' ) goto fini;           // EOF ?
    1817                                while ( f->ch == '\n' ) return;                 // ignore
    19                                 printf( "%c", f->ch );                                  // print character
     18//                              printf( "%c", f->ch );                                  // print character
    2019                        }
    21                         printf( " " );                                                          // block separator
     20//                      printf( " " );                                                          // block separator
    2221                }
    23                 printf( "\n" );                                                                 // group separator
     22//              printf( "\n" );                                                                 // group separator
    2423        }
    25   fini:
    26         if ( f->g != 0 || f->b != 0 ) printf( "\n" );
     24  fini:;
     25//      if ( f->g != 0 || f->b != 0 ) printf( "\n" );
    2726}
    2827
    2928int main() {
    30         Fmt fmt = { NULL };
     29        Fmt fmt = { 0 };
    3130        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;
    3535                format( &fmt );
    3636        }
    37         fmt.ch = '\0';
     37        fmt.ch = '\0';                                                                          // sentential (EOF)
    3838        format( &fmt );
    3939}
  • doc/papers/concurrency/examples/PingPong.c

    rb0795be ra573c22  
    22
    33typedef struct PingPong {
     4        int restart;                                                                            // style 1
     5        int N, i;
    46        const char * name;
    5         int N, i;
    67        struct PingPong * partner;
    7         void * next;
     8        void * next;                                                                            // style 2
    89} PingPong;
    9 #define PPCtor( name, N ) { name, N, 0, NULL, NULL }
     10#define PPCtor( name, N ) { 0, N, 0, name, NULL, NULL }
     11
    1012void comain( PingPong * pp ) __attribute__(( noinline ));
    1113void comain( PingPong * pp ) {
     14#if 0
    1215        if ( __builtin_expect(pp->next != 0, 1) ) goto *pp->next;
    13 #if 0
    14         pp->next = &&here;
    15                 asm( "mov  %0,%%rdi" : "=m" (pp) );
    16                 asm( "mov  %rdi,%rax" );
    17 #ifndef OPT
    18 #ifdef PRINT
    19                 asm( "add  $16, %rsp" );
    20 #endif // PRINT
    21                 asm( "popq %rbp" );
    22 #endif // ! OPT
    23 
    24 #ifdef OPT
    25 #ifdef PRINT
    26                 asm( "popq %rbx" );
    27 #endif // PRINT
    28 #endif // OPT
    29                 asm( "jmp  comain" );
    30   here: ;
    31 #endif // 0
    32 
    3316        pp->next = &&cycle;
    3417        for ( ; pp->i < pp->N; pp->i += 1 ) {
     
    5336          cycle: ;
    5437        } // 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
    5566}
    5667
     
    7081// Local Variables: //
    7182// tab-width: 4 //
    72 // compile-command: "gcc-8 -g -DPRINT PingPong.c" //
     83// compile-command: "gcc-9 -g -DPRINT PingPong.c" //
    7384// End: //
  • doc/papers/concurrency/examples/Pingpong.py

    rb0795be ra573c22  
    11def PingPong( name, N ):
    2         partner = (yield)           # get partner
    3         yield                       # resume scheduler
     2        partner = yield                         # get partner
     3        yield                                           # resume scheduler
    44        for i in range( N ):
    55                print( name )
    6                 yield partner           # execute next
     6                yield partner                   # execute next
    77        print( "end", name )
    88
    99def 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
    1316
    1417pi = PingPong( "ping", 5 )
    1518po = PingPong( "pong", 5 )
    16 next( pi )                      # prime
    17 pi.send( po )                   # send partner
    18 next( po )                      # prime
    19 po.send( pi )                   # send partner
     19next( pi )                                              # prime
     20pi.send( po )                                   # send partner
     21next( po )                                              # prime
     22po.send( pi )                                   # send partner
    2023
    2124s = Scheduler();
    22 next( s )                       # prime
     25next( s )                                               # prime
    2326try:
    2427        s.send( pi )                            # start cycle
    25 except StopIteration:
    26         print( "scheduler stop" )
     28except StopIteration:                   # scheduler stopped
     29        pass
    2730print( "stop" )
    2831
    2932# Local Variables: #
    3033# tab-width: 4 #
    31 # compile-command: "python3.5 Pingpong.py" #
     34# compile-command: "python3.7 Pingpong.py" #
    3235# End: #
  • doc/papers/concurrency/examples/ProdCons.py

    rb0795be ra573c22  
    11def Prod( N ):
    2         cons = (yield)              # get cons
    3         yield                       # resume scheduler
     2        cons = yield                            # get cons
     3        yield                                           # resume scheduler
    44        for i in range( N ):
    55                print( "prod" )
    6                 yield cons              # execute next
     6                yield cons                              # execute next
    77        print( "end", "prod" )
    88
    99def Cons( N ):
    10         prod = (yield)              # get prod
    11         yield                       # resume scheduler
     10        prod = yield                            # get prod
     11        yield                                           # resume scheduler
    1212        for i in range( N ):
    1313                print( "cons" )
    14                 yield prod              # execute next
     14                yield prod                              # execute next
    1515        print( "end", "cons" )
    1616
    1717def 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
    2124
    2225prod = Prod( 5 )
    2326cons = Cons( 5 )
    24 next( prod )                    # prime
    25 prod.send( cons )               # send cons
    26 next( cons )                    # prime
    27 cons.send( prod )               # send prod
     27next( prod )                                    # prime
     28prod.send( cons )                               # send cons
     29next( cons )                                    # prime
     30cons.send( prod )                               # send prod
    2831
    2932s = Scheduler();
    30 next( s )                       # prime
     33next( s )                                               # prime
    3134try:
    3235        s.send( prod )                          # start cycle
    33 except StopIteration:
    34         print( "scheduler stop" )
     36except StopIteration:                   # scheduler stopped
     37        pass
    3538print( "stop" )
    3639
    3740# Local Variables: #
    3841# tab-width: 4 #
    39 # compile-command: "python3.5 ProdCons.py" #
     42# compile-command: "python3.7 ProdCons.py" #
    4043# End: #
  • doc/papers/concurrency/examples/Refactor.py

    rb0795be ra573c22  
    2626# Local Variables: #
    2727# tab-width: 4 #
    28 # compile-command: "python3.5 Refactor.py" #
     28# compile-command: "python3.7 Refactor.py" #
    2929# End: #
Note: See TracChangeset for help on using the changeset viewer.