You are on page 1of 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