-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequential_nBody_benchmarking.c
131 lines (100 loc) · 3.65 KB
/
sequential_nBody_benchmarking.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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#define SOFTENING 1e-9f
/*Implementing the simulation of the n-body problem
Sequential_version, taken from the example given
at link https://github.com/harrism/mini-nbody/blob/master/nbody.c
and adapted*/
typedef struct{
float mass;
float x, y, z;
float vx, vy, vz;
} Particle;
/* Functions definition */
int convertStringToInt(char *str);
void bodyForce(Particle *p, float dt, int n);
int main(int argc, char* argv[]){
int nBodies = 10; //number of bodies if no paramters is given from command-line
if (argc > 1) nBodies = convertStringToInt(argv[1]);
const float dt = 0.01f; // time step
const int nIters = 10; // simulation iterations
clock_t startIter, endIter;
clock_t startTotal = clock(), endTotal;
double totalTime = 0.0;
Particle *particles = NULL;
particles = (Particle *)malloc(nBodies * sizeof(Particle));
FILE *fileRead = fopen("particles.txt", "r");
if (fileRead == NULL){
/* Impossibile aprire il file */
printf("\nImpossibile aprire il file.\n");
exit(EXIT_FAILURE);
}
int particlesRead = fread(particles, sizeof(Particle) * nBodies, 1, fileRead);
if( particlesRead == 0){
/*il numero di particelle da leggere è maggiore del numero di particelle nel file*/
printf("ERROR: Il numero di particelle da leggere è maggiore del numero di particelle nel file\n");
exit(EXIT_FAILURE);
}
fclose(fileRead);
for (int iter = 1; iter <= nIters; iter++){
startIter = clock();
bodyForce(particles, dt, nBodies); // compute interbody forces
for (int i = 0; i < nBodies; i++){ // integrate position
particles[i].x += particles[i].vx * dt;
particles[i].y += particles[i].vy * dt;
particles[i].z += particles[i].vz * dt;
}
endIter = clock() - startIter;
printf("Iterazione %d di %d completata in %f seconds\n", iter, nIters, (double)endIter / CLOCKS_PER_SEC);
}
endTotal = clock();
totalTime = (double)(endTotal - startTotal) / CLOCKS_PER_SEC;
double avgTime = totalTime / (double)(nIters);
printf("\nAvg iteration time: %f seconds\n", avgTime);
printf("Total time: %f seconds\n", totalTime);
free(particles);
}
/*Conversione da stringa a intero*/
int convertStringToInt(char *str){
char *endptr;
long val;
errno = 0; //Per distinguere successo/fallimento dopo la chiamata
val = strtol(str, &endptr, 10);
/* Check possibili errori */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) {
perror("strtol");
exit(EXIT_FAILURE);
}
if (endptr == str) {
fprintf(stderr, "No digits were found\n");
exit(EXIT_FAILURE);
}
/* Se siamo qui, strtol() ha convertito un numero correttamente */
return (int)val;
}
/*Function that make computation*/
void bodyForce(Particle *p, float dt, int n){
for (int i = 0; i < n; i++){
float Fx = 0.0f;
float Fy = 0.0f;
float Fz = 0.0f;
for (int j = 0; j < n; j++){
float dx = p[j].x - p[i].x;
float dy = p[j].y - p[i].y;
float dz = p[j].z - p[i].z;
float distSqr = dx * dx + dy * dy + dz * dz + SOFTENING;
float invDist = 1.0f / sqrtf(distSqr);
float invDist3 = invDist * invDist * invDist;
Fx += dx * invDist3;
Fy += dy * invDist3;
Fz += dz * invDist3;
}
p[i].vx += dt * Fx;
p[i].vy += dt * Fy;
p[i].vz += dt * Fz;
}
}