Changes in / [8a48f4b:5434d04]
- Location:
- src
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/stdlib
r8a48f4b r5434d04 10 10 // Created On : Thu Jan 28 17:12:35 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Aug 23 20:29:47201713 // Update Count : 2 2412 // Last Modified On : Mon Oct 30 17:30:09 2017 13 // Update Count : 242 14 14 // 15 15 … … 27 27 // allocation, non-array types 28 28 static inline forall( dtype T | sized(T) ) T * malloc( void ) { 29 // printf( "X1\n" );29 // printf( "* malloc\n" ); 30 30 return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 31 31 } // malloc 32 33 // static inline forall( dtype T | sized(T) ) T & malloc( void ) { 34 // int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 35 // printf( "& malloc %p\n", &p ); 36 // return p; 37 // // return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc 38 // } // malloc 32 39 33 40 extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine … … 208 215 void rand48seed( long int s ); 209 216 char rand48( void ); 217 char rand48( char l, char u ); 210 218 int rand48( void ); 211 219 unsigned int rand48( void ); 220 unsigned int rand48( unsigned int u ); 221 unsigned int rand48( unsigned int l, unsigned int u ); 212 222 long int rand48( void ); 213 223 unsigned long int rand48( void ); 224 unsigned long int rand48( unsigned long int u ); 225 unsigned long int rand48( unsigned long int l, unsigned long int u ); 214 226 float rand48( void ); 215 227 double rand48( void ); -
src/libcfa/stdlib.c
r8a48f4b r5434d04 10 10 // Created On : Thu Jan 28 17:10:29 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Aug 23 20:30:44 201713 // Update Count : 29 212 // Last Modified On : Mon Oct 30 17:53:04 2017 13 // Update Count : 295 14 14 // 15 15 … … 277 277 void rand48seed( long int s ) { srand48( s ); } 278 278 char rand48( void ) { return mrand48(); } 279 char rand48( char l, char u ) { return lrand48() % (u - l) + l; } 279 280 int rand48( void ) { return mrand48(); } 280 281 unsigned int rand48( void ) { return lrand48(); } 282 unsigned int rand48( unsigned int u ) { return lrand48() % u; } 283 unsigned int rand48( unsigned int l, unsigned int u ) { return lrand48() % (u - l) + l; } 281 284 long int rand48( void ) { return mrand48(); } 282 285 unsigned long int rand48( void ) { return lrand48(); } 286 unsigned long int rand48( unsigned long int u ) { return lrand48() % u; } 287 unsigned long int rand48( unsigned long int l, unsigned long int u ) { return lrand48() % (u - l) + l; } 283 288 float rand48( void ) { return (float)drand48(); } // otherwise float uses lrand48 284 289 double rand48( void ) { return drand48(); } -
src/tests/boundedBuffer.c
r8a48f4b r5434d04 1 // 2 // The contents of this file are covered under the licence agreement in the 3 // file "LICENCE" distributed with Cforall. 4 // 5 // boundedBuffer.c -- 6 // 7 // Author : Peter A. Buhr 8 // Created On : Mon Oct 30 12:45:13 2017 9 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Mon Oct 30 18:00:10 2017 11 // Update Count : 7 12 // 13 1 14 #include <stdlib> 2 15 #include <fstream> … … 6 19 7 20 monitor Buffer { 8 9 10 21 condition full, empty; 22 int front, back, count; 23 int elements[20]; 11 24 }; 12 25 13 26 void ?{}( Buffer & buffer ) { 14 27 buffer.front = buffer.back = buffer.count = 0; 15 28 } 16 29 … … 18 31 19 32 void insert( Buffer & mutex buffer, int elem ) { 20 21 22 23 24 33 if ( buffer.count == 20 ) wait( &buffer.empty ); 34 buffer.elements[buffer.back] = elem; 35 buffer.back = ( buffer.back + 1 ) % 20; 36 buffer.count += 1; 37 signal( &buffer.full ); 25 38 } 26 39 int remove( Buffer & mutex buffer ) { 27 28 29 30 31 32 40 if ( buffer.count == 0 ) wait( &buffer.full ); 41 int elem = buffer.elements[buffer.front]; 42 buffer.front = ( buffer.front + 1 ) % 20; 43 buffer.count -= 1; 44 signal( &buffer.empty ); 45 return elem; 33 46 } 34 47 35 48 thread Producer { 36 Buffer *buffer;37 49 Buffer & buffer; 50 unsigned int N; 38 51 }; 39 52 void main( Producer & prod ) { 40 41 yield( (unsigned int)rand48() % 5);42 insert( *prod.buffer, 1 );43 44 insert( *prod.buffer, -1 );53 for ( int i = 1; i <= prod.N; i += 1 ) { 54 yield( rand48( 5 ) ); 55 insert( prod.buffer, 1 ); 56 } // for 57 insert( prod.buffer, -1 ); 45 58 } 46 59 void ?{}( Producer & prod, Buffer * buffer, unsigned int N ) { 47 48 49 }60 &prod.buffer = buffer; 61 prod.N = N; 62 } 50 63 51 64 thread Consumer { 52 Buffer *buffer;53 int *sum; // summation of producer values65 Buffer & buffer; 66 int & sum; // summation of producer values 54 67 }; 55 68 void main( Consumer & cons ) { 56 *cons.sum = 0;57 58 yield( (unsigned int)rand48() % 5);59 int item = remove( *cons.buffer );60 61 *cons.sum += item;62 69 cons.sum = 0; 70 for ( ;; ) { 71 yield( rand48( 5 ) ); 72 int item = remove( cons.buffer ); 73 if ( item == -1 ) break; // sentinel ? 74 cons.sum += item; 75 } // for 63 76 } 64 77 void ?{}( Consumer & cons, Buffer * buffer, int * sum ) { 65 66 78 &cons.buffer = buffer; 79 &cons.sum = sum; 67 80 } 68 81 69 82 int main() { 70 71 72 73 74 75 76 77 83 Buffer buffer; 84 enum { Prods = 5, Cons = 5 }; 85 Producer * prods[Prods]; 86 Consumer * cons[Cons]; 87 const int Sentinel = -1; 88 int sums[Cons]; 89 int i; 90 processor p; 78 91 79 80 92 //rand48seed( getpid() ); 93 rand48seed( 1003 ); 81 94 82 83 cons[i] = new( &buffer, &sums[i] );84 85 86 prods[i] = new( &buffer, 100000u );87 95 for ( i = 0; i < Cons; i += 1 ) { // create consumers 96 cons[i] = new( &buffer, &sums[i] ); 97 } // for 98 for ( i = 0; i < Prods; i += 1 ) { // create producers 99 prods[i] = new( &buffer, 100000u ); 100 } // for 88 101 89 90 delete( prods[i] );91 92 93 insert( buffer, Sentinel );94 95 96 97 delete( cons[i] );98 sum += sums[i];99 100 102 for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish 103 delete( prods[i] ); 104 } // for 105 for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers 106 insert( buffer, Sentinel ); 107 } // for 108 int sum = 0; 109 for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish 110 delete( cons[i] ); 111 sum += sums[i]; 112 } // for 113 sout | "total:" | sum | endl; 101 114 } 115 116 // Local Variables: // 117 // tab-width: 4 // 118 // compile-command: "cfa boundedBuffer.c" // 119 // End: // -
src/tests/matrixSum.c
r8a48f4b r5434d04 11 11 // Created On : Mon Oct 9 08:29:28 2017 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Mon Oct 9 08:30:08 201714 // Update Count : 113 // Last Modified On : Sun Oct 29 21:08:48 2017 14 // Update Count : 2 15 15 // 16 16 … … 20 20 21 21 thread Adder { 22 int * row, cols, * subtotal; // communication22 int * row, cols, * subtotal; // communication 23 23 }; 24 24 … … 32 32 *adder.subtotal = 0; 33 33 for ( int c = 0; c < adder.cols; c += 1 ) { 34 *adder.subtotal += adder.row[c];34 *adder.subtotal += adder.row[c]; 35 35 } // for 36 36 } … … 39 39 const int rows = 10, cols = 1000; 40 40 int matrix[rows][cols], subtotals[rows], total = 0; 41 processor p; // extra kernel thread41 processor p; // extra kernel thread 42 42 43 43 for ( int r = 0; r < rows; r += 1 ) { 44 for ( int c = 0; c < cols; c += 1 ) {45 46 } // for44 for ( int c = 0; c < cols; c += 1 ) { 45 matrix[r][c] = 1; 46 } // for 47 47 } // for 48 48 Adder * adders[rows]; 49 for ( int r = 0; r < rows; r += 1 ) { // start threads to sum rows50 adders[r] = &(*malloc()){ matrix[r], cols, subtotals[r] };51 // adders[r] = new( matrix[r], cols, &subtotals[r] );49 for ( int r = 0; r < rows; r += 1 ) { // start threads to sum rows 50 adders[r] = &(*malloc()){ matrix[r], cols, subtotals[r] }; 51 // adders[r] = new( matrix[r], cols, &subtotals[r] ); 52 52 } // for 53 for ( int r = 0; r < rows; r += 1 ) { // wait for threads to finish54 delete( adders[r] );55 total += subtotals[r];// total subtotals53 for ( int r = 0; r < rows; r += 1 ) { // wait for threads to finish 54 delete( adders[r] ); 55 total += subtotals[r]; // total subtotals 56 56 } // for 57 57 sout | total | endl; -
src/tests/prodcons.c
r8a48f4b r5434d04 10 10 // Created On : Mon Sep 18 12:23:39 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Sep 20 17:03:28201713 // Update Count : 4 012 // Last Modified On : Mon Oct 30 18:01:19 2017 13 // Update Count : 41 14 14 // 15 15 … … 30 30 // 1st resume starts here 31 31 for ( int i = 0; i < prod.N; i += 1 ) { 32 int p1 = (unsigned int)rand48() % 100; // non-negative33 int p2 = (unsigned int)rand48() % 100;32 int p1 = rand48( 100 ); 33 int p2 = rand48( 100 ); 34 34 sout | p1 | " " | p2 | endl; 35 35 int status = delivery( *prod.c, p1, p2 );
Note: See TracChangeset
for help on using the changeset viewer.