A C++ implementation of the MapReduce paradigm with multi-threading support for parallel data processing.
This framework provides a thread-safe MapReduce implementation that allows clients to process large datasets in parallel across multiple stages: Map → Shuffle → Reduce.
- Multi-threaded execution with configurable thread count
- Automatic shuffling and sorting of intermediate results
- Thread-safe operations with atomic counters and mutexes
- Progress tracking via job state queries
- Barrier synchronization between phases
Using CMake:
cmake -B build
cmake --build buildUsing provided Makefile:
makeclass MyClient : public MapReduceClient {
public:
void map(const K1* key, const V1* value, void* context) const override {
// Your map logic
emit2(k2, v2, context);
}
void reduce(const IntermediateVec* pairs, void* context) const override {
// Your reduce logic
emit3(k3, v3, context);
}
};class MyK1 : public K1 {
bool operator<(const K1 &other) const override { /* ... */ }
};
// Implement V1, K2, V2, K3, V3...MyClient client;
InputVec inputVec = /* your input data */;
OutputVec outputVec;
JobHandle job = startMapReduceJob(client, inputVec, outputVec, 4);
waitForJob(job);
closeJobHandle(job);JobHandle startMapReduceJob(const MapReduceClient& client,
const InputVec& inputVec,
OutputVec& outputVec,
int multiThreadLevel);Starts a MapReduce job with specified number of threads.
void waitForJob(JobHandle job);Blocks until the job completes.
void getJobState(JobHandle job, JobState* state);Queries current job progress (stage and percentage).
void closeJobHandle(JobHandle job);Cleans up job resources. Waits for completion if not already done.
void emit2(K2* key, V2* value, void* context); // Call in map()
void emit3(K3* key, V3* value, void* context); // Call in reduce()┌─────────────────────────────────────────┐
│ MapReduce Framework │
├─────────────────────────────────────────┤
│ Input Phase │
│ └─ Distribute across threads │
├─────────────────────────────────────────┤
│ MAP Phase (parallel) │
│ └─ Each thread processes inputs │
│ └─ Emits intermediate (K2,V2) pairs │
├─────────────────────────────────────────┤
│ SORT Phase (parallel) │
│ └─ Each thread sorts its results │
├─────────────────────────────────────────┤
│ SHUFFLE Phase (single thread) │
│ └─ Merge-sort all intermediate data │
│ └─ Group by K2 keys │
├─────────────────────────────────────────┤
│ REDUCE Phase (parallel) │
│ └─ Process each K2 group │
│ └─ Emits final (K3,V3) pairs │
└─────────────────────────────────────────┘
- Atomic operations for thread-safe counters
- Barrier synchronization between Map/Sort/Shuffle/Reduce phases
- Per-thread intermediate storage to minimize lock contention
- Single atomic variable for job state (stage + progress)
- Mutex-protected output to prevent race conditions
| File | Purpose |
|---|---|
MapReduceFramework.cpp/h |
Main framework implementation |
MapReduceClient.h |
Client interface and type definitions |
Barrier.cpp/h |
Thread barrier synchronization |
CMakeLists.txt |
CMake build configuration |
Makefile |
Alternative build system |
- Map phase: Lock-free (per-thread vectors)
- Sort phase: Lock-free (independent sorting)
- Shuffle phase: Single-threaded (thread 0 only)
- Reduce phase: Lock-free reads, mutex-protected output writes
- C++14 or later
- pthreads support
- CMake 3.1+ (for CMake build)
Optional sanitizer flags in CMakeLists.txt:
- ThreadSanitizer: Detects data races
- AddressSanitizer: Detects memory errors
Uncomment the appropriate lines to enable.
Yonghao Lee
Part of Operating Systems course project