SHARE
TWEET

Untitled

a guest Aug 20th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Distributed
  2. global numWorkers = 5
  3. addprocs(numWorkers)
  4.  
  5. @everywhere function workerInit(_size, firstLeader)
  6.         global workload = round(rand(), digits=1)
  7.         global workloads = Vector{Float64}(undef, _size)
  8.         global participant = false
  9.         global currLeader = firstLeader
  10.         global size = _size
  11.         work()
  12.     end
  13.  
  14. @everywhere function dataFromWorker()
  15.         myid_ = myid()
  16.             println("Workloads: $(workloads)")
  17.     end
  18.  
  19. @everywhere function work()
  20.         while true
  21.             if !participant
  22.                 sendWorkload()
  23.             end
  24.             sleep(2)
  25.             dataFromWorker()
  26.             checkPossibleElection()
  27.         end
  28.     end
  29.  
  30. @everywhere function sendWorkload()
  31.         global workload =  round(rand(), digits=1)
  32.         id = myid()
  33.         for w in workers()
  34.             @spawnat(w, updateWorkload(id, workload))
  35.         end
  36.     end
  37.  
  38. @everywhere function updateWorkload(id, newWorkload)
  39.         global workloads[id-1] = newWorkload
  40.     end
  41.  
  42. @everywhere function checkPossibleElection()
  43.         id = myid()
  44.         if !participant && workloads[currLeader-1] >= 0.8 && workloads[id-1] < 0.8
  45.             global participant = true
  46.             local next = nextWorker()
  47.             id = myid()
  48.             @spawnat next propagateElection(workloads[id-1], id, id)
  49.         end
  50.     end
  51.  
  52. @everywhere function propagateElection(best, bestId, starterId)
  53.         id = myid()
  54.         next = nextWorker()
  55.         if id != starterId
  56.             global participant = true
  57.             if (best > workloads[id-1]) || (best == workloads[id-1] && id > bestId)
  58.                 @spawnat next propagateElection(workloads[id-1], id, starterId)
  59.             else
  60.                 @spawnat next propagateElection(best, bestId, starterId)
  61.             end
  62.         else
  63.             for w in workers()
  64.                 @spawnat w updateLeader(bestId)
  65.             end
  66.         end
  67.     end
  68.  
  69. @everywhere function updateLeader(newLeader)
  70.         if participant
  71.             id = myid()
  72.             global participant = false
  73.             if id == newLeader
  74.                 println("New leader elected! Worker $(id)")
  75.             end
  76.             global currLeader = newLeader
  77.         end
  78.     end
  79.  
  80. @everywhere function nextWorker()
  81.         local id = myid()
  82.         id = ((id-1)%size)+2
  83.         return id
  84.     end
  85.  
  86. @sync for w in workers()
  87.     @spawn workerInit(numWorkers, 2)
  88. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top