Changeset b067d9b for tests/concurrent/examples/quickSort.cfa
- Timestamp:
- Oct 29, 2019, 4:01:24 PM (6 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:
- 773db65, 9421f3d8
- Parents:
- 7951100 (diff), 8364209 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
tests/concurrent/examples/quickSort.cfa
r7951100 rb067d9b 1 // 2 // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo 1 3 // 2 4 // The contents of this file are covered under the licence agreement in the … … 9 11 // Created On : Wed Dec 6 12:15:52 2017 10 12 // Last Modified By : Peter A. Buhr 11 // Last Modified On : T ue Jan 30 15:58:58 201812 // Update Count : 1 6213 // Last Modified On : Thu Oct 10 13:58:18 2019 14 // Update Count : 176 13 15 // 14 16 15 #include <fstream >16 #include <stdlib >17 #include <kernel >18 #include <thread >17 #include <fstream.hfa> 18 #include <stdlib.hfa> 19 #include <kernel.hfa> 20 #include <thread.hfa> 19 21 #include <string.h> // strcmp 20 22 … … 64 66 if ( depth > 0 ) { 65 67 depth -= 1; 66 Quicksort rqs = { values, low, right, depth }; // concurrently sort upper half 67 //Quicksort lqs( values, left, high, depth ); // concurrently sort lower half 68 sort( values, left, high, depth ); // concurrently sort lower half 68 Quicksort lqs = { values, low, right, depth }; // concurrently sort lower half 69 Quicksort rqs = { values, left, high, depth }; // concurrently sort upper half 70 // Quicksort lqs = { values, low, right, depth }; // concurrently sort lower half 71 // sort( values, left, high, depth ); // concurrently sort upper half 69 72 } else { 70 73 sort( values, low, right, 0 ); // sequentially sort lower half … … 88 91 89 92 void usage( char * argv[] ) { 90 sout | "Usage:" | argv[0] | "( -s unsorted-file [ sorted-file ] | -t size (>= 0) [ depth (>= 0) ] )" | endl;93 sout | "Usage:" | argv[0] | "( -s unsorted-file [ sorted-file ] | -t size (>= 0) [ depth (>= 0) ] )"; 91 94 exit( EXIT_FAILURE ); // TERMINATE! 92 95 } // usage … … 114 117 &sortedfile = new( (const char *)argv[2] ); // open the output file 115 118 if ( fail( sortedfile ) ) { 116 serr | "Error! Could not open sorted output file \"" | argv[2] | "\"" | endl;119 serr | "Error! Could not open sorted output file \"" | argv[2] | "\""; 117 120 usage( argv ); 118 121 } // if … … 121 124 &unsortedfile = new( (const char *)argv[1] ); // open the input file 122 125 if ( fail( unsortedfile ) ) { 123 serr | "Error! Could not open unsorted input file \"" | argv[1] | "\"" | endl;126 serr | "Error! Could not open unsorted input file \"" | argv[1] | "\""; 124 127 usage( argv ); 125 128 } // if … … 127 130 } // if 128 131 } // if 132 sortedfile | nlOff; // turn off auto newline 129 133 130 134 enum { ValuesPerLine = 22 }; // number of values printed per line 131 135 132 136 if ( &unsortedfile ) { // generate output ? 133 for ( ;;) {137 for () { 134 138 unsortedfile | size; // read number of elements in the list 135 139 if ( eof( unsortedfile ) ) break; 136 140 int * values = alloc( size ); // values to be sorted, too large to put on stack 137 for ( int counter = 0; counter < size; counter += 1 ) {// read unsorted numbers141 for ( counter; size ) { // read unsorted numbers 138 142 unsortedfile | values[counter]; 139 if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | endl | " ";143 if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | nl | " "; 140 144 sortedfile | values[counter]; 141 145 if ( counter < size - 1 && (counter + 1) % ValuesPerLine != 0 ) sortedfile | ' '; 142 146 } // for 143 sortedfile | endl;147 sortedfile | nl; 144 148 if ( size > 0 ) { // values to sort ? 145 149 Quicksort QS = { values, size - 1, 0 }; // sort values 146 150 } // wait until sort tasks terminate 147 for ( int counter = 0; counter < size; counter += 1 ) {// print sorted list148 if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | endl | " ";151 for ( counter; size ) { // print sorted list 152 if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | nl | " "; 149 153 sortedfile | values[counter]; 150 154 if ( counter < size - 1 && (counter + 1) % ValuesPerLine != 0 ) sortedfile | ' '; 151 155 } // for 152 sortedfile | endl | endl;156 sortedfile | nl | nl; 153 157 154 158 delete( values ); … … 159 163 processor processors[ (1 << depth) - 1 ] __attribute__(( unused )); // create 2^depth-1 kernel threads 160 164 161 int * values = alloc( size ); // values to be sorted, too large to put on stack162 for ( int counter = 0; counter < size; counter += 1 ) {// generate unsorted numbers165 int * values = alloc( size ); // values to be sorted, too large to put on stack 166 for ( counter; size ) { // generate unsorted numbers 163 167 values[counter] = size - counter; // descending values 168 } // for 169 for ( int i = 0; i < 200; i +=1 ) { // random shuffle a few values 170 swap( values[rand() % size], values[rand() % size] ); 164 171 } // for 165 172 { … … 167 174 } // wait until sort tasks terminate 168 175 169 // for ( int counter = 0; counter < size - 1; counter += 1 ) {// check sorting176 // for ( counter; size - 1 ) { // check sorting 170 177 // if ( values[counter] > values[counter + 1] ) abort(); 171 178 // } // for … … 175 182 } // main 176 183 184 // for depth in 0 1 2 3 4 5 ; do echo "sort 500000000 values with ${depth} depth" ; time -f "%Uu %Ss %E %Mkb" a.out -t 500000000 ${depth} ; done 185 177 186 // Local Variables: // 178 187 // tab-width: 4 // 179 // compile-command: "cfa quickSort.c " //188 // compile-command: "cfa quickSort.cfa" // 180 189 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.