-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
143 lines (121 loc) · 3.22 KB
/
main.c
File metadata and controls
143 lines (121 loc) · 3.22 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
/*! \file main.c
* \brief A file containing the main entry point for the program..
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char checkTileForEntity(short x, short y);
char checkTileForShark(short x, short y);
char checkTileForFish(short x, short y);
void _activateAt(int index, char fishFlag);
void deactivateAt(short x, short y);
void activateFishAt(short x, short y);
void activateSharkAt(short x, short y);
#include "Globals.h"
#include "Drawing.h"
#include "World.h"
#include <omp.h>
/*!
* \brief Saves a performance log for the simulation.
*
* \param fpsLogs The array of logged frames per second.
*/
int saveLogTofile(float * averageFrameLog)
{
char name [50];
float minimum = 10000000.0f;
float maximum = 0.0f;
float average = 0.0f;
float sum = 0.0f;
int count = 0;
for (count = 0; count < NUMBER_OF_RUNS; count++)
{
sum += averageFrameLog[count];
if (averageFrameLog[count] > maximum)
{
maximum = averageFrameLog[count];
}
if (averageFrameLog[count] < minimum)
{
minimum = averageFrameLog[count];
}
}
average = sum / (float)count;
int length = sprintf (name, "%d %s %d %s %d %s\n", GRID_ROWS, "x", GRID_COLUMNS, ":", numThreads, "cores.log");
name[length-1] = '\0';
FILE * pLogFile = fopen( "Performance_data.log", "w" );
FILE * pFile = fopen( name, "w" );
// Added a comment tag, this is for use in gnuplot
fprintf( pFile, "#Min #Max #Average\n");
fprintf( pLogFile, "#Min #Max #Average\n");
fprintf( pFile, "%f%s%f%s%f\n", minimum,"\t", maximum, "\t", average);
fprintf( pLogFile, "%f%s%f%s%f\n", minimum, "\t", maximum, "\t", average);
close( pLogFile );
close( pFile );
}
/*! \brief The main entry point for the program.
*/
int main(int argc, char *argv[])
{
omp_set_num_threads(numThreads);
omp_set_nested(1);
int runCount = 0;
float averageFrames[NUMBER_OF_RUNS];
while (runCount < NUMBER_OF_RUNS)
{
printf( "Simulation will run for %d seconds.\n", SIMULATION_LENGTH );
int running= 1;
float totalSeconds = 0.0;
double secondTimer = 0.0;
int frameCounter = 0;
time_t currentTime = 0;
time_t previousTime = 0;
float framesPerSecond[SIMULATION_LENGTH];
populateWorld(N_FISH,N_SHARKS);
if (InitializeOpenGL())
{
int count = 0;
while (running)
{
++count;
if (count == CYCLES_PER_FRAME)
{
count = 0;
updateWorld();
if (DRAW_GRID)
{
XGetWindowAttributes(dpy, win, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
DrawBackground();
drawWorld();
glXSwapBuffers(dpy, win);
}
else
{
frameCounter++;
currentTime = time(NULL);
if (previousTime)
{
secondTimer += difftime(currentTime, previousTime);
}
previousTime = time(NULL);
if (secondTimer >= 1.0)
{
totalSeconds += secondTimer;
secondTimer = 0.0;
if (totalSeconds >= SIMULATION_LENGTH)
{
averageFrames[runCount] = (float)frameCounter / totalSeconds;
running = 0;
runCount ++;
}
}
}
}
}
}
printf( "Simulation complete.\n");
}
saveLogTofile(averageFrames);
printf( "Performance data saved to log file.\n");
}