# Get number of cpus on allocated job via environment variable

**URL:** <https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843>\
**Category:** Users/Site Administrators\
**Created:** [November 8, 2021, 2:38am UTC](https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843 "2021-11-08T02:38:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hollow\_op](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@hollow\_op](https://community.openpbs.org/u/hollow_op)\
**Post date:** [November 8, 2021, 2:38am UTC](https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843/1 "2021-11-08T02:38:02Z")

</div>

In slurm we can pass an environment variable to a job to get the number of cpus allocated to the job.

> **SLURM\_JOB\_CPUS\_PER\_NODE**
> 
> Count of CPUs available to the job on the nodes in the allocation, using the format _CPU\_count_ [(x _number\_of\_nodes_ )][, _CPU\_count_ [(x _number\_of\_nodes_ )] …]. For example: SLURM\_JOB\_CPUS\_PER\_NODE=‘72(x2),36’ indicates that on the first and second nodes (as listed by SLURM\_JOB\_NODELIST) the allocation has 72 CPUs, while the third node has 36 CPUs. **NOTE** : The **select/linear** plugin allocates entire nodes to jobs, so the value indicates the total count of CPUs on allocated nodes. The **select/cons\_res** and **select/cons\_tres** plugins allocate individual CPUs to jobs, so this number indicates the number of CPUs allocated to the job.

Is there a similar env in pbspro ? Thanks

---

<div class="post-metadata">

**Author:** ![eviatar\_yadai\_KI](https://avatars.discourse-cdn.com/v4/letter/e/eb8c5e/32.png) [@eviatar\_yadai\_KI](https://community.openpbs.org/u/eviatar_yadai_KI)\
**Post date:** [November 8, 2021, 7:54am UTC](https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843/2 "2021-11-08T07:54:08Z")

</div>

Do you need only an environment variable? because there are alternative ways.  
At python I use for example:  
**import psutil**  
**number\_of\_cpus = len(psutil.Process().cpu\_affinity())**

Note you can also watch which PCU ID is assigned.  
I think there is no such PBS environment variable.

---

<div class="post-metadata">

**Author:** ![fnevgeny](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/fnevgeny/32/375_2.png) [@fnevgeny](https://community.openpbs.org/u/fnevgeny)\
**Post date:** [November 11, 2021, 4:20pm UTC](https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843/3 "2021-11-11T16:20:48Z")

</div>

You can use `OMP_NUM_THREADS`, which is pretty standard for multithreading applications.

---

<div class="post-metadata">

**Author:** ![hollow\_op](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@hollow\_op](https://community.openpbs.org/u/hollow_op)\
**Post date:** [November 12, 2021, 2:48am UTC](https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843/4 "2021-11-12T02:48:17Z")

</div>

thanks for your replies. I also found out `${N_CPUS}`, thus setting to `export OMP_NUM_THREADS=${NCPUS}`. I’m curious about how is the standard to way to set memory limitation as well ? thanks

---

<div class="post-metadata">

**Author:** ![nlouis](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@nlouis](https://community.openpbs.org/u/nlouis)\
**Post date:** [November 15, 2021, 11:59am UTC](https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843/5 "2021-11-15T11:59:33Z")

</div>

Hi,

you can use a hook to modify the job’s environment at submission time.  
This is what I used to set some values as memory and cpus:

```auto
import pbs
 
E = pbs.event()
J = E.job
R = J.Resource_List
V = J.Variable_List
if R["select"] != None:
    sel = repr(R["select"])
    ncpus = 0
    mem = pbs.size(0)
 
    chunks = sel.split("+")
    for chunk in chunks:
        mult = 1
        for c in chunk.split(":"):
            kv = c.split("=")
            if len(kv) == 1:
                mult = int(kv[0])
            elif len(kv) == 2:
                if kv[0] == "ncpus":
                    ncpus += mult * int(kv[1])
                elif kv[0] == "mem":
                    size = pbs.size_to_kbytes(pbs.size(kv[1]))*1024
                    mem += pbs.size(mult*int(size))

if ncpus == 0:
    if R["ncpus"] != None:
        ncpus = int(R["ncpus"])
 
if pbs.size_to_kbytes(mem)*1024 == 0:
    if R["mem"] != None:
        m = pbs.size(R["mem"])
        mkb = pbs.size_to_kbytes(m)*1024
        mem = pbs.size(mkb)
 
 

V["PBS_MEM"] = mem
V["PBS_NPROCS"] = ncpus
 
print(V)
E.accept()

```

V is the list of variables exported to the job’s env.

---

<div class="post-metadata">

**Author:** ![fnevgeny](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.openpbs.org/fnevgeny/32/375_2.png) [@fnevgeny](https://community.openpbs.org/u/fnevgeny)\
**Post date:** [November 18, 2021, 2:43pm UTC](https://community.openpbs.org/t/get-number-of-cpus-on-allocated-job-via-environment-variable/2843/6 "2021-11-18T14:43:52Z")

</div>

> thus setting to `export OMP_NUM_THREADS=${NCPUS}` .

Hmm, `OMP_NUM_THREADS` should already be set by PBS. At least in my case, it is.
