Problem with multithreading

31 views
Skip to first unread message

Tadeusz

unread,
Jan 29, 2019, 1:09:11 PM1/29/19
to Renjin
Hello,

We faced a problem with multithreading in Renjin. The 'optim' function with 'L-BFGS' method calculates different results when is invoked from many threads.

A simple Java project was prepared to reproduce the problem. Also the code is attached at the bottom of this mail.


When we run the code with THREADS_NUMBER=1 we get following correct results (every time the same):
list(par = c(0,99980003783379, 0,99960011579784), value = 3.9984867919192357E-8, counts = c(49L, 49L), convergence = c(0L), message = CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH)
list
(par = c(0,99980003783379, 0,99960011579784), value = 3.9984867919192357E-8, counts = c(49L, 49L), convergence = c(0L), message = CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH)
list
(par = c(0,99980003783379, 0,99960011579784), value = 3.9984867919192357E-8, counts = c(49L, 49L), convergence = c(0L), message = CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH)

But when we run it with more threads e.g. THREADS_NUMBER = 2 we get different results for the calls and sometimes even an errors:
list(par = c(-1,02632082910288, 1,06079411634746), value = 24.199999999999996, counts = c(2L, 2L), convergence = c(52L), message = ERROR: ABNORMAL_TERMINATION_IN_LNSRCH)
list
(par = c(-1,2, 1), value = 24.199999999999996, counts = c(2L, 2L), convergence = c(52L), message = ERROR: ABNORMAL_TERMINATION_IN_LNSRCH)
list
(par = c(-1,2, 1), value = 0.0, counts = c(0L, 0L), convergence = c(52L), message = ERROR: ABNORMAL_TERMINATION_IN_LNSRCH)
list
(par = c(0,37771903024192, 1,64396551742973), value = 225.77555649737445, counts = c(0L, 0L), convergence = c(0L), message = CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH)
list
(par = c(0,99987487694407, 0,99974046721066), value = 2.4309119528957442E-8, counts = c(52L, 52L), convergence = c(0L), message = CONVERGENCE: REL_REDUCTION_OF_F <= FACTR*EPSMCH)


Separate ScriptEngine is created for each thread as it is described in Renjin documentation. Do you have any ideas what is wrong and how we can fix it?
Please let us know if you encounter any problems with running our project.

Thanks in advance. 
Tadeusz



code:
package com.example;

import javax.script.ScriptEngine;
import org.renjin.script.RenjinScriptEngineFactory;
import org.renjin.sexp.SEXP;

public class Example {

 
private static final ThreadLocal<ScriptEngine> engines = new ThreadLocal<>();
 
private static final int RUNS = 100;
 
private static final int THREADS_NUMBER = 2;

 
public static void main(String[] args) {
   
new Example().execute();
 
}

 
private void execute() {
   
for (int i = 0; i < THREADS_NUMBER; i++) {
     
new Thread(this::task).start();
   
}
 
}

 
private void task() {
   
ScriptEngine engine = getEngine();
   
try {
     engine
.eval("fr <- function(x) { ## Rosenbrock Banana function\\n\" +\n"
     
+ " x1 <- x[1]\n"
     
+ " x2 <- x[2]\n"
     
+ " 100 * (x2 - x1 * x1) ^ 2 + (1 - x1) ^ 2\n"
     
+ "}");
   
} catch (Exception e) {
     
System.err.println(e.getMessage());
   
}

   
for (int i = 0; i < Example.RUNS; i++) {
     
try {
       SEXP result
= (SEXP) engine.eval("optim(c(-1.2,1), fr, method = \"L-BFGS\")");
       
System.out.println(result);
     
} catch (Exception e) {
       
System.err.println(e.getMessage());
     
}
   
}
 
}

 
private ScriptEngine getEngine() {
   
ScriptEngine engine = engines.get();
   
if (engine == null) {
     
RenjinScriptEngineFactory factory = new RenjinScriptEngineFactory();
     engine
= factory.getScriptEngine();
     engines
.set(engine);
   
}
   
return engine;
 
}
}


Reply all
Reply to author
Forward
0 new messages