Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the earliest way to call a Java/Scala method?

I use some dirty code to set environment variable in Scala from second answer of this question

I test this in IDE(IDEA Intellij) and set OMP_NUM_THREADS at the beginning of my class.

import org.scalatest.{FlatSpec, Matchers}
class MyTest extends FlatSpec with Matchers {
  val m = Map("OMP_NUM_THREADS" -> "1")
  EnvHacker.setEnv(m)

After set, I could read from System.env, it works. But when my program runs, it does not use this. I tried set it in static block, but still not work.

But if I set it in IDE run configuration(before JVM run), it works and runs as I expect. So seems it is read before I modify the variable.

Or in other word, I have a piece of code, what is the earliest way to call it in Java/Scala. e.g. static block is called before the first line of main method.

Some details updated:

I am using tensorflow-mkl Java API, it would read System environment variable OMP_NUM_THREADS at some time, according to my test result, this operation is before the system static block. However, I want to control in code because I do not know the configuration expected without code logic.

like image 557
Litchy Avatar asked Nov 14 '22 21:11

Litchy


1 Answers

I think this is an XY problem: you want to control OpenMP via "OMP_NUM_THREADS" env but you can't or don't want to set the actual environment variables on your process for some reason? Is that right?

How are you invoking OpenMP? OpenMP is a C lib https://www.openmp.org/ , so it won't notice any changes you make to the java.lang.ProcessEnvironment#theEnvironment field, no matter how early you set it.

If you are invoking OpenMP via an exec call, then you should be able to pass a new environment to it.

If you are invoking OpenMP via JNI, then you won't be able to change your environment variables from Java, you will need to actually set the env when you start the process. See Can I set an Environment Variable for Java Native Interface (JNI) libraries?

Could you instead use omp_set_num_threads() rather than OMP_NUM_THREADS env?

like image 160
Rich Avatar answered Dec 20 '22 00:12

Rich