<commit_before># gvas.sims.simple
# A relatively simple simulation to exercise the cluster objects.
#
# Author:   <NAME> <<EMAIL>>
# Created:  Sun Dec 06 12:49:03 2015 -0500
#
# Copyright (C) 2015 <NAME>
# For license information, see LICENSE.txt
#
# ID: filename.py [] <EMAIL> $

"""
A relatively simple simulation to exercise the cluster objects.
"""

##########################################################################
# Imports
##########################################################################

import simpy

from gvas.base import Process, NamedProcess
from gvas.base import Simulation
from gvas.cluster import *
from gvas.dynamo import Uniform

##########################################################################
# Simulation Configuration
##########################################################################

CLUSTER_SIZE    = 1
RACK_SIZE       = 16
NODE_CPUS       = 1
NODE_MEMORY     = 4

##########################################################################
# Classes
##########################################################################

class SimpleSimulation(Simulation):

    def script(self):
        node_options = {
            'cpus': NODE_CPUS,
            'memory': NODE_MEMORY
        }
        gen = Cluster.create(
            self.env,
            size=RACK_SIZE,
            node_options=node_options
        )
        cluster = gen.next()

        nodes = [cluster.add() for i in range(4)]
        print nodes

        pgen = PingProgram.create(self.env)
        print pgen
        p = pgen.next()


class PingProgram(Program):

    def __init__(self, env, *args, **kwargs):
        self.randy = Uniform(10, 100, 'int')

        self.wait_for_recv = self.env.Event()

        super(PingProgram, self).__init__(env, *args, **kwargs)


    def wait(self):
        """
        Part of the circle of life for this program.
        """
        print "Program {}: waiting for recv at {}".format(self.id, self.env.now)
        # duration = yield self.env.process(wait_for_recv)
        duration = yield self.env.timeout(self.randy.next())
        print "Program {}: received at {}\n".format(self.id, self.env.now)

    def work(self):
        """
        Part of the circle of life for this program.
        """
        print "Program {}: starting work at {}".format(self.id, self.env.now)
        yield self.env.timeout(self.randy.next())
        print "Program {}: done working at {}\n".format(self.id, self.env.now)

    def send(self):
        """
        Send a message to one or more nodes.
        """
        print "Program {}: sending at {}".format(self.id, self.env.now)

        # TODO: CREATE EVENT TO TRIGGER WAKEUP FOR ANOTHER NODE

        print "Program {}: done sending at {}\n".format(self.id, self.env.now)

    def run(self):
        """
        Kicks off execution of a simulated program. This method contains a loop
        to cycle through specific behaviors such as:
            - wait for recv
            - sleep for random time
            - does a send to one or more other Programs/Nodes
            - repeat
        """
        while True:
            duration = self.wait()
            self.work(duration)
            self.send()


##########################################################################
# Execution
##########################################################################

if __name__ == '__main__':
    s = SimpleSimulation()
    s.run()<commit_msg>very basic pingprogram which can wait, work, send based solely on timeouts<commit_after># gvas.sims.simple
# A relatively simple simulation to exercise the cluster objects.
#
# Author:   <NAME> <<EMAIL>>
# Created:  Sun Dec 06 12:49:03 2015 -0500
#
# Copyright (C) 2015 <NAME>
# For license information, see LICENSE.txt
#
# ID: filename.py [] <EMAIL> $

"""
A relatively simple simulation to exercise the cluster objects.
"""

##########################################################################
# Imports
##########################################################################

import simpy

from gvas.base import Process, NamedProcess
from gvas.base import Simulation
from gvas.cluster import *
from gvas.dynamo import Uniform

##########################################################################
# Simulation Configuration
##########################################################################

CLUSTER_SIZE    = 1
RACK_SIZE       = 16
NODE_CPUS       = 1
NODE_MEMORY     = 4

##########################################################################
# Classes
##########################################################################

class SimpleSimulation(Simulation):

    def script(self):
        node_options = {
            'cpus': NODE_CPUS,
            'memory': NODE_MEMORY
        }
        gen = Cluster.create(
            self.env,
            size=RACK_SIZE,
            node_options=node_options
        )
        cluster = gen.next()

        nodes = [cluster.add() for i in range(4)]
        print "Adding nodes:\n {}\n".format(nodes)

        pgen = PingProgram.create(self.env)
        nodes[0].assign(pgen.next())
        nodes[1].assign(pgen.next())



"""
Program needs something like a work queue (container) such that if it's working
and then recieves a message, it can queue the work for the message.

Program should only wait if there are no messages/work in the queue
"""
class PingProgram(Program):

    def __init__(self, env, *args, **kwargs):
        self.randy = Uniform(10, 50, 'int')

        self.wait_finished = env.event()
        self.msg_recieved = env.event()
        self.finished_work = env.event()
        self.msg_sent = env.event()

        super(PingProgram, self).__init__(env, *args, **kwargs)


    def receive(self):
        self.msg_recieved.succeed()

    def wait(self):
        """
        Waits until its event is triggered.
        """
        print "Program {}: waiting for recv at {}\n".format(self.id, self.env.now)
        yield self.env.timeout(self.randy.next())
        print "Program {}: received at {}\n".format(self.id, self.env.now)

        self.msg_recieved.succeed()
        self.msg_recieved = self.env.event()

        # force advance of the clock
        yield self.env.timeout(1)

    def work(self, duration=None):
        """
        Simulates work by going to sleep for a bit.
        """

        if not duration:
            duration = self.randy.next()

        print "Program {}: starting work at {}\n".format(self.id, self.env.now)
        yield self.env.timeout(duration)
        print "Program {}: done working at {}\n".format(self.id, self.env.now)

        self.finished_work.succeed()
        self.finished_work = self.env.event()

        # force advance of the clock
        yield self.env.timeout(1)

    def send(self):
        """
        Send a message to one or more nodes.
        """
        print "Program {}: sending at {}\n".format(self.id, self.env.now)
        # TODO: CREATE EVENT TO TRIGGER WAKEUP FOR ANOTHER NODE
        print "Program {}: done sending at {}\n".format(self.id, self.env.now)

        # force advance of the clock
        yield self.env.timeout(1)

    def run(self):
        """
        Kicks off execution of a simulated program. This method contains a loop
        to cycle through specific behaviors such as:
            - wait for recv
            - sleep for random time
            - does a send to one or more other Programs/Nodes
            - repeat
        """
        counter = 0
        while True:
            yield self.env.process(self.wait())
            yield self.env.process(self.work())
            yield self.env.process(self.send())

            counter = counter + 1
            print '==========================\nProgram {}: end of loop {}\n==========================\n'.format(self.id, counter)

##########################################################################
# Execution
##########################################################################

if __name__ == '__main__':
    s = SimpleSimulation(max_sim_time=100)
    s.run()
{'fraction_non_alphanumeric': 0.24848259630868327, 'fraction_numerical': 0.008794747925182709, 'mean_word_length': 2.7448979591836733, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4076674', 'n_tokens_mistral': 2254, 'n_tokens_neox': 2102, 'n_words': 617}