home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- * *
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc. *
- * All Rights Reserved. *
- * *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.; *
- * the contents of this file may not be disclosed to third parties, copied or *
- * duplicated in any form, in whole or in part, without the prior written *
- * permission of Silicon Graphics, Inc. *
- * *
- * RESTRICTED RIGHTS LEGEND: *
- * Use, duplication or disclosure by the Government is subject to restrictions *
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data *
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or *
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished - *
- * rights reserved under the Copyright Laws of the United States. *
- * *
- *******************************************************************************
- * *
- * PSORT written by *
- * Jean-Pierre Panziera *
- * jpp@corp.sgi.com *
- * *
- **************************************************************************** */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
-
- #include "psort.h"
-
- #define DEFAULT_TRIALS 11
- #define MAX_SIZE 1111
-
- void (*ref_sort)();
- void (*this_sort)();
- extern void merge_sort();
-
- float second();
-
-
- int this_compar( a, b)
- int *a, *b;
- {
- if( (*a) < (*b))
- return (-1);
- if( (*a) > (*b))
- return (1);
- return (0);
- }
-
- int total_trials;
- int is_random;
-
- main( argc, argv)
- int argc;
- char *argv[];
- {
- int n_total;
- int i,j,k, iseed, itmp;
- int *int_array, *int_copy;
-
- float t;
-
- GetArguments();
- srandom((91*getpid())|1);
- for ( k = 0 ; k < total_trials ; k++) {
- /*
- ************************************************
- setting up the arrays
- ************************************************
- */
- n_total = get_value();
-
- t = second();
-
- int_array = (int *) malloc( (n_total+1) * sizeof(int ));
- int_copy = (int *) malloc( (n_total+1) * sizeof(int ));
-
- for( i = 0 ; i < n_total ; i++) {
- int_array [i] = (random() % (2* n_total)) - n_total;
- }
- int_array[n_total] = -1;
-
- /*
- bcopy( int_array, int_copy, (n_total + 1) * sizeof(int));
- */
- #pragma parallel
- #pragma shared(n_total, int_array, int_copy)
- #pragma local(i)
- {
- #pragma pfor iterate(i=0;(n_total+1);1)
- for (i = 0 ; i < (n_total+1) ; i++) {
- int_copy[i] = int_array[i];
- }
- }
- t = second() - t;
-
- printf("Trial #%3d size %10d initialized in %6.2f seconds \n", k, n_total, t);
- /*
- ************************************************
- p_sorting the array #2
- ************************************************
- */
- t = second();
-
- psort( (char *)int_copy, n_total, sizeof(int), this_compar);
-
- t = second() - t;
-
- printf("array psorted in %6.2f seconds \n",t);
- /*
- ************************************************
- q_sorting the array #1
- ************************************************
- */
- t = second();
-
- qsort( (char *)int_array, n_total, sizeof(int), this_compar);
-
- t = second() - t;
-
- printf("array qsorted in %6.2f seconds \n",t);
- /*
- ************************************************
- checking the sorted values
- ************************************************
- */
- if( int_array[n_total] != -1) {
- printf("Outside value modified \n");
- exit (-1);
- }
- if( this_check( n_total, int_array, int_copy)) {
- exit(-1);
- }
- printf("-----------------------------------\n");
- /*
- ************************************************
- */
- free(int_array);
- free(int_copy);
- }
- }
-
- int this_check ( nb, array1, array2)
- int nb, *array1, *array2;
- {
- int i, k=0;
- for ( i = 0 ; i < nb ; i = i +1 ) {
-
- if ( array1[i] < array2[i] ) {
-
- if( k < 10) printf( "error for # %8d , %10d != %10d \n", i, array1[i], array2[i]);
- k = k + 1;
- }
- }
- printf("%d errors detected \n", k);
- return k;
- }
-
-
- #include <sys/time.h>
- /* #include <sys/resource.h> */
-
- /* **********************************************************************
-
- give the elapsed wall clock time
-
- ********************************************************************** */
- float second()
- {
- struct timeval s_val;
- struct timezone s_z;
-
- static long zero_sec = 0;
- static long zero_usec = 0;
- long n_sec, n_usec;
-
- gettimeofday(&s_val, &s_z);
-
- n_sec = s_val.tv_sec;
- n_usec = s_val.tv_usec;
- if( zero_sec == 0 ) {
- zero_sec = n_sec;
- zero_usec = n_usec;
- }
-
- n_sec = n_sec - zero_sec;
- n_usec = n_usec - zero_usec;
-
- return( (float) n_sec + ( (float) n_usec * 1.0E-6 ) );
- }
-
- GetArguments( argc, argv)
- int argc;
- char *argv[];
- {
- int i, j, k;
- int nerror = 0;
-
- total_trials = 0;
- is_random = 1;
- srand( 0x01 | (123 * getpid()));
-
- this_sort = psort;
- ref_sort = qsort;
- /* ******************************************************* */
- for ( i = 1 ; (i < argc) && (nerror != 1) ; i ++ ) {
- if( argv[i][0] == '-') {
- switch ( argv[i][1]) {
- case 'i' :
- case 'I' :
- is_random = 0;
- break;
- default : nerror = 1;
- }
- }
- else {
- if( i+1 > argc)
- nerror = 1;
- else {
- total_trials = atoi( argv[i]);
- }
- }
- }
- if( nerror == 1) {
- fprintf( stderr,
- "Usage : %s [-p[arallel]] [-s[erial]] [minsize maxsize step]\n",
- argv[0]);
- exit(-1);
- }
- this_sort = psort;
- ref_sort = qsort;
-
- if( total_trials == 0) {
- total_trials = DEFAULT_TRIALS;
- }
- }
-
- int get_value(){
- int i;
- if(is_random) {
- i = random() % MAX_SIZE ;
- }
- else {
- srandom(1);
- printf("Enter size : ");
- scanf("%d", &i);
- }
- return(i);
- }
-