/** * @brief DataIO::open_for_reading load frame from file into dataset * @return A Dataset object ready for use */ Dataset DataIO::open_for_reading(){ file.open(filename, std::ios::in | std::ios::binary); // read header uint8_t header[1+1+1+8+8]; // magic cookie, proto version, data mask, number of frames, frame-length file.read((char*)&header, sizeof(header)); if(header[0] != 170){ std::cout << "Tried to read file, but it appears to be corrupted! (no magic cookie found)" << std::endl; exit(EXIT_FAILURE); } if(header[1] != 0){ std::cout << "Tried to read file, but the file format version does not match the version this parser understands!" << std::endl; exit(EXIT_FAILURE); } uint8_t datamask = header[2]; frames_available = *((uint64_t*)&header[3]); std::cout << "number of frames: " << frames_available << std::endl; bodies = *((uint64_t*)&header[3+8]); std::cout << "number of bodies: " << bodies << std::endl; dataset = Dataset(bodies); for(unsigned long i = 0; i < bodies; i++){ dataset.masses[i] = 1.0; dataset.velocities[i] = number3(0.0, 0.0, 0.0); dataset.positions[i] = number3(0.0, 0.0, 0.0); } read_buffer = (float*)malloc(sizeof(float)*3*bodies); return dataset; }
void loadDataset(string location){ ToolState &state = *ToolState::getInstance(); if(state.classes != 0) state.classes->close(); state.classes = GLUI_Master.create_glui( "classes", 0, 800, 315 ); state.current_db = Dataset(location); vector<Category> * cats = state.current_db.getCategories(); size_t c = 0; // classes->add_column(true); GLUI_Panel * pan = state.classes->add_panel("categories"); vector<int*> live_vars; for(size_t i = 0; i < cats->size(); ++i){ stringstream name; name << i << " " << cats->at(i).get_name(); state.classes->add_checkbox_to_panel( pan, name.str().c_str(), cats->at(i).enabledLiveVar(), 1, (GLUI_Update_CB) viewDataset); live_vars.push_back(cats->at(i).enabledLiveVar()); c++; if(c%34==0) state.classes->add_column_to_panel(pan, false); } }
int main(void){ Dataset ds = Dataset(2); DataIO io = DataIO("test.dat"); io.open_for_writing(ds); ds.initialize_random(Dataset::RandomConfiguration::SOLAR_SYSTEM); number initial_energy = ds.compute_energy(GRAVITATIONAL_CONSTANT); std::cout << "initial energy in system: " << initial_energy << std::endl; Renderer renderer = Renderer(ds); StatLogger<number> energylogger = StatLogger<number>("energy-conservation.dat"); number dt = 153.0; //dt = 0.000001; Integrator::Integrator *integrator = new Integrator::EulerCromer(ds, dt, DISTANCE_SMOOTHING_FACTOR, GRAVITATIONAL_CONSTANT); unsigned long frame = 0; while(true){ if(frame > 100000000) break; integrator->swap(); integrator->step(); if(frame % 16000 == 0){ number3 center_of_mass = ds.compute_center_of_mass(); number energy = ds.compute_energy(GRAVITATIONAL_CONSTANT); //std::cout << "energy drift: initial_energy - energy = " << initial_energy << " - " << energy << " = " << (initial_energy - energy) << std::endl; energylogger.append(energy); //renderer.set_center_of_mass(center_of_mass.x, center_of_mass.y, center_of_mass.z); //renderer.push_tails(); } if(frame % 200000 == 0){ if(renderer.render()) break; io.append(); } frame++; } energylogger.finalize(); renderer.quit(); io.finalize(); }
HLTDatasets::HLTDatasets(const std::vector<TString>& triggerNames, const Char_t* datasetDefinitionFile, Bool_t preferEmulatedTriggers, TString emulationPrefix) : datasetsConfig("", &timer) { //........................................................................... // Parse input file and mark all the triggers that belong to datasets const UInt_t numTriggers = triggerNames.size(); std::vector<Bool_t> notInDataset (numTriggers, kTRUE); if (datasetDefinitionFile && datasetDefinitionFile[0]) { std::ifstream input(datasetDefinitionFile); if (!input.good()) std::cerr << "ERROR : Cannot open dataset definitions file " << datasetDefinitionFile << std::endl; else { scenarioName = gSystem->BaseName(datasetDefinitionFile); Int_t iExtension = scenarioName.Last('.'); if (iExtension != kNPOS) scenarioName = scenarioName(0, iExtension); // Parse dataset definition blocks TString line; int lineNumber = 0; while (input.good() && !input.eof()) { ++lineNumber; // Get a line, strip comments and whitespace line.ReadLine(input); strip(line); const Int_t commentIndex = line.First('#'); if (commentIndex != kNPOS) {line = line(0, commentIndex); strip(line);} if (line.Length() < 1) continue; // If the line ends with a semi-colon, it is the start of a dataset definition block if (line.EndsWith(":")) { TString name = line(0, line.Length()-1); strip(name); if (indexOf(datasetsConfig, name) >= 0) std::cerr << "WARNING : One or more datasets have the same name '" << name << "'. Are you sure this is acceptable?" << std::endl; datasetsConfig.push_back(Dataset(name)); } // Otherwise it is a trigger in the current dataset else { if (datasetsConfig.empty()) std::cerr << "ERROR : Skipping unexpected line before declaration of first dataset : " << std::endl << " [" << std::setw(3) << lineNumber << "] " << line << std::endl; else { const Int_t triggerIndex = indexOf(triggerNames, line); // Also check in case there is an emulated version -- don't use both! TString emulationName = emulationPrefix + line; const Int_t emulationIndex= indexOf(triggerNames, emulationName); if (triggerIndex < 0 && emulationIndex < 0) { std::cerr << "WARNING : Skipping un-available trigger : " << std::endl << " [" << std::setw(3) << lineNumber << "] " << line << std::endl; } else { if (triggerIndex < 0 || (emulationIndex >= 0 && preferEmulatedTriggers)) datasetsConfig.back().push_back(Trigger(emulationName, emulationIndex)); else datasetsConfig.back().push_back(Trigger(line, triggerIndex)); if (triggerIndex >= 0) notInDataset[triggerIndex] = kFALSE; if (emulationIndex >= 0) notInDataset[emulationIndex] = kFALSE; } } } } // end loop over lines in file } } // If there are no datasets defined, there's no point in marking all triggers as new // since the extra diagnostics will show nothing else { scenarioName = "pertrigger"; for (UInt_t iTrigger = 0; iTrigger < numTriggers; ++iTrigger) { // Prefer either the original or emulated version (but not both) Bool_t isEmulated = triggerNames[iTrigger].BeginsWith(emulationPrefix); if ( preferEmulatedTriggers && !isEmulated) { if (indexOf(triggerNames, emulationPrefix + triggerNames[iTrigger]) >= 0) continue; } else if (!preferEmulatedTriggers && isEmulated) { Int_t prefix = emulationPrefix.Length(); if (indexOf(triggerNames, triggerNames[iTrigger](prefix, triggerNames[iTrigger].Length()-prefix)) >= 0) continue; } datasetsConfig.push_back(Dataset(triggerNames[iTrigger])); datasetsConfig.back().push_back(Trigger(triggerNames[iTrigger], iTrigger)); notInDataset[iTrigger] = kFALSE; } // end loop over triggers } //........................................................................... const UInt_t numDatasets = datasetsConfig.size(); std::clog << "-- HLTDatasets -----------------------------------------------------------" << std::endl; std::clog << "There are " << numDatasets << " datasets:" << std::endl; for (UInt_t iSet = 0; iSet < numDatasets; ++iSet) std::clog << " " << std::left << std::setw(40) << datasetsConfig[iSet].name << " (x " << std::right << std::setw(2) << datasetsConfig[iSet].size() << " triggers)" << std::endl; //........................................................................... // Register all triggers _not_ in any dataset as new triggers UInt_t numNewTriggers = 0; for (UInt_t iTrigger = 0; iTrigger < numTriggers; ++iTrigger) { if (notInDataset[iTrigger] && (!triggerNames[iTrigger].Contains("AlCa_Ecal" ,TString::kIgnoreCase) && !triggerNames[iTrigger].Contains("AlCa_IsoTrack" ,TString::kIgnoreCase) && !triggerNames[iTrigger].Contains("AlCa_HcalPhiSym",TString::kIgnoreCase) && !triggerNames[iTrigger].Contains("AlCa_RPC" ,TString::kIgnoreCase))) { datasetsConfig.push_back(Dataset(triggerNames[iTrigger], kTRUE)); datasetsConfig.back().push_back(Trigger(triggerNames[iTrigger], iTrigger)); ++numNewTriggers; } } // end loop over triggers if (numNewTriggers) { std::clog << "and " << numNewTriggers << " new triggers:" << std::endl; for (UInt_t iTrigger = 0; iTrigger < numNewTriggers; ++iTrigger) std::clog << " + " << datasetsConfig[numDatasets + iTrigger].name << std::endl; } else std::clog << "There are no new triggers." << std::endl; std::clog << "==========================================================================" << std::endl << std::endl; datasetsConfig.setup(); // Make sure to allocate space at the end after everything is registered }
// [[Rcpp::export]] Rcpp::List run(SEXP dataset, SEXP model_control, SEXP sgd_control) { boost::timer ti; Rcpp::List Dataset(dataset); Rcpp::List Model_control(model_control); Rcpp::List Sgd_control(sgd_control); if (Rcpp::as<bool>(Sgd_control["verbose"])) { Rcpp::Rcout << "Converting arguments from R to C++ types..." << std::endl; } // Construct data. data_set data(Dataset["bigmat"], Rcpp::as<mat>(Dataset["X"]), Rcpp::as<mat>(Dataset["Y"]), Rcpp::as<unsigned>(Sgd_control["npasses"]), Rcpp::as<bool>(Dataset["big"]), Rcpp::as<bool>(Sgd_control["shuffle"])); // Construct model. std::string model_name = Rcpp::as<std::string>(Model_control["name"]); if (model_name == "cox") { cox_model model(Model_control); // Construct stochastic gradient method. std::string sgd_name = Rcpp::as<std::string>(Sgd_control["method"]); if (sgd_name == "sgd" || sgd_name == "asgd") { explicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "implicit" || sgd_name == "ai-sgd") { implicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "momentum") { momentum_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "nesterov") { nesterov_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else { Rcpp::Rcout << "error: stochastic gradient method not implemented" << std::endl; return Rcpp::List(); } } else if (model_name == "lm" || model_name == "glm") { glm_model model(Model_control); // Construct stochastic gradient method. std::string sgd_name = Rcpp::as<std::string>(Sgd_control["method"]); if (sgd_name == "sgd" || sgd_name == "asgd") { explicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "implicit" || sgd_name == "ai-sgd") { implicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "momentum") { momentum_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "nesterov") { nesterov_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else { Rcpp::Rcout << "error: stochastic gradient method not implemented" << std::endl; return Rcpp::List(); } } else if (model_name == "gmm") { gmm_model model(Model_control); // Construct stochastic gradient method. std::string sgd_name = Rcpp::as<std::string>(Sgd_control["method"]); if (sgd_name == "sgd" || sgd_name == "asgd") { explicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "implicit" || sgd_name == "ai-sgd") { implicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "momentum") { momentum_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "nesterov") { nesterov_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else { Rcpp::Rcout << "error: stochastic gradient method not implemented" << std::endl; return Rcpp::List(); } } else if (model_name == "m") { m_model model(Model_control); // Construct stochastic gradient method. std::string sgd_name = Rcpp::as<std::string>(Sgd_control["method"]); if (sgd_name == "sgd" || sgd_name == "asgd") { explicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "implicit" || sgd_name == "ai-sgd") { implicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "momentum") { momentum_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "nesterov") { nesterov_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else { Rcpp::Rcout << "error: stochastic gradient method not implemented" << std::endl; return Rcpp::List(); } } else { Rcpp::Rcout << "error: model not implemented" << std::endl; return Rcpp::List(); } #if 0 // TODO The above duplicates code within the if-else statement. // Construct stochastic gradient method. std::string sgd_name = Rcpp::as<std::string>(Sgd_control["method"]); if (sgd_name == "sgd" || sgd_name == "asgd") { explicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "implicit" || sgd_name == "ai-sgd") { implicit_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "momentum") { momentum_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else if (sgd_name == "nesterov") { nesterov_sgd sgd(Sgd_control, data.n_samples, ti); return run(data, model, sgd); } else { Rcpp::Rcout << "error: stochastic gradient method not implemented" << std::endl; return Rcpp::List(); } #endif }