Site Tools


embarrassingly-parallel

Table of Contents

Embarrassingly parallel

Embarrassingly parallel describes a problem that can be split into independent pieces with no communication or synchronization between them. Each piece runs on its own core, thread, or machine, and results are collected at the end. The name reflects that there's nothing clever about the parallelization—the difficulty of real parallel problems (data dependencies, load balancing, synchronization) is absent.

Embarrassingly parallel workloads scale nearly linearly and often map better to Gustafson's law than Amdahl's law.

Example

This example shows embarrassingly parallel image processing.

// compile: gcc -O2 -o embarrass embarrass.c
// run: ./embarrass
// description: embarrassingly parallel workload with no inter-thread dependency
 
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <math.h>
 
int mandelbrot_iter(double x, double y) {
    double zx = 0, zy = 0;
    for (int i = 0; i < 100; i++) {
        double zx2 = zx * zx, zy2 = zy * zy;
        if (zx2 + zy2 > 4.0) return i;
        double tmp = zx2 - zy2 + x;
        zy = 2 * zx * zy + y;
        zx = tmp;
    }
    return 100;
}
 
int main() {
    int width = 4000, height = 4000;
    int* pixels = malloc(width * height * sizeof(int));
 
    #pragma omp parallel for schedule(static) collapse(2)
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double fx = (x - width/2.0) / (width/4.0);
            double fy = (y - height/2.0) / (height/4.0);
            pixels[y * width + x] = mandelbrot_iter(fx, fy);
        }
    }
 
    printf("Computed %d x %d Mandelbrot set\n", width, height);
    free(pixels);
    return 0;
}
embarrassingly-parallel.md · Last modified: by 127.0.0.1