Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin для науки

Kotlin для науки

Александр Нозик
Старший научный сотрудник МФТИ и руководитель направления JetBrains Research

1 октября 2020
Online Dev Meetup

Где использовать Kotlin кроме мобильной разработки?
Александр Нозик расскажет участникам Dev Meetup:
✔ о стремительно растущей научной экосистеме языка Kotlin, которая включает в себя Data Science,
✔ разнообразных, часто неожиданных задачах, которые можно решать с его помощью,
✔ своем опыте разработки научных приложений на Kotlin.

Подписывайтесь на Exactpro в социальных сетях:

LinkedIn https://www.linkedin.com/company/exactpro-systems-llc
Twitter https://twitter.com/exactpro
Facebook https://www.facebook.com/exactpro/
Instagram https://www.instagram.com/exactpro/

Подписывайтесь на YouTube канал Exactpro http://www.youtube.com/c/ExactproVlog



October 01, 2020


  1. Alexander Nozik

  2. Science is special • Big experiments • Big data (25

    GB/s on LHC) • Big distances • Big times (tens of years scale) • Low salaries • Need a simple and bug-proof yet powerful language http://www-sk.icrr.u-tokyo.ac.jp/sk/index-e.html https://www.symmetrymagazine.org/article/august- 2015/icecube-sees-highest-energy-neutrino-ever-found 2
  3. A language for science? 3 Candidates: • C/Fortran • C++

    • Python • Java • Haskell • Julia Too inexpressive Too dangerous Too slow Too verbose Too hard Too bad tooling
  4. Why Kotlin • Better java • Scoped functions • Coroutines

    • Multiplatform • Industrial support (JetBrains) 4
  5. Muon monitor • https://bitbucket.org/mipt- npm/muon-sim • Simulation for muon monitor

    project (arXiv:1902.00868). Measure muon tracks in deep underground laboratory (~1 km underground). • Java + JavaFX code moved to Kotlin (several hours of work). • Works the same but looks better. • ~30% less code. 5
  6. Troitsk nu-mass • Problem: • Readout from devices using USB,

    direct RS-232 and TCP IP. • File-based data storage and indexing. Communication protocols between different systems. • One of the most complicated analysis procedures in particle physics. • 100k+ LOC and only one man to maintain. • Kotlin gives: • Better safety. Nice lambda syntax. Data classes and other language features. Coroutines instead of CompletableFuture. • Gradual migration from java (not relevant for most HEP projects) 6
  7. I need to do some mathematics (linear algebra, geometry, etc.).

    What should I use? Gnu maths? R? Numpy? Julia? Established ecosystems but they have one thing in common: You can use what is already existing, but the development of something new is hard. 7 Kmath is a multiplatform library for kotlin. It uses unique kotlin features (extensions, context-oriented programming), to achieve both performance and flexibility in mathematical operations. Kmath relies on high-performance libraries in Java ecosystem. https://github.com/mipt-npm/kmath
  8. 8 fun main() { // 2d element val element =

    NDElement.complex(2, 2) { (i,j) -> Complex(i.toDouble() - j.toDouble(), i.toDouble() + j.toDouble()) } println(element) // 1d element operation val result = with(NDField.complex(8)) { val a = produce { (it) -> i * it - it.toDouble() } val b = 3 val c = Complex(1.0, 1.0) (a pow b) + c } println(result) } sample
  9. Plotly.kt demo val trace1 = Scatter { x(0, 1, 2,

    3, 4, 5, 6, 7, 8) y(8, 7, 6, 5, 4, 3, 2, 1, 0) } val trace2 = Scatter { x(0, 1, 2, 3, 4, 5, 6, 7, 8) y(0, 1, 2, 3, 4, 5, 6, 7, 8) } val plot = Plotly.plot { traces(trace1, trace2) layout { title = "Log scale axis" xaxis { type = AxisType.log autorange = true } yaxis { type = AxisType.log autorange = true } } } 9 https://github.com/mipt-npm/plotly.kt
  10. Plotly.kt behind the scenes • Kotlin-common dynamic model (based on

    dataforge-meta). • Plotly statically-typed schemas over dynamic models. • Direct Json converter for dynamic model (does not use serialization or DTO). • Fine grained update with a single callback at the root of plot tree. Type-safe kotlin wrapper Dynamic JSON- like structure Frontend Plotly client JSON WebSocket updates 10
  11. Fine-grained tree updates • Generate change event that include changed

    property name and new tree item value • Send only this value (no serialization) • Apply change directly to client-side (no reconcile) • Real-time updates with back-pressure Trace name type marker color Trace name type marker Client-side model Server-side model Coroutine-based change aggregator 11
  12. VisionForge • Up to 400k primitives per display • Each

    element is configurable by user • Templates and references to optimize both communication and rendering optimization. • Tree transformation and optimization logic to work with HEP software formats. • Multiplatform (only ThreeJS renderer for now, but stubs for the rest). https://github.com/mipt-npm/visionforge 12
  13. VisionForge inside • Fat kotlin-common logic (full scene graph for

    general “Visions” and 3d-”Solids”). • Each Vision is a doubly-linked tree that includes: • Custom observable properties • Stylesheets for each node • Style references and properties propagation • Solids additionally could contain templates, which allow to re-use geometries. 13
  14. Device control • Most control systems use monolithic code on

    top of specific serialization protocol (CORBA, SUN ONC, etc.). • Which makes support and development quite hard. • “New” idea: use reactive streams and protocol-agnostic transport 14 Network Logical state Physical state Asynchronous change Synchronous update State change message Asynchronous update State change message Command validation
  15. Device control 15 • Micro-service architecture. • Shared event loop

    with ability to subscribe to specific events and send evaluation results asynchronously back into the loop. • Can plug analyzers and processors into the loop.
  16. Simulations in HEP 16 https://www.physicscentral.com/explore/pictures/bubble-tracks.cfm Bubble chamber photo

  17. Kotlin for data science • Data science is not a

    science and is not about data. • Yet a lot of people are working in it. A lot of ${money}. • Kotlin is great for data science: • Modern concise language • Full access to robust Java ecosystem • The best JIT (means good performance) • What one need to make it the best is the tooling. • In 2020 Kotlin for data science team published • Kotlin jupyter kernel • Lets-plot multiplatform graphic library • Spark bindings for Kotlin 17
  18. Contacts • JetBrains Research: https://research.jetbrains.org/groups/npm • Npm lab: http://npm.mipt.ru/ru/ •

    Kotlin site: https://kotlinlang.org/ • Kotlin slack: http://kotlin.in/slack • Kotlin telegram community: https://t.me/kotlin_lang 18