Program11. Parallel program to add 1-dimensional arrays a and b and store in c.
#include "stdafx.h"
#include "omp.h"
#include "stdio.h"
#include "conio.h"
int _tmain(int argc, _TCHAR* argv[])
{
int tid,chunk,n,i;
float a[1000],b[1000],c[1000];
printf("Enter the size of array\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=1.0;
b[i]=2.0;
}
chunk = 10;
#pragma omp parallel
{
tid = omp_get_thread_num();
#pragma omp for schedule(dynamic,chunk)
for(i=0;i<n;i++)
{
c[i]=a[i]+b[i];
printf("Thread %d:c[%d] = %f\n",tid,i,c[i]);
}
}
getch();
return 0;
}
Program 14: Parallel program to print prime numbers within n using “seive of Eratosthenes
method”.
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
#include "omp.h"
#include "math.h"
int _tmain(int argc, _TCHAR* argv[])
{
int a[10000];
float n;
int i,k;
printf("\nEnter the value of n:\n");
scanf_s("%f",&n);
for(i=2;i<=n;i++)a[i]=1;
#pragma omp parallel
{
omp_get_thread_num();
for(k=2;k<=sqrt(n);k++)
{
if(a[k]!=0)
{
#pragma omp for
//eliminate multiples of k
for(i=k*k;i<=n;i+=k)a[i]=0;
}
}
}
printf("prime number are\n");
for(i=2;i<=n;i++)
{
if(a[i])
printf("%d\n",i);
}
_getch();
return 0;
}