diff --git a/.gitignore b/.gitignore index e263687..a520516 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +*.idb +*.pdb **obj** **bin** .cache** diff --git a/run_before_build.bat b/activate.bat similarity index 65% rename from run_before_build.bat rename to activate.bat index 8a4e494..7a6b935 100644 --- a/run_before_build.bat +++ b/activate.bat @@ -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" diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..cdd3073 --- /dev/null +++ b/build.bat @@ -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\\ diff --git a/build.sh b/build.sh deleted file mode 100644 index 565a2e5..0000000 --- a/build.sh +++ /dev/null @@ -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 -) diff --git a/debug.rad b/debug.rad index 71af2bd..70d96d9 100644 --- a/debug.rad +++ b/debug.rad @@ -1,5 +1,11 @@ // 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/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" @@ -21,5 +27,5 @@ target: breakpoint: { source_location: "inc/genetic.h:292:1" - hit_count: 1 + hit_count: 0 } diff --git a/export.bat b/export.bat new file mode 100644 index 0000000..49b6ec6 --- /dev/null +++ b/export.bat @@ -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 diff --git a/export_compdb.sh b/export_compdb.sh deleted file mode 100644 index 9bd825c..0000000 --- a/export_compdb.sh +++ /dev/null @@ -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 diff --git a/inc/genetic.h b/inc/genetic.h index e08b88c..7181ee0 100644 --- a/inc/genetic.h +++ b/inc/genetic.h @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include "util.h" @@ -16,6 +18,29 @@ template struct Stats; template struct Strategy; 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 T run(Strategy); template struct Strategy { @@ -120,8 +145,6 @@ template DWORD worker(LPVOID args) { bool gt = strat.higher_fitness_is_better; // Writing strat.higher... is annoying - // printf("Core: %d\n", get_affinity()); - TimeSpan start, diff, gen_start; while(stats.gen < strat.num_generations) { gen_start = now(); @@ -200,9 +223,13 @@ template DWORD worker(LPVOID args) { append(stats.fitness_time, now() - start); 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 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); append(stats.sorting_time, now() - start); @@ -256,12 +283,13 @@ template T run(Strategy strat) { threads[i] = make_thread(worker, &args[i], i+1); } + // We are the stats thread bool complete = false; while (!complete) { sleep(from_s(strat.stats_print_period_s)); - printf("**********************\n"); + log("**********************\n"); float g_avg_gen_time = 0; float g_avg_crossover_time = 0; float g_avg_mutate_time = 0; @@ -273,6 +301,7 @@ template T run(Strategy strat) { complete = true; + for (int i = 0; i < stats.len; i++) { lock(stats[i].m); complete &= stats[i].done; @@ -301,7 +330,8 @@ template T run(Strategy strat) { 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); } @@ -316,8 +346,8 @@ template T run(Strategy strat) { 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; } diff --git a/src/main.cpp b/src/main.cpp index cad03e7..cd826ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,22 +75,24 @@ int main(int argc, char **argv) { .crossover=crossover, .fitness=fitness }; + n_threads = atoi(argv[1]); + log("Running w/ %d threads\n", atoi(argv[1])); TimeSpan start = now(); auto best_cell = run(strat); TimeSpan runtime = now() - start; float sum = 0; float product = 1; - printf("Winning cell: "); + log("Winning cell: "); for (int i = 0; i < best_cell.len; i++) { float val = best_cell[i]; sum += val; product *= val; - printf("%f ", val); + log("%f ", val); } - printf("\n"); - printf("Final Sum: %f\n", sum); - printf("Final Product: %f\n", product); - printf("Execution Time %d (min) %f (s)\n", static_cast(sync::to_min(runtime)), fmod(to_s(runtime), 60) ); + log("\n"); + log("Final Sum: %f\n", sum); + log("Final Product: %f\n", product); + log("Execution Time %d (min) %f (s)\n", static_cast(sync::to_min(runtime)), fmod(to_s(runtime), 60) ); }