Parallelizing Ocean plug-in computations using the Background Worker + PFX pattern

Similar documents
Programming in.net. Microsoft Development Center Serbia programming course. Lesson 8 Parallelism and Threading in.net

Santiago Canyon College Computer Science

Concurrent Programming

Multiple processes can run in parallel on a single computer and multiple threads can run in parallel in a single process.

BackgroundWorker Component Overview 1 Multithreading with the BackgroundWorker Component 3 Walkthrough Running an Operation in the Background 10 How

VS08 This One Goes to Going Parallel with PFX, PLINQ, TPL and Async Keywords

C# Threading. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

The Open Core Interface SDK has to be installed on your development computer. The SDK can be downloaded at:

Chapter 13 Working with Threads

Event-based Asynchronous Pattern Overview 1 Implementing the Event-based Asynchronous Pattern 5 Deciding When to Implement the Event-based

Asynchronous Programming Demystified

Software Exception Flow Analysis for WPF C# Asynchronous Programs Using Microsoft Visual Studio

An Async Primer. By Bill Wagner August Introduction

The Various Faces of the.net Task Parallel Library

JS Event Loop, Promises, Async Await etc. Slava Kim

ArcGIS Pro SDK for.net: Asynchronous Programming and MVVM Patterns in Pro. Wolfgang Kaiser

DNParallel - Version: 3. TPL Dataflow

Concurrency: An Overview

Ocean Wizards and Developers Tools in Visual Studio

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

Batches and Commands. Overview CHAPTER

ArcGIS Pro SDK for.net Intro and Pro Add-in Programming Patterns. Wolfgang Kaiser

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

POSIX Threads: a first step toward parallel programming. George Bosilca

C# Asynchronous Programming Model

CS 160: Interactive Programming

Task-based Asynchronous Pattern 1 Implementing the Task-based Asynchronous Pattern 8 Interop with Other Asynchronous Patterns and Types 14

ArcGIS Pro SDK for.net Beginning Pro Customization. Charles Macleod

CS 3305 Intro to Threads. Lecture 6

Multithreading and Interactive Programs

Asynchronous Events on Linux

1 de :02

Microsoft Visual C# Step by Step. John Sharp

Asynchronous Programming

Microsoft. Microsoft Visual C# Step by Step. John Sharp

High Performance Computing Course Notes Shared Memory Parallel Programming

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Joe Hummel, PhD. Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago.

ArcGIS Pro SDK for.net: Add-in Fundamentals and Development Patterns. Wolf Kaiser, Uma Harano

PCAP Assignment I. 1. A. Why is there a large performance gap between many-core GPUs and generalpurpose multicore CPUs. Discuss in detail.

UriQuery query.add + query.tostring()

Agus Kurniawan Microsoft MVP. Blog

Java Concurrency in practice Chapter 9 GUI Applications

Schlumberger Private Customer Use

Simplifying Asynchronous Programming with Microsoft Visual Studio Async CTP

Swift 5, ABI Stability and

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

IBSDK Quick Start Tutorial for C# 2010

Concurrency: Deadlock and Starvation. Chapter 6

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

Ocean Framework for Studio Extending the Ecosystem

Grand Central Dispatch and NSOperation. CSCI 5828: Foundations of Software Engineering Lecture 28 12/03/2015

Synchronising Threads

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Windows 8. Rainer Stropek. System Architecture. System Architecture re of Windows Store Apps. Saves the day. software architects gmbh

Concurrent Programming in C++ Venkat

Microsoft.CerTkiller v by.SANDRA.65q

CS 450 Operating System Week 4 Lecture Notes

Processes. Process Concept

New CAL Functions in Dynamics NAV 2017

Professional ASP.NET Web Services : Asynchronous Programming

Accurate study guides, High passing rate! IT TEST BOOK QUESTION & ANSWER. Ittestbook provides update free of charge in one year!

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Processes and Threads

CSCE 313: Intro to Computer Systems

Lecture 24: Java Threads,Java synchronized statement

An Introduction to What s Available in.net 4.0 PARALLEL PROGRAMMING ON THE.NET PLATFORM

Virtual Machine Design

Break Through Your Software Development Challenges with Microsoft Visual Studio 2008

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

MRCP. Client Integration Manual. Developer Guide. Powered by Universal Speech Solutions LLC

Schlumberger Private Customer Use

CS510 Operating System Foundations. Jonathan Walpole

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

C# 6.0 in a nutshell / Joseph Albahari & Ben Albahari. 6th ed. Beijin [etc.], cop Spis treści

(Refer Slide Time: 1:26)

COMP 3430 Robert Guderian

Implementation of Process Networks in Java

Multithreading Deep Dive. my twitter. a deep investigation on how.net multithreading primitives map to hardware and Windows Kernel.

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Item 18: Implement the Standard Dispose Pattern

Windows Presentation Foundation Visual Studio.NET 2008

Definition Multithreading Models Threading Issues Pthreads (Unix)

Programming C# 5.0. Ian Griffiths O'REILLY' Beijing Cambridge * Farnham Kbln Sebastopol Tokyo

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

SOA Distilled. Udi Dahan The Software Simplist..NET Development Expert & SOA Specialist Microsoft Solutions Architect MVP

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Managed Threading 1 Managed Threading Basics 3 Threads and Threading 5 Synchronizing Data for Multithreading 8 Foreground and Background Threads 11

Thread. A Thread is a concurrent unit of execution. The thread has its own call stack for methods being invoked, their arguments and local variables.

Chapter 4: Multi-Threaded Programming

COMIC BOOK GRAPHIC NOVEL APPROACH TO ASYNC

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Manage program flow. Have you read page xxi? Objectives in this chapter:

ASYNCHRONOUS PROGRAMMING IN C# 5 WITHOUT USE OF MULTIPLE THREADS


Asynchronous Functions in C#

Lecture 29: Java s synchronized statement

Transcription:

Parallelizing Ocean plug-in computations using the Background Worker + PFX pattern Dmitriy Repin Program Architect, Schlumberger PTS Ocean Development Framework User Meeting Houston, October 24, 2014

2014 Schlumberger. All rights reserved. An asterisk is used throughout this presentation to denote a mark of Schlumberger. Other company, product, and service names are the properties of their respective owners.

User requirements for a Petrel plug-in Deliver the result faster Do the work in parallel, I ve got the cores! Stay responsive to user interactions Do not freeze the Petrel UI Allow me to do something else (e.g., view the data) while the computation proceeds Report your progress Provide a way to cancel the computation, if needed

Petrel ecosystem constraints Windows Forms / WPF thread affinity Controls can only be used on the main application thread. Must use ISynchronizeInvoke.Invoke() to access from other thread Most of the Ocean API (with small exceptions, e.g., Seismic and StructuralFramework) does not support asynchronous access Ocean domain objects must be accessed on the main application thread Petrel does not use locking while accessing objects through UI No application-wide deadlock prevention strategy

Given the limitations, what is possible? Live dangerously: Perform parallel computations in the background without locking the data Be ready to crush - user can delete/change inputs at any moment Freeze conservatively: Spawn threads / tasks from the main thread and block it until the computation is complete. Good for a short-running operations Beware of Application.DoEvents() use for progress reporting! Use optimistic concurrency: 1) Copy the data to a local buffer 2) Perform parallel computations in the background using the buffer 3) Copy the result back, if possible Good for long-running operations when the computation is more expensive than the data/result copy

Our original implementation (before Petrel 2014) BackgroundWorker + Parallel.For = Usability Petrel is alive even with near 100% CPU load Provide staged progress reporting Step <N> of <L>: <M>% competed Provide cooperative cancellation It takes some time to stop computation

Optimistic concurrency activity diagram Background computation Compute Work item Compute Work item Main thread Background thread Thread pool thread Compute Work item Progress reporting Cancellation Data Preparation Copy the data to a local buffer Updating Petrel & plug-in UI Result publishing Update domain objects

EAP and BackgroundWorker Event-based Asynchronous Pattern from Microsoft Perform an operation asynchronously Receive a notification when the operation completes Communicate with the operation using events/delegates Support for cooperative cancellation and progress reporting BackgroundWorker

Simplified example of BackgroundWorker backgroundworker.runworkerasync(); backgroundworker.cancelasync(); void backgroundworker_dowork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; } for (int i = 1; i < WorkItemCount; i++) { if (worker.cancellationpending == true) { e.cancel = true; break; } SmoothInline(data, i); worker.reportprogress((int)(100.0f*i/workitemcount); } TODO: Parallelize this void backgroundworker_progresschanged(object sender, ProgressChangedEventArgs e) { resultlabel.text = (e.progresspercentage.tostring() + "%"); } void backgroundworker_runworkercompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.cancelled == true){ resultlabel.text = "Canceled!"; } else if (e.error!= null){ resultlabel.text = "Error: " + e.error.message; } else { resultlabel.text = "Done! + e.result.tostring(); } }

System.Threading.Tasks.Parallel.For Parallel Extensions for the.net Framework (PFX) include Parallel LINQ or PLINQ The System.Threading.Tasks.Parallel class The System.Threading.Tasks.Task parallelism constructs The concurrent collections Parallel class supports basic data parallel computations: Operation is performed concurrently on elements in a array Scheduled on the thread pool and managed by the Task Scheduler Blocks until all work is completed After an exception, workers are stopped and AggregateException is thrown

Optimistic concurrency activity diagram Background computation Compute Work item Compute Work item Main thread Background thread Thread pool thread Compute Work item Progress reporting Cancellation Data Preparation Copy the data to a local buffer Updating Petrel & plug-in UI Result publishing Update domain objects

Putting it all together Data preparation (main thread) Copy the Ocean domain object data to a local computation parameter buffer Initialize a background worker. Run it asynchronously with the buffer as an argument Keep updating Petrel / plug-in UI (main thread) Listening to ProgressChanged event and when it is received, update the progress bar Call CancelAsync() if user wants to stop the computation DoWork event handler (background thread) Divide the computation work into an number of small work items and schedule them for execution using Parallel.For() Block the DoWork handler until all items are processed Upon the DoWork completion, the RunWorkerCompleted event will be raised Compute work item / Parallel.For body (thread pool thread) Check CancellationPending and quit if cancellation was requested Perform a part of the computation and report the progress RunWorkerCompleted event handler (main thread) Check the RunWorkerCompletedEventArgs If no errors or cancellation occurred, update the domain object using the computation result

Petrel 2014 implementation using Async/Await Visual Studio 2012 (.NET 4.5) introduced a simplified approach to asynchronous programming Async / Await pattern The compiler does the difficult work that the developer used to do, and your application retains a logical structure that resembles synchronous code. As a result, you get all the advantages of asynchronous programming with a fraction of the effort.

Petrel 2014 implementation using Async/Await

Petrel 2014 implementation using Async/Await

Petrel 2014 implementation using Async/Await Demo

Outstanding issues: Major Ocean/Petrel multithreading issues Ocean/Petrel does not use locking while accessing objects and UI No an application-wide deadlock prevention strategy Minor issues To accomodate.net4.0 Slb.Ocean.Petrel.IProgress needs to support System.Threading.CancellationToken SetProgressText(String) does not work for NewAsyncProgress

References (1) Pro Asynchronous Programming with.net, Richard Blewett, Andrew Clymer http://www.amazon.com/dp/b00drefxna/ (2) Parallel Processing and Concurrency in the.net Framework http://msdn.microsoft.com/en-us/library/hh156548.aspx (3) Asynchronous Programming with Async and Await http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx (4) Async in C# 5.0, Alex Davies http://www.amazon.com/dp/1449337163/ (5) Threading in C#, Joseph Albahari http://www.albahari.com/threading/