|
|
OpenMP program to find the minimum in an array:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
int n, start, stop;
double my_min;
n = MAX/nThreads; // Step size
start = id * n;
stop = ( (start + n < MAX) ? (start + n) : MAX );
my_min = x[start];
for ( int i = start+1; i < stop; i++ )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Define the parallel region (code will be executed by num_threads threads):
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int id = omp_get_thread_num();
int n, start, stop;
double my_min;
n = MAX/num_threads; // Step size
start = id * n;
stop = ( (start + n < MAX) ? (start + n) : MAX );
my_min = x[start];
for ( int i = start+1; i < stop; i++ )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Find the thread ID and the number of threads:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
int n, start, stop;
double my_min;
n = MAX/nThreads; // Step size
start = id * n;
stop = ( (start + n < MAX) ? (start + n) : MAX );
my_min = x[start];
for ( int i = start+1; i < stop; i++ )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Find the segment size for the thread (work distribution: thread 0: [0..n), thread 1: [n..2n), etc):
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
int n, start, stop;
double my_min;
n = MAX/nThreads; // Segment size
start = id * n;
stop = ( (start + n < MAX) ? (start + n) : MAX );
my_min = x[start];
for ( int i = start+1; i < stop; i++ )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Find the segment range for the thread (work distribution: thread 0: [0..n), thread 1: [n..2n), etc):
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
int n, start, stop;
double my_min;
n = MAX/nThreads; // Segment size
start = id * n; // Start index for thread "id"
stop = ( (start + n < MAX) ? (start + n) : MAX );
my_min = x[start];
for ( int i = start+1; i < stop; i++ )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Find the minimum value per thread:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
int n, start, stop;
double my_min;
n = MAX/nThreads; // Segment size
start = id * n; // Start index for thread "id"
stop = ( (start + n < MAX) ? (start + n) : MAX );
my_min = x[start];
for ( int i = start+1; i < stop; i++ )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Store the minimum value in Tmin[id]:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
int n, start, stop;
double my_min;
n = MAX/nThreads; // Segment size
start = id * n; // Start index for thread "id"
stop = ( (start + n < MAX) ? (start + n) : MAX );
my_min = x[start];
for ( int i = start+1; i < stop; i++ )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
DEMO: demo/OpenMP/openMP-min.c
|
OpenMP program to find the minimum in an array using the alternate work load distribution:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int id = omp_get_thread_num();
double my_min;
my_min = x[id];
for ( int i = id+num_threads; i < MAX; i += num_threads )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Define the parallel region (code will be executed by num_threads threads):
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int id = omp_get_thread_num();
double my_min;
my_min = x[id];
for ( int i = id+num_threads; i < MAX; i += num_threads )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Find the thread ID and the number of threads:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
double my_min;
my_min = x[id];
for ( int i = id+num_threads; i < MAX; i += num_threads )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Find the minimum value over the values x[id], x[id+num_threads], x[id+2*num_threads], ...:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
double my_min;
my_min = x[id];
for ( int i = id+nThreads; i < MAX; i += nThreads )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
Store the minimum value in Tmin[id]:
double x[MAX]; // Input data
double Tmin[100]; // The min found by each thread
int main(int argc, char *argv[])
{
omp_set_num_threads( num_threads ); // Set thread group size
#pragma omp parallel
{
int nThreads = omp_get_num_threads();
int id = omp_get_thread_num();
double my_min;
my_min = x[id];
for ( int i = id+nThreads; i < MAX; i += nThreads )
if ( x[i] < my_min )
my_min = x[i];
Tmin[id] = my_min;
}
/* Post-processing: find min in Tmin[ ] (omitted for brevity) */
}
|
DEMO: demo/OpenMP/openMP-min2.c