import simpy
from simpy import events
from simpy.core import StopSimulation
from simpy.events import Timeout
import csv
import numpy
import itertools
from multiprocessing import Pool
class Cluster:
def __init__(self, env):
self.total_machines = simpy.Container(env,init=10)
self.total_time=0
#users of resource
class Job:
#enter: time the job enters the system
#timeout is how long the job occupies a resource for
#resources is how many resources a job needs in order to run
def __init__(self, job_id,job_time, job_machine_req):
#self.env = env
self.job_id = job_id
#self.job_arrivaltime = job_arrivaltime
self.job_time = job_time
self.job_machine_req = job_machine_req
#system
def system(env, job, cluster):
yield env.timeout(env.now)
#print("cuurent level of cluster",cluster.total_machines.level)
#yield cluster.total_machines_cont.get(lambda machine: machine.capacity-job.job_machine_req)
#print('%s arrives at %s' % (job.job_id, env.now))
yield cluster.total_machines.get(job[2]) #get the required resources
#yield cluster.total_machines_cont.get(job.job_machine_req)
#print('%s starts running with %s resources at %s' % (job.job_id, job.job_machine_req, env.now))
yield env.timeout(job[1]) #wait for desired time of the job
#print('%s completed job at %s' % (job.job_id, env.now))
yield cluster.total_machines.put(job[2])
cluster.total_time=env.now
print("total execution time",cluster.total_time)
env = simpy.Environment()
#create an simpy enviroment
cluster = Cluster(env) #intialize the cluster object
#jobs = [
# Job(1,0,1,8),
# Job(2,0,1,3),
# Job(3,0,2,10),
#]
jobs1= [[1,2,3],[2,3,4],[3,4,3]]
jobs2= [[1,2,3],[3,3,4],[2,4,3]]
for job in jobs1:
proc = env.process(system(env, job, cluster))
env.run(until=99999)