static void Problem1(void) { Vector y(P1_NEQ), yp(P1_NEQ); Real reltol=RTOL, abstol=ATOL, t, tout, hu; int flag, iout, qu; y[0] = TWO; y[1] = ZERO; Problem1System sys1; // Use the explicit function to initialize derivatives for the implicit one. if (odeType == CPodes::ImplicitODE) sys1.explicitODE(P1_T0, y, yp); CPodes cpode(odeType, CPodes::Adams, CPodes::Functional); /* flag = cpode.setInitStep(4.0e-9);*/ flag = cpode.init(sys1, P1_T0, y, yp, CPodes::ScalarScalar, reltol, &abstol); printf("\n t x xdot qu hu \n"); for(iout=1, tout=P1_T1; iout <= P1_NOUT; iout++, tout += P1_DTOUT) { flag = cpode.step(tout, &t, y, yp, CPodes::Normal); if (flag != CPodes::Success) break; flag = cpode.getLastOrder(&qu); flag = cpode.getLastStep(&hu); printf("%10.5f %12.5le %12.5le %2d %6.4le\n", t, y[0], y[1], qu, hu); } PrintFinalStats(cpode); return; }
static void Problem2(void) { Vector y(P2_NEQ), yp(P2_NEQ); Real reltol=RTOL, abstol=ATOL, t, tout, erm, hu; int flag, iout, qu; y = 0; y[0] = ONE; Problem2System sys2; // Use the explicit function to initialize derivatives for the implicit one. if (odeType == CPodes::ImplicitODE) sys2.explicitODE(P2_T0, y, yp); CPodes cpode(odeType, CPodes::Adams, CPodes::Functional); /* flag = cpode.setInitStep(2.0e-9);*/ flag = cpode.init(sys2, P2_T0, y, yp, CPodes::ScalarScalar, reltol, &abstol); printf("\n t max.err qu hu \n"); for(iout=1, tout=P2_T1; iout <= P2_NOUT; iout++, tout*=P2_TOUT_MULT) { flag = cpode.step(tout, &t, y, yp, CPodes::Normal); if (flag != CPodes::Success) break; erm = MaxError(y, t); flag = cpode.getLastOrder(&qu); flag = cpode.getLastStep(&hu); printf("%10.3f %12.4le %2d %12.4le\n", t, erm, qu, hu); } PrintFinalStats(cpode); return; }
// main program that runs all the cppad_ipopt speed tests int main(void) { using std::printf; const char* name; double seconds; size_t count; name = "simple_retape_yes"; seconds = ode_speed(name, count); printf("ode %20s: seconds = %5.2f: eval_r_count = %d\n", name, seconds, int(count) ); name = "simple_retape_no"; seconds = ode_speed(name, count); printf("ode %20s: seconds = %5.2f: eval_r_count = %d\n", name, seconds, int(count) ); name = "fast_retape_yes"; seconds = ode_speed(name, count); printf("ode %20s: seconds = %5.2f: eval_r_count = %d\n", name, seconds, int(count) ); name = "fast_retape_no"; seconds = ode_speed(name, count); printf("ode %20s: seconds = %5.2f: eval_r_count = %d\n", name, seconds, int(count) ); return 0; }
/*! Evaluate the objective fucntion f(x). \param[in] n is the dimension of the argument space for f(x); i.e., must be equal \c n_. \param[in] x is a vector of size \c n containing the point at which to evaluate the function f(x). \param[in] new_x is false if the previous call to any one of the \ref Evaluation_Methods used the same value for \c x. \param[out] obj_value is the value of the objective f(x) at this value of \c x. \return The return value is always true; see \ref Evaluation_Methods. \par Efficiency This routine could be more efficient (for certain when when L[k] > 1 and retape[k] is true) if the users also provided a version of the function <tt>fg_info->eval_r(k, u)</tt> where \c u was of type \c NumberVector. */ bool cppad_ipopt_nlp::eval_f( Index n, const Number* x, bool new_x, Number& obj_value ) { CPPAD_ASSERT_UNKNOWN(size_t(n) == n_ ); size_t iobj, j, k, ell; // initialize summation obj_value = 0.; // update tape_ok_ flag for(k = 0; k < K_; k++) { if( retape_[k] && (new_x || L_[k] > 1) ) tape_ok_[k] = false; } for(k = 0; k < K_; k++) for(ell = 0; ell < L_[k]; ell++) { fg_info_->index(k, ell, I_, J_); for(iobj = 0; iobj < p_[k]; iobj++) if( I_[iobj] == 0 ) { if( ! tape_ok_[k] ) { // Record r_k for value of u corresponding to x fun_record( fg_info_ , // inputs k , p_ , q_ , n_ , x , J_ , r_fun_ // output ); tape_ok_[k] = ! (retape_[k] || L_[k] > 1); } NumberVector u(q_[k]); NumberVector r(p_[k]); for(j = 0; j < q_[k]; j++) { CPPAD_ASSERT_UNKNOWN( J_[j] < n_ ); u[j] = x[ J_[j] ]; } r = r_fun_[k].Forward(0, u); obj_value += r[iobj]; } } # if CPPAD_IPOPT_NLP_TRACE using std::printf; for(j = 0; j < n_; j++) printf("cppad_ipopt_nlp::eval_f::x[%d] = %20.14g\n", j, x[j]); printf("cppad_ipopt_nlp::eval_f::obj_value = %20.14g\n", obj_value); # endif # ifndef NDEBUG CPPAD_ASSERT_KNOWN( (-infinity_ < obj_value) && (obj_value < infinity_), "cppad_ipopt_nlp::eval_f:: objective value is not finite" ); # endif return true; }
int main(void) { printf("Van der Pol\n\n"); Problem1(); printf("\n\ny' = A*y\n\n"); Problem2(); return(0); }
void HashTable<P>::dump_stats() { using std::printf; printf("LOAD: %f SIZE %u: BUCKETS: %u\n", (double)size_ / (double)table_size_, size_, table_size_); Vector<unsigned> tally; Vector<unsigned> tally2; Vector<const Key *> uniq; Vector<std::pair<unsigned long, unsigned> > gather; for (Node * * i = table_; i != table_end_; ++i) { unsigned c = 0; Node * n = *i; gather.clear(); uniq.clear(); //printf("---\n"); while (n) { ++c; unsigned j = 0; const Key & k = parms_.key(n->data); while (j != uniq.size() && !(*uniq[j] == k)) ++j; if (j == uniq.size()) { uniq.push_back(&k); j = 0; unsigned h = parms_.hash(k); while (j != gather.size() && gather[j].first != h) ++j; if (j == gather.size()) gather.resize(j+1); gather[j].first = h; gather[j].second++; } n = n->next; //printf(" %u %u\n", h, j); } if (tally.size() <= c) tally.resize(c+1); tally[c]++; for (unsigned j = 0; j != gather.size(); ++j) { unsigned c = gather[j].second; if (tally2.size() <= c) tally2.resize(c+1); tally2[c]++; } if (c > 16) { n = *i; while (n) { const Key & k = parms_.key(n->data); printf("?? %lu %p %p\n", parms_.hash(k), k.prod, k.str); n = n->next; } printf("---\n"); } } for (unsigned i = 0; i != tally.size(); ++i) printf(" -- %u: %u (%f)\n", i, tally[i], (double)tally[i]/(double)table_size_); for (unsigned i = 0; i != tally2.size(); ++i) printf(" == %u: %u (%f)\n", i, tally2[i], (double)tally2[i]/(double)table_size_); }
int main() { int width = 32; for (int i = 2 ; i < width ; i ++) { printf(" %d or %d(chain[%d], in[%d], chain[%d);\n", i, i, i, i, i-1); } }
void aSsErT(bool b, const char *s, int i) { if (b) { printf("Error " __FILE__ "(%d): %s (failed)\n", i, s); if (testStatus >= 0 && testStatus <= 100) ++testStatus; } }
int main() { Singleton *p1 = Singleton::getInstance(); Singleton *p2 = Singleton::getInstance(); printf("address of p1: %p\naddress of p2: %p\n", p1, p2); return 0; }
void print_xmltree(xmlNodePtr xroot, int level) { print_indent(level); printf("%s ", xroot->name); if (xroot->properties) { printf("["); for (xmlAttrPtr a = xroot->properties; a != 0; a = a->next) { printf("%s=%s", a->name, xmlNodeGetContent(a->children)); // or one can write xmlGetProp(xroot, a->name) if (a->next) printf(", "); } printf("]"); } if (xroot->children && xroot->children->next == 0 && xroot->children->type == XML_TEXT_NODE) { xmlChar *sz = xmlNodeGetContent(xroot->children); printf("{%s}", sz); xmlFree(sz); } printf("\n"); for (xmlNodePtr c = xroot->children; c != 0; c = c->next) { if (c->type == XML_ELEMENT_NODE) print_xmltree(c, level + 1); } }
int main(int argc, char *argv[]) { for (unsigned i = 0; i < 0x8000; ++i) { cache.clear(); printf("\rguessing ver(4, 1, %5d) = ", i); fflush(stdout); unsigned guess = ver(4, 1, i); printf("%5d", guess); if (guess == 6) { printf("\nFound solution!\n"); break; } } return 0; }
int main() { /* exceptions dont work right now, I dont know why */ #if 0 try { printf("throwing\n"); throw 1; } catch (int z) { printf("threw %d (int)\n", z); } catch (...) { printf("general purpose catch\n"); } #endif int *foo = new int; printf("allocated memory with new, address %p\n", foo); delete foo; }
void tiny_hash<H,INIT_SZ,MAX_SZ>::dump_stats() { if (hash) { hash->dump_stats(); } else { unsigned c = 0; for (Node * n = first; n; n = n->next) { ++c; } printf("TINY HASH: %u\n", c); } }
double ComputeMagnitude(DistanceType dtype, double dim) { switch (dtype) { case DistanceDot: return dim * dim; case DistanceMin: return dim; default: printf("[ComputeMagnitude] No case value found!\n"); return 0.0; } }
void botch(const char *what, double wanted, double got) { printf("%s: expected %g, got %g, diff = %.2g\n", what, wanted, got, wanted-got); rc = 1; }
bool testEquals( int actualValue, int expectedValue, const char * lineText, int lineNumber ) { if( actualValue == expectedValue ) return true; printf( "%s\nat line %i - " "Actual: %i - Expected: %i\n\n", lineText, lineNumber, actualValue, expectedValue ); return false; }
int main(int argc, const char *argv[]) { if (argc != 2) { printf("Missing argument: script file name.\n"); return 1; } lua_State *L = luaL_newstate(); luaL_openlibs(L); if (luaL_loadfile(L, argv[1]) != 0) { printf("Could not load script \"%s\"!\n", argv[1]); return 1; } lua_newtable(L); for (int i = 1; i <= 5; i++) { lua_pushnumber(L, i); lua_pushnumber(L, i*2); lua_rawset(L, -3); } lua_setglobal(L, "testtable"); (new Test())->push(L); lua_setglobal(L, "start"); if (lua_pcall(L, 0, 0, 0) != 0) { const char *msg = lua_tostring(L, -1); printf( "Script execution failed!\n%s\n", msg ? msg : "(no error message)" ); return 1; } lua_close(L); return 0; }
int VocabTree::NormalizeDatabase(int start_index, int num_db_images) { std::vector<float> mags; mags.resize(num_db_images); m_root->ComputeDatabaseMagnitudes( m_branch_factor, m_distance_type, start_index, mags); for (int i = 0; i < num_db_images; i++) { printf("[NormalizeDatabase] Vector %d has magnitude %0.3f\n", start_index + i, mags[i]); } return m_root->NormalizeDatabase(m_branch_factor, start_index, mags); }
bool testEquals( bool actualValue, bool expectedValue, const char * lineText, int lineNumber ) { if( actualValue == expectedValue ) return true; printf( "%s\nat line %i - " "Actual: %s - Expected: %s\n\n", lineText, lineNumber, actualValue ? "true":"false", expectedValue ? "true":"false" ); return false; }
bool testEquals( char actualValue, char expectedValue, const char * lineText, int lineNumber ) { if( actualValue == expectedValue ) return true; printf( "%s\nat line %i - " "Actual: '%c' (0x%X) - Expected: '%c' (0x%X)\n\n", lineText, lineNumber, actualValue, (int) actualValue, expectedValue, (int) expectedValue ); return false; }
int main(int argc, char* argv[]) { if( argc < 2 ){ printf("%s", usage); return 1; } int len = atoi(argv[1] ); if( len < 3 || len > 12 ){ printf("%s", usage); return 2; } int fact[16]; fact[0] = 1; for(int i = 1; i<len+1; ++i) fact[i] = fact[i-1]*i; unsigned n_cpu = thread::hardware_concurrency(); Fannkuchredux::R r= { 0, 0}; const unsigned max_cpu_limit = 4; Task parts[max_cpu_limit]; unsigned n = min(n_cpu, max_cpu_limit); thread_group tg; int index = 0; int index_max = fact[len]; int index_step = (index_max + n-1)/n; for(unsigned i = 0; i<n; ++i, index += index_step){ Task& p = parts[i]; p.init(fact, len, index, index + index_step); tg.create_thread(ref(p)); } tg.join_all(); for(unsigned i = 0; i<n; ++i){ Task const& p = parts[i]; r.maxflips = max( p.r.maxflips, r.maxflips ); r.checksum += p.r.checksum; } printf("%d\nPfannkuchen(%d) = %d\n", r.checksum, len, r.maxflips); return 0; }
double VocabTree::AddImageToDatabase( int index, int ndescriptors, uint8_t *descriptors, unsigned long *ids) { map<unsigned long, float> node_scores; unsigned long off = 0; printf("[AddImageToDatabase] Adding image with %d features...\n", ndescriptors); fflush(stdout); for (int i = 0; i < ndescriptors; i++) { unsigned long id = m_root->PushAndScoreFeature( descriptors + off, index, m_branch_factor, m_dim, true /*add*/, node_scores); if (ids != NULL) { ids[i] = id; } off += m_dim; fflush(stdout); } double mag = m_root->ComputeDatabaseVectorMagnitude( m_branch_factor, m_distance_type, node_scores); m_database_images++; switch (m_distance_type) { case DistanceDot: return sqrt(mag); case DistanceMin: return mag; default: printf("[VocabTree::AddImageToDatabase] No case value found!\n"); return 0.0; } }
int main() { int width =32; for(int i=1; i<width; i++){ printf("alu1 a%d(out[%d],carryout[%d], A[%d], B[%d], carryout[%d], control);\n",i, i, i, i, i, i-1); //alu1(out[0], carryout[0], A[0],B[0], control[0], control); } // for(int i=1; i<width; i++){ // printf("out[%d],",i); //} }
bool TesterDouble::testEquals( double actualValue, double expectedValue, const char * lineText, int lineNumber ) const { if( fabs(actualValue - expectedValue) < epsilon ) return true; printf( "%s\nat line %i - " "Actual: %le - Expected: %le\n" "The value is within %le of expected. Epsilon is %le\n\n", lineText, lineNumber, actualValue, expectedValue, fabs(actualValue - expectedValue), epsilon); return false; }
bool testEquals( const char * actualValue, const char * expectedValue, const char * lineText, int lineNumber ) { int i; for( i = 0; expectedValue[i] != '\0'; ++i ) if( expectedValue[i] != actualValue[i] ) { printf( "%s\nat line %i - " "Actual: %s - Expected: %s\n" "First non-match is at %i\n\n", lineText, lineNumber, actualValue, expectedValue, i ); return false; } if( actualValue[i] != '\0' ) { printf( "%s\nat line %i - " "Actual: %s - Expected: %s\n" "Actual string is longer\n\n", lineText, lineNumber, actualValue, expectedValue ); return false; } return true; }
RCX_Result RCX_Link::Download(const UByte *data, int length, int chunk) { PDEBUGVAR("RCX_Link::Download chunk", chunk); RCX_Cmd cmd; RCX_Result result; UShort seq; int remain = length; int n; seq = 1; while (remain > 0) { // Transfer the remaining bytes if what is left to send // is less than the current chunk size. if (remain <= chunk) { // TODO: I have no clear idea what gProgramMode is for, but // it is almost always true. if (!gQuiet || gProgramMode) { seq = 0; } n = remain; } else { n = chunk; } n = AdjustChunkSize(n, data, fTransport->GetComplementData()); PDEBUGVAR("sending bytes", n); if (fVerbose) { printf("sending %d bytes\n", n); } result = Send(cmd.MakeDownload(seq++, data, (UShort)n), true, fDownloadWaitTime); if (RCX_ERROR(result)) return result; remain -= n; data += n; if (!IncrementProgress(n)) { return kRCX_AbortError; } } return kRCX_OK; }
void main() { char s[100], *p, delimit[20]; int i=0, len=0; cout << "Enter the input string \n"; gets_s(s); cout << "Enter the delimiter string \n"; gets_s(delimit); while(len++ != '\0'); p = strtok(s,delimit); while(p != NULL) { printf("%s \n", p); p = strtok(NULL, delimit); } cout << "Press 1 to exit \n"; cin >> s; }
int main() { using std::printf; printf(" \n coliru doesn't print leading spaces of the first line of output...\n"); printf("%4d\n", 42); printf("%4d\n", 12345); printf("%04d\n", 42); printf("%04d\n", 12345); printf("%.4f\n", 3.14); printf("%.4f\n", 3.14159); }
static void PrintFinalStats(CPodes& cpode) { int nst, nfe, nni, ncfn, netf; Real h0u; int flag; flag = cpode.getActualInitStep(&h0u); flag = cpode.getNumSteps(&nst); flag = cpode.getNumFctEvals(&nfe); flag = cpode.getNumErrTestFails(&netf); flag = cpode.getNumNonlinSolvIters(&nni); flag = cpode.getNumNonlinSolvConvFails(&ncfn); printf("\n Final statistics:\n\n"); printf(" Number of steps = %4d \n", nst); printf(" Number of f-s = %4d \n", nfe); printf(" Number of nonlinear iterations = %4d \n", nni); printf(" Number of nonlinear convergence failures = %4d \n", ncfn); printf(" Number of error test failures = %4d \n", netf); printf(" Initial step size = %g \n\n", h0u); }
int main(int argc, char* argv[]) { google::InitGoogleLogging(argv[0]); gflags::ParseCommandLineFlags(&argc, &argv, true); // Detect the hardware concurrency level. const std::size_t num_hw_threads = DefaultsConfigurator::GetNumHardwareThreads(); // Use the command-line value if that was supplied, else use the value // that we computed above, provided it did return a valid value. // TODO(jmp): May need to change this at some point to keep one thread // available for the OS if the hardware concurrency level is high. const unsigned int real_num_workers = quickstep::FLAGS_num_workers != 0 ? quickstep::FLAGS_num_workers : (num_hw_threads != 0 ? num_hw_threads : 1); if (real_num_workers > 0) { printf("Starting Quickstep with %d worker thread(s) and a %.2f GB buffer pool\n", real_num_workers, (static_cast<double>(quickstep::FLAGS_buffer_pool_slots) * quickstep::kSlotSizeBytes)/quickstep::kAGigaByte); } else { LOG(FATAL) << "Quickstep needs at least one worker thread to run"; } #ifdef QUICKSTEP_HAVE_FILE_MANAGER_HDFS if (quickstep::FLAGS_use_hdfs) { LOG(INFO) << "Using HDFS as the default persistent storage, with namenode at " << quickstep::FLAGS_hdfs_namenode_host << ":" << quickstep::FLAGS_hdfs_namenode_port << " and block replication factor " << quickstep::FLAGS_hdfs_num_replications << "\n"; } #endif // Initialize the thread ID based map here before the Foreman and workers are // constructed because the initialization isn't thread safe. quickstep::ClientIDMap::Instance(); MessageBusImpl bus; bus.Initialize(); // The TMB client id for the main thread, used to kill workers at the end. const client_id main_thread_client_id = bus.Connect(); bus.RegisterClientAsSender(main_thread_client_id, kPoisonMessage); // Setup the paths used by StorageManager. string fixed_storage_path(quickstep::FLAGS_storage_path); if (!fixed_storage_path.empty() && (fixed_storage_path.back() != quickstep::kPathSeparator)) { fixed_storage_path.push_back(quickstep::kPathSeparator); } string catalog_path(fixed_storage_path); catalog_path.append("catalog.pb.bin"); if (quickstep::FLAGS_initialize_db) { // Initialize the database // TODO(jmp): Refactor the code in this file! LOG(INFO) << "Initializing the database, creating a new catalog file and storage directory\n"; // Create the directory // TODO(jmp): At some point, likely in C++-17, we will just have the // filesystem path, and we can clean this up #ifdef QUICKSTEP_OS_WINDOWS std::filesystem::create_directories(fixed_storage_path); LOG(FATAL) << "Failed when attempting to create the directory: " << fixed_storage_path << "\n"; LOG(FATAL) << "Check if the directory already exists. If so, delete it or move it before initializing \n"; #else { string path_name = "mkdir " + fixed_storage_path; if (std::system(path_name.c_str())) { LOG(FATAL) << "Failed when attempting to create the directory: " << fixed_storage_path << "\n"; } } #endif // Create the default catalog file. std::ofstream catalog_file(catalog_path); if (!catalog_file.good()) { LOG(FATAL) << "ERROR: Unable to open catalog.pb.bin for writing.\n"; } quickstep::Catalog catalog; catalog.addDatabase(new quickstep::CatalogDatabase(nullptr, "default")); if (!catalog.getProto().SerializeToOstream(&catalog_file)) { LOG(FATAL) << "ERROR: Unable to serialize catalog proto to file catalog.pb.bin\n"; return 1; } // Close the catalog file - it will be reopened below by the QueryProcessor. catalog_file.close(); } // Setup QueryProcessor, including CatalogDatabase and StorageManager. std::unique_ptr<QueryProcessor> query_processor; try { query_processor.reset(new QueryProcessor(catalog_path, fixed_storage_path)); } catch (const std::exception &e) { LOG(FATAL) << "FATAL ERROR DURING STARTUP: " << e.what() << "\nIf you intended to create a new database, " << "please use the \"-initialize_db=true\" command line option."; } catch (...) { LOG(FATAL) << "NON-STANDARD EXCEPTION DURING STARTUP"; } // Parse the CPU affinities for workers and the preloader thread, if enabled // to warm up the buffer pool. const vector<int> worker_cpu_affinities = InputParserUtil::ParseWorkerAffinities(real_num_workers, quickstep::FLAGS_worker_affinities); const std::size_t num_numa_nodes_covered = DefaultsConfigurator::GetNumNUMANodesCoveredByWorkers(worker_cpu_affinities); if (quickstep::FLAGS_preload_buffer_pool) { std::chrono::time_point<std::chrono::steady_clock> preload_start, preload_end; preload_start = std::chrono::steady_clock::now(); printf("Preloading the buffer pool ... "); fflush(stdout); quickstep::PreloaderThread preloader(*query_processor->getDefaultDatabase(), query_processor->getStorageManager(), worker_cpu_affinities.front()); preloader.start(); preloader.join(); preload_end = std::chrono::steady_clock::now(); printf("in %g seconds\n", std::chrono::duration<double>(preload_end - preload_start).count()); } Foreman foreman(&bus, query_processor->getDefaultDatabase(), query_processor->getStorageManager(), num_numa_nodes_covered); // Get the NUMA affinities for workers. vector<int> cpu_numa_nodes = InputParserUtil::GetNUMANodesForCPUs(); if (cpu_numa_nodes.empty()) { // libnuma is not present. Assign -1 as the NUMA node for every worker. cpu_numa_nodes.assign(worker_cpu_affinities.size(), -1); } vector<int> worker_numa_nodes; PtrVector<Worker> workers; vector<client_id> worker_client_ids; // Initialize the worker threads. DCHECK_EQ(static_cast<std::size_t>(real_num_workers), worker_cpu_affinities.size()); for (std::size_t worker_thread_index = 0; worker_thread_index < worker_cpu_affinities.size(); ++worker_thread_index) { int numa_node_id = -1; if (worker_cpu_affinities[worker_thread_index] >= 0) { // This worker can be NUMA affinitized. numa_node_id = cpu_numa_nodes[worker_cpu_affinities[worker_thread_index]]; } worker_numa_nodes.push_back(numa_node_id); workers.push_back( new Worker(worker_thread_index, &bus, worker_cpu_affinities[worker_thread_index])); worker_client_ids.push_back(workers.back().getBusClientID()); } // TODO(zuyu): Move WorkerDirectory within Shiftboss once the latter is added. WorkerDirectory worker_directory(worker_cpu_affinities.size(), worker_client_ids, worker_numa_nodes); foreman.setWorkerDirectory(&worker_directory); // Start the worker threads. for (Worker &worker : workers) { worker.start(); } LineReaderImpl line_reader("quickstep> ", " ...> "); std::unique_ptr<SqlParserWrapper> parser_wrapper(new SqlParserWrapper()); std::chrono::time_point<std::chrono::steady_clock> start, end; for (;;) { string *command_string = new string(); *command_string = line_reader.getNextCommand(); if (command_string->size() == 0) { delete command_string; break; } parser_wrapper->feedNextBuffer(command_string); bool quitting = false; // A parse error should reset the parser. This is because the thrown quickstep // SqlError does not do the proper reset work of the YYABORT macro. bool reset_parser = false; for (;;) { ParseResult result = parser_wrapper->getNextStatement(); if (result.condition == ParseResult::kSuccess) { if (result.parsed_statement->getStatementType() == ParseStatement::kQuit) { quitting = true; break; } if (result.parsed_statement->getStatementType() == ParseStatement::kCommand) { try { quickstep::cli::executeCommand( *result.parsed_statement, *(query_processor->getDefaultDatabase()), stdout); } catch (const quickstep::SqlError &sql_error) { fprintf(stderr, "%s", sql_error.formatMessage(*command_string).c_str()); reset_parser = true; break; } continue; } std::unique_ptr<QueryHandle> query_handle; try { query_handle.reset(query_processor->generateQueryHandle(*result.parsed_statement)); } catch (const quickstep::SqlError &sql_error) { fprintf(stderr, "%s", sql_error.formatMessage(*command_string).c_str()); reset_parser = true; break; } DCHECK(query_handle->getQueryPlanMutable() != nullptr); foreman.setQueryPlan(query_handle->getQueryPlanMutable()->getQueryPlanDAGMutable()); foreman.reconstructQueryContextFromProto(query_handle->getQueryContextProto()); try { start = std::chrono::steady_clock::now(); foreman.start(); foreman.join(); end = std::chrono::steady_clock::now(); const CatalogRelation *query_result_relation = query_handle->getQueryResultRelation(); if (query_result_relation) { PrintToScreen::PrintRelation(*query_result_relation, query_processor->getStorageManager(), stdout); DropRelation::Drop(*query_result_relation, query_processor->getDefaultDatabase(), query_processor->getStorageManager()); } query_processor->saveCatalog(); printf("Execution time: %g seconds\n", std::chrono::duration<double>(end - start).count()); } catch (const std::exception &e) { fprintf(stderr, "QUERY EXECUTION ERROR: %s\n", e.what()); break; } printf("Query Complete\n"); } else { if (result.condition == ParseResult::kError) { fprintf(stderr, "%s", result.error_message.c_str()); } reset_parser = true; break; } } if (quitting) { break; } else if (reset_parser) { parser_wrapper.reset(new SqlParserWrapper()); reset_parser = false; } } // Terminate all workers before exiting. // The main thread broadcasts poison message to the workers. Each worker dies // after receiving poison message. The order of workers' death is irrelavant. MessageStyle style; style.Broadcast(true); Address address; address.All(true); std::unique_ptr<WorkerMessage> poison_message(WorkerMessage::PoisonMessage()); TaggedMessage poison_tagged_message(poison_message.get(), sizeof(*poison_message), kPoisonMessage); const tmb::MessageBus::SendStatus send_status = bus.Send(main_thread_client_id, address, style, std::move(poison_tagged_message)); CHECK(send_status == tmb::MessageBus::SendStatus::kOK) << "Broadcast message from Foreman to workers failed"; for (Worker &worker : workers) { worker.join(); } return 0; }