diff --git a/src/libaevol/ExpManager.cpp b/src/libaevol/ExpManager.cpp index 1007ffc87adb383f2639b7df25ff7855cdb9463d..500d05961f771285bb62bd9e6f0e684c9d01a466 100644 --- a/src/libaevol/ExpManager.cpp +++ b/src/libaevol/ExpManager.cpp @@ -342,11 +342,10 @@ auto t1 = high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - s_t1 ).count(); #endif - bool simd_first = false; +/* bool simd_first = false; if (simd_individual == nullptr) { simd_first = true; - simd_individual = new SIMD_Individual(this); - } + }*/ //} t1 = high_resolution_clock::now(); @@ -365,12 +364,11 @@ auto t1 = high_resolution_clock::now(); auto ta = high_resolution_clock::now(); - if (simd_first) { - simd_individual->run_a_step(best_indiv()->w_max(),selection_pressure(),false); +/* if (simd_first) { //simd_individual->check_result(); - } else { + } else {*/ simd_individual->run_a_step(best_indiv()->w_max(),selection_pressure(),true); - } + //} auto tb = high_resolution_clock::now(); @@ -595,15 +593,18 @@ void ExpManager::run_evolution() { dna_mutator_array_[i] = nullptr; } + simd_individual = new SIMD_Individual(this); + simd_individual->run_a_step(best_indiv()->w_max(),selection_pressure(),false); + - // For each generation + // For each generation while (true) { // termination condition is into the loop printf( "============================== %" PRId64 " ==============================\n", AeTime::time()); if (!first_run) { - if (simd_individual->standalone()) { + if (SIMD_Individual::standalone_simd) { printf( " Best individual's (%d) distance to target (metabolic) : %f (clones %d)\n", simd_individual->best_indiv->indiv_id, diff --git a/src/libaevol/SIMD_Individual.cpp b/src/libaevol/SIMD_Individual.cpp index e71d2e9485695b1d1155633a17c6733a0d2794b0..a92e9063244aa39448280b0b56899e1a26b3d9ed 100644 --- a/src/libaevol/SIMD_Individual.cpp +++ b/src/libaevol/SIMD_Individual.cpp @@ -12,10 +12,12 @@ namespace aevol { + bool SIMD_Individual::standalone_simd = true; + SIMD_Individual::SIMD_Individual(ExpManager* exp_m) { printf("Create SIMD Controller\n"); - + standalone_ = standalone_simd; exp_m_ = exp_m; nb_indivs_ = exp_m_->nb_indivs(); @@ -345,6 +347,10 @@ SIMD_Individual::~SIMD_Individual() { delete[] internal_simd_struct; delete[] dna_size; + + + delete stats_best; + delete stats_mean; } void SIMD_Individual::start_stop_RNA() { @@ -1234,7 +1240,7 @@ void SIMD_Individual::compute_protein() { for (int rna_idx = 0; rna_idx < (int) internal_simd_struct[indiv_id]->rna_count_; rna_idx++) { if (internal_simd_struct[indiv_id]->rnas[rna_idx]->is_init_) { -///#pragma omp parallel for firstprivate(indiv_id, rna_idx) schedule(dynamic) +//#pragma omp parallel for firstprivate(indiv_id, rna_idx) schedule(dynamic) for (int protein_idx = 0; protein_idx < (int) internal_simd_struct[indiv_id]-> rnas[rna_idx]->start_prot.size(); protein_idx++) { @@ -2216,13 +2222,17 @@ void SIMD_Individual::run_a_step(double w_max, double selection_pressure,bool op best_indiv = prev_internal_simd_struct[idx_best]; // Stats - Stats_SIMD* stats_best = new Stats_SIMD(this, AeTime::time(), true); - stats_best->write_best(); - delete stats_best; + if (!optim_prom) { + stats_best = new Stats_SIMD(this, AeTime::time(), true); + stats_mean = new Stats_SIMD(this, AeTime::time(), false); + } else { + stats_best->reinit(AeTime::time()); + stats_mean->reinit(AeTime::time()); + } + - Stats_SIMD* stats_mean = new Stats_SIMD(this, AeTime::time(), false); + stats_best->write_best(); stats_mean->write_average(); - delete stats_mean; if (standalone_ && AeTime::time() % exp_m_->backup_step() == 0) { @@ -2279,7 +2289,7 @@ void SIMD_Individual::run_a_step(double w_max, double selection_pressure,bool op last_gener_file << AeTime::time() << std::endl; last_gener_file.close(); - //if (AeTime::time() != exp_m_->end_step()) { + if (AeTime::time() == exp_m_->end_step()) { for (int indiv_id = 0; indiv_id < (int) exp_m_->nb_indivs(); indiv_id++) { int x = indiv_id / exp_m_->world()->height(); int y = indiv_id % exp_m_->world()->height(); @@ -2288,7 +2298,7 @@ void SIMD_Individual::run_a_step(double w_max, double selection_pressure,bool op exp_m_->world()->grid(x, y)->individual()->genetic_unit_list_nonconst().clear(); delete exp_m_->world()->grid(x, y)->individual(); } - //} + } } diff --git a/src/libaevol/SIMD_Individual.h b/src/libaevol/SIMD_Individual.h index b483ae36436476b278802be5186ddfc5e3677385..d274e17e403c0fbb96f60ae931af9f1ff90de793 100644 --- a/src/libaevol/SIMD_Individual.h +++ b/src/libaevol/SIMD_Individual.h @@ -6,6 +6,7 @@ #define RAEVOL_CUDA_SIMD_INDIVIDUAL_H #include "ExpManager.h" +#include "Stats_SIMD.h" namespace aevol { @@ -245,11 +246,16 @@ class SIMD_Individual { int32_t nb_indivs_; int32_t nb_clones_; + static bool standalone_simd;//= true; + private: ExpManager* exp_m_; int* dna_size; float target[300]; - bool standalone_ = true; + bool standalone_; + + Stats_SIMD* stats_best; + Stats_SIMD* stats_mean; void selection(); diff --git a/src/libaevol/Stats_SIMD.cpp b/src/libaevol/Stats_SIMD.cpp index 231d8243045053f8f3ba70e7b896a17c55f92543..71a554ace245898e5e902f0387f07e40b2b272e9 100644 --- a/src/libaevol/Stats_SIMD.cpp +++ b/src/libaevol/Stats_SIMD.cpp @@ -1,6 +1,10 @@ // // Created by arrouan on 19/01/18. // +#include <iostream> +#include <fstream> +#include <string> +#include <fcntl.h> #include "Stats_SIMD.h" #include "Dna_SIMD.h" @@ -35,30 +39,88 @@ Stats_SIMD::Stats_SIMD(SIMD_Individual* simd_individual, int64_t generation, boo nb_inv_ = 0; - if (generation_==1) { - statfile_best_.open("stats/stats_simd_best.csv",std::ofstream::trunc); - statfile_mean_.open("stats/stats_simd_mean.csv",std::ofstream::trunc); - if (is_indiv_) - statfile_best_<<"Generation"<<","<<"fitness"<<","<<"metabolic_error"<<","<< - "amount_of_dna"<<","<<"nb_coding_rnas"<<","<<"nb_non_coding_rnas"<<","<< - "nb_functional_genes"<<","<<"nb_non_functional_genes"<<","<<"nb_mut" - <<","<<"nb_switch"<<","<<"nb_indels"<<","<<"nb_rear"<<","<<"nb_dupl"<<","<< - "nb_del"<<","<<"nb_trans"<<","<<"nb_inv"<<","<<"dupl_rate"<<","<<"del_rate" - <<","<<"trans_rate"<<","<<"inv_rate" - <<std::endl; - else - statfile_mean_<<"Generation"<<","<<"fitness"<<","<<"metabolic_error"<<","<< - "amount_of_dna"<<","<<"nb_coding_rnas"<<","<<"nb_non_coding_rnas"<<","<< - "nb_functional_genes"<<","<<"nb_non_functional_genes"<<","<<"nb_mut" - <<","<<"nb_switch"<<","<<"nb_indels"<<","<<"nb_rear"<<","<<"nb_dupl"<<","<< - "nb_del"<<","<<"nb_trans"<<","<<"nb_inv"<<","<<"dupl_rate"<<","<<"del_rate" - <<","<<"trans_rate"<<","<<"inv_rate" - <<std::endl; + if (generation_==0) { + if (is_indiv_) + statfile_best_.open("stats/stats_simd_best.csv",std::ofstream::trunc); + else + statfile_mean_.open("stats/stats_simd_mean.csv",std::ofstream::trunc); + + if (is_indiv_) { + statfile_best_ << "Generation" << "," << "fitness" << "," << "metabolic_error" << "," << + "amount_of_dna" << "," << "nb_coding_rnas" << "," << "nb_non_coding_rnas" << "," << + "nb_functional_genes" << "," << "nb_non_functional_genes" << "," << "nb_mut" + << "," << "nb_switch" << "," << "nb_indels" << "," << "nb_rear" << "," << "nb_dupl" << "," << + "nb_del" << "," << "nb_trans" << "," << "nb_inv" << "," << "dupl_rate" << "," << "del_rate" + << "," << "trans_rate" << "," << "inv_rate" + << std::endl; + statfile_best_.flush(); + } else { + statfile_mean_ << "Generation" << "," << "fitness" << "," << "metabolic_error" << "," << + "amount_of_dna" << "," << "nb_coding_rnas" << "," << "nb_non_coding_rnas" << "," << + "nb_functional_genes" << "," << "nb_non_functional_genes" << "," << "nb_mut" + << "," << "nb_switch" << "," << "nb_indels" << "," << "nb_rear" << "," << "nb_dupl" << "," << + "nb_del" << "," << "nb_trans" << "," << "nb_inv" << "," << "dupl_rate" << "," << "del_rate" + << "," << "trans_rate" << "," << "inv_rate" + << std::endl; + statfile_mean_.flush(); + } } else { - statfile_best_.open("stats/stats_simd_best.csv",std::ofstream::app); - statfile_mean_.open("stats/stats_simd_mean.csv",std::ofstream::app); - } + printf("Resume without rheader\n"); + std::ifstream tmp_mean; + std::ifstream tmp_best; + + if (is_indiv_) { + tmp_best.open("stats/stats_simd_best.csv",std::ifstream::in); + statfile_best_.open("stats/stats_simd_best.csv.tmp", std::ofstream::trunc); + } else { + tmp_mean.open("stats/stats_simd_mean.csv",std::ifstream::in); + statfile_mean_.open("stats/stats_simd_mean.csv.tmp", std::ofstream::trunc); + } + + std::string str; + for (int i = 0; i <= generation_; i++) { + if (is_indiv_) { + std::getline(tmp_best, str); + statfile_best_ << str << std::endl; + } else { + std::getline(tmp_mean, str); + statfile_mean_ << str << std::endl; + } + } + + if (is_indiv_) { + statfile_best_.flush(); + statfile_best_.close(); + } else { + statfile_mean_.flush(); + statfile_mean_.close(); + } + if (is_indiv_) { + statfile_best_.open("stats/stats_simd_best.csv", std::ofstream::trunc); + tmp_best.close(); + tmp_best.open("stats/stats_simd_best.csv.tmp", std::ifstream::in); + tmp_best.seekg(0, std::ios::beg); + } else { + statfile_mean_.open("stats/stats_simd_mean.csv", std::ofstream::trunc); + tmp_mean.close(); + tmp_mean.open("stats/stats_simd_mean.csv.tmp", std::ifstream::in); + tmp_mean.seekg(0, std::ios::beg); + } + + for (int i = 0; i <= generation_; i++) { + if (is_indiv_) { + std::getline(tmp_best, str); + statfile_best_ << str << std::endl; + } else { + std::getline(tmp_mean, str); + statfile_mean_ << str << std::endl; + } + } + + tmp_best.close(); + tmp_mean.close(); + } } void Stats_SIMD::compute_best() { @@ -219,7 +281,35 @@ void Stats_SIMD::write_average() { nb_del_<<","<<nb_trans_<<","<<nb_inv_<<","<<dupl_rate_<<","<<del_rate_ <<","<<trans_rate_<<","<<inv_rate_ <<std::endl; + statfile_mean_.flush(); } } + void Stats_SIMD::reinit(int64_t generation) { + generation_ = generation; + + pop_size_ = 0; + + fitness_ = 0; + metabolic_error_ = 0; + + amount_of_dna_ = 0; + nb_coding_rnas_ = 0; + nb_non_coding_rnas_ = 0; + + nb_functional_genes_ = 0; + nb_non_functional_genes_ = 0; + + nb_mut_ = 0; + nb_rear_ = 0; + nb_switch_ = 0; + nb_indels_ = 0; + nb_dupl_ = 0; + nb_del_ = 0; + nb_trans_ = 0; + nb_inv_ = 0; + + is_computed_ = false; + } + } diff --git a/src/libaevol/Stats_SIMD.h b/src/libaevol/Stats_SIMD.h index 68208ef2edf666a756a1a5358716e5501ed2bf10..4adb0752768771f9cada24730bc7fc5581449709 100644 --- a/src/libaevol/Stats_SIMD.h +++ b/src/libaevol/Stats_SIMD.h @@ -8,11 +8,12 @@ #include <cstdint> #include <fstream> - -#include "SIMD_Individual.h" +#include <limits> namespace aevol { + class SIMD_Individual; + class Stats_SIMD { public: Stats_SIMD(SIMD_Individual* simd_individual, int64_t generation, @@ -34,6 +35,8 @@ class Stats_SIMD { void write_best(); void write_average(); + void reinit(int64_t generation); + bool is_indiv() { return is_indiv_; } @@ -86,6 +89,7 @@ class Stats_SIMD { std::ofstream statfile_best_; std::ofstream statfile_mean_; + }; }