home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / adaptor.zip / adapt.zip / adaptor / dalib / pvm3 / mcube.c < prev    next >
C/C++ Source or Header  |  1993-05-02  |  6KB  |  197 lines

  1. /*******************************************************************
  2. *                                                                  *
  3. *  Author      : Dr. Thomas Brandes, GMD, I1.HR                    *
  4. *  Copyright   : GMD St. Augustin, Germany                         *
  5. *  Date        : Feb 92                                            *
  6. *  Last Update : Aug 92                                            *
  7. *                                                                  *
  8. *  This Module is part of the DALIB                                *
  9. *                                                                  *
  10. *  MAIN of ONLY NODE PROGRAM (for PVM)                             *
  11. *                                                                  *
  12. *  MODULE : mcube.c                                                *
  13. *                                                                  *
  14. *  Function: Realization of System Dependent Operations            *
  15. *                                                                  *
  16. *  - enrolls node in PVM                                           *
  17. *  - initiates other node processes                                *
  18. *  - calls nodemodule ()                                           *
  19. *                                                                  *
  20. *******************************************************************/
  21.  
  22. /*******************************************************************
  23. *                                                                  *
  24. *  Interface to Adaptor:                                           *
  25. *                                                                  *
  26. *    dalib_pid ()                                                  *
  27. *    dalib_nproc ()                                                *
  28. *                                                                  *
  29. *******************************************************************/
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "system.h"
  34. #include "pvm3.h"
  35.  
  36. int NP;          /* number of node processes */
  37. int my_id;       /* number of my process: 0 (host), 1 .. NP */
  38.  
  39. #define INIT_MSGID 9999
  40.  
  41. #if defined(IBM)
  42. void nodemodule ();
  43. #else
  44. void nodemodule_ ();
  45. #endif
  46.  
  47. int MAIN_ (argc, argv)
  48. int argc;
  49. char **argv;
  50.  
  51. { int i, j;
  52.   int ntasks;
  53.   int my_tid, parent_tid;
  54.   int me;
  55.  
  56.   unsigned long startt;   /* start timer value */
  57.   int htrace_flag;
  58.  
  59.   my_id = 1;
  60.  
  61.   target_model = 1;   /* only nodes */
  62.  
  63.   /* enroll in PVM */
  64.  
  65.   my_tid = pvm_mytid();
  66.  
  67.   if (my_tid < 0)
  68.       { /* there is an error , illegal tid */
  69.         if (my_tid == PvmSysErr)
  70.             printf ("pvmd not responding\n");
  71.           else
  72.             printf ("illegal tid\n");
  73.         exit (-1);
  74.       }
  75.  
  76.   parent_tid = pvm_parent ();
  77.  
  78.   if (parent_tid < 0)
  79.  
  80.      { /* I am the first process and have to start the other processes */
  81.  
  82.        tids [1] = my_tid;
  83.  
  84.        /* Get number of hosts of configuration */
  85.  
  86.        pvm_config (&NP, (int*) 0, (struct hostinfo**) 0);
  87.        printf ("configuration has %d hosts\n", NP);
  88.        eval_arg (NP, MAXP, &NP, &trace_flag);
  89.        trace_flag = 0; /* not realized yet */
  90.  
  91.        /* create node processes */
  92.  
  93.        if (NP > 1)
  94.            ntasks = pvm_spawn ("cube", (char**)0, PvmTaskDefault, "", 
  95.                                NP - 1, tids+2);
  96.          else
  97.            ntasks = 0;
  98.  
  99.        if (ntasks != NP - 1)
  100.           { if (ntasks < 0)
  101.                 printf ("node1: pvm_spawn has error code %d\n", ntasks);
  102.               else
  103.                 printf ("node1: pvm_spawn creates only %d tasks\n", ntasks);
  104.             exit(-1);
  105.           } 
  106.  
  107.        for (i=2; i<=NP; i++)
  108.          if (tids[i] < 0)
  109.            { pvm_perror ("spawn failure");
  110.              pvm_exit ();
  111.              exit(-1);
  112.            }
  113.  
  114.         /* make initial message */
  115.  
  116.         if (NP > 1)
  117.            { pvm_initsend (PvmDataRaw);
  118.              pvm_pkbyte (&NP, sizeof(int), 1);
  119.              pvm_pkbyte (tids+1, NP * sizeof(int), 1); 
  120.              pvm_mcast  (tids+2, NP-1, INIT_MSGID);
  121.            }
  122.  
  123.         /* wait for response of nodes */
  124.  
  125.         for (i=2; i<=NP; i++)
  126.            { pvm_recv (-1, INIT_MSGID - 1);
  127.              pvm_upkbyte (&me, sizeof(me), 1);
  128.              /* printf ("node process %d responded\n", me); */
  129.            }
  130.  
  131.      }
  132.  
  133.     else 
  134.  
  135.      { /* I AM a new process */
  136.  
  137.        /* printf ("node started, tid = %d\n", my_tid); */
  138.  
  139.        /* recv initial message */
  140.  
  141.        pvm_recv (-1, INIT_MSGID);
  142.        pvm_upkbyte (&NP, sizeof(int), 1);
  143.        pvm_upkbyte (tids+1, NP * sizeof(int), 1);
  144.  
  145.        /* printf ("received intial message\n");  */
  146.  
  147.        /* try to find my_id */
  148.  
  149.        for (i=2; i<=NP ; i++)
  150.           if (my_tid == tids[i]) { my_id = i; break; }
  151.  
  152.        /* printf ("here is node %d of %d\n", my_id, NP); */
  153.  
  154.        /* tell host that I am ready */
  155.  
  156.        pvm_initsend (PvmDataRaw);
  157.        pvm_pkbyte (&my_id, sizeof(int), 1);
  158.        pvm_send (tids[1], INIT_MSGID -1);
  159.  
  160.      }
  161.  
  162.   /* for (i=1; i<=NP; i++)
  163.      printf ("node %d: tid of node [%d] = %d\n", my_id, i, tids[i]);
  164.   */
  165.  
  166.   /* definition of the process control block */
  167.  
  168.   pcb.i       = my_id;
  169.   pcb.p       = NP;
  170.  
  171.   random_block_init (); 
  172.   dalib_init_walltime ();
  173.  
  174.   /* initialization of tracing */
  175.  
  176. #if defined(IBM)
  177.   nodemodule ();
  178. #else
  179.   nodemodule_ ();
  180. #endif
  181.   pvm_exit ();
  182. }
  183.  
  184. /*******************************************************************
  185. *                                                                  *
  186. *  number and ids of processes                                     *
  187. *                                                                  *
  188. *******************************************************************/
  189.  
  190. int dalib_pid_ ()
  191. {
  192.    return (my_id);
  193. }
  194.  
  195. int dalib_nproc_ ()
  196. { return (NP); }
  197.