oscarbenjamin / protosym Goto Github PK
View Code? Open in Web Editor NEWSimple framework for symbolic manipulation
License: BSD 3-Clause "New" or "Revised" License
Simple framework for symbolic manipulation
License: BSD 3-Clause "New" or "Revised" License
We should add matrices and the capability to lambdify a matrix. We need to add the matrix type itself first but in the meantime here is the LLVM IR for a function that puts some values into a 2D numpy array:
module_code = """
; ModuleID = "mod1"
target triple = "unknown-unknown-unknown"
target datalayout = ""
declare double @llvm.pow.f64(double %Val1, double %Val2)
declare double @llvm.sin.f64(double %Val)
declare double @llvm.cos.f64(double %Val)
define void @"jit_func1"(double* %"x")
{
%1 = getelementptr double, double* %"x", i32 0
store double 0x3ff0000000000000, double* %1
%2 = getelementptr double, double* %"x", i32 1
store double 0x4000000000000000, double* %2
%3 = getelementptr double, double* %"x", i32 2
store double 0x4008000000000000, double* %3
%4 = getelementptr double, double* %"x", i32 3
store double 0x4010000000000000, double* %4
ret void
}
"""
# 1, 2, 3, 4:
# 0x3ff0000000000000
# 0x4000000000000000
# 0x4008000000000000
# 0x4010000000000000
import ctypes
try:
import llvmlite.binding as llvm
except ImportError: # pragma: no cover
msg = "llvmlite needs to be installed to use lambdify_llvm."
raise ImportError(msg) from None
_exe_eng = []
llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
llmod = llvm.parse_assembly(module_code)
pmb = llvm.create_pass_manager_builder()
pmb.opt_level = 2
pass_manager = llvm.create_module_pass_manager()
pmb.populate(pass_manager)
pass_manager.run(llmod)
target_machine = llvm.Target.from_default_triple().create_target_machine()
exe_eng = llvm.create_mcjit_compiler(llmod, target_machine)
exe_eng.finalize_object()
_exe_eng.append(exe_eng)
fptr = exe_eng.get_function_address("jit_func1")
c_float64 = ctypes.POINTER(ctypes.c_double)
rettype = ctypes.c_double
argtypes = [c_float64]
cfunc = ctypes.CFUNCTYPE(rettype, *argtypes)(fptr)
import numpy as np
def npfunc():
c_float64 = ctypes.POINTER(ctypes.c_double)
arr = np.zeros((2, 2), np.float64)
arr_p = arr.ctypes.data_as(c_float64)
cfunc(arr_p)
return arr
print(npfunc())
Running that gives
$ python test.py
[[1. 2.]
[3. 4.]]
There might be a better (e.g. vectorised) way to transfer memory than just using getelementptr
and store
. I am presuming for now that it is important to be able to do this efficiently for a sparse matrix so we expect that most entries of the matrix will be zero. In that case I think we want to run through all of our calculations to build up the final numbers and then copy them into the output array one by one.
It is possible that it would be better to have a callable function that reuses the underlying numpy array. It is also possible that it would be better to target some kind of sparse array format as the immediate output rather than storing values sparsely into an otherwise dense numpy array. I don't know whether subsequent calculations would prefer to have a dense or sparse format for the array. Another possibility is that this could fill a numpy array that only contains the nonzero values and then at the Python level that could be converted to a sparse array like:
In [16]: from scipy.sparse import bsr_array
In [17]: vals = np.array([1, 2, 3, 4])
In [18]: rows = cols = np.arange(4)
In [19]: a = bsr_array((vals, (rows, cols)), shape=(4, 4))
In [20]: a
Out[20]:
<4x4 sparse matrix of type '<class 'numpy.int64'>'
with 4 stored elements (blocksize = 1x1) in Block Sparse Row format>
In [21]: a.toarray()
Out[21]:
array([[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]])
Then the LLVM function could just be responsible for populating vals
.
This repository currently doesnot have a proper channel to chat with contributors.
Implement a google group or slack channel for discussion, or moving this repository to an organization(Sympy) would enable discussion forum.
Folllowing comments in gh-32.
The following sections need adding or filling out in docs:
TreeExpr.children
replace dummy docstring.ForwardGraph
add a good docstring with diagrams.(Feel free to edit/add to the list)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.