Coverage for rhodent/utils/env.py: 74%
19 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-08-01 16:57 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-08-01 16:57 +0000
1from __future__ import annotations
4from os import getenv
7def rhodent_getenv(variable) -> str:
8 """ Get value of environment variable, or default.
10 Possible environment variables are:
12 - `RHODENT_RESPONSE_MAX_MEM` - Value in units of MiB. When constructing
13 response, try to limit memory usage below this value.
14 - `RHODENT_RESPONSE_MAX_MEM_PER_RANK` - Value in units of MiB.
15 Ignored if `RHODENT_RESPONSE_MAX_MEM` is set.
16 - `RHODENT_REDISTRIBUTE_MAXSIZE` - Maximal number of elements in single redistribute call.
17 Larger arrays are split.
19 Parameters
20 ----------
21 variable
22 Name of variable, without the ``'RHODENT_'`` prefix.
24 Returns
25 -------
26 Value of variable as string.
27 """
29 defaults = {'REDISTRIBUTE_MAXSIZE': '1e7',
30 'RESPONSE_MAX_MEM': '',
31 'RESPONSE_MAX_MEM_PER_RANK': '1000',
32 }
34 if variable not in defaults.keys():
35 allowed = '\n'.join([f' RHODENT_{var}' for var in defaults.keys()])
36 raise ValueError(f'Environment variable RHODENT_{variable} is unknown. '
37 f'Allowed variables are:\n{allowed}')
39 return getenv(f'RHODENT_{variable}', defaults[variable])
42def get_float(variable) -> float:
43 strvalue = rhodent_getenv(variable)
44 try:
45 value = float(strvalue)
46 except ValueError:
47 raise ValueError(f'Expected environment variable RHODENT_{variable} to '
48 f'be castable to float. Value is {strvalue!r}.')
50 return value
53def get_response_max_mem(comm_size: int):
54 if rhodent_getenv('RESPONSE_MAX_MEM') == '':
55 return get_float('RESPONSE_MAX_MEM_PER_RANK') * comm_size
57 return get_float('RESPONSE_MAX_MEM')