source: src/tests/concurrent/examples/quickSort.c @ ebcb7ba

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since ebcb7ba was ff39851, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

  • Property mode set to 100644
File size: 6.3 KB
Line 
1//
2// The contents of this file are covered under the licence agreement in the
3// file "LICENCE" distributed with Cforall.
4//
5// quickSort.c -- In-place concurrent quick-sort: threads are created to partition to a specific depth, then sequential
6//              recursive-calls are use to sort each partition.
7//
8// Author           : Peter A. Buhr
9// Created On       : Wed Dec  6 12:15:52 2017
10// Last Modified By : Peter A. Buhr
11// Last Modified On : Mon Jan 29 08:41:37 2018
12// Update Count     : 155
13//
14
15#include <fstream>
16#include <stdlib>
17#include <kernel>
18#include <thread>
19#include <string.h>                                                                             // strcmp
20
21thread Quicksort {
22        int * values;                                                                           // communication variables
23        int low, high, depth;
24};
25
26void ?{}( Quicksort & qs, int values[], int size, int depth ) {
27        qs.values = values;  qs.low = 0;  qs.high = size;  qs.depth = depth;
28} // Quicksort
29
30void main( Quicksort & qs ) {                                                   // thread starts here
31        // nested routines: information hiding
32
33        void ?{}( Quicksort & qs, int values[], int low, int high, int depth ) {
34                qs.values = values;  qs.low = low;  qs.high = high;  qs.depth = depth;
35        } // Quicksort
36
37        void sort( int values[], int low, int high, int depth ) {
38                int left, right;                                                                // index to left/right-hand side of the values
39                int pivot;                                                                              // pivot value of values
40                int swap;                                                                               // temporary
41
42                //verify();                                                                             // check for stack overflow due to recursion
43
44                // partition while 2 or more elements in the array
45                if ( low < high ) {
46                        pivot = values[low + ( high - low ) / 2];
47                        left  = low;
48                        right = high;
49
50                        // partition: move values less < pivot before the pivot and values > pivot after the pivot
51                        do {
52                                while ( values[left] < pivot ) left += 1; // changed values[left] < pivot
53                                while ( pivot < values[right] ) right -= 1;
54                                if ( left <= right ) {
55                                        swap = values[left];                            // interchange values
56                                        values[left]  = values[right];
57                                        values[right] = swap;
58                                        left += 1;
59                                        right -= 1;
60                                } // if
61                        } while ( left <= right );
62
63                        // restrict number of tasks to slightly greater than number of processors
64                        if ( depth > 0 ) {
65                                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
69                        } else {
70                                sort( values, low, right, 0 );                  // sequentially sort lower half
71                                sort( values, left, high, 0 );                  // sequentially sort upper half
72                        } // if
73                } // if
74        } // sort
75
76        with( qs ) {
77                sort( values, low, high, depth );
78        } // with
79} // main
80
81
82bool convert( int & val, const char * nptr ) {                  // convert C string to integer
83        char * eptr;
84        int temp = strto( nptr, &eptr, 10 );                            // do not change val on false
85        // true => entire string valid with no extra characters
86        return *nptr != '\0' && *eptr == '\0' ? val = temp, true : false;
87} // convert
88
89void usage( char * argv[] ) {
90        sout | "Usage:" | argv[0] | "( -s unsorted-file [ sorted-file ] | -t size (>= 0) [ depth (>= 0) ] )" | endl;
91        exit( EXIT_FAILURE );                                                           // TERMINATE!
92} // usage
93
94
95int main( int argc, char * argv[] ) {
96        ifstream & unsortedfile = sin;
97        ofstream & sortedfile = sout;                                           // default value
98        int depth = 0, size;
99
100        if ( argc != 1 ) {                                                                      // do not use defaults
101                if ( argc < 2 || argc > 4 ) usage( argv );              // wrong number of options
102//              if ( strcmp( argv[1], "-t" ) == 0 ) {                   // timing ?
103                if ( argv[1][0] == '-' && argv[1][1] == 't' ) { // timing ?
104                        &unsortedfile = (ifstream *)0;                          // no input
105                        choose ( argc ) {
106                          case 4:
107                                if ( ! convert( depth, argv[3] ) || depth < 0 ) usage( argv );
108                                fallthrough;
109                          case 3:
110                                if ( ! convert( size, argv[2] ) || size < 0 ) usage( argv );
111                        } // choose
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
129        } // if
130
131        enum { ValuesPerLine = 22 };                                            // number of values printed per line
132
133        if ( &unsortedfile ) {                                                          // generate output ?
134                for ( ;; ) {
135                        unsortedfile | size;                                            // read number of elements in the list
136                  if ( eof( unsortedfile ) ) break;
137                        int * values = alloc( size );                           // values to be sorted, too large to put on stack
138                        for ( int counter = 0; counter < size; counter += 1 ) { // read unsorted numbers
139                                unsortedfile | values[counter];
140                                if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | endl | "  ";
141                                sortedfile | values[counter];
142                                if ( counter < size - 1 && (counter + 1) % ValuesPerLine != 0 ) sortedfile | ' ';
143                        } // for
144                        sortedfile | endl;
145                        if ( size > 0 ) {                                                       // values to sort ?
146                                Quicksort QS = { values, size - 1, 0 }; // sort values
147                        } // wait until sort tasks terminate
148                        for ( int counter = 0; counter < size; counter += 1 ) { // print sorted list
149                                if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | endl | "  ";
150                                sortedfile | values[counter];
151                                if ( counter < size - 1 && (counter + 1) % ValuesPerLine != 0 ) sortedfile | ' ';
152                        } // for
153                        sortedfile | endl | endl;
154
155                        delete( values );
156                } // for
157                if ( &unsortedfile != &sin ) delete( &unsortedfile ); // close input/output files
158                if ( &sortedfile != &sout ) delete( &sortedfile );
159        } else {
160                processor processors[ (1 << depth) - 1 ] __attribute__(( unused )); // create 2^depth-1 kernel threads
161
162                int * values = alloc( size );                           // values to be sorted, too large to put on stack
163                for ( int counter = 0; counter < size; counter += 1 ) { // generate unsorted numbers
164                        values[counter] = size - counter;                       // descending values
165                } // for
166                {
167                        Quicksort QS = { values, size - 1, depth }; // sort values
168                } // wait until sort tasks terminate
169
170                // for ( int counter = 0; counter < size - 1; counter += 1 ) { // check sorting
171                //      if ( values[counter] > values[counter + 1] ) abort();
172                // } // for
173
174                delete( values );
175        } // if
176} // main
177
178// Local Variables: //
179// tab-width: 4 //
180// compile-command: "cfa quickSort.c" //
181// End: //
Note: See TracBrowser for help on using the repository browser.