SlideShare a Scribd company logo
Updating gopy to support Python3 and PyPy
Dong-hee Na, Chungnam National University, Daejeon, Korea
Mentors: Sebastien Binet, Alexandre Claude
13. June. 2017
Main topics
• What is gopy?
• Why Go ..?
• Performance benchmark
• Support CFFI backend
What is gopy?
• gopy is heavily inspired from gomobile.
• gopy is a set of packages and build tools for using Go from python
interpreters.
• Generates (and compiles) a Python extension module from a Go package.
( https://github.com/golang/mobile)
• LHC: 90% of C++ codes and a bit of Python codes for steering.
Why Go? – Software at CERN
0
100
200
300
400
500
600
700
Users in CERN
Know_The_User_Communities_Results_Summary.pdf
Why Go …?
Powerful performance
Pros
Long long compiling times
Cons
Manual memory Management
Pros
Cons
- Easy to install libraries
with ‘go get’.
- Rich standard libraries.
- Built-in concurrency
support
- Compile fast.
- Elegant and simple built-in
build system.
- Garbage-collected
language
- Error detecting is
awesome.
- Learning curve is low
- Powerful performance.Learning curve is high
Global Interpreter Lock
Interpreter based
Library installation is easy
Learning curve is low
Useful Scientific Packages
Compiled language
gopy: Calculation of Pi Using the Monte Carlo Method
func monte_carlo_pi(reps int, result *int, wait *sync.WaitGroup) {
var x, y float64
count := 0
seed := rand.NewSource(time.Now().UnixNano())
random := rand.New(seed)
for i := 0; i < reps; i++ {
x = random.Float64() * 1.0
y = random.Float64() * 1.0
if num := math.Sqrt(x*x + y*y); num < 1.0 {
count++
}
}
*result = count
wait.Done()
}
http://tstra.us/code/goPi/
func GetPI(samples int) float64 {
cores := runtime.NumCPU()
runtime.GOMAXPROCS(cores)
var wait sync.WaitGroup
counts := make([]int, cores)
wait.Add(cores)
for i := 0; i < cores; i++ {
go monte_carlo_pi(samples/cores, &counts[i], &wait)
}
wait.Wait()
total := 0
for i := 0; i < cores; i++ {
total += counts[i]
}
pi := (float64(total) / float64(samples)) * 4
return pi
}
gopy vs Python:
Calculation of Pi Using the Monte Carlo Method
• gopy is very easy to install.
• Using a go get is all you have to do!
$> go get -u -v github.com/go-python/gopy github.com/go-python/gopy
(download) github.com/gonuts/commander (download) github.com/gonuts/flag
(download) github.com/gonuts/flag github.com/go-python/gopy/bind
github.com/gonuts/commander github.com/go-python/gopy
gopy vs Python:
Calculation of Pi Using the Monte Carlo Method
def monte_carlo_pi_part(n):
count = 0
for i in range(int(n)):
x=random.random()
y=random.random()
# if it is within the unit circle
if x*x + y*y <= 1:
count=count+1
#return
return count
def GetPI(n):
np = multiprocessing.cpu_count()
part_count=[n/np for i in range(np)]
pool = Pool(processes=np)
count=pool.map(monte_carlo_pi_part, part_count)
return sum(count)/(n*1.0)*4
https://gist.github.com/amitsaha/2036026
if __name__ == '__main__':
n = 100000
py_start = time.time()
result = GetPI(n)
py_end = time.time()
print("Python result: %f time_elapsed: %f" % (result, py_end-py_start))
go_start = time.time()
result = calculatePi.GetPI(n)
go_end = time.time()
print("gopy result: %f time_elapsed: %f" %(result, go_end-go_start))
gopy vs Python:
Calculation of Pi Using the Monte Carlo Method
• gopy helps to use Go’s useful features on the Python interpreter.
• End-user can easily run Go codes on the Python interpreter.
root@180a6474ebba:~/test# gopy bind github.com/go-python/gopy/_examples/calculatePi
2017/06/11 06:03:21 work: /tmp/gopy-546154656
root@180a6474ebba:~/test# python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import calculatePi
>>> calculatePi.GetPI(10000)
3.1564
root@180a6474ebba:~/test# python pi_mp.py
n: 100000
Python result: 3.145880 time_elapsed: 0.030254
gopy result: 3.137400 time_elapsed: 0.002960 ß Much Faster!!!
gopy: Limitation
• gopy does not supports CPython3 nor PyPy.
• Many go’s implementations/features are not yet implemented in gopy
root@180a6474ebba:~/test# pypy pi_mp.py
Traceback (most recent call last):
File "pi_mp.py", line 12, in <module>
import calculatePi
ImportError: No module named calculatePi
root@180a6474ebba:~/test# python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import calculatePi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_calculatePi)
GSoC Project: Support Python3 and PyPy
• Need to support Py2/3 and PyPy.
• PyPy’s implementation is not strictly 100% compatible with Ctypes.
• CFFI is a good choice to support various python compilers.
• CFFI interacts with almost any C code from Python.
root@180a6474ebba:~/test# gopy bind --lang=cffi github.com/go-
python/gopy/_examples/calculatePi
2017/06/11 06:06:46 work: /tmp/gopy-214312004
root@180a6474ebba:~/test# python pi_mp.py
n: 100000
Python result: 3.135280 time_elapsed: 0.024898
gopy result: 3.143200 time_elapsed: 0.006861 ß Much Faster!!!
root@180a6474ebba:~/test# pypy pi_mp.py
n: 100000
Python result: 3.147240 time_elapsed: 0.023687
gopy result: 3.145040 time_elapsed: 0.017225 ß Much Faster!!!
root@180a6474ebba:~/test# python3 pi_mp.py
Python result: 3.136560 time_elapsed: 0.037512
gopy result: 3.143920 time_elapsed: 0.003738 <- Much Faster
GSoC Project: Support Python3 and PyPy
1. Inspects a Go package
2. Extracts the exported types, funcs, vars and consts
3. Creates a Go package that cgo exports the exported entities
4. Designates which interface should be exported to CFFI.
ffi.cdef("""
typedef signed char GoInt8;
typedef unsigned char GoUint8;
.
.
.
extern void cgo_pkg_calculatePi_init();
extern GoFloat64 cgo_func_calculatePi_GetPI(GoInt p0);
""")
GSoC Project: Support Python3 and PyPy
1. Inspects a Go package
2. Extracts the exported types, funcs, vars and consts
3. Creates a Go package that cgo exports the exported entities
4. Designates which interface should be exported to CFFI.
5. Creates a wrapping codes for CFFI by Python.
# pythonization of: calculatePi.GetPI
def GetPI(samples):
c_samples = _cffi_helper.cffi_cnv_py2c_int(samples)
cret = _cffi_helper.lib.cgo_func_calculatePi_GetPI(c_samples)
ret = _cffi_helper.cffi_cnv_c2py_float64(cret)
return ret
GSoC Project: Support Python3 and PyPy
0
0.02
0.04
0.06
0.08
0.1
0.12
0.14
10 100 1000 10000 100000 1000000
Timeelapsed
Sample counts
CPython2 VS gopy
CPython2 gopy
GSoC Project: Support Python3 and PyPy
0
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
10 100 1000 10000 100000 1000000
Timeelapsed
Sample counts
PyPy VS gopy
pypy gopy
GSoC Project: Support Python3 and PyPy
0
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
10 100 1000 10000 100000 1000000
Timeelapsed
Sample counts
Cpython3 VS gopy
CPython3 gopy
GSoC Project: Project Plan
• Migrate into CFFI library to gencffi*.go for current implementation.
• Implement wrapping of functions with builtin arguments.
• Implement wrapping of functions with slices/arrays of builtin arguments.
• Implement wrapping of functions with user types.
• Detect functions returning a Go error and make them pythonic
(raising an Exception).
• Implement wrapping of user types / Go maps / Go interfaces.
• Write documents for English and Korean.
GSoC Project: Goal
• Able to use Go’s awesome features on Python 2/3 and PyPy.
Newcomers are always welcomed
• https://github.com/go-python/gopy
• https://groups.google.com/forum/#!forum/go-python
• https://gophers.slack.com/messages/go-python
Thank you
donghee.na92@gmail.com

More Related Content

What's hot

Go. why it goes v2
Go. why it goes v2Go. why it goes v2
Go. why it goes v2
Sergey Pichkurov
 
Python on a chip
Python on a chipPython on a chip
Python on a chip
stoggi
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
GlobalLogic Ukraine
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
Yi-Lung Tsai
 
Boost.Python - domesticating the snake
Boost.Python - domesticating the snakeBoost.Python - domesticating the snake
Boost.Python - domesticating the snake
Sławomir Zborowski
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
Sergey Pichkurov
 
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike MullerShared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
PyData
 
Easy native wrappers with SWIG
Easy native wrappers with SWIGEasy native wrappers with SWIG
Easy native wrappers with SWIG
Javier Arturo Rodríguez
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
David Beazley (Dabeaz LLC)
 
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija SiskoTrivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis
 
carrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-API
Yoni Davidson
 
CGo for fun and profit
CGo for fun and profitCGo for fun and profit
CGo for fun and profit
Liz Frost
 
Python Developer Certification
Python Developer CertificationPython Developer Certification
Python Developer Certification
Vskills
 
SWIG Hello World
SWIG Hello WorldSWIG Hello World
SWIG Hello World
e8xu
 
Про асинхронность / Максим Щепелин / Web Developer Wargaming
Про асинхронность / Максим Щепелин / Web Developer WargamingПро асинхронность / Максим Щепелин / Web Developer Wargaming
Про асинхронность / Максим Щепелин / Web Developer Wargaming
Python Meetup
 
Reversing the dropbox client on windows
Reversing the dropbox client on windowsReversing the dropbox client on windows
Reversing the dropbox client on windows
extremecoders
 
Python on pi
Python on piPython on pi
Python on pi
swee meng ng
 
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
Yusuke Izawa
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
David Beazley (Dabeaz LLC)
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Yusuke Izawa
 

What's hot (20)

Go. why it goes v2
Go. why it goes v2Go. why it goes v2
Go. why it goes v2
 
Python on a chip
Python on a chipPython on a chip
Python on a chip
 
Boost.Python: C++ and Python Integration
Boost.Python: C++ and Python IntegrationBoost.Python: C++ and Python Integration
Boost.Python: C++ and Python Integration
 
Threads and Callbacks for Embedded Python
Threads and Callbacks for Embedded PythonThreads and Callbacks for Embedded Python
Threads and Callbacks for Embedded Python
 
Boost.Python - domesticating the snake
Boost.Python - domesticating the snakeBoost.Python - domesticating the snake
Boost.Python - domesticating the snake
 
Go. Why it goes
Go. Why it goesGo. Why it goes
Go. Why it goes
 
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike MullerShared Memory Parallelism with Python by Dr.-Ing Mike Muller
Shared Memory Parallelism with Python by Dr.-Ing Mike Muller
 
Easy native wrappers with SWIG
Easy native wrappers with SWIGEasy native wrappers with SWIG
Easy native wrappers with SWIG
 
Interfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIGInterfacing C/C++ and Python with SWIG
Interfacing C/C++ and Python with SWIG
 
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija SiskoTrivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
Trivadis TechEvent 2016 Go - The Cloud Programming Language by Andija Sisko
 
carrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-APIcarrow - Go bindings to Apache Arrow via C++-API
carrow - Go bindings to Apache Arrow via C++-API
 
CGo for fun and profit
CGo for fun and profitCGo for fun and profit
CGo for fun and profit
 
Python Developer Certification
Python Developer CertificationPython Developer Certification
Python Developer Certification
 
SWIG Hello World
SWIG Hello WorldSWIG Hello World
SWIG Hello World
 
Про асинхронность / Максим Щепелин / Web Developer Wargaming
Про асинхронность / Максим Щепелин / Web Developer WargamingПро асинхронность / Максим Щепелин / Web Developer Wargaming
Про асинхронность / Максим Щепелин / Web Developer Wargaming
 
Reversing the dropbox client on windows
Reversing the dropbox client on windowsReversing the dropbox client on windows
Reversing the dropbox client on windows
 
Python on pi
Python on piPython on pi
Python on pi
 
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
Stack Hybridization: A Mechanism for Bridging Two Compilation Strategies in a...
 
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python ExceptionsWAD : A Module for Converting Fatal Extension Errors into Python Exceptions
WAD : A Module for Converting Fatal Extension Errors into Python Exceptions
 
Two-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One EngineTwo-level Just-in-Time Compilation with One Interpreter and One Engine
Two-level Just-in-Time Compilation with One Interpreter and One Engine
 

Similar to [GSoC 2017] gopy: Updating gopy to support Python3 and PyPy

PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
Henry Schreiner
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io
 
Startup Camp - Git, Python, Django session
Startup Camp - Git, Python, Django sessionStartup Camp - Git, Python, Django session
Startup Camp - Git, Python, Django session
Juraj Michálek
 
SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...
SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...
SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...
Henry Schreiner
 
Debugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDBDebugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDB
bmbouter
 
Writing Fast Code - PyCon HK 2015
Writing Fast Code - PyCon HK 2015Writing Fast Code - PyCon HK 2015
Writing Fast Code - PyCon HK 2015
Younggun Kim
 
PyPy London Demo Evening 2013
PyPy London Demo Evening 2013PyPy London Demo Evening 2013
PyPy London Demo Evening 2013
Carl Friedrich Bolz
 
Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015
Younggun Kim
 
Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024
Henry Schreiner
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meeting
Henry Schreiner
 
Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
Henry Schreiner
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
Henry Schreiner
 
Pybind11 - SciPy 2021
Pybind11 - SciPy 2021Pybind11 - SciPy 2021
Pybind11 - SciPy 2021
Henry Schreiner
 
GoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPHGoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPH
Gautam Rege
 
GitGot: The Swiss Army Chainsaw of Git Repo Management
GitGot: The Swiss Army Chainsaw of Git Repo ManagementGitGot: The Swiss Army Chainsaw of Git Repo Management
GitGot: The Swiss Army Chainsaw of Git Repo Management
John Anderson
 
Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023
Henry Schreiner
 
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
Pôle Systematic Paris-Region
 
CHEP 2018: A Python upgrade to the GooFit package for parallel fitting
CHEP 2018: A Python upgrade to the GooFit package for parallel fittingCHEP 2018: A Python upgrade to the GooFit package for parallel fitting
CHEP 2018: A Python upgrade to the GooFit package for parallel fitting
Henry Schreiner
 
Bind Python and C @ COSCUP 2015
Bind Python and C @ COSCUP 2015Bind Python and C @ COSCUP 2015
Bind Python and C @ COSCUP 2015
Jian-Hong Pan
 
PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
Eran Shlomo
 

Similar to [GSoC 2017] gopy: Updating gopy to support Python3 and PyPy (20)

PyCon2022 - Building Python Extensions
PyCon2022 - Building Python ExtensionsPyCon2022 - Building Python Extensions
PyCon2022 - Building Python Extensions
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Startup Camp - Git, Python, Django session
Startup Camp - Git, Python, Django sessionStartup Camp - Git, Python, Django session
Startup Camp - Git, Python, Django session
 
SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...
SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...
SciPy22 - Building binary extensions with pybind11, scikit build, and cibuild...
 
Debugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDBDebugging Hung Python Processes With GDB
Debugging Hung Python Processes With GDB
 
Writing Fast Code - PyCon HK 2015
Writing Fast Code - PyCon HK 2015Writing Fast Code - PyCon HK 2015
Writing Fast Code - PyCon HK 2015
 
PyPy London Demo Evening 2013
PyPy London Demo Evening 2013PyPy London Demo Evening 2013
PyPy London Demo Evening 2013
 
Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015Writing Fast Code (JP) - PyCon JP 2015
Writing Fast Code (JP) - PyCon JP 2015
 
Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024Software Quality Assurance Tooling - Wintersession 2024
Software Quality Assurance Tooling - Wintersession 2024
 
Digital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meetingDigital RSE: automated code quality checks - RSE group meeting
Digital RSE: automated code quality checks - RSE group meeting
 
Princeton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance ToolingPrinceton Wintersession: Software Quality Assurance Tooling
Princeton Wintersession: Software Quality Assurance Tooling
 
Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024Modern binary build systems - PyCon 2024
Modern binary build systems - PyCon 2024
 
Pybind11 - SciPy 2021
Pybind11 - SciPy 2021Pybind11 - SciPy 2021
Pybind11 - SciPy 2021
 
GoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPHGoFFIng around with Ruby #RubyConfPH
GoFFIng around with Ruby #RubyConfPH
 
GitGot: The Swiss Army Chainsaw of Git Repo Management
GitGot: The Swiss Army Chainsaw of Git Repo ManagementGitGot: The Swiss Army Chainsaw of Git Repo Management
GitGot: The Swiss Army Chainsaw of Git Repo Management
 
Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023Software Quality Assurance Tooling 2023
Software Quality Assurance Tooling 2023
 
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
PyParis 2017 / Writing a C Python extension in 2017, Jean-Baptiste Aviat
 
CHEP 2018: A Python upgrade to the GooFit package for parallel fitting
CHEP 2018: A Python upgrade to the GooFit package for parallel fittingCHEP 2018: A Python upgrade to the GooFit package for parallel fitting
CHEP 2018: A Python upgrade to the GooFit package for parallel fitting
 
Bind Python and C @ COSCUP 2015
Bind Python and C @ COSCUP 2015Bind Python and C @ COSCUP 2015
Bind Python and C @ COSCUP 2015
 
PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
 

Recently uploaded

AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
drshikhapandey2022
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
Lubi Valves
 
Beckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview PresentationBeckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview Presentation
VanTuDuong1
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
pvpriya2
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
Kamal Acharya
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
MadhavJungKarki
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
uqyfuc
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
harshapolam10
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
snaprevwdev
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
sydezfe
 
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
felixwold
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
wafawafa52
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
DharmaBanothu
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
ijseajournal
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
ydzowc
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
Roger Rozario
 

Recently uploaded (20)

AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
 
Beckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview PresentationBeckhoff Programmable Logic Control Overview Presentation
Beckhoff Programmable Logic Control Overview Presentation
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
Blood finder application project report (1).pdf
Blood finder application project report (1).pdfBlood finder application project report (1).pdf
Blood finder application project report (1).pdf
 
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
1FIDIC-CONSTRUCTION-CONTRACT-2ND-ED-2017-RED-BOOK.pdf
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
SCALING OF MOS CIRCUITS m .pptx
SCALING OF MOS CIRCUITS m                 .pptxSCALING OF MOS CIRCUITS m                 .pptx
SCALING OF MOS CIRCUITS m .pptx
 
openshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoinopenshift technical overview - Flow of openshift containerisatoin
openshift technical overview - Flow of openshift containerisatoin
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
 
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
A high-Speed Communication System is based on the Design of a Bi-NoC Router, ...
 
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...Call For Paper -3rd International Conference on Artificial Intelligence Advan...
Call For Paper -3rd International Conference on Artificial Intelligence Advan...
 
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
原版制作(Humboldt毕业证书)柏林大学毕业证学位证一模一样
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
Transformers design and coooling methods
Transformers design and coooling methodsTransformers design and coooling methods
Transformers design and coooling methods
 

[GSoC 2017] gopy: Updating gopy to support Python3 and PyPy

  • 1. Updating gopy to support Python3 and PyPy Dong-hee Na, Chungnam National University, Daejeon, Korea Mentors: Sebastien Binet, Alexandre Claude 13. June. 2017
  • 2. Main topics • What is gopy? • Why Go ..? • Performance benchmark • Support CFFI backend
  • 3. What is gopy? • gopy is heavily inspired from gomobile. • gopy is a set of packages and build tools for using Go from python interpreters. • Generates (and compiles) a Python extension module from a Go package. ( https://github.com/golang/mobile)
  • 4. • LHC: 90% of C++ codes and a bit of Python codes for steering. Why Go? – Software at CERN 0 100 200 300 400 500 600 700 Users in CERN Know_The_User_Communities_Results_Summary.pdf
  • 5. Why Go …? Powerful performance Pros Long long compiling times Cons Manual memory Management Pros Cons - Easy to install libraries with ‘go get’. - Rich standard libraries. - Built-in concurrency support - Compile fast. - Elegant and simple built-in build system. - Garbage-collected language - Error detecting is awesome. - Learning curve is low - Powerful performance.Learning curve is high Global Interpreter Lock Interpreter based Library installation is easy Learning curve is low Useful Scientific Packages Compiled language
  • 6. gopy: Calculation of Pi Using the Monte Carlo Method func monte_carlo_pi(reps int, result *int, wait *sync.WaitGroup) { var x, y float64 count := 0 seed := rand.NewSource(time.Now().UnixNano()) random := rand.New(seed) for i := 0; i < reps; i++ { x = random.Float64() * 1.0 y = random.Float64() * 1.0 if num := math.Sqrt(x*x + y*y); num < 1.0 { count++ } } *result = count wait.Done() } http://tstra.us/code/goPi/ func GetPI(samples int) float64 { cores := runtime.NumCPU() runtime.GOMAXPROCS(cores) var wait sync.WaitGroup counts := make([]int, cores) wait.Add(cores) for i := 0; i < cores; i++ { go monte_carlo_pi(samples/cores, &counts[i], &wait) } wait.Wait() total := 0 for i := 0; i < cores; i++ { total += counts[i] } pi := (float64(total) / float64(samples)) * 4 return pi }
  • 7. gopy vs Python: Calculation of Pi Using the Monte Carlo Method • gopy is very easy to install. • Using a go get is all you have to do! $> go get -u -v github.com/go-python/gopy github.com/go-python/gopy (download) github.com/gonuts/commander (download) github.com/gonuts/flag (download) github.com/gonuts/flag github.com/go-python/gopy/bind github.com/gonuts/commander github.com/go-python/gopy
  • 8. gopy vs Python: Calculation of Pi Using the Monte Carlo Method def monte_carlo_pi_part(n): count = 0 for i in range(int(n)): x=random.random() y=random.random() # if it is within the unit circle if x*x + y*y <= 1: count=count+1 #return return count def GetPI(n): np = multiprocessing.cpu_count() part_count=[n/np for i in range(np)] pool = Pool(processes=np) count=pool.map(monte_carlo_pi_part, part_count) return sum(count)/(n*1.0)*4 https://gist.github.com/amitsaha/2036026 if __name__ == '__main__': n = 100000 py_start = time.time() result = GetPI(n) py_end = time.time() print("Python result: %f time_elapsed: %f" % (result, py_end-py_start)) go_start = time.time() result = calculatePi.GetPI(n) go_end = time.time() print("gopy result: %f time_elapsed: %f" %(result, go_end-go_start))
  • 9. gopy vs Python: Calculation of Pi Using the Monte Carlo Method • gopy helps to use Go’s useful features on the Python interpreter. • End-user can easily run Go codes on the Python interpreter. root@180a6474ebba:~/test# gopy bind github.com/go-python/gopy/_examples/calculatePi 2017/06/11 06:03:21 work: /tmp/gopy-546154656 root@180a6474ebba:~/test# python Python 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import calculatePi >>> calculatePi.GetPI(10000) 3.1564 root@180a6474ebba:~/test# python pi_mp.py n: 100000 Python result: 3.145880 time_elapsed: 0.030254 gopy result: 3.137400 time_elapsed: 0.002960 ß Much Faster!!!
  • 10. gopy: Limitation • gopy does not supports CPython3 nor PyPy. • Many go’s implementations/features are not yet implemented in gopy root@180a6474ebba:~/test# pypy pi_mp.py Traceback (most recent call last): File "pi_mp.py", line 12, in <module> import calculatePi ImportError: No module named calculatePi root@180a6474ebba:~/test# python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import calculatePi Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dynamic module does not define module export function (PyInit_calculatePi)
  • 11. GSoC Project: Support Python3 and PyPy • Need to support Py2/3 and PyPy. • PyPy’s implementation is not strictly 100% compatible with Ctypes. • CFFI is a good choice to support various python compilers. • CFFI interacts with almost any C code from Python. root@180a6474ebba:~/test# gopy bind --lang=cffi github.com/go- python/gopy/_examples/calculatePi 2017/06/11 06:06:46 work: /tmp/gopy-214312004 root@180a6474ebba:~/test# python pi_mp.py n: 100000 Python result: 3.135280 time_elapsed: 0.024898 gopy result: 3.143200 time_elapsed: 0.006861 ß Much Faster!!! root@180a6474ebba:~/test# pypy pi_mp.py n: 100000 Python result: 3.147240 time_elapsed: 0.023687 gopy result: 3.145040 time_elapsed: 0.017225 ß Much Faster!!! root@180a6474ebba:~/test# python3 pi_mp.py Python result: 3.136560 time_elapsed: 0.037512 gopy result: 3.143920 time_elapsed: 0.003738 <- Much Faster
  • 12. GSoC Project: Support Python3 and PyPy 1. Inspects a Go package 2. Extracts the exported types, funcs, vars and consts 3. Creates a Go package that cgo exports the exported entities 4. Designates which interface should be exported to CFFI. ffi.cdef(""" typedef signed char GoInt8; typedef unsigned char GoUint8; . . . extern void cgo_pkg_calculatePi_init(); extern GoFloat64 cgo_func_calculatePi_GetPI(GoInt p0); """)
  • 13. GSoC Project: Support Python3 and PyPy 1. Inspects a Go package 2. Extracts the exported types, funcs, vars and consts 3. Creates a Go package that cgo exports the exported entities 4. Designates which interface should be exported to CFFI. 5. Creates a wrapping codes for CFFI by Python. # pythonization of: calculatePi.GetPI def GetPI(samples): c_samples = _cffi_helper.cffi_cnv_py2c_int(samples) cret = _cffi_helper.lib.cgo_func_calculatePi_GetPI(c_samples) ret = _cffi_helper.cffi_cnv_c2py_float64(cret) return ret
  • 14. GSoC Project: Support Python3 and PyPy 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 10 100 1000 10000 100000 1000000 Timeelapsed Sample counts CPython2 VS gopy CPython2 gopy
  • 15. GSoC Project: Support Python3 and PyPy 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 10 100 1000 10000 100000 1000000 Timeelapsed Sample counts PyPy VS gopy pypy gopy
  • 16. GSoC Project: Support Python3 and PyPy 0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 10 100 1000 10000 100000 1000000 Timeelapsed Sample counts Cpython3 VS gopy CPython3 gopy
  • 17. GSoC Project: Project Plan • Migrate into CFFI library to gencffi*.go for current implementation. • Implement wrapping of functions with builtin arguments. • Implement wrapping of functions with slices/arrays of builtin arguments. • Implement wrapping of functions with user types. • Detect functions returning a Go error and make them pythonic (raising an Exception). • Implement wrapping of user types / Go maps / Go interfaces. • Write documents for English and Korean.
  • 18. GSoC Project: Goal • Able to use Go’s awesome features on Python 2/3 and PyPy.
  • 19. Newcomers are always welcomed • https://github.com/go-python/gopy • https://groups.google.com/forum/#!forum/go-python • https://gophers.slack.com/messages/go-python