CS61C: Great Ideas in Computer Architecture (aka Machine Structures)
Lecture 29: Process-Level Programming
Instructors: Lisa Yan, Justin Yokota
#
CS 61C
Spring 2024
Agenda
2
CS 61C
Spring 2024
Multithreading vs Multiprocess Code
3
CS 61C
Spring 2024
Multiprocess Framework Overview
4
CS 61C
Spring 2024
Multiprocess Mayhem
5
CS 61C
Spring 2024
Multiprocess Framework Overview
6
CS 61C
Spring 2024
Open MPI
7
CS 61C
Spring 2024
Open MPI
8
CS 61C
Spring 2024
Aside: Naming of OpenMP vs Open MPI
9
CS 61C
Spring 2024
Open MPI: Setup
10
CS 61C
Spring 2024
Open MPI: Process Identification
11
CS 61C
Spring 2024
Open MPI: Example
int main(int argc, char** argv) {� if (argc != 2) {� printf("Usage: %s <foldername>\n", argv[0]);� return 1;� }� MPI_Init(&argc, &argv);� int processID, clusterSize;� MPI_Comm_size(MPI_COMM_WORLD, &clusterSize);� MPI_Comm_rank(MPI_COMM_WORLD, &processID);� ... //Actual Code� MPI_Finalize();�}
12
CS 61C
Spring 2024
Open MPI: Process Identification
13
CS 61C
Spring 2024
Open MPI: Communication
14
CS 61C
Spring 2024
Open MPI: Communication
15
CS 61C
Spring 2024
Open MPI: Communication
16
CS 61C
Spring 2024
Application to Matrix Multiplication
17
CS 61C
Spring 2024
Multiprocessing ManyMatMul: Naive Approach
Have process 0 do tasks 0-99�Have process 1 do tasks 100-199�…�Have process 999 do tasks 99900-99999
18
CS 61C
Spring 2024
MPI Example: The Manager-Worker framework
19
CS 61C
Spring 2024
Manager Pseudocode
Set up�While there's work to do:� Wait until a worker says "I'm ready for more work" (recv from all)� Find the next task to do� Send to the worker what task to do�Repeat #Worker times:� Wait until a worker says "I'm ready for more work" (recv from all)� Send to the worker "All work done"�Finalize�
20
CS 61C
Spring 2024
Worker Pseudocode
Set up�While True:� Send to the manager "I'm ready for more work"� Receive message from manager� If message is "Here's more work":� Do the work� Else if message is "All work done":� break�Finalize
21
CS 61C
Spring 2024
How to send messages?
22
CS 61C
Spring 2024
Multiprocessing ManyMatMul: Manager-Worker Approach
23
CS 61C
Spring 2024
Multiprocess+Multithreading?
24
CS 61C
Spring 2024
Performance Programming Overview
25
Optimization | Max Speedup | Pros | Cons |
Register/Function Inlining | <2x | Easy change, reduces memory accesses | Minimal effect, optimizing compiler might do this already |
Loop Unrolling | <2x | Reduces Branching | Minimal effect, significant penalty to maintainability |
Cache Optimizations | ~10x | Surprisingly good | Often requires algorithmic changes |
SIMD | ~8x | Fairly applicable, minimal overhead | Limited by hardware, often hit hard by Amdahl's Law |
Multithreading/OpenMP | #cores/node | More flexible than SIMD and MPI, generally | Concurrency issues, high overhead |
Multiprocess/Open MPI | #cores | Can be extended arbitrarily large | Expensive communication, high overhead |
CS 61C
Spring 2024