-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from granule-project/fractional
Fractional permissions for mutable and immutable borrowing
- Loading branch information
Showing
45 changed files
with
1,068 additions
and
158 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Parallel | ||
import Prelude | ||
import Vec | ||
|
||
--- Convert an indexed natural number to an untyped int | ||
natToInt' | ||
: forall {n : Nat} | ||
. N n -> Int | ||
natToInt' Z = 0; | ||
natToInt' (S m) = 1 + natToInt' m | ||
|
||
toFloatArray : forall {n : Nat} . Vec n Float -> exists {id : Name} . *(FloatArray id) | ||
toFloatArray v = | ||
let (n', v) = length' v | ||
in unpack <id, arr> = newFloatArray (natToInt' n') | ||
in pack <id, (toFloatArrayAux arr [0] v)> as exists {id : Name} . *(FloatArray id) | ||
|
||
toFloatArrayAux : forall {n : Nat, id : Name} . *(FloatArray id) -> Int [n] -> Vec n Float -> *(FloatArray id) | ||
toFloatArrayAux a [n] Nil = a; | ||
toFloatArrayAux a [n] (Cons x xs) = | ||
toFloatArrayAux (writeFloatArray a n x) [n + 1] xs | ||
|
||
sumFromTo : forall {id : Name, f : Fraction} . & f (FloatArray id) -> !Int -> !Int -> (Float, & f (FloatArray id)) | ||
sumFromTo array [i] [n] = | ||
if i == n | ||
then (0.0, array) | ||
else | ||
let (x, a) = readFloatArray array i; | ||
(y, arr) = sumFromTo a [i+1] [n] | ||
in (x + y, arr) | ||
|
||
-- A reference to a droppable value can be written to without violating linearity | ||
writeRef : forall {a : Type, id : Name} . {Dropable a} => a -> & 1 (Ref id a) -> & 1 (Ref id a) | ||
writeRef x r = let | ||
(y, r') = swapRef r x; | ||
() = drop@a y in r' | ||
|
||
parSum : forall {id id' : Name} . *(FloatArray id) -> *(Ref id' Float) -> *(Ref id' Float, FloatArray id) | ||
parSum array ref = let | ||
([n], array) : (!Int, *(FloatArray id)) = lengthFloatArray array; | ||
compIn = borrowPull (ref, array) | ||
in flip withBorrow compIn (\compIn -> | ||
|
||
let (ref, array) = borrowPush compIn; | ||
(array1, array2) = split array; | ||
|
||
-- Compute in parallel | ||
((x, array1), (y, array2)) = | ||
par (\() -> sumFromTo array1 [0] [div n 2]) | ||
(\() -> sumFromTo array2 [div n 2] [n]); | ||
|
||
-- Update the reference | ||
ref' = writeRef ((x : Float) + y) ref; | ||
compOut = borrowPull (ref', join (array1, array2)) | ||
|
||
in compOut) | ||
|
||
main : Float | ||
main = | ||
unpack <id , arr> = toFloatArray (Cons 10.0 (Cons 20.0 (Cons 30.0 (Cons 40.0 Nil)))) in | ||
unpack <id', ref> = newRef 0.0 in | ||
let | ||
(result, array) = borrowPush (parSum arr ref); | ||
() = deleteFloatArray array | ||
in freezeRef result |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
example : () | ||
example = unpack <id , a> = newFloatArray 3 in | ||
clone (share a) as x in | ||
unpack <id' , a'> = x in (deleteFloatArray a') |
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
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
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
Oops, something went wrong.