Enforcing Memory Consistency
Memory consistency is when the values held in registers by a thread match those held in memory. If another thread modifies a variable held in a register by this thread, the value has become inconsistent and needs to be refetched from memory. OpenMP directives already enforce appropriate memory consistency, so it is rare for it to be a concern for codes parallelized using OpenMP. However, there could be situations where it is neces-sary to manually enforce consistency.
OpenMP allows the developer to explicitly specify the places in the code where vari-ables need to be saved to memory or loaded from memory using the flush directive. Unless the directive specifies a list of variables, it applies to all the thread visible state. If a list of variables is specified, these variables will either be stored to memory or be reloaded from memory depending on which action is necessary.
The example shown in Listing 7.62 uses the flush directive to produce a synchro-nization barrier between a pair of threads.
Listing 7.62 Using the flush Directive to Produce a Barrier
int *ready = calloc( sizeof(int), 2 );
#pragma omp parallel num_threads( 2 )
printf( "Thread %i is ready\n", omp_get_thread_num() ); ready[ omp_get_thread_num() ] = 1;
int neighbour = ( omp_get_thread_num()+1 ) % omp_get_num_threads();
while( ready[neighbour] == 0 )
#pragma omp flush
printf( "Thread %i is done\n", omp_get_thread_num() );
free( ready );
The master thread allocates an array with a single element per thread. In the parallel region, each thread sets their index in the array to be one and then waits for their neigh-boring thread to set their index to be nonzero. Each thread is released from the barrier when its neighboring thread arrives. The flush directive is used to ensure that the cur-rent thread stores its value into the array and that the current thread constantly reloads its neighbor’s value until the neighboring thread sets it to one.
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.