[yt-svn] commit/yt: 4 new changesets

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Nov 29 07:40:35 PST 2017


4 new commits in yt:

https://bitbucket.org/yt_analysis/yt/commits/279ac612b033/
Changeset:   279ac612b033
User:        ngoldbaum
Date:        2017-10-23 19:53:47+00:00
Summary:     Use OpenMP detection logic from astropy_helpers
Affected #:  1 file

diff -r 89cf5553a4699f5583704674b5bb578b9a1766b4 -r 279ac612b033da93d5582a247ed23d1b5f548c4e setupext.py
--- a/setupext.py
+++ b/setupext.py
@@ -1,10 +1,52 @@
+import contextlib
+import glob
 import os
-from pkg_resources import resource_filename
 import shutil
-from subprocess import Popen, PIPE
+import subprocess
 import sys
 import tempfile
 
+from distutils import log
+from distutils.ccompiler import new_compiler
+from distutils.sysconfig import customize_compiler
+from distutils.errors import CompileError, LinkError
+from pkg_resources import resource_filename
+from subprocess import Popen, PIPE
+
+
+CCODE = """
+#include <omp.h>
+#include <stdio.h>
+int main() {
+  #pragma omp parallel
+  printf("nthreads=%d\\n", omp_get_num_threads());
+  return 0;
+}
+"""
+
+ at contextlib.contextmanager
+def stdchannel_redirected(stdchannel, dest_filename):
+    """
+    A context manager to temporarily redirect stdout or stderr
+
+    e.g.:
+
+    with stdchannel_redirected(sys.stderr, os.devnull):
+        if compiler.has_function('clock_gettime', libraries=['rt']):
+            libraries.append('rt')
+    """
+
+    try:
+        oldstdchannel = os.dup(stdchannel.fileno())
+        dest_file = open(dest_filename, 'w')
+        os.dup2(dest_file.fileno(), stdchannel.fileno())
+
+        yield
+    finally:
+        if oldstdchannel is not None:
+            os.dup2(oldstdchannel, stdchannel.fileno())
+        if dest_file is not None:
+            dest_file.close()
 
 def check_for_openmp():
     """Returns True if local setup supports OpenMP, False otherwise"""
@@ -14,51 +56,65 @@
         return False
 
     # Create a temporary directory
-    tmpdir = tempfile.mkdtemp()
-    curdir = os.getcwd()
-    exit_code = 1
+    ccompiler = new_compiler()
+    customize_compiler(ccompiler)
+
+    tmp_dir = tempfile.mkdtemp()
+    start_dir = os.path.abspath('.')
+
+    if os.name == 'nt':
+        # TODO: make this work with mingw
+        # AFAICS there's no easy way to get the compiler distutils
+        # will be using until compilation actually happens
+        compile_flag = '-openmp'
+        link_flag = ''
+    else:
+        compile_flag = '-fopenmp'
+        link_flag = '-fopenmp'
 
     try:
-        os.chdir(tmpdir)
+        os.chdir(tmp_dir)
+
+        with open('test_openmp.c', 'w') as f:
+            f.write(CCODE)
+
+        os.mkdir('objects')
 
-        # Get compiler invocation
-        compiler = os.getenv('CC', 'cc')
-        compiler = compiler.split(' ')
+        # Compile, link, and run test program
+        with stdchannel_redirected(sys.stderr, os.devnull):
+            ccompiler.compile(['test_openmp.c'], output_dir='objects',
+                              extra_postargs=[compile_flag])
+            ccompiler.link_executable(
+                glob.glob(os.path.join('objects', '*')), 'test_openmp',
+                extra_postargs=[link_flag])
+            output = subprocess.check_output('./test_openmp').decode(
+                sys.stdout.encoding or 'utf-8').splitlines()
 
-        # Attempt to compile a test script.
-        # See http://openmp.org/wp/openmp-compilers/
-        filename = r'test.c'
-        file = open(filename, 'wt', 1)
-        file.write(
-            "#include <omp.h>\n"
-            "#include <stdio.h>\n"
-            "int main() {\n"
-            "#pragma omp parallel\n"
-            "printf(\"Hello from thread %d, nthreads %d\\n\", omp_get_thread_num(), omp_get_num_threads());\n"
-            "}"
-        )
-        file.flush()
-        p = Popen(compiler + ['-fopenmp', filename],
-                  stdin=PIPE, stdout=PIPE, stderr=PIPE)
-        output, err = p.communicate()
-        exit_code = p.returncode
-        
-        if exit_code != 0:
-            print("Compilation of OpenMP test code failed with the error: ")
-            print(err.decode('utf8'))
-            print("Disabling OpenMP support. ")
+        if 'nthreads=' in output[0]:
+            nthreads = int(output[0].strip().split('=')[1])
+            if len(output) == nthreads:
+                using_openmp = True
+            else:
+                log.warn("Unexpected number of lines from output of test "
+                         "OpenMP program (output was {0})".format(output))
+                using_openmp = False
+        else:
+            log.warn("Unexpected output from test OpenMP "
+                     "program (output was {0})".format(output))
+            using_openmp = False
 
-        # Clean up
-        file.close()
-    except OSError:
-        print("check_for_openmp() could not find your C compiler. "
-              "Attempted to use '%s'. " % compiler)
-        return False
+    except (CompileError, LinkError):
+        using_openmp = False
     finally:
-        os.chdir(curdir)
-        shutil.rmtree(tmpdir)
+        os.chdir(start_dir)
 
-    return exit_code == 0
+    if using_openmp:
+        log.warn("Using OpenMP to compile parallel extensions")
+    else:
+        log.warn("Unable to compile OpenMP test program so Cython\n"
+                 "extensions will be compiled without parallel support")
+
+    return using_openmp
 
 
 def check_for_pyembree():


https://bitbucket.org/yt_analysis/yt/commits/5ae4793484dd/
Changeset:   5ae4793484dd
User:        ngoldbaum
Date:        2017-10-23 19:54:42+00:00
Summary:     use distutils logger instead of print statements in setupext
Affected #:  1 file

diff -r 279ac612b033da93d5582a247ed23d1b5f548c4e -r 5ae4793484dd15d21a9e4c11ed6cc3d905d0beb7 setupext.py
--- a/setupext.py
+++ b/setupext.py
@@ -180,17 +180,18 @@
         exit_code = p.returncode
 
         if exit_code != 0:
-            print("Pyembree is installed, but I could not compile Embree test code.")
-            print("The error message was: ")
-            print(err)
-            print(fail_msg)
+            log.warn("Pyembree is installed, but I could not compile Embree "
+                     "test code.")
+            log.warn("The error message was: ")
+            log.warn(err)
+            log.warn(fail_msg)
 
         # Clean up
         file.close()
 
     except OSError:
-        print("read_embree_location() could not find your C compiler. "
-              "Attempted to use '%s'. " % compiler)
+        log.warn("read_embree_location() could not find your C compiler. "
+                 "Attempted to use '%s'. " % compiler)
         return False
 
     finally:


https://bitbucket.org/yt_analysis/yt/commits/3f07fc189c3c/
Changeset:   3f07fc189c3c
User:        ngoldbaum
Date:        2017-10-23 19:57:35+00:00
Summary:     add notes about where the code came from
Affected #:  1 file

diff -r 5ae4793484dd15d21a9e4c11ed6cc3d905d0beb7 -r 3f07fc189c3c221095cbd24f71313d0b1dcdcd5e setupext.py
--- a/setupext.py
+++ b/setupext.py
@@ -34,6 +34,8 @@
     with stdchannel_redirected(sys.stderr, os.devnull):
         if compiler.has_function('clock_gettime', libraries=['rt']):
             libraries.append('rt')
+
+    Code adapted from https://stackoverflow.com/a/17752455/1382869
     """
 
     try:
@@ -49,7 +51,11 @@
             dest_file.close()
 
 def check_for_openmp():
-    """Returns True if local setup supports OpenMP, False otherwise"""
+    """Returns True if local setup supports OpenMP, False otherwise
+
+    Code adapted from astropy_helpers, originally written by Tom 
+    Robitaille and Curtis McCully.
+    """
 
     # See https://bugs.python.org/issue25150
     if sys.version_info[:3] == (3, 5, 0) or os.name == 'nt':


https://bitbucket.org/yt_analysis/yt/commits/f6279767c26e/
Changeset:   f6279767c26e
User:        xarthisius
Date:        2017-11-29 15:40:19+00:00
Summary:     Merge pull request #1591 from ngoldbaum/omp-detect

Improve OpenMP detection
Affected #:  1 file

diff -r 0e28d5ea5938979f536edf8f55dbc078f3406102 -r f6279767c26ef709cd0a53611867ab59621a3faf setupext.py
--- a/setupext.py
+++ b/setupext.py
@@ -1,64 +1,126 @@
+import contextlib
+import glob
 import os
-from pkg_resources import resource_filename
 import shutil
-from subprocess import Popen, PIPE
+import subprocess
 import sys
 import tempfile
 
+from distutils import log
+from distutils.ccompiler import new_compiler
+from distutils.sysconfig import customize_compiler
+from distutils.errors import CompileError, LinkError
+from pkg_resources import resource_filename
+from subprocess import Popen, PIPE
+
+
+CCODE = """
+#include <omp.h>
+#include <stdio.h>
+int main() {
+  #pragma omp parallel
+  printf("nthreads=%d\\n", omp_get_num_threads());
+  return 0;
+}
+"""
+
+ at contextlib.contextmanager
+def stdchannel_redirected(stdchannel, dest_filename):
+    """
+    A context manager to temporarily redirect stdout or stderr
+
+    e.g.:
+
+    with stdchannel_redirected(sys.stderr, os.devnull):
+        if compiler.has_function('clock_gettime', libraries=['rt']):
+            libraries.append('rt')
+
+    Code adapted from https://stackoverflow.com/a/17752455/1382869
+    """
+
+    try:
+        oldstdchannel = os.dup(stdchannel.fileno())
+        dest_file = open(dest_filename, 'w')
+        os.dup2(dest_file.fileno(), stdchannel.fileno())
+
+        yield
+    finally:
+        if oldstdchannel is not None:
+            os.dup2(oldstdchannel, stdchannel.fileno())
+        if dest_file is not None:
+            dest_file.close()
 
 def check_for_openmp():
-    """Returns True if local setup supports OpenMP, False otherwise"""
+    """Returns True if local setup supports OpenMP, False otherwise
+
+    Code adapted from astropy_helpers, originally written by Tom 
+    Robitaille and Curtis McCully.
+    """
 
     # See https://bugs.python.org/issue25150
     if sys.version_info[:3] == (3, 5, 0) or os.name == 'nt':
         return False
 
     # Create a temporary directory
-    tmpdir = tempfile.mkdtemp()
-    curdir = os.getcwd()
-    exit_code = 1
+    ccompiler = new_compiler()
+    customize_compiler(ccompiler)
+
+    tmp_dir = tempfile.mkdtemp()
+    start_dir = os.path.abspath('.')
+
+    if os.name == 'nt':
+        # TODO: make this work with mingw
+        # AFAICS there's no easy way to get the compiler distutils
+        # will be using until compilation actually happens
+        compile_flag = '-openmp'
+        link_flag = ''
+    else:
+        compile_flag = '-fopenmp'
+        link_flag = '-fopenmp'
 
     try:
-        os.chdir(tmpdir)
+        os.chdir(tmp_dir)
+
+        with open('test_openmp.c', 'w') as f:
+            f.write(CCODE)
+
+        os.mkdir('objects')
 
-        # Get compiler invocation
-        compiler = os.getenv('CC', 'cc')
-        compiler = compiler.split(' ')
+        # Compile, link, and run test program
+        with stdchannel_redirected(sys.stderr, os.devnull):
+            ccompiler.compile(['test_openmp.c'], output_dir='objects',
+                              extra_postargs=[compile_flag])
+            ccompiler.link_executable(
+                glob.glob(os.path.join('objects', '*')), 'test_openmp',
+                extra_postargs=[link_flag])
+            output = subprocess.check_output('./test_openmp').decode(
+                sys.stdout.encoding or 'utf-8').splitlines()
 
-        # Attempt to compile a test script.
-        # See http://openmp.org/wp/openmp-compilers/
-        filename = r'test.c'
-        file = open(filename, 'wt', 1)
-        file.write(
-            "#include <omp.h>\n"
-            "#include <stdio.h>\n"
-            "int main() {\n"
-            "#pragma omp parallel\n"
-            "printf(\"Hello from thread %d, nthreads %d\\n\", omp_get_thread_num(), omp_get_num_threads());\n"
-            "}"
-        )
-        file.flush()
-        p = Popen(compiler + ['-fopenmp', filename],
-                  stdin=PIPE, stdout=PIPE, stderr=PIPE)
-        output, err = p.communicate()
-        exit_code = p.returncode
-        
-        if exit_code != 0:
-            print("Compilation of OpenMP test code failed with the error: ")
-            print(err.decode('utf8'))
-            print("Disabling OpenMP support. ")
+        if 'nthreads=' in output[0]:
+            nthreads = int(output[0].strip().split('=')[1])
+            if len(output) == nthreads:
+                using_openmp = True
+            else:
+                log.warn("Unexpected number of lines from output of test "
+                         "OpenMP program (output was {0})".format(output))
+                using_openmp = False
+        else:
+            log.warn("Unexpected output from test OpenMP "
+                     "program (output was {0})".format(output))
+            using_openmp = False
 
-        # Clean up
-        file.close()
-    except OSError:
-        print("check_for_openmp() could not find your C compiler. "
-              "Attempted to use '%s'. " % compiler)
-        return False
+    except (CompileError, LinkError):
+        using_openmp = False
     finally:
-        os.chdir(curdir)
-        shutil.rmtree(tmpdir)
+        os.chdir(start_dir)
 
-    return exit_code == 0
+    if using_openmp:
+        log.warn("Using OpenMP to compile parallel extensions")
+    else:
+        log.warn("Unable to compile OpenMP test program so Cython\n"
+                 "extensions will be compiled without parallel support")
+
+    return using_openmp
 
 
 def check_for_pyembree():
@@ -124,17 +186,18 @@
         exit_code = p.returncode
 
         if exit_code != 0:
-            print("Pyembree is installed, but I could not compile Embree test code.")
-            print("The error message was: ")
-            print(err)
-            print(fail_msg)
+            log.warn("Pyembree is installed, but I could not compile Embree "
+                     "test code.")
+            log.warn("The error message was: ")
+            log.warn(err)
+            log.warn(fail_msg)
 
         # Clean up
         file.close()
 
     except OSError:
-        print("read_embree_location() could not find your C compiler. "
-              "Attempted to use '%s'. " % compiler)
+        log.warn("read_embree_location() could not find your C compiler. "
+                 "Attempted to use '%s'. " % compiler)
         return False
 
     finally:

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.



More information about the yt-svn mailing list