home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / psort / test / sortest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.6 KB  |  251 lines

  1. /******************************************************************************
  2. *                                          *
  3. * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.              *
  4. * All Rights Reserved.                                  *
  5. *                                          *
  6. * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;      *
  7. * the contents of this file may not be disclosed to third parties, copied or  *
  8. * duplicated in any form, in whole or in part, without the prior written      *
  9. * permission of Silicon Graphics, Inc.                          *
  10. *                                          *
  11. * RESTRICTED RIGHTS LEGEND:                              *
  12. * Use, duplication or disclosure by the Government is subject to restrictions *
  13. * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data      *
  14. * and Computer Software clause at DFARS 252.227-7013, and/or in similar or    *
  15. * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -     *
  16. * rights reserved under the Copyright Laws of the United States.          *
  17. *                                          *
  18. *******************************************************************************
  19. *                                          *
  20. *    PSORT written by                              *
  21. *                Jean-Pierre Panziera                  *
  22. *                            jpp@corp.sgi.com      *
  23. *                                          *
  24. **************************************************************************** */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28.  
  29. #include "psort.h"
  30.  
  31. #define DEFAULT_TRIALS 11
  32. #define MAX_SIZE    1111
  33.  
  34. void (*ref_sort)();
  35. void (*this_sort)();
  36. extern void merge_sort();
  37.  
  38. float second();
  39.  
  40.  
  41. int this_compar( a, b)
  42. int *a, *b;
  43. {
  44.     if( (*a) < (*b)) 
  45.     return (-1);
  46.     if( (*a) > (*b))
  47.     return (1);
  48.     return (0);
  49. }
  50.  
  51. int total_trials;
  52. int is_random;
  53.  
  54. main( argc, argv)
  55.      int argc;
  56.      char *argv[];
  57. {
  58.     int n_total;
  59.     int i,j,k, iseed, itmp;
  60.     int *int_array, *int_copy;
  61.  
  62.     float t;
  63.  
  64.     GetArguments();
  65.     srandom((91*getpid())|1);
  66.     for ( k = 0 ; k < total_trials ; k++) {
  67. /*
  68. ************************************************
  69.     setting up the arrays
  70. ************************************************
  71. */
  72.     n_total = get_value();
  73.  
  74.       t = second();
  75.  
  76.     int_array = (int  *) malloc( (n_total+1) * sizeof(int  ));
  77.     int_copy  = (int  *) malloc( (n_total+1) * sizeof(int  ));
  78.  
  79.     for( i = 0 ; i < n_total ; i++) {
  80.         int_array [i] = (random() % (2* n_total)) - n_total;
  81.     }
  82.     int_array[n_total] = -1;
  83.  
  84. /*
  85.     bcopy( int_array, int_copy, (n_total + 1) * sizeof(int));
  86. */
  87. #pragma parallel 
  88. #pragma shared(n_total, int_array, int_copy)
  89. #pragma local(i)
  90. {
  91. #pragma pfor iterate(i=0;(n_total+1);1)
  92.     for (i = 0 ; i < (n_total+1) ; i++) {
  93.         int_copy[i] = int_array[i];
  94.     }
  95. }
  96.       t = second() - t;
  97.  
  98.     printf("Trial #%3d size %10d initialized in %6.2f seconds \n", k, n_total, t);
  99. /*
  100. ************************************************
  101.     p_sorting the array #2
  102. ************************************************
  103. */
  104.       t = second();
  105.  
  106.     psort( (char *)int_copy, n_total, sizeof(int), this_compar);
  107.  
  108.       t = second() - t;
  109.  
  110.     printf("array psorted in %6.2f seconds \n",t);
  111. /*
  112. ************************************************
  113.     q_sorting the array #1
  114. ************************************************
  115. */
  116.       t = second();
  117.  
  118.     qsort( (char *)int_array, n_total, sizeof(int), this_compar);
  119.  
  120.       t = second() - t;
  121.  
  122.     printf("array qsorted in %6.2f seconds \n",t);
  123. /*
  124. ************************************************
  125.     checking the sorted values
  126. ************************************************
  127. */
  128.     if( int_array[n_total] != -1) {
  129.         printf("Outside value modified \n");
  130.         exit (-1);
  131.     }
  132.     if( this_check( n_total, int_array, int_copy)) {
  133.         exit(-1);
  134.     }
  135.     printf("-----------------------------------\n");
  136. /*
  137. ************************************************
  138. */
  139.     free(int_array);
  140.     free(int_copy);
  141.     }
  142. }
  143.  
  144. int this_check ( nb, array1, array2)
  145. int nb, *array1, *array2;
  146. {
  147. int i, k=0;
  148.  for ( i = 0 ; i < nb ; i = i +1 )  {
  149.  
  150.     if ( array1[i] < array2[i] ) {
  151.  
  152.     if( k < 10) printf( "error for # %8d , %10d != %10d \n", i, array1[i], array2[i]);
  153.       k = k + 1;
  154.     }
  155.   }
  156. printf("%d errors detected \n", k);
  157. return k;
  158. }
  159.  
  160.  
  161. #include <sys/time.h>
  162. /*  #include <sys/resource.h>  */
  163.  
  164. /* **********************************************************************
  165.  
  166.    give the elapsed wall clock time
  167.  
  168. ********************************************************************** */
  169. float second()
  170. {
  171.         struct timeval s_val;
  172.     struct timezone s_z;
  173.  
  174.     static long zero_sec = 0;
  175.     static long zero_usec = 0;
  176.     long n_sec, n_usec;
  177.  
  178.         gettimeofday(&s_val, &s_z);
  179.  
  180.     n_sec = s_val.tv_sec;
  181.     n_usec = s_val.tv_usec;
  182.     if( zero_sec == 0 ) {
  183.          zero_sec = n_sec;
  184.         zero_usec = n_usec;
  185.     }
  186.  
  187.     n_sec = n_sec - zero_sec;
  188.     n_usec = n_usec - zero_usec;
  189.  
  190.     return( (float) n_sec +  ( (float) n_usec * 1.0E-6 ) );
  191. }
  192.  
  193. GetArguments( argc, argv)
  194. int argc;
  195. char *argv[];
  196. {
  197.     int i, j, k;
  198.     int nerror = 0;
  199.  
  200.     total_trials = 0;
  201.     is_random = 1;
  202.     srand( 0x01 | (123 * getpid()));
  203.  
  204.     this_sort = psort;
  205.     ref_sort  = qsort;
  206. /* ******************************************************* */
  207.     for ( i = 1 ; (i < argc) && (nerror != 1) ; i ++ ) {
  208.     if( argv[i][0] == '-') {
  209.         switch ( argv[i][1]) {
  210.         case 'i' :
  211.         case 'I' :  
  212.                 is_random = 0;
  213.                 break;
  214.         default  :  nerror = 1;
  215.         }
  216.     }
  217.     else {
  218.         if( i+1 > argc)
  219.         nerror = 1;
  220.         else { 
  221.         total_trials  = atoi( argv[i]);
  222.         }
  223.     }
  224.     }
  225.     if( nerror == 1) {
  226.     fprintf( stderr, 
  227.         "Usage : %s [-p[arallel]] [-s[erial]] [minsize maxsize step]\n", 
  228.         argv[0]);
  229.     exit(-1);
  230.     }
  231.     this_sort = psort;
  232.     ref_sort = qsort;
  233.     
  234.     if( total_trials == 0)  {
  235.     total_trials = DEFAULT_TRIALS;
  236.     }
  237. }
  238.  
  239. int get_value(){
  240.   int i;
  241.   if(is_random) { 
  242.     i = random() % MAX_SIZE ;
  243.   }
  244.   else {
  245.     srandom(1);
  246.     printf("Enter size : ");
  247.     scanf("%d", &i);
  248.   }
  249.   return(i);
  250. }
  251.