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

21cs125 Distributed Computing Assignment 2

This document discusses two simple parallel programs using OpenMP: 1) A "Hello World" program that prints the message from multiple threads. 2) A program that uses random numbers to approximate pi in parallel by counting points within a circle. The pi program uses OpenMP to parallelize a for loop that calculates distances and increments a count variable shared between threads. Both programs demonstrate basic parallelization of loops using OpenMP.

Uploaded by

21CS185 VISHAL.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

21cs125 Distributed Computing Assignment 2

This document discusses two simple parallel programs using OpenMP: 1) A "Hello World" program that prints the message from multiple threads. 2) A program that uses random numbers to approximate pi in parallel by counting points within a circle. The pi program uses OpenMP to parallelize a for loop that calculates distances and increments a count variable shared between threads. Both programs demonstrate basic parallelization of loops using OpenMP.

Uploaded by

21CS185 VISHAL.M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DISTRIBUTED COMPUTING

ASSIGNMENT 1

SUBMITTED BY

PRANAV.S

21CS125
1. Create a Simple parallel Loop program "HELLO WORLD"

Code:

#include <stdio.h>
#include <omp.h>

int main() {
// Set the number of threads to use (optional)
omp_set_num_threads(4); // You can adjust the number of threads as
needed

// Parallel loop using OpenMP


#pragma omp parallel
{
// Get the thread number
int thread_id = omp_get_thread_num();

// Print "Hello, World!" from each thread


printf("Hello, World! from thread %d\n", thread_id);
}

return 0;
}

Output:
2. Create a Simple parallel Loop program "Pi"

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

#define NUM_POINTS 1000000

int main() {
int i, count = 0;

// Set the number of threads to use (optional)


omp_set_num_threads(4); // You can adjust the number of threads as needed

// Seed the random number generator


srand(123);

// Parallel loop using OpenMP to approximate pi

for (i = 0; i < NUM_POINTS; ++i) {


double x = (double)rand() / RAND_MAX;
double y = (double)rand() / RAND_MAX;

double distance = x * x + y * y;

if (distance <= 1) {
count++;
}
}

// Calculate the approximation of pi


double pi_approximation = 4.0 * count / NUM_POINTS;

// Print the result


printf("Approximation of pi: %f\n", pi_approximation);

return 0;
}

Output:
---

You might also like