Docsity

Defect Patterns in High Performance Computing - Lecture Slides | CMSC 714, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC714
Defect Patterns in
High Performance Computing
Taiga Nakamura
(edits by A. Sussman)
University of Maryland
2
CMSC714
Background
•Debugging and testing parallel code is hard
– How can bugs be prevented or found/fixed effectively?
•“Knowing” common defects (bugs) will reduce the
time spent debugging
– Novice developers can learn how to detect/prevent them
– Someone may develop tools and/or improve language
•HPCS project is building “Defect patterns” for high
performance programming (HPC)
– Based on the empirical data collected in various studies
– Examples in this presentation are shown in C + MPI
(Message Passing Interface)
– Project is building a website called HPCBugBase
(http://www.hpcbugbase.org/ ) to share and evolve these
patterns
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download Defect Patterns in High Performance Computing - Lecture Slides | CMSC 714 and more Study notes Computer Science in PDF only on Docsity!

CMSC714 1

Defect Patterns in

High Performance Computing

Taiga Nakamura

(edits by A. Sussman)

University of Maryland

Background

• Debugging and testing parallel code is hard

  • How can bugs be prevented or found/fixed effectively?

• “Knowing” common defects (bugs) will reduce the

time spent debugging

  • Novice developers can learn how to detect/prevent them
  • Someone may develop tools and/or improve language

• HPCS project is building “Defect patterns” for high

performance programming (HPC)

  • Based on the empirical data collected in various studies
  • Examples in this presentation are shown in C + MPI

(Message Passing Interface)

  • Project is building a website called HPCBugBase

(http://www.hpcbugbase.org/ ) to share and evolve these

patterns

CMSC714 3

Differentiating Factors of HPC

  • Platform : Powerful computation power of today's HPC systems is achieved by massively parallel systems. Writing a scalable program on these systems is difficult.
  • Performance : Slow execution speed can be a defect even if the output is correct. Achieving good performance on multiple processors is often difficult
  • Language : Developers usually use special HPC languages and libraries (MPI, OpenMP, UPC, CAF, Titanium, ...), each with their own ways of handling issues such as communication and synchronization. SPMD (Single Program, Multiple Data) approach is dominant
  • Developers : Software often developed by scientists and grad students without formal training in software engineering. Traditional software engineering processes or practices are not necessarily used in HPC projects
  • Tools : The use of modern tools (IDEs, graphical debuggers, defect detection tools, profiling tools, etc.) is not as common as in other domains
  • Portability : Portability is very important for HPC applications since they must be run on various platforms depending on the computational resources available
  • Validation : Given the nature of HPC applications, the correct outputs are not always known, so debugging is particularly challenging and costly.

Example Problem

  • Consider the following problem:

1. N cells, each of which holds an integer [0..9]

  • E.g., cell[0]=2, cell[1]=1, …, cell[N-1]=

2. In each step, cells are updated using the values of neighboring cells

  • cell (^) next [x] = (cell[x-1] + cell[x+1]) mod 10
  • cell (^) next [0]=(3+1), cellnext [1]=(2+6), …
  • (Assume the last cell is adjacent to the first cell)

3. Repeat 2 for steps times

A sequence of N cells

2 1 6 8 7 1 0 2 4 5 1 …^3

What defects can appear when implementing a parallel solution in MPI?

CMSC714 7

Approach to a Parallel Version

  • Each process stores (1/size) of the cells
    • size :number of processes

2 1 6 8 7 1 0 2 4 5 1 …^3

Process 0

  • Each process needs to:
    • update the locally-stored cells
    • exchange boundary cell values between neighboring

processes (nearest-neighbor communication)

Process 1

Process ( size -1)

Process 2

Recurring HPC Defects

  • We simulate the process of writing parallel

code and discuss what kinds of defects can

appear.

  • Defect types are shown as:
    • Pattern descriptions (symptoms, causes, cures & preventions)
    • Concrete examples in MPI implementation

CMSC714 9

Pattern: Erroneous use of parallel language features

  • “Simple” mistakes that are common for novices: language

usage, choice of function, etc.

  • E.g., forgotten mandatory function calls for init/finalize
  • E.g., inconsistent parameter types between send and recv Symptoms:
  • Compile-type error (easy to fix)
  • Some defects may surface only under specific conditions
  • (number of processors, value of input, hardware/software

environment…)

Causes:

  • Lack of experience with the syntax and semantics of new

language features

Cures & preventions:

  • Understand subtleties and variations of language features
  • In a large code, confine parallel function calls to a particular

part of the code to help make fewer errors

/* Initialize MPI */ MPI_Status status; status = MPI_Init(NULL, NULL); if (status != MPI_SUCCESS) { exit(-1); }

/* Initialize cells */ fp = fopen("input.dat", "r"); if (fp == NULL) { exit(-1); } for (x = 0; x < N; x++) { fscanf(fp, "%d", &buffer[x]); } fclose(fp);

/* Main loop */ ...

/* Final output */ ...

/* Finalize MPI */ MPI_Finalize();

Adding basic MPI functions

What are the bugs?

CMSC714 13

Pattern: Space Decomposition

  • Incorrect mapping between the problem space and the

program memory space

Symptoms:

  • Segmentation fault (if array index is out of range)
  • Incorrect (maybe slightly incorrect) output Causes:
  • Mapping in parallel version can be different from that in

serial version

  • E.g., Array origin is different in every processor
  • E.g., Additional memory space for communication can

complicate the mapping logic

Cures & preventions:

  • Validate array origin, whether buffer includes guard buffers,

whether buffer refers to global space or local space, etc. -

these can change while parallelizing the code

  • Encapsulate the mapping logic to a dedicated function
  • Consider designing serial code that is easy to parallelize

MPI_Comm_size(MPI_COMM_WORLD &size); MPI_Comm_rank(MPI_COMM_WORLD &rank); nlocal = N / size; buffer = (int)malloc((nlocal+2) * sizeof(int)); nextbuffer = (int)malloc((nlocal+2) * sizeof(int));

/* Main loop / for (n = 0; n < steps; n++) { for (x = 0; x < nlocal; x++) { nextbuffer[x] = (buffer[(x-1+N)%N]+buffer[(x+1)%N]) % 10; } / Exchange boundary cells with neighbors */ ... tmp = buffer; buffer = nextbuffer; nextbuffer = tmp; }

Decompose the problem space

buffer[]

0 (nlocal+1)

What are the bugs?

CMSC714 15

MPI_Comm_size(MPI_COMM_WORLD &size); MPI_Comm_rank(MPI_COMM_WORLD &rank); nlocal = N / size; buffer = (int)malloc((nlocal+2) * sizeof(int)); nextbuffer = (int)malloc((nlocal+2) * sizeof(int));

/* Main loop / for (n = 0; n < steps; n++) { for (x = 0; x < nlocal; x++) { nextbuffer[x] = (buffer[(x-1+N)%N]+buffer[(x+1)%N]) % 10; } / Exchange boundary cells with neighbors */ ... tmp = buffer; buffer = nextbuffer; nextbuffer = tmp; }

What are the defects?

N may not be divisible by size

(x = 1; x < nlocal+1; x++)

  • Loop boundary and array indexes must be changed to reflect the

effect of space decomposition (a sequential implementation

should have been written to make parallelization easier)

  • Lesson : make sure the parallel code works correctly on 1 proc

x-1 x+

Pattern: Hidden Serialization

  • Side-effect of parallelization: ordinary serial constructs can

cause defects when they are used in parallel contexts

  • E.g. I/O hotspots
  • E.g. Hidden serialization in library functions Symptoms:
  • Various correctness/performance problems Causes:
  • “Sequential part” tends to be overlooked
  • Typical parallel programs contain only a few parallel

primitives, and the rest of the code is a sequential

program running in parallel

Cures & preventions:

  • Don’t just focus on the parallel code
  • Check that the serial code is working on one processor, but

remember that the defect may surface only in a parallel

context

CMSC714 19

/* What if we initialize cells with random values... */ srand(time(NULL)); for (x = 0; x < nlocal; x++) { buffer[x+1] = rand() % 10; }

/* Main loop */ ...

Generating Initial Data

  • What are the defects?
  • (Other than the fact that rand() is not a good pseudo- random number generator in the first place…)

/* What if we initialize cells with random values... */ srand(time(NULL)); for (x = 0; x < nlocal; x++) { buffer[x+1] = rand() % 10; }

/* Main loop */ ...

What are the Defects?

  • Lesson : all processors might use the same pseudo- random sequence, breaking independence assumption (correctness)
  • Lesson : Hidden serialization in the library function rand() causes performance bottleneck

srand(time(NULL) + rank);

CMSC714 21

Pattern: Synchronization

  • Improper coordination between processes
    • Well-known defect type in parallel programming
    • Some defects can be very subtle Symptoms:
  • Deadlocks: some execution path can lead to cyclic

dependencies among processors and nothing ever happens

  • Race conditions: incorrect/non-deterministic output and there

can be performance defects due to synchronization too

Causes:

  • Use of asynchronous (non-blocking) communication can lead to

more synchronization defects

  • (Too much synchronization can be a performance problem) Cures & preventions:
  • Make sure that all communications are correctly coordinated
    • Check the communication pattern with specific number of

processes/threads using charts

/* Main loop / for (n = 0; n < steps; n++) { for (x = 1; x < nlocal+1; x++) { nextbuffer[x] = (buffer[(x-1+N)%N]+buffer[(x+1)%N]) % 10; } / Exchange boundary cells with neighbors */ MPI_Recv (&nextbuffer[0], 1, MPI_INT, (rank+size-1)%size, tag, MPI_COMM_WORLD, &status); MPI_Send (&nextbuffer[nlocal],1,MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[nlocal+1],1,MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &status); MPI_Send (&nextbuffer[1], 1, MPI_INT, (rank+size-1)%size, tag, MPI_COMM_WORLD); tmp = buffer; buffer = nextbuffer; nextbuffer = tmp; }

Communication

  • What are the defects?

CMSC714 25

/* Main loop / for (n = 0; n < steps; n++) { for (x = 1; x < nlocal+1; x++) { nextbuffer[x] = (buffer[(x-1+N)%N]+buffer[(x+1)%N]) % 10; } / Exchange boundary cells with neighbors */ MPI_Ssend (&nextbuffer[nlocal],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[0], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &status); MPI_Ssend (&nextbuffer[1], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[nlocal+1],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD, &status); tmp = buffer; buffer = nextbuffer; nextbuffer = tmp; }

What are the Defects?

  • This causes deadlock too
  • MPI_Ssend is a synchronous send (see the next slides.)

/* Main loop / for (n = 0; n < steps; n++) { for (x = 1; x < nlocal+1; x++) { nextbuffer[x] = (buffer[(x-1+N)%N]+buffer[(x+1)%N]) % 10; } / Exchange boundary cells with neighbors */ MPI_Send (&nextbuffer[nlocal],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[0], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &status); MPI_Send (&nextbuffer[1], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[nlocal+1],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD, &status); tmp = buffer; buffer = nextbuffer; nextbuffer = tmp; }

Yet Another Example

  • What are the defects?

CMSC714 27

/* Main loop / for (n = 0; n < steps; n++) { for (x = 1; x < nlocal+1; x++) { nextbuffer[x] = (buffer[(x-1+N)%N]+buffer[(x+1)%N]) % 10; } / Exchange boundary cells with neighbors */ MPI_Send (&nextbuffer[nlocal],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[0], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &status); MPI_Send (&nextbuffer[1], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[nlocal+1],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD, &status); tmp = buffer; buffer = nextbuffer; nextbuffer = tmp; }

Potential Deadlock

  • This may work (many novice programmers write this code)
  • but it can cause deadlock with some MPI implementation,

runtime environment and/or execution parameters

Modes of MPI blocking communication

  • http://www.mpi-forum.org/docs/mpi-11-html/node40.html
    • Standard (MPI_Send): may either return immediately when the outgoing message is buffered in the MPI buffers, or block until a matching receive has been posted.
    • Buffered (MPI_Bsend): a send operation is completed when the MPI buffers the outgoing message. An error is returned when there is insufficient buffer space
    • Synchronous (MPI_Ssend): a send operation is complete only when the matching receive operation has started to receive the message.
    • Ready (MPI_Rsend): a send can be started only after the matching receive has been posted.
  • In our code MPI_Send probably won’t block in most implementations

(each message is just one integer), but it should still be avoided for

correctness

  • A “correct” solution for this defect could be:
    • (1) alternate the order of send and recv
    • (2) use MPI_Bsend with sufficient buffer size
    • (3) use MPI_Sendrecv, or
    • (4) use MPI_Isend and MPI_Irecv

CMSC714 31

/* Main loop / for (n = 0; n < steps; n++) { for (x = 1; x < nlocal+1; x++) { nextbuffer[x] = (buffer[(x-1+N)%N]+buffer[(x+1)%N]) % 10; } / Exchange boundary cells with neighbors */ MPI_Isend (&nextbuffer[nlocal],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD, &request1); MPI_Irecv (&nextbuffer[0], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &request2); MPI_Isend (&nextbuffer[1], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &request3); MPI_Irecv (&nextbuffer[nlocal+1],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD, &request4); tmp = buffer; buffer = nextbuffer; nextbuffer = tmp; }

What are the Defects?

  • Synchronization (e.g. MPI_Wait, MPI_Barrier) is needed at each

iteration (but too many barriers can cause a performance

problem)

Pattern: Performance defect

  • Scalability problem because processors are not working in

parallel

  • The program output itself is correct
  • Perfect parallelization is often difficult: need to decide if

the execution speed is not acceptable

Symptoms:

  • Sub-linear scalability
  • Performance much less than expected (e.g, most time spent

waiting),

Causes:

  • Unbalanced amount of computation per processor
  • Load balancing may depend on input data

Cures & preventions:

  • Make sure all processors are “working” in parallel
  • Profiling tool might help

CMSC714 33

if (rank != 0) { MPI_Ssend (&nextbuffer[nlocal],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[0], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &status); } if (rank != size-1) { MPI_Recv (&nextbuffer[nlocal+1],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD, &status); MPI_Ssend (&nextbuffer[1], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD); }

Scheduling communication

  • Complicated communication pattern- does not cause deadlock

What are the defects?

if (rank != 0) { MPI_Ssend (&nextbuffer[nlocal],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD); MPI_Recv (&nextbuffer[0], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD, &status); } if (rank != size-1) { MPI_Recv (&nextbuffer[nlocal+1],1,MPI_INT,(rank+size-1)%size, tag, MPI_COMM_WORLD, &status); MPI_Ssend (&nextbuffer[1], 1, MPI_INT, (rank+1)%size, tag, MPI_COMM_WORLD); }

What are the bugs?

1 Send → 0 Recv → 0 Send → 1 Recv 2 Send → 1 Recv → 1 Send → 2 Recv 3 Send → 2 Recv → 2 Send → 3 Recv

0 (nlocal+1)

  • Serialization in communication : requires O(size) time (a “correct” solution

takes O(1))