remove .sh in favor of .bat. Do some testing and verify that running w/ 15 threads does indeed use the whole cpu (via btop)

This commit is contained in:
2025-09-16 15:04:14 -05:00
parent f7e804607f
commit 6365dffda9
9 changed files with 87 additions and 33 deletions

View File

@@ -2,6 +2,8 @@
#include <algorithm>
#include <cfloat>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include "util.h"
@@ -16,6 +18,29 @@ template <class T> struct Stats;
template <class T> 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 <class T> T run(Strategy<T>);
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
// 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 <class T> 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 <class T> T run(Strategy<T> strat) {
threads[i] = make_thread(worker<T>, &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 <class T> T run(Strategy<T> strat) {
complete = true;
for (int i = 0; i < stats.len; i++) {
lock(stats[i].m);
complete &= stats[i].done;
@@ -301,7 +330,8 @@ template <class T> T run(Strategy<T> 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 <class T> T run(Strategy<T> 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;
}