Matematikai számolások HPC környezetben

HPC Szakmai Konzultációs Fórum
2017 április 24

A SageMath programcsomag
William Stein 2005-ben kezdte fejleszteni a Sage matematikai
programcsomagot.
System for Arithmetic Geometry Experimentation
A fejlesztés során a rendszerbe több népszerű nyı́lt forráskódú
matematikai programcsomag/könyvtár integrálásra került:• eclib, Flint, mwrank, NTL, PARI
• Maxima, Pynac, Sympy• Givaro, GMP, MPFI, MPFR
• PolyBoRi, SINGULAR• GAP, LinBox, NetworkX, R• Python, Cython
1

A SageMath programcsomag
SageMath a matematikán túl: néhány csomag biológusoknak,fizikusoknak, vegyészeknek.
benzenebiopython
brianbuckygenJmolnonisomorphic fusenes and benzenoids
computational molecular biology
simulator for spiking neural networks
nonisomorphic fullerenes
three-dimensional chemical structures

2

A SageMath arcai
Parancssor:

3

A SageMath arcai
Sage-notebook:

4

A SageMath arcai
SageMathCloud:5

A SageMath arcai
SageMathCloud:6A SageMath arcai
Jupyter-notebook:

7

A SageMath arcai
Animált ábrák:

8

A SageMath arcai
Interaktı́v applikáció:9SageMath-szuperszámı́tógép-oktatás?
https://cloud.sagemath.com/policies/pricing.html

10SageMath-szuperszámı́tógép-oktatás?

11

SageMath-szuperszámı́tógép-oktatás?

12SageMath-szuperszámı́tógép-oktatás?

13

SageMath-szuperszámı́tógép-oktatás?

14

Jupyter-notebook nbgrader
http://nbgrader.readthedocs.io/15

Jupyterhub
https://jupyterhub.readthedocs.io

16

Jupyterhub
https://jupyterhub.readthedocs.io

17

JupyterLab
https://jupyterlab-tutorial.readthedocs.io/18

JupyterLab
JupyterLab használatban:

19

Párhuzamos számı́tások SageMathban
Fizikai korlátok:• Budapest 2 × 12 = 24 mag• Budapest2 2 × 10 = 20 mag• Szeged 4 × 12 = 48 mag• Debrecen 2 × 6 = 12 mag• Pécs 192 × 6 = 1152 mag• Miskolc 44 × 8 = 352 mag
SageMathban maximum ennyi magot tudunk használni az adottkörnyezetben.
sage.parallel.ncpus.ncpus()

20

Párhuzamos számı́tások SageMathban@parallel
def f(n): return n^2time sorted(list(f([1..20])))[(((1,), {}), 1),(((2,), {}), 4),(((3,), {}), 9),(((4,), {}), 16),(((5,), {}), 25),(((6,), {}), 36),(((7,), {}), 49),(((8,), {}), 64),(((9,), {}), 81),(((10,), {}), 100)]
Time: CPU 0.04 s, Wall: 0.04 stime print [k^2 for k in [1..10]][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Time: CPU 0.00 s, Wall: 0.00 s

21

Párhuzamos számı́tások SageMathban@parallel(ncpus=3, timeout=1)
def fac(n): return factor(2^n-1)

for X, Y in sorted(list(fac([200..205]))): print((X,Y))(((200,), {}), 3 * 5^3 * 11 * 17 * 31 * 41 * 101 * 251 * 401 * 601
1801 * 4051 * 8101 * 61681 * 268501 * 340801 * 2787601 * 3173389601(((201,), {}), 7 * 1609 * 22111 * 193707721 * 761838257287 *87449423397425857942678833145441)(((202,), {}), ’NO DATA (timed out)’)(((203,), {}), 127 * 233 * 1103 * 2089 * 136417 * 121793911 *11348055580883272011090856053175361113)(((204,), {}), 3^2 * 5 * 7 * 13 * 103 * 137 * 307 * 409 * 953 * 2142857 * 3061 * 6529 * 11119 * 13669 * 26317 * 43691 * 131071 *1326700741)(((205,), {}), 31 * 13367 * 2940521 * 164511353 * 70171342151 *3655725065508797181674078959681)
22

Párhuzamos számı́tások SageMathban
from sage.parallel.multiprocessing_sage import pyprocessing
p_iter = pyprocessing(4)P = parallel(p_iter=p_iter)def f(x): return x*xv = list(P(f)(range(10))); v[(((0,), {}), 0),(((1,), {}), 1),(((3,), {}), 9),(((2,), {}), 4),(((5,), {}), 25),(((4,), {}), 16),(((7,), {}), 49),(((8,), {}), 64),(((9,), {}), 81),(((6,), {}), 36)]23

Párhuzamos számı́tások SageMathban
from sage.parallel.multiprocessing_sage import pyprocessing
p_iter = pyprocessing(6)P = parallel(p_iter=p_iter)def f(x): return x*xv = list(P(f)(range(10))); v[(((0,), {}), 0),(((1,), {}), 1),(((2,), {}), 4),(((3,), {}), 9),(((6,), {}), 36),(((4,), {}), 16),(((7,), {}), 49),(((9,), {}), 81),(((8,), {}), 64),(((5,), {}), 25)]
24

Párhuzamos számı́tások SageMathban

from sage.parallel.map_reduce import RESetMPExample
S = RESetMPExample(maxl=7)
S.run()
5040*x^7 + 720*x^6 + 120*x^5 + 24*x^4 + 6*x^3 + 2*x^2 + x + 1
S.print_communication_statistics()#proc:
012
3
4
reqs sent:
71233
219
reqs rcvs:
1014181819- thefs:
80000+ thefs:
01
1
1
2

5
5
120
1

622
1001

7
19
120
125

SageGPUPyCUDA integrálásán is dolgoznak.
SageGPU: Web interface to CUDA development and GPU cloud computingIgor Rychkov and Paul Brouwers,

University of Canterbury, New Zealand, http://dev.math.canterbury.ac.nz/

PyCUDAWe acknowledge the Nvidia’s hardware donation and the UC College of Engineering’s research grant.

1. Purpose and benefits
Our initial aim was to simplify CUDA programming by using a interpreted language to send
CUDA code to the GPU. Python and PyCUDA met this need. Sage, http://sagemath.org/
expanded upon this by delivering further flexibility to our programming and publishing needs.
• SageGPU’s notebook service provides a web interface into the development environment forPython, C++, and CUDA. This allows anyone with a web browser and internet connection aremote access to our server’s GPU (Tesla Fermi C2070) and CPU.
• The server maintenance is provided by the University of Canterbury, NZ. The developer isfreed from hardware and software maintenance.
• SageGPU builds on the Sage notebook server, http://nb.sagemath.org/ and uses Python toauthor and run C++/CUDA programs, via PyCUDA or calling NVCC complier directly.
• The worksheet combines executable cells with Python code, surrounded by documentationin HTML and LATEX format allowing for links, images and video. The cells can be evaluated insequence as the reader progresses through the worksheet.
• Realization of “programs writing other programs”: Python is used to organize the workflow
and to customize templated C++/CUDA source strings for specific parameters.
• SageGPU is a novel solution for collaboration and publishing:o Worksheets can be shared with other SageGPU users to enable collaboration among
researchers or teams.
o The worksheet can be published giving it a permanent URL for sharing of code and results

Analyze• Data is available back in Python through NumPy,ctypes,PyCUDA interfaces• Format results in HTML tables, pictures, even interactive controls. This is another exampleof metaprogramming: Sage notebook amending HTML into itselfCollaborate• Worksheets can be shared with other SageGPU users to enable collaboration among
researchers or teams.
Publish• The worksheet can be published giving it a permanent URL for sharing of results.
• Better than source code publishing: rich active document containing code, usage,
background reading and results.
• The published worksheet can be run again on the server by the reader enabling readyconfirmation of the results.

4. Accessing GPU via PyCUDA

Easy to install into Sage, Python and PyCUDA, http://documen.tician.de/pycuda/, a library for
CUDA programming. This provides an interface interface to all essential CUDA features,
automatic initialization of CUDA with error checking, convenient and thin abstractions like events,
gpuarray for memory allocation and copy, SourceModule (also ReductionKernel etc.) for CUDA
kernel compilation/calling,
import pycuda.autoinit
print pycuda.autoinit.device.name()
import pycuda.driver as cuda#create two timers for speed-testing

start = cuda.Event(); end = cuda.Event()
from pycuda.compiler import SourceModule
import pycuda.gpuarray as gpuarraydt = np.long # 64-bit integers
from pycuda.reduction import ReductionKernel
a_gpu = gpuarray.arange(400000, dtype=dt)
sumreduce = ReductionKernel(dt, neutral="0",reduce_expr="a+b", map_expr="x[i]",arguments="long *x")
sum_gpu = sumreduce(a_gpu)
print "sum (PyCUDA):", sum_gpu.get()[()]

5. Accessing GPU with using the shell to call nvcc• Author or customize templated C++/CUDA/Thrust source• Use %sh cell or system call to compile it with nvcc
• use Python’s library ctypes for loading dynamic shared libraries (inprocess sharing of GPUmemory pointers)

2. What is Sage?
Features important to SageGPU are in bold.
• Sage is a free open-source mathematics software. It combines the power of many existing
open-source packages into a common Python-based interface. A worksheet interface issimilar to Mathematica notebook: Shift-Enter to evaluate the cell, interactive plots, etc. Sageis built out of nearly 100 open-source packages and features a unified interface. Sage can beused to study elementary and advanced, pure and applied mathematics. It combines various
software packages and seamlessly integrates their functionality into a common experience. It
is well-suited for education and research.
• The user interface is a notebook in a web browser or the command line. Using thenotebook, Sage connects either locally to your own Sage installation or to a Sage server on
the network. Inside the Sage notebook you can create embedded graphics, beautifully
typeset mathematical expressions, add and delete input, and share your work across thenetwork.
• Special Cells in Sage notebooks
o %auto – cell content evaluated automaticallyo %hide – to hide cells for better presentation
o %fortran – inline fortran code that will be compiled and linked automaticallyo %interact – for interactive graphics etc.
o %sh – the cell’s content is executed in the servers Linux shellthrust_source = """// sumreduce using thrust, C++ source#include <thrust/device_vector.h>#include <thrust/reduce.h>extern "C" long sumreduce(long *dev_raw_ptr, long n) {thrust::device_ptr<long> dev_ptr(dev_raw_ptr);
return thrust::reduce(dev_ptr, dev_ptr + n, long(0), thrust::plus<long>());}"""# A folder for saving the source and the DLL into

FOLDER = '/home/sageservice/tmp/'# Hashed filename is different whenever the source changes!

thrust_fn = FOLDER +'sumreduce' + str(hash(thrust_source))
if not os.path.exists(thrust_fn + '.so'):f = open(thrust_fn + '.cu', 'w')
f.write(thrust_source)
f.close()# System call to nvcc!

err = call(["nvcc","-arch","sm_20","-Xcompiler","-fpic","-shared","-o“, thrust_fn + '.so', thrust_fn + '.cu'])
cprint(thrust_source) # pretty print for debugging
print not(err)# Load using ctypes!

DLL = ctypes.CDLL(thrust_fn + '.so')
DLL.sumreduce.restype = ctypes.c_long# Call DLL function with GPU array pointers properly cast

sum_thrust = DLL.sumreduce(ctypes.c_ulong(a_gpu.ptr),
ctypes.c_long(len(a_gpu)))
print “sum (thrust GPU):”, sum_thrust

3. Workflow of working with SageGPU, all in one web document
Edit• Create the Python cells and C++/CUDA (template) source strings• “Programs writing other programs”: use Python to define the algorithm and auxiliary steps,
putting only the very essential, critical parts to the C++/CUDA source strings; enter and test
the parameters, and bake a customized, small C++/CUDA code allowing for faster and more
efficient compilation step.
• Allocate memory for arrays (e.g., numpy)

6. Example usage

Compile/load/debug C++/CUDA modules• using either PyCUDA or calling nvcc from the shell• with printf-debugging against syntax-highlighted, line-numbered view of C++/CUDA sourceRun• calling into system’s shell to execute EXE, or using ctypes to load the DLL, or using PyCUDAto load the compiled CUDA to the graphics driver• passing pointers into the compiled DLL or through PyCUDA interface• short or many days runs are possible

Figure 1. Screenshots showing example worksheet (RBF approximation) with syntax
highlighted CUDA source, HTML/ LATEX documentation, execution parameters and output,
inline Python, tables and graphs.

• Research: fast Radial Basis Function
evaluation methods, Figure 1.
• Teaching: Parallel algorithms in computational
mathematics, course lectures on CUDA
programming• See other published worksheets, Figure 2.
Figure 2. Example worksheets publishedat http://dev.math.canterbury.ac.nz/pub26

Pethő problémaMaciej Ulas lengyel matematikussal közösen nyert eredmény.
Theorem
There are infinitely many quartic algebraic integers defined by
α4 + aα3 + bα2 + cα + d = 0 for which
β=4α4α−4α −1 α−1

is a quadratic algebraic number. Moreover, there are infinitely manyquartic algebraic numbers α such that β is real quadratic.
Gröbner bázis számı́tások:e1 :2
22−3dpa + 5dpa + 3dpb − 6dp − dqa + 2dqa + dqb − 3dq − 9da + 12da + 9db − 10d + q,e2 :2
23dpa − 5dp + dqa − 2dq + 9da − 12d − 3pa c + 5pac + 3pbc − 6pc + p − qa c + 2qac +e3 :2+qbc − 3qc + 2q − 9a c + 12ac + 9bc − 10c,222−3dp − dq − 9d − 3pa b + 5pab + 3pac + 3pb − 6pb − 5pc + 3p − qa b + 2qab + qac +e4 :222+qb − 3qb − 2qc + 3q − 9a b + 12ab + 9ac + 9b − 10b − 12c + 1,32
32−3pa + 5pa + 6pab − 6pa − 5pb − 3pc + 6p − qa + 2qa + 2qab − 3qa − 2qb − qc + 4q −3
2−9a + 12a + 18ab − 10a − 12b − 9c + 4.

27

Pethő probléma
Megfelelően megválasztott rezultáns számı́tással:
1· (a − 2b + c) ·233·(233a4 − 352a3 b + 108a3 c + 168a3 + 368a2 b 2 − 264a2 bc −−624a2 b + 46a2 c 2 − 184a2 c − 544a2 − 160ab 3 + 128ab 2 c +352ab 2 − 16abc 2 + 64abc + 128ab − 4ac 3 − 8ac 2 + 768ac ++640a + 48b 4 − 64b 3 c − 256b 3 + 32b 2 c 2 + 288b 2 c + 384b 2 −−8bc 3 − 144bc 2 − 512bc + c 4 + 24c 3 + 96c 2 − 640c − 256).

28Pethő problémaKét végtelen család:
a=2,
2a

=2t,

b=2t + 2,

b=t 2 + 2t + 2,

c=

4t 2 − 4t + 2,c=

2t 2 + 2t,

d=

d=

p=

q

=

3t 2 − 2t + 1,
2 3 t2 − 5 t + 4−,
t2 − 2 t + 2
9 t 3 − 12 t 2 + 7 t − 2.
t3 − 2 t2 + 2 t

pq

6t 2 − 4t + 1,
6 t2 − 6 t + 1= −,t2 − t
18 t 3 − 18 t 2 + 7 t − 1=.
2 (t 3 − t 2 )29Pethő problémaRacionális parametrizáció a komplikált faktor esetében:
P6b(t)

=c(t)

=d(t)

=

P4i=0bni (t)ai,i
i=0 bdi (t)aP6cni (t)ai,
Pi=04i
i=0 cdi (t)aP6dni (t)ai.
Pi=04ii=0 ddi (t)aibni (t)bdi (t)012
3
456663552 t 4 − 2211840 t 3 + 2764800 t 2 − 1536000 t + 320000−331776 t 4 + 1050624 t 3 − 1244160 t 2 + 652800 t − 128000−41472 t 3 + 105984 t 2 − 90240 t + 25600
38016 t 3 − 89280 t 2 + 69696 t − 18080
12960 t 4 − 47520 t 3 + 62928 t 2 − 36240 t + 7748
−3888 t 4 + 11664 t 3 − 13248 t 2 + 6792 t − 1332
324 t 4 − 864 t 3 + 900 t 2 − 432 t + 81

331776 t 4 − 1105920 t 3 + 1382400 t 2 − 768000 t + 160000−331776 t 4 + 1050624 t 3 − 1244160 t 2 + 652800 t − 128000
124416 t 4 − 373248 t 3 + 419328 t 2 − 209280 t + 39200−20736 t 4 + 58752 t 3 − 62784 t 2 + 30048 t − 54401296 t 4 − 3456 t 3 + 3528 t 2 − 1632 t + 288
0030Pethő problémaicni (t)cdi (t)01234560165888 t 4 − 552960 t 3 + 691200 t 2 − 384000 t + 80000−82944 t 4 + 235008 t 3 − 241920 t 2 + 105600 t − 16000−20736 t 4 + 86400 t 3 − 126720 t 2 + 79296 t − 18080
20736 t 4 − 66528 t 3 + 79920 t 2 − 42744 t + 8620−4536 t 4 + 13176 t 3 − 14580 t 2 + 7308 t − 1404
324 t 4 − 864 t 3 + 900 t 2 − 432 t + 81165888 t 4 − 552960 t 3 + 691200 t 2 − 384000 t + 80000−165888 t 4 + 525312 t 3 − 622080 t 2 + 326400 t − 64000
62208 t 4 − 186624 t 3 + 209664 t 2 − 104640 t + 19600−10368 t 4 + 29376 t 3 − 31392 t 2 + 15024 t − 2720648 t 4 − 1728 t 3 + 1764 t 2 − 816 t + 14400icni (t)cdi (t)0123456331776 t 4 − 1105920 t 3 + 1382400 t 2 − 768000 t + 160000−663552 t 4 + 2211840 t 3 − 2764800 t 2 + 1536000 t − 320000
705024 t 4 − 2350080 t 3 + 2939904 t 2 − 1635840 t + 341600−393984 t 4 + 1271808 t 3 − 1540224 t 2 + 829536 t − 167680
115344 t 4 − 353376 t 3 + 407880 t 2 − 210624 t + 41148−16848 t 4 + 48384 t 3 − 52992 t 2 + 26304 t − 5004
972 t 4 − 2592 t 3 + 2700 t 2 − 1296 t + 243331776 t 4 − 1105920 t 3 + 1382400 t 2 − 768000 t + 160000−331776 t 4 + 1050624 t 3 − 1244160 t 2 + 652800 t − 128000
124416 t 4 − 373248 t 3 + 419328 t 2 − 209280 t + 39200−20736 t 4 + 58752 t 3 − 62784 t 2 + 30048 t − 5440
1296 t 4 − 3456 t 3 + 3528 t 2 − 1632 t + 288
0031