void addOldToTree(AllData& alldata, std::ostringstream& old_filter) { vector<string> pathes; if( !alldata.params.dispersion.filter_file_name.empty()) { string filter_file_name = alldata.params.dispersion.filter_file_name; ifstream b_file(filter_file_name.c_str()); if (b_file.good()) { string line; while (std::getline(b_file, line)) { trimString(line); if (line.substr(0, 1) != "#" && line.find_first_of("--") != line.npos) { pathes.push_back(line); } else { if (line != STARTTEXT && line != STARTTEXT2 && line != STARTTEXT3) old_filter << line << endl; } } b_file.close(); } while (!pathes.empty()) { string path = pathes.back(); string tmp_path = path; pathes.pop_back(); int pos = path.find_last_of("--") - 1; string tail = path.substr(pos, path.length() - pos); path = path.substr(0, pos); trimString(path); trimString(tail); string func = path.substr(path.find_last_of(";") + 1, path.length() - (path.find_last_of(";") + 1)); if (tail.find("C") != tail.npos && tail.find(" 0 ") != tail.npos) { parsePath(alldata, path, FILTEROUT); } else { old_filter << tmp_path << endl; } } } }
void ModNeatExperiment7::processGroup(shared_ptr<NEAT::GeneticGeneration> generation) { // FORK a number of processes equal to the number of individuals we are going to process // getGroupSize is the size of ::getGroupCapacity const int groupSize = getGroupSize(); pid_t* pids = new pid_t[groupSize]; for (int i = 0; i < groupSize; ++i) { // increment individual count if the current individual number is smaller than the population size. // if it is not, then it is the first individual of the next population. // this is an ugly workaround since it is not easily possible to get the current individual or generation // from HyperNEAT itself, thus it needs to be managed by ourselves. if(currIndividual == popSize) { // reset individual and increase generation number currIndividual = 1; currGeneration++; } else { // this is just a next individual in the current generation currIndividual++; } screen << "processing generation: " << currGeneration << ", individual: " << currIndividual << ", on thread: " << i << endl; shared_ptr<NEAT::GeneticIndividual> individual = group[i]; // avoid 0 fitness if (individual->getFitness() <= 0) individual->setFitness(0.000001); // spawn child process after 1 sec (webotsrc test) usleep(1000 * 1000); pids[i] = fork(); // child code if (pids[i] == 0) { // code only executed by child process // evaluate individual and get fitness double fitness = processEvaluation(individual, NULL, i); // write fitness to a named textfile stringstream ss; ss << i; string temp_str = "simulation" + ss.str() + ".txt"; // write fitness to textfile ofstream a_file (temp_str.c_str()); a_file << fitness; a_file.close(); exit(0); // exit child process successfully } } // WAIT for each individual process and only when all have exited, continue main process for (int i = 0; i < groupSize; ++i) { int status; while (-1 == waitpid(pids[i], &status, 0)) { if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { screen << "Process " << i << " (pid " << pids[i] << ") failed" << endl; exit(1); } } } // READ back the fitness values and for (int i = 0; i < groupSize; ++i) { // get individual to reward fitness too shared_ptr<NEAT::GeneticIndividual> individual = group[i]; // Opens for reading the file stringstream ss; ss << i; string temp_str = "simulation" + ss.str() + ".txt"; ifstream b_file (temp_str.c_str()); // Reads one string from the file string fitness; b_file >> fitness; b_file.close(); // reward this individual its fitness individual->reward(abs(::atof(fitness.c_str())) + 0.000001); } }
int main(int argc, char *argv[]) { float *a, *b, *c; gmactime_t s, t; ecl::error err; assert(ecl::compileSource(kernel) == eclSuccess); float * orig = (float *) malloc(vecSize * sizeof(float)); std::ifstream o_file(VECTORC); o_file.read((char *)orig, vecSize * sizeof(float)); o_file.close(); getTime(&s); // Alloc & init input data assert(ecl::malloc((void **)&a, vecSize * sizeof(float)) == eclSuccess); assert(ecl::malloc((void **)&b, vecSize * sizeof(float)) == eclSuccess); assert(ecl::malloc((void **)&c, vecSize * sizeof(float)) == eclSuccess); getTime(&t); printTime(&s, &t, "Alloc: ", "\n"); std::ifstream a_file(VECTORA); std::ifstream b_file(VECTORB); getTime(&s); a_file.read((char *)a, vecSize * sizeof(float)); a_file.close(); b_file.read((char *)b, vecSize * sizeof(float)); b_file.close(); getTime(&t); printTime(&s, &t, "Init: ", "\n"); // Call the kernel getTime(&s); ecl::config localSize (blockSize); ecl::config globalSize (vecSize / blockSize); if(vecSize % blockSize) globalSize.x++; globalSize.x *= localSize.x; ecl::kernel kernel("vecAdd", err); assert(err == eclSuccess); #ifndef __GXX_EXPERIMENTAL_CXX0X__ err = kernel.setArg(0, c); assert(err == eclSuccess); err = kernel.setArg(1, a); assert(err == eclSuccess); err = kernel.setArg(2, b); assert(err == eclSuccess); err = kernel.setArg(3, vecSize); assert(err == eclSuccess); err = kernel.callNDRange(globalSize, localSize); assert(err == eclSuccess); #else assert(kernel(c, a, b, vecSize)(globalSize, localSize) == eclSuccess); #endif getTime(&t); printTime(&s, &t, "Run: ", "\n"); getTime(&s); float error = 0.f; for(unsigned i = 0; i < vecSize; i++) { error += orig[i] - (c[i]); } getTime(&t); printTime(&s, &t, "Check: ", "\n"); getTime(&s); std::ofstream c_file("vectorC_shared"); c_file.write((char *)c, vecSize * sizeof(float)); c_file.close(); getTime(&t); printTime(&s, &t, "Write: ", "\n"); getTime(&s); ecl::free(a); ecl::free(b); ecl::free(c); getTime(&t); printTime(&s, &t, "Free: ", "\n"); return error != 0; }