Assigment 3
SKR 3308 Sem II 2020/2021
Objectives
i. To practice students with OpenMP library.
1. Compile and execute all programs successfully.
2. Include 2 more section for Program 2 and set number of threads 2 for section 2 and 3 for section 3.
3. Set the Environment Variable export OMP_PROC_BIND=true and false. Then add library routine omp_get_wtime() to measure the time for Program 1 and 2 for 2 until 16 processors using power of 2 increment.
4. Explain output and function of each program.
Submission
1. Screen shot the output and code with your home dir included in the screen shots.
2. Write your explanation in the report
/******************************************************************************
* FILE: omp_workshare1.c
* DESCRIPTION:
* OpenMP Example – Loop Work-sharing – C/C++ Version
* In this example, the iterations of a loop are scheduled dynamically
* across the team of threads. A thread will perform CHUNK iterations
* at a time before being scheduled for the next CHUNK of work.
* AUTHOR: Blaise Barney 5/99
* LAST REVISED: 04/06/05
******************************************************************************/
#include
#include
#include
#define CHUNKSIZE 10
#define N 100
int main (int argc, char *argv[])
int nthreads, tid, i, chunk;
float a[N], b[N], c[N];
/* Some initializations */
for (i=0; i < N; i++)
a[i] = b[i] = i * 1.0;
chunk = CHUNKSIZE;
#pragma omp parallel shared(a,b,c,nthreads,chunk) private(i,tid)
tid = omp_get_thread_num();
if (tid == 0)
nthreads = omp_get_num_threads();
printf("Number of threads = %d\n", nthreads);
printf("Thread %d starting...\n",tid);
#pragma omp for schedule(static,chunk)
for (i=0; i
#include
#include
#define N 50
int main (int argc, char *argv[])
int i, nthreads, tid;
float a[N], b[N], c[N], d[N];
/* Some initializations */
for (i=0; i
#include
#include
int main (int argc, char *argv[])
int i, n;
float a[100], b[100], sum;
/* Some initializations */
for (i=0; i < n; i++)
a[i] = b[i] = i * 1.0;
sum = 0.0;
#pragma omp parallel for reduction(+:sum)
for (i=0; i < n; i++)
sum = sum + (a[i] * b[i]);
printf(" Sum = %f\n",sum);
/******************************************************************************
* FILE: omp_orphan.c
* DESCRIPTION:
* OpenMP Example - Parallel region with an orphaned directive - C/C++ Version
* This example demonstrates a dot product being performed by an orphaned
* loop reduction construct. Scoping of the reduction variable is critical.
* AUTHOR: Blaise Barney 5/99
* LAST REVISED: 06/30/05
******************************************************************************/
#include
#include
#include
#define VECLEN 100
float a[VECLEN], b[VECLEN], sum;
float dotprod ()
int i,tid;
tid = omp_get_thread_num();
#pragma omp for reduction(+:sum)
for (i=0; i < VECLEN; i++)
sum = sum + (a[i]*b[i]);
printf(" tid= %d i=%d\n",tid,i);
int main (int argc, char *argv[]) {
for (i=0; i < VECLEN; i++)
a[i] = b[i] = 1.0 * i;
sum = 0.0;
#pragma omp parallel
dotprod();
printf("Sum = %f\n",sum);