G ETTING F ULL S PEED WITH D ELPHI Primož Gabrijelčič otl.17slon.com [W HY S INGLE -T HREADING I S N OT E.

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

H IGH - LEVEL M ULTITHREADED P ROGRAMMING [P ART I] Primož Gabrijelčič.
H IGH - LEVEL M ULTITHREADED P ROGRAMMING [P ART II] Primož Gabrijelčič.
H IGH - LEVEL M ULTITHREADED P ROGRAMMING [P ART III] Primož Gabrijelčič.
Parallel Programming with OmniThreadLibrary
Fast programs for modern computers
Multithreading Made Simple with OmniThreadLibrary
Parallel Programming Made Easy Primož Gabrijelčič,
490dp Synchronous vs. Asynchronous Invocation Robert Grimm.
Precept 3 COS 461. Concurrency is Useful Multi Processor/Core Multiple Inputs Don’t wait on slow devices.
Concurrency CS 510: Programming Languages David Walker.
CS533 Concepts of Operating Systems Class 2 Thread vs Event-Based Programming.
1 Chapter 4 Threads Threads: Resource ownership and execution.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Introduction to Parallel Processing 3.1 Basic concepts 3.2 Types and levels of parallelism 3.3 Classification of parallel architecture 3.4 Basic parallel.
ADLB Update Recent and Current Adventures with the Asynchronous Dynamic Load Balancing Library Rusty Lusk Mathematics and Computer Science Division Argonne.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Building Multithreaded Solutions with OmniThreadLibrary
View-Oriented Parallel Programming for multi-core systems Dr Zhiyi Huang World 45 Univ of Otago.
Threads, Thread management & Resource Management.
1 OpenMP Writing programs that use OpenMP. Using OpenMP to parallelize many serial for loops with only small changes to the source code. Task parallelism.
T HREADS Gursharan Singh Tatla 27-Jan-11 1
Threaded Applications Introducing additional threads in a Delphi application is easy.
Parallel Extensions A glimpse into the parallel universe By Eric De Carufel Microsoft.NET Solution Architect at Orckestra
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
111 © 2002, Cisco Systems, Inc. All rights reserved.
An Intro to Programming with C# Threads Presentation by: Jason Bender, Garrett Lund, Ben Gamble, Michael Calvo, and Jeff Corbell.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
SIMPLE PARALLEL PROGRAMMING WITH PATTERNS AND OMNITHREADLIBRARY PRIMOŽ GABRIJELČIČ SKYPE: GABR42
B. RAMAMURTHY 10/24/ Realizing Concurrency using the thread model.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
CS333 Intro to Operating Systems Jonathan Walpole.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
CS- 492 : Distributed system & Parallel Processing Lecture 7: Sun: 15/5/1435 Foundations of designing parallel algorithms and shared memory models Lecturer/
Department of Computer Science and Software Engineering
Simplify Parallel Programming with Patterns Primož Gabrijelčič R&D Manager, FAB.
Threads, Thread management & Resource Management.
CGS 3763 Operating Systems Concepts Spring 2013 Dan C. Marinescu Office: HEC 304 Office hours: M-Wd 11: :30 AM.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
A Pattern Language for Parallel Programming Beverly Sanders University of Florida.
2/20/2016 EEC492/693/793 - iPhone Application Development 12/20/2016 EEC492/693/793 - iPhone Application Development 1 EEC-492/693/793 iPhone Application.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
Unit 4: Processes, Threads & Deadlocks June 2012 Kaplan University 1.
Real world OS programming Nezer J. Zaidenberg.  Create process/thread  File and directory I/O  I/O multiplexing  Sync and locking  Access system.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Concurrent Revisions: A deterministic concurrency model. Daan Leijen & Sebastian Burckhardt Microsoft Research (OOPSLA 2010, ESOP 2011)
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Concepts of Multithreading CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Async or Parallel? No they aren’t the same thing!
Computer Engg, IIT(BHU)
Lecture 21 Concurrency Introduction
Task Scheduling for Multicore CPUs and NUMA Systems
Chapter 4 Multithreading programming
Chapter 4: Threads.
Parallel Programming Done Right with OTL and PPL
Writing High Performance Delphi Application
Threads Chapter 4.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Multithreaded Programming
Operating System Overview
Presentation transcript:

G ETTING F ULL S PEED WITH D ELPHI Primož Gabrijelčič otl.17slon.com [W HY S INGLE -T HREADING I S N OT E NOUGH ?]

Multithreading

What? The art of doing multiple things at the same time

Why? The end of free lunch

How? OmniThreadLibrary

When? Rarely

W HAT ?

Threading A thread is a line of execution through a program – There is always one thread Multitasking (and multithreading) – Cooperative – Preemptive Time slicing Parallel execution

Processes vs. Threads Pros – Processes are isolated – data protection is simple Cons – Processes are isolated – data sharing is simple – Processes are heavy, threads are light

Problems Data sharing – Messaging – Synchronization Synchronization causes – Race conditions – Deadlocking – Livelocking Slowdown

W HY ?

The End of Free Lunch © Herb Sutter, What we want What we have

H OW ?

Four paths to multithreading The Delphi Way – TMyThread = class(TThread) The Windows Way – FHandle := BeginThread(nil, Pointer(Self), 0, FThreadID); The Lightweight Way (AsyncCalls) – TAsyncCalls.Invoke(procedure begin DoTheCalculation; end); OmniThreadLibrary

OmniThreadLibrary is … … VCL for multithreading – Simplifies programming tasks – Componentizes solutions – Allows access to the bare metal … trying to make multithreading possible for mere mortals … providing well-tested components packed in reusable classes with high-level parallel programming support

Project Status OpenBSD license Actively developed – 886 commits [ code.google.com/p/omnithreadlibrary/] code.google.com/p/omnithreadlibrary/ Actively used – 2.0: 1206 downloads – 1.05: 2028 downloads [March 1 st, 2011] Almost no documentation 

H OW ?

High level multithreading Join Futures Pipelines Fork/Join Parallel for Delphi 2009 required

Join Divide and Wait – Start multiple background calculations – Wait for all to complete – No result is returned (directly) Two basic forms – Join(task1, task2); – Join([task1, task2, task3, … taskN]);

Future Wikipedia – “They (futures) describe an object that acts as a proxy for a result that is initially not known, usually because the computation of its value has not yet completed.” – Start background calculation, wait on result. How to use? – Future:=TOmniFuture.Create( calculation); – Query Future.Value;

Pipeline Multistage process

Pipelines var pipeOut: IOmniBlockingCollection; pipeOut := Parallel.Pipeline.Stage(StageGenerate).Stage(StageMult2).Stage(StageSum).Run;

Fork/Join Divide and conquer – Execute multiple tasks – Wait for them to terminate – Collect results – Proceed Subtasks may spawn new subtasks

Fork/Join max1 := forkJoin.Compute( function: integer begin Result := … end); Result := Max(max1.Value, max2.Value);

Parallel For Parallel.ForEach(1, CMaxSGPrimeTest).Execute( procedure (const value: integer) begin if IsPrime(value) then numPrimes.Increment; end);

Messaging TOmniMessageQueue TOmniQueue – Dynamically allocated, O(1) enqueue and dequeue, threadsafe, microlocking queue TOmniBlockingCollection TOmniValue

Tasks vs. Threads Task is part of code that has to be executed Thread is the execution environment You take care of the task, OTL takes care of the thread

Task Execution CreateTask(task_procedure) CreateTask(task_method ) CreateTask(TOmniWorker_object) CreateTask(anonymous_procedure)

Thread Pool Starting up a thread takes time Thread pool keeps threads alive and waits for tasks Automatic thread startup/shutdown User code executed at thread creation – Connection pool.Run ⇨.Schedule

W HEN ?

Danger! “New programmers are drawn to multithreading like moths to flame, with similar results.” -Danny Thorpe

When To Use Slow background process Background communication Executing synchronous API Multicore data processing Multiple clients

Keep in mind Don’t parallelize everything Don’t create thousands of threads Rethink the algorithm Prove the improvements Test, test and test

Be Afraid Designing parallel solutions is hard Writing multithreaded code is hard Testing multicore applicationss is hard Debugging multithreading code is pure insanity

Q UESTIONS ?