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

mmap()'d Ring Space #135

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
a0e46bd
Initial commit of the disk backed ring idea. This concept uses a mma…
jaycedowell Sep 30, 2019
f04ca10
Fixed build problems with the CPU-only version.
jaycedowell Sep 30, 2019
2d5a582
Renamed to BF_SPACE_MAPPED since that seems a little more clear. Cle…
jaycedowell Sep 30, 2019
3e057bf
Typos. Missing file. So fun.
jaycedowell Sep 30, 2019
e48d266
Added flags to user.mk and Makefile to help control where the files a…
jaycedowell Sep 30, 2019
40a5cb6
Renamed MAPPED_DIR to MAPPED_RING_DIR to make it a little more clear …
jaycedowell Sep 30, 2019
388fd8c
Added the mapped space into a couple of other places in Bifrost.
jaycedowell Oct 2, 2019
8258a1e
Added a missing BF_SPACE_MAPPED case to bfMemcpy2D.
jaycedowell Oct 2, 2019
fb2c49e
Cleaned up the typing and naming inside the MappedMgr class.
jaycedowell Oct 2, 2019
23c4751
Hacked in a few quick tests for the mapped ring space.
jaycedowell Oct 2, 2019
9ded2aa
And a few more tests that involve the mapped space.
jaycedowell Oct 2, 2019
817979e
More cleanup in the MappedMgr class.
jaycedowell Oct 2, 2019
5d0a9e3
Updated the dates in the files that were modified as part of this bra…
jaycedowell Oct 2, 2019
a74f24d
Fixed a problem specifying the MAPPED_RING_DIR path in user.mk.
jaycedowell Nov 9, 2019
120da36
Updated the version of PyPy used for testing.
jaycedowell Nov 11, 2019
f9ed7bc
Moved all of the file locking and directory managment features into f…
jaycedowell Mar 18, 2020
9a6750d
Formatting cleanup in memory.cpp.
jaycedowell Mar 18, 2020
1d755d8
Formatting cleanup in memory.h.
jaycedowell Mar 18, 2020
497057a
Clean up some compiler warnings in memory.cpp a la PR #136. Updated …
jaycedowell Mar 18, 2020
4ccffeb
Formatting cleanup.
jaycedowell Jun 9, 2020
9bbac38
Updated memory.cpp so that the BIFROST_MAPPED_RING_DIR enviroment var…
jaycedowell Jun 12, 2020
7d3aa57
Oh yeah, it's not strictly static anymore.
jaycedowell Jun 12, 2020
a489d0e
Switched to a shorter environment variable name for runtime specifica…
jaycedowell Jun 14, 2020
b3338e9
Another iteration of the environemnt variable name. Plus, a note to …
jaycedowell Jun 15, 2020
3abcc4a
Pulled in master.
jaycedowell Jul 23, 2020
e8714cf
Merge remote-tracking branch 'upstream/master' into disk-backed-ring
jaycedowell Feb 13, 2021
772acd2
Merge remote-tracking branch 'upstream/master' into disk-backed-ring
jaycedowell Apr 20, 2021
4ff90d0
Merge branch 'master' into disk-backed-ring
jaycedowell Dec 3, 2021
c4cac5a
Merge remote-tracking branch 'upstream/master' into disk-backed-ring
jaycedowell Mar 22, 2022
2567d99
Rebuild configure.
jaycedowell Mar 22, 2022
e60ae31
Rebuild configure correctly.
jaycedowell Mar 22, 2022
7e89a4c
Bad merge.
jaycedowell Mar 22, 2022
e87ad47
Include the mapped ring directory.
jaycedowell Mar 22, 2022
9e51695
Use config.h instead of CPPFLAGS.
jaycedowell Mar 22, 2022
f0cf402
Updated for BF_MAPPED_RING_DIR.
jaycedowell Mar 22, 2022
dca077f
Rebuild configure.
jaycedowell Mar 22, 2022
39c5a53
Bad fix for a bad merge.
jaycedowell Mar 22, 2022
3b5e7e9
Get the mapped ring space to work on MacOS.
jaycedowell Mar 22, 2022
d7cd7fe
No struct.
jaycedowell Mar 22, 2022
f2cd51a
Ugh, maybe I should switch to my laptop.
jaycedowell Mar 22, 2022
d2c96d0
Changed the default mapped ring directory location.
jaycedowell Mar 23, 2022
58c7281
Rebuild configure (this gets old).
jaycedowell Mar 23, 2022
2ac8b05
Fix segfault under MacOS.
jaycedowell Mar 25, 2022
5e6a6e8
Formatting cleanup.
jaycedowell Mar 25, 2022
ed7ae5d
Reorder to make MacOS happier.
jaycedowell Mar 25, 2022
84b29ba
Style.
jaycedowell Mar 25, 2022
56afbdd
Match the features of #132.
jaycedowell Mar 25, 2022
196e66b
Merge branch 'master' into disk-backed-ring
jaycedowell Mar 29, 2022
86c56e6
Updated for new fileutils function names.
jaycedowell Mar 29, 2022
159ccb4
Updated for the new 'mapped' ring space.
jaycedowell Mar 29, 2022
0693fbb
Avoid warning about catching polymorphic exception by value
league Mar 29, 2022
35f7cd6
Merge branch 'master' into disk-backed-ring
jaycedowell Apr 22, 2022
d4d3376
remove_file_glob -> remove_file.
jaycedowell Apr 22, 2022
fa08729
Octal strikes again.
jaycedowell Apr 22, 2022
0e30bb2
Ignore the libbifrost_generated.py file that is automatically created.
jaycedowell Apr 25, 2022
e6a4dea
Merge remote-tracking branch 'upstream/master' into disk-backed-ring
jaycedowell Aug 3, 2022
d768ed5
Merge remote-tracking branch 'upstream/master' into disk-backed-ring
jaycedowell Jul 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ language: python
python:
- 2.7
# PyPy versions
- pypy-5.4.1
- pypy2.7-6.0

services:
- docker
Expand Down
6 changes: 4 additions & 2 deletions python/bifrost/Space.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2016, The Bifrost Authors. All rights reserved.
# Copyright (c) 2016-2019, The Bifrost Authors. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -29,20 +29,22 @@

SPACEMAP_TO_STR = {_bf.BF_SPACE_AUTO: 'auto',
_bf.BF_SPACE_SYSTEM: 'system',
_bf.BF_SPACE_MAPPED: 'mapped',
_bf.BF_SPACE_CUDA: 'cuda',
_bf.BF_SPACE_CUDA_HOST: 'cuda_host',
_bf.BF_SPACE_CUDA_MANAGED: 'cuda_managed'}

SPACEMAP_FROM_STR = {'auto': _bf.BF_SPACE_AUTO,
'system': _bf.BF_SPACE_SYSTEM,
'mapped': _bf.BF_SPACE_MAPPED,
'cuda': _bf.BF_SPACE_CUDA,
'cuda_host': _bf.BF_SPACE_CUDA_HOST,
'cuda_managed': _bf.BF_SPACE_CUDA_MANAGED}

class Space(object):
def __init__(self, s):
if isinstance(s, basestring):
if s not in set(['auto', 'system',
if s not in set(['auto', 'system', 'mapped',
'cuda', 'cuda_host', 'cuda_managed']):
raise ValueError('Invalid space: %s' % s)
self._space = s
Expand Down
4 changes: 3 additions & 1 deletion python/bifrost/libbifrost.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2016, The Bifrost Authors. All rights reserved.
# Copyright (c) 2016-2019, The Bifrost Authors. All rights reserved.
# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -145,6 +145,7 @@ def _get(func, *args):

STRING2SPACE = {'auto': _bf.BF_SPACE_AUTO,
'system': _bf.BF_SPACE_SYSTEM,
'mapped': _bf.BF_SPACE_MAPPED,
'cuda': _bf.BF_SPACE_CUDA,
'cuda_host': _bf.BF_SPACE_CUDA_HOST,
'cuda_managed': _bf.BF_SPACE_CUDA_MANAGED}
Expand All @@ -156,6 +157,7 @@ def _string2space(s):

SPACE2STRING = {_bf.BF_SPACE_AUTO: 'auto',
_bf.BF_SPACE_SYSTEM: 'system',
_bf.BF_SPACE_MAPPED: 'mapped',
_bf.BF_SPACE_CUDA: 'cuda',
_bf.BF_SPACE_CUDA_HOST: 'cuda_host',
_bf.BF_SPACE_CUDA_MANAGED: 'cuda_managed'}
Expand Down
15 changes: 12 additions & 3 deletions python/bifrost/memory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# Copyright (c) 2016, The Bifrost Authors. All rights reserved.
# Copyright (c) 2016-2019, The Bifrost Authors. All rights reserved.
# Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -35,10 +35,19 @@ def space_accessible(space, from_spaces):
from_spaces = set(from_spaces)
if space in from_spaces:
return True
elif space in ('system', 'mapped'):
return 'system' in from_spaces \
or 'mapped' in from_spaces \
or 'cuda_host' in from_spaces \
or 'cuda_managed' in from_spaces
elif space == 'cuda_host':
return 'system' in from_spaces
return 'system' in from_spaces \
or 'mapped' in from_spaces \
or 'cuda_managed' in from_spaces
elif space == 'cuda_managed':
return 'system' in from_spaces or 'cuda' in from_spaces
return 'system' in from_spaces \
or 'mapped' in from_spaces \
or 'cuda' in from_spaces
else:
return False

Expand Down
4 changes: 2 additions & 2 deletions python/bifrost/ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def zeros(shape, dtype='f32', space=None, **kwargs):
def copy_array(dst, src):
dst_bf = asarray(dst)
src_bf = asarray(src)
if (space_accessible(dst_bf.bf.space, ['system']) and
space_accessible(src_bf.bf.space, ['system'])):
if (space_accessible(dst_bf.bf.space, ['system',]) and # TODO: Should mapped be here as well?
space_accessible(src_bf.bf.space, ['system',])):
np.copyto(dst_bf, src_bf)
else:
_check(_bf.bfArrayCopy(dst_bf.as_BFarray(),
Expand Down
1 change: 1 addition & 0 deletions python/bifrost/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def dot_graph(self, parent_graph=None):
for oring in block.orings:
space_colors = {
'system': 'orange',
'mapped': 'goldenrod',
'cuda': 'limegreen',
'cuda_host': 'deepskyblue'
}
Expand Down
4 changes: 4 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ ifdef ALIGNMENT
CPPFLAGS += -DBF_ALIGNMENT=$(ALIGNMENT)
endif

ifdef MAPPED_RING_DIR
CPPFLAGS += -DBF_MAPPED_RING_DIR='"$(patsubst %/,%,$(MAPPED_RING_DIR))"'
endif

ifdef CUDA_DEBUG
NVCCFLAGS += -G
endif
Expand Down
11 changes: 6 additions & 5 deletions src/bifrost/memory.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, The Bifrost Authors. All rights reserved.
* Copyright (c) 2016-2019, The Bifrost Authors. All rights reserved.
* Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -43,13 +43,14 @@ extern "C" {
#ifndef BF_ALIGNMENT
#define BF_ALIGNMENT 4096//512
#endif

typedef enum BFspace_ {
BF_SPACE_AUTO = 0,
BF_SPACE_SYSTEM = 1, // aligned_alloc
BF_SPACE_CUDA = 2, // cudaMalloc
BF_SPACE_CUDA_HOST = 3, // cudaHostAlloc
BF_SPACE_CUDA_MANAGED = 4 // cudaMallocManaged
BF_SPACE_MAPPED = 2, // mmapped to a file
BF_SPACE_CUDA = 3, // cudaMalloc
BF_SPACE_CUDA_HOST = 4, // cudaHostAlloc
BF_SPACE_CUDA_MANAGED = 5 // cudaMallocManaged
} BFspace;

BFstatus bfMalloc(void** ptr, BFsize size, BFspace space);
Expand Down
99 changes: 99 additions & 0 deletions src/fileutils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright (c) 2016-2020, The Bifrost Authors. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of The Bifrost Authors nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <sys/file.h> // For flock
#include <sys/stat.h> // For fstat
#include <sys/types.h> // For getpid
#include <unistd.h> // For getpid
#include <system_error>

inline void make_dir(std::string path, int perms=775) {
if( std::system(("mkdir -p "+path+" -m "+std::to_string(perms)).c_str()) ) {
throw std::runtime_error("Failed to create path: "+path);
}
}
inline void remove_all(std::string path) {
if( std::system(("rm -rf "+path).c_str()) ) {
throw std::runtime_error("Failed to remove all: "+path);
}
}
inline void remove_dir(std::string path) {
if( std::system(("rmdir "+path+" 2> /dev/null").c_str()) ) {
throw std::runtime_error("Failed to remove dir: "+path);
}
}
inline void remove_file(std::string path) {
if( std::system(("rm -f "+path).c_str()) ) {
throw std::runtime_error("Failed to remove file: "+path);
}
}
inline bool file_exists(std::string path) {
struct stat s;
return !(stat(path.c_str(), &s) == -1
&& errno == ENOENT);
}
inline bool process_exists(pid_t pid) {
struct stat s;
return !(stat(("/proc/"+std::to_string(pid)).c_str(), &s) == -1
&& errno == ENOENT);
}

inline std::string get_dirname(std::string filename) {
// TODO: This is crude, but works for our proclog use-case
return filename.substr(0, filename.find_last_of("/"));
}

class LockFile {
std::string _lockfile;
int _fd;
public:
LockFile(LockFile const& ) = delete;
LockFile& operator=(LockFile const& ) = delete;
LockFile(std::string lockfile) : _lockfile(lockfile) {
while( true ) {
_fd = open(_lockfile.c_str(), O_CREAT, 600);
flock(_fd, LOCK_EX);
struct stat fd_stat, lockfile_stat;
fstat(_fd, &fd_stat);
stat(_lockfile.c_str(), &lockfile_stat);
// Compare inodes
if( fd_stat.st_ino == lockfile_stat.st_ino ) {
// Got the lock
break;
}
close(_fd);
}
}
~LockFile() {
unlink(_lockfile.c_str());
flock(_fd, LOCK_UN);
}
};