Skip to content

Parameters with default value zero (0) cause cryptic error message if undefined. #77

@djvergad

Description

@djvergad

Consider the following test program (named scratch/scratch-simulator.cc:

/*
 * SPDX-License-Identifier: GPL-2.0-only
 */

#include "ns3/core-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE("ScratchSimulator");

int
main(int argc, char* argv[])
{
    NS_LOG_UNCOND("Scratch Simulator");
    uint8_t my_var = 0;

    CommandLine cmd;
    cmd.AddValue("my_var",
                 "A parameter the defaults to zero (0)",
                 my_var);
    cmd.Parse(argc, argv);
    std::cout << "Running with " << (int) my_var << std::endl;
    Simulator::Run();
    Simulator::Destroy();

    return 0;
}

And the following python script:

import sem
campaign = sem.CampaignManager.new('../ns-3-optimized/', 'scratch-simulator', './tmp-results',  optimized=True,check_repo=False)
campaign.run_missing_simulations({}, runs=1)

It fails with the following error:

$ python test_sem.py 
Building ns-3: 100%|█████████████████████████████████| 2/2 [00:00<00:00, 58.40file/s]
Building ns-3: 100%|█████████████████████████████████| 2/2 [00:00<00:00, 54.63file/s]
Running simulations:   0%|                             | 0/1 [00:00<?, ?simulation/s]
Traceback (most recent call last):
  File "/home/djvergad/research/run_sims_dash_lena2/test_sem.py", line 3, in <module>
    campaign.run_missing_simulations({}, runs=1)
  File "/home/djvergad/research/run_sims_dash_lena2/venv/lib/python3.11/site-packages/sem/manager.py", line 520, in run_missing_simulations
    self.run_simulations(
  File "/home/djvergad/research/run_sims_dash_lena2/venv/lib/python3.11/site-packages/sem/manager.py", line 358, in run_simulations
    self.run_and_save_results(result_generator)
  File "/home/djvergad/research/run_sims_dash_lena2/venv/lib/python3.11/site-packages/sem/manager.py", line 368, in run_and_save_results
    for result in result_generator:
  File "/home/djvergad/research/run_sims_dash_lena2/venv/lib/python3.11/site-packages/tqdm/std.py", line 1181, in __iter__
    for obj in iterable:
  File "/home/djvergad/research/run_sims_dash_lena2/venv/lib/python3.11/site-packages/sem/parallelrunner.py", line 36, in run_simulations
    for result in pool.imap_unordered(self.launch_simulation,
  File "/usr/lib/python3.11/multiprocessing/pool.py", line 873, in next
    raise value
  File "/usr/lib/python3.11/multiprocessing/pool.py", line 125, in worker
    result = (True, func(*args, **kwds))
                    ^^^^^^^^^^^^^^^^^^^
  File "/home/djvergad/research/run_sims_dash_lena2/venv/lib/python3.11/site-packages/sem/parallelrunner.py", line 54, in launch_simulation
    return next(SimulationRunner.run_simulations(self, [parameter],
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/djvergad/research/run_sims_dash_lena2/venv/lib/python3.11/site-packages/sem/runner.py", line 342, in run_simulations
    return_code = subprocess.call(command, cwd=temp_dir,
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 389, in call
    with Popen(*popenargs, **kwargs) as p:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/subprocess.py", line 1024, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.11/subprocess.py", line 1834, in _execute_child
    self.pid = _fork_exec(
               ^^^^^^^^^^^
ValueError: embedded null byte

The real issue is that the zero parameter is treated like a null byte in python:
If you add print(f"command: {command}")
in file sem/runner.py line 341, you will see the following:

command: ['/home/djvergad/research/ns-3-optimized/build/optimized/scratch/ns3.43-scratch-simulator-optimized', '--my_var=\x00', '--RngRun=1']

which shows the problem.

The workaround is to define all zero parameters explicitly, e.g. this script works:

import sem
campaign = sem.CampaignManager.new('../ns-3-optimized/', 'scratch-simulator', './tmp-results',  optimized=True,check_repo=False)
campaign.run_missing_simulations({"my_var": 0}, runs=1)

But this is not at all obvious.

I believe that the problem should be either be fixed to allow for zero-valued parameters, or at least the error message to show more precisely the root cause, because it is not easy to realize what is causing this error message.

environment is:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions