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,

pip3 install -r py_cpu/requirements.txt   #manually install the build requirements
pip3 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 FeatureFalgs 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 pprint 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 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.0.tar.gz (718.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

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

Uploaded CPython 3.6mmanylinux: glibc 2.12+ x86-64

File details

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

File metadata

  • Download URL: py_cpu-0.1.0.tar.gz
  • Upload date:
  • Size: 718.3 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.0.tar.gz
Algorithm Hash digest
SHA256 e67f0380afbc2a7d3450211c95489d1618a71e92804d4ea9da82070b3d49f331
MD5 a2dff33e1d5a106dcac42a49c1272bc0
BLAKE2b-256 125e08fa5d0e9b518e530d60c0f72421fb650c19bcf0ea1b11067298ce76851e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_cpu-0.1.0-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.0-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 c057667b4ab2329618c01673f368b33f2d5049015f3a11911783b5057a2e1a7f
MD5 45b130423f90311e98d6a78376ae432f
BLAKE2b-256 382506713eed543605e3b3e45cdcb7ecb68320708623feff482c6cbc4ae2bfc5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_cpu-0.1.0-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.0-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 83b797fabfeb40159ef4a8e4d2ecf5cbc77cc3fed84a95dc8080b1f9cc4332c5
MD5 95d6e250a5acab3dfc58e6e5e5092558
BLAKE2b-256 9b50cbe60810db2ebcb1b301efabb546dd44859686dfc30373cdbf3252861e53

See more details on using hashes here.

Supported by

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