Changeset f0322e20
- Timestamp:
- Jan 29, 2018, 9:18:16 AM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- ff39851
- Parents:
- 604e76d
- Location:
- src/tests/concurrent/examples
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tests/concurrent/examples/quickSort.c
r604e76d rf0322e20 9 9 // Created On : Wed Dec 6 12:15:52 2017 10 10 // Last Modified By : Peter A. Buhr 11 // Last Modified On : Thu Dec 14 11:20:40 201712 // Update Count : 1 4211 // Last Modified On : Mon Jan 29 08:41:37 2018 12 // Update Count : 155 13 13 // 14 14 … … 19 19 #include <string.h> // strcmp 20 20 21 forall( otype T | { int ?<?( T, T ); } )22 21 thread Quicksort { 23 T * values;// communication variables22 int * values; // communication variables 24 23 int low, high, depth; 25 24 }; 26 25 27 forall( otype T | { int ?<?( T, T ); } ) 28 void ?{}( Quicksort(T) & qs, T values[], int size, int depth ) { 26 void ?{}( Quicksort & qs, int values[], int size, int depth ) { 29 27 qs.values = values; qs.low = 0; qs.high = size; qs.depth = depth; 30 28 } // Quicksort 31 29 32 forall( otype T | { int ?<?( T, T ); } ) 33 void main( Quicksort(T) & qs ) { 30 void main( Quicksort & qs ) { // thread starts here 34 31 // nested routines: information hiding 35 32 36 void ?{}( Quicksort (T) & qs, Tvalues[], int low, int high, int depth ) {33 void ?{}( Quicksort & qs, int values[], int low, int high, int depth ) { 37 34 qs.values = values; qs.low = low; qs.high = high; qs.depth = depth; 38 35 } // Quicksort 39 36 40 void sort( Tvalues[], int low, int high, int depth ) {37 void sort( int values[], int low, int high, int depth ) { 41 38 int left, right; // index to left/right-hand side of the values 42 Tpivot; // pivot value of values43 T swap;// temporary39 int pivot; // pivot value of values 40 int swap; // temporary 44 41 45 42 //verify(); // check for stack overflow due to recursion … … 67 64 if ( depth > 0 ) { 68 65 depth -= 1; 69 //sout << &uThisTask() << " " << depth << endl; 70 Quicksort(T) rqs = { values, low, right, depth }; // concurrently sort upper half 66 Quicksort rqs = { values, low, right, depth }; // concurrently sort upper half 71 67 //Quicksort lqs( values, left, high, depth ); // concurrently sort lower half 72 68 sort( values, left, high, depth ); // concurrently sort lower half … … 97 93 98 94 99 #define ELEMTYPE int100 101 95 int main( int argc, char * argv[] ) { 102 96 ifstream & unsortedfile = sin; … … 106 100 if ( argc != 1 ) { // do not use defaults 107 101 if ( argc < 2 || argc > 4 ) usage( argv ); // wrong number of options 108 if ( strcmp( argv[1], "-s" ) == 0 ) { 109 choose ( argc ) { 110 case 4: 111 &sortedfile = new( (const char *)argv[3] ); // open the output file 112 if ( fail( sortedfile ) ) { 113 serr | "Error! Could not open sorted output file \"" | argv[3] | "\"" | endl; 114 usage( argv ); 115 } // if 116 fallthrough; 117 case 3: 118 &unsortedfile = new( (const char *)argv[2] ); // open the input file 119 if ( fail( unsortedfile ) ) { 120 serr | "Error! Could not open unsorted input file \"" | argv[2] | "\"" | endl; 121 usage( argv ); 122 } // if 123 } // choose 124 } else if ( strcmp( argv[1], "-t" ) == 0 ) { 125 unsortedfile = *(ifstream *)0; // no input 102 // if ( strcmp( argv[1], "-t" ) == 0 ) { // timing ? 103 if ( argv[1][0] == '-' && argv[1][1] == 't' ) { // timing ? 104 &unsortedfile = (ifstream *)0; // no input 126 105 choose ( argc ) { 127 106 case 4: … … 131 110 if ( ! convert( size, argv[2] ) || size < 0 ) usage( argv ); 132 111 } // choose 133 } else usage( argv ); // invalid flag 112 } else { // sort file 113 choose ( argc ) { 114 case 3: 115 &sortedfile = new( (const char *)argv[2] ); // open the output file 116 if ( fail( sortedfile ) ) { 117 serr | "Error! Could not open sorted output file \"" | argv[2] | "\"" | endl; 118 usage( argv ); 119 } // if 120 fallthrough; 121 case 2: 122 &unsortedfile = new( (const char *)argv[1] ); // open the input file 123 if ( fail( unsortedfile ) ) { 124 serr | "Error! Could not open unsorted input file \"" | argv[1] | "\"" | endl; 125 usage( argv ); 126 } // if 127 } // choose 128 } // if 134 129 } // if 135 130 … … 140 135 unsortedfile | size; // read number of elements in the list 141 136 if ( eof( unsortedfile ) ) break; 142 // ELEMTYPE * values = anew( size ); // values to be sorted, too large to put on stack 143 ELEMTYPE * values = alloc( size ); // values to be sorted, too large to put on stack 144 // ELEMTYPE * values = (ELEMTYPE *)malloc( sizeof(ELEMTYPE) * size ); 137 int * values = alloc( size ); // values to be sorted, too large to put on stack 145 138 for ( int counter = 0; counter < size; counter += 1 ) { // read unsorted numbers 146 139 unsortedfile | values[counter]; … … 151 144 sortedfile | endl; 152 145 if ( size > 0 ) { // values to sort ? 153 Quicksort (ELEMTYPE)QS = { values, size - 1, 0 }; // sort values146 Quicksort QS = { values, size - 1, 0 }; // sort values 154 147 } // wait until sort tasks terminate 155 148 for ( int counter = 0; counter < size; counter += 1 ) { // print sorted list … … 167 160 processor processors[ (1 << depth) - 1 ] __attribute__(( unused )); // create 2^depth-1 kernel threads 168 161 169 // ELEMTYPE * values = anew( size ); // values to be sorted, too large to put on stack 170 ELEMTYPE * values = alloc( size ); // values to be sorted, too large to put on stack 162 int * values = alloc( size ); // values to be sorted, too large to put on stack 171 163 for ( int counter = 0; counter < size; counter += 1 ) { // generate unsorted numbers 172 164 values[counter] = size - counter; // descending values 173 165 } // for 174 166 { 175 Quicksort (ELEMTYPE)QS = { values, size - 1, depth }; // sort values167 Quicksort QS = { values, size - 1, depth }; // sort values 176 168 } // wait until sort tasks terminate 177 169
Note: See TracChangeset
for help on using the changeset viewer.