some fix and better benshmarking
This commit is contained in:
2
Makefile
2
Makefile
@@ -1,5 +1,5 @@
|
||||
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 += -fsanitize=address
|
||||
LDFLAGS := $(shell sdl2-config --libs)
|
||||
|
||||
100
src/main.c
100
src/main.c
@@ -14,57 +14,32 @@
|
||||
#define SEED (5)
|
||||
#define WIDTH (800)
|
||||
#define HEIGHT (600)
|
||||
#define SIM_WIDTH (80)
|
||||
#define SIM_HEIGHT (60)
|
||||
#define SIM_WIDTH (800)
|
||||
#define SIM_HEIGHT (600)
|
||||
#define N_SIMULATION (1000)
|
||||
|
||||
const int FPS = 15;
|
||||
const int FPS = 60;
|
||||
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[]) {
|
||||
pthread_t* threads;
|
||||
pthread_t* threads = NULL;
|
||||
int n_threads = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
size_t n_chunk;
|
||||
ThreadArgs* ta;
|
||||
size_t n_chunk = 0;
|
||||
ThreadArgs* ta = NULL;
|
||||
t_flags flags = process_args(argc, argv);
|
||||
printf("G : %d , M : %d (n : %d)\n", flags.graphical, flags.multithread,
|
||||
n_threads);
|
||||
srand(time(NULL));
|
||||
|
||||
if (flags.graphical) {
|
||||
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
||||
b.pixels[0 * SIM_WIDTH + 1] = WHITE;
|
||||
b.pixels[1 * SIM_WIDTH + 2] = WHITE;
|
||||
b.pixels[2 * SIM_WIDTH + 0] = WHITE;
|
||||
b.pixels[2 * SIM_WIDTH + 1] = WHITE;
|
||||
b.pixels[2 * SIM_WIDTH + 2] = WHITE;
|
||||
if (flags.multithread) {
|
||||
ta = make_chunk(&b, (int)n_threads);
|
||||
threads = malloc(sizeof(pthread_t) * n_threads);
|
||||
n_chunk = b.height / n_threads;
|
||||
}
|
||||
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
||||
if (flags.multithread) {
|
||||
ta = make_chunk(&b, (int)n_threads);
|
||||
threads = malloc(sizeof(pthread_t) * n_threads);
|
||||
n_chunk = b.height / n_threads;
|
||||
}
|
||||
TimeFrameHeap* timeframe = NULL;
|
||||
|
||||
if (flags.graphical) {
|
||||
// SDL2
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_Window* win = SDL_CreateWindow("pixels", 0, 0, WIDTH, HEIGHT, 0);
|
||||
@@ -79,15 +54,16 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
// While running frames
|
||||
int running = 1;
|
||||
int paused = 0;
|
||||
int next_frame = 0;
|
||||
int paused = 1;
|
||||
int next_frame = 1;
|
||||
struct timespec start, end;
|
||||
double elapsed;
|
||||
Uint32 frame_start;
|
||||
Uint32 frame_time;
|
||||
SDL_Rect dst = {0, 0, WIDTH, HEIGHT};
|
||||
while (running) {
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
frame_start = SDL_GetTicks();
|
||||
if (next_frame)
|
||||
next_frame = 0;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_QUIT)
|
||||
running = 0;
|
||||
@@ -111,30 +87,38 @@ int main(int argc, char* argv[]) {
|
||||
: process_game_mt(ta, b, threads, n_threads);
|
||||
}
|
||||
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)
|
||||
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_DestroyRenderer(ren);
|
||||
SDL_DestroyWindow(win);
|
||||
SDL_Quit();
|
||||
free_board(b);
|
||||
free(threads);
|
||||
|
||||
} else {
|
||||
Board b = init_board(SIM_WIDTH, SIM_HEIGHT, 1);
|
||||
display_board(b);
|
||||
puts("----------");
|
||||
b = process_game(b);
|
||||
display_board(b);
|
||||
puts("----------");
|
||||
b = process_game(b);
|
||||
display_board(b);
|
||||
puts("----------");
|
||||
b = process_game(b);
|
||||
display_board(b);
|
||||
free_board(b);
|
||||
struct timespec start, end;
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
for (int i = 0; i < N_SIMULATION; i++) {
|
||||
b = !flags.multithread ? process_game(b)
|
||||
: process_game_mt(ta, b, threads, n_threads);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &end);
|
||||
double elapsed =
|
||||
(end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;
|
||||
printf("Simulation with %d iteration, duration : %.6f s\n",
|
||||
N_SIMULATION, elapsed);
|
||||
}
|
||||
|
||||
free_board(b);
|
||||
free(ta);
|
||||
free(threads);
|
||||
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
|
||||
#define UTILS_H
|
||||
|
||||
#include <SDL2/SDL_stdinc.h>
|
||||
typedef struct {
|
||||
int graphical;
|
||||
int multithread;
|
||||
int opti;
|
||||
int graphical;
|
||||
int multithread;
|
||||
int opti;
|
||||
} 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