Online Book Reader

Home Category

High Performance Computing - Charles Severance [87]

By Root 1293 0
{ /* Time Step Loop */

/* Setup tasks */

for (ith=0;ithfor (ith=0;ith}

work_routine() {

/* Perform Task */

return;

}

The shortcoming of this approach is the overhead cost associated with creating and destroying an operating system thread for a potentially very short task.

The other approach is to have the threads created at the beginning of the program and to have them communicate amongst themselves throughout the duration of the application. To do this, they use such techniques as critical sections or barriers.


Synchronization

Synchronization is needed when there is a particular operation to a shared variable that can only be performed by one processor at a time. For example, in previous SpinFunc( ) examples, consider the line:

globvar++;

In assembly language, this takes at least three instructions:

LOAD R1,globvar

ADD R1,1

STORE R1,globvar

What if globvar contained 0, Thread 1 was running, and, at the precise moment it completed the LOAD into Register R1 and before it had completed the ADD or STORE instructions, the operating system interrupted the thread and switched to Thread 2? Thread 2 catches up and executes all three instructions using its registers: loading 0, adding 1 and storing the 1 back into globvar. Now Thread 2 goes to sleep and Thread 1 is restarted at the ADD instruction. Register R1 for Thread 1 contains the previously loaded value of 0; Thread 1 adds 1 and then stores 1 into globvar. What is wrong with this picture? We meant to use this code to count the number of threads that have passed this point. Two threads passed the point, but because of a bad case of bad timing, our variable indicates only that one thread passed. This is because the increment of a variable in memory is not atomic. That is, halfway through the increment, something else can happen.

Another way we can have a problem is on a multiprocessor when two processors execute these instructions simultaneously. They both do the LOAD, getting 0. Then they both add 1 and store 1 back to memory.[58] Which processor actually got the honor of storing their 1 back to memory is simply a race.

We must have some way of guaranteeing that only one thread can be in these three instructions at the same time. If one thread has started these instructions, all other threads must wait to enter until the first thread has exited. These areas are called critical sections. On single-CPU systems, there was a simple solution to critical sections: you could turn off interrupts for a few instructions and then turn them back on. This way you could guarantee that you would get all the way through before a timer or other interrupt occurred:

INTOFF // Turn off Interrupts

LOAD R1,globvar

ADD R1,1

STORE R1,globvar

INTON // Turn on Interrupts

However, this technique does not work for longer critical sections or when there is more than one CPU. In these cases, you need a lock, a semaphore, or a mutex. Most thread libraries provide this type of routine. To use a mutex, we have to make some modifications to our example code:

...

pthread_mutex_t my_mutex; /* MUTEX data structure */

...

main() {

...

pthread_attr_init(&attr); /* Initialize attr with defaults */

pthread_mutex_init (&my_mutex, NULL);

.... pthread_create( ... )

...

}

void *SpinFunc(void *parm) {

...

pthread_mutex_lock (&my_mutex);

globvar ++;

pthread_mutex_unlock (&my_mutex);

while(globvar < THREAD_COUNT ) ;

printf("SpinFunc me=%d – done globvar=%d...\n", me, globvar);

...

}

The mutex data structure must be declared in the shared area of the program. Before the threads are created, pthread_mutex_init must be called to initialize the mutex. Before globvar is incremented, we must lock the mutex and after we finish updating globvar (three instructions later), we unlock the mutex. With the code as shown above, there will never be more than one processor executing the globvar++ line of code, and the code will never hang because an increment was missed. Semaphores

Return Main Page Previous Page Next Page

®Online Book Reader