Пример #1
0
double farmer(int numprocs) {
	
	MPI_Status status;
	stack* bag_of_tasks = new_stack();
	
	int i,id;
	int tag = -1;
	double area = 0.0;
	
	/* slackers is a variable to keep track of the free ("slacking") processes.*/
	int slackers = numprocs-1; 
	int* is_worker_busy = (int*) malloc(sizeof(int)*(numprocs-1)); 
	
	/* An integer array for workers. If 1 then the worker is busy if 0 then it is not doing any tasks.
	We fill the array with zeroes initially since none of the workers are working. */
	for (i=0;i<slackers;i++){
		is_worker_busy[i] = 0;
	}
		
	double point[2];
	point[0] = A;
	point[1] = B;
	/* We push the initial task on the bag of tasks stack*/	
	push(point, bag_of_tasks);
	
	
	while ((!is_empty(bag_of_tasks)||slackers!=0)&&(tag!=READY_TAG)){
		
		MPI_Recv(point, 2, MPI_DOUBLE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status); 
		
		id = status.MPI_SOURCE;
		tag = status.MPI_TAG;
		is_worker_busy[id-1] = 1;
		slackers--;

		if ( tag == AREA_TAG) { 
			area += point[0];
		}
		else if (tag == SPLIT_TAG) { 
			push(point,bag_of_tasks);
      			MPI_Recv(point, 2, MPI_DOUBLE, id, tag, MPI_COMM_WORLD, &status);
      			push(point,bag_of_tasks);
		} 
		
		
		slackers=send_tasks(bag_of_tasks,is_worker_busy,slackers,tasks_per_process);
		
	}	


	// Signal all the workers that we are ready.
	for (i=1;i<numprocs;i++) { 

		MPI_Send(NULL, 0, MPI_INT, i, READY_TAG, MPI_COMM_WORLD);
	}
	return area;
}
Пример #2
0
int master_fun(int argc, char *argv[])
{
  msg_vm_t vm;
  unsigned int i;

  xbt_dynar_t worker_pms = MSG_process_get_data(MSG_process_self());
  int nb_workers = xbt_dynar_length(worker_pms);

  xbt_dynar_t vms = xbt_dynar_new(sizeof(msg_vm_t), NULL);


  /* Launch VMs and worker processes. One VM per PM, and one worker process per VM. */

  XBT_INFO("# Launch %d VMs", nb_workers);
  for (i = 0; i< nb_workers; i++) {
    char *vm_name = bprintf("VM%02d", i);
    char *pr_name = bprintf("WRK%02d", i);

    msg_host_t pm = xbt_dynar_get_as(worker_pms, i, msg_host_t);

    XBT_INFO("create %s on PM(%s)", vm_name, MSG_host_get_name(pm));
    msg_vm_t vm = MSG_vm_create_core(pm, vm_name);

    s_vm_params_t params;
    memset(&params, 0, sizeof(params));
    params.ramsize = 1L * 1024 * 1024 * 1024; // 1Gbytes
    MSG_host_set_params(vm, &params);

    MSG_vm_start(vm);
    xbt_dynar_push(vms, &vm);

    XBT_INFO("put a process (%s) on %s", pr_name, vm_name);
    MSG_process_create(pr_name, worker_fun, NULL, vm);

    xbt_free(vm_name);
    xbt_free(pr_name);
  }


  /* Send a bunch of work to every one */
  XBT_INFO("# Send a task to %d worker process", nb_workers);
  send_tasks(nb_workers);

  XBT_INFO("# Suspend all VMs");
  xbt_dynar_foreach(vms, i, vm) {
    const char *vm_name = MSG_host_get_name(vm);
    XBT_INFO("suspend %s", vm_name);
    MSG_vm_suspend(vm);
  }

  XBT_INFO("# Wait a while");
  MSG_process_sleep(2);

  XBT_INFO("# Resume all VMs");
  xbt_dynar_foreach(vms, i, vm) {
    MSG_vm_resume(vm);
  }
Пример #3
0
//**********************************************************************************	
	void perform_respond(){
		char params[20][256];
		int count, rolle, parent, fltausw, fltwert;
		time_t sessionid;
		long len;
		int maxlen = 1199;
		char *envptr;
		char input[1200];
		char *lenstr;

		if(strcmp(getenv("REQUEST_METHOD"), "GET") == 0){
			envptr = getenv("QUERY_STRING");

			if(envptr == NULL || strlen(envptr) == 0){
				count = 0;
			}
			else if(strlen(envptr) > maxlen){
				send_error(mERR_MAXLEN);
				return;
			}
			else{
				count = parse_data_from_input(envptr, params);
			}
		}
		else if(strcmp(getenv("REQUEST_METHOD"), "POST") == 0){
			lenstr = getenv("CONTENT_LENGTH");

			if(lenstr == NULL || sscanf(lenstr, "%ld", &len) != 1 || len > maxlen){
				send_error(mERR_MAXLEN);
				return;
			}
			else{
				fgets(input, len + 1, stdin);
				count = parse_data_from_input(input, params);
			}
		}
		else{
			send_error(mERR_REQUEST);
			return;
		}

		if(count == 0){
			send_login(mMSG_BLANK);
		}
		else if(strcmp(params[0], "action=verify_login") == 0){
			verify_login(params, count);
		}
		else if(strcmp(params[0], "action=home") == 0){
			if(count == 5){
				sscanf(params[1], "sessionid=%ld", &sessionid);
				sscanf(params[2], "rolle=%d", &rolle);
				fltausw = 0;
				sscanf(params[3], "fltausw=%d", &fltausw);
				fltwert = 0;
				sscanf(params[4], "fltwert=%d", &fltwert);
				send_tasks(sessionid, rolle, fltausw, fltwert, mMSG_BLANK);
			}
		}
		else if(strcmp(params[0], "action=send_subtasks") == 0){
			if(count == 4){
				sscanf(params[1], "parent=%d", &parent);
				sscanf(params[2], "sessionid=%ld", &sessionid);
				sscanf(params[3], "rolle=%d", &rolle);
				send_subtasks(parent, sessionid, rolle, mMSG_BLANK);
			}
		}
		else if(strcmp(params[0], "action=save_task") == 0){
			save_task(params, count);
		}
		else if(strcmp(params[0], "action=save_subtask") == 0){
			save_subtask(params, count);
		}
		else if(strcmp(params[0], "action=send_query") == 0){
			if(count == 3){
				sscanf(params[1], "sessionid=%ld", &sessionid);
				sscanf(params[2], "rolle=%d", &rolle);
				send_query(sessionid, rolle);
			}
		}
		else if(strcmp(params[0], "action=query") == 0){
			query(params, count);
		}
		else if(strcmp(params[0], "action=send_usradmin") == 0){
			if(count == 3){
				sscanf(params[1], "sessionid=%ld", &sessionid);
				sscanf(params[2], "rolle=%d", &rolle);
				send_usradmin(sessionid, rolle, mMSG_BLANK);
			}
		}
		else if(strcmp(params[0], "action=verify_pwchange") == 0){
			verify_pwchange(params, count);
		}
		else if(strcmp(params[0], "action=admin") == 0){
			send_admin(params, count);
		}
		else{
			send_error(mERR_ACTION);
			return;
		}
	}
Пример #4
0
Task_t* Master::svc( Task_t* task )
{
	if ( task == nullptr )
	{
		send_tasks();
		// Keep it alive.
		return GO_ON;
	}
	else
	{
		// Increment the counter of complete tasks and delete the task.
		this->counter_complete_tasks++;
		delete( task );

		// Get the worker identifier of who is responding.
		int worker_id = lb->get_channel_id();

		// If it did not complete scattering the Grid.
		if ( this->counter_sent_tasks < this->num_tasks )
		{
			Task_t* task = create_new_task();
			this->lb->ff_send_out_to( task, worker_id );
		}
#if TAKE_ALL_TIME
		else if ( this->first_worker )
		{
			// When it has completed to scatter the Grid, it start counting the Barrier Phase
			// from the first Worker that answer back.
			// Start - Barrier Phase
			t1 = std::chrono::high_resolution_clock::now();
			this->first_worker = false;
		}
#endif // TAKE_ALL_TIME

		// If the counter is equal to the total number of tasks, we complete the iteration.
		if ( this->counter_complete_tasks == this->num_tasks )
		{
#if TAKE_ALL_TIME
			// End - Barrier Phase
			t2 = std::chrono::high_resolution_clock::now();
			barrier_time += std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
#endif // TAKE_ALL_TIME

			// Reset the accumulators that tracks the starting and ending point of the current chunk.
			this->start_chunk = 0;
			this->end_chunk = start;

			// Reset the first worker variable
			this->first_worker = true;

			// Reset counters.
			this->counter_sent_tasks = 0;
			this->counter_complete_tasks = 0;

			// Increment the number of completed iterations.
			this->completed_iterations++;

			// Compute the action necessary to complete the computation of this generation.
			copyborder_time += end_generation( g, this->completed_iterations );

			// Send EOS if we completed all the iterations.
			if ( this->completed_iterations == this->iterations )
			{
#if TAKE_ALL_TIME
				// Print the total time in order to compute the end_generation functions.
				printTime( copyborder_time, "copy border" );

				// Print the total time in order to compute the barrier phase.
				printTime( barrier_time, "barrier phase" );
#endif // TAKE_ALL_TIME

				return EOS;
			}
			else // We go on with the computation of the next generation.
			{
				send_tasks();
				// Keep it alive.
				return GO_ON;
			}
		}
		else // Keep it alive.
			return GO_ON;
	}
}