0% found this document useful (0 votes)
27 views

Parallel Lab Program

The document describes two parallel programs: 1) A program that uses OpenMP to parallelize adding elements of 1D arrays a and b and storing the results in c across multiple threads. 2) A program that uses OpenMP to parallelize calculating prime numbers within a given value n using the Sieve of Eratosthenes algorithm across threads.

Uploaded by

Chetan Raju
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Parallel Lab Program

The document describes two parallel programs: 1) A program that uses OpenMP to parallelize adding elements of 1D arrays a and b and storing the results in c across multiple threads. 2) A program that uses OpenMP to parallelize calculating prime numbers within a given value n using the Sieve of Eratosthenes algorithm across threads.

Uploaded by

Chetan Raju
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

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;
}

You might also like