-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbody-threads.c
224 lines (157 loc) · 5.98 KB
/
nbody-threads.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <pthread.h>
#define NUM_THREADS 1
#define CONST_GravConstant 0.01
typedef struct{
double x,y,z;
} vector;
typedef struct {
long start;
long end;
} slice_t;
pthread_barrier_t GLOBAL_barrier;
int GLOBAL_numBodies = 300; // default number of bodies
int GLOBAL_numSteps = 1; // default number of time steps (1 million)
int GLOBAL_windowWidth=800; // default window width is 800 pixels
int GLOBAL_windowHeight=800; // default window height is 800 pixels
double *GLOBAL_masses;
vector *GLOBAL_positions;
vector *GLOBAL_velocities;
vector *GLOBAL_accelerations;
////////////////////////////////////////////////////////////////////////
vector addVectors(vector a,vector b){
return (vector) {a.x + b.x, a.y + b.y, a.z + b.z};
}
////////////////////////////////////////////////////////////////////////
vector scaleVector(double b,vector a){
return (vector) {b * a.x, b * a.y, b * a.z};
}
////////////////////////////////////////////////////////////////////////
vector subtractVectors(vector a,vector b){
return (vector) {a.x - b.x, a.y - b.y , a.z - b.z};
}
////////////////////////////////////////////////////////////////////////
double mod(vector a){
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
////////////////////////////////////////////////////////////////////////
void initSystemFromRandom() {
GLOBAL_masses = (double*)malloc(GLOBAL_numBodies*sizeof(double));
GLOBAL_positions = (vector*)malloc(GLOBAL_numBodies*sizeof(vector));
GLOBAL_velocities = (vector*)malloc(GLOBAL_numBodies*sizeof(vector));
GLOBAL_accelerations = (vector*)malloc(GLOBAL_numBodies*sizeof(vector));
for (int i = 0; i < GLOBAL_numBodies; i += 1) {
GLOBAL_masses[i] = rand()%GLOBAL_numBodies;
GLOBAL_positions[i].x = rand()%GLOBAL_windowWidth;
GLOBAL_positions[i].y = rand()%GLOBAL_windowHeight;
GLOBAL_positions[i].z = 0;
GLOBAL_velocities[i].x = GLOBAL_velocities[i].y = GLOBAL_velocities[i].z = 0;
}
}
////////////////////////////////////////////////////////////////////////
void showSystem(){
for (int i = 0; i < GLOBAL_numBodies; i += 1) {
fprintf(stderr,"Body %d : %lf\t%lf\t%lf\t|\t%lf\t%lf\t%lf\n",i,\
GLOBAL_positions[i].x,GLOBAL_positions[i].y,GLOBAL_positions[i].z,\
GLOBAL_velocities[i].x,GLOBAL_velocities[i].y,GLOBAL_velocities[i].z);
}
}
////////////////////////////////////////////////////////////////////////
void validateSystem(){
for(int i = 0; i < GLOBAL_numBodies; i += 1) {
if (isnan(GLOBAL_positions[i].x) || isnan(GLOBAL_positions[i].y) || isnan(GLOBAL_positions[i].z) || \
isnan(GLOBAL_velocities[i].x) || isnan(GLOBAL_velocities[i].y) || isnan(GLOBAL_velocities[i].z) ) {
fprintf(stderr,"NAN Body %d : %lf\t%lf\t%lf\t|\t%lf\t%lf\t%lf\n",i,\
GLOBAL_positions[i].x,GLOBAL_positions[i].y,GLOBAL_positions[i].z,\
GLOBAL_velocities[i].x,GLOBAL_velocities[i].y,GLOBAL_velocities[i].z);
exit(1);
}
}
}
////////////////////////////////////////////////////////////////////////
void *computeAccelerations(void *arg){
slice_t *slice = (slice_t*) arg;
for (int i = slice->start; i < slice->end + 1; i += 1) {
GLOBAL_accelerations[i] = (vector) {0, 0, 0};
for (int j = 0; j < GLOBAL_numBodies; j += 1) {
if (i != j) {
GLOBAL_accelerations[i] = addVectors(GLOBAL_accelerations[i],scaleVector(CONST_GravConstant*GLOBAL_masses[j]/pow(mod(subtractVectors(GLOBAL_positions[i],GLOBAL_positions[j])),3),subtractVectors(GLOBAL_positions[j],GLOBAL_positions[i])));
}
}
}
pthread_barrier_wait(&GLOBAL_barrier);
pthread_exit(NULL);
}
////////////////////////////////////////////////////////////////////////
void computePositionsAnddVelocities(){
for(int i = 0; i < GLOBAL_numBodies; i += 1) {
//printf("bitch %d\n", i);
GLOBAL_positions[i] = addVectors(GLOBAL_positions[i],addVectors(GLOBAL_velocities[i],scaleVector(0.5,GLOBAL_accelerations[i])));
GLOBAL_velocities[i] = addVectors(GLOBAL_velocities[i], GLOBAL_accelerations[i]);
}
}
////////////////////////////////////////////////////////////////////////
void resolveCollisions(){
for (int i = 0 ; i < GLOBAL_numBodies - 1; i += 1) {
for (int j = i + 1; j < GLOBAL_numBodies; j += 1) {
if (GLOBAL_positions[i].x == GLOBAL_positions[j].x && GLOBAL_positions[i].y == GLOBAL_positions[j].y && GLOBAL_positions[i].z == GLOBAL_positions[j].z){
vector temp = GLOBAL_velocities[i];
GLOBAL_velocities[i] = GLOBAL_velocities[j];
GLOBAL_velocities[j] = temp;
}
}
}
}
////////////////////////////////////////////////////////////////////////
void simulate(){
long slice_width;
int i;
pthread_t threads[NUM_THREADS];
slice_width = GLOBAL_numBodies / NUM_THREADS;
slice_t slices[NUM_THREADS];
for (i = 0; i < NUM_THREADS; i += 1) {
slices[i].start = i * slice_width;
if (i == NUM_THREADS - 1)
slices[i].end = GLOBAL_numBodies - 1;
else
slices[i].end = slices[i].start + slice_width - 1;
}
pthread_barrier_init(&GLOBAL_barrier, NULL, NUM_THREADS);
for (i = 0; i < NUM_THREADS; i += 1)
pthread_create(&threads[i], NULL, computeAccelerations, (void *) &slices[i]);
for (i = 0; i < NUM_THREADS; i += 1)
pthread_join(threads[i], NULL);
pthread_barrier_destroy(&GLOBAL_barrier);
computePositionsAnddVelocities();
resolveCollisions();
validateSystem();
}
////////////////////////////////////////////////////////////////////////
int main(int argC,char* argV[])
{
const unsigned int randSeed=10; // default value; do not change
if (argC >= 3) {
GLOBAL_numBodies=atoi(argV[1]);
GLOBAL_numSteps=atoi(argV[2]);
if(argC >= 5) {
GLOBAL_windowWidth=atoi(argV[3]);
GLOBAL_windowHeight=atoi(argV[4]);
}
} else if (argC!=1) {
printf("Usage : %s [numBodies numSteps [windowWidth windowHeight]]\n",argV[0]);
exit(1);
}
srand(randSeed);
initSystemFromRandom();
//showSystem();
for(int i = 0; i < GLOBAL_numSteps; i += 1){
//printf("%d\n", i);
simulate();
//showSystem();
}
// dump final result
showSystem();
pthread_exit(NULL);
}