public
Description: Prototype of the Swarm Distributed Programming Language
Homepage: http://blog.locut.us/category/projects/swarm/
Clone URL: git://github.com/sanity/swarm-proto.git
swarm-proto / src / dpl / Cluster.scala
100644 38 lines (30 sloc) 0.867 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package dpl
 
import scala.collection.mutable._
import scala.collection.immutable._
 
class Cluster(numNodes : Int, storeSize : Int, program : IntMap[Instruction]) {
  val maxImbalance = 0.8
  val nodes = new Array[Node](numNodes)
  
  for (i <- 1 to numNodes) {
    nodes(i-1) = new Node(this, storeSize, program)
  }
  
  def loadBalance(thisNode : Node) : Option[Node] = {
    var smallest : Node = null
    var smallestSz = Integer.MAX_VALUE
    for (n <- nodes) {
      val nSz = n.store.size
      if (nSz < smallestSz) {
        smallest = n
        smallestSz = nSz
      }
    }
    
    if (smallestSz >= storeSize) {
          throw new RuntimeException("Out of space")
    }
    
    val thisSz = thisNode.store.size
    
    if ((thisSz - smallestSz) > (storeSize * maxImbalance)) {
        return Some(smallest)
    } else {
      return None
    }
  }
}