September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,20 @@
|
|||
function runsim(numworkers, runs)
|
||||
for count in 1:runs
|
||||
@sync begin
|
||||
for worker in 1:numworkers
|
||||
@async begin
|
||||
tasktime = rand()
|
||||
sleep(tasktime)
|
||||
println("Worker $worker finished after $tasktime seconds")
|
||||
end
|
||||
end
|
||||
end
|
||||
println("Checkpoint reached for run $count.")
|
||||
end
|
||||
println("Finished all runs.\n")
|
||||
end
|
||||
|
||||
const trials = [[3, 2], [4, 1], [2, 5], [7, 6]]
|
||||
for trial in trials
|
||||
runsim(trial[1], trial[2])
|
||||
end
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
#!/usr/bin/env perl6
|
||||
|
||||
use v6;
|
||||
|
||||
my $TotalWorkers = 3;
|
||||
my $BatchToRun = 3;
|
||||
my @TimeTaken = (5..15); # in seconds
|
||||
|
||||
my $batch_progress = 0;
|
||||
my @batch_lock = map { Semaphore.new(1) } , ^$TotalWorkers;
|
||||
my $lock = Lock.new;
|
||||
|
||||
sub assembly_line ($ID) {
|
||||
my $wait;
|
||||
for ^$BatchToRun -> $j {
|
||||
$wait = @TimeTaken.roll;
|
||||
say "Worker ",$ID," at batch $j will work for ",$wait," seconds ..";
|
||||
sleep($wait);
|
||||
$lock.protect: {
|
||||
my $k = ++$batch_progress;
|
||||
print "Worker ",$ID," is done and update batch $j complete counter ";
|
||||
say "to $k of $TotalWorkers";
|
||||
if ($batch_progress == $TotalWorkers) {
|
||||
say ">>>>> batch $j completed.";
|
||||
$batch_progress = 0; # reset for next batch
|
||||
for @batch_lock { .release }; # and ready for next batch
|
||||
};
|
||||
};
|
||||
|
||||
@batch_lock[$ID].acquire; # for next batch
|
||||
}
|
||||
}
|
||||
|
||||
for ^$TotalWorkers -> $i {
|
||||
Thread.start(
|
||||
sub {
|
||||
@batch_lock[$i].acquire;
|
||||
assembly_line($i);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
-- demo\rosetta\checkpoint_synchronisation.exw
|
||||
constant NPARTS = 3
|
||||
integer workers = 0
|
||||
sequence waiters = {}
|
||||
bool terminate = false
|
||||
|
||||
procedure checkpoint(integer task_id)
|
||||
if length(waiters)+1=NPARTS or terminate then
|
||||
printf(1,"checkpoint\n")
|
||||
for i=1 to length(waiters) do
|
||||
task_schedule(waiters[i],1)
|
||||
end for
|
||||
waiters = {}
|
||||
else
|
||||
waiters &= task_id
|
||||
task_suspend(task_id)
|
||||
task_yield()
|
||||
end if
|
||||
end procedure
|
||||
|
||||
procedure worker(string name)
|
||||
printf(1,"worker %s running\n",{name})
|
||||
while not terminate do
|
||||
printf(1,"worker %s begins part\n",{name})
|
||||
task_delay(rnd())
|
||||
printf(1,"worker %s completes part\n",{name})
|
||||
checkpoint(task_self())
|
||||
if rnd()>0.95 then exit end if
|
||||
task_delay(rnd())
|
||||
end while
|
||||
printf(1,"worker %s leaves\n",{name})
|
||||
workers -= 1
|
||||
end procedure
|
||||
|
||||
string name = "A"
|
||||
|
||||
while get_key()!=#1B do -- (key escape to shut down)
|
||||
if workers<NPARTS then
|
||||
integer task_id = task_create(routine_id("worker"),{name})
|
||||
task_schedule(task_id,1)
|
||||
name[1] += 1
|
||||
workers += 1
|
||||
end if
|
||||
task_yield()
|
||||
end while
|
||||
printf(1,"escape keyed\n")
|
||||
terminate = true
|
||||
while workers>0 do
|
||||
task_yield()
|
||||
end while
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
"""
|
||||
|
||||
Based on https://pymotw.com/3/threading/
|
||||
|
||||
"""
|
||||
|
||||
import threading
|
||||
import time
|
||||
import random
|
||||
|
||||
|
||||
def worker(workernum, barrier):
|
||||
# task 1
|
||||
sleeptime = random.random()
|
||||
print('Starting worker '+str(workernum)+" task 1, sleeptime="+str(sleeptime))
|
||||
time.sleep(sleeptime)
|
||||
print('Exiting worker'+str(workernum))
|
||||
barrier.wait()
|
||||
# task 2
|
||||
sleeptime = random.random()
|
||||
print('Starting worker '+str(workernum)+" task 2, sleeptime="+str(sleeptime))
|
||||
time.sleep(sleeptime)
|
||||
print('Exiting worker'+str(workernum))
|
||||
|
||||
barrier = threading.Barrier(3)
|
||||
|
||||
w1 = threading.Thread(target=worker, args=((1,barrier)))
|
||||
w2 = threading.Thread(target=worker, args=((2,barrier)))
|
||||
w3 = threading.Thread(target=worker, args=((3,barrier)))
|
||||
|
||||
w1.start()
|
||||
w2.start()
|
||||
w3.start()
|
||||
Loading…
Add table
Add a link
Reference in a new issue