bc_domgrunt.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 
00003 #include "dp_config.h"
00004 
00005 #include "eg_ddomino.h"
00006 #include "eg_nettype.h"
00007 #include "eg_mempool.h"
00008 #include "eg_list.h"
00009 #include "eg_dgraph.h"
00010 #include "eg_util.h"
00011 
00012 #include "bc_util.h"
00013 
00014 #define DOM_TASK_WAIT_SECONDS  (5)
00015 #define DOM_GRUNT_VERBOSE 5
00016 #define DOM_GRUNT_BASIC 0
00017 
00018 #ifndef CCtsp_DOMINO_PORT
00019 #define CCtsp_DOMINO_PORT ((unsigned short) 24869)
00020 #endif
00021 
00022 #ifndef CCtsp_DOMINO_WORK
00023 #define CCtsp_DOMINO_WORK        'A'
00024 #endif
00025 #ifndef CCtsp_DOMINO_GRAPH
00026 #define CCtsp_DOMINO_GRAPH       'G'
00027 #endif
00028 #ifndef CCtsp_DOMINO_NO
00029 #define CCtsp_DOMINO_NO          'N'
00030 #endif
00031 #ifndef CCtsp_DOMINO_RECEIVE
00032 #define CCtsp_DOMINO_RECEIVE     'R'
00033 #endif
00034 #ifndef CCtsp_DOMINO_SEND
00035 #define CCtsp_DOMINO_SEND        'S'
00036 #endif
00037 #ifndef CCtsp_DOMINO_WAIT
00038 #define CCtsp_DOMINO_WAIT        'W'
00039 #endif
00040 #ifndef CCtsp_DOMINO_YES
00041 #define CCtsp_DOMINO_YES         'Y'
00042 #endif
00043 #ifndef CCtsp_DOMINO_EXIT
00044 #define CCtsp_DOMINO_EXIT        'X'
00045 #endif
00046 
00047 int main (int ac,
00048           char **av)
00049 {
00050   char *bosshost = (char *) NULL;
00051   double rtime = 0.0;
00052   int id = -1;
00053   int rval = 0;
00054   CC_SFILE *s = (CC_SFILE *) NULL;
00055   double szeit;
00056   char task,
00057     need,
00058     ready;
00059 
00060   int current_g_id = -1,
00061     domino_k = -1;
00062 
00063   EGdGraph_t *G = 0;
00064   EGdGraphNode_t *source,
00065   **node_array = 0;
00066   EGlist_t *D = 0;
00067   EGlistNode_t *l_it;
00068   EGddomino_t *ddom;
00069   EGmemPool_t *mem;
00070   double ddp_heuristic_maxtime;
00071 
00072   mem = EGnewMemPool (8192, EGmemPoolNewSize, EGmemPoolNewSize (1));
00073   D = EGnewList (mem);
00074 
00075   if (ac != 2)
00076   {
00077     fprintf (stderr, "Usage: %s boss\n", av[0]);
00078     rval = 1;
00079     goto CLEANUP;
00080   }
00081 
00082   CCutil_printlabel ();
00083 
00084   bosshost = av[1];
00085 
00086   while (1)
00087   {
00088 
00089     /* open port                                                          */
00090 
00091     MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Opening Port (%d).",
00092              CCtsp_DOMINO_PORT);
00093     s = CCutil_snet_open (bosshost, CCtsp_DOMINO_PORT);
00094     MESSAGE (DOM_GRUNT_VERBOSE, "open fd %d", s->desc);
00095     if (!s)
00096     {
00097       fprintf (stderr, "CCutil_snet_open failed\n");
00098       rval = 1;
00099       goto CLEANUP;
00100     }
00101 
00102     /* establish communication with the boss.                             */
00103 
00104     MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Communicating with boss.");
00105     rval = CCutil_swrite_char (s, CCtsp_DOMINO_RECEIVE);
00106     CCcheck_rval (rval, "CCutil_swrite_char failed (RECEIVE)");
00107 
00108     /* Is the boss ready to work with us?                                 */
00109 
00110     MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Awaiting instructions.");
00111     rval = CCutil_sread_char (s, &ready);
00112     CCcheck_rval (rval, "CCutil_sread_char failed (ready)");
00113     MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Instruction: %c.", ready);
00114 
00115     /* if the answer is yes.                                              */
00116 
00117     if (ready == CCtsp_DOMINO_YES)
00118     {
00119 
00120       /* Tell the boss which graph id we have in memory                 */
00121 
00122       MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Current graph id = %d.",
00123                current_g_id);
00124 
00125       rval = CCutil_swrite_int (s, current_g_id);
00126       CCcheck_rval (rval, "CCutil_swrite_int failed (gid");
00127 
00128 
00129       /* Tell it which problem we are working on (-1 if we are have not
00130        * been working on anything, or if we are just starting.          */
00131 
00132       MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Current id = %d.", id);
00133       rval = CCutil_swrite_int (s, id);
00134       CCcheck_rval (rval, "CCutil_swrite_int failed (id)");
00135 
00136 
00137       /* If we have been working on a problem, we offer to send the 
00138        * dominoes we have found to the boss.                            */
00139 
00140       if (id != -1)
00141       {
00142 
00143         rval = CCutil_swrite_double (s, rtime);
00144         CCcheck_rval (rval, "CCutil_swrite_double failed (rtime)");
00145 
00146         /* Does the boss still need the dominoes?                     */
00147 
00148         rval = CCutil_sread_char (s, &need);
00149         CCcheck_rval (rval, "CCutil_sread_char failed (need)");
00150 
00151         /* If so, send them to him.                                   */
00152 
00153         if (need == CCtsp_DOMINO_YES)
00154         {
00155 
00156           fprintf (stderr, "\r  Domgrunt: Sending dominoes for id %5d.", id);
00157 
00158           rval = CCutil_swrite_int (s, (int) (D->size));
00159           CCcheck_rval (rval, "send ndominoes failed");
00160 
00161           for (l_it = D->begin; l_it; l_it = l_it->next)
00162           {
00163             ddom = (EGddomino_t *) (l_it->this);
00164             rval = EGsendRealDomino (s, ddom);
00165             CCcheck_rval (rval, "send_dominos failed");
00166           }
00167 
00168         }
00169 
00170         /* Free up the memory used to hold the dominoes               */
00171 
00172 
00173         MESSAGE (DOM_GRUNT_VERBOSE,
00174                  "  Domgrunt: Clearing current domino list.");
00175 
00176         EGlistClearMP (D, EGfreeDdomino, mem);
00177         id = -1;
00178 
00179         rtime = 0;
00180 
00181         MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Done.");
00182       }
00183 
00184       /* What does the boss want us to do?                              */
00185 
00186       MESSAGE (DOM_GRUNT_VERBOSE,
00187                "  Domgrunt: Waiting for instructions from boss.");
00188 
00189       rval = CCutil_sread_char (s, &task);
00190       CCcheck_rval (rval, "CCutil_sread_char failed (task)");
00191 
00192       MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Instructions: %c.", task);
00193 
00194       switch (task)
00195       {
00196 
00197         /* Wait.                                                          */
00198 
00199       case CCtsp_DOMINO_WAIT:
00200         break;
00201 
00202         /* Receive a graph.                                               */
00203 
00204       case CCtsp_DOMINO_GRAPH:
00205 
00206         MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Receiving new graph.");
00207 
00208 
00209         if (G)
00210         {
00211           EGmemPoolFree (node_array,
00212                          sizeof (EGdGraphNode_t *) * (G->nodes->size), mem);
00213           EGdGraphClearMP (G, EGfreeMengerEdgeDataMP, EGfreeMengerNodeDataMP, 0,
00214                            mem, mem, 0);
00215           G = 0;
00216         }
00217 
00218         MESSAGE (DOM_GRUNT_VERBOSE, "  Domgrunt: Receiving graph ID.");
00219 
00220         rval = CCutil_sread_int (s, &current_g_id);
00221         CCcheck_rval (rval, "receive_g_id failed");
00222 
00223         MESSAGE (DOM_GRUNT_BASIC, "  Domgrunt: graph ID = %d.", current_g_id);
00224 
00225 
00226         rval = EGreceiveRealGraph (s, &G, mem);
00227         CCcheck_rval (rval, "receive_graph failed");
00228 
00229         rval = CCutil_sread_int (s, &domino_k);
00230         CCcheck_rval (rval, "receive_domino_k failed");
00231 
00232         rval = CCutil_sread_double (s, &ddp_heuristic_maxtime);
00233         CCcheck_rval (rval, "receive_ddp_heuristic_maxtime failed");
00234         DDP_HEURISTIC_MAXTIME = ddp_heuristic_maxtime;
00235 
00236         MESSAGE (0, "  Domgrunt: nnodes= %u. nedges = %u. "
00237                  "domino_k = %d.", G->nodes->size, G->nedges, domino_k);
00238 
00239         node_array =
00240           (EGdGraphNode_t **) EGmemPoolMalloc (mem,
00241                                                sizeof (EGdGraphNode_t *) *
00242                                                G->nodes->size);
00243 
00244         for (l_it = G->nodes->begin; l_it; l_it = l_it->next)
00245         {
00246           source = (EGdGraphNode_t *) (l_it->this);
00247           node_array[source->id] = source;
00248         }
00249         break;
00250 
00251 
00252 
00253         /* Start solving for dominoes on the current graph on a source.   */
00254 
00255       case CCtsp_DOMINO_WORK:
00256         rval = CCutil_sread_int (s, &id);
00257         CCcheck_rval (rval, "CCutil_sread_int failed (id)");
00258         break;
00259 
00260         /* Quit.                                                          */
00261 
00262       case CCtsp_DOMINO_EXIT:
00263         MESSAGE (DOM_GRUNT_VERBOSE, "Shutting down the domino grunt");
00264 
00265         goto CLEANUP;
00266       }
00267 
00268       if (s)
00269         CCutil_sclose (s);
00270       s = (CC_SFILE *) NULL;
00271 
00272       /* Now we work: We call EGddominoProcessS                         */
00273 
00274       if (id != -1)
00275       {
00276 
00277         MESSAGE (DOM_GRUNT_VERBOSE, "PROCESSING node %d, graph %d", id,
00278                  current_g_id);
00279 
00280         szeit = CCutil_zeit ();
00281 
00282         source = node_array[id];
00283 
00284         rval = EGddominoComputeS (mem, source, G, D, domino_k, 1.0);
00285         CHECKRVAL (rval);
00286 
00287         rtime = CCutil_zeit () - szeit;
00288       }
00289 
00290       /* If the boss does not want to work with us ...                      */
00291 
00292     }
00293     else
00294     {
00295       EGlistClearMP (D, EGfreeDdomino, mem);
00296       id = -1;
00297       if (s)
00298         CCutil_sclose (s);
00299       s = 0;
00300       sleep (DOM_TASK_WAIT_SECONDS);
00301     }
00302   }
00303 
00304 CLEANUP:
00305 
00306   if (s != (CC_SFILE *) NULL)
00307   {
00308     CCutil_sclose (s);
00309   }
00310 
00311   EGmemPoolFree (node_array, sizeof (EGdGraphNode_t *) * G->nodes->size, mem);
00312   EGdGraphClearMP (G, EGfreeMengerEdgeDataMP, EGfreeMengerNodeDataMP, 0, mem,
00313                    mem, 0);
00314   EGfreeDGraph (G);
00315   EGfreeList (D);
00316   EGfreeMemPool (mem);
00317 
00318   return rval;
00319 }

Generated on Thu Oct 20 14:58:40 2005 for DominoParitySeparator by  doxygen 1.4.5