Ejemplo n.º 1
0
/* simulate_set performs the required piping to setup and run a simulation with the given parameters
	parameters:
		parameters: the parameters to pass as a parameter set to the simulation
	returns: the score the simulation received
	notes:
	todo:
*/
double simulate_set (double parameters[]) {
	// Get the MPI rank of the process
	int rank = get_rank();
	ostream& v = term->verbose();
	
	// Create a pipe
	int pipes[2];
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Creating a pipe " << term->reset << ". . . ";
	if (pipe(pipes) == -1) {
		term->failed_pipe_create();
		exit(EXIT_PIPE_CREATE_ERROR);
	}
	v << term->blue << "Done: " << term->reset << "using file descriptors " << pipes[0] << " and " << pipes[1] << endl;
	
	// Fork the process so the child can run the simulation
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Forking the process " << term->reset << ". . . ";
	pid_t pid = fork();
	if (pid == -1) {
		term->failed_fork();
		exit(EXIT_FORK_ERROR);
	}
	
	int child_pid;
	if (pid == 0) {
		child_pid = getpid();
	} else {
		child_pid = pid;
		v << term->blue << "Done: " << term->reset << "the child process's PID is " << child_pid << endl;
	}
	int rank_strlen = INT_STRLEN(rank);
	char* grad_fname = (char*)mallocate(sizeof(char) * (strlen("input-.gradients") + rank_strlen + 1));
	sprintf(grad_fname, "input-%d.gradients", rank);
	
	if (pid == 0) {
		char** sim_args = copy_args(ip.sim_args, ip.num_sim_args);
		store_pipe(sim_args, ip.num_sim_args - 6, pipes[0]);
		store_pipe(sim_args, ip.num_sim_args - 4, pipes[1]);
		sim_args[ip.num_sim_args - 2] = grad_fname;
		
		ofstream grad_file;
		v << "  ";
		term->rank(rank, v);
		open_file(&grad_file, grad_fname, false);
		if (ip.base_gradients != NULL) {
			grad_file << ip.base_gradients << "\n";
		}
		int loc_start = parameters[0];
		int loc_end = parameters[1];
		int val = parameters[2];
		gradient_index* gi = ip.gradient_indices;
		while (gi != NULL) {
			grad_file << gi->index << " (" << loc_start << " 100) (" << loc_end << " " << val << ")\n";
			gi = gi->next;
		}
		grad_file.close();
		
		v << "  ";
		term->rank(rank, v);
		v << term->blue << "Checking that the simulation file exists and can be executed " << term->reset << ". . . ";
		if (access(ip.sim_file, X_OK) == -1) {
			term->failed_exec();
			exit(EXIT_EXEC_ERROR);
		}
		term->done(v);
		if (execv(ip.sim_file, sim_args) == -1) {
			term->failed_exec();
			exit(EXIT_EXEC_ERROR);
		}
	} else {
		v << "  ";
		term->rank(rank, v);
		v << term->blue << "Writing to the pipe " << term->reset << "(file descriptor " << pipes[1] << ") . . . ";
		write_pipe(pipes[1], ip.sets, ip.num_sets);
		term->done(v);
	}
	
	// Wait for the child to finish simulating
	int status = 0;
	waitpid(pid, &status, WUNTRACED);
	if (WIFEXITED(status) == 0) {
		term->failed_child();
		exit(EXIT_CHILD_ERROR);
	}
	
	// Close the writing end of the pipe
	if (close(pipes[1]) == -1) {
		term->failed_pipe_write();
		exit(EXIT_PIPE_WRITE_ERROR);
	}
	
	// Pipe in the simulation's score
	int max_score;
	int scores[ip.num_sets];
	memset(scores, 0, sizeof(int) * ip.num_sets);
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Reading the pipe " << term->reset << "(file descriptor " << pipes[0] << ") . . . ";
	read_pipe(pipes[0], &max_score, scores, ip.num_sets);
	v << term->blue << "Done: " << term->reset << "(raw score of set 0: " << scores[0] << " / " << max_score << ")" << endl;
	
	// Close the reading end of the pipe
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Closing the reading end of the pipe " << term->reset << "(file descriptor " << pipes[0] << ") . . . ";
	if (close(pipes[0]) == -1) {
		term->failed_pipe_read();
		exit(EXIT_PIPE_WRITE_ERROR);
	}
	term->done(v);
	
	// Remove the gradient file
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Removing " << term->reset << grad_fname << " . . . ";
	if (remove(grad_fname) != 0) {
		term->failed_file_remove(grad_fname);
		exit(EXIT_FILE_REMOVE_ERROR);
	}
	mfree(grad_fname);
	term->done(v);
	
	// Calculate the average score of all the runs
	int avg_score = 0;
	for (int i = 0; i < ip.num_sets; i++) {
		avg_score += scores[i];
	}
	avg_score /= ip.num_sets;
	
	// libSRES requires scores from 0 to 1 with 0 being a perfect score so convert the simulation's score format into libSRES's
	return 1 - ((double)avg_score / max_score);
}
Ejemplo n.º 2
0
/* simulate_set performs the required piping to setup and run a simulation with the given parameters
	parameters:
		parameters: the parameters to pass as a parameter set to the simulation
	returns: the score the simulation received
	notes:
	todo:
*/
double simulate_set (double parameters[]) {
	// Get the MPI rank of the process
	int rank = get_rank();
	ostream& v = term->verbose();

	// Create a pipe
	int sim_in[2];
	int sim_out[2]; 
	
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Creating a pipe " << term->reset << ". . . ";
	if (pipe(sim_in) == -1 || pipe(sim_out) == -1) {
		term->failed_pipe_create();
		exit(EXIT_PIPE_CREATE_ERROR);
	}
	v << term->blue << "Done: " << term->reset << "parent_read " << sim_out[0] << " parent_write " << sim_in[1] << " child_write " << sim_out[1] << " child_read "<< sim_in[0] << endl;
	
	int parent_read = sim_out[0];
	int parent_write = sim_in[1];
	int child_write = sim_out[1];
	int child_read = sim_in[0];
	// Copy the user-specified simulation arguments and fill the copy with the pipe's file descriptors
	char** sim_args = copy_args(ip.sim_args, ip.num_sim_args);
	store_pipe(sim_args, ip.num_sim_args - 4, child_read);
	store_pipe(sim_args, ip.num_sim_args - 2, child_write);
	
	// Fork the process so the child can run the simulation
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Forking the process " << term->reset << ". . . ";
	pid_t pid = fork();
	if (pid == -1) {
		term->failed_fork();
		exit(EXIT_FORK_ERROR);
	}
	if (pid == 0) { // The child runs the simulation
		v << "  ";
		term->rank(rank, v);
		v << term->blue << "Checking that the simulation file exists and can be executed " << term->reset << ". . . ";
		if (access(ip.sim_file, X_OK) == -1) {
			term->failed_exec();
			exit(EXIT_EXEC_ERROR);
		}
		term->done(v);
		if (execv(ip.sim_file, sim_args) == -1) {
			term->failed_exec();
			exit(EXIT_EXEC_ERROR);
		}
	} else { // The parent pipes in the parameter set to run
		v << term->blue << "Done: " << term->reset << "the child process's PID is " << pid << endl;
		v << "  ";
		term->rank(rank, v);
		v << term->blue << "Writing to the pipe " << term->reset << "(file descriptor " << parent_write << ") . . . ";
		write_pipe(parent_write, parameters);
		term->done(v);
	}
	
	// Wait for the child to finish simulating
	int status = 0;
	waitpid(pid, &status, WUNTRACED);
	if (WIFEXITED(status) == 0) {
		term->failed_child();
		exit(EXIT_CHILD_ERROR);
	}
	
	
	// Pipe in the simulation's score
	//double max_score;
	double score;
	double cond_score;
	double exp_score;
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Reading the pipe " << term->reset << "(file descriptor " << parent_read << ") . . . ";
	read_pipe(parent_read, &score, &cond_score, &exp_score);
	v << term->blue << "Done: " << term->reset << "(raw score " << score << ")" << endl;
	
	/////PRINT OUT GOOD SETS
	print_good_set(parameters, score, cond_score, exp_score);
	
	// CLOSE PIPES
	v << "  ";
	term->rank(rank, v);
	v << term->blue << "Closing the the pipes " << term->reset << "(file descriptor " << parent_read << ", "<< parent_write << ", " << child_read << ", " << child_write<< ") . . . ";
	if (close(parent_read) == -1) {
		term->failed_pipe_read();
		exit(EXIT_PIPE_WRITE_ERROR);
	}
	
	if (close(parent_write) == -1) {
		term->failed_pipe_write();
		exit(EXIT_PIPE_WRITE_ERROR);
	}

	if (close(child_read) == -1) {
		term->failed_pipe_write();
		exit(EXIT_PIPE_WRITE_ERROR);
	}
	
	if (close(child_write) == -1) {
		term->failed_pipe_write();
		exit(EXIT_PIPE_WRITE_ERROR);
	}
	term->done(v);
	
	// Free the simulation arguments
	for (int i = 0; sim_args[i] != NULL; i++) {
		mfree(sim_args[i]);
	}
	mfree(sim_args);
	
	
	// libSRES requires scores from 0 to 1 with 0 being a perfect score so convert the simulation's score format into libSRES's
	return score;
}
Ejemplo n.º 3
0
/* simulate_set performs the required piping to setup and run a simulation with the given parameters
	parameters:
		parameters: the parameters to pass as a parameter set to the simulation
	returns: the score the simulation received
	notes:
	todo:
*/
void simulate_set (parameters& pr) {
	ostream& v = term->verbose();
	for (int i = 0 ; i < pr.num_sets/NUM_SETS_PER_SIM; i++){
		// Create a pipe
		int sim_in[2];
		int sim_out[2]; 
		v << "  ";
		v << term->blue << "Creating a pipe " << term->reset << ". . . ";
		if (pipe(sim_in) == -1 || pipe(sim_out) == -1) {
			term->failed_pipe_create();
			exit(EXIT_PIPE_CREATE_ERROR);
		}
		v << term->blue << "Done: " << term->reset << "parent_read " << sim_out[0] << " parent_write " << sim_in[1] << " child_write " << sim_out[1] << " child_read "<< sim_in[0] << endl;
		
		int parent_read = sim_out[0];
		int parent_write = sim_in[1];
		int child_write = sim_out[1];
		int child_read = sim_in[0];
		
		cout << ip.sim_args[0] << ip.sim_args[1] << ip.sim_args[2]<< endl;
		// Copy the user-specified simulation arguments and fill the copy with the pipe's file descriptors
		char** sim_args = copy_args(ip.sim_args, ip.num_sim_args);
		cout << "after storing pipe"<< endl;
		store_pipe(sim_args, ip.num_sim_args - 4, child_read);
		store_pipe(sim_args, ip.num_sim_args - 2, child_write);
		
		// Fork the process so the child can run the simulation
		v << "  ";
		v << term->blue << "Forking the process " << term->reset << ". . . ";
		pid_t pid = fork();
		if (pid == -1) {
			term->failed_fork();
			exit(EXIT_FORK_ERROR);
		}
		if (pid == 0) { // The child runs the simulation
			v << "  ";
			v << term->blue << "Checking that the simulation file exists and can be executed " << term->reset << ". . . ";
			if (access(ip.sim_file, X_OK) == -1) {
				term->failed_exec();
				exit(EXIT_EXEC_ERROR);
			}
			term->done(v);
			if (execv(ip.sim_file, sim_args) == -1) {
				term->failed_exec();
				exit(EXIT_EXEC_ERROR);
			}
		}
		else { // The parent pipes in the parameter set to run
			v << term->blue << "Done: " << term->reset << "the child process's PID is " << pid << endl;
			v << "  ";
			v << term->blue << "Writing to the pipe " << term->reset << "(file descriptor " << parent_write << ") . . . ";
			write_pipe(parent_write, pr, i);
			term->done(v);
		}
		
		// Wait for the child to finish simulating
		int status = 0;
		waitpid(pid, &status, WUNTRACED);
		if (WIFEXITED(status) == 0) {
			term->failed_child();
			exit(EXIT_CHILD_ERROR);
		}
		
		
		// Pipe in the simulation's score
		double* score;
		v << "  ";
		v << term->blue << "Reading the pipe " << term->reset << "(file descriptor " << parent_read << ") . . . ";
		read_pipe(parent_read, &score);
		v << term->blue << "Done: " << term->reset << "(raw score " << score << " / " << 1 << ")" << endl;
	
		// CLOSE PIPES
		v << "  ";
		v << term->blue << "Closing the the pipes " << term->reset << "(file descriptor " << parent_read << ", "<< parent_write << ", " << child_read << ", " << child_write<< ") . . . ";
		if (close(parent_read) == -1) {
			term->failed_pipe_read();
			exit(EXIT_PIPE_WRITE_ERROR);
		}
		
		if (close(parent_write) == -1) {
			term->failed_pipe_write();
			exit(EXIT_PIPE_WRITE_ERROR);
		}
	
		if (close(child_read) == -1) {
			term->failed_pipe_write();
			exit(EXIT_PIPE_WRITE_ERROR);
		}
		
		if (close(child_write) == -1) {
			term->failed_pipe_write();
			exit(EXIT_PIPE_WRITE_ERROR);
		}
		term->done(v);
		
		print_good_set(pr, &score, i);
		
		// Free the simulation arguments
		for (int i = 0; sim_args[i] != NULL; i++) {
			mfree(sim_args[i]);
		}
		mfree(sim_args);
	}
}