//
//	SIMULATION INTERACTIONS
//	(mostly dealing with importing initial values/setting up, or with running the simulation (timers/graphics)
//	I would have preferred to put these outside in Node Simulator, but unfortunately this code is master-specific
//
    public void setup()
    {           // (invoked remotely to get things started)
        node          = GetComponent <NodeSimulator>();
        updateCounter = -1;

        if (node.nodeID == 0)                   // node 0 is the starting leader, the one from which the user invoked the map-reduce operation
        {
            updateCounter = 0;
            becomeLeader();
        }
        else
        {
            becomeFollower();
        }
        masterCount = node.masterCount;
        workerCount = node.workerCount;

        mapTasks = new TaskData[node.mapCount];
        for (int i = 0; i < mapTasks.Length; i++)
        {
            mapTasks[i] = new TaskData();
        }
        reduceTasks = new TaskData[node.reduceCount];
        for (int i = 0; i < reduceTasks.Length; i++)
        {
            reduceTasks[i] = new TaskData();
        }
    }
Ejemplo n.º 2
0
        public void SimulateNodesUpdatesNodeDictionary()
        {
            //arrange
            var uniformRandom     = .01D;
            var price             = 2D;
            var distributionIndex = 0;
            var uniformRandoms    = new[] { uniformRandom };

            var cdfProbability   = 1D;
            var cdfProbabilities = new[] { cdfProbability };
            var distribution     = new Mock <IDistribution>();

            distribution.Setup(d => d.CdfProbabilities).Returns(cdfProbabilities);
            distribution.Setup(d => d.GetPrice(It.Is <double>(rand => rand == uniformRandom), It.Is <int>(ix => ix == distributionIndex))).Returns(price);
            var distributions = new[] { distribution.Object };

            var parentId = 1;
            var parent   = new Mock <INode>();

            parent.Setup(p => p.Id).Returns(parentId);
            parent.Setup(p => p.Distributions).Returns(distributions);

            var childId = 2;
            var child   = new Mock <INode>();

            child.Setup(c => c.Id).Returns(childId);
            child.Setup(c => c.Parent).Returns(parent.Object);
            child.Setup(c => c.Distributions).Returns(distributions);

            IDictionary <int, INode> nodeDictionary = new SortedDictionary <int, INode> {
                { parentId, parent.Object }, { childId, child.Object }
            };

            var repository = new Mock <IUniformRandomRepository>();

            repository.Setup(r => r.GetUniformRandoms()).Returns(uniformRandoms);

            var simulator = new NodeSimulator(repository.Object);

            //act
            var results = simulator.SimulateNodes(nodeDictionary);

            //assert
            Assert.AreEqual(results[parentId][0], price);
            Assert.AreEqual(results[childId][0], price);
        }
Ejemplo n.º 3
0
/*	THIS WAS AN ATTEMPT TO BREAK DOWN THE SORTING JOB INTO A BUNCH OF LITTLE PIECES SO THE WHOLE SIMULATOR DOESN"T HAVE TO WAIT FOR ITS COMPLETION
 *      (NEGATING THE "HITCHING" ISSUE)
 *      UNFORTUNATELY EVEN WITH ONLY 5 WORDS PER FRAME IT WAS TOO CHOPPY AND ONLY 5 WORDS/FRAME WOULD TAKE WAY TOO LONG
 *
 *      string[] words;
 *      int sorted = 0;
 *
 *      void doReducing()
 *      {	// ATTEMPT 2: A helper method for actually doing file reducing
 *
 *              string rawText = "";
 *              for (int i = 0; i < node.mapCount; i++)
 *              {
 *                      rawText += File.ReadAllText(node.directory + "\\intermediate" + taskNumber + "-" + i + ".txt");
 *              }
 *              // (split the text into words)
 *              words = rawText.Split('\n');
 *              simulatedTaskTime = ((words.Length/300.0f)*UnityEngine.Random.Range(0.9f, 1.1f))/1000f;	// now that we have more information, we can give a better simulated task time of 300 words per millisecond (slower than mapping)
 *
 *              // (set up the sort)
 *              sorted = 0;
 *
 *      }
 *
 *      void doReducing2()
 *      {
 *              if (taskCompleted || !taskType.Equals("REDUCE"))
 *                      return;
 *
 *              if (sorted >= words.Length)
 *              {
 *                      // We're done with the sorting! Move on to the last part
 *
 *                      // (count the words)
 *                      int[] counts = new int[words.Length];
 *                      for (int i = 0; i < counts.Length; i++)
 *                              counts[i] = 1;
 *                      for (int i = 0; i < words.Length; i++)
 *                      {
 *                              if (counts[i] == 0)
 *                                      continue;
 *
 *                              for (int j = i+1; j < words.Length && words[j] == words[i]; j++)
 *                              {
 *                                      counts[i] += 1;
 *                                      counts[j] = 0;
 *                              }
 *                      }
 *
 *                      // Output to the file
 *                      taskFiles = new string[1];	// (only one output file, but our format is an array, so a length 1 array it is)
 *                      taskFiles[0] = node.directory + "\\" + "output"+taskNumber+".txt";
 *                      StreamWriter writeFile = new StreamWriter(File.Create(taskFiles[0]));
 *
 *                      for (int i = 0; i < words.Length; i++)
 *                      {
 *                              if (counts[i] > 0)
 *                              {
 *                                      string thisWord = words[i].Substring(0,words[i].Length-1);
 *                                      writeFile.WriteLine(thisWord + " " + counts[i]);
 *                              }
 *                      }
 *
 *                      taskCompleted = true;
 *                      return;
 *              }
 *
 *              Debug.Log("Sorting 5!");
 *              // otherwise, we're NOT done with the sorting
 *              // do X words per tick and then record our progress and stop
 *              for (int i = 0; i < 5 && sorted < words.Length; i++)
 *              {
 *                      int minIndex = sorted;
 *                      for (int j = sorted+1; j < words.Length; j++)
 *                      {
 *                              if (string.Compare(words[j], words[minIndex]) < 0)
 *                                      minIndex = j;
 *                      }
 *
 *                      if (minIndex != sorted)
 *                      {
 *                              string tempword = words[minIndex];
 *                              words[minIndex] = words[sorted];
 *                              words[sorted] = tempword;
 *                      }
 *
 *                      sorted += 1;
 *              }
 *
 *      }*/



//
//	SIMULATION INTERACTIONS
//	(mostly dealing with importing initial values/setting up, or with running the simulation (timers/graphics)
//	I would have preferred to put these outside in Node Simulator, but unfortunately this code is worker-specific
//

    public void setup()
    {
        node              = GetComponent <NodeSimulator>();
        taskTimer         = 0;
        simulatedTaskTime = UnityEngine.Random.Range(0.001f, 0.030f);           // initial task request after 1ms to 30ms
    }