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

SWE2017 - Lab Assignment 1pages-7

program mpi code

Uploaded by

ccannavar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SWE2017 - Lab Assignment 1pages-7

program mpi code

Uploaded by

ccannavar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SWE2017 - Parallel Programming Lab Assignment – 6

Lab Assignment- 6
1.
Code:
#include <stdio.h>
#include <omp.h>

int main(int argc, char *argv[])


{
int p = 100000;
int sum = 0;

double start_time = omp_get_wtime();

#pragma omp parallel for reduction(+ : sum)


for (int i = 0; i < p; i++)
{
sum += i + 1;
}

double end_time = omp_get_wtime();

if (omp_get_thread_num() == 0)
{
printf("Sum using reduction: %d\n", sum);
double formula_start_time = omp_get_wtime();
int formula_sum = p * (p + 1) / 2;
double formula_end_time = omp_get_wtime();

printf("Sum using formula: %d\n", formula_sum);


printf("Time taken using formula: %f seconds\n", formula_end_time -
formula_start_time);
printf("Time taken: %f seconds\n", end_time - start_time);
}

return 0;
}

Output:
SWE2017 - Parallel Programming Lab Assignment – 6

2.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define LIMIT 1000000

int is_prime(int n)
{
if (n <= 1)
return 0;
if (n <= 3)
return 1;
if (n % 2 == 0 || n % 3 == 0)
return 0;
for (int i = 5; i * i <= n; i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return 0;
}
return 1;
}

int main()
{
int count = 0;

double start_time = omp_get_wtime();

#pragma omp parallel for reduction(+ : count)


for (int i = 3; i < LIMIT; i += 2)
{
if (is_prime(i) && is_prime(i + 2))
{
count++;
}
}

double end_time = omp_get_wtime();


printf("Execution time: %f seconds\n", end_time - start_time);

printf("Number of times two consecutive odd integers are both prime:


%d\n", count);
return 0;
}
SWE2017 - Parallel Programming Lab Assignment – 6

Output:

3.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <omp.h>

#define LIMIT 1000000

void generate_primes(bool *is_prime)


{
for (int i = 2; i < LIMIT; i++)
{
is_prime[i] = true;
}
for (int p = 2; p * p < LIMIT; p++)
{
if (is_prime[p])
{
for (int i = p * p; i < LIMIT; i += p)
{
is_prime[i] = false;
}
}
}
}

int main()
{
bool *is_prime = (bool *)malloc(LIMIT * sizeof(bool));
int max_gap = 0;

generate_primes(is_prime);

int *primes = (int *)malloc(LIMIT * sizeof(int));


int num_primes = 0;
for (int i = 2; i < LIMIT; i++)
{
if (is_prime[i])
{
SWE2017 - Parallel Programming Lab Assignment – 6

primes[num_primes++] = i;
}
}

#pragma omp parallel for reduction(max : max_gap)


for (int i = 1; i < num_primes; i++)
{
int gap = primes[i] - primes[i - 1];
if (gap > max_gap)
{
max_gap = gap;
}
}

printf("Largest gap between consecutive primes under %d is: %d\n", LIMIT,


max_gap);

free(is_prime);
free(primes);

return 0;
}

Output:

4.
Code:
#include <stdio.h>
#include <omp.h>

int main() {
int total_population = 500;
int growth_rates[4] = {10, 15, 20, 5};

#pragma omp parallel num_threads(4)


{
int thread_id = omp_get_thread_num();
int new_bacteria = growth_rates[thread_id];

#pragma omp critical


{
total_population += new_bacteria;
SWE2017 - Parallel Programming Lab Assignment – 6

printf("Thread %d added %d bacteria. New total population: %d\n",


thread_id, new_bacteria, total_population);
}
}

printf("Final total population after one iteration: %d\n",


total_population);
return 0;
}

Output:

If we are starting with a total population of 500, and assuming the threads are updating the
population in the order of their ranks:

• Thread 0 (Colony A) adds 10, making the total population 510.


• Thread 1 (Colony B) adds 15, updating the total population to 525.
• Thread 2 (Colony C) adds 20, bringing the total population to 545.
• Thread 3 (Colony D) adds 5, giving a final total population of 550 after one iteration.

Thus, the expected final population count after all colonies contribute in this iteration is
550.

Importance of #pragma omp critical

The #pragma omp critical directive is essential here because it ensures that only one
thread at a time updates the total_population variable. Without it, multiple threads
might attempt to read, modify, and write to total_population simultaneously, leading to
data races. For example, if two threads read the same initial population value before
updating it, both might add their increments to the old value, resulting in an incorrect final
population count.

Performance Consideration

While #pragma omp critical prevents data races, it can also introduce thread contention
because only one thread can enter the critical section at any time. This serialization can
reduce performance, particularly if the critical section is in a loop or if there are many
threads. In cases where performance is critical and the shared update is frequent,

You might also like