Forum Discussion

FICO's avatar
FICO
Community Manager
4 years ago

Solving several problems concurrently in a multithreaded application

Hello. We are currently trying to port an existing application from a purely single threaded approach to a heavily threaded one.

While I am aware that Xpress has out-of-the-box support for multithreaded MIP search, we understand that it is necessary for the whole application to be run in parallel in order to fully take advantage of the hardware now available to us.

I have been working with OpenMP but have had no success in getting Xpress to work concurrently. I would like to be able to do the following for each thread:

  1. Run a model with certain parameters set
  2. Extract the resulting problem
  3. Solve the problem

There are several resources and sample codes (like this one) to easily do steps 1-3. However, problems arise once the code is parallelized.

A very simple code like this one:

int main()
{
	#pragma omp parallel for
	for (int k = 0; k < 20; k++){
		XPRMmodel mod;
		XPRSprob prob;
		int result;
 
		XPRMinit();
		mod=XPRMloadmod("burglar4.bim", NULL);
		XPRMrunmod(mod, &result, NULL);
	}
	
}

... fails with error messages like

Mosel: E-84: File `burglar4.bim': model cannot be renamed.

The problem appears to only happen if concurrency does actually occur - if the XPRMloadmod and XPRMrunmod are both wrapped inside a #pragma omp critical section, then the program finishes as expected.

So my question is:

How should I write my code in order for it to work in a multithreaded fashion using OpenMP?

5 Replies

  • FICO's avatar
    FICO
    Community Manager

    Is this by any chance related to this post on stackoverflow?

    In any case, I think the call to XPRMinit() should be outside the loop since you only want to initialize mosel once.

    What exactly is the line that raises the error message? Is it XPRMloadmod() or XPRMrunmod()?

  • FICO's avatar
    FICO
    Community Manager

    When you are using the function 'XPRMloadmod' repeatedly to load the same model within the same Mosel instance you need to use "*" in place of NULL as the second argument in order to obtain unique internal names (this behaviour is documented, please see the documentation of this function in the Mosel Libraries Reference), otherwise Mosel tries to replace the existing model that has already been loaded and this fails if this model is being executed (see documentation of this error message in the Mosel Language Reference).

  • FICO's avatar
    FICO
    Community Manager

    Thank you Daniel Junglas and Susanne Heipcke for providing support. There was indeed an issue with XPRMloadmod and the names used. THANK YOU.

    After changing the second parameter like Susanne suggested, the problem disappeared.

    Thank you very much for the support provided, I will continue to work on this project.

    Best regards,

    Juan Pablo.

  • FICO's avatar
    FICO
    Community Manager

    I would also like to ask Daniel if the call to XPRMinit() should be done once per thread like I did or whether a single call run on the master thread would be enough.

    Is there any documentation/examples about multithreaded applications and Xpress?

  • FICO's avatar
    FICO
    Community Manager

    The initialization function XPRMinit should be called once per process (with a matching number of calls to XPRMfinish if you want to terminate Mosel explicitly to free up memory and delete the temporary directory). We don't provide any examples of multithreaded use of Mosel on the C level - given that the Mosel language itself provides such functionality our examples explain how to implement this on the Mosel level (see in particular in the examples discussed in the whitepaper 'Multiple models and parallel solving with Mosel').