Skip to content

Commit

Permalink
Faster simulaiton
Browse files Browse the repository at this point in the history
  • Loading branch information
EdvinGermer committed May 15, 2023
1 parent 87397ae commit 7f532e2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 22 deletions.
Binary file modified Project/histogram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Project/histogram.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
458, 479, 500, 521, 542, 563, 584, 605, 626, 647, 668, 689, 710, 731, 752, 773, 794, 815, 836, 857, 895
15, 110, 307, 700, 1181, 1540, 1518, 1308, 1044, 806, 503, 343, 237, 171, 96, 57, 27, 21, 9, 4
416, 447, 478, 509, 540, 571, 602, 633, 664, 695, 726, 757, 788, 819, 850, 881, 912, 943, 974, 1005, 1036
37, 1446, 18420, 84897, 182233, 226089, 193834, 133062, 78835, 43027, 21758, 9801, 4113, 1610, 583, 189, 40, 20, 4, 1
Binary file modified Project/montecarlo
Binary file not shown.
30 changes: 11 additions & 19 deletions Project/montecarlo.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ int main(int argc, char *argv[])
return -1;
}


/* HOW MANY EXPERIMENTS PER PROCESS */
N = atoi(argv[1]); // Total number of experiments
n = N/size; // Experiments per process


/* DEFINE MATRIX P */
int P[15][7] = {
{1, 0, 0, 0, 0, 0, 0},
Expand All @@ -73,20 +75,24 @@ int main(int argc, char *argv[])
{0, 0, 0, 0, 0, 0, -1}
};


/* START LOCAL TIME */
double start = MPI_Wtime();


/* MONTE CARLO SIMULATION */
int **X = calloc(7, sizeof(int*));
int **X = calloc(7, sizeof(int*)); // Big result matrix
for (int i=0; i<7; i++)
X[i] = calloc(n, sizeof(int));


srand(time(NULL)+rank); // Random seed for each rank

int x[7]; // Initial state vector

srand(time(NULL)+rank);// Random seed for each rank

for (int i=0;i<n;i++)
{
int x[7] = {900,900,30,330,50,270,20}; // Initial state vector
// Initialize
x[0]=900; x[1]=900; x[2]=30; x[3]=330; x[4]=50; x[5]=270; x[6]=20;

// Do experiment (simulation)
t=0;
Expand Down Expand Up @@ -132,13 +138,6 @@ int main(int argc, char *argv[])
X[row][i] = x[row]; // save x as a column in X
}

/* SANITY CHECK */
/* printf("Rank %d: \n", rank);
for (int i=0;i<n;i++)
printf("%d, ", X[0][i]);
printf("\n"); */


/* EXTRACT SUSCEPTIBLE HUMANS */
int* local_res = (int*)malloc(n * sizeof(int));

Expand All @@ -165,12 +164,6 @@ int main(int argc, char *argv[])

MPI_Allreduce(&local_max, &global_max, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
MPI_Allreduce(&local_min, &global_min, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);


/* SANITY CHECK */
//printf("RANK %d: local_max = %d, local_min = %d\n",rank, local_max, local_min);
//if (rank==0)
// printf("RANK %d: global_max = %d, global_min = %d\n",rank, global_max, global_min);


/* LOCAL HISTOGRAM */
Expand All @@ -180,7 +173,6 @@ int main(int argc, char *argv[])
for (int i=0;i<b;i++)
bins[i] = global_min + i*interval;
bins[b] = global_max;


int freqs[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // count each bin
int bin;
Expand Down
2 changes: 1 addition & 1 deletion Project/plot_histogram.py → Project/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
plt.bar(mid_points, counts, width=np.diff(boundaries), align='center', edgecolor='black')

# Add labels and title
plt.xlabel('Boundaries')
plt.xlabel('Susceptible humans')
plt.ylabel('Counts')
plt.title('MC Simulation of Malara Epidemic')

Expand Down

0 comments on commit 7f532e2

Please sign in to comment.