F#nctional and MultiCore
Programming
Rodrigo Vidal
Quem sou eu?
Rodrigo Vidal
Twitter: @rodrigovidal
Blog: www.rodrigovidal.net
•Focado em Arquitetura de Software e Boas Prá...
Agenda
• MultiCore
• Functional Paradigm
• F# Language
Functional Programming
• Velho Paradigma
• 52 anos hoje
• Ignorado pelo MainStream até agora
Functional – Why now?
• Novo desafios
• Processar grandes quantidades de dados
• Escalabilidade para Cores e Clusters
• De...
Why another language?
• Programação Funcional está por ai a um
longo tempo.
– Não é algo novo
– Tem uma longa história
• P...
“What” rather than “How”
Imperativo Vs. Declarativo
Quem usa ForEach() ?
|
List<string> res = new List<string>();
foreach(Product p in Products) {
    if (p.UnitPrice > 75.0M...
Domain Driven Design
Foco no Core Domain, revelando a
intenção do seu código.
Interfaces Reveladoras de Intenções
Concurrency-Friendly
Imperative-Problems:
•É dificil tornar codigo sequencial em
paralelo.
•É dificil usar “Shared States”...
The Functional Paradigm
• Immutable Data
• Ability to compose functions
• Functions can be thread as data
• Lazy Evalution...
Immutability
“Values are immutable by default”
Side-Effects
– Also knowed as “The Butterfly Effect”
• A side effect
– Modifies some state
– Has observable interaction wi...
Composing Functions
f(x) = 3x+2
g(y) = 4y-1
g(f(2)) = ?
f(x)
g(x)
Motivação
#Inception
Language Evolution
http://channel9.msdn.com/posts/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana/
C#, V...
F# Evolution
What is F#?
• Functional language developed by
Microsoft Research
– By Don Syme and his team, who
productized Generics
– B...
F# 1.0
Functional + Objects + .NET
F# 2.0
Functional + Objects + .NET +
Async + Parallel + Interactive +
Scripting
F# 2.0
Functional + Objects + .NET +
Async + Parallel + Interactive +
Scripting
F# 2.0
• Succinct, Expressive, Functional
– Productive, simple, powerful, and fun language
• Parallel, Explorative, Data-r...
let demo = “Code!”
Let’s Go Parallel
The Free Lunch is Over
Why not 10GHz?
Software
Matrix Revolution
Parallel
Pattern
Library
Resource Manager
Task Scheduler
Task Parallel Library
Parallel LINQ
Threads
Native Concurrency Ru...
Imperative Parallelism
Declarative Data Parallelism
CODE!
Amdahl’s Law (1 / percent of time spent in
sequential processing)
Limits of Parallelism
GPU – Beyond CPU
CODE!
The F#ture
Latest Books about F#
Upcoming SlideShare
Loading in …5
×

F# Functional and MultiCore Programming

2,407 views

Published on

ECODevelopers 2010 Presentation about F# Functional and MultiCore Programming - Rio de Janeiro - Brazil

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,407
On SlideShare
0
From Embeds
0
Number of Embeds
400
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • List&amp;lt;string&amp;gt; res = new List&amp;lt;string&amp;gt;(); #1
    foreach(Product p in Products) { #2
    if (p.UnitPrice &amp;gt; 75.0M) {
    res.Add(String.Format(&amp;quot;{0} - ${1}&amp;quot;,
    p.ProductName, p.UnitPrice)); #3
    }
    }
    var res = from p in Products
    where p.UnitPrice &amp;gt; 75.0M #1
    select string.Format(&amp;quot;{0} - ${1}&amp;quot;,
    p.ProductName, p.UnitPrice); #2
    return res;
  • C#
    Parallel.For(1,inp.Length-1,i =&amp;gt; {
    var sum = inp[i-1] +
    inp[i] + inp[i+1];
    res[i] = sum / 3;
    });
    F#
    let pfor nfrom nto f =
    Parallel.For(nfrom, nto + 1, Action&amp;lt;_&amp;gt;(f))
    pfor 1 (inp.Length-2) (fun i -&amp;gt;
    let sum = inp.[i-1] +
    inp.[i] + inp.[i+1]
    res.[i] &amp;lt;- sum / 3
    )
  • The key idea behind the declarative style of programming is that the code doesn&amp;apos;t specify
    how exactly it should be executed. Execution is provided by a minimal number of primitives
    such as select and where in LINQ or map and filter in F#, and these primitives can
    behave in a sophisticated way.
  • The key idea behind the declarative style of programming is that the code doesn&amp;apos;t specify
    how exactly it should be executed. Execution is provided by a minimal number of primitives
    such as select and where in LINQ or map and filter in F#, and these primitives can
    behave in a sophisticated way.
  • The key idea behind the declarative style of programming is that the code doesn&amp;apos;t specify
    how exactly it should be executed. Execution is provided by a minimal number of primitives
    such as select and where in LINQ or map and filter in F#, and these primitives can
    behave in a sophisticated way.
  • The key idea behind the declarative style of programming is that the code doesn&amp;apos;t specify
    how exactly it should be executed. Execution is provided by a minimal number of primitives
    such as select and where in LINQ or map and filter in F#, and these primitives can
    behave in a sophisticated way.
  • F# Functional and MultiCore Programming

    1. 1. F#nctional and MultiCore Programming Rodrigo Vidal
    2. 2. Quem sou eu? Rodrigo Vidal Twitter: @rodrigovidal Blog: www.rodrigovidal.net •Focado em Arquitetura de Software e Boas Práticas. •Agilista e entusiasta de F# e IronRuby/Ruby. •Membro ativo do grupo .NetArchitects •Professional Scrum Developer •Presente no maiores eventos do tecnologia do país.
    3. 3. Agenda • MultiCore • Functional Paradigm • F# Language
    4. 4. Functional Programming • Velho Paradigma • 52 anos hoje • Ignorado pelo MainStream até agora
    5. 5. Functional – Why now? • Novo desafios • Processar grandes quantidades de dados • Escalabilidade para Cores e Clusters • Declarative Programming
    6. 6. Why another language? • Programação Funcional está por ai a um longo tempo. – Não é algo novo – Tem uma longa história • Programação Funcional é “safe” – Devemos nos preocupar principalmente quando lidamos com aplicações MultiCore e de Cloud Computing • Programção Funcional está crescendo!
    7. 7. “What” rather than “How” Imperativo Vs. Declarativo
    8. 8. Quem usa ForEach() ? | List<string> res = new List<string>(); foreach(Product p in Products) {     if (p.UnitPrice > 75.0M) {         res.Add(String.Format("{0} - ${1}",         p.ProductName, p.UnitPrice));      } } var res = from p in Products where p.UnitPrice > 75.0M select string.Format("{0} - ${1}", p.ProductName, p.UnitPrice); return res;
    9. 9. Domain Driven Design Foco no Core Domain, revelando a intenção do seu código. Interfaces Reveladoras de Intenções
    10. 10. Concurrency-Friendly Imperative-Problems: •É dificil tornar codigo sequencial em paralelo. •É dificil usar “Shared States” e “Locks”
    11. 11. The Functional Paradigm • Immutable Data • Ability to compose functions • Functions can be thread as data • Lazy Evalution • Pattern Matching
    12. 12. Immutability “Values are immutable by default”
    13. 13. Side-Effects – Also knowed as “The Butterfly Effect” • A side effect – Modifies some state – Has observable interaction with calling functions – Has observable interaction with the outside world – Example: a function or method with no return value
    14. 14. Composing Functions f(x) = 3x+2 g(y) = 4y-1 g(f(2)) = ? f(x) g(x)
    15. 15. Motivação
    16. 16. #Inception
    17. 17. Language Evolution http://channel9.msdn.com/posts/Charles/Simon-Peyton-Jones-Towards-a-Programming-Language-Nirvana/ C#, VB, Java, C are imperative programming languages. Very useful but can change the state of the world at anytime creating side effects. Nirvana! Useful and Safe Haskell is Very Safe, but not very useful. Used heavily in research and academia, but rarely in business. F#F#
    18. 18. F# Evolution
    19. 19. What is F#? • Functional language developed by Microsoft Research – By Don Syme and his team, who productized Generics – Based on OCaml (influenced by C# and Haskell)
    20. 20. F# 1.0 Functional + Objects + .NET
    21. 21. F# 2.0 Functional + Objects + .NET + Async + Parallel + Interactive + Scripting
    22. 22. F# 2.0 Functional + Objects + .NET + Async + Parallel + Interactive + Scripting
    23. 23. F# 2.0 • Succinct, Expressive, Functional – Productive, simple, powerful, and fun language • Parallel, Explorative, Data-rich, Algorithmic – Extends the .NET platform to important new audiences • Innovative, Industry/Platform-Leading – Async, parallel, easy-objects, immutability, tuples, units- of-measure • Note: F# is not a replacement for C#/VB/C++ – Complementary, interoperable and augments and builds on .NET as multi-lang platform
    24. 24. let demo = “Code!”
    25. 25. Let’s Go Parallel
    26. 26. The Free Lunch is Over
    27. 27. Why not 10GHz?
    28. 28. Software Matrix Revolution
    29. 29. Parallel Pattern Library Resource Manager Task Scheduler Task Parallel Library Parallel LINQ Threads Native Concurrency Runtime Managed Libraries ThreadPool DataStructures DataStructures Tools Async Agents Library UMS Threads Microsoft Research Visual Studio 2010 Parallel Debugger Windows Profiler Concurrency Analysis Race Detection Fuzzing AxumVisual F# Managed Languages Rx Native Libraries Managed Concurrency Runtime DryadLINQ Research / Incubation Research / Incubation Visual Studio 2010 / .NET 4 Windows 7 / Server 2008 R2 HPC Server Operating System Parallel Computing and PDC09
    30. 30. Imperative Parallelism Declarative Data Parallelism CODE!
    31. 31. Amdahl’s Law (1 / percent of time spent in sequential processing) Limits of Parallelism
    32. 32. GPU – Beyond CPU CODE!
    33. 33. The F#ture
    34. 34. Latest Books about F#

    ×