4 Cloud Programming Models

Introduction to Cloud Computing · Unit 4 · 12 hrs

Cloud Programming Models

Exam-focused notes for Cloud Programming Models (Introduction to Cloud Computing, CSC481): what the TU syllabus asks and how it has actually been tested, with 3 solved past questions from this unit.

Cloud Programming Models in short

This unit covers the programming models used to write applications that run efficiently on cloud infrastructure: thread-based and task-based parallelism within a single application, and the MapReduce model for processing very large datasets across many machines at once.

Cloud Programming Models: notes

Thread Programming

A thread is a lightweight, independently schedulable sequence of instructions executed by a CPU within a process. A process is an instance of a running program with its own memory space and resources, and while multiple processes can run concurrently on one computer without sharing memory, the threads within a single process share that process's memory space and resources with each other. Thread programming is the technique of running multiple threads concurrently within one process to achieve parallelism, and in a cloud application it is used to let different parts of an application execute simultaneously, improving performance, scalability, and responsiveness while making efficient use of the underlying computing resources.

Thread programming, in general, follows a recurring pattern:

  • Thread creation: a thread is created either by the operating system directly or by a thread-management library provided by the programming language.
  • Thread synchronization: since threads within the same process access shared resources, mechanisms such as locks, mutexes, semaphores, and condition variables are needed to prevent conflicting access and keep shared data consistent.
  • Thread execution: a scheduler decides the order and duration of execution for each thread, based on factors such as thread priority, time slicing, and resource availability.
  • Thread communication: threads exchange information through mechanisms such as shared memory or explicit message passing.
  • Thread termination: a thread ends either because the program explicitly stops it, or because the operating system reclaims it once its work is complete.

Task Programming

Task programming is an approach to parallel programming in which the fundamental unit of work is a task rather than a thread. A task represents a discrete, independently executable piece of work, and task programming typically involves defining a set of tasks together with the dependencies between them, then handing them to a task scheduler that decides the order of execution automatically based on those dependencies and the resources currently available. The key building blocks are task definition (specifying units of work, usually through language constructs or libraries that support task parallelism), task dependencies (a task that needs another task's output as input cannot start until that other task finishes), and the task scheduler itself, which analyzes the dependency graph and schedules ready tasks onto available processors.

Task programming has several practical advantages over writing raw threaded code directly. The programmer creates tasks, not threads, so the underlying runtime is free to schedule those tasks across however many processors are actually available, letting the same code scale automatically as more cores are added. Because scheduling is handled by the runtime, multiple algorithms expressed as task graphs can run together without one significantly degrading the performance of another, and many complex problems can be expressed and solved without the programmer having to reach for lower-level synchronization tools such as mutexes and condition variables directly, since task functions themselves stay simple.

What this unit covers

  • Thread programming
  • Task programming
  • Map-reduce programming
  • Parallel efficiency of Map-Reduce
  • Enterprise batch processing using Map-Reduce
  • Comparisons between Thread, Task and Map reduce

Map-reduce programming

2081 · 10 marks

What is Map Reduce? Using an example, discuss how Map function, shuffling, and sorting, and Reduce function work in Map Reduce.[10]

Show solved answer

MapReduce: Concept, Components, and Working

Definition of MapReduce

MapReduce is a programming model and framework for processing and generating large datasets in a distributed computing environment. It allows developers to write programs that can process massive amounts of data in parallel across a cluster of computers, without worrying about the details of parallelization, fault tolerance, or load balancing.

MapReduce works by breaking a large computation into two main phases:

  1. Map Phase - processes input data and produces intermediate key-value pairs
  2. Reduce Phase - aggregates and summarizes the intermediate key-value pairs to produce the final output

Between these two phases, Shuffling and Sorting acts as an intermediate step that organizes the data.


Core Components of MapReduce

1. Map Function

  • Takes an input key-value pair and produces a set of intermediate key-value pairs
  • Processes each record independently and in parallel
  • Output: a list of (key, value) pairs

2. Shuffling and Sorting

  • Groups all intermediate values associated with the same intermediate key together
  • Transfers the mapped output from mapper nodes to the appropriate reducer nodes
  • Sorts the intermediate key-value pairs by key so that all values for the same key are grouped

3. Reduce Function

  • Takes a key and a set of values associated with that key
  • Applies aggregation, summarization, or other operations
  • Produces the final output key-value pairs
  • As stated in the notes: "The reduce function combines the values associated with each key, performing operations like aggregation, summarization, or generating the final output."

Example: Word Count Problem

Problem: Count the number of occurrences of each word in the following input text:

"hello world hello"
"world hello world"

Step 1: Input Splitting

The input is split into chunks and distributed across multiple mappers:

SplitContent
Split 1"hello world hello"
Split 2"world hello world"

Step 2: Map Phase

Each mapper reads its input split and emits intermediate (word, 1) key-value pairs for every word encountered.

Mapper 1 processes "hello world hello":

Input:  (line1, "hello world hello")

Output (Intermediate Key-Value Pairs):
  (hello, 1)
  (world, 1)
  (hello, 1)

Mapper 2 processes "world hello world":

Input:  (line2, "world hello world")

Output (Intermediate Key-Value Pairs):
  (world, 1)
  (hello, 1)
  (world, 1)

Step 3: Shuffling and Sorting

All intermediate key-value pairs from all mappers are collected, sorted by key, and grouped so that all values for the same key are sent to the same reducer.

After Shuffling and Sorting:

(hello, [1, 1, 1])
(world, [1, 1, 1])
  • All (hello, 1) pairs from both mappers are grouped together
  • All (world, 1) pairs from both mappers are grouped together
  • The keys are sorted alphabetically

This step ensures that each reducer receives all values for a particular key, regardless of which mapper produced them.


Step 4: Reduce Phase

Each reducer receives a key and its list of values, then applies the reduce function to produce the final result.

Reducer 1 processes (hello, [1, 1, 1]):

Input:  (hello, [1, 1, 1])
Operation: sum(1 + 1 + 1) = 3
Output: (hello, 3)

Reducer 2 processes (world, [1, 1, 1]):

Input:  (world, [1, 1, 1])
Operation: sum(1 + 1 + 1) = 3
Output: (world, 3)

Final Output

hello  -->  3
world  -->  3

Complete Flow Diagram

INPUT DATA
    |
    v
[Splitting] --> Split 1, Split 2, ...
    |
    v
[MAP PHASE]
Mapper 1: (hello,1),(world,1),(hello,1)
Mapper 2: (world,1),(hello,1),(world,1)
    |
    v
[SHUFFLE & SORT]
(hello,[1,1,1])
(world,[1,1,1])
    |
    v
[REDUCE PHASE]
Reducer 1: (hello, 3)
Reducer 2: (world, 3)
    |
    v
FINAL OUTPUT: hello=3, world=3

Summary Table

PhaseInputOperationOutput
MapRaw key-value pairsExtract and emit intermediate pairsIntermediate (key, value) pairs
Shuffle & SortIntermediate (key, value) pairsGroup by key, sortGrouped (key, [list of values])
Reduce(key, [list of values])Aggregate/summarizeFinal (key, value) pairs

Advantages of MapReduce

  • Handles large-scale data processing efficiently
  • Provides fault tolerance automatically
  • Achieves parallel efficiency by distributing work across multiple processors
  • As noted: "Parallel efficiency of MapReduce measures how well the system scales and utilizes available resources to perform the required computation in parallel"
  • Developers do not need to manage low-level parallelism details
Open this answer on its own page →

Thread programming

2080 · 10 marks

What is thread? Why multi-threading is used? Describe how thread programming is done in cloud.[10]

--- A thread is the smallest unit of execution within a process. It is a lightweight sub-process that shares the same memory space and resources (such as code, data, and files) of its parent process but executes independently. Multiple threads can exist within a single process, each performing a ...

Open this answer on its own page →

Parallel efficiency of Map-Reduce

2079 · 10 marks

What is parallel efficiency of map-reduce means? Compare between thread, task and map-reduce.[10]

--- Parallel efficiency of MapReduce means the effectiveness of utilizing computational resources and achieving speedup when executing a MapReduce job on a distributed computing system. It measures how well the system scales and utilizes available resources to perform the required computation in ...

Open this answer on its own page →