F# Applications to Computational Financial and GPU Computing May 16th Dr. Daniel Egloff +41 44 520 01 17 +41 79 430 03 61
Today! Why care about F#? Just another fashion?! Three success stories! How Alea.cuBase turns F# into first class CUDA language! Live coding examples including CUDA scripting in Excel! Pointers to more information
About Us! Software development! Quantitative finance and risk management! Derivative pricing and modeling! Numerical computing! High performance computing (clusters, grid, GPUs)! Software engineering (C++, F#, Scala, )! Early adopter of GPUs! Creator of Alea.cuBase! Consulting and advisory! Risk management, quantitative finance, reporting, regulations! Banks, insurance firms, asset management firms, energy and commodity trading firms! Interdisciplinary team, with expertise in methodology, implementation, IT architecture! Using QuantAlea technology in projects
Challenges! Software applications in financial industry are! High complexity! Heterogeneous! Strongly interlinked! Numerically intense! Subject to continuous change! Challenges to build and maintain financial software! Time to market! Correctness of results! Robustness of applications! Performance and efficient execution! Extendibility of solutions! Total cost of ownership
Needs How to face these challenges? Flexible high productivity software engineering tools Modern programming languages and concepts Solid framework and library components Support iterative development and rapid prototyping Building on top of modern technologies like.net or the JVM Executing as fast as compiled Fortran or C/ C++ F# with Visual Studio tooling has been specifically designed for these needs!
How does F# achieve this?! Strongly typed functional first language with OO elements! Compact and clear syntax! Designed for! High productivity! Development of correct, robust and efficient code! F# is highly flexible and extensible! Computational work flows, code quotations, type providers,! F# is ideal for! Numerical computing and GPU computing! Domain specific languages, code generation and transformation! F# has lot of potential in computational finance and big data! Good tool support through Visual Studio, Tsunami, Excel (Fcell)! Intellisense, F# interactive! Vast.NET ecosystem
Three success stories! Real project examples are more convincing than listings of technical benefits! Illustrate capabilities of F# with 3 project examples
F# success story I! Derivative pricing library! Self-contained pricing library developed in F#! Innovative design on the general idea of perturbations! Framework to do Greeks and more general calculation! GPU Monte Carlo! GPU accelerated local volatility calibration and pricing! PDE pricing algorithms! Main focus on equity derivatives, some FX! F# advantages! Pattern matching logic for complex dispatching! Computational work flows for data preprocessing! Async work flow for parallel processing! Rich data containers
F# success story II! Grid infrastructure for derivative pricing! High throughput CPU / GPU cluster! Based on ICE from ZeroC! Integration in Sungard Front Arena! F# advantage! Multithreaded server side programming! Async work flows! Callback handling with F# functions and closures! Code generation and transformation from meta information and reflection
F# success story III! Alea.cuBase! Dynamically compile F# code to fast CUDA code! Based on LLVM technology! Started May 2012! First prototype ready after 4 month! First production release after 7 month! Working on Nsight GPU debugging support in Visual Studio! F# advantage! Code quotations! AST processing with pattern matching! Extensibility of F# with computational work flows! High productivity
Our lessons! Lessons! Significant productivity improvement! Less code for more features! Fewer bugs, less testing and debugging! Better code quality! Increased reusability of components! Consequences! Smaller development team! Increased project agility! Reduced project costs! Faster time to market! Today F# is our preferred development language
What are GPUs?
Schematic GPU Architecture! GPU is a coprocessor! Integrates several streaming multiprocessors (SM)! Complex memory hierarchy! Execution of hundreds of threads in parallel! Thread creation, scheduling and switching in hardware! Minimal thread scheduling unit is a warp of 32 threads! Zero overhead for warp scheduling 400-600 cycles 1 cycle Processor 1 Registers 1-2 cycles Processor 1 1-2 cycles 1 cycle... 1 cycle Registers Host Controller 1-2 cycles Data Bus 1-2 cycles... Processor 1 cyclem Shared Memory Device Memory Registers Texture Cache Constant Cache Processor m Registers Instruction Unit Shared Memory Texture Cache Constant Cache Multiprocessor 1 Instruction Unit Multiprocessor n GPU
Programming GPUs! CUDA programming model abstracts GPU device! Kernel is a function executing sequential code in parallel with large number of threads! Two level thread hierarchy! Each thread obtains thread id in its block and its block id in grid! Three dimensional block and grid layout Thread Thread Block Grid
What is?
Alea.cuBase! Alea.cuBase extends F# to a first class CUDA language! Based on LLVM and CUDA 5 technology! Noninvasive single language solution for host and GPU programming! No additional language additions required, in particular no <<< >>>! Extensible! Basis for creating higher level GPU aware DSLs Dynamic code generation GPU algorithm scripting Industry grade performance Rapid development Solid framework for reusability Advanced CUDA programming
Benefits Dynamic code generation! Generate GPU code programmatically at run-time! Usable from any other.net language! Use generics and F# code quotation splicing for flexible kernels! Foundation to develop GPU aware domain specific languages
Benefits Rapid development! Easy and quick setup of development environment, no need to install NVIDIA nvcc compiler tools! Rapid prototyping in F# interactive! Iteratively improve CUDA kernel algorithms without time consuming build cycles! Simple deployment
Benefits GPU algorithm scripting! Execute F# scripts with GPU algorithms on command line or in F# interactive! GPU scripting in Excel! Integrate Alea.cuBase directly with Python
Benefits Solid framework for reusability! Framework for type-safe definition of GPU resources! CUDA monad to specify GPU resources together with launch logic in unified manner! Reuse GPU kernel code and compose them to modular GPU kernel libraries cuda { kernel_a launch logic } cuda { kernel_b launch logic }
Benefits Industry grade performance! Generating performance optimized code which is on par with compiled CUDA C/C++ code! Low level device functions and special math functions! Built in occupancy calculator to identify optimal thread block layout Segmented Scan by Key Alea.cuExtension against CUDA Thrust 800.00% 700.00% 600.00% 500.00% 400.00% 300.00% int32 float32 float64 200.00% 100.00% 0.00% 2097152 8388608 16777216 33554432
Benefits Advanced CUDA programming! Support for texture, constant and shared memory! Pointer operations to partition array data! Special pointer types such as volatile pointers! Runtime compilation control e.g. fast math! Multiple streams! Thread safe use of multiple GPUs! Inline PTX assembly instructions
Ecosystem CUDA C Ecosystem Alea Ecosystem User Applications Thrust CUDPP Alea.cuExtension CUDA Runtime API Alea.cuBase CUDA Driver API
How does work?
Development Process! Four steps to a CUDA kernel with F# and Alea.cuBase cuda { constant array texture kernel... launch logic } PTemplate 1 NVVM IR module 3 CUDA module 4 2 PTX module Launch function CUDA device CUDA context CUDA cubin module PModule Device worker Comilation process
How easily can I use?
Live Coding! We can run code! As executable! In Visual Studio F# interactive! As script on the command line! Smoothly integrated with Python, Matlab,! Can use the new IDE Tsunami, integrated into Excel, to do GPU kernel scripting within Excel! Consider the following GPU examples! Simple kernel! Simplistic Monte Carlo simulation to calculate Pi! PDE solver for 2d heat equation in GPU! Excel GPU scripting with Alea.cuBase and Alea.cuExtension, in Tsunami IDE and FCell
Conclusion! F# combined with modern programming technologies! Allow seamless integration of modern hardware acceleration technologies such as GPUs and CUDA! Significantly improve reusability and composition of components! Enhance correctness and robustness! Allows to solve problems with less code! Requires less developers! Shortens time to market! Reduces development and maintenance cost! If properly used, virtually no performance difference as compared to Fortran and C / C++
More Resources and Free Licenses! More resources! https://www.quantalea.net/products/resources/! https://github.com/quantalea! How to set up! Fermi device or higher! Windows with.net 4 and F# 2.0! CUDA 5 driver! Install Alea.cuBase! No need for CUDA toolkit or NVCC compiler! Apply for PESONAL licenses! https://www.quantalea.net/news/22/
Thank you