| 1 | /* ***********************************************************************
|
|---|
| 2 | This program is part of the
|
|---|
| 3 | OpenMP Source Code Repository
|
|---|
| 4 |
|
|---|
| 5 | http://www.pcg.ull.es/ompscr/
|
|---|
| 6 | e-mail: ompscr@etsii.ull.es
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | (LICENSE file) along with this program; if not, write to
|
|---|
| 20 | the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
|---|
| 21 | Boston, MA 02111-1307 USA
|
|---|
| 22 |
|
|---|
| 23 | FILE: c_qsort.c
|
|---|
| 24 | VERSION: 1.0
|
|---|
| 25 | DATE: May 2004
|
|---|
| 26 | AUTHOR: F. de Sande
|
|---|
| 27 | COMMENTS TO: sande@csi.ull.es
|
|---|
| 28 | DESCRIPTION: Parallel implementation of Quicksort using OpenMP
|
|---|
| 29 | Sorts an integer array
|
|---|
| 30 | COMMENTS: The code requires nested Parallelism.
|
|---|
| 31 | REFERENCES: C. A. R. Hoare,
|
|---|
| 32 | ACM Algorithm 64}: Quicksort",
|
|---|
| 33 | Communications of the ACM",
|
|---|
| 34 | vol. 4, no. 7, pg. 321. Jul 1961
|
|---|
| 35 | http://en.wikipedia.org/wiki/Quicksort
|
|---|
| 36 | BASIC PRAGMAS: parallel for
|
|---|
| 37 | USAGE: ./c_qsort.par 2000000
|
|---|
| 38 | INPUT: The size (in K) of the vector to sort
|
|---|
| 39 | OUTPUT: The code tests that the vector is sorted
|
|---|
| 40 | FILE FORMATS: -
|
|---|
| 41 | RESTRICTIONS: -
|
|---|
| 42 | REVISION HISTORY:
|
|---|
| 43 | **************************************************************************/
|
|---|
| 44 | //#include "OmpSCR.h"
|
|---|
| 45 | #include <omp.h>
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | #define NUM_ARGS 1
|
|---|
| 49 | #define NUM_TIMERS 1
|
|---|
| 50 | #define KILO (1024)
|
|---|
| 51 | #define MEGA (1024 * 1024)
|
|---|
| 52 | #define DEFAULT_SIZE (2 * MEGA)
|
|---|
| 53 | #define MAXSIZE (9 * MEGA)
|
|---|
| 54 | #define NUM_STEPS 10 /* No. of iterations (number of vectors to sort) */
|
|---|
| 55 |
|
|---|
| 56 | #define SIZEINIT 128
|
|---|
| 57 |
|
|---|
| 58 | char USAGE_STR[] = "<size_in_Kb>";
|
|---|
| 59 | int SIZE;
|
|---|
| 60 | int array[MAXSIZE];
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 | /* -----------------------------------------------------------------------
|
|---|
| 64 | PROTOTYPES
|
|---|
| 65 | * ----------------------------------------------------------------------- */
|
|---|
| 66 |
|
|---|
| 67 | void initialize(int *v, int seed);
|
|---|
| 68 | void testit(int *v);
|
|---|
| 69 | void qs(int *v, int first, int last);
|
|---|
| 70 |
|
|---|
| 71 | /* -----------------------------------------------------------------------
|
|---|
| 72 | IMPLEMENTATION
|
|---|
| 73 | * ----------------------------------------------------------------------- */
|
|---|
| 74 | /* -----------------------------------------------------------------------
|
|---|
| 75 | Sets randomly the values for the array
|
|---|
| 76 | * ----------------------------------------------------------------------- */
|
|---|
| 77 | void initialize(int *v, int seed) {
|
|---|
| 78 | unsigned i;
|
|---|
| 79 |
|
|---|
| 80 | srandom(seed);
|
|---|
| 81 | for(i = 0; i < SIZE; i++)
|
|---|
| 82 | v[i] = (int)random();
|
|---|
| 83 | }
|
|---|
| 84 | /* -----------------------------------------------------------------------
|
|---|
| 85 | Tests the result
|
|---|
| 86 | * ----------------------------------------------------------------------- */
|
|---|
| 87 | void testit(int *v) {
|
|---|
| 88 | register int k;
|
|---|
| 89 | int not_sorted = 0;
|
|---|
| 90 |
|
|---|
| 91 | for (k = 0; k < SIZE - 1; k++)
|
|---|
| 92 | if (v[k] > v[k + 1]) {
|
|---|
| 93 | not_sorted = 1;
|
|---|
| 94 | break;
|
|---|
| 95 | }
|
|---|
| 96 | if (not_sorted)
|
|---|
| 97 | printf("Array NOT sorted.\n");
|
|---|
| 98 | else
|
|---|
| 99 | printf("Array sorted.\n");
|
|---|
| 100 | }
|
|---|
| 101 | /* ----------------------------------------------------------------------- */
|
|---|
| 102 | void qs(int *v, int first, int last) {
|
|---|
| 103 | int start[2], end[2], pivot, i, temp;
|
|---|
| 104 |
|
|---|
| 105 | if (first < last) {
|
|---|
| 106 | start[1] = first;
|
|---|
| 107 | end[0] = last;
|
|---|
| 108 | pivot = v[(first + last) / 2];
|
|---|
| 109 | while (start[1] <= end[0]) {
|
|---|
| 110 | while (v[start[1]] < pivot)
|
|---|
| 111 | start[1]++;
|
|---|
| 112 | while (pivot < v[end[0]])
|
|---|
| 113 | end[0]--;
|
|---|
| 114 | if (start[1] <= end[0]) {
|
|---|
| 115 | temp = v[start[1]];
|
|---|
| 116 | v[start[1]] = v[end[0]];
|
|---|
| 117 | v[end[0]] = temp;
|
|---|
| 118 | start[1]++;
|
|---|
| 119 | end[0]--;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 | start[0] = first;
|
|---|
| 123 | end[1] = last;
|
|---|
| 124 |
|
|---|
| 125 | #pragma omp parallel
|
|---|
| 126 | {
|
|---|
| 127 | #pragma omp for nowait
|
|---|
| 128 | for(i = 0; i <= 1; i++) {
|
|---|
| 129 | qs(v, start[i], end[i]);
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 | /* ----------------------------------------------------------------------- */
|
|---|
| 136 | int main(int argc, char *argv[]) {
|
|---|
| 137 | int STEP, NUMTHREADS;
|
|---|
| 138 | double total_time;
|
|---|
| 139 | char *PARAM_NAMES[NUM_ARGS] = {"Size (in K)"};
|
|---|
| 140 | char *TIMERS_NAMES[NUM_TIMERS] = {"Total_time" };
|
|---|
| 141 | char *DEFAULT_VALUES[NUM_ARGS] = {"2048 K"};
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 | NUMTHREADS = 1; //omp_get_num_threads();
|
|---|
| 145 | //OSCR_init (NUMTHREADS, "Quicksort", "Use 'qsort' <size (in K)>", NUM_ARGS,
|
|---|
| 146 | // PARAM_NAMES, DEFAULT_VALUES , NUM_TIMERS, NUM_TIMERS, TIMERS_NAMES,
|
|---|
| 147 | // argc, argv);
|
|---|
| 148 |
|
|---|
| 149 | SIZE = SIZEINIT; //OSCR_getarg_int(1);
|
|---|
| 150 | if (SIZE > MAXSIZE) {
|
|---|
| 151 | printf("Size: %d Maximum size: %d\n", SIZE, MAXSIZE);
|
|---|
| 152 | exit(-1);
|
|---|
| 153 | }
|
|---|
| 154 | /* Default: DEFAULT_SIZE */
|
|---|
| 155 | for (STEP = 0; STEP < NUM_STEPS; STEP++) {
|
|---|
| 156 | initialize(array, STEP);
|
|---|
| 157 | //OSCR_timer_start(0);
|
|---|
| 158 | qs(array, 0, SIZE-1);
|
|---|
| 159 | OSCR_timer_stop(0);
|
|---|
| 160 | testit(array);
|
|---|
| 161 | }
|
|---|
| 162 | total_time = 1; //OSCR_timer_read(0);
|
|---|
| 163 | //OSCR_report(1, TIMERS_NAMES);
|
|---|
| 164 | printf("\n \t# THREADS \tSIZE \tSTEPS \tTIME (secs.) \n");
|
|---|
| 165 | printf("\t%d \t\t%d \t%d \t%14.6lf \n", NUMTHREADS, SIZE, NUM_STEPS, total_time);
|
|---|
| 166 |
|
|---|
| 167 | } /* main */
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | /*
|
|---|
| 171 | * vim:ts=2:sw=2:
|
|---|
| 172 | */
|
|---|