Skip to content
Merged
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
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ readme = "README.md"
maintainers = [
{ name = "Theodore Kisner", email = "tskisner.public@gmail.com" },
]
license = "BSD-2-Clause"
license-files = ["LICENSE"]
requires-python = ">=3.10"
dependencies = [
"numpy",
]
classifiers = [
"Development Status :: 5 - Production/Stable",
"License :: OSI Approved :: BSD License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
Expand Down
2 changes: 2 additions & 0 deletions src/pshmem/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ def _register_signals():
signal.signal(signal.SIGTERM, _signal_handler)
signal.signal(signal.SIGQUIT, _signal_handler)
signal.signal(signal.SIGHUP, _signal_handler)
signal.signal(signal.SIGABRT, _signal_handler)
signal.signal(signal.SIGSEGV, _signal_handler)


# Register signal handlers on import
Expand Down
7 changes: 2 additions & 5 deletions src/pshmem/shmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .utils import (
SharedMemory,
mpi_data_type,
random_shm_key,
random_shm_name,
)


Expand Down Expand Up @@ -151,12 +151,9 @@ def __init__(self, shape, dtype, comm, comm_node=None, comm_node_rank=None):
self._name = None
self._shm_index = None
if self._rank == 0:
# Get a random 64bit integer between the supported range of keys
self._shm_index = random_shm_key()
# Name, used as global tag.
self._name = f"MPIShared_{self._shm_index}"
self._name = random_shm_name()
if self._comm is not None:
self._shm_index = self._comm.bcast(self._shm_index, root=0)
self._name = self._comm.bcast(self._name, root=0)

# Only allocate our buffers if the total number of elements is > 0
Expand Down
23 changes: 12 additions & 11 deletions src/pshmem/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# LICENSE file.
##

import random
import secrets
import sys
import threading
import time
Expand Down Expand Up @@ -96,21 +96,22 @@ def mpi_data_type(comm, dt):
return (dsize, mpitype)


def random_shm_key():
"""Get a random positive integer for using in shared memory naming.
def random_shm_name():
"""Get a random string for use in shared memory naming.

The python random library is used, and seeded with the default source
(either system time or os.urandom).
The naming convention used is similar to the syntax in
multiprocessing.shared_memory.SharedMemory.

Returns:
(int): The random integer.
(str): The random name.

"""
min_val = 0
max_val = sys.maxsize
# Seed with default source of randomness
random.seed(a=None)
return random.randint(min_val, max_val)
# Copy the conventions used in python SharedMemory
prefix = "psm_"
rnbytes = 4 # 14 byte limit on BSD, minus prefix, minus slash, minus termination
hex = secrets.token_hex(rnbytes)
name = f"{prefix}{hex}"
return name


@contextmanager
Expand Down