-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_densitycomp.cpp
More file actions
170 lines (138 loc) · 3.87 KB
/
parallel_densitycomp.cpp
File metadata and controls
170 lines (138 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <pthread.h>
#include <random>
using namespace std;
#define NUM_THREADS 4
struct thread_data { // For passing argument to and from threads
int thread_id;
int pn_steps;
double out;
double acc_ratio;
};
void *partition_function(void *threadarg) {
// Initialize thread data
struct thread_data *my_data;
my_data = (struct thread_data *) threadarg;
long psteps = my_data->pn_steps;
// Initialize other variables
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> acc_dist(0.0,1.0);
uniform_real_distribution<> sampling_dist(-1.0,1.0);
double pi = 3.14159265358979;
double x = sampling_dist(gen);
double p = sampling_dist(gen);
int i = 0;
long steps = pow(10,psteps);
double tot = 0.0;
long accepted_moves = 0.0;
double step = 1.5;
// metropolis MC
while (i < steps) {
double x1 = x + step * sampling_dist(gen);
double p1 = p + step * sampling_dist(gen);
double u0 = exp( -0.5 * p * p - 0.5 * x * x );
double u1 = exp( -0.5 * p1 * p1 - 0.5 * x1 * x1 );
double acceptance_crit = acc_dist(gen);
if ( (u1 / u0) > acceptance_crit ) {
x = x1;
p = p1;
accepted_moves++;
}
tot += 1.0;
i++;
}
double s = static_cast<double>(steps);
double acc = static_cast<double>(accepted_moves);
my_data->acc_ratio = acc / s;
my_data->out = (2 * pi * tot / s); // Return the result of integration
pthread_exit(NULL);
}
void *energy_distribution(void *threadarg) {
// Initialize thread data
struct thread_data *my_data;
my_data = (struct thread_data *) threadarg;
long psteps = my_data->pn_steps;
// Initialize other variables
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> acc_dist(0.0,1.0);
uniform_real_distribution<> sampling_dist(-1.0,1.0);
double pi = 3.14159265358979;
double x = sampling_dist(gen);
double p = sampling_dist(gen);
int i = 0;
long steps = pow(10,psteps);
double tot = 0.0;
long accepted_moves = 0.0;
double step = 1.5;
// metropolis MC
while (i < steps) {
double x1 = x + step * sampling_dist(gen);
double p1 = p + step * sampling_dist(gen);
double u0 = exp( -0.5 * p * p - 0.5 * x * x );
double u1 = exp( -0.5 * p1 * p1 - 0.5 * x1 * x1 );
double acceptance_crit = acc_dist(gen);
if ( (u1 / u0) > acceptance_crit ) {
x = x1;
p = p1;
accepted_moves++;
}
tot += 0.5 * ( p * p + x * x );
i++;
}
double s = static_cast<double>(steps);
double acc = static_cast<double>(accepted_moves);
my_data->acc_ratio = acc / s;
my_data->out = (2 * pi * tot / s); // Return the result of integration
pthread_exit(NULL);
}
int main () {
pthread_t threads[NUM_THREADS];
struct thread_data td[NUM_THREADS];
int rc;
int i;
double numerator = 0.0;
double denominator = 0.0;
double pi = 3.14159265358979;
// Calculate the partition function
for( i = 0; i < NUM_THREADS; i++ ) {
td[i].thread_id = i;
td[i].pn_steps = 7;
rc = pthread_create(&threads[i], NULL, partition_function, (void *)&td[i]);
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
denominator += td[i].out;
}
cout << "Total steps = " << NUM_THREADS * pow(10,td[0].pn_steps) << endl;
denominator = denominator / NUM_THREADS;
cout << "Z = " << denominator << endl;
// Calculate \int \int H exp(-beta H) dx dp
for( i = 0; i < NUM_THREADS; i++ ) {
td[i].thread_id = i;
td[i].pn_steps = 7;
rc = pthread_create(&threads[i], NULL, energy_distribution, (void *)&td[i]);
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
numerator += td[i].out;
}
numerator = numerator / NUM_THREADS;
cout << "<E> = " << numerator / denominator << endl;
pthread_exit(NULL);
return 0;
}