/*
Copyright (c) 2014, Texas State University. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted for academic, research, experimental, or personal use 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.
* Neither the name of Texas State University nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
For all other uses, please contact the Office for Commercialization and Industry
Relations at Texas State University .
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.
Author: Martin Burtscher
*/
#include
#include
#include
#include
int main(int argc, char *argv[])
{
register int i, j, val, size = 0, threads;
register int *a, *b;
int cnt;
struct timeval start, end;
printf("RankSort v1.1 [OpenMP]\n");
/* check command line */
if (argc != 3) {fprintf(stderr, "usage: %s number_of_elements number_of_threads\n", argv[0]); exit(-1);}
size = atoi(argv[1]);
if (size < 1) {fprintf(stderr, "number of elements must be at least 1\n"); exit(-1);}
threads = atoi(argv[2]);
if (threads < 1) {fprintf(stderr, "number of threads must be at least 1\n"); exit(-1);}
/* allocate arrays */
a = (int *)malloc(size * sizeof(int));
b = (int *)malloc(size * sizeof(int));
if ((a == NULL) || (b == NULL)) {fprintf(stderr, "could not allocate arrays\n"); exit(-1);}
/* generate input */
for (i = 0; i < size; i++) a[i] = -((i & 2) - 1) * i;
printf("sorting %d values with %d threads\n", size, threads);
/* start time */
gettimeofday(&start, NULL);
/* sort the values */
for (i = 0; i < size; i++) {
cnt = 0;
val = a[i];
#pragma omp parallel for num_threads(threads) default(none) private(j) shared(a, val, size, cnt)
for (j = 0; j < size; j++) {
if (val > a[j])
#pragma omp atomic
cnt++;
}
b[cnt] = val;
}
/* end time */
gettimeofday(&end, NULL);
printf("runtime: %.4lf s\n", end.tv_sec + end.tv_usec / 1000000.0 - start.tv_sec - start.tv_usec / 1000000.0);
/* verify result */
i = 1;
while ((i < size) && (b[i - 1] < b[i])) i++;
if (i < size) printf("NOT sorted\n\n"); else printf("sorted\n\n");
free(a);
free(b);
return 0;
}