void mps_context_set_degree (mps_context * s, int n) { if (s->initialized) { if (s->secular_equation != NULL) { mps_secular_equation_free (s, MPS_POLYNOMIAL (s->secular_equation)); s->secular_equation = NULL; } mps_context_resize (s, n); } s->deg = s->n = n; /* Check if the numer of thread is greater of the number of roots, and in that case decrease it */ if (s->n_threads > s->deg) { MPS_DEBUG_WITH_INFO (s, "Adjusting concurrency limit to %d", s->deg); mps_thread_pool_set_concurrency_limit (s, s->pool, s->deg); } /* If a secular equation is present in the old context we should free it * now so it will be reallocated on the first call to the algorithm. */ if (s->secular_equation && MPS_POLYNOMIAL (s->secular_equation)->degree < n) mps_secular_equation_free (s, MPS_POLYNOMIAL (s->secular_equation)); s->secular_equation = NULL; }
void mps_context_set_degree (mps_context * s, int n) { s->deg = s->n = n; /* Check if the numer of thread is greater of the number of roots, and in that case decrease it */ if (s->n_threads > s->deg) mps_thread_pool_set_concurrency_limit (s, s->pool, s->deg); }
/** * @brief Perform some preliminary checks and setup before starting the real * MPSolve loop. */ static void mps_preliminary_setup (mps_context * ctx) { /* Make sure that non thread safe polynomial implementations are handled * in a safe way. */ if (!ctx->active_poly->thread_safe) { mps_thread_pool_set_concurrency_limit (ctx, NULL, 1); } }
/** * @brief Set the monomial poly p as the input polynomial for * the current equation. * * @param s The mps_context to set the monomial_poly into. * @param p The mps_monomial_poly to solve. */ void mps_context_set_input_poly (mps_context * s, mps_polynomial * p) { MPS_DEBUG_THIS_CALL (s); MPS_DEBUG (s, "Setting input poly"); if (p->degree < 0) { mps_error (s, "Polynomial degree should be positive"); return; } int i; s->active_poly = p; if (!p->thread_safe) mps_thread_pool_set_concurrency_limit (s, s->pool, 1); /* Set the density or sparsity of the polynomial, if it's not * a user polynomial */ if (MPS_IS_MONOMIAL_POLY (p)) { int original_degree = p->degree; mps_monomial_poly *mp = MPS_MONOMIAL_POLY (p); /* Deflate the polynomial if necessary */ mps_monomial_poly_deflate (s, p); s->zero_roots = original_degree - p->degree; MPS_DEBUG_WITH_INFO (s, "Degree = %d", p->degree); /* Check if the input polynomial is sparse or not. We can simply check if * the again vector is all of true values */ p->density = MPS_DENSITY_DENSE; for (i = 0; i <= MPS_POLYNOMIAL (mp)->degree; ++i) { if (!mp->spar[i]) { p->density = MPS_DENSITY_SPARSE; break; } } } mps_context_set_degree (s, p->degree); }
int main (int argc, char **argv) { #if HAVE_GRAPHICAL_DEBUGGER mps_boolean graphic_debug = false; #endif /* Create a new status */ s = mps_context_new (); /* Associate the SIGUSR1 signal to the mps_dump () function */ #ifndef __WINDOWS signal (SIGUSR1, status); #endif /* Leave this value to -1 if not precision has been enforced * by the user. Otherwise, override the polynomial input * precision. */ long int input_precision = -1; char * inline_poly = NULL; FILE *infile; /* Parse options */ mps_opt *opt; mps_phase phase = no_phase; /* This will be true if the user has explicitely selected the algorithm, otherwise we will be using our own heuristic. */ mps_boolean explicit_algorithm_selection = false; opt = NULL; while ((mps_getopts (&opt, &argc, &argv, MPSOLVE_GETOPT_STRING))) { switch (opt->optchar) { case 'l': { FILE* logstr = fopen (opt->optvalue, "w"); if (!logstr) mps_error (s, "Cannot open selected log file."); mps_context_set_log_stream (s, logstr); } break; case 'v': #ifdef HAVE_CONFIG_H printf (PACKAGE_STRING "\n"); #else printf ("MPSolve 3.1.5\n"); #endif mps_context_free (s); exit (EXIT_SUCCESS); case 'r': mps_context_select_starting_strategy (s, MPS_STARTING_STRATEGY_RECURSIVE); break; case 'O': /* Select the desired output format */ if (!opt->optvalue) { mps_error (s, "An argument is needed for option 'O'"); break; } switch (*opt->optvalue) { case 'f': mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_FULL); break; case 'b': mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_BARE); break; case 'g': mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_GNUPLOT); if (*(opt->optvalue + 1) == 'f') { mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_GNUPLOT_FULL); s->gnuplot_format = "xyerrorbars"; } else if (*(opt->optvalue + 1) == 'p') { mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_GNUPLOT_FULL); s->gnuplot_format = "points"; } break; case 'v': mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_VERBOSE); break; case 'c': mps_context_set_output_format (s, MPS_OUTPUT_FORMAT_COMPACT); break; default: mps_error (s, "The selected output format is not supported"); break; } break; /* select search set */ case 'S': switch (*opt->optvalue) { case 'a': s->output_config->search_set = MPS_SEARCH_SET_COMPLEX_PLANE; break; case 'r': s->output_config->search_set = MPS_SEARCH_SET_POSITIVE_REAL_PART; break; case 'l': s->output_config->search_set = MPS_SEARCH_SET_NEGATIVE_REAL_PART; break; case 'u': s->output_config->search_set = MPS_SEARCH_SET_POSITIVE_IMAG_PART; break; case 'd': s->output_config->search_set = MPS_SEARCH_SET_NEGATIVE_IMAG_PART; break; case 'i': s->output_config->search_set = MPS_SEARCH_SET_UNITARY_DISC; break; case 'o': s->output_config->search_set = MPS_SEARCH_SET_UNITARY_DISC_COMPL; break; case 'R': s->output_config->search_set = MPS_SEARCH_SET_REAL; break; case 'I': s->output_config->search_set = MPS_SEARCH_SET_IMAG; break; case 'U': s->output_config->search_set = MPS_SEARCH_SET_CUSTOM; break; default: mps_error (s, "Bad search set switch: ", opt->optvalue, ", use a|r|l|u|d|i|o|R|I|U"); } if (strlen (opt->optvalue) != 1) mps_error (s, "Bad set: ", opt->optvalue); break; /* select multiplicity */ case 'M': switch (*opt->optvalue) { case '+': s->output_config->multiplicity = true; break; case '-': s->output_config->multiplicity = false; break; default: mps_error (s, "Bad multiplicity switch: ", opt->optvalue, ", use +|-"); } if (strlen (opt->optvalue) != 3) mps_error (s, "Bad multiplicity option: ", opt->optvalue); break; /* detection */ case 'D': switch (*opt->optvalue) { case 'n': s->output_config->root_properties = MPS_OUTPUT_PROPERTY_NONE; break; case 'r': s->output_config->root_properties = MPS_OUTPUT_PROPERTY_REAL; break; case 'i': s->output_config->root_properties = MPS_OUTPUT_PROPERTY_IMAGINARY; break; case 'b': s->output_config->root_properties = MPS_OUTPUT_PROPERTY_REAL | MPS_OUTPUT_PROPERTY_IMAGINARY; break; default: mps_error (s, "Bad detection switch: ", opt->optvalue, ", use n|r|i|b"); } if (strlen (opt->optvalue) != 1) mps_error (s, "Bad detection option: ", opt->optvalue); break; /* I/O streams */ case 'R': s->rtstr = fopen (opt->optvalue, "r"); if (s->rtstr == NULL) mps_error (s, "Cannot open roots file: ", opt->optvalue); s->resume = true; break; /* Additional checks */ case 'C': switch (*opt->optvalue) { case 'R': s->chkrad = true; break; case 'r': s->chkrad = false; break; default: mps_error (s, "Bad check switch: ", opt->optvalue, ", use R|r"); } if (strlen (opt->optvalue) != 1) mps_error (s, "Bad check option: ", opt->optvalue); break; case 'a': explicit_algorithm_selection = true; switch (*opt->optvalue) { case 'u': mps_context_select_algorithm (s, MPS_ALGORITHM_STANDARD_MPSOLVE); break; case 's': mps_context_select_algorithm (s, MPS_ALGORITHM_SECULAR_GA); break; default: mps_error (s, "The selected algorithm is not supported"); break; } break; case 'b': mps_context_set_jacobi_iterations (s, true); break; case 'c': mps_context_set_crude_approximation_mode (s, true); break; case 'o': mps_context_set_output_prec (s, (atoi (opt->optvalue)) * LOG2_10 + 1); break; case 'i': input_precision = atoi (opt->optvalue) * LOG2_10; break; case 'G': switch (*opt->optvalue) { case 'a': mps_context_set_output_goal (s, MPS_OUTPUT_GOAL_APPROXIMATE); break; case 'i': mps_context_set_output_goal (s, MPS_OUTPUT_GOAL_ISOLATE); break; case 'c': mps_context_set_output_goal (s, MPS_OUTPUT_GOAL_COUNT); break; default: mps_error (s, "The selected goal does not exists"); break; } break; case 'p': inline_poly = strdup (opt->optvalue); break; #ifndef DISABLE_DEBUG case 'd': mps_context_add_debug_domain (s, MPS_DEBUG_INFO); if (!opt->optvalue) break; /* If debugging was enabled, parse debug_level */ while (*opt->optvalue) { char output[255]; switch (*opt->optvalue++) { case 't': mps_context_add_debug_domain (s, MPS_DEBUG_TRACE); break; case 'a': mps_context_add_debug_domain (s, MPS_DEBUG_APPROXIMATIONS); break; case 'c': mps_context_add_debug_domain (s, MPS_DEBUG_CLUSTER); break; case 'i': mps_context_add_debug_domain (s, MPS_DEBUG_IMPROVEMENT); break; case 'w': mps_context_add_debug_domain (s, MPS_DEBUG_TIMINGS); break; case 'o': mps_context_add_debug_domain (s, MPS_DEBUG_IO); break; case 'm': mps_context_add_debug_domain (s, MPS_DEBUG_MEMORY); break; case 'f': mps_context_add_debug_domain (s, MPS_DEBUG_FUNCTION_CALLS); break; case 'p': mps_context_add_debug_domain (s, MPS_DEBUG_PACKETS); break; case 'r': mps_context_add_debug_domain (s, MPS_DEBUG_REGENERATION); break; default: sprintf (output, "Unrecognized debug option: %c", *(opt->optvalue - 1)); mps_error (s, output); break; } } break; #endif #if HAVE_GRAPHICAL_DEBUGGER case 'x': graphic_debug = true; break; #endif case 's': if (opt->optvalue == NULL) { mps_error (s, "You need to specify a valid file with -s"); break; } else { s->rtstr = fopen (opt->optvalue, "r"); if (! s->rtstr) { mps_error (s, "Cannot open the given file: %s", opt->optvalue); break; } else { mps_context_select_starting_strategy (s, MPS_STARTING_STRATEGY_FILE); } } break; case 't': switch (opt->optvalue[0]) { case 'f': phase = float_phase; break; case 'd': phase = dpe_phase; break; default: usage (s, argv[0]); } break; case 'j': mps_thread_pool_set_concurrency_limit (s, NULL, atoi (opt->optvalue)); s->n_threads = atoi (opt->optvalue); break; default: usage (s, argv[0]); break; } } #if HAVE_GRAPHICAL_DEBUGGER if (graphic_debug) { /* Init GTK only if graphic debug is requested. In all other case is unnecessary * and will waste computational time that is likely to bias benchmarks. */ gtk_init (&argc, &argv); logger = mps_iteration_logger_new (); mps_iteration_logger_set_mps_context (logger, s); g_signal_connect (GTK_WIDGET (logger), "delete_event", G_CALLBACK (on_iteration_logger_destroy), NULL); gtk_widget_show_all (GTK_WIDGET (logger)); } #endif if (mps_context_has_errors (s)) { mps_print_errors (s); return EXIT_FAILURE; } if (argc > 2) usage (s, argv[0]); /* If no file is provided use standard input */ if (inline_poly) { // poly = mps_parse_inline_poly_from_string (s, inline_poly); mps_abstract_input_stream * stream = (mps_abstract_input_stream *) mps_memory_file_stream_new (inline_poly); poly = mps_monomial_yacc_parser (s, stream); if (mps_context_has_errors (s)) { mps_print_errors (s); return EXIT_FAILURE; } free (inline_poly); } else { if (argc == 1) infile = stdin; else infile = fopen (argv[1], "r"); if (!infile) { mps_error (s, "Cannot open input file for read, aborting."); mps_print_errors (s); return EXIT_FAILURE; } /* Parse the input stream and if a polynomial is given as output, * allocate also a secular equation to be used in regeneration */ poly = mps_parse_stream (s, infile); } if (!poly) { mps_error (s, "Error while parsing the polynomial, aborting."); mps_print_errors (s); return EXIT_FAILURE; } else mps_context_set_input_poly (s, poly); /* Override input precision if needed */ if (input_precision >= 0) mps_polynomial_set_input_prec (s, poly, input_precision); /* Perform some heuristic for the algorithm selection, but only if the user * hasn't explicitely selected one. */ if (! explicit_algorithm_selection) { mps_context_select_algorithm (s, (MPS_IS_MONOMIAL_POLY (poly) && MPS_DENSITY_IS_SPARSE (poly->density)) ? MPS_ALGORITHM_STANDARD_MPSOLVE : MPS_ALGORITHM_SECULAR_GA ); } /* Close the file if it's not stdin */ if (argc == 2 && ! inline_poly) fclose (infile); /* Select the starting phase according to user input */ mps_context_set_starting_phase (s, phase); /* Solve the polynomial */ #if HAVE_GRAPHICAL_DEBUGGER if (graphic_debug) { mps_mpsolve_async (s, cleanup_context, NULL); gtk_main (); } else { #endif mps_mpsolve (s); cleanup_context (s, NULL); #if HAVE_GRAPHICAL_DEBUGGER } #endif }