home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / prospero / PRM / src / testprog / sortnode.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-02  |  3.7 KB  |  148 lines

  1. /*
  2.  * Copyright (c) 1992, 1993 by the University of Southern California
  3.  *
  4.  * For copying and distribution information, please see the files
  5.  * <prm-copyr.h>.
  6.  *
  7.  * Written by srao 9/92
  8.  *
  9.  */
  10.  
  11. #include <prm-copyr.h>
  12.  
  13.  
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <math.h>
  17.  
  18. #define  MAIN_PROG
  19. #include <comm.h>
  20.  
  21. #ifdef MACH386   
  22. #  include <sys/file.h>
  23. #  define  SEEK_SET L_SET
  24. #  define log2(x) log(x)/log(2.0)
  25. #else
  26. #  ifdef HPUX
  27. #    define srandom srand
  28. #    define random rand
  29. #  endif
  30. #  include <unistd.h>
  31. #endif
  32.  
  33.  
  34. #define MAXSIZE 16384
  35. #define K_NEIGH(x,dist) (x-dist)
  36.  
  37. char *progname;
  38. u_char my_portid=1;
  39. int my_tid;
  40.  
  41. struct message {
  42.   int start_index, nitems;
  43.   char dflname[80];
  44. };
  45.  
  46. main(argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50.   int tmp, ntasks, msg_length;
  51.   float *a;
  52.   u_long jnum;
  53.   char  *outflname;
  54.   int dfd, mergetimes, k, two2k, two2km1, dest, first_posn, offset;
  55.   struct message msg;
  56.   extern char *(write_output_file());
  57.  
  58.   init_task(argv);    /* Initialization is required for all tasks in every
  59.              application */
  60.   my_tid = gettid(); 
  61.   if (my_tid == -1) {
  62.     io_printf(" task could not get its tid!", (char *)0);
  63.     exit(1);
  64.   }
  65.  
  66.   
  67.   ntasks = numtasks() ;   /* Get number of tasks in this job */
  68.   msg_length = sizeof(struct message);
  69.  
  70.   two2km1 = 0;
  71.  
  72.   for (two2k=1; two2k <= ntasks ; two2k *= 2) {
  73.     if ((my_tid-1)%two2k == 0) {   /* If my task number is a multiple of
  74.                      2^k I receive data  */
  75.       
  76.       vrecv(-1, my_portid, -1, &msg, msg_length ); 
  77.       /* Recv message containing name of file to read, offset into 
  78.      array and length */
  79.       
  80.       if ((dfd = io_open(msg.dflname, O_RDONLY, 0)) < 0) {
  81.     io_printf("iter=%d, Opening file %s failed: %s", k, msg.dflname, 
  82.           p_err_string);
  83.     exit(1);
  84.       }
  85.       if (two2k == 1) { /* In the first iteration, input data is read is from  
  86.                a shared data file. I have to allocate an array of 
  87.                the appropriate size, open the remote file through
  88.                the file-iotask and seek to the appropriate location
  89.                in the and read a subarray from that position */
  90.     
  91.     a = (float *)calloc(ntasks * msg.nitems, sizeof(float));
  92.  
  93.     offset = msg.start_index * sizeof(float);
  94. #if 0
  95.     if( io_seek(dfd, msg.start_index*sizeof(float), SEEK_SET) == -1) {
  96.       io_printf("seek: descriptor = %d; %s", dfd, p_err_string);
  97.       exit(1);
  98.     }
  99. #endif
  100.       }
  101.       else offset = 0;
  102.  
  103.       /* read data from file */
  104.       if (io_read(dfd, a + msg.start_index, msg.nitems*sizeof(float), 
  105.           offset) == -1) {
  106.     io_printf("read: pdap_errno = %d ; %s", pdap_errno, p_err_string);
  107.     exit(1);
  108.       }
  109.       io_close(dfd);
  110.       
  111.       if(two2k == 1)  { /* in the first iteration, we do a quicksort */
  112.     first_posn = msg.start_index;
  113.     qsort(a, msg.start_index, msg.start_index+msg.nitems-1 );
  114.       }
  115.       else   {  /* in all other iterations, we merge two sorted subarrays. */
  116.     io_delete(msg.dflname);
  117.     msg.nitems=2* msg.nitems;
  118.     merge(a, first_posn, msg.start_index, msg.nitems);
  119.       }
  120.       
  121.       /* write the sorted subarray to an output data file */
  122.       outflname = write_output_file(msg.dflname, a, first_posn, msg.nitems, 
  123.                     two2k);
  124.       
  125.     }  /* if i had recvd data */
  126.     
  127.     else  { /* if i don't receive data in this iteration wheck whether i
  128.            need to send any data */
  129.       if ( (my_tid-1+two2km1)%two2k == 0) { /* i send data */
  130.     
  131.     dest = K_NEIGH(my_tid, two2km1); /* destination is my sibling on the
  132.                            kth level of binary tree */ 
  133.     msg.start_index = first_posn;
  134.     strcpy(msg.dflname, outflname);
  135.  
  136.     vsend(dest, 1, 10, &msg, msg_length);  /* send to sibling */
  137.       }
  138.     }
  139.     two2km1 = two2k;
  140.  
  141.   }  /* for */
  142.  
  143.   if(my_tid == 1)  /* if i am the root of the tree, send filename to iotsk*/
  144.     vsend(0, 1, 10, outflname, strlen(outflname)+1);
  145.  
  146.   exit(0);
  147. }
  148.