Program #1:
#include<stdio.h>
#include<omp.h>
/* Main Program */
main()
{
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t CDAC Summer School ");
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to print Hello World \n ");
printf("\n\t\t..........................................................................\n");
#pragma omp parallel
{
/*Printing by showing the thread that is showing the data*/
printf("\n\t\t Hello World by %d out of %d\n",omp_get_thread_num(),omp_get_num_threads());
}
/* All thread join Master thread */
}
Program #2:
#include<stdio.h>
#include<omp.h>
//Main Program
main()
{
int i;
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t CDAC Summer School ");
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to perform parllel computing on for loop\n ");
printf("\n\t\t..........................................................................\n");
#pragma omp parallel for
for(i=0;i<1000;i++)
{
//Prints the number & shows the thread in which it is present.
printf("%d given by %d\n",i,omp_get_thread_num());
}
}
Program #3:
#include<stdio.h>
#include<omp.h>
main()
{
int nthreads,tid,procs,maxt,inpar,dynamic,nested;
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t CDAC Summer School ");
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to show information about the device\n ");
printf("\n\t\t..........................................................................\n");
#pragma omp parallel private(nthreads,tid)
{
tid=omp_get_thread_num();//get total number of threads.
if(tid==0)
{
printf("Thread %d getting environment info...n",tid);
procs= omp_get_num_procs();//gets no of processor's.
nthreads= omp_get_num_threads();//gets no of threads.
maxt= omp_get_max_threads();//gets max num of threads present.
inpar= omp_in_parallel();//no of threads working paralle.
dynamic= omp_get_dynamic();//Checks dynamic or not.
nested= omp_get_nested();//checks the nested increment support.
//Prints the details about the environment of the hardware.
printf("No of processors=%d\n",procs);
printf("No of threads=%d\n",nthreads);
printf("Max threads=%d\n",maxt);
printf("In parallel= %d",inpar);
printf("Dynamic threads enabled?=%d\n",dynamic);
printf("Nested support?= %d\n",nested);
}
}//combines with master thread.
}
Program #4:
#include<stdio.h>
#include<omp.h>
main(){
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t CDAC Summer School ");
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to show maximum number of threads\n ");
printf("\n\t\t..........................................................................\n");
printf("Total no of threads: %d",omp_get_max_threads());
}
Program #5:
#include<stdio.h>
#include<omp.h>
main()
{
int i,n;
float a[100],b[100],sum;
n=100;
for(i=0;i<n;i++)
{
a[i]=i*1.0;
b[i]=i*1.0;
}
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t CDAC Summer School ");
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to show maximum number of threads\n ");
printf("\n\t\t..........................................................................\n");
sum=0.0;
//Parallel programming.
#pragma omp parallel for reduction (+:sum)
for(i=0;i<n;i++)
{
sum =sum+( a[i]*b[i]);
}
printf("%f is the sum\n",sum);
//Sequential programming.
for(i=0;i<n;i++)
{
sum =sum+( a[i]*b[i]);
}
printf("%f is the sum without reduction\n",sum);
}
Program #6:
#include<stdio.h>
#include<omp.h>
main()
{
int i;
double start, end;
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t CDAC Summer School ");
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to show maximum number of threads\n ");
printf("\n\t\t..........................................................................\n");
start= omp_get_wtime();
//Parallel programming.
#pragma omp parallel for
for(i=0;i<1000;i++)
{
printf("%d given by %d\n",i,omp_get_thread_num());
}
end= omp_get_wtime();
printf("Run time of the program is : %f",end-start);
//Sequential programming.
for(i=0;i<1000;i++)
{
printf("%d given by %d\n",i,omp_get_thread_num());
}
end= omp_get_wtime();
printf("Run time of the program is : %f",end-start);
}
Program #7:
#include<stdio.h>
#include<omp.h>
main()
{
int i=256;
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t CDAC Summer School ");
printf("\n\t\t---------------------------------------------------------------------------");
printf("\n\t\t Objective : OpenMP program to show information about the device \n ");
printf("\n\t\t..........................................................................\n");
#pragma omp parallel
{
int x;
x=omp_get_thread_num();
printf("x=%d, i=%d\n",x,i);
}
}