Parallel Prefix Sum Algorithm Explained
Learn about the Parallel Prefix Sum algorithm, a technique used to efficiently compute prefix sums of an array in parallel. Discover how the recursive doubling method enables this algorithm to overcome inherent sequential challenges, showcasing a beautiful parallel computing approach.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Given array A[0..N-1], produce B[N], such that B[k] is the sum of all elements of A upto A[k] Prefix Sum Problem 0 1 2 3 4 5 6 7 A 3 1 4 2 5 2 7 4 + + + + + B 3 4 8 10 15 17 24 28 B[3] is the sum of A[0], A[1], A[2], A[3] But B[3] can also be calculated as B[2]+ A[3] 1 Charm Tutorial
Parallel Prefix Data dependency from iteration to iteration. How can this be parallelized at all? B[0] = A[0]; for (i=1; i<N; i++) B[i] = B[i-1] + A[i]; It looks like the problem is inherently sequential, but theoreticians came up with a beautiful algorithm called recursive doubling or just parallel prefix 2 Charm Tutorial
Parallel prefix : recursive doubling 0 1 2 3 4 5 6 7 N Data Items P Processors N=P 5 3 7 2 1 3 1 2 5 8 10 9 3 4 4 3 Log P Phases P additions in each phase P log P operations Completes in O(logP) time 5 8 15 17 13 13 7 7 5 8 15 17 18 21 22 24 3 Charm Tutorial
Parallel Prefix Example: prefix.ci mainmodule prefix { readonly CProxy_Main mainProxy; readonly CProxy_Prefix prefixArray; readonlyint numElements; mainchare Main { entry Main(CkArgMsg* msg); entry [reductiontarget] void done(); }; array [1D] Prefix { entry Prefix(); entryvoid step(); entryvoid passValue(int value); } } Charm Tutorial 4
#include "prefix.decl.h" #include <math.h> /*readonly*/ CProxy_Main mainProxy; /*readonly*/ CProxy_Prefix prefixArray; /*readonly*/ int numElements; class Main : public CBase_Main { public: Main(CkArgMsg *msg) { mainProxy = thisProxy; numElements = (msg->argc > 1) ? atoi(msg->argv[1]) : 8; delete msg; prefixArray = CProxy_Prefix::ckNew(numElements); } void done() { CkExit(); } }; Charm Tutorial 5
class Prefix : public CBase_Prefix { int value, distance; public: Prefix() : distance(1) { srand(time(NULL)); value = rand() % 10; step(); } ... Charm Tutorial 6
void step() { if (distance >= numElements) { CkPrintf("Prefix[%d].value = %d\n", thisIndex, value); CkCallback cb(CkReductionTarget(Main, done), mainProxy); contribute(sizeof(int), &value, CkReduction::sum_int, cb); } else { if (thisIndex+distance < numElements) thisProxy[thisIndex + distance].passValue(value); } } } void passValue(int incoming_value) { ... } }; #include "prefix.def.h" void passValue(int incoming_value) { value += incoming_value; distance *= 2; step(); } Charm Tutorial 7
void step() { if (distance >= numElements) { CkPrintf("Prefix[%d].value = %d\n", thisIndex, value); CkCallback cb(CkReductionTarget(Main, done), mainProxy); contribute(sizeof(int), &value, CkReduction::sum_int, cb); } else { if (thisIndex+distance < numElements) thisProxy[thisIndex + distance].passValue(value); } //if you no longer receive, but need to continue sending if (thisIndex - distance < 0) { distance = distance*2; step(); } } } void passValue(int incoming_value) { ... } }; #include "prefix.def.h" void passValue(int incoming_value) { value += incoming_value; distance *= 2; step(); } Charm Tutorial 8
void step() { if (distance >= numElements) { CkPrintf("Prefix[%d].value = %d\n", thisIndex, value); CkCallback cb(CkReductionTarget(Main, done), mainProxy); contribute(sizeof(int), &value, CkReduction::sum_int, cb); } else { if (thisIndex+distance < numElements) thisProxy[thisIndex + distance].passValue(value); } //if you no longer receive, but need to continue sending if (thisIndex - distance < 0) { distance = distance*2; step(); } } } void passValue(int incoming_value) { ... } }; #include "prefix.def.h" Still wrong Parallel Prefix: Why? void passValue(int incoming_value) { value += incoming_value; distance *= 2; step(); } Charm Tutorial 9
Parallel Prefix Example, Correct Version: prefix.ci mainmodule prefix { readonly CProxy_Main mainProxy; readonly CProxy_Prefix prefixArray; readonlyint numElements; readonly int numStages; mainchare Main { entry Main(CkArgMsg* msg); entry [reductiontarget] void done(); }; array [1D] Prefix { entry Prefix(); entryvoid step(); entryvoid passValue(int stage, int value); } } Charm Tutorial 10
#include "prefix.decl.h" #include <math.h> /*readonly*/ CProxy_Main mainProxy; /*readonly*/ CProxy_Prefix prefixArray; /*readonly*/ int numElements; /*readonly*/ int numStages; class Main : public CBase_Main { public: Main(CkArgMsg *msg) { mainProxy = thisProxy; numElements = (msg->argc > 1) ? atoi(msg->argv[1]) : 8; numStages = (int) ceil(log2(numElements)); delete msg; prefixArray = CProxy_Prefix::ckNew(numElements); } void done() { CkExit(); } }; Charm Tutorial 11
class Prefix : public CBase_Prefix { int *flagBuf, *valueBuf, value, stage; public: Prefix() : stage(0) { srand(time(NULL)); value = rand() % 10; valueBuf = newint[numStages]; flagBuf = newint[numStages]; step(); } ... Charm Tutorial 12
void step() { if (stage >= numStages) { CkPrintf("Prefix[%d].value = %d\n", thisIndex, value); CkCallback cb(CkReductionTarget(Main, done), mainProxy); contribute(sizeof(int), &value, CkReduction::sum_int, cb); } else { int sendIndex = thisIndex + (1 << stage); // thisIndex + distance if (sendIndex < numElements) thisProxy[sendIndex].passValue(stage, value); void passValue(int stg, int val) { flagBuf[stg] = 1; valueBuf[stg] = val; if (flagBuf[stg] == 1) updateValue(); } if (flagBuf[stage] == 1) updateValue(); elseif (thisIndex - (1 << stage) < 0) { stage++; step(); } } } ... void updateValue() { value += valueBuf[stage]; flagBuf[stage] = 0; stage++; step(); } Charm Tutorial 13
Parallel Prefix with SDAG: prefix.ci mainmodule prefix { readonly CProxy_Main mainProxy; readonly CProxy_Prefix prefixArray; readonlyint numElements; readonlyint numStages; mainchare Main { entry Main(CkArgMsg* msg); entry [reductiontarget] void done(); }; array [1D] Prefix { entry Prefix(); entryvoid passValue(int incoming_stage, int incoming_val); entry void step_through() { ... }; } } Charm Tutorial 14
entryvoid step_through() { for (stage = 0; stage < numStages; stage++) { serial "send_value" { int sendIndex = thisIndex + (1 << stage); if (sendIndex < numElements) thisProxy[sendIndex].passValue(stage, value); } if (thisIndex - (1 << stage) >= 0) { when passValue[stage](int stg, int val) { serial { value += val; } } } } serial "done" { CkPrintf("Prefix[%d].value = %d\n", thisIndex, value); CkCallback cb(CkReductionTarget(Main, done), mainProxy); contribute(sizeof(int), &value, CkReduction::sum_int, cb); } }; Charm Tutorial 15
serial "done" { CkPrintf("Prefix[%d].value = %d\n", thisIndex, value); CkCallback cb(CkReductionTarget(Main, done), mainProxy); contribute(sizeof(int), &value, CkReduction::sum_int, cb); } }; Charm Tutorial 16