judge_result testcase_impl::run(env &env, compiler::result &cr) { judge_result result = {0}; shared_ptr<temp_dir> dir = _prepare_dir(env.pool(), cr); env.grant_access(dir->path()); path_a executable_path; if (cr.compiler->target_executable_path().empty()) { executable_path = dir->path(); executable_path.push(cr.compiler->target_filename().c_str()); } else { executable_path = cr.compiler->target_executable_path(); } shared_ptr<testcase_impl::context> context(new testcase_impl::context(*this)); judge::bunny bunny(env, false, executable_path.c_str(), cr.compiler->target_command_line(), dir->path(), context->stdin_pipe.read_handle(), context->stdout_pipe.write_handle(), context->stderr_pipe.write_handle(), limit_); context->stdin_pipe.close_read(); context->stdout_pipe.close_write(); context->stderr_pipe.close_write(); // stdin thread env.pool().thread_pool().queue([context]()->void { try { istream in(&context->stdin_buf); os_filebuf out_buf(context->stdin_pipe.write_handle(), false); ostream out(&out_buf); const size_t buffer_size = 4096; util::stream_copy<buffer_size>(in, out); } catch (...) { } context->stdin_pipe.close_write(); context->stdin_event.set(); }); // stderr thread env.pool().thread_pool().queue([context]()->void { try { os_filebuf in_buf(context->stderr_pipe.read_handle(), false); istream in(&in_buf); const size_t buffer_size = 4096; util::stream_copy_n<buffer_size>(in, context->stderr_stream, context->stderr_output_limit); } catch (...) { } context->stderr_pipe.close_read(); context->stderr_event.set(); }); bunny.start(); // judge { istream model_in(&context->stdout_buf); os_filebuf user_buf(context->stdout_pipe.read_handle(), false); istream user_in(&user_buf); pair<bool, string> compare_result = compare_stream(model_in, user_in); if (compare_result.first) { result.flag = max(result.flag, JUDGE_ACCEPTED); } else { result.flag = max(result.flag, JUDGE_WRONG_ANSWER); } judge_output_ = move(compare_result.second); // read all user output const size_t buffer_size = 4096; util::stream_copy<buffer_size>(user_in, onullstream()); } bunny::result bunny_result = bunny.wait(); DWORD wait_result = winstl::wait_for_multiple_objects(context->stdin_event, context->stderr_event, true, INFINITE); if (wait_result == WAIT_FAILED) { throw win32_exception(::GetLastError()); } result.flag = max(result.flag, bunny_result.flag); result.time_usage_ms = bunny_result.time_usage_ms; result.memory_usage_kb = bunny_result.memory_usage_kb; result.runtime_error = bunny_result.runtime_error; result.judge_output = judge_output_.c_str(); user_output_ = context->stderr_stream.str(); result.user_output = user_output_.c_str(); return result; }
int L1rlrOwlqnAlgorithm::load_model(std::string filename) { std::ifstream model_in(filename.c_str()); if (!model_in.is_open()) { std::cout << "Open model file [" << filename << "] failed!" << std::endl; } std::string line; uint32_t dim = 0, dim_idx = 0; double weight = 0; if ( getline(model_in, line, '\n') ) { sscanf(line.c_str(), "%u %lf", &dim, &_l1weight); } _x_k.set_size(dim); if ( getline(model_in, line, '\n') ) { std::stringstream stringsplitter(line.c_str()); std::string token; while ( std::getline(stringsplitter, token, ' ') ) { if (token.length() != 0) { sscanf(token.c_str(), "%lf", &weight); _x_k[dim_idx++] = weight; } } } model_in.close(); return 0; }
int main(int argc, char* argv[]) { NERConfiguration ner_config; StringVector input_files; std::string model_file; bool eval_mode = false; bool running_text = false; bool force_tsv_output = false; std::string output_format; unsigned order = 1; parse_options(argc, argv, model_file, input_files, ner_config, order, running_text, eval_mode, output_format); // if (running_text) // ner_config.set_running_text_input(true); if (order > 3) { std::cerr << "crf-apply: Error: Currently, only the orders 1, 2 or 3 are supported" << std::endl; exit(2); } std::ifstream model_in(model_file.c_str(), std::ios::binary); if (!model_in) { std::cerr << "crf-apply: Error: Could not open binary model file '" << model_file << "'\n"; exit(2); } if (order == 1) load_and_apply_model<1>(model_in, model_file, input_files, ner_config, running_text, eval_mode, output_format); else if (order == 2) load_and_apply_model<2>(model_in, model_file, input_files, ner_config, running_text, eval_mode, output_format); else if (order == 3) load_and_apply_model<3>(model_in, model_file, input_files, ner_config, running_text, eval_mode, output_format); }