Ignore:
Timestamp:
Oct 29, 2019, 4:01:24 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
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.
Message:

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

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
    13//
    24// The contents of this file are covered under the licence agreement in the
     
    911// Created On       : Wed Dec  6 12:15:52 2017
    1012// Last Modified By : Peter A. Buhr
    11 // Last Modified On : Tue Jan 30 15:58:58 2018
    12 // Update Count     : 162
     13// Last Modified On : Thu Oct 10 13:58:18 2019
     14// Update Count     : 176
    1315//
    1416
    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>
    1921#include <string.h>                                                                             // strcmp
    2022
     
    6466                        if ( depth > 0 ) {
    6567                                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
    6972                        } else {
    7073                                sort( values, low, right, 0 );                  // sequentially sort lower half
     
    8891
    8992void 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) ] )";
    9194        exit( EXIT_FAILURE );                                                           // TERMINATE!
    9295} // usage
     
    114117                                &sortedfile = new( (const char *)argv[2] ); // open the output file
    115118                                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] | "\"";
    117120                                        usage( argv );
    118121                                } // if
     
    121124                                &unsortedfile = new( (const char *)argv[1] ); // open the input file
    122125                                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] | "\"";
    124127                                        usage( argv );
    125128                                } // if
     
    127130                } // if
    128131        } // if
     132        sortedfile | nlOff;                                                                     // turn off auto newline
    129133
    130134        enum { ValuesPerLine = 22 };                                            // number of values printed per line
    131135
    132136        if ( &unsortedfile ) {                                                          // generate output ?
    133                 for ( ;; ) {
     137                for () {
    134138                        unsortedfile | size;                                            // read number of elements in the list
    135139                  if ( eof( unsortedfile ) ) break;
    136140                        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 numbers
     141                        for ( counter; size ) {                                         // read unsorted numbers
    138142                                unsortedfile | values[counter];
    139                                 if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | endl | "  ";
     143                                if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | nl | "  ";
    140144                                sortedfile | values[counter];
    141145                                if ( counter < size - 1 && (counter + 1) % ValuesPerLine != 0 ) sortedfile | ' ';
    142146                        } // for
    143                         sortedfile | endl;
     147                        sortedfile | nl;
    144148                        if ( size > 0 ) {                                                       // values to sort ?
    145149                                Quicksort QS = { values, size - 1, 0 }; // sort values
    146150                        } // wait until sort tasks terminate
    147                         for ( int counter = 0; counter < size; counter += 1 ) { // print sorted list
    148                                 if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | endl | "  ";
     151                        for ( counter; size ) {                                         // print sorted list
     152                                if ( counter != 0 && counter % ValuesPerLine == 0 ) sortedfile | nl | "  ";
    149153                                sortedfile | values[counter];
    150154                                if ( counter < size - 1 && (counter + 1) % ValuesPerLine != 0 ) sortedfile | ' ';
    151155                        } // for
    152                         sortedfile | endl | endl;
     156                        sortedfile | nl | nl;
    153157
    154158                        delete( values );
     
    159163                processor processors[ (1 << depth) - 1 ] __attribute__(( unused )); // create 2^depth-1 kernel threads
    160164
    161                 int * values = alloc( size );                           // values to be sorted, too large to put on stack
    162                 for ( int counter = 0; counter < size; counter += 1 ) { // generate unsorted numbers
     165                int * values = alloc( size );                                   // values to be sorted, too large to put on stack
     166                for ( counter; size ) {                                                 // generate unsorted numbers
    163167                        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] );
    164171                } // for
    165172                {
     
    167174                } // wait until sort tasks terminate
    168175
    169                 // for ( int counter = 0; counter < size - 1; counter += 1 ) { // check sorting
     176                // for ( counter; size - 1 ) {                          // check sorting
    170177                //      if ( values[counter] > values[counter + 1] ) abort();
    171178                // } // for
     
    175182} // main
    176183
     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
    177186// Local Variables: //
    178187// tab-width: 4 //
    179 // compile-command: "cfa quickSort.c" //
     188// compile-command: "cfa quickSort.cfa" //
    180189// End: //
Note: See TracChangeset for help on using the changeset viewer.