Commit 6541594e authored by Millian Poquet's avatar Millian Poquet

Machines : C->C++

parent d9e6ba37
#include "machines.h"
int nb_machines = 0;
Machine * machines = NULL;
void createMachines(xbt_dynar_t hosts)
{
xbt_assert(machines == NULL, "Bad call to createMachines(): machines already created");
nb_machines = xbt_dynar_length(hosts);
machines = xbt_new(Machine, nb_machines);
msg_host_t host;
unsigned int i;
xbt_dynar_foreach(hosts, i, host)
{
Machine * machine = &machines[i];
machine->host = host;
machine->jobs_being_computed = xbt_dynar_new(sizeof(int), NULL);
machine->state = MACHINE_STATE_IDLE;
}
}
void displayDynarOfIntegers(const xbt_dynar_t d)
{
unsigned int index;
int value;
unsigned int lastIndex = xbt_dynar_length(d) - 1;
printf("[");
xbt_dynar_foreach(d, index, value)
{
if (index == lastIndex)
printf("%d", value);
else
printf("%d,", value);
}
printf("]");
}
void displayMachine(int machineID, const Machine * machine)
{
static const char* machineStateToStr[] = {"MACHINE_STATE_SLEEPING",
"MACHINE_STATE_IDLE",
"MACHINE_STATE_COMPUTING"};
printf("Machine %d, state=%s, jobs=", machineID, machineStateToStr[machine->state]);
displayDynarOfIntegers(machine->jobs_being_computed);
printf("\n");
fflush(stdout);
}
void freeMachines()
{
if (machines != NULL)
{
for (int i = 0; i < nb_machines; ++i)
xbt_dynar_free(&(machines[i].jobs_being_computed));
free(machines);
machines = NULL;
}
}
void updateMachinesOnJobRun(int jobID, int nbUsedMachines, int *usedMachines)
{
for (int i = 0; i < nbUsedMachines; ++i)
{
int machineID = usedMachines[i];
Machine * machine = &machines[machineID];
machine->state = MACHINE_STATE_COMPUTING;
//displayMachine(machineID, machine);
// Let's update the jobs_being_computed data structure
// This structure contains sorted job IDs
unsigned long nbBeingComputedJobs = xbt_dynar_length(machine->jobs_being_computed);
int inserted = 0;
unsigned int j;
int job;
xbt_dynar_foreach(machine->jobs_being_computed, j, job)
{
if (job > jobID)
{
xbt_dynar_insert_at(machine->jobs_being_computed, j, &jobID);
//displayMachine(machineID, machine);
inserted = 1;
break;
}
}
if (!inserted)
{
xbt_dynar_push_as(machine->jobs_being_computed, int, jobID);
//displayMachine(machineID, machine);
}
xbt_assert(xbt_dynar_length(machine->jobs_being_computed) == nbBeingComputedJobs + 1);
xbt_assert(*((int*)xbt_dynar_get_ptr(machine->jobs_being_computed, j)) == jobID);
}
}
void updateMachinesOnJobEnd(int jobID, int nbUsedMachines, int *usedMachines)
{
for (int i = 0; i < nbUsedMachines; ++i)
{
int machineID = usedMachines[i];
Machine * machine = &machines[machineID];
//displayMachine(machineID, machine);
// Let's find where jobID is in the jobs_being_computed data structure
unsigned long nbBeingComputedJobs = xbt_dynar_length(machine->jobs_being_computed);
int found = 0;
unsigned int j;
int job;
xbt_dynar_foreach(machine->jobs_being_computed, j, job)
{
if (job == jobID)
{
xbt_dynar_remove_at(machine->jobs_being_computed, j, NULL);
//displayMachine(machineID, machine);
found = 1;
break;
}
}
xbt_assert(found == 1);
xbt_assert(xbt_dynar_length(machine->jobs_being_computed) == nbBeingComputedJobs - 1);
if (xbt_dynar_is_empty(machine->jobs_being_computed))
{
machine->state = MACHINE_STATE_IDLE;
// todo: handle the Pajé trace in this file, not directly in batsim.c
}
}
}
#include "machines.hpp"
#include <algorithm>
#include <iterator>
using namespace std;
Machines::Machines()
{
}
Machines::~Machines()
{
_machines.clear();
}
void Machines::createMachines(xbt_dynar_t hosts)
{
xbt_assert(_machines.size() == 0, "Bad call to Machines::createMachines(): machines already created");
int nb_machines = xbt_dynar_length(hosts);
_machines.resize(nb_machines);
msg_host_t host;
unsigned int i;
xbt_dynar_foreach(hosts, i, host)
{
Machine & machine = _machines[i];
machine.id = i;
machine.host = host;
machine.jobs_being_computed = {};
machine.state = machine_state::IDLE;
}
}
const Machine & Machines::operator[](int machineID) const
{
return _machines[machineID];
}
Machine & Machines::operator[](int machineID)
{
return _machines[machineID];
}
void Machines::updateMachinesOnJobRun(int jobID, const std::vector<int> & usedMachines)
{
for (int machineID : usedMachines)
{
Machine & machine = _machines[machineID];
machine.state = machine_state::COMPUTING;
// cout << machine;
machine.jobs_being_computed.insert(jobID);
// cout << machine;
}
}
void Machines::updateMachinesOnJobEnd(int jobID, const std::vector<int> & usedMachines)
{
for (int machineID : usedMachines)
{
Machine & machine = _machines[machineID];
// cout << machine;
// Let's find where jobID is in the jobs_being_computed data structure
int ret = machine.jobs_being_computed.erase(jobID);
xbt_assert(ret == 1);
if (machine.jobs_being_computed.empty())
{
machine.state = machine_state::IDLE;
// todo: handle the Pajé trace in this file, not directly in batsim.c
}
// cout << machine;
}
}
ostream & operator<<(ostream & out, const Machine & machine)
{
static const char* machineStateToStr[] = {"SLEEPING",
"IDLE",
"COMPUTING"};
out << "Machine " << machine.id << ", ";
out << "state = " << machineStateToStr[machine.state] << ", ";
out << "jobs = [";
std::copy(machine.jobs_being_computed.begin(), machine.jobs_being_computed.end(),
std::ostream_iterator<char>(out, " "));
out << "]" << endl;
return out;
}
int main()
{
return 0;
}
\ No newline at end of file
#pragma once
#include <iostream>
#include <vector>
#include <set>
#include <simgrid/msg.h>
enum e_machine_state_t
namespace machine_state
{
MACHINE_STATE_SLEEPING,
MACHINE_STATE_IDLE,
MACHINE_STATE_COMPUTING,
};
typedef enum e_machine_state_t MachineState;
enum MachineState
{
SLEEPING,
IDLE,
COMPUTING,
};
}
struct s_machine_t
struct Machine
{
int id;
msg_host_t host;
MachineState state;
xbt_dynar_t jobs_being_computed; // the IDs of the jobs being computed by the machine
machine_state::MachineState state;
std::set<int> jobs_being_computed;
};
std::ostream & operator<<(std::ostream & out, const Machine & machine);
class Machines
{
public:
Machines();
~Machines();
void createMachines(xbt_dynar_t hosts);
void updateMachinesOnJobRun(int jobID, const std::vector<int> & usedMachines);
void updateMachinesOnJobEnd(int jobID, const std::vector<int> & usedMachines);
const Machine & operator[](int machineID) const;
Machine & operator[](int machineID);
private:
std::vector<Machine> _machines;
};
typedef struct s_machine_t Machine;
// The array of machines
extern int nb_machines;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment