Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Atomic-updates/Zkl/atomic-updates-1.zkl
Normal file
29
Task/Atomic-updates/Zkl/atomic-updates-1.zkl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
class B{
|
||||
const N=10;
|
||||
var [const]
|
||||
buckets=(1).pump(N,List).copy(), //(1,2,3...)
|
||||
lock=Atomic.Lock(), cnt=Atomic.Int();
|
||||
fcn init{ "Initial sum: ".println(values().sum()); }
|
||||
fcn transferArb{ // transfer arbitary amount from 1 bucket to another
|
||||
b1:=(0).random(N); b2:=(0).random(N);
|
||||
critical(lock){
|
||||
t:=(0).random(buckets[b1]);
|
||||
buckets[b1]=buckets[b1]-t; buckets[b2]=buckets[b2]+t;
|
||||
}
|
||||
cnt.inc();
|
||||
}
|
||||
fcn transferEq{ // try to make two buckets equal
|
||||
b1:=(0).random(N); b2:=(0).random(N);
|
||||
critical(lock){
|
||||
v1:=buckets[b1]; v2:=buckets[b2];
|
||||
t:=(v1-v2).abs()/2;
|
||||
if (v1<v2) t = -t;
|
||||
buckets[b1]=v1-t; buckets[b2]=v2+t;
|
||||
}
|
||||
cnt.inc();
|
||||
}
|
||||
fcn values{ critical(lock){buckets.copy()} }
|
||||
}
|
||||
|
||||
fcn threadA(b){ while(1) { b.transferArb(); } }
|
||||
fcn threadE(b){ while(1) { b.transferEq(); } }
|
||||
9
Task/Atomic-updates/Zkl/atomic-updates-2.zkl
Normal file
9
Task/Atomic-updates/Zkl/atomic-updates-2.zkl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
b:=B();
|
||||
do(10){ threadA.launch(b); } do(10){ threadE.launch(b); }
|
||||
|
||||
while(1){
|
||||
v:=b.values();
|
||||
v.println("-->",v.sum()," ", b.cnt.value," transfers ",
|
||||
vm.numThreads," threads");
|
||||
Atomic.sleep(2.5);
|
||||
}
|
||||
30
Task/Atomic-updates/Zkl/atomic-updates-3.zkl
Normal file
30
Task/Atomic-updates/Zkl/atomic-updates-3.zkl
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
class C{
|
||||
const N=10;
|
||||
var [const]
|
||||
buckets=(1).pump(N,List).copy(), //(1,2,3...)
|
||||
pipe = Thread.Pipe(), cnt=Atomic.Int();
|
||||
fcn init{
|
||||
pipe.write(buckets);
|
||||
"Initial sum: ".println(values().sum());
|
||||
}
|
||||
fcn transferArb{ // transfer arbitary amount from 1 bucket to another
|
||||
b1:=(0).random(N); b2:=(0).random(N);
|
||||
v:=pipe.read();
|
||||
t:=(0).random(v[b1]); v[b1]=v[b1]-t; v[b2]=v[b2]+t;
|
||||
pipe.write(v);
|
||||
cnt.inc();
|
||||
}
|
||||
fcn transferEq{ // try to make two buckets equal
|
||||
b1:=(0).random(N); b2:=(0).random(N);
|
||||
v:=pipe.read();
|
||||
v1:=v[b1]; v2:=v[b2]; t:=(v1-v2).abs()/2;
|
||||
if (v1<v2) t = -t;
|
||||
v[b1]=v1-t; v[b2]=v2+t;
|
||||
pipe.write(v);
|
||||
cnt.inc();
|
||||
}
|
||||
fcn values{
|
||||
v:=pipe.read(); v2:=v.copy(); pipe.write(v);
|
||||
v2;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue