Skip to main content

Python bindings for Google's cpu_features library.

Project description

py_cpu

Python bindings for Google's cpu_features library. Using this library, Python developers can check for hardware specific features and enable the respective optimizations in their software at runtime. py_cpu provides bindings for multiple hardware architectures like x86, ARM, AARCH64, MIPS and PPC.

Quick start

To use py_cpu, You can directly download and install the pre-built wheel file, using the command below:

pip install py-cpu

(For x86, the pre-built wheel will be installed automatically, on other platforms, the source distribution will be downloaded and the package will be built on the platform natively)

Building from source:

Requirements: 1. Python3 2. CMake 3. setuptools 4. wheel 5. scikit-build

TO build from source, you can just clone this repository, you need not have to clone the submodules, as they will be downloaded by the cmake build system automatically.

git clone https://github.com/Narasimha1997/py_cpu.git

To directly install the package from the source, use the command below:

pip install py_cpu/   #-- the repo root directory

If you are using an older version of pip, the build-system packages will not be automatically installed, in that case,

pip install -r py_cpu/requirements.txt   #manually install the build requirements
pip install py_cpu/                      #then install the package

To build sdist and bdist_wheel you can just run setup.py as follows:

cd py_cpu/
python3 install sdist bdist_wheel

Usage guide:

To use the package in your codebase, just import py_cpu.

import py_cpu

1. Get the CPU info

import py_cpu

#get cpu info
cpu_info = py_cpu.CPUInfo()

2. Check for features:

import py_cpu


#call this once during the program init, to avoid unnecessary compute unless required.
cpu_info = py_cpu.CPUInfo()

#check if the CPU supports AES instructions
if cpu_info.features.aes :
    print('Yes, it supports, Run the optimized code')
else :
    print('No, run normal code')

3. Get the list of supported features :

import py_cpu
cpu_info = py_cpu.CPUInfo()

#returns a python dictionary, you can check the feature by
# subscripting, example : features_dict['aes'] -> either True or False
features_dict = cpu_info.get_features_as_dict()
print(features_dict['avx'])

# returns a FeatureFlags object, this is simple to use because you can use . operator instead of subscripting.

features = cpu_info.get_features()
print(features.avx)

Get the general info about the hardware

Apart from features and SOCs, you can also query the general info - about architecture type, vendor etc. These fields are different for different hardware.

import py_cpu
cpu_info = py_cpu.CPUInfo()

#get list of field names
supported_fields = cpu_info.get_info_fields()

#example, on x86
# ['arch', 'brand', 'family', 'features', 'model', 'stepping', 'uarch', 'vendor']

#query the fields: Because the cpu_info object supports subscripting

brand_name = cup_info['brand']

#if you want the entire object as a dict
info_dict = cpu_info.as_dict()

#if you want the entire object but exclude features
info_dict_without_features = cpu_info.as_dict(include_features = False)

5. Print functions

If you just want to print the output, you can use any of these two methods. These methods will be just for a fancy fun use.

Pretty-Print Dict - This function uses pprint internally.

import py_cpu
#obtain CPU info
cpu_info = py_cpu.CPUInfo()

#call pprint method
cpu_info.pprint()

Print as table - This function uses python formatting/spacings to display the list as a table.

import py_cpu
#obtain CPU info
cpu_info = py_cpu.CPUInfo()

#call print_as_table method
cpu_info.print_as_table()

Guide for developers:

If you want to add new features, this section is for you. The repository depends on pybind11 and the original cpu_features repository by Google. Both of these are includes as submodules under src/. To get the complete codebase, you have to clone the submodules as well. Just run these commands from the project directory :

git submodule init 
git submodule update

Or you can clone the repo recursively.

Under the hood details:

The binding code is written in C++ and uses pybind11 to build a Cpython extension. binding.cc implements the binding code for all the five platform which are supported in the original repo. Since most of the C/C++ compiler implementations on any operating system expose the architecture flags as preprocessor definitions, only the target hardware binding code gets retained for compilation. The binding code also declares structures that are python friendly - Like STL maps etc to store the data. The entire structure is a read-only object for python and cannot be modified, this makes the implementation much more easy and faster. During the build-phase, CMake first compiles Google's cpu_features library as a submodule and builds a Position independent object code (PIC), since the Cpython extension is a shared dynamic library. Then the target is compiled with Pybind11 to create the final cpython extension. __init__.py is just like a glue which provides caching functionality by storing it inside an object, so you can only init once and use it throughout the lifecycle of the application.

Note : This python binding is not an offical Google release. The project respects the license and distribution terms of both cpu_features and pybind11 by adding them as sub-modules - this helps us to keep the original implementation as it is.

Contributing

If you like to contribute code to this repo, you are always welcome. I would encourage newbies to take up the tasks, as it would allow them to get into the open source world. Also, please do test it on variety of platforms. Please do raise issues if you have any problems.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_cpu-0.1.2.tar.gz (714.1 kB view details)

Uploaded Source

Built Distributions

py_cpu-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl (140.3 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

py_cpu-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl (140.3 kB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

File details

Details for the file py_cpu-0.1.2.tar.gz.

File metadata

  • Download URL: py_cpu-0.1.2.tar.gz
  • Upload date:
  • Size: 714.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.6.9

File hashes

Hashes for py_cpu-0.1.2.tar.gz
Algorithm Hash digest
SHA256 70001c7e00f9e09ea8768af42f0b37116abaad1c5216c2085b4fb932e1c21a40
MD5 3f5ce96fbb303d8f6d7715207a5b98ee
BLAKE2b-256 73f3f581ed27908bac4f365f8b1381e051a79d5cfbe73023c18feaa3faacde5f

See more details on using hashes here.

File details

Details for the file py_cpu-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: py_cpu-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 140.3 kB
  • Tags: CPython 3.7m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for py_cpu-0.1.2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 27cf783283e35c6b8392b1f9f28bb8cb2b6021cb16f4d2185c4a36cfb9e42642
MD5 0811d0e1951b925a66e797a0fb70e58f
BLAKE2b-256 3873d65c5b378a087b8368fcbe5f8038acef36bcbfc88a3df8c7f3a7d8cd5a55

See more details on using hashes here.

File details

Details for the file py_cpu-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl.

File metadata

  • Download URL: py_cpu-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl
  • Upload date:
  • Size: 140.3 kB
  • Tags: CPython 3.6m, manylinux: glibc 2.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.6

File hashes

Hashes for py_cpu-0.1.2-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 0221ddfebf50cd31765c87e4bd9d007cc583099d0ef7f607d5088a1e35c153bf
MD5 d08a498cde933c2afe98350fbb856b5a
BLAKE2b-256 3d0477d377a8de71c65b775a3d4ef61ef0ef8ef735f0c1ed1d4ab98eae0d418a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page