Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience extensions on old API #101

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 155 additions & 28 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,40 +1,167 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

.vscode
build
# Prerequisites
*.d
# C extensions
*.so

# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Precompiled Headers
*.gch
*.pch
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# Fortran module files
*.mod
*.smod
# pytype static type analyzer
.pytype/

# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Cython debug symbols
cython_debug/

# Executables
*.exe
*.out
*.app
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# Custom
py_interface/build/
py_interface/dist/
py_interface/ns3_ai.egg-info/
cmake-build-debug/
examples/elegante/
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
set(name ns3-ai)

set(source_files
model/memory-pool.cc
)

set(header_files
model/memory-pool.h
model/train-var.h
model/ns3-ai-rl.h
model/ns3-ai-dl.h
)

set(libraries_to_link
libcore
)

build_lib(
LIBNAME ${name}
SOURCE_FILES ${source_files}
HEADER_FILES ${header_files}
LIBRARIES_TO_LINK ${libraries_to_link}
)

2 changes: 1 addition & 1 deletion examples/rl-tcp/run_rl_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def learn(self, ):

if args.use_rl:
dqn = DQN()
exp = Experiment(1234, 4096, 'rl-tcp', '../../')
exp = Experiment(1234, 4096, 'rl-tcp/', '../../')
exp.run(show_output=1)
try:
while not var.isFinish():
Expand Down
36 changes: 36 additions & 0 deletions examples/run_example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# validity checks
if [[ "$(dirname "$0")" != "." ]]; then
echo '[bash ] please change to the folder containing this script before execution. Exiting...'
exit 1
fi

TARGET=$1
if [ ! -f $TARGET ]; then
echo "[bash ] invalid example script file name. Exiting..."
exit 1
fi

# parsing input script path into example folder to be copied and the script file name.
IFS='/'; arrIN=($TARGET); unset IFS;
EXAMPLE_DIR=${arrIN[0]}
RUN_FILE=${arrIN[1]}

# copy latest version of the example to the scratch folder, assuming it lies 3 levels above the examples folder.
SCRATCH_DIR=../../../scratch
TARGET_DIR=$SCRATCH_DIR/$EXAMPLE_DIR
if [[ -d $TARGET_DIR ]]
then
echo "[bash ] deleting existing example in scratch folder"
rm -rf $TARGET_DIR
fi
echo "[bash ] copying example to scratch folder"
cp -r $EXAMPLE_DIR $SCRATCH_DIR
echo "[bash ] copied example to scratch folder"

# cd there and run the script with the python script arguments
pushd $TARGET_DIR > /dev/null || exit
shift # removes the first argument from "$@" (which is the python file accessible via $TARGET)
echo "[bash ] running python entry script"
python $RUN_FILE "$@"
popd > /dev/null || exit
bash ../freeshm.sh
Empty file modified freeshm.sh
100644 → 100755
Empty file.
4 changes: 4 additions & 0 deletions model/memory-pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,21 @@ void SharedMemoryPool::ReleaseMemory (uint16_t id) //Should register first
{
NS_LOG_FUNCTION (this << "ID: " << id);
SharedMemoryLockable *info = m_memoryLocker[id];
NS_LOG_INFO ("memory version before: " << +info->version << " , nextVersion: " << +info->nextVersion);
while (!__sync_bool_compare_and_swap(&info->version, info->nextVersion - (uint8_t)1, info->nextVersion))
ShmYield();
NS_LOG_INFO ("memory version after: " << +info->version << " , nextVersion: " << +info->nextVersion);
}

void
SharedMemoryPool::ReleaseMemoryAndRollback (uint16_t id)
{
NS_LOG_FUNCTION (this << "ID: " << id);
SharedMemoryLockable *info = m_memoryLocker[id];
NS_LOG_INFO ("memory version before: " << +info->version << " , nextVersion: " << +info->nextVersion);
while (!__sync_bool_compare_and_swap(&info->nextVersion, info->version + (uint8_t)1, info->version))
ShmYield();
NS_LOG_INFO ("memory version after: " << +info->version << " , nextVersion: " << +info->nextVersion);
}

uint8_t
Expand Down
12 changes: 12 additions & 0 deletions model/ns3-ai-rl.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ class Ns3AIRL : public SimpleRefCount<Ns3AIRL<EnvType, ActionType, SimInfoType>>
* \return finish flag.
*/
bool GetIsFinish(void);

/**
* TODO comment
* @return
*/
uint8_t GetMemVersion(void);
};

template <typename EnvType, typename ActionType, typename SimInfoType>
Expand Down Expand Up @@ -546,4 +552,10 @@ bool Ns3AIRL<EnvType, ActionType, SimInfoType>::GetIsFinish(void)
return *m_isFinish;
}

template <typename EnvType, typename ActionType, typename SimInfoType>
uint8_t Ns3AIRL<EnvType, ActionType, SimInfoType>::GetMemVersion(void)
{
return SharedMemoryPool::Get()->GetMemoryVersion(m_id);
}

} // namespace ns3
7 changes: 4 additions & 3 deletions py_interface/memory-pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void *GetMemory(uint16_t id, uint32_t size)
{
if (id >= MAX_ID)
{
PyErr_SetString(PyExc_RuntimeError, "Id out of range");
PyErr_SetString(PyExc_RuntimeError, "Id out of range (upper exclusive limit is %u)");
return NULL;
}
CtrlInfoLock();
Expand All @@ -80,7 +80,7 @@ static void *GetMemory(uint16_t id, uint32_t size)
if (gMemoryCtrlInfo[gCurCtrlInfo->id] != 0)
{
CtrlInfoUnlock();
PyErr_Format(PyExc_RuntimeError, "Id %u has been used", gCurCtrlInfo->id);
PyErr_Format(PyExc_RuntimeError, "Id %u has been used already", gCurCtrlInfo->id);
return NULL;
}
gMemoryCtrlInfo[gCurCtrlInfo->id] = gCurCtrlInfo;
Expand All @@ -92,7 +92,8 @@ static void *GetMemory(uint16_t id, uint32_t size)
if (size != gMemoryCtrlInfo[id]->size)
{
CtrlInfoUnlock();
PyErr_Format(PyExc_RuntimeError, "Size of memory error(%u %u)", size, gMemoryCtrlInfo[id]->size);
PyErr_Format(PyExc_RuntimeError, "Size mismatch in (requested, allocated) memory: (%u %u)",
size, gMemoryCtrlInfo[id]->size);
return NULL;
}
CtrlInfoUnlock();
Expand Down
Loading