GithubHelp home page GithubHelp logo

ashermancinelli / cxbqn Goto Github PK

View Code? Open in Web Editor NEW
29.0 5.0 4.0 5.91 MB

BQN virtual machine

License: GNU General Public License v3.0

CMake 3.99% C++ 95.70% Python 0.03% Cuda 0.24% Nix 0.04%
bqn apl cuda cplusplus cpp20

cxbqn's Introduction

CXBQN

NOTE: I haven't updated this in a while, so the bytecode for the BQN compiler is out of date.

VM for BQN.

See CONTRIBUTING.md for information on contributing.

Building

Requires a compiler capable of C++20. GCC 9.3.1 is the oldest compiler I have tested it with.

mkdir build
cd build
# Use CXX to choose your compiler
CXX=g++-11 cmake ..
make -j12
./BQN -v
CXBQN 0.11.0 compiled on Dec 25 2021

Or if you want to use nix, CXBQN has a flake:

$ nix shell github:ashermancinelli/cxbqn
$ BQN -v
CXBQN 0.11.0 compiled on Jan  1 1980

Using

$ ./BQN -h
Usage: BQN [options] args

Positional arguments:
args                   	all remaining arguments are passed into the BQN program as •args [default: {}]

Optional arguments:
-h --help              	shows help message and exits [default: false]
-e --execute           	execute a string as BQN code
-v --version           	prints version information and exits [default: false]
-p --execute-and-print 	execute a string as BQN code, pretty print the result
-x --dump-cu           	dump compilation units after compiling [default: false]
-r --repl              	enter REPL [default: false]
-f --file              	execute a string as BQN code
$ ./BQN -e '•Show 5+5'
10
$ ./BQN -p ↕10
⟨ 0 1 2 3 4 5 6 7 8 9 ⟩
$ ./BQN
   foo←5
5
$ echo '•Show 3‿3⥊↕9' > foo.bqn && ./BQN -f foo.bqn
┌─
╵ 0 1 2
  3 4 5
  6 7 8
        ┘

See the values of •listSys to see what system functions are available in your build of CXBQN.

The REPL also has Readline support if you enable that option at configure time. When you run cmake, pass the argument -DCXBQN_READLINE=ON like so:

mkdir build && cd build
cmake .. -DCXBQN_READLINE=ON

If you have a readline library and development headers installed, this will use them and provide a much nicer REPL environment with variable name completion like so:

$ ./BQN -r
   foo←5
5
   foobar←10
10
   # Tab once for attempted completion
   fo<TAB>
   foo

   # Tab twice for all options if there is no single completion available
   foo<TAB><TAB>
foo     foobar
   foo

Configuring Options

CMake Option Notes
CXBQN_READLINE Enable Readline support in the BQN repl (recommended)
CXBQN_MEM_STRATEGY Possible values include "shared_ptr", "leak", and "gc". "gc" has not been implemented yet. For best performance, select "leak"
CXBQN_BUILD_TESTS Build test suite. Builds take much longer.
CXBQN_LOG Enable logging. You will have to set the log level above level::off to get output. See log file cxbqn-debug.log after running BQN or a test runner.
CXBQN_LOGLEVEL Wrapper around spdlog's log levels. level::debug will produce an extremely large amount of data (~10-15gib for executing "2+2").
CXBQN_DEBUG_VM Output VM debugging information, including the opcode being executed, the program counter, and any opcode arguments.
CXBQN_DEEPCHECKS Enable deeper checks which may produce more helpful errors but will hurt performance.
CXBQN_CUDA Enable CUDA execution via •_CUDAFor
CXBQN_FFI Enable loading dynamic libraries and calling user functions from them.
CXBQN_NATIVE_R0 Enable native C++ implementation of some runtime functions from r0.
CXBQN_NATIVE_R1 Enable native C++ implementation of some runtime functions from r1.
CXBQN_PLOT Enable matplotlib bindings and the namespace •cxbqn.plot. See doc/plot.md for more information.

Specification Feature Support

Spec Item Compliance Notes
Bytecode Full
Runtime BQN-native and r0 Almost all of r0 has a native implementation. We plan to replace elements of r1 with native functions as well
System Functions Partial See •listsys for the supported system values. Good candidate for first contribution.
Namespaces Partial Using fields of a namespace and destructuring without aliasing work, however mutable namespace fields are not supported. System-provided namespace •file has support for •file.List and •file.Lines.

Nonstandard Extensions

GPU Execution

To build with CUDA, enable the CMake option CXBQN_CUDA at configure time. Support for CUDA execution is limited, but it exists! The primary function for CUDA execution is •_CUDAFor, which is a 1-modifier that performs the primitive 𝕗 on array 𝕩 and optionally 𝕨. If you give this functionality a try, feel free to open an issue for any bugs you encounter (there will be many). Here's an example:

$ cmake .. -DCXBQN_CUDA=ON
$ make -j 12
$ ./BQN -r
   (↕10) × •_CUDAFor ⌽↕10
⟨ 0 8 14 18 20 20 18 14 8 0 ⟩
   10 + •_CUDAFor ↕15
⟨ 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ⟩

See the WIP design document for parallel execution here.

Plotting

CXBQN supports bindings to matplotlib via the matplotlib-cpp library. To enable plotting, enable the CMake option CXBQN_PLOT. You must have Python, matplotlib, and numpy installed.

See doc/plot.md for more information.

BQN -e '•cxbqn.plot.Plot ↕10⋄•cxbqn.plot.Show@'

Simple Plotting Example

FFI

See the documentation for FFI here.

Libraries and Packaging

There has been discussion on this topic on the Matrix server, but no consensus has been reached. CXBQN supports importing from locations defined in the environment variable BQNPATH, a :-delimited path just like $PATH.

The order in which •Import searches for files is:

  1. Current working directory
  2. The BQNPATH environment variable (: delimited)
  3. <prefix>/share/bqn where <prefix> is the installation prefix of CXBQN
  4. /usr/share/bqn

Custom Namespace •cxbqn

Everything under the namespace •cxbqn is specific to the CXBQN implementation of BQN. As of v0.10.4, only the value •cxbqn.config is available under this namespace:

$ ./BQN -p '•cxbqn.config'
⟨ ⟨ "CXBQN_READLINE" 1 ⟩ ⟨ "CXBQN_CUDA" 0 ⟩ ⟨ "CXBQN_FFI" 0 ⟩ ⟨ "CXBQN_PLOT" 0 ⟩ ⟩

cxbqn's People

Contributors

ashermancinelli avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cxbqn's Issues

Throwing tests failing

Here's a log of the failing test as of v0.8.1:

[doctest] doctest version is "2.4.6"
[doctest] run with "--help" for options
[2021-11-29 16:58:28.405] [critical] test=''a'+'c''
[2021-11-29 16:58:28.406] [critical] test='F←-⋄f+2'
[2021-11-29 16:58:28.406] [critical] test='97-'a''
[2021-11-29 16:58:28.406] [critical] test='@-1'
[2021-11-29 16:58:28.406] [critical] test='-'a''
[2021-11-29 16:58:28.406] [critical] test='F←÷⋄-f'
[2021-11-29 16:58:28.406] [critical] test='2×'a''
[2021-11-29 16:58:28.406] [critical] test='÷'b''
[2021-11-29 16:58:28.406] [critical] test='F←√-⋄÷f'
[2021-11-29 16:58:28.406] [critical] test='⋆'π''
[2021-11-29 16:58:28.406] [critical] test=''e'⋆'π''
[2021-11-29 16:58:28.406] [critical] test='F←⌈⋄⌊f'
[2021-11-29 16:58:28.406] [critical] test='F←+⋄G←-⋄f≤g'
[2021-11-29 16:58:28.406] [critical] test='!0'
[2021-11-29 16:58:28.406] [critical] test='"error"!"abc"'
[2021-11-29 16:58:28.406] [critical] test='√'x''
[2021-11-29 16:58:28.406] [critical] test=''a'∧¯1'
[2021-11-29 16:58:28.406] [critical] test='F←-⋄2∨f'
[2021-11-29 16:58:28.406] [critical] test='¬'a''
[2021-11-29 16:58:28.406] [critical] test='2¬'c''
[2021-11-29 16:58:28.406] [critical] test='F←{𝕩}⋄0¬f'
[2021-11-29 16:58:28.407] [critical] test='F←+-⋄|f'
[2021-11-29 16:58:28.407] [critical] test='26|'A''
===============================================================================
/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:529:
TEST CASE:  26|'A'

/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:550: FATAL ERROR: REQUIRE_THROWS( vm::run(cu) ) did NOT throw at all!

[2021-11-29 16:58:28.407] [critical] test='⊑""'
[2021-11-29 16:58:28.407] [critical] test='⊑2‿0⥊⟨⟩'
[2021-11-29 16:58:28.407] [critical] test='+´11'
[2021-11-29 16:58:28.407] [critical] test='-´<'a''
[2021-11-29 16:58:28.407] [critical] test='×´3‿1⥊"abc"'
[2021-11-29 16:58:28.407] [critical] test='2‿3⊢¨4‿5‿6'
[2021-11-29 16:58:28.407] [critical] test='"abcd"-"a"'
[2021-11-29 16:58:28.407] [critical] test='(↕4)×(↕3)⊢⌜↕2'
[2021-11-29 16:58:28.408] [critical] test='10⊑↕10'
[2021-11-29 16:58:28.408] [critical] test='¯11⊑↕10'
[2021-11-29 16:58:28.408] [critical] test='0.5⊑↕10'
[2021-11-29 16:58:28.408] [critical] test=''x'⊑↕10'
[2021-11-29 16:58:28.408] [critical] test='⟨⟩⊑↕10'
[2021-11-29 16:58:28.408] [critical] test='2⊑3+⌜○↕4'
[2021-11-29 16:58:28.408] [critical] test='21‿12‿03≡⟨2‿¯3‿0,1‿2,0‿¯1⟩⊑(10×↕3)+⌜↕4'
[2021-11-29 16:58:28.408] [critical] test='⟨2,⟨3⟩⟩⊑↕4'
[2021-11-29 16:58:28.409] [critical] test='(<2)⊑↕4'
[2021-11-29 16:58:28.409] [critical] test='(≍≍2)⊑↕4'
[2021-11-29 16:58:28.409] [critical] test='↕@'
[2021-11-29 16:58:28.409] [critical] test='↕2.4'
[2021-11-29 16:58:28.409] [critical] test='↕<6'
[2021-11-29 16:58:28.409] [critical] test='↕≍2‿3'
[2021-11-29 16:58:28.409] [critical] test='↕¯1‿2'
[2021-11-29 16:58:28.409] [critical] test='-˙◶÷‿× 4'
[2021-11-29 16:58:28.409] [critical] test='¯3⥊3'
[2021-11-29 16:58:28.410] [critical] test='1.6‿2.5⥊↕4'
[2021-11-29 16:58:28.410] [critical] test='(≍2‿3)⥊↕3'
[2021-11-29 16:58:28.410] [critical] test='"     "≡5⥊""'
[2021-11-29 16:58:28.410] [critical] test='4‿∘⥊↕15'
[2021-11-29 16:58:28.410] [critical] test='>↕¨2‿3'
[2021-11-29 16:58:28.410] [critical] test='>⟨⥊2,3⟩'
[2021-11-29 16:58:28.410] [critical] test='>(≍⋈⊢)↕4'
[2021-11-29 16:58:28.411] [critical] test='1‿0≍1‿2‿3'
[2021-11-29 16:58:28.411] [critical] test='≍⟜≍↕3'
[2021-11-29 16:58:28.411] [critical] test='⌽⎉1.1 ↕4'
[2021-11-29 16:58:28.411] [critical] test='⌽⎉'x' ↕4'
[2021-11-29 16:58:28.411] [critical] test='⌽⎉(<<0) ↕4'
[2021-11-29 16:58:28.411] [critical] test='⌽⎉≍ ↕4'
[2021-11-29 16:58:28.412] [critical] test='⌽⚇2‿2.5 ↕3'
[2021-11-29 16:58:28.412] [critical] test='2+⍟1‿'c'4'
[2021-11-29 16:58:28.412] [critical] test='⋆⍟1.5 2'
[2021-11-29 16:58:28.412] [critical] test='+`4'
[2021-11-29 16:58:28.412] [critical] test='+`<'c''
===============================================================================
/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:1585:
TEST CASE:  +`<'c'

/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:1606: FATAL ERROR: REQUIRE_THROWS( vm::run(cu) ) did NOT throw at all!

[2021-11-29 16:58:28.412] [critical] test='3‿4+`4+⌜○↕3'
[2021-11-29 16:58:28.412] [critical] test='⊏""'
[2021-11-29 16:58:28.412] [critical] test='⊏0‿3⥊""'
[2021-11-29 16:58:28.412] [critical] test='3⊏"abc"'
[2021-11-29 16:58:28.413] [critical] test='1.5⊏"abc"'
[2021-11-29 16:58:28.413] [critical] test=''x'⊏"abc"'
[2021-11-29 16:58:28.413] [critical] test='⟨⥊0,1⟩⊏≍"abc"'
[2021-11-29 16:58:28.413] [critical] test='0‿0<¨⊸⊏"abc"'
[2021-11-29 16:58:28.413] [critical] test='⟨3‿¯∞,⟨⟩⟩⊏4‿3⥊0'
[2021-11-29 16:58:28.414] [critical] test='(≍≍<5‿1)⊏↕6‿2'
[2021-11-29 16:58:28.414] [critical] test='2.5↑"abce"'
[2021-11-29 16:58:28.414] [critical] test='2‿'c'↑"abcd"'
[2021-11-29 16:58:28.414] [critical] test='(≍2‿3)↑"abcd"'
[2021-11-29 16:58:28.414] [critical] test='0.1↓"abcd"'
[2021-11-29 16:58:28.415] [critical] test='⟨∘⟩↓"abcd"'
[2021-11-29 16:58:28.415] [critical] test='@↕↕5'
[2021-11-29 16:58:28.415] [critical] test='2‿1↕↕5'
[2021-11-29 16:58:28.415] [critical] test='¯1↕↕5'
[2021-11-29 16:58:28.415] [critical] test='7↕↕5'
[2021-11-29 16:58:28.415] [critical] test=''a'«'b''
[2021-11-29 16:58:28.415] [critical] test='"a"»'b''
[2021-11-29 16:58:28.415] [critical] test='≍⊸»"abc"'
[2021-11-29 16:58:28.415] [critical] test='⌽'a''
[2021-11-29 16:58:28.415] [critical] test='⌽<∞'
[2021-11-29 16:58:28.415] [critical] test='2⌽'a''
[2021-11-29 16:58:28.416] [critical] test='1‿2⌽↕4'
[2021-11-29 16:58:28.416] [critical] test='⌽‿2⌽3+⌜○↕4'
[2021-11-29 16:58:28.416] [critical] test='(<<3)⌽↕4'
[2021-11-29 16:58:28.416] [critical] test='/2'
[2021-11-29 16:58:28.416] [critical] test='/1‿¯1‿0'
[2021-11-29 16:58:28.416] [critical] test='/=⌜˜↕2'
[2021-11-29 16:58:28.416] [critical] test='2/<2'
[2021-11-29 16:58:28.416] [critical] test='0‿1/"abc"'
[2021-11-29 16:58:28.416] [critical] test='⟨↕3,↕3⟩/"abc"'
[2021-11-29 16:58:28.417] [critical] test='1‿2/○≍"ab"'
[2021-11-29 16:58:28.417] [critical] test='¯1‿2/"ab"'
[2021-11-29 16:58:28.417] [critical] test='∾'c''
[2021-11-29 16:58:28.417] [critical] test='∾"abc"'
[2021-11-29 16:58:28.417] [critical] test='∾≍"ab"‿"cde"‿""'
[2021-11-29 16:58:28.418] [critical] test='∾⟨2‿3,1‿3,2‿2⟩⥊¨0'
[2021-11-29 16:58:28.419] [critical] test=''a'∾≍"abc"'
[2021-11-29 16:58:28.419] [critical] test='"ab"∾○≍"cde"'
[2021-11-29 16:58:28.419] [critical] test='(2‿3⥊↕6)∾↕2'
[2021-11-29 16:58:28.419] [critical] test='⊔3'
[2021-11-29 16:58:28.420] [critical] test='⊔<3'
[2021-11-29 16:58:28.420] [critical] test='⊔≍↕3'
[2021-11-29 16:58:28.420] [critical] test='⊔1.5‿0‿2'
[2021-11-29 16:58:28.420] [critical] test='⊔1‿¯2'
[2021-11-29 16:58:28.421] [critical] test='⊔˜'a'‿1‿0'
[2021-11-29 16:58:28.421] [critical] test='4⊔○↕2'
[2021-11-29 16:58:28.421] [critical] test='⟨1‿2,3‿1⟩⊔2‿3⥊0'
[2021-11-29 16:58:28.422] [critical] test='⟨1‿2,3‿4‿5,6‿7⟩⊔2‿3⥊0'
[2021-11-29 16:58:28.422] [critical] test='≍⊸⊔≍˘↕3'
[2021-11-29 16:58:28.422] [critical] test='⟨⟨<3,2⟩,¯1‿0‿¯1⟩⊔2‿3‿4⥊↕24'
[2021-11-29 16:58:28.424] [critical] test='(2‿3⥊↕4)⊔↕2‿2'
[2021-11-29 16:58:28.424] [critical] test='(3‿3⥊↕4)⊔↕2‿2'
[2021-11-29 16:58:28.425] [critical] test='⊐˜'a''
[2021-11-29 16:58:28.425] [critical] test='⊏⊸⊐"abc"'
[2021-11-29 16:58:28.425] [critical] test='(3‿2‿4⥊0)⊐4⥊1'
[2021-11-29 16:58:28.425] [critical] test='⊐+˙@'
[2021-11-29 16:58:28.425] [critical] test='(↕5)∊1'
[2021-11-29 16:58:28.425] [critical] test='2∊≍˘↕4'
[2021-11-29 16:58:28.426] [critical] test='∊<4'
[2021-11-29 16:58:28.426] [critical] test='⍷'a''
[2021-11-29 16:58:28.426] [critical] test='≍⊸⍷"abc"'
[2021-11-29 16:58:28.426] [critical] test='0‿¯1‿1⍉(3⥊1)⥊1'
[2021-11-29 16:58:28.426] [critical] test='1‿0≍˘⊸⍉"ab"≍"cd"'
[2021-11-29 16:58:28.427] [critical] test='0‿2⍉+⌜˜↕3'
[2021-11-29 16:58:28.427] [critical] test='2‿0‿0⍉↕↕3'
[2021-11-29 16:58:28.428] [critical] test='3⍉↕↕3'
[2021-11-29 16:58:28.428] [critical] test='⍋'a''
[2021-11-29 16:58:28.428] [critical] test='⍋'a'‿∘'
===============================================================================
/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:3351:
TEST CASE:  ⍋'a'‿∘

/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:3372: FATAL ERROR: REQUIRE_THROWS( vm::run(cu) ) did NOT throw at all!

[2021-11-29 16:58:28.428] [critical] test='⍒2'
[2021-11-29 16:58:28.428] [critical] test='∧⊏⟨+⟩'
[2021-11-29 16:58:28.428] [critical] test='∧+‿-'
[2021-11-29 16:58:28.429] [critical] test='∨'c''
[2021-11-29 16:58:28.429] [critical] test='⍋˜6'
[2021-11-29 16:58:28.429] [critical] test='⍒⟜↕4'
[2021-11-29 16:58:28.429] [critical] test='(3‿2‿4⥊0)⍋4⥊1'
[2021-11-29 16:58:28.429] [critical] test='(3‿2‿4⥊0)⍒1'
[2021-11-29 16:58:28.430] [critical] test='⟨+⟩⍋↕6'
[2021-11-29 16:58:28.430] [critical] test='⟨1‿3‿1,1‿3‿2⟩⍒⟨1‿3‿{𝕩}⟩'
[2021-11-29 16:58:28.431] [critical] test='⍉⌾≍ "abc"'
[2021-11-29 16:58:28.431] [critical] test='-⌾⊏ 4'
[2021-11-29 16:58:28.431] [critical] test='↕∘≠⊸+⌾(10⊸⥊)↕6'
[2021-11-29 16:58:28.432] [critical] test='↕∘≠⊸+⌾(2⊸/)↕5'
[2021-11-29 16:58:28.433] [critical] test='1⊸⌽⌾(1‿3‿3‿0⊸⊏)"abcd"'
[2021-11-29 16:58:28.433] [critical] test='⌽⌾(1↓4↑⊢)"abc"'
===============================================================================
/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:3745:
TEST CASE:  ⌽⌾(1↓4↑⊢)"abc"

/home/asher/workspace/cxbqn/test/test_prim_setprims_throws.cpp:3768: FATAL ERROR: REQUIRE_THROWS( vm::run(cu) ) did NOT throw at all!

===============================================================================
[doctest] test cases: 154 | 150 passed | 4 failed | 0 skipped
[doctest] assertions: 154 | 150 passed | 4 failed |
[doctest] Status: FAILURE!

`•_CUDAFor` development plan

The following system modifier is now available with limited support:

$ ./BQN -e '•Show (↕10) + •_CUDAFor ↕10'
⟨ 0 2 4 6 8 10 12 14 16 18 ⟩

What should the development plan be for CUDA/GPU support after the runtime is implemented on the device?

shared_ptr is extremely slow

The locking policy on shared pointers takes about 33% of the runtime of CXBQN. This should be replaced by a gc.

Blockinst calls use unneeded heap allocs

v0.7.0:

10.70%  _int_free
 7.15%  malloc
 6.18%  cxbqn::types::BlockInst::call
 5.82%  cxbqn::vm::instructions::fn2o
 5.42%  cxbqn::types::Md2Deferred::call
 4.80%  cxbqn::vm::vm
 4.69%  std::vector<std::shared_ptr<cxbqn::types::Value>, std::allocator<std::shared_ptr<cxbqn::types::Value> > >::~vector
 4.52%  cxbqn::vm::instructions::varo
 4.10%  cxbqn::vm::instructions::fn2c
 3.32%  cfree@GLIBC_2.2.5

This is because of all the vector constructors. Should probably use O<Value> (&args)[6] as the param type instead of a vec.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.