NB. =========================================================================== NB. Futhark-J Bridge Demonstration NB. =========================================================================== NB. NB. This script demonstrates the Futhark-J bridge with clean syntax, NB. comparing Futhark operations with native J equivalents. NB. NB. Run with: load 'futhark_demo.ijs' NB. =========================================================================== load '/path/to/futhark.ijs' NB. =========================================================================== NB. Define Futhark Operations NB. =========================================================================== echo 'Compiling Futhark operations...' NB. Array Transformations f_double =: 'entry f (xs: []f64) : []f64 = map (*2) xs' fut f_square =: 'entry f (xs: []f64) : []f64 = map (\x -> x * x) xs' fut f_cube =: 'entry f (xs: []f64) : []f64 = map (\x -> x * x * x) xs' fut NB. Scalar Reductions f_sum =: 'entry f (xs: []f64) : f64 = reduce (+) 0 xs' fut f_product =: 'entry f (xs: []f64) : f64 = reduce (*) 1 xs' fut f_min =: 'entry f (xs: []f64) : f64 = reduce f64.min f64.highest xs' fut f_max =: 'entry f (xs: []f64) : f64 = reduce f64.max f64.lowest xs' fut f_mean =: 'entry f (xs: []f64) : f64 = reduce (+) 0 xs / f64.i64 (length xs)' fut NB. Scan Operations f_cumsum =: 'entry f (xs: []f64) : []f64 = scan (+) 0 xs' fut f_runmax =: 'entry f (xs: []f64) : []f64 = scan f64.max f64.lowest xs' fut NB. Statistical Functions f_variance =: 'entry f (xs: []f64) : f64 = let mean = reduce (+) 0 xs / f64.i64 (length xs) in let sq_diffs = map (\x -> (x - mean) ** 2) xs in reduce (+) 0 sq_diffs / f64.i64 (length xs)' fut f_stddev =: 'entry f (xs: []f64) : f64 = let mean = reduce (+) 0 xs / f64.i64 (length xs) in let sq_diffs = map (\x -> (x - mean) ** 2) xs in let var = reduce (+) 0 sq_diffs / f64.i64 (length xs) in f64.sqrt var' fut NB. Normalization f_normalize =: 'entry f (xs: []f64) : []f64 = let min_val = reduce f64.min f64.highest xs in let max_val = reduce f64.max f64.lowest xs in let range = max_val - min_val in if range == 0.0 then replicate (length xs) 0.5 else map (\x -> (x - min_val) / range) xs' fut NB. Softmax f_softmax =: 'entry f (xs: []f64) : []f64 = let max_val = reduce f64.max f64.lowest xs in let exps = map (\x -> f64.exp (x - max_val)) xs in let sum_exps = reduce (+) 0 exps in map (\e -> e / sum_exps) exps' fut NB. Z-Score normalization f_zscore =: 'entry f (xs: []f64) : []f64 = let n = length xs in let mean = reduce (+) 0 xs / f64.i64 n in let sq_diffs = map (\x -> (x - mean) ** 2) xs in let std = f64.sqrt (reduce (+) 0 sq_diffs / f64.i64 n) in if std == 0.0 then replicate n 0.0 else map (\x -> (x - mean) / std) xs' fut echo 'Compilation complete.' NB. =========================================================================== NB. J Native Equivalents NB. =========================================================================== j_double =: 2&* j_square =: *: j_cube =: ^&3 j_sum =: +/ j_product =: */ j_min =: <./ j_max =: >./ j_mean =: +/ % # j_variance =: [: (+/ % #) [: *: ] - +/ % # j_stddev =: [: %: [: (+/ % #) [: *: ] - +/ % # j_normalize =: (] - <./) % (>./ - <./) j_cumsum =: +/\ j_runmax =: >./\ j_softmax =: 3 : 0 shifted =. y - >./ y exps =. ^ shifted exps % +/ exps ) j_zscore =: 3 : 0 mean =. (+/ % #) y std =. %: (+/ % #) *: y - mean if. std = 0 do. (# y) # 0 else. (y - mean) % std end. ) NB. =========================================================================== NB. Display Helpers NB. =========================================================================== show_result =: 4 : 0 'name fres jres' =. y echo ' ', name, ':' echo ' Futhark: ', ": fres echo ' J: ', ": jres if. (3!:0 fres) e. 4 8 do. echo ' Match: ', ": +./ , (| fres - jres) < 1e_9 else. echo ' Match: ', ": fres -: jres end. ) NB. =========================================================================== NB. Run Demonstration NB. =========================================================================== echo '' echo '+===============================================================+' echo '| FUTHARK-J BRIDGE DEMONSTRATION |' echo '+===============================================================+' echo '' echo '+---------------------------------------------------------------+' echo '| 1. Basic Array Transformations |' echo '+---------------------------------------------------------------+' echo '' small =: 1 2 3 4 5 echo ' Input: ', ": small echo '' '' show_result 'Double';(f_double small);(j_double small) echo '' '' show_result 'Square';(f_square small);(j_square small) echo '' '' show_result 'Cube';(f_cube small);(j_cube small) echo '' echo '+---------------------------------------------------------------+' echo '| 2. Scalar Reductions |' echo '+---------------------------------------------------------------+' echo '' echo ' Input: ', ": small echo '' '' show_result 'Sum';(f_sum small);(j_sum small) echo '' '' show_result 'Product';(f_product small);(j_product small) echo '' '' show_result 'Mean';(f_mean small);(j_mean small) echo '' '' show_result 'Min';(f_min small);(j_min small) echo '' '' show_result 'Max';(f_max small);(j_max small) echo '' echo '+---------------------------------------------------------------+' echo '| 3. Statistical Functions |' echo '+---------------------------------------------------------------+' echo '' stats_data =: 2 4 4 4 5 5 7 9 echo ' Input: ', ": stats_data echo '' '' show_result 'Variance';(f_variance stats_data);(j_variance stats_data) echo '' '' show_result 'Std Dev';(f_stddev stats_data);(j_stddev stats_data) echo '' echo '+---------------------------------------------------------------+' echo '| 4. Array-Returning Operations |' echo '+---------------------------------------------------------------+' echo '' norm_data =: 10 20 30 40 50 echo ' Normalize input: ', ": norm_data echo '' '' show_result 'Normalize';(f_normalize norm_data);(j_normalize norm_data) echo '' echo ' Cumsum input: ', ": small echo '' '' show_result 'Cumsum';(f_cumsum small);(j_cumsum small) echo '' pi_data =: 3 1 4 1 5 9 2 6 echo ' Running max input: ', ": pi_data echo '' '' show_result 'RunMax';(f_runmax pi_data);(j_runmax pi_data) echo '' echo ' Softmax input: ', ": small echo '' '' show_result 'Softmax';(f_softmax small);(j_softmax small) echo '' echo ' Z-Score input: ', ": norm_data echo '' '' show_result 'Z-Score';(f_zscore norm_data);(j_zscore norm_data) echo '' echo '+---------------------------------------------------------------+' echo '| 5. Performance Comparison |' echo '+---------------------------------------------------------------+' echo '' echo ' Testing on large arrays to show parallel performance...' echo '' perf_test =: 3 : 0 for_size. 100000 500000 1000000 2000000 do. data =. ? size # 0 f_time =. 6!:2 'f_r =. f_stddev data' j_mean =. (+/ % #) data j_time =. 6!:2 'j_r =. %: (+/ % #) *: data - j_mean' ratio =. j_time % f_time echo ' StdDev n=', (9 ": size), ': Futhark ', (8j5 ": f_time), 's J ', (8j5 ": j_time), 's Ratio ', (5j2 ": ratio), 'x' end. ) perf_test '' echo '' echo '+---------------------------------------------------------------+' echo '| 6. Seamless Data Flow: J ↔ Futhark |' echo '+---------------------------------------------------------------+' echo '' echo ' Chaining J and Futhark operations:' echo '' input =: 100 200 300 400 500 echo ' [J input] ', ": input step1 =: f_normalize input echo ' [Futhark normalize] ', ": step1 step2 =: 100 * step1 echo ' [J scale *100] ', ": step2 step3 =: f_square step2 echo ' [Futhark square] ', ": step3 step4 =: <. step3 echo ' [J floor] ', ": step4 step5 =: f_cumsum step4 echo ' [Futhark cumsum] ', ": step5 echo '' echo ' Data flows seamlessly between J and Futhark!' echo '' echo '+---------------------------------------------------------------+' echo '| 7. Scalar Input Functions (v1.6.0) |' echo '+---------------------------------------------------------------+' echo '' NB. Prime factorization: scalar in, array out f_factors =: 'entry f (n: i64) : []i64 = let (_, _, result) = loop (x, i, acc) = (n, 2i64, ([] : []i64)) while x > 1 do if x % i == 0 then (x / i, i, acc ++ [i]) else (x, i + 1, acc) in result' fut NB. Fibonacci: scalar in, scalar out f_fib =: 'entry f (n: i64) : i64 = let (result, _, _) = loop (a, b, i) = (0i64, 1i64, 0i64) while i < n do (b, a + b, i + 1) in result' fut NB. J native equivalents j_factors =: q: j_fib =: 3 : 0 if. y < 2 do. y return. end. 'a b' =. 0 1 for_i. i. y - 1 do. 'a b' =. b, a + b end. b ) echo ' Scalar input, array output:' echo '' echo ' f_factors 12: ', ": f_factors 12 echo ' J q: 12: ', ": j_factors 12 echo '' echo ' f_factors 100: ', ": f_factors 100 echo ' J q: 100: ', ": j_factors 100 echo '' echo ' f_factors 12345: ', ": f_factors 12345 echo '' echo ' Scalar input, scalar output:' echo '' echo ' f_fib 10: ', ": f_fib 10 echo ' J fib 10: ', ": j_fib 10 echo '' echo ' f_fib 20: ', ": f_fib 20 echo ' J fib 20: ', ": j_fib 20 echo '' echo '+===============================================================+' echo '| DEMONSTRATION COMPLETE |' echo '+===============================================================+' echo '' echo ' Available Futhark verbs:' echo ' f_double, f_square, f_cube' echo ' f_sum, f_product, f_min, f_max, f_mean' echo ' f_variance, f_stddev' echo ' f_normalize, f_cumsum, f_runmax' echo ' f_softmax, f_zscore' echo ' f_factors, f_fib (v1.6.0 scalar input)' echo '' echo ' Usage: result =. f_double 1 2 3 4 5' echo ' result =. f_factors 12345' echo ''