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. pycpu provides bindings for multiple hardware architectures like x86, ARM, AARCH64, MIPS and PPC.

Quick start

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

pip install pycpu

(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 pycpu.

import pycpu

1. Get the CPU info

import pycpu

#get cpu info
cpu_info = pycpu.CPUInfo()

2. Check for features:

import pycpu


#call this once during the program init, to avoid unnecessary compute unless required.
cpu_info = pycpu.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 pycpu
cpu_info = pycpu.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 pycpu
cpu_info = pycpu.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 pycpu
#obtain CPU info
cpu_info = pycpu.CPUInfo()

#call pprint method
cpu_info.pprint()

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

import pycpu
#obtain CPU info
cpu_info = pycpu.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.0.1.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.0.1-cp37-cp37m-manylinux2010_x86_64.whl (140.3 kB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.12+ x86-64

py_cpu-0.0.1-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.0.1.tar.gz.

File metadata

  • Download URL: py_cpu-0.0.1.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.0.1.tar.gz
Algorithm Hash digest
SHA256 4c61190d1d11684a313e30a74a2b4bd0ccbc092ad6887687841a37dc0f0d0a59
MD5 a2b6c07f5eef1a897064f0aa3eb09f78
BLAKE2b-256 13e7fe867b0805b69e25a3c039b6164b620e8e7480c5b47b51a2277ba478ea22

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_cpu-0.0.1-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.0.1-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 8f0256106160ac75e6614fc368ed5a6ef8e2afef8ef856a80972bc8a43c7875b
MD5 78ce74c9861bea73d4cebbb01f8262c4
BLAKE2b-256 b17413d1c40eb50ba09a8ecb4b10b971d6cec07b302e6db4709b3afb62b16fc7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: py_cpu-0.0.1-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.0.1-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 abcbdbd68a96a48a78f6347327aaa420226b9a31f40f15902796a3a376837bf3
MD5 c560115652a5c420282b408d67513371
BLAKE2b-256 3958a187305e645bbfdbd401a0c6eefc0f385a1fc18844d162309a022bcbb44a

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