2

大规模并行功能运行时 HVM

 2 years ago
source link: https://www.oschina.net/p/hvm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

高阶虚拟机 (HVM) 是一个纯函数式编译目标,它是惰性的、非垃圾收集的大规模并行的,同时也是 beta 最优的,这意味着在某些情况下,它可以比大多数功能运行时(包括 Haskell 的 GHC)快得多。

这得益于一种新的计算模型,即交互网络,它结合了图灵机和 Lambda 演算,该模型的先前实现在实践中效率低下,然而,最近的一项突破大大提高了其效率,催生了 HVM。尽管是一个原型,但它在许多情况下已经击败了成熟的编译器,并且将朝着未知的性能水平扩展。

cargo install hvm

2.创建一个HVM文件

HVM 文件看起来像无类型的 Haskell,将以下文件另存为main.hvm

// Creates a tree with `2^n` elements
(Gen 0) = (Leaf 1)
(Gen n) = (Node (Gen(- n 1)) (Gen(- n 1)))

// Adds all elements of a tree
(Sum (Leaf x))   = x
(Sum (Node a b)) = (+ (Sum a) (Sum b))

// Performs 2^n additions in parallel
(Main n) = (Sum (Gen n))

上面的程序创建了一个包含元素的完美二叉树2^n并将它们相加,由于它是递归的,HVM 会自动并行化它。

3.运行和编译

hvm r main 10                       #以 n=10 运行
hvm c main                          #将 HVM 编译为 C 
clang -O2 main.c -o main -lpthread  #将 C 编译为 BIN 
./main 30                           #以 n=30 运行

上面的程序在现代 8 核处理器上运行大约需要 6.4 秒,而相同的 Haskell 代码在具有 GHC 的同一台机器上运行大约需要19.2 秒

这就是 HVM:编写一个函数式程序,获得一个并行的 C 运行时。

其他:

展开阅读全文

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK