F#
Applications to Computational Finan-cial and GPU Computing
May 16th
Dr. Daniel Egloff
+41 44 520 01 17
+41 79 430 03 61
! 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
Today
! 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
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
! 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
Challenges
How to face these challenges?
F# with Visual Studio tooling has been specifically designed for these needs!
Needs
Flexible high productivity software
engineering tools
Support iterative development and rapid prototyping
Modern programming languages and
concepts
Building on top of modern technologies like .NET or the JVM
Executing as fast as compiled Fortran or C/
C++
Solid framework and library components
! 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
How does F# achieve this?
! Real project examples are more convincing than listings of technical benefits
! Illustrate capabilities of F# with 3 project examples
Three success stories
! 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 I
! 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 II
! 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
F# success story III
! 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
Our lessons
What are
?
GPUs
! 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
Schematic GPU Architecture
Processor 1
Registers
InstructionUnit
Processor m
Registers.........
1 cycle
1 cycle
Shared Memory
Constant Cache
Texture Cache
1-2 cycles
1-2 cycles
Host Controller
Data Bus
Processor 1
Registers
InstructionUnit.........
1 cycle
1 cycle
Shared Memory
Constant Cache
Texture Cache
1-2 cycles
1-2 cycles
400-600 cycles
Mu
ltip
rocess
or
1
Mu
ltip
rocess
or
n
Device Memory
GPU
Processor m
Registers
! 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
Programming GPUs
Thread Thread Block Grid
What is
?
! 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
Alea.cuBase
Dynamic code generation
GPU algorithm scripting
Industry grade performance
Rapid development
Solid framework for reusability
Advanced CUDA programming
! 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
Dynamic code generation
! 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
Rapid development
! Execute F# scripts with GPU algorithms on command line or in F# interactive
! GPU scripting in Excel
! Integrate Alea.cuBase directly with Python
Benefits
GPU algorithm scripting
! 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
Benefits
Solid framework for reusability
cuda !{ kernel_B launch logic}
cuda !{ kernel_A launch logic}
! 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
Benefits
Industry grade performance
0.00%
100.00%
200.00%
300.00%
400.00%
500.00%
600.00%
700.00%
800.00%
2097152 8388608 16777216 33554432
int32 float32 float64
Segmented Scan by Key Alea.cuExtension against CUDA Thrust
! 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
Benefits
Advanced CUDA programming
Ecosystem
Alea.cuBaseCUDA Runtime
API
ThrustCUDPP Alea.cuExtension
User Applications
CUDA Driver API
Alea EcosystemCUDA C Ecosystem
How does
work ?
PModule
Comilationprocess
PTemplate
cuda !{ constant array texture kernel ... launch logic}
Device worker
CUDA device
CUDA context
NVVM IR module
PTX module
CUDA cubinmodule
CUDA module
Launch function
! Four steps to a CUDA kernel with F# and Alea.cuBase
Development Process
2
3
4
1
How easily can I use
?
! 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
Live Coding
! 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++
Conclusion
! 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/
More Resources and Free Licenses
Thank you