Compare commits

1 Commits

9 changed files with 87 additions and 33 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
*.idb
*.pdb
**obj** **obj**
**bin** **bin**
.cache** .cache**

View File

@@ -1 +1 @@
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" && bash "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat"

10
build.bat Normal file
View File

@@ -0,0 +1,10 @@
@ECHO off
SET flags=-Od -ZI -MTd
SET config=Debug
IF "%1" == "release" (
SET flags=-O2 -MT
SET config=Release
)
SET srcs=src\*
mkdir bin obj
cl %srcs% -I inc %flags% -std:c++20 -MP -Fo:obj\\ -Fe:bin\\

View File

@@ -1,13 +0,0 @@
(
cd bin
rm main.*
srcs=../src/*
if [ $# -eq 1 ] && [ "$1" == "release" ]
then
flags="-O2"
else
flags="-Od -ZI"
fi
echo $flags
cl $srcs -I ../inc/ $flags -std:c++20 -Fe
)

View File

@@ -1,5 +1,11 @@
// raddbg 0.9.21 project file // raddbg 0.9.21 project file
recent_file: path: "C:/Program Files (x86)/Windows Kits/10/include/10.0.26100.0/ucrt/stdio.h"
recent_file: path: "d:/os/obj/amd64fre/minkernel/crts/ucrt/src/appcrt/stdio/xmt/objfre/amd64/minkernel/crts/ucrt/src/appcrt/stdio/output.cpp"
recent_file: path: "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/include/xutility"
recent_file: path: "C:/Users/sethh/Documents/repos/Petri/inc/genetic.h"
recent_file: path: "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.44.35207/include/algorithm"
recent_file: path: "C:/Users/sethh/Documents/repos/Petri/inc/sync.h"
recent_file: path: "inc/genetic.h" recent_file: path: "inc/genetic.h"
recent_file: path: "inc/sync.h" recent_file: path: "inc/sync.h"
recent_file: path: "d:/os/obj/amd64fre/minkernel/crts/ucrt/src/appcrt/startup/mt/objfre/amd64/minkernel/crts/ucrt/src/appcrt/startup/abort.cpp" recent_file: path: "d:/os/obj/amd64fre/minkernel/crts/ucrt/src/appcrt/startup/mt/objfre/amd64/minkernel/crts/ucrt/src/appcrt/startup/abort.cpp"
@@ -21,5 +27,5 @@ target:
breakpoint: breakpoint:
{ {
source_location: "inc/genetic.h:292:1" source_location: "inc/genetic.h:292:1"
hit_count: 1 hit_count: 0
} }

22
export.bat Normal file
View File

@@ -0,0 +1,22 @@
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO [ > compile_commands.json
FOR /r "src\" %%F IN (*.cpp) DO (
SET "file=%%F"
SET "file=!file:\=/!"
SET "directory=%~dp0"
SET "directory=!directory:\=/!"
ECHO { >> compile_commands.json
ECHO "directory": "!directory!", >> compile_commands.json
ECHO "command": "cl !file! -I inc %flags% -std:c++20 -MP -Fo:obj\\ ", >> compile_commands.json
ECHO "file": "!file!" >> compile_commands.json
ECHO }, >> compile_commands.json
)
ECHO ] >> compile_commands.json

View File

@@ -1,5 +0,0 @@
srcs=src/*
echo [ > compile_commands.json
find src -iname "*.cpp" -exec sh -c 'echo { \"directory\": \"$(cygpath -m $(pwd))\", \"command\": \"cl "$(cygpath -m {})" -I inc -Od -std:c++20 -Fo\", \"file\": \"$(cygpath -m {})\" }, >> compile_commands.json' \;
echo ] >> compile_commands.json

View File

@@ -2,6 +2,8 @@
#include <algorithm> #include <algorithm>
#include <cfloat> #include <cfloat>
#include <cstdarg>
#include <cstdio>
#include <cstdlib> #include <cstdlib>
#include "util.h" #include "util.h"
@@ -16,6 +18,29 @@ template <class T> struct Stats;
template <class T> struct Strategy; template <class T> struct Strategy;
struct CellTracker; struct CellTracker;
const char *global_stat_format_str = "GLOBAL, Progress %.1f%%, Top: %.5e, Overhead Per: %.4f%%, Gen: %.4f, Overhead: %.4f, Cross: %.4f (s), Mutate: %.4f (s), Fitness: %.4f (s), Sorting: %.4f (s)\n";
const char *thread_stat_format_str = "%d, Progress %d/%d, Top: %.5e, Overhead Per: %.4f%%, Gen: %.4f, Overhead: %.4f, Cross: %.4f (s), Mutate: %.4f (s), Fitness: %.4f (s), Sorting: %.4f (s)\n";
static int stat_str_len = 2*max(strlen(thread_stat_format_str), strlen(global_stat_format_str));
static char *stat_str = (char*)malloc(stat_str_len);
static char *filename = (char*)malloc(64);
static int n_threads = 0;
void log(const char *format_str, ...) {
va_list list;
va_start(list, format_str);
vsprintf_s(stat_str, 2*max(strlen(thread_stat_format_str), strlen(global_stat_format_str)), format_str, list);
printf("%s", stat_str);
FILE *f;
sprintf(filename, "logs/logs-%d.txt", n_threads);
fopen_s(&f, filename, "a");
fwrite(stat_str, sizeof(char), strlen(stat_str), f);
fclose(f);
}
template <class T> T run(Strategy<T>); template <class T> T run(Strategy<T>);
template <class T> struct Strategy { template <class T> struct Strategy {
@@ -120,8 +145,6 @@ template <class T> DWORD worker(LPVOID args) {
bool gt = strat.higher_fitness_is_better; // Writing strat.higher... is annoying bool gt = strat.higher_fitness_is_better; // Writing strat.higher... is annoying
// printf("Core: %d\n", get_affinity());
TimeSpan start, diff, gen_start; TimeSpan start, diff, gen_start;
while(stats.gen < strat.num_generations) { while(stats.gen < strat.num_generations) {
gen_start = now(); gen_start = now();
@@ -200,9 +223,13 @@ template <class T> DWORD worker(LPVOID args) {
append(stats.fitness_time, now() - start); append(stats.fitness_time, now() - start);
unlock(stats.m); unlock(stats.m);
auto comp = [strat](CellTracker &a, CellTracker &b){
return strat.higher_fitness_is_better ? (a.score > b.score) : (a.score < b.score);
};
// 4. sort // 4. sort
start = now(); start = now();
std::sort(&trackers[0], &trackers[trackers.len-1], [strat](CellTracker &a, CellTracker &b){ return better(strat.higher_fitness_is_better, a.score, b.score) == a.score; }); std::sort(&trackers[0], &trackers[trackers.len-1], comp);
lock(stats.m); lock(stats.m);
append(stats.sorting_time, now() - start); append(stats.sorting_time, now() - start);
@@ -256,12 +283,13 @@ template <class T> T run(Strategy<T> strat) {
threads[i] = make_thread(worker<T>, &args[i], i+1); threads[i] = make_thread(worker<T>, &args[i], i+1);
} }
// We are the stats thread // We are the stats thread
bool complete = false; bool complete = false;
while (!complete) { while (!complete) {
sleep(from_s(strat.stats_print_period_s)); sleep(from_s(strat.stats_print_period_s));
printf("**********************\n"); log("**********************\n");
float g_avg_gen_time = 0; float g_avg_gen_time = 0;
float g_avg_crossover_time = 0; float g_avg_crossover_time = 0;
float g_avg_mutate_time = 0; float g_avg_mutate_time = 0;
@@ -273,6 +301,7 @@ template <class T> T run(Strategy<T> strat) {
complete = true; complete = true;
for (int i = 0; i < stats.len; i++) { for (int i = 0; i < stats.len; i++) {
lock(stats[i].m); lock(stats[i].m);
complete &= stats[i].done; complete &= stats[i].done;
@@ -301,7 +330,8 @@ template <class T> T run(Strategy<T> strat) {
g_avg_overhead_time += overhead; g_avg_overhead_time += overhead;
printf("%d, Progress %d/%d, Top: %.5e, Overhead Per: %.4f%%, Gen: %.4f, Overhead: %.4f, Cross: %.4f (s), Mutate: %.4f (s), Fitness: %.4f (s), Sorting: %.4f (s)\n", i, stats[i].gen, strat.num_generations, best_score, overhead_per, gen_time, overhead, crossover_time, mutate_time, fitness_time, sorting_time); log(thread_stat_format_str, i, stats[i].gen, strat.num_generations, best_score, overhead_per, gen_time, overhead, crossover_time, mutate_time, fitness_time, sorting_time);
unlock(stats[i].m); unlock(stats[i].m);
} }
@@ -316,8 +346,8 @@ template <class T> T run(Strategy<T> strat) {
float g_avg_overhead_per = g_avg_overhead_time / g_avg_gen_time * 100; float g_avg_overhead_per = g_avg_overhead_time / g_avg_gen_time * 100;
printf("GLOBAL, Progress %.1f%%, Top: %.5e, Overhead Per: %.4f%%, Gen: %.4f, Overhead: %.4f, Cross: %.4f (s), Mutate: %.4f (s), Fitness: %.4f (s), Sorting: %.4f (s)\n", g_progress_per, g_best_fitness, g_avg_overhead_per, g_avg_gen_time, g_avg_overhead_time, g_avg_crossover_time, g_avg_mutate_time, g_avg_fitness_time, g_avg_sorting_time); log(global_stat_format_str, g_progress_per, g_best_fitness, g_avg_overhead_per, g_avg_gen_time, g_avg_overhead_time, g_avg_crossover_time, g_avg_mutate_time, g_avg_fitness_time, g_avg_sorting_time);
if (complete) break; if (complete) break;
} }

View File

@@ -75,22 +75,24 @@ int main(int argc, char **argv) {
.crossover=crossover, .crossover=crossover,
.fitness=fitness .fitness=fitness
}; };
n_threads = atoi(argv[1]);
log("Running w/ %d threads\n", atoi(argv[1]));
TimeSpan start = now(); TimeSpan start = now();
auto best_cell = run(strat); auto best_cell = run(strat);
TimeSpan runtime = now() - start; TimeSpan runtime = now() - start;
float sum = 0; float sum = 0;
float product = 1; float product = 1;
printf("Winning cell: "); log("Winning cell: ");
for (int i = 0; i < best_cell.len; i++) { for (int i = 0; i < best_cell.len; i++) {
float val = best_cell[i]; float val = best_cell[i];
sum += val; sum += val;
product *= val; product *= val;
printf("%f ", val); log("%f ", val);
} }
printf("\n"); log("\n");
printf("Final Sum: %f\n", sum); log("Final Sum: %f\n", sum);
printf("Final Product: %f\n", product); log("Final Product: %f\n", product);
printf("Execution Time %d (min) %f (s)\n", static_cast<int>(sync::to_min(runtime)), fmod(to_s(runtime), 60) ); log("Execution Time %d (min) %f (s)\n", static_cast<int>(sync::to_min(runtime)), fmod(to_s(runtime), 60) );
} }