0% found this document useful (0 votes)
21 views3 pages

Monte Carlo Lab

Uploaded by

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

Monte Carlo Lab

Uploaded by

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

High Performance Computing

MONTE-CARLO LAB
Name: Raja Vignesh Selvamani
Student Id: 202103607

Step 1: Write a serial (sequential) C/C++ program that


simulates a random walk of 100,000 particles in 10 steps,
and outputs the distribution of the distances that particles
have travelled.
Code:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>

int main() {
const int n = 100000;
const int nsteps = 10;
const double pi = 3.1415926;
int itotal[nsteps + 1] = {0};
srand(time(NULL));
for (int i = 0; i < n; ++i) {
double x = 0.0;
double y = 0.0;
for (int istep = 0; istep < nsteps; ++istep) {
double angle = 2.0 * pi * rand() / RAND_MAX;
x += cos(angle);
y += sin(angle);
}
int itemp = static_cast<int>(sqrt(x * x + y * y));
if (itemp <= nsteps) {
itotal[itemp]++;
}
}
for (int i = 0; i <= nsteps; ++i) {
std::cout << "Distance " << i << ": " << itotal[i] << "
particles" << std::endl;
}
return 0;
}
Output:

Step 2: Building on step 1, write an equivalent parallel


code.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <mpi.h>

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


const int n = 100000;
const int nsteps = 10;
const double pi = 3.1415926;
int itotal[nsteps + 1] = {0};
double x, y, angle;
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
srand(time(NULL) + rank);
int local_n = n / size;
int local_start = rank * local_n;
int local_end = local_start + local_n;
for (int i = local_start; i < local_end; ++i) {
x = 0.0;
y = 0.0;
for (int istep = 0; istep < nsteps; ++istep) {
angle = 2.0 * pi * ((double) rand() / RAND_MAX);
x += cos(angle);
y += sin(angle);
}
int itemp = (int) sqrt(x * x + y * y);
if (itemp <= nsteps) {
itotal[itemp]++;
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) {
int recv_counts[size];
MPI_Gather(&local_n, 1, MPI_INT, recv_counts, 1, MPI_INT,
0, MPI_COMM_WORLD);
int recv_displs[size];
recv_displs[0] = 0;
for (int i = 1; i < size; ++i) {
recv_displs[i] = recv_displs[i - 1] + recv_counts[i -
1];
}
int recv_data[nsteps + 1];
MPI_Gatherv(itotal, local_n, MPI_INT, recv_data,
recv_counts, recv_displs, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0) {
for (int i = 0; i <= nsteps; ++i) {
int total = 0;
for (int j = 0; j < size; ++j) {
total += recv_data[i + recv_displs[j]];
}
printf("Distance %d: %d particles\n", i, total);
}
}
}
MPI_Finalize();
return 0;
}

Output:

You might also like