8.7 Data Parallelism vs Task Parallelism

CONCURRENCYSTATUS: DEVELOPINGLEVEL: INTERMEDIATEREVIEWED: 2026-11-18
Before this page Processes vs Threads

8.7.1 Motivation

Parallel execution splits into two patterns. Data parallelism runs the same operation over partitions of data. Task parallelism splits the work into independent jobs, which can run different code on different inputs. The choice decides which synchronization you need, how load balances, and whether race conditions can happen at all.

8.7.2 The problem

Consider a simple operation: add 1 to each element of a vector.

# Data parallelism: same operation, many data
x <- 1:1000000
result <- x + 1

The operation is + 1. The data varies: each element gets the same treatment.

Now consider a different workload.

# Task parallelism: independent jobs, one per URL
downloads <- list(url1, url2, url3, url4)
results <- lapply(downloads, function(url) {
  download.file(url, destfile = tempfile())
})

Each task is its own job: a download from a different URL, which takes as long as that server and that file take. The data (the URL) is part of the task definition, not a slice of a larger data set.

In data parallelism, each worker handles a chunk of the same vector. In task parallelism, each worker handles a distinct job, and the jobs need not take the same time.

8.7.3 Mental model

Data parallelism: Partition the data. Replicate the operation. Each worker runs the operation on its partition. All workers do identical work on different data.

Task parallelism: Partition the work. Each worker runs a different task. Workers can process different data, execute different code, or both.

Data parallelism is often embarrassingly parallel. When partitions touch only their own data, workers never interfere, and the only synchronization is collecting the results at the end. Not every data-parallel job is that clean: a sum needs a reduction step, and a stencil over a grid needs each worker to see its neighbors’ edges.

Task parallelism usually needs more coordination. Tasks can read shared data, write shared state, or produce results that other tasks consume. Tasks that share mutable state must use mutexes or other primitives to avoid race conditions.

8.7.4 Mechanism

Data parallelism: static and simple

Data-parallel code splits the data into chunks before execution.

# Pseudocode: parallel addition
# Workers do not interact during compute
x <- 1:1000000
chunks <- split(x, ceiling(seq_along(x) / 250000))

# Worker 1 computes 1:250000 + 1
# Worker 2 computes 250001:500000 + 1
# Worker 3 computes 500001:750000 + 1
# Worker 4 computes 750001:1000000 + 1

# Gather results: no contention
result <- combine_results(partial_results)

Workers compute in parallel. They do not share state during compute. The only synchronization is the final gather.

Load balance is easy if chunks are equal. A matrix multiply across four workers: each gets a quarter. Each quarter takes about the same time, because dense arithmetic does the same number of operations whatever the values are. All workers finish together.

Load balance breaks if chunks are unequal. A machine learning pipeline that processes images: some images are small, some are large. A worker finishes its image early and idles while others work. This is load imbalance, and how finely the work is cut, its task granularity, decides how much of it a scheduler can repair.

Task parallelism: dynamic and complex

Task parallelism spawns independent jobs. Each job can have its own data.

# Pseudocode: download URLs in parallel
# Each task is independent
downloads <- list(
  list(url = urls[1], dest = "/tmp/a"),
  list(url = urls[2], dest = "/tmp/b"),
  list(url = urls[3], dest = "/tmp/c")
)

# Each worker runs a different download
# No two workers fetch the same URL
# No worker interferes with another

If tasks access shared state, synchronization becomes necessary.

# Task parallelism with shared state
counter <- 0  # Shared mutable state
increment_and_get <- function() {
  counter <<- counter + 1
  counter
}

# Suppose two threads each call increment_and_get() 1000 times
# on the same counter. Without a mutex, their read, add and
# write steps interleave and the counter loses updates.
# GNU R cannot run R code on two threads at once. With
# parallel's worker processes, each worker gets its own copy
# of counter: no update races with another, and the parent's
# counter never sees any of the workers' increments.

Load balance in task parallelism depends on task distribution. If the scheduler assigns tasks evenly, balance is good. If tasks vary in size, imbalance can occur. Work stealing can help.

8.7.5 The practical distinction

Data parallelism fits:

  • Vector and matrix operations (add, multiply, filter)
  • Applying a function to each element of a data frame
  • Image processing on a batch of images
  • Simulations where each worker computes a replicate from the same model

Task parallelism fits:

  • Web scraping or downloading multiple URLs
  • Database queries to different tables
  • Fitting several different models to the same data set
  • Divide-and-conquer recursion (each branch is a task)

8.7.6 Synchronization needs

Data parallelism can avoid race conditions by design.

If each worker touches only its partition and partitions do not overlap, no two workers write to the same memory. No race can occur. No lock is needed.

Task parallelism gets this guarantee only when its tasks share no mutable data. If tasks touch shared data, synchronization is mandatory.

# Task parallelism: shared state
tasks <- list(
  function() { write_result_1() },
  function() { write_result_2() },
  function() { update_counter() },
  function() { update_counter() }
)
# Tasks 3 and 4 both write to counter
# Without synchronization, the result is uncertain

8.7.7 A simple example: quicksort

Quicksort naturally suggests task parallelism.

quicksort(array):
  if length <= 1:
    return array

  pivot = choose_pivot(array)
  left = filter(array, < pivot)
  equal = filter(array, == pivot)
  right = filter(array, > pivot)

  // Task parallelism: spawn two independent tasks
  left_sorted = spawn quicksort(left)
  right_sorted = spawn quicksort(right)

  left_sorted = join(left_sorted)
  right_sorted = join(right_sorted)

  return concatenate(left_sorted, equal, right_sorted)

Each spawn creates a new task. The two tasks do not interfere, because they sort different partitions, and the tree of tasks grows as deep as the recursion.

The data-parallel version cuts the array into four chunks and has each worker sort its chunk with the same sequential sort. There is no nesting, but the four sorted chunks still have to be merged at the end.

8.7.8 Tradeoffs and R workloads

Data parallelism wins when:

  • The same operation applies uniformly
  • Chunks have similar size
  • Synchronization cost matters relative to compute

Task parallelism wins when:

  • Tasks run different code or take very different times
  • Tasks need to coordinate (producer-consumer patterns)
  • Load imbalance calls for dynamic scheduling

R’s own tools follow the data-parallel shape. lapply() and vectorized operations apply one function across the elements of a vector or list, and parallel::parLapply() and parallel::mclapply() run exactly that shape on several worker processes. Work stealing can help task parallelism balance load, but task parallelism still requires careful synchronization if shared state exists.

flowchart LR
  A["data parallelism"] -->|same operation| B["Partition<br/>Data"]
  B -->|chunk 1| C["Worker 1"]
  B -->|chunk 2| D["Worker 2"]
  B -->|chunk 3| E["Worker 3"]
  C -->|results| F["Gather"]
  D -->|results| F
  E -->|results| F

  G["task parallelism"] -->|different operations| H["Task Queue"]
  H -->|job 1| I["Worker 1"]
  H -->|job 2| J["Worker 2"]
  H -->|job 3| K["Worker 3"]
  I -->|results| L["Collect"]
  J -->|results| L
  K -->|results| L

Data parallelism partitions data and replicates the operation. Task parallelism partitions work and runs different operations concurrently.

Further reading

  • “Introduction to Parallel Computing Tutorial” by Blaise Barney and Donald Frederick, Lawrence Livermore National Laboratory. Covers the data-parallel model, domain versus functional decomposition, load balancing and granularity. https://hpc.llnl.gov/documentation/tutorials/introduction-parallel-computing-tutorial
  • Mattson, T. G., Sanders, B. A., & Massingill, B. L. (2004). Patterns for Parallel Programming. Addison-Wesley. Chapters on data and task parallelism patterns.
  • OpenMP specifications. The worksharing-loop construct is the data-parallel side and the task construct the task-parallel side. https://www.openmp.org/specifications/

Where this appears

  • threaded-r The choice between data and task parallelism shapes which synchronization patterns threaded-r must support.
  • r-in-rust (rrs) rrs can express both via rayon’s data-parallel iterators and task-spawning APIs.

Where to go next

Back to top