/* Copyright (c) 2014, 2015 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. Authors: Martin Burtscher and James Edwards */ #include #include #include #include static int size, threads, cnt, val; static int *a, *b; static pthread_mutex_t mutex; static void* rankSort(void* arg) { int j, my_cnt = 0; long my_rank = (long)arg; int start = my_rank * size / threads; int end = (my_rank + 1) * size / threads; /* partial sum */ for (j = start; j < end; j++) { if (val > a[j] ) { my_cnt++; } } /* reduction */ pthread_mutex_lock(&mutex); cnt += my_cnt; pthread_mutex_unlock(&mutex); return NULL; } int main(int argc, char *argv[]) { int i; long counter; struct timeval start, end; printf("RankSort v1.1 [pthreads reduction]\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);} /* create data structure for holding thread information */ pthread_t* threadHandles = (pthread_t*)malloc(threads * sizeof(pthread_t)); if (threadHandles == NULL) {fprintf(stderr, "could not allocate array\n"); exit(-1);} pthread_mutex_init(&mutex, NULL); /* 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]; /* launch the worker threads */ for (counter = 1; counter < threads; counter++) { pthread_create(&threadHandles[counter], NULL, rankSort, (void*)counter); } /* have the master thread do some of the work */ rankSort(0); /* wait for the threads to finish */ for (counter = 1; counter < threads; counter++) { pthread_join(threadHandles[counter], NULL); } b[cnt] = val; } /* end time */ gettimeofday(&end, NULL); printf("runtime: %.4f 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); free(threadHandles); pthread_mutex_destroy(&mutex); return 0; }