# Remote function with multithreading does not get maximum cpu usages

**URL:** <https://discuss.ray.io/t/remote-function-with-multithreading-does-not-get-maximum-cpu-usages/7847>\
**Category:** Ray Core\
**Created:** [October 11, 2022, 9:02am UTC](https://discuss.ray.io/t/remote-function-with-multithreading-does-not-get-maximum-cpu-usages/7847 "2022-10-11T09:02:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xjyu](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/xjyu/32/3303_2.png) [@xjyu](https://discuss.ray.io/u/xjyu)\
**Post date:** [October 11, 2022, 9:02am UTC](https://discuss.ray.io/t/remote-function-with-multithreading-does-not-get-maximum-cpu-usages/7847/1 "2022-10-11T09:02:58Z")

</div>

I have a remote worker that invokes a parallelized numba function, like:

```python
import numba
import time 
import ray

@numa.njit(parallel=True)
def nba_sum(x):
     r = 0
     for i in numba.prange(x.shape[0]):
        r += x[i] 
     return r

@ray.remote
def sum(shared_token):       
       x = <get the array view from shared token>
       nba_sum(x) # so i ensure the numba function is already compiled
       t = time.time()
       nba_sum(x)
       return time.time() - t

x = <some very large array view to shared memory>
shared_token = <shared token to the memory of x>

t = time.time()
nba_sum(x)
print('local time =', time.time() - t)

print('remote time =', ray.get(sum.remote(shared_token))

```

I used a machine with 256 logical cpus (two AMD EPYC 7763 64-core Processors) to test the script and get that the output remote time always slower than the local one, about \> 20% more time.  
Can I avoid the cost?

---

<div class="post-metadata">

**Author:** ![Chen\_Shen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/chen_shen/32/1486_2.png) [@Chen\_Shen](https://discuss.ray.io/u/Chen_Shen)\
**Post date:** [October 15, 2022, 6:51pm UTC](https://discuss.ray.io/t/remote-function-with-multithreading-does-not-get-maximum-cpu-usages/7847/2 "2022-10-15T18:51:27Z")

</div>

hi @xjyu

Could the 20% overhead come from the data serialization cost when you pass x into sum? You can verify it by measuring the time inside the sum call.

Another possibility is you need to set the env variable `OMP_NUM_THREADS=num_cpus` before you start your script; since Ray by default set it to 1.

---

<div class="post-metadata">

**Author:** ![zhz](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/zhz/32/12_2.png) [@zhz](https://discuss.ray.io/u/zhz)\
**Post date:** [December 2, 2022, 6:01am UTC](https://discuss.ray.io/t/remote-function-with-multithreading-does-not-get-maximum-cpu-usages/7847/3 "2022-12-02T06:01:59Z")

</div>

Thanks for reporting the issue @xjyu !

I’m going to mark this as resolved since @Chen_Shen provided directions to troubleshoot. Once you have a chance to try these we should reopen the discussion.

(Indeed I agree it could be serialization cost, depending on how large `x` is
