Skip to content

Commit

Permalink
Make all sizes of type i64.
Browse files Browse the repository at this point in the history
This has wide-ranging implications for the types of things in the prelude:

* Functions like `replicate` and `iota` now take `i64` arguments.

* The `from_fraction` function now takes `i64`.

* The `to_i32` function has been removed.

Closes #134.
  • Loading branch information
athas committed Oct 5, 2020
1 parent 7fe3bcb commit 644ca2b
Show file tree
Hide file tree
Showing 442 changed files with 2,183 additions and 2,149 deletions.
5 changes: 1 addition & 4 deletions examples/linear_solve.fut
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ let Gauss_Jordan [n][m] (A: [n][m]f32): [n][m]f32 =
Ap
in Ap ++ [irow]) :> [n][m]f32

let concat 'a (m: i32) (a: []a) (b: []a) : [m]a =
a ++ b :> [m]a

let linear_solve [n][m] (A: [n][m]f32) (b: [n]f32): [n]f32 =
-- Pad the matrix with b.
let Ap = map2 (concat (m+1)) A (transpose [b])
let Ap = map2 (concat_to (m+1)) A (transpose [b])
let Ap' = Gauss_Jordan Ap
-- Extract last column.
in Ap'[0:n,m]
Expand Down
6 changes: 3 additions & 3 deletions examples/perceptron.fut
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ let train [d] (w: [d]f32) (x: [d]f32) (y: f32) (eta: f32): [d]f32 =
let main [d][m] (w: [d]f32) (xd: [m][d]f32) (yd: [m]f32) (limit: i32) (eta: f32): (i32, [d]f32, f32) =
let (w,i) = loop (w, i) = (w, 0) while i < limit && !(checkList w xd yd) do
-- Find data for this iteration.
let x = xd[i%m]
let y = yd[i%m]
let x = xd[i%i32.i64 m]
let y = yd[i%i32.i64 m]
in (train w x y eta, i+1)
in (i, w, accuracy w xd yd / r32(m))
in (i, w, accuracy w xd yd / f32.i64(m))
8 changes: 4 additions & 4 deletions examples/quickselect.fut
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
--
-- ==
-- tags { no_csharp }
-- input { [1] 0 } output { 1 }
-- input { [4, -8, 2, 2, 0, 0, 5, 9, -6, 2] 7 } output { 4 }
-- input { [1] 0i64 } output { 1 }
-- input { [4, -8, 2, 2, 0, 0, 5, 9, -6, 2] 7i64 } output { 4 }

let quickselect [n] (s: [n]i32) (k:i32): i32 =
let quickselect [n] (s: [n]i32) (k:i64): i32 =
let (_, s) =
loop (k, s) while length s > 1 do
let pivot = s[length s/2]
Expand All @@ -20,4 +20,4 @@ let quickselect [n] (s: [n]i32) (k:i32): i32 =
else (0,[pivot])
in s[0]

let main (s:[]i32) (k:i32) : i32 = quickselect s k
let main (s:[]i32) (k:i64) : i32 = quickselect s k
4 changes: 2 additions & 2 deletions examples/rosettacode/100doors.fut
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
-- the doors we care about, while still remaining parallel. 0-indexes the doors.
--
-- ==
-- input { 10 }
-- input { 10i64 }
-- output { [false, true, false, false, true, false, false, false, false, true] }

let main(n: i32): [n]bool =
let main(n: i64): [n]bool =
loop is_open = replicate n false for i < n do
let js = map (*i+1) (iota n)
let flips = map (\j ->
Expand Down
9 changes: 5 additions & 4 deletions examples/rosettacode/amicablepairs.fut
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-- requires a giant amount of memory. Oh well.
--
-- ==
-- compiled input { 300 }
-- compiled input { 300i64 }
-- output { [[220i32, 284i32]] }

let divisors(n: i32): []i32 =
Expand All @@ -13,13 +13,14 @@ let divisors(n: i32): []i32 =
let amicable((n: i32, nd: i32), (m: i32, md: i32)): bool =
n < m && nd == m && md == n

let getPair [upper] (divs: [upper](i32, i32)) (flat_i: i32): ((i32,i32), (i32,i32)) =
let getPair [upper] (divs: [upper](i32, i32)) (flat_i: i64): ((i32,i32), (i32,i32)) =
let i = flat_i / upper
let j = flat_i % upper
in (divs[i], divs[j])

let main(upper: i32): [][2]i32 =
let main(upper: i64): [][2]i32 =
let range = map (1+) (iota upper)
let divs = zip range (map (\n -> reduce (+) 0 (divisors n)) range)
let divs = zip (map i32.i64 range)
(map (\n -> reduce (+) 0 (divisors (i32.i64 n))) range)
let amicable = filter amicable (map (getPair divs) (iota (upper*upper)))
in map (\((x,_),(y,_)) -> [x, y]) amicable
2 changes: 1 addition & 1 deletion examples/rosettacode/arithmetic_means.fut
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

-- Divide first to improve numerical behaviour.
let main [n] (as: [n]f64): f64 =
reduce (+) 0f64 (map (/r64(n)) as)
reduce (+) 0f64 (map (/f64.i64(n)) as)
4 changes: 2 additions & 2 deletions examples/rosettacode/binarysearch.fut
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
--
-- ==
-- input { [1,2,3,4,5,6,8,9] 2 }
-- output { 1 }
-- output { 1i64 }

let main [n] (as: [n]i32) (value: i32): i32 =
let main [n] (as: [n]i32) (value: i32): i64 =
let low = 0
let high = n-1
let (low, _) = loop ((low,high)) while low <= high do
Expand Down
8 changes: 4 additions & 4 deletions examples/rosettacode/count_in_octal.fut
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
-- look like octal numbers when printed in decimal.
--
-- ==
-- input { 20 }
-- input { 20i64 }
-- output { [0i32, 1i32, 2i32, 3i32, 4i32, 5i32, 6i32, 7i32, 10i32, 11i32,
-- 12i32, 13i32, 14i32, 15i32, 16i32, 17i32, 20i32, 21i32, 22i32, 23i32] }

let octal(x: i32): i32 =
let (out,_,_) = loop (out,mult,x) = (0,1,x) while x > 0 do
let octal(x: i64): i32 =
let (out,_,_) = loop (out,mult,x) = (0,1,i32.i64 x) while x > 0 do
let digit = x % 8
let out = out + digit * mult
in (out, mult * 10, x / 8)
in out

let main(n: i32): [n]i32 =
let main(n: i64): [n]i32 =
map octal (iota n)
4 changes: 2 additions & 2 deletions examples/rosettacode/eulermethod.fut
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ let cooling(_time: f64) (temperature: f64): f64 =
-0.07 * (temperature-20.0)

let main(t0: f64) (a: f64) (b: f64) (h: f64): []f64 =
let steps = i32.f64 ((b-a)/h)
let steps = i64.f64 ((b-a)/h)
let temps = replicate steps 0.0
let (_,temps) = loop (t,temps)=(t0,temps) for i < steps do
let x = a + f64.i32 i * h
let x = a + f64.i64 i * h
let temps[i] = f64.abs(t-analytic t0 x)
in (t + h * cooling x t,
temps)
Expand Down
6 changes: 3 additions & 3 deletions examples/rosettacode/for.fut
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
-- Futhark does not have I/O, so this program simply counts in the
-- inner loop.
-- ==
-- input { 10 }
-- output { [0i32, 1i32, 3i32, 6i32, 10i32, 15i32, 21i32, 28i32, 36i32, 45i32] }
-- input { 10i64 }
-- output { [0i64, 1i64, 3i64, 6i64, 10i64, 15i64, 21i64, 28i64, 36i64, 45i64] }

let main(n: i32): [n]i32 =
let main(n: i64): [n]i64 =
loop a = replicate n 0 for i < n do
(let a[i] = loop s = 0 for j < i+1 do s + j
in a)
3 changes: 2 additions & 1 deletion examples/rosettacode/hailstone.fut
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ let max (x: i32) (y: i32): i32 = if x < y then y else x

let main (x: i32) (n: i32): ([]i32, i32) =
(hailstone_seq x,
reduce max 0 (map hailstone_len (map (1+) (iota (n-1)))))
reduce max 0 (map hailstone_len
(map (1+) (map i32.i64 (iota (i64.i32 n-1))))))
4 changes: 2 additions & 2 deletions examples/rosettacode/integer_sequence.fut
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
-- accepts an input indicating how many integers to generate.
--
-- ==
-- input { 10 } output { [0,1,2,3,4,5,6,7,8,9] }
-- input { 10i64 } output { [0i64,1i64,2i64,3i64,4i64,5i64,6i64,7i64,8i64,9i64] }

let main(n: i32): [n]i32 = iota n
let main(n: i64): [n]i64 = iota n
12 changes: 6 additions & 6 deletions examples/rosettacode/mandelbrot.fut
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Computes escapes for each pixel, but not the colour.
-- ==
-- compiled input { 10 10 100 0.0f32 0.0f32 1.0f32 1.0f32 }
-- compiled input { 10i64 10i64 100 0.0f32 0.0f32 1.0f32 1.0f32 }
-- output {
-- [[100i32, 100i32, 100i32, 100i32, 100i32, 100i32, 100i32, 12i32, 17i32, 7i32],
-- [100i32, 100i32, 100i32, 100i32, 100i32, 100i32, 100i32, 8i32, 5i32, 4i32],
Expand Down Expand Up @@ -37,13 +37,13 @@ let divergence(depth: i32, c0: complex): i32 =
(addComplex(c0, multComplex(c, c)),
i + 1)).1

let main (screenX: i32) (screenY: i32) (depth: i32) (xmin: f32) (ymin: f32) (xmax: f32) (ymax: f32): [screenX][screenY]i32 =
let main (screenX: i64) (screenY: i64) (depth: i32) (xmin: f32) (ymin: f32) (xmax: f32) (ymax: f32): [screenX][screenY]i32 =
let sizex = xmax - xmin
let sizey = ymax - ymin
in map (\(x: i32): [screenY]i32 ->
map (\(y: i32): i32 ->
let c0 = (xmin + (r32(x) * sizex) / r32(screenX),
ymin + (r32(y) * sizey) / r32(screenY))
in map (\x: [screenY]i32 ->
map (\y: i32 ->
let c0 = (xmin + (f32.i64(x) * sizex) / f32.i64(screenX),
ymin + (f32.i64(y) * sizey) / f32.i64(screenY))
in divergence(depth, c0))
(iota screenY))
(iota screenX)
2 changes: 1 addition & 1 deletion examples/rosettacode/md5.fut
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ let main [n] (ms: [n]u8): [16]u8 =
let ms_padded = ms ++
bytes 0x80u32 ++
replicate (padding-12) 0x0u8 ++
bytes (u32.i32(n*8)) ++
bytes (u32.i64(n*8)) ++
[0u8,0u8,0u8,0u8]
let (a,b,c,d) = md5 (map unbytes_block (unflatten (n_padded / 64) 64 ms_padded))
in flatten (map bytes [a,b,c,d]) :> [16]u8
16 changes: 8 additions & 8 deletions examples/rosettacode/monte_carlo_methods.fut
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ let testBit(n: i32, ind: i32): bool =

let xorInds [num_bits] (n: i32) (dir_vs: [num_bits]i32): i32 =
let reldv_vals = map2 (\ dv i ->
if testBit(grayCode n,i)
if testBit(grayCode n,i32.i64 i)
then dv else 0)
dir_vs (iota num_bits)
in reduce (^) 0 reldv_vals

let sobolIndI [m] [num_bits] (dir_vs: [m][num_bits]i32, n: i32): [m]i32 =
map (xorInds n) dir_vs
let sobolIndI [m] [num_bits] (dir_vs: [m][num_bits]i32, n: i64): [m]i32 =
map (xorInds (i32.i64 n)) dir_vs

let sobolIndR [m] [num_bits] (dir_vs: [m][num_bits]i32) (n: i32 ): [m]f32 =
let divisor = 2.0 ** r32(num_bits)
let sobolIndR [m] [num_bits] (dir_vs: [m][num_bits]i32) (n: i64): [m]f32 =
let divisor = 2.0 ** f32.i64(num_bits)
let arri = sobolIndI( dir_vs, n )
in map (\x -> r32(x) / divisor) arri
in map (\x -> f32.i32 x / divisor) arri

let main(n: i32): f32 =
let rand_nums = map (sobolIndR (dirvcts())) (iota n)
let rand_nums = map (sobolIndR (dirvcts())) (iota (i64.i32 n))
let dists = map (\xy ->
let (x,y) = (xy[0],xy[1]) in f32.sqrt(x*x + y*y))
rand_nums

let bs = map (\d -> if d <= 1.0f32 then 1 else 0) dists

let inside = reduce (+) 0 bs
in 4.0f32*r32(inside)/r32(n)
in 4.0f32*f32.i64(inside)/f32.i32(n)
8 changes: 4 additions & 4 deletions examples/rosettacode/pythagorean_means.fut
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

-- Divide first to improve numerical behaviour.
let arithmetic_mean [n] (as: [n]f64): f64 =
reduce (+) 0.0 (map (/r64(n)) as)
reduce (+) 0.0 (map (/f64.i64(n)) as)

let geometric_mean [n] (as: [n]f64): f64 =
reduce (*) 1.0 (map (**(1.0/r64(n))) as)
reduce (*) 1.0 (map (**(1.0/f64.i64(n))) as)

let harmonic_mean [n] (as: [n]f64): f64 =
r64(n) / reduce (+) 0.0 (map (1.0/) as)
f64.i64(n) / reduce (+) 0.0 (map (1.0/) as)

let main(as: []f64): (f64,f64,f64) =
(arithmetic_mean as,
geometric_mean as,
harmonic_mean as)
harmonic_mean as)
2 changes: 1 addition & 1 deletion examples/rosettacode/rms.fut
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
-- output { 1.936f64 }

let main [n] (as: [n]f64): f64 =
f64.sqrt ((reduce (+) 0.0 (map (**2.0) as)) / r64 n)
f64.sqrt ((reduce (+) 0.0 (map (**2.0) as)) / f64.i64 n)
2 changes: 1 addition & 1 deletion futhark-benchmarks
6 changes: 3 additions & 3 deletions libtests/c/test_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ int main() {

int err;

struct futhark_i32_1d *arr;
err = futhark_entry_main(ctx, &arr, alloc_per_run/4);
struct futhark_i64_1d *arr;
err = futhark_entry_main(ctx, &arr, alloc_per_run/8);
assert(err == 0);

err = futhark_free_i32_1d(ctx, arr);
err = futhark_free_i64_1d(ctx, arr);
assert(err == 0);

futhark_context_free(ctx);
Expand Down
32 changes: 16 additions & 16 deletions prelude/array.fut
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ let tail [n] 't (x: [n]t) = x[1:]
let init [n] 't (x: [n]t) = x[0:n-1]

-- | Take some number of elements from the head of the array.
let take [n] 't (i: i32) (x: [n]t): [i]t = x[0:i]
let take [n] 't (i: i64) (x: [n]t): [i]t = x[0:i]

-- | Remove some number of elements from the head of the array.
let drop [n] 't (i: i32) (x: [n]t) = x[i:]
let drop [n] 't (i: i64) (x: [n]t) = x[i:]

-- | Split an array at a given position.
let split [n] 't (i: i32) (xs: [n]t): ([i]t, []t) =
let split [n] 't (i: i64) (xs: [n]t): ([i]t, []t) =
(xs[:i] :> [i]t, xs[i:])

-- | Return the elements of the array in reverse order.
Expand All @@ -46,28 +46,28 @@ let concat [n] [m] 't (xs: [n]t) (ys: [m]t): *[]t = xs ++ ys
-- | Concatenation where the result has a predetermined size. If the
-- provided size is wrong, the function will fail with a run-time
-- error.
let concat_to [n] [m] 't (k: i32) (xs: [n]t) (ys: [m]t): *[k]t = xs ++ ys :> [k]t
let concat_to [n] [m] 't (k: i64) (xs: [n]t) (ys: [m]t): *[k]t = xs ++ ys :> [k]t

-- | Rotate an array some number of elements to the left. A negative
-- rotation amount is also supported.
--
-- For example, if `b==rotate r a`, then `b[x+r] = a[x]`.
let rotate [n] 't (r: i32) (xs: [n]t): [n]t = intrinsics.rotate (r, xs) :> [n]t
let rotate [n] 't (r: i64) (xs: [n]t): [n]t = intrinsics.rotate (r, xs) :> [n]t

-- | Construct an array of consecutive integers of the given length,
-- starting at 0.
let iota (n: i32): *[n]i32 =
let iota (n: i64): *[n]i64 =
0..1..<n

-- | Construct an array comprising valid indexes into some other
-- array, starting at 0.
let indices [n] 't (_: [n]t) : *[n]i32 =
let indices [n] 't (_: [n]t) : *[n]i64 =
iota n

-- | Construct an array of the given length containing the given
-- value.
let replicate 't (n: i32) (x: t): *[n]t =
map (\_ -> x) (iota n)
let replicate 't (n: i64) (x: t): *[n]t =
map (const x) (iota n)

-- | Copy a value. The result will not alias anything.
let copy 't (a: t): *t =
Expand All @@ -79,7 +79,7 @@ let flatten [n][m] 't (xs: [n][m]t): []t =

-- | Like `flatten`@term, but where the final size is known. Fails at
-- runtime if the provided size is wrong.
let flatten_to [n][m] 't (l: i32) (xs: [n][m]t): [l]t =
let flatten_to [n][m] 't (l: i64) (xs: [n][m]t): [l]t =
flatten xs :> [l]t

-- | Combines the outer three dimensions of an array.
Expand All @@ -91,15 +91,15 @@ let flatten_4d [n][m][l][k] 't (xs: [n][m][l][k]t): []t =
flatten (flatten_3d xs)

-- | Splits the outer dimension of an array in two.
let unflatten [p] 't (n: i32) (m: i32) (xs: [p]t): [n][m]t =
let unflatten [p] 't (n: i64) (m: i64) (xs: [p]t): [n][m]t =
intrinsics.unflatten (n, m, xs) :> [n][m]t

-- | Splits the outer dimension of an array in three.
let unflatten_3d [p] 't (n: i32) (m: i32) (l: i32) (xs: [p]t): [n][m][l]t =
let unflatten_3d [p] 't (n: i64) (m: i64) (l: i64) (xs: [p]t): [n][m][l]t =
unflatten n m (unflatten (n*m) l xs)

-- | Splits the outer dimension of an array in four.
let unflatten_4d [p] 't (n: i32) (m: i32) (l: i32) (k: i32) (xs: [p]t): [n][m][l][k]t =
let unflatten_4d [p] 't (n: i64) (m: i64) (l: i64) (k: i64) (xs: [p]t): [n][m][l][k]t =
unflatten n m (unflatten_3d (n*m) l k xs)

let transpose [n] [m] 't (a: [n][m]t): [m][n]t =
Expand All @@ -122,13 +122,13 @@ let foldr [n] 'a 'b (f: b -> a -> a) (acc: a) (bs: [n]b): a =
foldl (flip f) acc (reverse bs)

-- | Create a value for each point in a one-dimensional index space.
let tabulate 'a (n: i32) (f: i32 -> a): *[n]a =
let tabulate 'a (n: i64) (f: i64 -> a): *[n]a =
map1 f (iota n)

-- | Create a value for each point in a two-dimensional index space.
let tabulate_2d 'a (n: i32) (m: i32) (f: i32 -> i32 -> a): *[n][m]a =
let tabulate_2d 'a (n: i64) (m: i64) (f: i64 -> i64 -> a): *[n][m]a =
map1 (f >-> tabulate m) (iota n)

-- | Create a value for each point in a three-dimensional index space.
let tabulate_3d 'a (n: i32) (m: i32) (o: i32) (f: i32 -> i32 -> i32 -> a): *[n][m][o]a =
let tabulate_3d 'a (n: i64) (m: i64) (o: i64) (f: i64 -> i64 -> i64 -> a): *[n][m][o]a =
map1 (f >-> tabulate_2d m o) (iota n)
Loading

0 comments on commit 644ca2b

Please sign in to comment.