// // EACOF sort example - being run on the spEEDO/PRAZOR virtual platform and real platforms. // /* //Copyright (c) 2014, EACOF Authors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #define TRC(X) typedef long long unsigned int u64_t; typedef long int u32_t; #include "eacof_speedo.h" #ifdef NATIVE #include #include #include #else #include "prstdio.h" #endif void swap (int *a, int *b); int fill(int NumbersA[], int NumbersB[], int NumbersC[], int NumbersD[], int n); int run_sorts(int NumbersA[], int NumbersB[], int NumbersC[], int NumbersD[], int n); int bubbleSort(int Numbers[], int n); int quickSort(int Numbers[], int first, int last); int heapSort(int Numbers[], int n); int insertionSort(int Numbers[], int n); void siftDown(int Numbers[], int root, int bottom); int bm_main(int argc, char* argv[]) { int n; printf("sortBasicExample\n"); if(argc <= 1) { printf("Usage:\n\tsort n\n"); exit(1); } n = atoi(argv[1]); if (n==0) n=256; // Create four arrays to hold n integers int NumbersA[n]; int NumbersB[n]; int NumbersC[n]; int NumbersD[n]; // initialise EACOF if(initEACOF() != EACOF_OK) { stopEACOF(); fprintf(stderr, "Unable to init EACOF\n"); return 1; } // sort each copy of the array using a different algorithm run_sorts(NumbersA, NumbersB, NumbersC, NumbersD, n); _kill_sim(); return 0; } /** Helper function for swapping values */ void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } #define seeder(X) 0 /* was previously a call to time() */ /** Fills the four arrays with n pseudo-random integers */ int fill(int NumbersA[], int NumbersB[], int NumbersC[], int NumbersD[], int n) { srand(seeder(NULL)); int i; for(i = 0; i < n; i++) { int num = rand() % 10000; /* Sets the ith element to a pseudo-random integer from 0-10000 */ NumbersA[i] = num; NumbersB[i] = num; NumbersC[i] = num; NumbersD[i] = num; } return 0; } struct runstat { const char *name; int e; } answers[5]; /** Control the sorting of the Numbers arrays with different algorithms */ int run_sorts(int NumbersA[], int NumbersB[], int NumbersC[], int NumbersD[], int n) { eacof_Checkpoint *checkpoint; eacof_ProcessSpecifier processSpecifier = 0; eacof_Device sampledDevice = EACOF_DEVICE_SYSTEM_BATTERY_ALL; eacof_Sample before, after; printf("\nFilling array with %d random integers", n); printf("\nSorting %d integers:", n); if(1) { // create a checkpoint if(setCheckpoint(&checkpoint, processSpecifier, sampledDevice) != EACOF_OK) { fprintf(stderr,"Error setting checkpoint.\n"); exit(1); } int qr; for (qr=0; qr<5; qr++) { if(sampleCheckpoint(checkpoint, &before) != EACOF_OK) { fprintf(stderr,"Error sampling checkpoint.\n"); exit(1); } const char *name = 0; switch (qr) { case 0: name = "DataGenerate"; // fill arrays with n pseudo-random integers fill(NumbersA, NumbersB, NumbersC, NumbersD, n); break; case 1: // quick sort name = "QuickSort"; quickSort(NumbersA, 0, n-1); break; // heap sort case 2: name = "HeapSort"; heapSort(NumbersB, n); break; case 3:// name = "InsertionSort"; insertionSort(NumbersC, n); break; case 4:// bubble sort name = "BubbleSort"; bubbleSort(NumbersD, n); break; } if(sampleCheckpoint(checkpoint, &after) != EACOF_OK) { fprintf(stderr,"Error sampling checkpoint.\n"); exit(1); } int e = after - before; answers[qr].e = e; answers[qr].name = name; printf("Ans %s - %lf Joules\n", name, e); // delete the checkpoint if(deleteCheckpoint(&checkpoint) != EACOF_OK) { fprintf(stderr,"Error deleting checkpoint.\n"); exit(1); } } } printf("SCSV:%i,", n); // Grep for this and paste to spreadsheet int qr; for (qr=0;qr<5;qr++) printf("%i,", answers[qr].e); printf("\n"); return 0; } /** Sort the numbers using Bubble Sort - worst case O(n^2) */ int bubbleSort(int Numbers[], int n) { int i, j; for(i = (n - 1); i > 0; i--) { TRC(printf("bubble %i\n", i)); for(j = 1; j <= i; j++) { if(Numbers[j-1] > Numbers[j]) { swap(&Numbers[j-1], &Numbers[j]); } } } printf("Bubble done\n"); return 0; } /** Sort the numbers using Quick Sort - worst case O(n^2) */ int quickSort(int Numbers[], int first, int last) { int pivot, i, j; if(first < last) { //printf("qs it %i .. %i\n", first, last); pivot = first; i = first; j = last; while(i < j) { while((Numbers[i] <= Numbers[pivot]) && (i < last)) { i++; } while(Numbers[j] > Numbers[pivot]) { j--; } if(i < j) { swap(&Numbers[i], &Numbers[j]); } } swap(&Numbers[pivot], &Numbers[j]); quickSort(Numbers, first, j-1); quickSort(Numbers, j+1, last); } //printf("qs it done3 %i..%i\n", first, last); return 0; } /** Sort the numbers using Heap Sort - worst case O(nlogn) */ int heapSort(int Numbers[], int n) { int i; for(i = (n / 2); i >= 0; i--) { siftDown(Numbers, i, n - 1); } for(i = n - 1; i >= 1; i--) { swap(&Numbers[0], &Numbers[i]); siftDown(Numbers, 0, i-1); } return 0; } /** siftDown is used during Heap Sort */ void siftDown(int Numbers[], int root, int bottom) { int maxChild = root * 2 + 1; if(maxChild < bottom) { int otherChild = maxChild + 1; maxChild = (Numbers[otherChild] > Numbers[maxChild]) ? otherChild : maxChild; } else if(maxChild > bottom) { return; } else if(Numbers[root] >= Numbers[maxChild]) { return; } swap(&Numbers[root], &Numbers[maxChild]); siftDown(Numbers, maxChild, bottom); } /** Sort the numbers using Insertion Sort - worst case O(n^2) */ int insertionSort(int Numbers[], int n) { int val, i, j; for(i = 0; i < n; i++) { val = Numbers[i]; for(j = i - 1; j >= 0; j--) { if(Numbers[j] <= val) break; Numbers[j + 1] = Numbers[j]; } Numbers[j + 1] = val; } return 0; } // eof