help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-glpk] Thread safety of GLPK


From: Sylvain Fournier
Subject: Re: [Help-glpk] Thread safety of GLPK
Date: Thu, 15 Apr 2010 14:11:17 -0300

Hi,

Thank you Andrew for this feature which will be valuable for me as well!
I am using the java version of glpk, and I am wondering if it has also been tested with multi-threads. What do I have to change in the java or swig files in order to be able to take this into account? For example, where do I have to initialize the environment thread pointer?

Sylvain Fournier

Date: Thu, 15 Apr 2010 14:06:15 +0400
From: Andrew Makhorin <address@hidden>
Subject: Re: [Help-glpk] Thread safety of GLPK
To: "Hammond, Paul" <address@hidden>
Cc: address@hidden
Message-ID: <address@hidden>
Content-Type: text/plain; charset="us-ascii"

> Thanks for this. If you find your old code, do send it over, we
> don't mind if it's not the standard release, if it works, we'll
> consider using it.

Please see the attachment.

You need to replace file glpenv02.c with a reentrant version and then
build the package as usual. Note that glpsol will not work, because it
does not create the thread-specific data key used in the reentrant
version of glpenv02.c. Mtsamp.c is an example program that works.

Hope this helps.

Andrew Makhorin
-------------- next part --------------
/* glpenv02.c (thread local storage) */

/* (reserved for copyright notice) */

#include <pthread.h>
#include "glpenv.h"

/* re-enterable POSIX version */

pthread_key_t _glp_pth_key;
/* must be initialized in the main program */

void tls_set_ptr(void *ptr)
{     pthread_setspecific(_glp_pth_key, ptr);
     return;
}

void *tls_get_ptr(void)
{     void *ptr;
     ptr = pthread_getspecific(_glp_pth_key);
     return ptr;
}

/* eof */
-------------- next part --------------
/* mtsamp.c */

/***********************************************************************
*  This is an example program which illustrates how to use GLPK API in
*  multi-threaded application on POSIX platform.
*
*  To run this program use the following command:
*
*     mtsamp.exe mps-file-1 mps-file-2 ... mps-file-n
*
*  Each mps file specified in the command line is processed by separate
*  thread.
***********************************************************************/

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>

void *solve_mps(void *arg)
{     char *fname = arg;
     glp_prob *P;
     P = glp_create_prob();
     if (glp_read_mps(P, GLP_MPS_FILE, NULL, fname) != 0)
     {  fprintf(stderr, "Cannot read mps file `%s'\n", fname);
        return NULL;
     }
     glp_simplex(P, NULL);
     glp_delete_prob(P);
     glp_free_env();
     return NULL;
}

#define MAX_THREADS 20

extern pthread_key_t _glp_pth_key;

int main(int argc, char *argv[])
{     pthread_t h[1+MAX_THREADS];
     int t, ret;
     if (argc < 2)
     {  fprintf(stderr, "At least one mps file must be specified\n");
        exit(EXIT_FAILURE);
     }
     if (argc-1 > MAX_THREADS)
     {  fprintf(stderr, "Too many mps files\n");
        exit(EXIT_FAILURE);
     }
     ret = pthread_key_create(&_glp_pth_key, NULL);
     if (ret != 0)
     {  fprintf(stderr, "Unable to create thread-specific key\n");
        exit(EXIT_FAILURE);
     }
     for (t = 1; t < argc; t++)
     {  ret = pthread_create(&h[t], NULL, solve_mps, argv[t]);
        if (ret != 0)
        {  fprintf(stderr, "Unable to create thread for `%s'\n",
              argv[t]);
           exit(EXIT_FAILURE);
        }
     }
     for (t = 1; t < argc; t++)
     {  ret = pthread_join(h[t], NULL);
        if (ret != 0)
        {  fprintf(stderr, "Waiting failure for thread %d\n", t);
           exit(EXIT_FAILURE);
        }
     }
     fprintf(stderr, "GLPK multi-threaded DLL seems to work\n");
     return 0;
}

/* eof */


reply via email to

[Prev in Thread] Current Thread [Next in Thread]