-
Notifications
You must be signed in to change notification settings - Fork 30
Make dppl rt threadlocal #9
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5a5da04
improve comment
diptorupd ec5c5d4
Make the runtime thread local and expose the API using global preboun…
diptorupd 22041b7
Import setuptools before sython.
diptorupd 79508a2
Import setuptools before cython.
diptorupd c726395
Merge branch 'make_dppl_rt_threadlocal' of github.com:diptorupd/pydpp…
diptorupd fb468df
Ignore PyDev project files.
diptorupd 8318d44
Removes the flag to report if inside a dppl device_context.
diptorupd 0b69284
Merge branch 'master' of github.com:IntelPython/pydppl
diptorupd c80e5ab
Merge branch 'master' of github.com:IntelPython/pydppl
diptorupd 364d3f0
Merge branch 'master' into make_dppl_rt_threadlocal
diptorupd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
##===---------- t_is_in_dppl_ctxt.py - dppl -------------*- Python -*-----===## | ||
## | ||
## Python Data Parallel Processing Library (PyDPPL) | ||
## | ||
## Copyright 2020 Intel Corporation | ||
## | ||
## Licensed under the Apache License, Version 2.0 (the "License"); | ||
## you may not use this file except in compliance with the License. | ||
## You may obtain a copy of the License at | ||
## | ||
## http://www.apache.org/licenses/LICENSE-2.0 | ||
## | ||
## Unless required by applicable law or agreed to in writing, software | ||
## distributed under the License is distributed on an "AS IS" BASIS, | ||
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
## See the License for the specific language governing permissions and | ||
## limitations under the License. | ||
## | ||
##===----------------------------------------------------------------------===## | ||
### | ||
### \file | ||
### This file has unit test cases to check the dump functions provided by dppl. | ||
##===----------------------------------------------------------------------===## | ||
|
||
from contextlib import contextmanager | ||
import ctypes | ||
import dppl | ||
import io | ||
import os, sys | ||
import tempfile | ||
import unittest | ||
|
||
|
||
libc = ctypes.CDLL(None) | ||
c_stdout = ctypes.c_void_p.in_dll(libc, 'stdout') | ||
|
||
# Sourced from | ||
# https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/ | ||
@contextmanager | ||
def stdout_redirector (stream): | ||
# The original fd stdout points to. Usually 1 on POSIX systems. | ||
original_stdout_fd = sys.stdout.fileno() | ||
|
||
def _redirect_stdout(to_fd): | ||
"""Redirect stdout to the given file descriptor.""" | ||
# Flush the C-level buffer stdout | ||
libc.fflush(c_stdout) | ||
# Flush and close sys.stdout - also closes the file descriptor (fd) | ||
sys.stdout.close() | ||
# Make original_stdout_fd point to the same file as to_fd | ||
os.dup2(to_fd, original_stdout_fd) | ||
# Create a new sys.stdout that points to the redirected fd | ||
sys.stdout = io.TextIOWrapper(os.fdopen(original_stdout_fd, 'wb')) | ||
|
||
# Save a copy of the original stdout fd in saved_stdout_fd | ||
saved_stdout_fd = os.dup(original_stdout_fd) | ||
try: | ||
# Create a temporary file and redirect stdout to it | ||
tfile = tempfile.TemporaryFile(mode='w+b') | ||
_redirect_stdout(tfile.fileno()) | ||
# Yield to caller, then redirect stdout back to the saved fd | ||
yield | ||
_redirect_stdout(saved_stdout_fd) | ||
# Copy contents of temporary file to the given stream | ||
tfile.flush() | ||
tfile.seek(0, io.SEEK_SET) | ||
if stream: | ||
stream.write(tfile.read()) | ||
finally: | ||
tfile.close() | ||
os.close(saved_stdout_fd) | ||
|
||
|
||
class TestDPPLDumpFns(unittest.TestCase): | ||
|
||
def test_dppl_dump_runtime(self): | ||
with stdout_redirector(None): | ||
self.assertEqual(dppl.dump(), 0) | ||
|
||
def test_dppl_dump_queue_info(self): | ||
with stdout_redirector(None): | ||
q = dppl.get_current_queue() | ||
self.assertEqual(dppl.dump_queue_info(q), 0) | ||
|
||
|
||
suite = unittest.TestLoader().loadTestsFromTestCase(TestDPPLDumpFns) | ||
unittest.TextTestRunner(verbosity=2).run(suite) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
##===---------- t_is_in_dppl_ctxt.py - dppl -------------*- Python -*-----===## | ||
## | ||
## Python Data Parallel Processing Library (PyDPPL) | ||
## | ||
## Copyright 2020 Intel Corporation | ||
## | ||
## Licensed under the Apache License, Version 2.0 (the "License"); | ||
## you may not use this file except in compliance with the License. | ||
## You may obtain a copy of the License at | ||
## | ||
## http://www.apache.org/licenses/LICENSE-2.0 | ||
## | ||
## Unless required by applicable law or agreed to in writing, software | ||
## distributed under the License is distributed on an "AS IS" BASIS, | ||
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
## See the License for the specific language governing permissions and | ||
## limitations under the License. | ||
## | ||
##===----------------------------------------------------------------------===## | ||
### | ||
### \file | ||
### This file has unit test cases to check the value returned by the | ||
### dppl.is_in_dppl_ctxt function. | ||
##===----------------------------------------------------------------------===## | ||
|
||
import dppl | ||
import unittest | ||
|
||
class TestDPPLIsInDPPLCtxt (unittest.TestCase): | ||
|
||
def test_is_in_dppl_ctxt_outside_device_ctxt (self): | ||
self.assertFalse(dppl.is_in_dppl_ctxt()) | ||
|
||
def test_is_in_dppl_ctxt_inside_device_ctxt (self): | ||
with dppl.device_context(dppl.device_type.gpu): | ||
self.assertTrue(dppl.is_in_dppl_ctxt()) | ||
|
||
def test_is_in_dppl_ctxt_inside_nested_device_ctxt (self): | ||
with dppl.device_context(dppl.device_type.cpu): | ||
with dppl.device_context(dppl.device_type.gpu): | ||
self.assertTrue(dppl.is_in_dppl_ctxt()) | ||
self.assertTrue(dppl.is_in_dppl_ctxt()) | ||
self.assertFalse(dppl.is_in_dppl_ctxt()) | ||
|
||
suite = unittest.TestLoader().loadTestsFromTestCase(TestDPPLIsInDPPLCtxt) | ||
unittest.TextTestRunner(verbosity=2).run(suite) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
##===---------- t_ocldrv_dump_fns.py - dppl.ocldrv ------*- Python -*-----===## | ||
## | ||
## Python Data Parallel Processing Library (PyDPPL) | ||
## | ||
## Copyright 2020 Intel Corporation | ||
## | ||
## Licensed under the Apache License, Version 2.0 (the "License"); | ||
## you may not use this file except in compliance with the License. | ||
## You may obtain a copy of the License at | ||
## | ||
## http://www.apache.org/licenses/LICENSE-2.0 | ||
## | ||
## Unless required by applicable law or agreed to in writing, software | ||
## distributed under the License is distributed on an "AS IS" BASIS, | ||
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
## See the License for the specific language governing permissions and | ||
## limitations under the License. | ||
## | ||
##===----------------------------------------------------------------------===## | ||
### | ||
### \file | ||
### This file has unit test cases to check the dump functions in dppl.ocldrv. | ||
##===----------------------------------------------------------------------===## | ||
|
||
from contextlib import contextmanager | ||
import ctypes | ||
import io | ||
import os, sys | ||
import tempfile | ||
import unittest | ||
|
||
import dppl.ocldrv as drv | ||
|
||
libc = ctypes.CDLL(None) | ||
c_stdout = ctypes.c_void_p.in_dll(libc, 'stdout') | ||
|
||
# Sourced from | ||
# https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/ | ||
@contextmanager | ||
def stdout_redirector (stream): | ||
# The original fd stdout points to. Usually 1 on POSIX systems. | ||
original_stdout_fd = sys.stdout.fileno() | ||
|
||
def _redirect_stdout(to_fd): | ||
"""Redirect stdout to the given file descriptor.""" | ||
# Flush the C-level buffer stdout | ||
libc.fflush(c_stdout) | ||
# Flush and close sys.stdout - also closes the file descriptor (fd) | ||
sys.stdout.close() | ||
# Make original_stdout_fd point to the same file as to_fd | ||
os.dup2(to_fd, original_stdout_fd) | ||
# Create a new sys.stdout that points to the redirected fd | ||
sys.stdout = io.TextIOWrapper(os.fdopen(original_stdout_fd, 'wb')) | ||
|
||
# Save a copy of the original stdout fd in saved_stdout_fd | ||
saved_stdout_fd = os.dup(original_stdout_fd) | ||
try: | ||
# Create a temporary file and redirect stdout to it | ||
tfile = tempfile.TemporaryFile(mode='w+b') | ||
_redirect_stdout(tfile.fileno()) | ||
# Yield to caller, then redirect stdout back to the saved fd | ||
yield | ||
_redirect_stdout(saved_stdout_fd) | ||
# Copy contents of temporary file to the given stream | ||
tfile.flush() | ||
tfile.seek(0, io.SEEK_SET) | ||
if stream: | ||
stream.write(tfile.read()) | ||
finally: | ||
tfile.close() | ||
os.close(saved_stdout_fd) | ||
|
||
|
||
class TestOcldrvDumpFns (unittest.TestCase): | ||
|
||
def test_dppl_ocldrv_dump_runtime(self): | ||
with stdout_redirector(None): | ||
self.assertEqual(drv.runtime.dump(), 0) | ||
|
||
|
||
suite = unittest.TestLoader().loadTestsFromTestCase(TestOcldrvDumpFns) | ||
unittest.TextTestRunner(verbosity=2).run(suite) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the entire runtime thread-local? The only thing which is thread-local is the stack of contexts. Making shared data thread-local is calling for trouble.
I think the runtime class should encapsulate the thread-local business and return the thread-local values in its member functions like get_current_queue. Maybe this needs to be done in C++ (e.g. your std::deque be thread-local).