some fix and better benshmarking
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,5 +1,5 @@
|
|||||||
CC := gcc
|
CC := gcc
|
||||||
CFLAGS := -Wall -Wextra -g -fsanitize=address,undefined $(shell sdl2-config --cflags)
|
CFLAGS := -Wall -Wextra -g $(shell sdl2-config --cflags)
|
||||||
CFLAGS += -O3 -march=native -fopt-info-optimized
|
CFLAGS += -O3 -march=native -fopt-info-optimized
|
||||||
#CFLAGS += -fsanitize=address
|
#CFLAGS += -fsanitize=address
|
||||||
LDFLAGS := $(shell sdl2-config --libs)
|
LDFLAGS := $(shell sdl2-config --libs)
|
||||||
|
|||||||
100
src/main.c
100
src/main.c
@@ -14,57 +14,32 @@
|
|||||||
#define SEED (5)
|
#define SEED (5)
|
||||||
#define WIDTH (800)
|
#define WIDTH (800)
|
||||||
#define HEIGHT (600)
|
#define HEIGHT (600)
|
||||||
#define SIM_WIDTH (80)
|
#define SIM_WIDTH (800)
|
||||||
#define SIM_HEIGHT (60)
|
#define SIM_HEIGHT (600)
|
||||||
|
#define N_SIMULATION (1000)
|
||||||
|
|
||||||
const int FPS = 15;
|
const int FPS = 60;
|
||||||
const int FRAME_DELAY = 1000 / FPS;
|
const int FRAME_DELAY = 1000 / FPS;
|
||||||
|
|
||||||
t_flags process_args(int argc, char** argv) {
|
|
||||||
t_flags output_flags = {0};
|
|
||||||
if (argc <= 1)
|
|
||||||
return output_flags;
|
|
||||||
if (argv[1][0] != '-')
|
|
||||||
return output_flags;
|
|
||||||
|
|
||||||
char* flags = argv[1];
|
|
||||||
int i = 1;
|
|
||||||
char c = flags[i];
|
|
||||||
while (c) {
|
|
||||||
if (c == 'g')
|
|
||||||
output_flags.graphical = 1;
|
|
||||||
if (c == 'm')
|
|
||||||
output_flags.multithread = 1;
|
|
||||||
if (c == 'o')
|
|
||||||
output_flags.opti = 1;
|
|
||||||
c = flags[++i];
|
|
||||||
}
|
|
||||||
return output_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
pthread_t* threads;
|
pthread_t* threads = NULL;
|
||||||
int n_threads = sysconf(_SC_NPROCESSORS_ONLN);
|
int n_threads = sysconf(_SC_NPROCESSORS_ONLN);
|
||||||
size_t n_chunk;
|
size_t n_chunk = 0;
|
||||||
ThreadArgs* ta;
|
ThreadArgs* ta = NULL;
|
||||||
t_flags flags = process_args(argc, argv);
|
t_flags flags = process_args(argc, argv);
|
||||||
printf("G : %d , M : %d (n : %d)\n", flags.graphical, flags.multithread,
|
printf("G : %d , M : %d (n : %d)\n", flags.graphical, flags.multithread,
|
||||||
n_threads);
|
n_threads);
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
if (flags.graphical) {
|
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
||||||
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
if (flags.multithread) {
|
||||||
b.pixels[0 * SIM_WIDTH + 1] = WHITE;
|
ta = make_chunk(&b, (int)n_threads);
|
||||||
b.pixels[1 * SIM_WIDTH + 2] = WHITE;
|
threads = malloc(sizeof(pthread_t) * n_threads);
|
||||||
b.pixels[2 * SIM_WIDTH + 0] = WHITE;
|
n_chunk = b.height / n_threads;
|
||||||
b.pixels[2 * SIM_WIDTH + 1] = WHITE;
|
}
|
||||||
b.pixels[2 * SIM_WIDTH + 2] = WHITE;
|
TimeFrameHeap* timeframe = NULL;
|
||||||
if (flags.multithread) {
|
|
||||||
ta = make_chunk(&b, (int)n_threads);
|
|
||||||
threads = malloc(sizeof(pthread_t) * n_threads);
|
|
||||||
n_chunk = b.height / n_threads;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (flags.graphical) {
|
||||||
// SDL2
|
// SDL2
|
||||||
SDL_Init(SDL_INIT_VIDEO);
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
SDL_Window* win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0);
|
SDL_Window* win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0);
|
||||||
@@ -79,15 +54,16 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
// While running frames
|
// While running frames
|
||||||
int running = 1;
|
int running = 1;
|
||||||
int paused = 0;
|
int paused = 1;
|
||||||
int next_frame = 0;
|
int next_frame = 1;
|
||||||
|
struct timespec start, end;
|
||||||
|
double elapsed;
|
||||||
Uint32 frame_start;
|
Uint32 frame_start;
|
||||||
Uint32 frame_time;
|
Uint32 frame_time;
|
||||||
SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
|
SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
|
||||||
while (running) {
|
while (running) {
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
frame_start = SDL_GetTicks();
|
frame_start = SDL_GetTicks();
|
||||||
if (next_frame)
|
|
||||||
next_frame = 0;
|
|
||||||
while (SDL_PollEvent(&e)) {
|
while (SDL_PollEvent(&e)) {
|
||||||
if (e.type == SDL_QUIT)
|
if (e.type == SDL_QUIT)
|
||||||
running = 0;
|
running = 0;
|
||||||
@@ -111,30 +87,38 @@ int main(int argc, char* argv[]) {
|
|||||||
: process_game_mt(ta, b, threads, n_threads);
|
: process_game_mt(ta, b, threads, n_threads);
|
||||||
}
|
}
|
||||||
frame_time = SDL_GetTicks() - frame_start;
|
frame_time = SDL_GetTicks() - frame_start;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
|
elapsed = (end.tv_sec - start.tv_sec) +
|
||||||
|
(end.tv_nsec - start.tv_nsec) / 1e9;
|
||||||
|
timeframe = add_time_frame(timeframe, elapsed);
|
||||||
if (frame_time < FRAME_DELAY)
|
if (frame_time < FRAME_DELAY)
|
||||||
SDL_Delay(FRAME_DELAY - frame_time);
|
SDL_Delay(FRAME_DELAY - frame_time);
|
||||||
|
if (next_frame)
|
||||||
|
next_frame = 0;
|
||||||
}
|
}
|
||||||
|
printf("Average time between frames : %.6fs\n",
|
||||||
|
avg_time_frame(timeframe));
|
||||||
SDL_DestroyTexture(tex);
|
SDL_DestroyTexture(tex);
|
||||||
SDL_DestroyRenderer(ren);
|
SDL_DestroyRenderer(ren);
|
||||||
SDL_DestroyWindow(win);
|
SDL_DestroyWindow(win);
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
free_board(b);
|
|
||||||
free(threads);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
struct timespec start, end;
|
||||||
display_board(b);
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
puts("----------");
|
for (int i = 0; i < N_SIMULATION; i++) {
|
||||||
b = process_game(b);
|
b = !flags.multithread ? process_game(b)
|
||||||
display_board(b);
|
: process_game_mt(ta, b, threads, n_threads);
|
||||||
puts("----------");
|
}
|
||||||
b = process_game(b);
|
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||||
display_board(b);
|
double elapsed =
|
||||||
puts("----------");
|
(end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;
|
||||||
b = process_game(b);
|
printf("Simulation with %d iteration, duration : %.6f s\n",
|
||||||
display_board(b);
|
N_SIMULATION, elapsed);
|
||||||
free_board(b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free_board(b);
|
||||||
|
free(ta);
|
||||||
|
free(threads);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
45
src/utils.c
Normal file
45
src/utils.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
t_flags process_args(int argc, char** argv) {
|
||||||
|
t_flags output_flags = {0};
|
||||||
|
if (argc <= 1)
|
||||||
|
return output_flags;
|
||||||
|
if (argv[1][0] != '-')
|
||||||
|
return output_flags;
|
||||||
|
|
||||||
|
char* flags = argv[1];
|
||||||
|
int i = 1;
|
||||||
|
char c = flags[i];
|
||||||
|
while (c) {
|
||||||
|
if (c == 'g')
|
||||||
|
output_flags.graphical = 1;
|
||||||
|
if (c == 'm')
|
||||||
|
output_flags.multithread = 1;
|
||||||
|
if (c == 'o')
|
||||||
|
output_flags.opti = 1;
|
||||||
|
c = flags[++i];
|
||||||
|
}
|
||||||
|
return output_flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
TimeFrameHeap* add_time_frame(TimeFrameHeap* heap, double time) {
|
||||||
|
TimeFrameHeap* new_head = malloc(sizeof(TimeFrameHeap));
|
||||||
|
new_head->time = time;
|
||||||
|
new_head->next = heap;
|
||||||
|
return new_head;
|
||||||
|
}
|
||||||
|
|
||||||
|
double avg_time_frame(TimeFrameHeap* heap) {
|
||||||
|
size_t count = 0;
|
||||||
|
double output = 0;
|
||||||
|
TimeFrameHeap* need_to_be_free;
|
||||||
|
while (heap) {
|
||||||
|
count += 1;
|
||||||
|
output += heap->time;
|
||||||
|
need_to_be_free = heap;
|
||||||
|
heap = heap->next;
|
||||||
|
free(need_to_be_free);
|
||||||
|
}
|
||||||
|
return output / (double)count;
|
||||||
|
}
|
||||||
18
src/utils.h
18
src/utils.h
@@ -1,12 +1,20 @@
|
|||||||
#ifndef UTILS_H
|
#ifndef UTILS_H
|
||||||
#define UTILS_H
|
#define UTILS_H
|
||||||
|
|
||||||
|
#include <SDL2/SDL_stdinc.h>
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int graphical;
|
int graphical;
|
||||||
int multithread;
|
int multithread;
|
||||||
int opti;
|
int opti;
|
||||||
} t_flags;
|
} t_flags;
|
||||||
|
|
||||||
t_flags process_args(int argc, char *argv[]);
|
t_flags process_args(int argc, char* argv[]);
|
||||||
|
|
||||||
#endif // !UTILS_H
|
typedef struct time_frame {
|
||||||
|
double time;
|
||||||
|
struct time_frame* next;
|
||||||
|
} TimeFrameHeap;
|
||||||
|
|
||||||
|
TimeFrameHeap* add_time_frame(TimeFrameHeap* heap, double time);
|
||||||
|
double avg_time_frame(TimeFrameHeap* heap);
|
||||||
|
#endif // !UTILS_H
|
||||||
|
|||||||
Reference in New Issue
Block a user