Пример #1
0
int main(int argc, char **argv){
	NodoBusqueda nodo_problema;
	int mintareas=4, cantidad_tareas, numt, *tids=NULL,i;
	GList *tareas, *tarptr;
	NodoBusqueda solucion;
	int problema[_SUDOK_FILAS][_SUDOK_COLUMNAS] = {		{1,0,0,0,0,7,0,9,0},
														{0,3,0,0,2,0,0,0,8},
														{0,0,9,6,0,0,5,0,0},
														{0,0,5,3,0,0,9,0,0},
														{0,1,0,0,8,0,0,0,2},
														{6,0,0,0,0,4,0,0,0},
														{3,0,0,0,0,0,0,1,0},
														{0,4,0,0,0,0,0,0,7},
														{0,0,7,0,0,0,3,0,0}

												};

	inicializarMatrizAdyacencia();
	nodo_problema= nodoInicial(problema);

	tareas=generarTareas(nodo_problema,mintareas);
	cantidad_tareas = g_list_length(tareas);
	tids=malloc(sizeof(int)*cantidad_tareas);

	numt=pvm_spawn("sudokuterm",NULL,0,"",cantidad_tareas,tids);

	if(numt<cantidad_tareas){
		fprintf(stderr, "ERROR. sudokuhub: no se pudo hacer spawn de todas las tareas");
		exit(1);
	}

	//iteramos sobre las tareas y les pasamos los datos
	tarptr=g_list_first(tareas);
	for(i=0;i<cantidad_tareas;i++){
		pvm_initsend(PvmDataDefault);
		pvm_pkNodoBusqueda((NodoBusqueda*)(tarptr->data));
		pvm_send(tids[i],_TIPOMSG_NODOBUSQUEDA);
		tarptr=tarptr->next;
	}

	//esperamos las respuestas y velamos por una solucion
	for(i=0;i<cantidad_tareas;i++){
		int exitofracaso,j;
		pvm_recv(tids[i],_TIPOMSG_EXITOFRACASO);
		pvm_upkint(&exitofracaso,1,1);
		if(exitofracaso==1){
			pvm_recv(tids[i],_TIPOMSG_NODOBUSQUEDA);
			pvm_upkNodoBusqueda(&solucion);
			printf("\nSE ENCONTRO UNA SOLUCION\n");
			printNodoBusqueda(&solucion);
			for(j=0;j<cantidad_tareas;j++)
				pvm_kill(tids[j]);
			pvm_exit();
			exit(0);
		}
	}
	printf("\nNO SE ENCONTRO NINGUNA SOLUCION\n");

	return 0;
}
Пример #2
0
// Now shut down all of the PVM stuff.  Tell all of the slaves to quit.  We
// can do this by sending the quit message or just by killing them all.  We'll
// just kill them all rather than telling them to quit nicely (sinister grin).
int
ShutdownPVM(PVMData& d) {
  for(int i=0; i<d.ntasks; i++)
    pvm_kill(d.tid[i]);
  delete [] d.tid;

  pvm_exit();			// leave the PVM

  return 0;
}
Пример #3
0
// Reap all of our spawned processes and detach ourselves from the PVM.
void
PVMDemeGA::reap() {
  for(int j=0; j<_Ntid; j++)
    if(_tid[j] > 0) pvm_kill(_tid[j]);
  delete [] _tid; 
  _tid = 0;
  _ntid = _Ntid = 0;
  if(_mid > 0) pvm_exit();
  _mid = 0;
}
Пример #4
0
adpvm_manager::~adpvm_manager(void)
{
  int i;
  // clean up slaves -- this stops all the slave processes
  for (i=1; i<=nhost; i++)	/* spawn processes on */
  {				/* all physical machines */
    for (int j=slave_assignments(i).indexmin();
             j<=slave_assignments(i).indexmax();j++)
    {
      pvm_kill(id(i,j));
    }
  }
  //delete [] slave_argv;
  //slave_argv=NULL;
}
Пример #5
0
/** Kills all filters in the void pipeline.
 * \param layout the system layout
 */
static void killAllFilters(Layout *layout) {
    int x, y;
    for(x = 0; x < layout->numFilters; x++) {
#ifdef ATTACH
        // if this is an attached filter i dont want to kill it
        // TODO: i just need to notify this filter with EOW
        if(layout->filters[x]->attached) {
            sendEowToAllInputs(layout->filters[x]);
            sendEof(&(layout->filters[x]->filterPlacement));
            continue;
        }
#endif
        for(y = 0; y < layout->filters[x]->filterPlacement.numInstances; y++) {
            if (pvm_kill(layout->filters[x]->filterPlacement.tids[y]) < 0) {
                fprintf(stderr, "%s(%d): Error killing filter %s instance: %d (pvm tid:t%x)\n",
                        __FILE__, __LINE__, layout->filters[x]->name, y,
                        layout->filters[x]->filterPlacement.tids[y]);
            }
        }
    }
}
Пример #6
0
/*---------------------------------------------------------------------------
**  MBUSCONNECT -- Connect the client to the message bus.  In the case of
**  a client we'll look first for a running Supervisor and notify them we've
**  come online.  The Client connection is complete when a MB_READY state
**  is sent.
*/
int
mbusConnect (char *whoAmI, char *group, int singleton)
{
    int     i, mytid, super, info, oldval;
    MBusPtr mbus = (MBusPtr) NULL;
    char    msg[SZ_LINE];
    pid_t   pid = getpid();


    /* Initialize host name.
    */
    mbInitMBHost ();
    mbSetMBHost ();


    /*  At this point we're only getting state information from the
     *  application cache, we don't talk to the message bus yet.
     */
    if (mbAppGet (APP_INIT)) {
	fprintf (stderr, "Application already initialized, resetting....\n");
        mbInitApp();

	/* FIXME:  ....disconnect old client.....*/
    }


    /*if (isSupervisor (whoAmI) ) {*/
        if (MB_DEBUG) printf ("Re-Initializing default message bus...\n");
        mbusInitialize (whoAmI, NULL);

	/*  Attach this process to the message bus.
	 */
    	if ((mytid = pvm_mytid()) < 0) {
	    pvm_perror (whoAmI);
	    exit (1);
    	}
    	oldval = pvm_setopt (PvmAutoErr, 0);  /* disable libpvm msgs */
    /*}*/

    /* No MBus pointer found meaning we have no connection established
     * or context yet.  If we're a SUPERVISOR, reinitialize.  Otherwise,
     * now is the time to connect and set the state.
     */
    mbus = mbAppGetMBus ();
    if (mbus == (MBusPtr) NULL) {
	/*  Message bus not initialized by supervisor.
    	 *  Attach this process to the message bus.
	 */
    	if ((mytid = pvm_mytid()) < 0) {
	    pvm_perror (whoAmI);
	    exit (1);
    	}
    	oldval = pvm_setopt (PvmAutoErr, 0);  /* disable libpvm msgs */

        /* Allocate an message bus structure.  We'll update the state from
	 * the supervisor once connected and setup.
         */
        if (MB_DEBUG) printf ("Allocating mbus struct ...\n");
        mbus = (MBusPtr) calloc (1, sizeof (MBus));
    }




    /**********************
     *  CLIENT CODE
     **********************/
    if (! isSupervisor (whoAmI)) {
        /*  If we're a Client, look for a Supervisor.  If we can't find a
         *  Supervisor the simply return an error, individuals client may 
         *  handle it differently.
         */
	for (i=MB_CONNECT_RETRYS; i; i--) {
            info = pvm_lookup (SUPERVISOR, -1, &super);
	    if (info == PvmNoEntry) {
	        fprintf (stderr, "Can't find Supervisor on msgbus, retrying\n");
		sleep (1);

	    } else {
	        if (MB_DEBUG) {
		    printf ("Supervisor on msgbus at tid=%d\n", super);
                    printf ("Registering '%s' with VM....\n", whoAmI);
	        }

                if (pvm_insert (whoAmI, -1, mytid) < 0) {
                    fprintf (stderr, "Register of '%s' failed....\n", whoAmI);
                    return (ERR);

                } else {
		    /* Send the Supervisor a CONNECT message. 
		     */
		    memset (msg, 0, SZ_LINE);
		    sprintf (msg, "{ tid=%d who=%s host=%s pid=%d }",
			mytid, whoAmI, mbGetMBHost(), pid);
		    mbusSend (SUPERVISOR, ANY, MB_CONNECT, msg);
		}
		break;
	    }
	}
	if (i == 0) {
	    if (MB_VERBOSE)
	        fprintf (stderr, "Supervisor not on msgbus, returning\n");
	    mytid *= (-1);
	    super = ERR;
	    /*return (((-1) * mytid)); */
	}


    /*********************
     * SUPERVISOR CODE
     **********************/
    } else if (isSupervisor (whoAmI)) {
	/* If we're a Supervisor, check whether we're already registered.
	 */
        info = pvm_lookup (SUPERVISOR, -1, &super);
	if (info == PvmBadParam) {
	    fprintf (stderr, "Supervisor lookup error, BadParam...\n");
	    return (ERR);

	} else if (info == PvmNoEntry) {
            /*  Supervisor does not exist */
	    if (MB_DEBUG) 
		fprintf (stderr, "Supervisor not found, registering...\n");
            if (pvm_insert (SUPERVISOR, -1, (super = mytid)) < 0) {
                fprintf (stderr, "Supervisor register failed....\n");
                return (ERR);
            }

	} else if (info >= 0) {
	    if (MB_DEBUG)
		fprintf (stderr, 
		    "Supervisor already registered at tid=%d, validating..\n",
		    super);

	    /* Try sending a message to the registered tid to see if it is
	     * alive.
	     */
	    if (mbusPing (super, 500) == OK) {
		if (singleton) {
		    /* tell other supervisor we're taking over.... */
		    printf ("got a ping reply....\n");
		} else {
		    fprintf (stderr, 
		        "ERROR: Supervisor already registered at tid=%d\n",
			super);
	            return (ERR);
		}

	    } else {
		/* The registered supervisor didn't respond, so delete it
		 * from the database and try again.
		 */
		extern int pvmreset();

	        if (MB_DEBUG)
		    fprintf (stderr, "Cleaning up earlier super at %d(%d)...\n",
			info, super);

		/* FIXME:  ....disconnect old supervisor.....*/
		(void) pvmreset (mytid, 1, "", 0);
    		if (pvm_delete (SUPERVISOR, info) != PvmOk)
		    if (MB_DEBUG)
        	        fprintf (stderr, "Supervisor cleanup failed\n");
    		if ((info = pvm_kill (super)) != PvmOk)
		    if (MB_DEBUG)
        	        fprintf (stderr, "Supervisor assasination failed\n");
		/* goto lookup_; */
	    }
        }


	/* Broadcast a message to any running clients telling them we're
	 * now running.
	 */
	memset (msg, 0, SZ_LINE); 
	sprintf (msg, "{ tid=%d who=%s host=%s pid=%d }", 
	    super, SUPERVISOR, mbGetMBHost(), pid);

	mbusBcast (CLIENT, msg, MB_CONNECT);
    }

    /* Save the Supervisor location and other bits about this client.
     */
    mbAppSet (APP_TID, mytid);
    mbAppSet (APP_STID, super);
    mbAppSet (APP_FD, mbGetMBusFD());
    mbAppSetName (whoAmI);
    mbAppSetMBus ((MBusPtr) mbus);


    /* Install the exit handler so we're sure to make a clean getaway.
     */
    atexit (mbusExitHandler);
	        
    if (MB_DEBUG)
	fprintf (stderr, "mbConnect: whoAmI='%s' group='%s'  mbus = 0x%x\n", 
	    whoAmI, group, (int) mbus);


    /* Join a specific group if specified.  A process may be part of
     * multiple groups and so we use the mbus routine the same as a
     * caller would.
     */
    if (group)
	mbusJoinGroup (group);


    /*  Return our tid.
     */
    return (mytid);
}
Пример #7
0
int main(int argc, char** argv)
{
   int pid = pvm_mytid();

   if (argc != 2 && argc != 3)
      stop("Usage: tm_driver <control_file> [TM_tid]\n");

   int tm_tid = 0;
   char * control_file = strdup(argv[1]);

   if (argc == 3)
      sscanf(argv[2], "t%x", &tm_tid);

   int info = 0;

   // Get the machine configuration
   int nhost = 0;
   int narch = 0;
   struct pvmhostinfo *hostp = 0;
   info = pvm_config(&nhost, &narch, &hostp);

   // Parse the control file
   int to_delete_size = 0;  // # of machsto delete
   char ** to_delete = 0;   // names of machs to delete
   int to_add_size = 0;     // # of machsto add
   char ** to_add = 0;      // names of machs to add

   int delete_proc_num = 0; // # of procs to delete
   int * tid_delete = 0;    // the tids of procs to delete

   // # of various procs to start
   int lp_num = 0;
   int cg_num = 0;
   int vg_num = 0;
   int cp_num = 0;
   int vp_num = 0;
   // the mach names where the procs shoud be started
   char ** lp_mach = 0;
   char ** cg_mach = 0;
   char ** vg_mach = 0;
   char ** cp_mach = 0;
   char ** vp_mach = 0;

   // Do the parsing. First count
   ifstream ctl(control_file);
   if (!ctl)
      stop("Cannot open parameter file... Aborting.\n");
   // Get the lines of the parameter file one-by-one and if a line contains a
   // (keyword, value) pair then interpret it.
   const int MAX_PARAM_LINE_LENGTH = 1024;
   char line[MAX_PARAM_LINE_LENGTH+1], *end_of_line, *keyword, *value, *ctmp;
   char ch;
   while (ctl) {
      ctl.get(line, MAX_PARAM_LINE_LENGTH);
      if (ctl) {
	 ctl.get(ch);
	 if (ch != '\n') {
	    printf("Too long (>= %i chars) line in the parameter file.\n",
		   MAX_PARAM_LINE_LENGTH);
	    stop("This is absurd. Aborting.\n");
	 }
      }
      end_of_line = line + strlen(line);
      //-------------------------- First separate the keyword and value ------
      keyword = find_if(line, end_of_line, isgraph);
      if (keyword == end_of_line) // empty line
	 continue;
      ctmp = find_if(keyword, end_of_line, isspace);
      if (ctmp == end_of_line) // line is just one word. must be a comment
	 continue;
      *ctmp = 0; // terminate the keyword with a 0 character
      ++ctmp;

      value = find_if(ctmp, end_of_line, isgraph);
      if (value == end_of_line) // line is just one word. must be a comment
	 continue;
      
      ctmp = find_if(value, end_of_line, isspace);
      *ctmp = 0; // terminate the value with a 0 character. this is good even
		 // if ctmp == end_ofline

      if (str_eq(keyword, "BCP_delete_machine")) {
	 ++to_delete_size;
      } else if (str_eq(keyword, "BCP_add_machine")) {
	 ++to_add_size;
      } else if (str_eq(keyword, "BCP_delete_proc")) {
	 ++delete_proc_num;
      } else if (str_eq(keyword, "BCP_lp_process")) {
	 ++lp_num;
      } else if (str_eq(keyword, "BCP_cg_process")) {
	 ++cg_num;
      } else if (str_eq(keyword, "BCP_vg_process")) {
	 ++vg_num;
      } else if (str_eq(keyword, "BCP_cp_process")) {
	 ++cp_num;
      } else if (str_eq(keyword, "BCP_vp_process")) {
	 ++vp_num;
      }
   }
   ctl.close();

   if (to_delete_size > 0) {
      to_delete = new char*[to_delete_size];
      to_delete_size = 0;
   }
   if (to_add_size > 0) {
      to_add = new char*[to_add_size];
      to_add_size = 0;
   }
   if (delete_proc_num > 0) {
      tid_delete = new int[delete_proc_num];
      delete_proc_num = 0;
   }
   if (lp_num) {
      lp_mach = new char*[lp_num];
      lp_num = 0;
   }
   if (cg_num) {
      cg_mach = new char*[cg_num];
      cg_num = 0;
   }
   if (vg_num) {
      vg_mach = new char*[vg_num];
      vg_num = 0;
   }
   if (cp_num) {
      cp_mach = new char*[cp_num];
      cp_num = 0;
   }
   if (vp_num) {
      vp_mach = new char*[vp_num];
      vp_num = 0;
   }

   ctl.open(control_file);
   while (ctl) {
      ctl.get(line, MAX_PARAM_LINE_LENGTH);
      if (ctl) {
	 ctl.get(ch);
	 if (ch != '\n') {
	    printf("Too long (>= %i chars) line in the parameter file.\n",
		   MAX_PARAM_LINE_LENGTH);
	    stop("This is absurd. Aborting.\n");
	 }
      }
      end_of_line = line + strlen(line);
      //-------------------------- First separate the keyword and value ------
      keyword = find_if(line, end_of_line, isgraph);
      if (keyword == end_of_line) // empty line
	 continue;
      ctmp = find_if(keyword, end_of_line, isspace);
      if (ctmp == end_of_line) // line is just one word. must be a comment
	 continue;
      *ctmp = 0; // terminate the keyword with a 0 character
      ++ctmp;

      value = find_if(ctmp, end_of_line, isgraph);
      if (value == end_of_line) // line is just one word. must be a comment
	 continue;
      
      ctmp = find_if(value, end_of_line, isspace);
      *ctmp = 0; // terminate the value with a 0 character. this is good even
		 // if ctmp == end_ofline

      if (str_eq(keyword, "BCP_delete_machine")) {
	 to_delete[to_delete_size++] = strdup(value);
      } else if (str_eq(keyword, "BCP_add_machine")) {
	 to_add[to_add_size++] = strdup(value);
      } else if (str_eq(keyword, "BCP_delete_proc")) {
	 sscanf(value, "t%x", &tid_delete[delete_proc_num++]);
      } else if (str_eq(keyword, "BCP_lp_process")) {
	 lp_mach[lp_num++] = strdup(value);
      } else if (str_eq(keyword, "BCP_cg_process")) {
	 cg_mach[cg_num++] = strdup(value);
      } else if (str_eq(keyword, "BCP_vg_process")) {
	 vg_mach[vg_num++] = strdup(value);
      } else if (str_eq(keyword, "BCP_cp_process")) {
	 cp_mach[cp_num++] = strdup(value);
      } else if (str_eq(keyword, "BCP_vp_process")) {
	 vp_mach[vp_num++] = strdup(value);
      }
   }
   ctl.close();

   // Check that machine deletions and additions are correct

   char ** last = 0;

   // Are there duplicates on the to be deleted list ?
   if (to_delete_size > 0) {
      sort(to_delete, to_delete + to_delete_size, str_lt);
      last = unique(to_delete, to_delete + to_delete_size, str_eq);
      if (to_delete_size != last - to_delete)
	 stop("A machine to be deleted is listed twice... Aborting.\n");
   }

   // Are there duplicates on the to be added list?
   if (to_add_size > 0) {
      sort(to_add, to_add + to_add_size, str_lt);
      last = unique(to_add, to_add + to_add_size, str_eq);
      if (to_add_size != last - to_add)
	 stop("A machine to be added is listed twice... Aborting.\n");
   }

   int i;
   char ** mach_list = new char*[nhost + to_add_size];
   for (i = 0; i < nhost; ++i)
      mach_list[i] = strdup(hostp[i].hi_name);
   sort(mach_list, mach_list + nhost, str_lt);

   char ** current_list = new char*[nhost + to_add_size];

   // Is there a nonexisting machine to be deleted?
   if (to_delete_size > 0) {
      last = set_difference(to_delete, to_delete + to_delete_size,
			    mach_list, mach_list + nhost,
			    current_list, str_lt);
      if (last != current_list)
	 stop("A nonexisting machine is to be deleted... Aborting.\n");
      last = set_difference(mach_list, mach_list + nhost,
			    to_delete, to_delete + to_delete_size,
			    current_list, str_lt);
      ::swap(mach_list, current_list);
   }

   // Is there an already existing machine to be added?
   if (to_add_size > 0) {
      last = set_intersection(to_add, to_add + to_add_size,
			      mach_list, mach_list + nhost,
			      current_list, str_lt);
      if (last != current_list)
	 stop("A machine to be added is already there... Aborting.\n");
      last = merge(to_add, to_add + to_add_size,
		   mach_list, mach_list + nhost,
		   current_list, str_lt);
      ::swap(mach_list, current_list);
   }

   const int mach_num = nhost - to_delete_size + to_add_size;

   // Check that the machines the new processes are supposed to be started on
   // really exist.

   if (lp_num > 0) {
      sort(lp_mach, lp_mach + lp_num, str_lt);
      if (set_difference(lp_mach, lp_mach + lp_num,
			 mach_list, mach_list + mach_num,
			 current_list, str_lt) != current_list)
	 stop("An lp machine is not in the final machine list... Aborting.\n");
   }
   if (cg_num > 0) {
      sort(cg_mach, cg_mach + cg_num, str_lt);
      if (set_difference(cg_mach, cg_mach + cg_num,
			 mach_list, mach_list + mach_num,
			 current_list, str_lt) != current_list)
	 stop("An cg machine is not in the final machine list... Aborting.\n");
   }
   if (vg_num > 0) {
      sort(vg_mach, vg_mach + vg_num, str_lt);
      if (set_difference(vg_mach, vg_mach + vg_num,
			 mach_list, mach_list + mach_num,
			 current_list, str_lt) != current_list)
	 stop("An vg machine is not in the final machine list... Aborting.\n");
   }
   if (cp_num > 0) {
      sort(cp_mach, cp_mach + cp_num, str_lt);
      if (set_difference(cp_mach, cp_mach + cp_num,
			 mach_list, mach_list + mach_num,
			 current_list, str_lt) != current_list)
	 stop("An cp machine is not in the final machine list... Aborting.\n");
   }
   if (vp_num > 0) {
      sort(vp_mach, vp_mach + vp_num, str_lt);
      if (set_difference(vp_mach, vp_mach + vp_num,
			 mach_list, mach_list + mach_num,
			 current_list, str_lt) != current_list)
	 stop("An vp machine is not in the final machine list... Aborting.\n");
   }

   // Find the tree manager
   find_tree_manager(pid, tm_tid);

   // Check that the TM is not on one of the machines to be deleted.
   if (to_delete_size > 0) {
      const int dtid = pvm_tidtohost(tm_tid);
      for (i = 0; i < nhost; ++i) {
	 if (hostp[i].hi_tid == dtid)
	    for (int j = 0; j < to_delete_size; ++j) {
	       if (str_eq(hostp[i].hi_name, to_delete[j]))
		  stop("Can't delete the machine the TM is on. Aborting.\n");
	    }
      }
   }

   // Check that the TM is not one of the processes to be deleted
   if (delete_proc_num > 0) {
      if (find(tid_delete, tid_delete + delete_proc_num, tm_tid) !=
	  tid_delete + delete_proc_num)
	 stop("Can't delete the TM... Aborting.\n");
   }

   // Modify the machine configuration
   if (to_delete_size > 0 || to_add_size > 0) {
      int * infos = new int[max(to_delete_size, to_add_size)];
      if (to_delete_size > 0)
	 if (pvm_delhosts(to_delete, to_delete_size, infos) < 0) {
	    printf("Failed to delete all specified machines...\n");
	    stop("Please check the situation manually... Aborting.\n");
	 }
      if (to_add_size > 0)
	 if (pvm_addhosts(to_add, to_add_size, infos) < 0) {
	    printf("Failed to add all specified machines...\n");
	    stop("Please check the situation manually... Aborting.\n");
	 }
   }

   // Kill the processes to be killed
   for (i = 0; i < delete_proc_num; ++i)
      pvm_kill(tid_delete[i]);

   // Put together a message to be sent to the TM that contains the machine
   // names on which the new processes should be spawned
   int len = (lp_num + cg_num + vg_num + cp_num + vp_num) * sizeof(int);
   if (len > 0) {
      len += 5 * sizeof(int);
      for (i = 0; i < lp_num; ++i) len += strlen(lp_mach[i]);
      for (i = 0; i < cg_num; ++i) len += strlen(cg_mach[i]);
      for (i = 0; i < vg_num; ++i) len += strlen(vg_mach[i]);
      for (i = 0; i < cp_num; ++i) len += strlen(cp_mach[i]);
      for (i = 0; i < vp_num; ++i) len += strlen(vp_mach[i]);

      char * buf = new char[len];

      memcpy(buf, &lp_num, sizeof(int));
      buf += sizeof(int);
      for (i = 0; i < lp_num; ++i) {
	 const int l = strlen(lp_mach[i]);
	 memcpy(buf, &l, sizeof(int));
	 buf += sizeof(int);
	 memcpy(buf, lp_mach[i], l);
	 buf += l;
      }

      memcpy(buf, &cg_num, sizeof(int));
      buf += sizeof(int);
      for (i = 0; i < cg_num; ++i) {
	 const int l = strlen(cg_mach[i]);
	 memcpy(buf, &l, sizeof(int));
	 buf += sizeof(int);
	 memcpy(buf, cg_mach[i], l);
	 buf += l;
      }

      memcpy(buf, &vg_num, sizeof(int));
      buf += sizeof(int);
      for (i = 0; i < vg_num; ++i) {
	 const int l = strlen(vg_mach[i]);
	 memcpy(buf, &l, sizeof(int));
	 buf += sizeof(int);
	 memcpy(buf, vg_mach[i], l);
	 buf += l;
      }

      memcpy(buf, &cp_num, sizeof(int));
      buf += sizeof(int);
      for (i = 0; i < cp_num; ++i) {
	 const int l = strlen(cp_mach[i]);
	 memcpy(buf, &l, sizeof(int));
	 buf += sizeof(int);
	 memcpy(buf, cp_mach[i], l);
	 buf += l;
      }

      memcpy(buf, &vp_num, sizeof(int));
      buf += sizeof(int);
      for (i = 0; i < vp_num; ++i) {
	 const int l = strlen(vp_mach[i]);
	 memcpy(buf, &l, sizeof(int));
	 buf += sizeof(int);
	 memcpy(buf, vp_mach[i], l);
	 buf += l;
      }

      buf -= len;

      pvm_initsend(PvmDataRaw);
      pvm_pkbyte(buf, len, 1);
      pvm_send(tm_tid, BCP_CONFIG_CHANGE);

      int bufid = pvm_recv(tm_tid, -1);
      int bytes = 0, msgtag = 0;
      pvm_bufinfo(bufid, &bytes, &msgtag, &tm_tid);
      if (msgtag == BCP_CONFIG_ERROR)
	 stop("TM had difficulties. Please check the situation manually.\n");
   }

   pvm_exit();
   return 0;
}
Пример #8
0
void kill_proc(int tid)
{
   pvm_kill(tid);
}
Пример #9
0
int main(int argc, char ** argv){
    TAILQ_INIT(&head);
    int nproc, numt, i, nhost, narch;
    int num_tasks = 0;
    struct pvmhostinfo *hostp;
    struct stat s;
    FILE * codefile = fopen("./codes", "w");

    if(argc != 2){
        exit_prog("USAGE: ./taskgen directory\n",1);
    }
    else {
        stat(argv[1], &s);
        if(s.st_mode & S_IFDIR){
            char path[PATH_MAX+1];
            realpath(argv[1],path);
            printf("Loading graphs from %s...", path);
            fflush(stdout);
            load_graph_dir(path, &num_tasks);
            printf("done. Found %d tasks.\n", num_tasks);
        }
        else{
            printf("%s is not a directory. Please check arguments.\n", argv[1]);
            exit_prog(NULL, 1);
        }
    }

    pvm_config(&nhost, &narch, &hostp);                         /*Set number of slaves to start */
    nproc = nhost * 3;                                          /*3 processes per host */

    printf("Spawning %d worker tasks on %d machines...", nproc, nhost);

    int tids[nproc];                                            /*hold the task ids of the workers */

    int flags = PvmTaskDefault;
   // flags += PvmTaskDebug;

    numt =pvm_spawn("worker", (char**)0, flags, "", nproc, tids);   /*start up the workers */

    if(numt < nproc){                                           /*Error Checking */
        printf("\n Trouble spawing slaves. Error codes are:\n");
        for(i = numt; i < nproc; i++){
            printf("TID %d: %d\n", i, tids[i]);
        }
        for(i = 0; i < numt; i++){
            pvm_kill(tids[i]);
        }
        exit_prog("Failure.\n",1);
    }
    printf("done. Connected.\n");
    
    /* Main loop */
    int bufid, bytes, msgtype, source;
    int sent_tasks, comp_tasks, found_codes = 0;
    int finished = 0;
    int percent_comp = 0;
    char * buf;
    while(!finished){    
        bufid = pvm_recv(-1, -1);                               /*Accept any message from any task BLOCKING CALL*/
        pvm_bufinfo(bufid, &bytes, &msgtype, &source);
        switch(msgtype){
            case MSGREQTASK:
                if(!TAILQ_EMPTY(&head)){
                    send_task(source);                              /*send out the message */
                    sent_tasks++;
                }
                break;
            case MSGCODE:
                buf = malloc(bytes);
                pvm_upkstr(buf);
                fprintf(codefile, "%s\n", buf);
                fflush(codefile);
                comp_tasks++;
                found_codes++;
                break;
            case MSGNOCODE:
                comp_tasks++;
                break;
            default:
                printf("Incorrect MSGTYPE received from task %d. Received: %d\n", source, msgtype);
                break;
            }
        
        if(((float)comp_tasks/num_tasks * 100) > percent_comp + 1){
                printf("Tasks Complete: %d    Tasks Sent: %d    Percent Complete: %.2f\n", 
                comp_tasks, sent_tasks, (float)comp_tasks/num_tasks * 100);
                percent_comp = (float)comp_tasks/num_tasks * 100;
        }
    
        if(comp_tasks == num_tasks){
            finished = 1;
        }
        
    }
 
    printf("All tasks complete.\n");

    exit_prog(NULL, 0);
    return 0;
}
Пример #10
0
int main (int argc, char* argv[])
{
	if(argc !=4)
	{
		printf("usage : ./craquage p r m\n");
		return EXIT_FAILURE;
	}
	
	//Initialisation des variables
	int nb_esclaves = atoi(argv[1]);
	int* tids = (int*) calloc(nb_esclaves, sizeof(int));
	int longueur_mdp = atoi(argv[2]);
	char* mdp = (char*) calloc(strlen(argv[3])+1, sizeof(char));
	strcpy(mdp, argv[3]);

	//declaration de type de tres long entiers (avec bibliotheque GMP)
	mpz_t debut_sequence, pas_reel, pas, fin_exec;
	mpz_init(debut_sequence);
	mpz_init(pas_reel);
	mpz_init(pas);
	mpz_init(fin_exec);

	//recuperation du chemin de l executable
	char* chemin = getcwd(NULL, 1000);
	strcat(chemin, "/craquage_esclave");

	//creation des arguments pour l esclave
	char *argv_esclave[3];
	argv_esclave[2]=NULL;
	argv_esclave[0] = (char*) calloc(strlen(argv[2])+1, sizeof(char));
	strcpy(argv_esclave[0],argv[2]);
	 
	argv_esclave[1] = (char*) calloc(strlen(argv[3])+1, sizeof(char));
	strcpy(argv_esclave[1],argv[3]);
	//printf("strlen %lu, %lu\n", (long unsigned)strlen(argv[2]),(long unsigned) strlen(argv[3]));
	//printf("nb_esclaves %d\n", nb_esclaves);
	
	int i;
	int trouve = 0;
	int fini = 0;
	int size;
	int nb_envoi = 0;
	int nb_pas = nb_esclaves*longueur_mdp;
	int nb_changement = 0;
	char* envoi_char;
		
	int bufid, info, bytes, type, source;
	char * solution;
	pvm_catchout(stderr);
	struct timeval tv1, tv2;
	gettimeofday(&tv1, NULL);
	pvm_spawn(chemin, argv_esclave, PvmTaskDefault,"", nb_esclaves, tids);

	//calcul du pas, fin_exec (= fin execution)
	mpz_set_ui(debut_sequence, 0);
	mpz_ui_pow_ui(fin_exec, 15, longueur_mdp+1);	
	mpz_sub_ui(fin_exec, fin_exec, 15);
	mpz_cdiv_q_ui(fin_exec, fin_exec, 14);
	
	mpz_set(pas, fin_exec);
	mpz_cdiv_q_ui(pas, pas, nb_pas);
	
	if(mpz_cmp_ui(pas, 0)==0)
	  {
	    mpz_set_ui(pas,1); 
	  }

	//gmp_printf("fin_exec: %Zd\npas:%Zd\ndebut_sequence:%Zd\n",fin_exec, pas, debut_sequence);
	
	//boucle principale
	while(!trouve && fini!=nb_esclaves)
	  {
	    //Attente de reception de donnees d un esclave
	    bufid = pvm_recv( -1, -1 );
	    info = pvm_bufinfo( bufid, &bytes, &type, &source );
	    
	    if (info < 0)
	      {
		printf("Erreur de reception : %d\n", info);
		exit(1);
	      }
	    
	    //selon le tag du message, demande de donnees ou solution trouvee
	    switch(type)
	      {
	      case(0)://mot de passe trouve
		solution = calloc(bytes, sizeof(char));
		pvm_upkstr(solution);
		printf("\nLa solution est : %s\n\n", solution);
		trouve = 1;
		break;	
		
	      case(1)://esclave veut plus de donnees
		//prendre en compte la fin des donnees dans le calcul du pas
		if(nb_changement <= 2  && nb_envoi>=(3*nb_pas/4))
		  {
		    mpz_cdiv_q_ui(pas, pas, 2);
		    nb_envoi = 0;
		    nb_pas/=2;
		    nb_changement++;
		  }
		//gmp_printf("fin_exec: %Zd pas:%Zd debut_sequence:%Zd\n",fin_exec, pas, debut_sequence);
		if(mpz_cmp(debut_sequence, fin_exec)< 0){
		  mpz_sub(pas_reel, fin_exec, debut_sequence);
		  if(mpz_cmp(pas, pas_reel)<0)
		    {
		      mpz_set(pas_reel, pas);
		    }
		 		  
		  //envoi des donnes a l esclave
		  pvm_initsend(PvmDataDefault);
		  size = gmp_asprintf(&envoi_char, "%Zd", debut_sequence);
		  pvm_pkint(&size, 1, 1);
		  pvm_pkbyte(envoi_char,size+1, 1);
		  free(envoi_char);
		 
		  size = gmp_asprintf(&envoi_char, "%Zd", pas_reel);
		  pvm_pkint(&size, 1, 1);
		  pvm_pkbyte(envoi_char,size+1 , 1);
		  free(envoi_char);
		  pvm_send(source,0);
		  
		  if(mpz_cmp(pas_reel,pas)!=0)
		    {
		      mpz_set(debut_sequence,fin_exec);
		    }
		  else
		    {
		      mpz_add(debut_sequence, debut_sequence,pas);
		    }
		  nb_envoi++;
		}
		else{
		  fini++ ;
		  printf("Pas de solution pour %d esclave(s)\n", fini);
		}		
		
		break;
		
	      default:
		break;
	      }
	  }
	// suppression des esclave
	for(i=0; i<nb_esclaves;i++)
	  {
	    info = pvm_kill(tids[i]);
	    //printf("Suppression de l esclave %d: retour de pvm_kill: %d\n",i ,info);
	  }
	
	pvm_exit();
	gettimeofday(&tv2, NULL);
	printf("%d %ld\n",longueur_mdp,(tv2.tv_sec-tv1.tv_sec)*1000 + (tv2.tv_usec-tv1.tv_usec)/1000); 
	
	mpz_clear(debut_sequence);
	mpz_clear(pas_reel);
	mpz_clear(pas);
	mpz_clear(fin_exec);
	free(tids);
	free(mdp);
	free(argv_esclave[0]);
	free(argv_esclave[1]);
	
	return EXIT_SUCCESS;
}