a cython interface

28
Wir schaffen Wissen – heute für morgen J. Chrin Paul Scherrer Institut A Cython Interface to EPICS Channel Access for High-Level Python Applications [at SwissFEL] PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Upload: others

Post on 09-Apr-2022

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Cython Interface

Wir schaffen Wissen – heute für morgen

J. ChrinPaul Scherrer Institut

A Cython Interface to

EPICS Channel Access for

High-Level Python Applications[at SwissFEL]

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 2: A Cython Interface

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

What is Cython?

Page 3: A Cython Interface

What is Cython?

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

C

Java

C#

Python

Page 4: A Cython Interface

CPython: is the primary Python execution environment

CPython: provides a C-level interface into Python,

Python/C API

used to wrap many external C/C++ libraries However, Python/C API is non-trivial, especially if more fluent in scripting languages like Python than in low-level language like C

CPython

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 5: A Cython Interface

CPython: is the primary Python execution environment

CPython: provides a C-level interface into Python,

Python/C API

used to wrap many external C/C++ libraries However, Python/C API is non-trivial, especially if more fluent in scripting languages like Python than in low-level language like C

CPython

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Cython uses the Python/C API extensivelyCython is not another implementation of Python, although it needs the CPython runtime

Page 6: A Cython Interface

Programming language based on Python, but with C/C++ static type declarations

Cython

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

The cython compiler translates Cython source code into optimized C/C++ code (Python/C API), which in turn is compiled to a module

Cython has the ability to call directly into C/C++ libraries

Page 7: A Cython Interface

Cython

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Fully-fledged language super-set of Python plus C-like constructs

Keeps abreast with developments in Python Supports data types (e.g., memoryview) that

implement the new Python protocol buffer, allowing data to be shared without copying

Page 8: A Cython Interface

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

A programming language with (extended) Python syntax

Performance level of C!

Cython

Page 9: A Cython Interface

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Use Case 1:To Speed up Python Code

Page 10: A Cython Interface

Cython can compile regular Python code to C code

Cython Example

from math import sin

def fn(x):return sin(x**2)

def integrate_fn(a,b,N):s=0dx=(b-a)/Nfor i in range(N):s+=fn(a+i*dx)return s*dx

from math import sin

def fn(x):return sin(x**2)

def integrate_fn(a,b,N):s=0dx=(b-a)/Nfor i in range(N):s+=fn(a+i*dx)return s*dx

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Python 3.5 Cython 0.23.4

Page 11: A Cython Interface

Cython can compile regular Python code to C code, and gain major speed improvements from optional static type declarations

Cython Example

from math import sin

def fn(x):return sin(x**2)

def integrate_fn(a,b,N):s=0dx=(b-a)/Nfor i in range(N):s+=fn(a+i*dx)return s*dx

from math import sin

cdef double fn(double x):return sin(x**2)

def integrate_fn(double a, double b, int N):

cdef double s, dx s=0dx=(b-a)/Nfor i in range(N):

s+=fn(a+i*dx)return s*dx

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Python 3.5 Cython 0.23.4

Page 12: A Cython Interface

Cython can compile regular Python code to C code, and so gain major speed improvements from external declarations

Cython Example

from math import sin

def fn(x):return sin(x**2)

def integrate_fn(a,b,N):s=0dx=(b-a)/Nfor i in range(N):s+=fn(a+i*dx)return s*dx

cdef extern from “math.h”double sin(double)

cdef double fn(double x):return sin(x**2)

def integrate_fn(double a, double b, int N):

cdef double s, dx s=0dx=(b-a)/Nfor i in range(N):

s+=fn(a+i*dx)return s*dx

Python 3.5 Cython 0.23.4

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 13: A Cython Interface

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Use Case 2:To Interface C/C++ Libraries

Page 14: A Cython Interface

CAFE, A C++ Channel Access LibraryCAFE is a modern, C++ interface to the native EPICS Channel Access (CA) C client library that provides a concise, complete, and clean interface with minimal details of the low-level CA implementation propagating to the user. It places careful attention to:

connection management and memory optimization, particularly as connections are broken and restored

separation between data acquisition and its presentation strategies for converting between requested and native types caching of data related to the channel and its current state aggregation of requests to enhance performance capturing and reporting errors with integrity adaptive correction procedures, e.g., network timeouts temporal decoupling from CA servers !!!!

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 15: A Cython Interface

Bindings to scripting and domain-specific languages simplified

Inherent convenience of maintaining a single ca interface code

New CA functionalities from future EPICS releases need only be integrated in a single repository

A uniform response to error conditions will help identify problems and increase recovery time

In-house CA expertise ensures a quick response to user needs and problem solving

Advantages of the CAFE library approach

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 16: A Cython Interface

CAFE Extensions

CA Boost XML

C++ CAFE

mocha PyCafeDDS

PythonMATLABC++/Qt

Third Party Layer

Adapter Layer

Application Layer

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

ROOT

CA Interface

Page 17: A Cython Interface

PyCafe.pyd

Cython wrapping around CAFE C++ library

CyCafe

def getPV(self, handlePV, str dt=‘native’):

%validate input, i.e., pvName or handle…cdef int statuscdef PVDataHolder pvdata = PVDataHolder(1)

with no gil: status=self._c_cafe.get(handle, pvdata)

%throw exception if status !=ICAFE_NORMAL else

return PVDataHoldertoStruct(pvdata, dt)

Cython

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

PyCafe.pyx

Page 18: A Cython Interface

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

setup.py

cythonize

C++ compilation

application.py

PyCafe.cpp

PyCafe.so

import

distutils

PyCafe Extension Module

PyCafe.pydfrom distutils.core import setupfrom distutils.extension import Extensionfrom Cython.Build import cythonizefrom numpy import get_includesetup(

ext_modules=cythonize([Extension('PyCafe',['PyCafe.pyx'],

language=‘c++’,include_dirs=[..., get_include()],library_dirs=[...],libraries=['ca','Com','dl','cafe'])

],annotate=True,compiler_directives={

'embedsignature':False,'language_level':3, 'c_string_type':'str','c_string_encoding':'ascii','py2_import':False, 'warning_errors':False,'warn_unreachable':False, 'noncheck':False,'boundscheck':False,'wraparound':False}

))

setup.py

PyCafe.pyx

Page 19: A Cython Interface

PyCafe Simple single channel operations

Waveforms and arrays (memoryview data types)

Multiple scalar operations, i.e., simultaneous operations

on several PVs with scalar values

Multiple compound operations, i.e., simultaneous operations on

several PVs with either scalar values or arrays

Synchronous group operations (user or externally defined)

Asynchronous interactions and retrieving data from cache

Monitors, either with or without user supplied callbacks

Control system parameters, i.e., operating limits, engineering units

Specialized methods, e.g., match, setAndMatch, wishlist …

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 20: A Cython Interface

Arrays and memoryview

Retrieving a waveform as a numpy.ndarray

value = cafe.getArray ( ⟨handlePV⟩, art=‘numpy’, [dt=‘native’])

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Retrieving a waveform as a memoryview

value = cafe.getArray ( ⟨handlePV⟩, art=‘memoryview’, [dt=‘native’])

Python objects that implement the new buffer protocol, e.g., bytes,bytearray, numpy.ndarray, array.array, can share their data without copying. The C level buffer interface may further be exposed as a memoryview object that, supports slicing and indexing

Page 21: A Cython Interface

Arrays and memoryview

Retrieving a waveform as a numpy.ndarray

value = cafe.getArray ( ⟨handlePV⟩, art=‘numpy’, [dt=‘native’])

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Retrieving a waveform as a memoryview

value = cafe.getArray ( ⟨handlePV⟩, art=‘memoryview’, [dt=‘native’])

Page 22: A Cython Interface

Monitors and CallbacksInitiating a monitor with a user supplied callback

def py_cb(handle):

pvData=cafe.getPVCache(handle, [dt=‘native’])

# 'set' operations, including those on other# handles, are permitted. # Invoke Qt signal here to update Qt Widget…return

monitorID = cafe.monitorStart (⟨handlePV⟩, cb=py_cb)

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Only the handle (i.e., object reference) is, and need be, reported back to the callback function

Page 23: A Cython Interface

Synchronous Group Operations

setList = [ 23.3, ‘on’, [1,2,3]]

status, statusList= setGroup (‘groupName’, setList)

% Returns List of values; a waveform is represented as a List within a Listvalues, overallStatus, statusList

= getGroup (‘groupName’, dt='native')

% Retrieves a pvgroup objectpvgroup = getPVGroup (‘groupName’, dt='native')

% Example for PVs with various record typesPVNames = ['PV:AI','PV:MBBI','PV:WF']cafe.defineGroup ('groupName', PVNames)

A channel access synchronous group operation

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 24: A Cython Interface

Application developed and tested on RF Calibration Orbit Tool and Orbit Feedback Linac Energy Manager and

Energy Feedback Beam Based Alignments (BBA)

Quad vs BPM Undulator section BPMs

SwissFEL HLA Development [Masamitsu Aiba]

~ 104 EPICS soft channels

Virtual Accelerator

Page 25: A Cython Interface

SwissFEL HLA Development

Applications proven on Virtual Accelerator, readily applied for SwissFEL commissioning

[Masamitsu Aiba]

Virtual Accelerator Real Accelerator

Page 26: A Cython Interface

Channel access requirements already met by the extensiveinterface provided by CAFE (hard work already done!)

Cython makes binding to external C++ libraries easy, interfaces can be simplified, customized, pythonized as they are wrapped.

Performance improvement for single scalar read:

PyCafe is ~ 400% times faster than Python/API

PyCafe is ~ 40% times faster than ctypes

Python, Cython performance difference vanishes for large waveforms (106 elements) but using memoryview gives a factor of 2 improvement

PyCafe Highlights

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

Page 27: A Cython Interface

PyCafe: A Python/Cython Interface to CAFE

https://ados.web.psi.ch/cafe/

[or google: cycafe]

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

PyCafe

Page 28: A Cython Interface

PCaPAC'16, Campinas, Brazil, Oct. 2016, paper WEUIPLCO04

THANK YOU FOR YOUR ATTENTION

2016 -- Campinas recebe uma importante conferência international sobre sistemas de controle para aceleradores de partículas. James Rezende Peton, do LNLS/CNPEM, é o presidente do comitte local de organização.