Skip to main content
Sign in
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • main
  • master
2 results

Target

Select target project
  • LABOUREL Arnaud / cluster template
  • LABOUREL Arnaud / cluster correction
2 results
Select Git revision
  • main
  • master
2 results
Show changes

Commits on Source 1

19 files
+ 397
10
Compare changes
  • Side-by-side
  • Inline

Files

+1 −2
Original line number Diff line number Diff line
@@ -10,5 +10,4 @@ sur les nœuds de manière à maximiser l'utilisation de la mémoire et du nombr

## Membres du projet

- NOM, prénom, numéro de groupe, du premier participant
- NOM, prénom, numéro de groupe, du deuxième participant
 No newline at end of file
- LABOUREL, Arnaud
 No newline at end of file
+2 −5
Original line number Diff line number Diff line
@@ -4,14 +4,11 @@ public class App {


  public static void main(String[] args) throws Exception{
    // TODO : décommenter pour tester le main.
    /*
    Job job1 = new Job(1000, 1000);
    Job job2 = new Job(3000, 1000);
    Node node = new Node("Calcul", 10000, 3000);
    node.acceptJob(job1);
    node.acceptJob(job2);
    node.accept(job1);
    node.accept(job2);
    node.printJobs();
     */
  }
}
+53 −0
Original line number Diff line number Diff line
package cluster;

import java.util.ArrayList;
import java.util.List;

public class Controller<J extends Job> {
  private List<J> nonScheduledJobs = new ArrayList<>();
  private final List<Node> nodes = new ArrayList<>();
  private final String name;
  private final Scheduler<J> scheduler;

  void addNode(Node node){
    nodes.add(node);
  }

  void submitJob(J job){
    nonScheduledJobs.add(job);
  }

  Controller(String name, Scheduler<J> scheduler){
    this.name = name;
    this.scheduler = scheduler;
  }

  void printNodesAndNonScheduledJobs(){
    System.out.println(this);
    printNodes();
    System.out.println("Non scheduled jobs");
    printNonScheduledJobs();
  }

  private void printNodes(){
    for (Node node : nodes){
      node.printJobs();
      System.out.println();
    }
  }

  private void printNonScheduledJobs(){
    for (Job job : nonScheduledJobs){
      System.out.println(job);
    }
  }

  public void scheduleJobs(){
    nonScheduledJobs = scheduler.scheduleJobs(nonScheduledJobs, nodes);
  }

  @Override
  public String toString() {
    return "Controller " + name;
  }
}
+46 −0
Original line number Diff line number Diff line
package cluster;

public class Job {
  private final int memory;
  private final int flop;
  private final int id;
  private static int jobCount = 0;

  public Job(int memory, int flop) {
    if (memory <= 0){
      throw new IllegalArgumentException("Cannot create a job with negative memory : " + getMemory());
    }
    if (flop <= 0){
      throw new IllegalArgumentException("Cannot create a job with a negative number of operations : " + getMemory());
    }
    this.memory = memory;
    this.flop = flop;
    id = jobCount++;
  }

  public int getFlop() {
    return flop;
  }

  public int getMemory() {
    return memory;
  }

  public int getId() {
    return id;
  }

  public static void resetJobCount() {
    jobCount = 0;
  }

  public static int getJobCount() {
    return jobCount;
  }

  public String toString(){
    return "Job "+ getId()
            + " (" + getFlop() + "FLOP, "
            + getMemory() + "o)";
  }
}
+5 −0
Original line number Diff line number Diff line
package cluster;

public interface JobGenerator {
  Job generateJob();
}
+30 −0
Original line number Diff line number Diff line
package cluster;

public class Main {
  public static void main(String[] args){
    JobGenerator randomJobGenerator = new RandomJobGenerator(10, 100, 10, 11, 0);
    Node node = new Node("Calcul", 10000, 3000);
    for(int i = 0; i<10; i++){
      try {
        node.accept(randomJobGenerator.generateJob());
      }
      catch (NotEnoughResourceException e){
        e.printStackTrace();
      }
    }
    node.printJobs();
    /*
    Job job1 = new Job(1000, 1000);
    Job job2 = new Job(3000, 1000);
    Node node = new Node("Calcul", 10000, 3000);
    Scheduler<Job> randomScheduler = new RandomScheduler();
    Controller<Job> controller = new Controller<>("Pharo", randomScheduler);
    controller.addNode(node);
    controller.submitJob(job1);
    controller.submitJob(job2);
    controller.scheduleJobs();
    controller.printNodesAndNonScheduledJobs();
    */

  }
}
 No newline at end of file
+69 −0
Original line number Diff line number Diff line
package cluster;

import java.util.ArrayList;
import java.util.List;

public class Node {
  private final String name;
  private final int memoryCapacity;
  private final int flopCapacity;

  private int availableMemory;
  private int availableFlop;
  private final List<Job> assignedJobs = new ArrayList<>();

  Node(String name, int memoryCapacity, int flopCapacity) {
    this.name = name;
    this.memoryCapacity = memoryCapacity;
    this.flopCapacity = flopCapacity;
    availableMemory = memoryCapacity;
    availableFlop = flopCapacity;
  }

  boolean canAccept(Job job){
    return availableMemory >= job.getMemory() &&
            availableFlop >= job.getFlop();
  }

  boolean canHandle(Job job){
    return memoryCapacity >= job.getMemory() &&
            flopCapacity >= job.getFlop();
  }

  void accept(Job job) throws NotEnoughResourceException{
    if(memoryCapacity < job.getMemory()){
      throw new NotEnoughTotalMemoryException(this, job);
    }
    if(availableMemory < job.getMemory()){
      throw new NotEnoughRemainingMemoryException(this, job);
    }
    if(flopCapacity < job.getFlop()){
      throw new NotEnoughTotalFlopException(this, job);
    }
    if(availableFlop < job.getFlop()){
      throw new NotEnoughRemainingFlopException(this, job);
    }
    assignedJobs.add(job);
    availableMemory -= job.getMemory();
    availableFlop -= job.getFlop();
  }

  int usedMemory(){
    return memoryCapacity - availableMemory;
  }

  int usedFlop(){
    return flopCapacity - availableFlop;
  }

  public String toString(){
    return "Node " + name + " (" + usedFlop() + "/" + flopCapacity + "FLOP, " + usedMemory() + "/" + memoryCapacity +"o)";
  }

  void printJobs(){
    System.out.println(this);
    for(Job job : assignedJobs){
      System.out.println(job);
    }
  }
}
+12 −0
Original line number Diff line number Diff line
package cluster;

public class NotEnoughRemainingFlopException extends NotEnoughResourceException {
  public NotEnoughRemainingFlopException(Node node, Job job) {
    super(node, job);
  }

  @Override
  public String getSentenceMessage() {
    return " has not enough remaining FLOP to handle ";
  }
}
+12 −0
Original line number Diff line number Diff line
package cluster;

public class NotEnoughRemainingMemoryException extends NotEnoughResourceException{
  public NotEnoughRemainingMemoryException(Node node, Job job) {
    super(node, job);
  }

  @Override
  public String getSentenceMessage() {
    return " has not enough remaining memory to handle ";
  }
}
+18 −0
Original line number Diff line number Diff line
package cluster;

public abstract class NotEnoughResourceException extends Exception {
  private final Node node;
  private final Job job;

  public NotEnoughResourceException(Node node, Job job) {
    this.node = node;
    this.job = job;
  }

  @Override
  public String getMessage() {
    return node + getSentenceMessage() + job;
  }

  public abstract String getSentenceMessage();
}
+12 −0
Original line number Diff line number Diff line
package cluster;

public class NotEnoughTotalFlopException extends NotEnoughResourceException{
  public NotEnoughTotalFlopException(Node node, Job job) {
    super(node, job);
  }

  @Override
  public String getSentenceMessage() {
    return " has not enough total number of operations to handle ";
  }
}
+12 −0
Original line number Diff line number Diff line
package cluster;

public class NotEnoughTotalMemoryException extends NotEnoughResourceException {
  public NotEnoughTotalMemoryException(Node node, Job job) {
    super(node, job);
  }

  @Override
  public String getSentenceMessage() {
    return " has not enough total memory to handle ";
  }
}
+15 −0
Original line number Diff line number Diff line
package cluster;

public class PriorityJob extends Job implements Comparable<PriorityJob>{
  private final int priority;

  public PriorityJob(int memory, int flop, int priority) {
    super(memory, flop);
    this.priority = priority;
  }

  @Override
  public int compareTo(PriorityJob job) {
    return job.priority - this.priority;
  }
}
+31 −0
Original line number Diff line number Diff line
package cluster;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class PriorityScheduler implements Scheduler<PriorityJob> {
  @Override
  public List<PriorityJob> scheduleJobs(List<PriorityJob> jobs, List<Node> nodes) {
    List<PriorityJob> nonScheduledJobs = new ArrayList<>();
    Collections.sort(jobs);
    for (PriorityJob job : jobs) {
      if(!scheduleJob(job, nodes)){
        nonScheduledJobs.add(job);
      }
    }
    return nonScheduledJobs;
  }


  private boolean scheduleJob(Job job, List<Node> nodes){
    for (Node node : nodes) {
      try {
        node.accept(job);
        return true;
      }
      catch (NotEnoughResourceException e){}
    }
    return false;
  }
}
+27 −0
Original line number Diff line number Diff line
package cluster;

import java.util.Random;

public class RandomJobGenerator implements JobGenerator{
  private final int minMemory;
  private final int maxMemory;
  private final int minFlop;
  private final int maxFlop;
  private final Random random;

  public RandomJobGenerator(int minMemory, int maxMemory, int minFlop, int maxFlop, long seed) {
    this.minMemory = minMemory;
    this.maxMemory = maxMemory;
    this.minFlop = minFlop;
    this.maxFlop = maxFlop;
    this.random = new Random(seed);
  }

  private int nextInt(int min, int max){
    return min + random.nextInt(max - min + 1);
  }

  public Job generateJob(){
    return new Job(nextInt(minMemory, maxMemory), nextInt(minFlop, maxFlop));
  }
}
+27 −0
Original line number Diff line number Diff line
package cluster;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RandomScheduler implements Scheduler<Job>{
  private Random random = new Random();

  public Node randomNode(List<Node> nodes){
    return nodes.get(random.nextInt(nodes.size()));
  }

  @Override
  public List<Job> scheduleJobs(List<Job> jobs, List<Node> nodes) {
    List<Job> nonScheduledJobs  = new ArrayList<>();
    for(Job job : jobs){
      try{
        randomNode(nodes).accept(job);
      }
      catch (NotEnoughResourceException e){
        nonScheduledJobs.add(job);
      }
    }
    return nonScheduledJobs ;
  }
}
+7 −0
Original line number Diff line number Diff line
package cluster;

import java.util.List;

public interface Scheduler<J extends Job> {
  List<J> scheduleJobs(List<J> jobs, List<Node> nodes);
}
+16 −0
Original line number Diff line number Diff line
package cluster;

public class UniformJobGenerator implements JobGenerator{
  private final int memory;
  private final int flop;

  public UniformJobGenerator(int memory, int flop) {
    this.memory = memory;
    this.flop = flop;
  }

  @Override
  public Job generateJob() {
    return new Job(memory, flop);
  }
}
+2 −3
Original line number Diff line number Diff line
@@ -5,8 +5,7 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

public class JobTest {
    //TODO : décommenter pour tester la classe Job
/*

    @Test
    void testToString(){
        // Test with Junit
@@ -44,6 +43,6 @@ public class JobTest {
            assertThat(Job.getJobCount()).isEqualTo(jobCount);
        }
    }
    */


}