Skip to content

[WIP] OpenBLAS and Numpy #671

Open
Open
@meow464

Description

@meow464

During the last ~2 months I got to what seems to be a working port of flang for iOS, but I still have to figure out how to make it work with numpy and kivy.

Here are the steps to get where I'm at, first build flang and openblas:

  1. Build flang for iOS whit this instructions. Build the version with embedded bitcode, feel free to ask questions here if it's not clear how.
  2. Build OpenBLAS with lapack for iOS: Clone openblas, save this script and run from within the repo. Everything will be installed in this same directory under the "INSTALL" folder.

Make a kivy project:

  1. mkdir kivy-OpenBLAS && cd kivy-OpenBLAS
  2. python -m venv venv && . venv/bin/activate
  3. git clone https://github.com/kivy/kivy-ios.git && pip install -e kivy-ios

You can make modification in the kivy-ios/ folder and it will affect the installed kivy-ios.

Create a new project as per usual:

  1. Create a test-app folder and put a hello world main.py in it;
  2. toolchain build python3 kivy

Copy the openblas files to `dist/hostpython3/:

  1. Copy INSTALL/lib/libopenblas_armv8p-r0.3.19.dev.a from the openblas folder to dist/hostpython3/lib/libopenblas.a. Note that you need to rename it.
  2. Copy everything in INSTALL/include to dist/hostpython3/include

So far openblas is only built for iOS therefore you will need to use toolchain build numpy --arch arm64 to test numpy.
To make changes to the numpy recipe simply edit kivy-ios/kivy_ios/recipes/numpy/__init__.py.

My currently not working numpy recipe
from kivy_ios.toolchain import CythonRecipe
from os.path import join
import sh
import shutil


class NumpyRecipe(CythonRecipe):
    version = "1.20.2"
    url = "https://pypi.python.org/packages/source/n/numpy/numpy-{version}.zip"
    library = "libnumpy.a"
    libraries = ["libnpymath.a", "libnpyrandom.a"]
    include_dir = "numpy/core/include"
    depends = ["python"]
    hostpython_prerequisites = ["Cython"]
    cythonize = False

    def prebuild_arch(self, arch):
        if self.has_marker("patched"):
            return
        self.apply_patch("duplicated_symbols.patch")
        self.set_marker("patched")

    def get_recipe_env(self, arch):
        env = super().get_recipe_env(arch)
        # CC must have the CFLAGS with arm arch, because numpy tries first to
        # compile and execute an empty C to see if the compiler works. This is
        # obviously not working when crosscompiling
        env["CC"] = "{} {}".format(env["CC"], env["CFLAGS"])
        env["FC"] = "/ur/local/bin/flang -L/usr/local/flang-iOS"
        # Disable Accelerate.framework by disabling the optimized BLAS and LAPACK libraries cause it's now unsupported
        env["NPY_BLAS_ORDER"] = "openblas"
        env["NPY_LAPACK_ORDER"] = "openblas"
        return env

    def build_arch(self, arch):
        super().build_arch(arch)
        sh.cp(sh.glob(join(self.build_dir, "build", "temp.*", "libnpy*.a")),
              self.build_dir)

    def reduce_python_package(self):
        dest_dir = join(self.ctx.site_packages_dir, "numpy")
        shutil.rmtree(join(dest_dir, "core", "include"))
        shutil.rmtree(join(dest_dir, "core", "tests"))
        shutil.rmtree(join(dest_dir, "distutils"))
        shutil.rmtree(join(dest_dir, "doc"))
        shutil.rmtree(join(dest_dir, "f2py", "tests"))
        shutil.rmtree(join(dest_dir, "fft", "tests"))
        shutil.rmtree(join(dest_dir, "lib", "tests"))
        shutil.rmtree(join(dest_dir, "linalg", "tests"))
        shutil.rmtree(join(dest_dir, "ma", "tests"))
        shutil.rmtree(join(dest_dir, "matrixlib", "tests"))
        shutil.rmtree(join(dest_dir, "polynomial", "tests"))
        shutil.rmtree(join(dest_dir, "random", "tests"))
        shutil.rmtree(join(dest_dir, "tests"))


recipe = NumpyRecipe()

All input is welcome!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions