A high-performance multithreaded text analysis system implemented in C++ using pthreads for parallel processing of large text datasets. The system processes UN document XML files to perform comprehensive word frequency analysis and linguistic statistics.
- Multithreaded Processing: Utilizes 8 worker threads for parallel text processing
- CPU Affinity Optimization: Enhanced versions with CPU core pinning for maximum performance
- Memory Efficient: Processes text in 4KB chunks to handle large files without excessive memory usage
- Comprehensive Analysis:
- Total word count statistics
- Unique word identification
- Word frequency analysis with sorting
- Vowel-starting word detection
- XML Processing Pipeline: Complete workflow from raw XML to clean text analysis
- Linux/Unix system (for CPU affinity features)
- GCC compiler with C++11 support
- pthread library
- Python 3.x (for preprocessing scripts)
# C++ compilation requirements
sudo apt-get install build-essential
sudo apt-get install libpthread-stubs0-dev
# Python requirements (no additional packages needed)
python3 --version- Clone the repository
git clone https://github.com/yourusername/repo.git
cd parallel-text-processing-
Prepare your dataset
- Place UN XML files in the directory structure:
un/xml/en/y20XX/ - Update paths in
Script1.pyas needed
- Place UN XML files in the directory structure:
-
Compile the C++ programs
# Basic versions
g++ -std=c++11 -pthread -O2 program.cpp -o word_analyzer
g++ -std=c++11 -pthread -O2 program.cpp -o frequency_analyzer
# CPU affinity optimized versions
g++ -std=c++11 -pthread -O2 -D_GNU_SOURCE program.cpp -o word_analyzer_affinity
g++ -std=c++11 -pthread -O2 program.cpp -o frequency_analyzer_affinitypython3 Script1.pyThis extracts all text content from XML files and creates text.txt.
python3 Script2.pyThis removes XML tags and creates clean_text.txt.
Basic Word Analysis:
./word_analyzerEnhanced Frequency Analysis:
./frequency_analyzerCPU Affinity Optimized Versions:
./word_analyzer_affinity
./frequency_analyzer_affinity- Real-time processing status
- Total word count
- Unique word count
- Words starting with vowels
- Top 10 most frequent words
- Execution time
3output.txt: Complete word frequency analysis (sorted by frequency)3affinityoutput.txt: Output from CPU affinity optimized version
Modify the thread count in the source files:
#define numberofthreads 8 // Adjust based on your CPU coresAdjust processing chunk size:
#define sizeofchunk 4096 // Modify for memory/performance balanceUpdate input/output paths in Python scripts:
input_directory = r"path/to/your/xml/files"
output_file = r"output/text.txt"The affinity versions include:
- Thread pinning to specific CPU cores
- Reduced context switching overhead
- Better cache locality
- Optimal resource utilization
- Efficient string handling with move semantics
- Reserved buffer allocation
- Thread-local data structures
- Minimal memory footprint per thread
- Mutex-protected task queue
- Condition variable signaling
- Lock-free local processing
- Efficient thread coordination
Compare performance between versions:
# Test basic version
time ./word_analyzer
# Test optimized version
time ./word_analyzer_affinity
# Monitor CPU usage
htop- File Reading: Sequential chunk-based file reading
- Task Distribution: Producer-consumer pattern with thread-safe queue
- Parallel Processing: Each thread processes assigned text chunks
- Word Extraction: Character-by-character parsing with case normalization
- Local Aggregation: Thread-local frequency maps
- Global Merging: Final consolidation of results
- Sorting & Output: Frequency-based sorting and file generation
- Mutex Protection: Shared queue access control
- Condition Variables: Efficient thread wake-up mechanism
- Atomic Operations: Safe flag checking and updates
- Fork the repository
- Create a feature branch (
git checkout -b feature/improvement) - Commit changes (
git commit -am 'Add new feature') - Push to branch (
git push origin feature/improvement) - Create Pull Request
- UN Parallel Corpus for test data
- pthread library for multithreading support
- Linux kernel for CPU affinity capabilities