简体   繁体   English

在parallel_for循环中使用GLPK

[英]Using GLPK in parallel_for loop

I want to run the LP solver in GLPK in a parallel_for loop. 我想在parallel_for循环中运行GLPK中的LP求解器。 The problems are all independent of each other so there shouldn't be any interference. 这些问题都是相互独立的,因此不应有任何干扰。

Here is an example code that fails, this is essentially the example code from the glpk wikibook but wrapped in a parallel_for loop. 这是一个失败的示例代码,这实际上是来自glpk wikibook的示例代码,但包含在parallel_for循环中。 Any help will be appreciated 任何帮助将不胜感激

//#include <stdio.h>            /* C input/output                       */
//#include <stdlib.h>           /* C standard library                   */
#include <iostream>
#include <glpk.h>             /* GNU GLPK linear/mixed integer solver */
#include <ppl.h>

using namespace concurrency;
using namespace std;

void main()
{
    parallel_for(0, 10, [](int i){
        /* declare variables */
        glp_prob *lp;
    int *ia = new int[4];
    int *ja = new int[4];
    double *ar = new double[4];
    double z, x1, x2;


    /* create problem */
    lp = glp_create_prob();
    glp_set_prob_name(lp, "minimax");
    glp_set_obj_dir(lp, GLP_MAX);


    /* fill problem */
    glp_add_rows(lp, 2);
    //glp_set_row_name(lp, 1, "p");
    glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 1.0);
    //glp_set_row_name(lp, 2, "q");
    glp_set_row_bnds(lp, 2, GLP_UP, 0.0, 2.0);


    glp_add_cols(lp, 2);
    //glp_set_col_name(lp, 1, "x1");
    glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);
    glp_set_obj_coef(lp, 1, 0.6);
    //glp_set_col_name(lp, 2, "x2");
    glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);
    glp_set_obj_coef(lp, 2, 0.5);


    ia[1] = 1, ja[1] = 1, ar[1] = 1.0; /* a[1,1] = 1 */
    ia[2] = 1, ja[2] = 2, ar[2] = 2.0; /* a[1,2] = 2 */
    ia[3] = 2, ja[3] = 1, ar[3] = 3.0; /* a[2,1] = 3 */
    ia[4] = 2, ja[4] = 2, ar[4] = 1.0; /* a[2,2] = 1 */
    glp_load_matrix(lp, 4, ia, ja, ar);



    /* solve problem */
    glp_simplex(lp, NULL);
    /* recover and display results */
    z = glp_get_obj_val(lp);
    x1 = glp_get_col_prim(lp, 1);
    x2 = glp_get_col_prim(lp, 2);
    printf("z = %g; x1 = %g; x2 = %g\n", z, x1, x2);
    /* housekeeping */
    glp_delete_prob(lp);
    glp_free_env();
});
    system("pause");
}

You're calling glp_free_env from inside each thread, while the library is still actively doing work in other threads. 你从每个线程内部调用glp_free_env ,而库仍然在其他线程中正在工作。 That won't work well -- you're yanking the rug out from under threads working hard.. 这样做不会很好 - 你正在努力工作的线程中扯下地毯。

Instead call it only after all threads completed computation (join them). 而是在所有线程完成计算(加入它们)之后才调用它。 For this simple example, you can probably skip the cleanup step altogether. 对于这个简单的示例,您可以完全跳过清理步骤。

I have found the problem. 我发现了这个问题。 The issue is with the GLPK source code, there are a couple of routines that are not reenterable causing some big issues. 问题在于GLPK源代码,有几个例程不可重新导入,导致一些重大问题。 This is documented here: http://en.wikibooks.org/wiki/GLPK/Using_the_GLPK_callable_library . 这在此处记录: http//en.wikibooks.org/wiki/GLPK/Using_the_GLPK_callable_library There is also a fix in place but it requires rebuilding it. 还有一个修复程序,但它需要重建它。 Note that this only works with version 4.50 of GLPK and older, newer versions have a slightly different layout. 请注意,这仅适用于GLPK版本4.50和旧版本,较新版本的布局略有不同。

While Ben was completely correct, making this change to the library solves all issues and lets you free the environment inside the loop. 虽然Ben完全正确,但对库进行此更改可以解决所有问题并让您释放循环内的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM