Changeset 40002c5 for tests/concurrency
- Timestamp:
- Jan 4, 2024, 12:12:08 PM (12 months ago)
- Branches:
- master
- Children:
- cfbc56ec, f0c9c9b
- Parents:
- ca2feff
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tests/concurrency/examples/quickSort.cfa
rca2feff r40002c5 11 11 // Created On : Wed Dec 6 12:15:52 2017 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Wed Feb 12 18:24:47 2020 14 // Update Count : 177 15 // 16 17 #include <fstream.hfa> 18 #include <stdlib.hfa> 19 #include <kernel.hfa> 13 // Last Modified On : Mon Jan 1 12:07:59 2024 14 // Update Count : 188 15 // 16 17 #include <fstream.hfa> // sin/sout 18 #include <stdlib.hfa> // convert 20 19 #include <thread.hfa> 20 #include <math.hfa> // sqrt 21 21 #include <string.h> // strcmp 22 22 … … 82 82 } // main 83 83 84 85 bool convert( int & val, const char * nptr ) { // convert C string to integer 86 char * eptr; 87 int temp = strto( nptr, &eptr, 10 ); // do not change val on false 88 // true => entire string valid with no extra characters 89 return *nptr != '\0' && *eptr == '\0' ? val = temp, true : false; 90 } // convert 91 92 void usage( char * argv[] ) { 93 sout | "Usage:" | argv[0] | "( -s unsorted-file [ sorted-file ] | -t size (>= 0) [ depth (>= 0) ] )"; 94 exit( EXIT_FAILURE ); // TERMINATE! 95 } // usage 96 84 // convert(...) throws out_of_range or invalid_argument 85 ExceptionDecl( cmd_error ); 97 86 98 87 int main( int argc, char * argv[] ) { 99 ifstream & unsortedfile = sin;100 ofstream & sortedfile = sout; // default value101 int depth = 0, size;102 103 i f ( argc != 1 ) { // do not use defaults104 if ( argc < 2 || argc > 4 ) usage( argv ); // wrong number of options 105 if ( strcmp( argv[1], "-t" ) == 0 ) { // timing ?106 &unsortedfile = (ifstream *)0; // no input88 ifstream unsortedfile = sin; // default values 89 ofstream sortedfile = sout; 90 // Must be signed because of the conversion routine. 91 intmax_t depth = 0; 92 intmax_t size = -1; // -1 means time mode not activated 93 94 try { 95 if ( 1 < argc && strcmp( argv[1], "-t" ) == 0 ) { // time mode ? 107 96 choose ( argc ) { 108 97 case 4: 109 if ( ! convert( depth, argv[3] ) || depth < 0 ) usage( argv ); 98 depth = convert( argv[3] ); // invalid integer ? 99 if ( depth < 0 ) throw ExceptionInst( cmd_error ); 110 100 fallthrough; 111 101 case 3: 112 if ( ! convert( size, argv[2] ) || size < 0 ) usage( argv ); 102 size = convert( argv[2] ); // invalid integer ? 103 if ( size < 0 ) throw ExceptionInst( cmd_error ); 104 default: // wrong number of options 105 throw ExceptionInst( cmd_error ); 113 106 } // choose 114 107 } else { // sort file 115 108 choose ( argc ) { 116 case 3: 117 &sortedfile = new( (const char *)argv[2] ); // open the output file 118 if ( fail( sortedfile ) ) { 119 serr | "Error! Could not open sorted output file \"" | argv[2] | "\""; 120 usage( argv ); 109 case 4: 110 depth = convert( argv[3] ); // invalid integer ? 111 if ( depth < 0 ) throw ExceptionInst( cmd_error ); 112 fallthrough; 113 case 3: case 2: 114 if ( strcmp( argv[1], "d" ) != 0 ) { 115 try { // open input file first as output creates file 116 open( unsortedfile, argv[1] ); 117 } catch( open_failure * ) { // open failed ? 118 serr | "Error! Could not open unsorted input file \"" | argv[1] | "\""; 119 throw ExceptionInst( cmd_error ); 120 } // try 121 } // if 122 if ( argc > 2 && strcmp( argv[2], "d" ) != 0 ) { 123 try { 124 open( sortedfile, argv[2] ); 125 } catch( open_failure * ) { // open failed ? 126 serr | "Error! Could not open sorted output file \"" | argv[2] | "\""; 127 throw ExceptionInst( cmd_error ); 128 } // try 121 129 } // if 122 130 fallthrough; 123 case 2: 124 &unsortedfile = new( (const char *)argv[1] ); // open the input file 125 if ( fail( unsortedfile ) ) { 126 serr | "Error! Could not open unsorted input file \"" | argv[1] | "\""; 127 usage( argv ); 128 } // if 131 case 1: ; // defaults 132 default: // wrong number of options 133 throw ExceptionInst( cmd_error ); 129 134 } // choose 130 135 } // if 131 } // if 136 } catch( exception_t * ) { // catch any 137 exit | "Usage: " | argv[0] | // TERMINATE 138 " ( [ unsorted-file | 'd' [ sorted-file | 'd' [ depth (>= 0) ] ] ]" 139 " | -t size (>= 0) [ depth (>= 0) ] )"; 140 } // try 141 142 enum { ValuesPerLine = 22 }; // number of values printed per line 143 132 144 sortedfile | nlOff; // turn off auto newline 133 145 134 enum { ValuesPerLine = 22 }; // number of values printed per line 135 136 if ( &unsortedfile ) { // generate output ? 146 if ( size == -1 ) { // generate output ? 137 147 for () { 138 148 unsortedfile | size; // read number of elements in the list 139 149 if ( eof( unsortedfile ) ) break; 140 int * values = alloc( size ); // values to be sorted, too large to put on stack 150 151 int * values = aalloc( size ); // values to be sorted, too large to put on stack 141 152 for ( counter; size ) { // read unsorted numbers 142 153 unsortedfile | values[counter]; … … 146 157 } // for 147 158 sortedfile | nl; 159 148 160 if ( size > 0 ) { // values to sort ? 149 161 Quicksort QS = { values, size - 1, 0 }; // sort values … … 158 170 delete( values ); 159 171 } // for 160 if ( &unsortedfile != &sin ) delete( &unsortedfile ); // close input/output files 161 if ( &sortedfile != &sout ) delete( &sortedfile ); 162 } else { 172 } else { // timing 173 PRNG prng; 163 174 processor processors[ (1 << depth) - 1 ] __attribute__(( unused )); // create 2^depth-1 kernel threads 164 165 int * values = alloc( size ); // values to be sorted, too large to put on stack 175 int * values = aalloc( size ); // values to be sorted, too large to put on stack 176 166 177 for ( counter; size ) { // generate unsorted numbers 167 178 values[counter] = size - counter; // descending values 168 179 } // for 169 for ( i; 200 ) { // random shuffle a few values 170 swap( values[rand() % size], values[rand() % size] ); 180 181 unsigned int times = sqrt( size ); 182 for ( unsigned int counter = 0; counter < times; counter += 1 ) { 183 swap( values[0], values[prng(size)] ); // randomize unsorted numbers 171 184 } // for 172 185 {
Note: See TracChangeset
for help on using the changeset viewer.