home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_07 / 8n07107a < prev    next >
Text File  |  1990-06-19  |  7KB  |  209 lines

  1. /*
  2.  *
  3.  *  findroot.c :  A parallel root finding program
  4.  *  author:       Victor R. Volkman
  5.  *
  6.  *  Based on a problem suggested by: Professor Dave Poplawski
  7.  *                                   Department of Computer Science
  8.  *                                   Michigan Technological University
  9.  *                                   Houghton, Michigan 49931
  10.  */
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "dvapi.h"
  17. #include "dvapi2.h"
  18. #include "monitor.h"
  19.  
  20. #include "monitor.fd"
  21. #ifndef MAKEFD
  22. #include "findroot.fd"
  23. #endif
  24.  
  25. double  f();
  26. static double global_x0, global_x1, global_y0, global_y1;
  27. #define STACKSIZE 1000
  28.  
  29. int master[NUMPRC];
  30. DESQ_HAN glob_win_han;
  31. static MONITOR *root_mon;
  32.  
  33.  
  34. /* The main() program serves a passive role in the root finding
  35.    process.  The main() program activates node-0 which assumes
  36.    temporary control over the other subprocesses.  Note that
  37.    the lower and upper bounds (global_x0 and global_x1 must be
  38.    set prior to activating node-0.  */
  39.   
  40. main()
  41. {
  42.    int i;
  43.    int version;
  44.    DESQ_HAN task_han;
  45.    char stacks[NUMPRC][STACKSIZE];
  46.    char node_str[40];
  47.    double dummy;
  48.  
  49.    global_x0 = 0.0;       /* Lower Limit */
  50.    global_x1 = 1.0;       /* Upper Limit */
  51.  
  52.    version = api_init();
  53.    if (version < REQ_DESQVIEW) {
  54.       printf("This program requires DESQView %d.%02d or later.\n",
  55.               REQ_DESQVIEW >> 8, REQ_DESQVIEW & 0xFF);
  56.       exit(1);
  57.       }
  58.    /* set minimum API function level */
  59.    api_level(REQ_DESQVIEW);
  60.  
  61.    if (NUMPRC % 2 != 0) {
  62.       printf("NUMPRC is %d, please use an even number of nodes\n",NUMPRC);
  63.       exit(-1);
  64.       }
  65.  
  66.    glob_win_han = win_new(GLOB_NAME,strlen(GLOB_NAME),
  67.                           GLOB_HEIGHT,GLOB_WIDTH);
  68.    win_move(glob_win_han,0,0);         /* Set screen row=0, col=0       */
  69.    win_logattr(glob_win_han,TRUE);     /* Use logical screen attributes */
  70.    win_attr(glob_win_han,1);           /* Display text in default color */
  71.    win_frame(glob_win_han,FALSE);      /* Don't display a box border    */
  72.    win_unhide(glob_win_han);           /* Mark this window as visible   */
  73.    win_top(glob_win_han);              /* Bring to top & redraw window  */
  74.  
  75.    win_printf(glob_win_han,
  76.              "Root finder activated with %d subprocesses and resolution %f...\n",
  77.               NUMPRC, EPSILON);
  78.    global_y0 = f(global_x0);          /* Establish initial values */
  79.    global_y1 = f(global_x1);
  80.    open_monitor("rootfinder",&root_mon);
  81.    sendv(root_mon,0.0,MASTER);  /* Allow MASTER signal to be claimed */
  82.  
  83.    for (i=0; i<NUMPRC; i++) {
  84.       master[i] = 0;
  85.       task_han = tsk_new(task_findroot, stacks[i], STACKSIZE, "", 0, 0, 0);
  86.       win_printf(glob_win_han,"spawned task %d\n",i);
  87.       win_top(glob_win_han);
  88.       sprintf(node_str,"%d",i);
  89.       mal_write(mal_of(task_han), node_str, sizeof(node_str));
  90.       }
  91.  
  92.    recvl(root_mon,&dummy,TERMINATE);
  93.    win_printf(glob_win_han,"Lower limit X0 is %f, 
  94.               f(X0) is %f\n",global_x0,global_y0);
  95.    win_printf(glob_win_han,"Upper limit X1 is %f, f(X1) is %f\n",
  96.               global_x1,global_y1);
  97.    win_printf(glob_win_han,"Master status %d, %d, %d, %d\n",
  98.               master[0],master[1],master[2],master[3]);
  99.    win_printf(glob_win_han,"Press any key to continue...\n");
  100.    win_top(glob_win_han);
  101.    close_monitor(&root_mon);
  102.    getch();
  103. }
  104.  
  105.  
  106.  
  107. task_findroot(void)
  108. {
  109.    char *msg_buffer;
  110.    char title[30];
  111.    DESQ_HAN my_mal_han;
  112.    DESQ_HAN win_han;
  113.    double interv, y0, y1, dummy;
  114.    int lastnode, indx;
  115.    int msg_len;
  116.    int node;
  117.    int term;
  118.  
  119.    my_mal_han = mal_me();    /* Find out and read mail from parent */
  120.    mal_read(my_mal_han,&msg_buffer,&msg_len);
  121.    sscanf(msg_buffer,"%d",&node);
  122.  
  123.    sprintf(title,"Node %03d",node);
  124.    win_han = win_new(title,strlen(title),3,38+(node<4)*40);
  125.    win_move(win_han,7+(node%4)*5,1+(node>3)*40);
  126.    win_logattr(win_han,1);            /* Use logical screen attributes */
  127.    win_attr(win_han,1);               /* Display text in default color */
  128.    win_unhide(win_han);               /* Mark this window as visible   */
  129.    win_top(win_han);                  /* Bring to top & redraw window  */
  130.  
  131.    lastnode = (node==(NUMPRC-1));
  132.    do
  133.       {
  134.       interv = (global_x1-global_x0)/(NUMPRC);
  135.       y1 = f(global_x0+interv*(node+1));
  136.  
  137.       /* First, all even nodes send & odd nodes receive, via recv semafore */
  138.       if (node % 2 == 0)
  139.          sendv(root_mon,y1,node+1);
  140.       else  /* I'm an odd node */
  141.          recvl(root_mon,&y0,node);
  142.  
  143.       /* Second, all odd nodes send & even nodes receive, via recv semafore */
  144.       /* Node 0 may not receive, and the lastnode may not send */
  145.       if (!lastnode)
  146.          {
  147.          if (node % 2 == 1)
  148.             sendv(root_mon,y1,node+1);
  149.          else if (node) /* I'm an even node > 0 */
  150.             recvl(root_mon,&y0, node);
  151.          else         /* I'm node 0, so my left value is already computed */
  152.             y0 = global_y0;
  153.          }
  154.  
  155.       win_printf(win_han,"y0 = %f, y1 = %f\n",y0,y1); win_top(win_han);
  156.       /* Determine which process will be the controller */
  157.       if ( (y1*y0) < 0.0)
  158.          {
  159.          /* MASTER status must be inherited from the ex-controller */
  160.          win_fprintf(win_han,"Node %d waiting for MASTER status\n",node);
  161.          win_top(win_han);
  162.          recvl(root_mon,&dummy,MASTER);
  163.          master[node]++;
  164.          win_printf(win_han,"Node %d has received MASTER status\n",node);
  165.          win_top(win_han);
  166.  
  167.          /* compute new boundary conditions */
  168.          global_x0 += interv * node;
  169.          global_y0 = y0;
  170.  
  171.          if (!lastnode)
  172.             global_x1 = global_x0 + interv;
  173.          global_y1 = y1;
  174.          term = (interv < EPSILON);
  175.  
  176.          /* synchronize waiting processes below */
  177.          for (indx=0; indx<NUMPRC; indx++)
  178.             if (indx!=node)
  179.                sendv(root_mon, (double)term, indx+NUMPRC);
  180.  
  181.          /* Relinquish MASTER status to the next controller or TERMINATE */
  182.          if (term)
  183.              sendv(root_mon,0.0,TERMINATE);
  184.          else
  185.              sendv(root_mon,0.0,MASTER);
  186.          }
  187.       else /* synchronize */
  188.          {
  189.          win_printf(win_han,"Node %d suspended until notified \n",node);
  190.          win_top(win_han);
  191.          recvl(root_mon,&dummy,node+NUMPRC);
  192.          term = (int) dummy;
  193.          win_printf(win_han,"Node %d restarted by the MASTER  \n",node);
  194.          win_top(win_han);
  195.          }
  196.       }
  197.       while (!term);
  198.  
  199.    win_printf(win_han,"Leaving...\n");
  200.    tsk_free(tsk_me());  /* De-allocate DESQview resources */
  201. }
  202.  
  203.  
  204. double f(x)
  205. float x;
  206. {
  207.    return(x*x*x - x*x + x - 0.5);
  208. }
  209.