简体   繁体   中英

where should I insert `os.environ[“MKL_NUM_THREADS”] = “1”`?

According to this document , I have to insert os.environ["MKL_NUM_THREADS"] = "1" to prevent numpy from using more than one core. But where should I insert this? before numpy or in each actor's construction? Should it be used in the Learner ?

For example, where should I insert it if I have the following three files?

""" learner.py """
import numpy as np
import ray

@ray.remote
class Learner:
    def __init__(self):
        # do something
""" worker.py """
import numpy as np
import ray

@ray.remote
class Worker:
    def __init__(self):
        # do something
""" main.py """
# program starts from here

import numpy as np
import ray

from learner import Learner
from worker import Worker

ray.init()

learner = Learner.remote()
worker = Worker.remote()

# do something...

How can I check if it works as expected? Here's a snapshot of top from my real project, where I insert os.environ["MKL_NUM_THREADS"] = "1" at the beginning of main.py . Does it work? 在此处输入图片说明

You can check if it's working by looking in top and making sure that none of the worker process CPU utilizations exceed 100%.

You want to set this before you call ray.init() so that when the worker processes are forked, the environment variable propagates to them.

You can achieve the same thing by starting your Python script with MKL_NUM_THREADS=1 python script.py .

Note that this environment variable makes sense when numpy uses MKL, but depending on the BLAS implementation you might need to set an environment variable like OPENBLAS_NUM_THREADS=1 or OMP_NUM_THREADS=1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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