Esempio n. 1
0
def Exec(opencl_environment: env.OpenCLEnvironment,
         *opts,
         timeout_seconds: int = 60,
         env: typing.Dict[str, str] = None) -> subprocess.Popen:
    """Execute cl_launcher.

  This creates a Popen process, executes it, and sets the stdout and stderr
  attributes to the process output.

  Args:
    opencl_environment: The OpenCL environment to execute cl_launcher with.
    opts: A list of arguments to pass to the cl_launcher binary.
    timeout_seconds: The maximum number of seconds to execute cl_launcher for.
    env: An optional environment to run cl_launcher under.

  Returns:
    A Popen instance, with string stdout and stderr attributes set.
  """
    if system.is_linux():
        env = env or {}
        env['LD_PRELOAD'] = PrependToPath(str(LIBOPENCL_SO),
                                          env.get('LD_PRELOAD'))
    with fs.TemporaryWorkingDir(prefix='cl_launcher_') as d:
        for src in CL_LAUNCHER_RUN_FILES:
            os.symlink(src, str(d / src.name))
        cmd = [
            'timeout', '-s9',
            str(timeout_seconds),
            str(CL_LAUNCHER), '-i',
            str(d)
        ] + list(opts)
        process = opencl_environment.Exec(cmd, env=env)
    return process
Esempio n. 2
0
def CompileDriver(src: str,
                  output_path: pathlib.Path,
                  platform_id: int,
                  device_id: int,
                  timeout_seconds: int = 60,
                  cflags: typing.List[str] = None) -> pathlib.Path:
    """Compile driver binary from source.

  Args:
    src: The C source code to compile.
    output_path: The path to the binary to generate.
    platform_id: The OpenCL platform ID.
    device_id: The OpenCL device ID.
    timeout_seconds: The number of seconds to allow for compilation.

  Returns:
    The path to the generated binary, same as output_path.

  Raises:
    DriverCompilationError: In case compilation fails.
  """
    cmd = [
        'timeout',
        '-s9',
        str(timeout_seconds),
        str(CLANG_PATH),
        '-xc',
        '-',
        '-o',
        str(output_path),
        f'-DPLATFORM_ID={platform_id}',
        f'-DDEVICE_ID={device_id}',
        '-ferror-limit=1',
        '-std=c99',
        '-Wno-deprecated-declarations',
        # Add OpenCL headers.
        '-isystem',
        str(OPENCL_HEADERS_DIR),
        # Link against libcxx.
        f'-L{LIBCXX_LIB_DIR}',
        f'-Wl,-rpath,{LIBCXX_LIB_DIR}',
        '-nodefaultlibs',
        '-stdlib=libc++',
        '-lc++',
        '-lc++abi',
        '-lm',
        '-lc',
    ]
    if system.is_linux():
        cmd += [
            # Additional libraries required to link against libcxxx.
            '-lgcc_s',
            '-lgcc',
            '-ldl',
            '-lpthread',
            # Link against libOpenCL.
            f'-L{LIBOPENCL_DIR}',
            f'-Wl,-rpath,{LIBOPENCL_DIR}',
            '-lOpenCL'
        ]
    elif system.is_mac():
        cmd += ['-framework', 'OpenCL']
    # Add any additional cflags.
    if cflags:
        cmd += cflags

    # logging.debug('$ %s', ' '.join(cmd))
    proc = subprocess.Popen(cmd,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            universal_newlines=True)
    stdout, stderr = proc.communicate(src)
    if not proc.returncode == 0:
        argv = ' '.join(cmd)
        raise DriverCompilationError(
            f'Driver compilation failed with returncode {proc.returncode}.\n'
            f'Command: {argv}\n'
            f'Stdout:\n{stdout}\n'
            f'Stderr:\n{stderr}\n'
            f'Driver source:\n{src}')
    return output_path
Esempio n. 3
0
from deeplearning.deepsmith.proto import deepsmith_pb2
from deeplearning.deepsmith.proto import harness_pb2
from deeplearning.deepsmith.proto import harness_pb2_grpc
from deeplearning.deepsmith.proto import service_pb2
from gpu.cldrive import cgen
from gpu.cldrive import data
from gpu.cldrive import driver
from gpu.cldrive import env
from phd.lib.labm8 import bazelutil
from phd.lib.labm8 import fs
from phd.lib.labm8 import labdate
from phd.lib.labm8 import system

FLAGS = flags.FLAGS

_UNAME = 'linux' if system.is_linux() else 'mac'
# Path to clang binary.
CLANG_PATH = bazelutil.DataPath(f'llvm_{_UNAME}/bin/clang')
# Flags for compiling with libcxx.
LIBCXX_LIB_DIR = bazelutil.DataPath(f'llvm_{_UNAME}/lib')
# Path to OpenCL headers.
OPENCL_HEADERS_DIR = bazelutil.DataPath('opencl_120_headers')
if system.is_linux():
    LIBOPENCL_DIR = bazelutil.DataPath('libopencl')


class DriverCompilationError(OSError):
    """Exception raised in case driver compilation fails."""
    pass

Esempio n. 4
0
  bazel run //gpu/oclgrind [-- -- <args>]
"""
import subprocess
import sys
import typing
from absl import app
from absl import flags

from gpu.clinfo.proto import clinfo_pb2
from phd.lib.labm8 import bazelutil
from phd.lib.labm8 import system

FLAGS = flags.FLAGS

_OCLGRIND_PKG = 'oclgrind_linux' if system.is_linux() else 'oclgrind_mac'
# The path to the oclgrind binary.
OCLGRIND_PATH = bazelutil.DataPath(f'{_OCLGRIND_PKG}/bin/oclgrind')
# The clinfo description of the local Oclgrind binary.
CLINFO_DESCRIPTION = clinfo_pb2.OpenClDevice(
    name='Emulator|Oclgrind|Oclgrind_Simulator|Oclgrind_18.3|1.2',
    platform_name='Oclgrind',
    device_name='Oclgrind Simulator',
    driver_version='Oclgrind 18.3',
    opencl_version='1.2',
    device_type='Emulator',
    platform_id=0,
    device_id=0,
)

Esempio n. 5
0
File: opt.py Progetto: BeauJoh/phd
import sys
import typing
from absl import app
from absl import flags
from absl import logging
from phd.lib.labm8 import bazelutil
from phd.lib.labm8 import system

from compilers.llvm import llvm

FLAGS = flags.FLAGS

flags.DEFINE_integer('opt_timeout_seconds', 60,
                     'The maximum number of seconds to allow process to run.')

_LLVM_REPO = 'llvm_linux' if system.is_linux() else 'llvm_mac'

# Path to opt binary.
OPT = bazelutil.DataPath(f'{_LLVM_REPO}/bin/opt')


class OptException(llvm.LlvmError):
    """An error from opt."""
    pass


def Exec(args: typing.List[str],
         timeout_seconds: int = 60) -> subprocess.Popen:
    """Run LLVM's optimizer.

  Args:
Esempio n. 6
0
    bazelutil.DataPath('CLSmith/runtime/custom_limits.h'),
    bazelutil.DataPath('CLSmith/runtime/custom_stdint_x86.h'),
    bazelutil.DataPath('CLSmith/runtime/platform_avr.h'),
    bazelutil.DataPath('CLSmith/runtime/platform_generic.h'),
    bazelutil.DataPath('CLSmith/runtime/platform_msp430.h'),
    bazelutil.DataPath('CLSmith/runtime/random_inc.h'),
    bazelutil.DataPath('CLSmith/runtime/safe_abbrev.h'),
    bazelutil.DataPath('CLSmith/runtime/stdint_avr.h'),
    bazelutil.DataPath('CLSmith/runtime/stdint_ia32.h'),
    bazelutil.DataPath('CLSmith/runtime/stdint_ia64.h'),
    bazelutil.DataPath('CLSmith/runtime/stdint_msp430.h'),
    bazelutil.DataPath('CLSmith/runtime/volatile_runtime.h')
]

# The path to libOpenCL.so, needed on Linux.
if system.is_linux():
    LIBOPENCL_SO = bazelutil.DataPath('libopencl/libOpenCL.so')


def PrependToPath(path_to_prepend: str, path: typing.Optional[str]) -> str:
    """Prepend a path to a path environment variable.

  This uses ':' to separate path components, as used in system $PATH variables.

  Args:
    path_to_prepend: The path to prepend.
    path: The environment variable.

  Returns:
    A concatenation of the path to prepend and the existing path.
  """
Esempio n. 7
0
from io import StringIO
from pathlib import Path
from phd.lib.labm8 import fs
from phd.lib.labm8 import system
from phd.lib.labm8 import tar

from experimental import dsmith


class Data404(Exception):
    pass


# test decorators
needs_cuda = pytest.mark.skipif(not dsmith.USE_CUDA, reason="no CUDA support")
needs_linux = pytest.mark.skipif(not system.is_linux(), reason="not linux")
skip_on_travis = pytest.mark.skipif(os.environ.get("TRAVIS") == 'true',
                                    reason="skip on Travis CI")


def data_path(*components, exists=True) -> str:
    """
  Return absolute path to unittest data file. Data files are located in
  <package>/test/data.

  Arguments:
      *components (str): Relative path.
      exists (bool, optional): If True, require that file exists.

  Returns:
      str: Absolute path.