Suzhou-Tongyuan / jnumpy

Writing Python C extensions in Julia within 5 minutes.
MIT License
234 stars 8 forks source link

JNumPy: writing high-performance C extensions for Python in minutes

Install JNumPy

Requirements:

You can install the Python package jnumpy with the following command:

pip install julia-numpy.

Note that JNumPy will install julia in JNUMPY_HOME for you, if there is no Julia installation available.

Usage

  1. create a Python package example, write and export julia functions in the file example/src/example.jl

    module example
    
    using TyPython
    using TyPython.CPython
    
    @export_py function mat_mul(a::AbstractArray, b::AbstractArray)::Array
        return a * b
    end
    
    function init()
        @export_pymodule _example begin
            jl_mat_mul = Pyfunc(mat_mul)
        end
    end
    
    # the following code is optional,
    # but makes Python code loading much faster since the second time.
    precompile(init, ())
    
    end
  2. create example/Project.toml as follows:

    name = "example"  # this is required to find the julia's entry module
    
    [deps]
    # specify your julia dependencies here
  3. initialize and import the julia functions at example/__init__.py:

    import jnumpy as np
    # you may call np.set_julia_mirror(server) to set the julia package server,
    # or leave the argument server empty to automatically search the nearest mirror.
    np.init_jl()
    np.init_project(__file__)
    
    from _example import jl_mat_mul
    
    __all__ = ['jl_mat_mul']
  4. enjoy your Python extension package:

    This is the structure of your Python extension package:

    > ls -R
    example/:
        __init__.py  Project.toml  src
    
    example/src:
        example.jl

    This is how you use it:

    from example import jl_mat_mul
    x = np.array([[1,2],[3,4]])
    y = np.array([[4,3],[2,1]])
    jl_mat_mul(x, y)
    # array([[ 8,  5],
    #       [20, 13]])

Environment Variables

Examples

There are several examples presented in the demo directory. Those examples are standalone Python packages created using JNumPy, and can be imported if you have JNumPy installed.

Contributions

Open-source contributions are kindly accepted and appreciated including bug reports, documentations, pull requests, and general suggestions.