Created
April 2, 2021 04:40
-
-
Save ConDai/bdfdfeaa83f00cb34bc3bf2e39701557 to your computer and use it in GitHub Desktop.
Chapel overloaded operator syntax error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Channel { | |
use semaphore; | |
record chan { | |
type eltType; | |
var size: int; | |
var buffer: [0..size-1] eltType; | |
var full: semaphore.Semaphore; | |
var empty: semaphore.Semaphore; | |
var sendX: int = 0; | |
var recvX: int = 0; | |
var mutex$: sync bool = true; | |
proc init(type eltType, size: int){ | |
this.eltType = eltType; | |
this.size = size; | |
this.full = new semaphore.Semaphore(0); | |
this.empty = new semaphore.Semaphore(size); | |
} | |
proc send(el:eltType) { | |
empty.wait(); | |
mutex$.readFE(); | |
buffer[recvX] = el; | |
recvX = (recvX + 1) % size; | |
mutex$.writeEF(true); | |
full.signal(); | |
} | |
proc read() { | |
full.wait(); | |
mutex$.readFE(); | |
var res:t = buffer[sendX]; | |
sendX = (sendX + 1) % size; | |
mutex$.writeEF(true); | |
empty.signal(); | |
return res; | |
} | |
proc << (ref val, ref ch:chan){ | |
val = ch.read(); | |
} | |
proc >> (const val, ref ch:chan){ | |
ch.write(val); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Channel; | |
proc worker(ref ch: chan){ | |
for i in 1..10 do{ | |
writeln("Will send ", i); | |
i >> ch; | |
} | |
} | |
proc consumer(ref ch: chan){ | |
for i in 1..10 do | |
writeln("read ", ch.read()); | |
} | |
proc main(){ | |
var ch = new chan(int, 10); | |
coforall i in 1..2 with (ref ch) { | |
if i%2 == 0 then | |
consumer(ch); | |
else | |
worker(ch); | |
} | |
writeln("Finished!"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment