A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://numexpr.readthedocs.io/en/latest/user_guide.html below:

Website Navigation


NumExpr 2.0 User Guide — numexpr 2.8.5.dev1 documentation

NumExpr 2.0 User Guide

The numexpr package supplies routines for the fast evaluation of array expressions elementwise by using a vector-based virtual machine.

Using it is simple:

>>> import numpy as np
>>> import numexpr as ne
>>> a = np.arange(10)
>>> b = np.arange(0, 20, 2)
>>> c = ne.evaluate("2*a+3*b")
>>> c
array([ 0,  8, 16, 24, 32, 40, 48, 56, 64, 72])
Building

NumExpr requires Python 2.6 or greater, and NumPy 1.7 or greater. It is built in the standard Python way:

$ python setup.py build
$ python setup.py install

You must have a C-compiler (i.e. MSVC on Windows and GCC on Linux) installed.

Then change to a directory that is not the repository directory (e.g. /tmp) and test numexpr with:

$ python -c "import numexpr; numexpr.test()"
Enabling Intel VML support

Starting from release 1.2 on, numexpr includes support for Intel’s VML library. This allows for better performance on Intel architectures, mainly when evaluating transcendental functions (trigonometrical, exponential, …). It also enables numexpr using several CPU cores.

If you have Intel’s MKL (the library that embeds VML), just copy the site.cfg.example that comes in the distribution to site.cfg and edit the latter giving proper directions on how to find your MKL libraries in your system. After doing this, you can proceed with the usual building instructions listed above. Pay attention to the messages during the building process in order to know whether MKL has been detected or not. Finally, you can check the speed-ups on your machine by running the bench/vml_timing.py script (you can play with different parameters to the set_vml_accuracy_mode() and set_vml_num_threads() functions in the script so as to see how it would affect performance).

Threadpool Configuration

Threads are spawned at import-time, with the number being set by the environment variable NUMEXPR_MAX_THREADS. The default maximum thread count is 64. There is no advantage to spawning more threads than the number of virtual cores available on the computing node. Practically NumExpr scales at large thread count (> 8) only on very large matrices (> 2**22). Spawning large numbers of threads is not free, and can increase import times for NumExpr or packages that import it such as Pandas or PyTables.

If desired, the number of threads in the pool used can be adjusted via an environment variable, NUMEXPR_NUM_THREADS (preferred) or OMP_NUM_THREADS. Typically only setting NUMEXPR_MAX_THREADS is sufficient; the number of threads used can be adjusted dynamically via numexpr.set_num_threads(int). The number of threads can never exceed that set by NUMEXPR_MAX_THREADS.

If the user has not configured the environment prior to importing NumExpr, info logs will be generated, and the initial number of threads that are used*_ will be set to the number of cores detected in the system or 8, whichever is *less.

Usage:

import os
os.environ['NUMEXPR_MAX_THREADS'] = '16'
os.environ['NUMEXPR_NUM_THREADS'] = '8'
import numexpr as ne
Usage Notes

NumExpr’s principal routine is:

evaluate(ex, local_dict=None, global_dict=None, optimization='aggressive', truediv='auto')

where ex is a string forming an expression, like "2*a+3*b". The values for a and b will by default be taken from the calling function’s frame (through the use of sys._getframe()). Alternatively, they can be specified using the local_dict or global_dict arguments, or passed as keyword arguments.

The optimization parameter can take the values 'moderate' or 'aggressive'. 'moderate' means that no optimization is made that can affect precision at all. 'aggressive' (the default) means that the expression can be rewritten in a way that precision could be affected, but normally very little. For example, in 'aggressive' mode, the transformation x~**3 -> x*x*x is made, but not in 'moderate' mode.

The truediv parameter specifies whether the division is a ‘floor division’ (False) or a ‘true division’ (True). The default is the value of __future__.division in the interpreter. See PEP 238 for details.

Expressions are cached, so reuse is fast. Arrays or scalars are allowed for the variables, which must be of type 8-bit boolean (bool), 32-bit signed integer (int), 64-bit signed integer (long), double-precision floating point number (float), 2x64-bit, double-precision complex number (complex) or raw string of bytes (str). If they are not in the previous set of types, they will be properly upcasted for internal use (the result will be affected as well). The arrays must all be the same size.

Datatypes supported internally

NumExpr operates internally only with the following types:

If the arrays in the expression does not match any of these types, they will be upcasted to one of the above types (following the usual type inference rules, see below). Have this in mind when doing estimations about the memory consumption during the computation of your expressions.

Also, the types in NumExpr conditions are somewhat stricter than those of Python. For instance, the only valid constants for booleans are True and False, and they are never automatically cast to integers.

Casting rules

Casting rules in NumExpr follow closely those of NumPy. However, for implementation reasons, there are some known exceptions to this rule, namely:

Supported operators

NumExpr supports the set of operators listed below:

Supported functions

The next are the current supported set:

Notes

More functions can be added if you need them. Note however that NumExpr 2.6 is in maintenance mode and a new major revision is under development.

Supported reduction operations

The next are the current supported set:

Note: because of internal limitations, reduction operations must appear the last in the stack. If not, it will be issued an error like:

>>> ne.evaluate('sum(1)*(-1)')
RuntimeError: invalid program: reduction operations must occur last
General routines
Intel’s VML specific support routines

When compiled with Intel’s VML (Vector Math Library), you will be able to use some additional functions for controlling its use. These are:

Authors

Numexpr was initially written by David Cooke, and extended to more types by Tim Hochberg.

Francesc Alted contributed support for booleans and simple-precision floating point types, efficient strided and unaligned array operations and multi-threading code.

Ivan Vilata contributed support for strings.

Gregor Thalhammer implemented the support for Intel VML (Vector Math Library).

Mark Wiebe added support for the new iterator in NumPy, which allows for better performance in more scenarios (like broadcasting, fortran-ordered or non-native byte orderings).

Gaëtan de Menten contributed important bug fixes and speed enhancements.

Antonio Valentino contributed the port to Python 3.

Google Inc. contributed bug fixes.

David Cox improved readability of the Readme.

Robert A. McLeod contributed bug fixes and ported the documentation to numexpr.readthedocs.io. He is the maintainer of the package since 2016.

License

NumExpr is distributed under the MIT license.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4