Esempio n. 1
0
    def benchmark_from_parsed_uri(self, uri: BenchmarkUri) -> Benchmark:
        self.install()

        benchmark_name = uri.path[1:]
        if not benchmark_name:
            raise LookupError(f"No benchmark specified: {uri}")

        # The absolute path of the file, without an extension.
        path_stem = self.dataset_root / benchmark_name

        bitcode_abspath = Path(f"{path_stem}.bc")
        c_file_abspath = Path(f"{path_stem}.c")

        # If the file does not exist, compile it on-demand.
        if not bitcode_abspath.is_file():
            if not c_file_abspath.is_file():
                raise LookupError(
                    f"Benchmark not found: {uri} (file not found: {c_file_abspath})"
                )

            with atomic_file_write(bitcode_abspath) as tmp_path:
                compile_cmd = ClangInvocation.from_c_file(
                    c_file_abspath,
                    copt=[
                        "-ferror-limit=1",  # Stop on first error.
                        "-w",  # No warnings.
                    ],
                ).command(outpath=tmp_path)
                subprocess.check_call(compile_cmd, timeout=300)

        return BenchmarkWithSource.create(
            uri, bitcode_abspath, "function.c", c_file_abspath
        )
Esempio n. 2
0
    def benchmark(self, uri: Optional[str] = None) -> Benchmark:
        self.install()
        if uri is None or len(uri) <= len(self.name) + 1:
            return self._get_benchmark_by_index(self.random.integers(
                self.size))

        # The absolute path of the file, without an extension.
        path_stem = self.dataset_root / uri[len(self.name) + 1:]

        # If the file does not exist, compile it on-demand.
        bitcode_path = Path(f"{path_stem}.bc")
        cc_file_path = Path(f"{path_stem}.txt")

        if not bitcode_path.is_file():
            if not cc_file_path.is_file():
                raise LookupError(
                    f"Benchmark not found: {uri} (file not found: {cc_file_path})"
                )

            # Load the C++ source into memory and pre-process it.
            with open(cc_file_path) as f:
                src = self.preprocess_poj104_source(f.read())

            # Compile the C++ source into a bitcode file.
            with atomic_file_write(bitcode_path) as tmp_bitcode_path:
                compile_cmd = ClangInvocation.from_c_file(
                    "-",
                    copt=[
                        "-xc++",
                        "-ferror-limit=1",  # Stop on first error.
                        "-w",  # No warnings.
                        # Some of the programs use the gets() function that was
                        # deprecated in C++11 and removed in C++14.
                        "-std=c++11",
                    ],
                ).command(outpath=tmp_bitcode_path)
                logger.debug("Exec %s", compile_cmd)
                clang = subprocess.Popen(
                    compile_cmd,
                    stdin=subprocess.PIPE,
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                )
                _, stderr = clang.communicate(src.encode("utf-8"), timeout=300)

            if clang.returncode:
                compile_cmd = " ".join(compile_cmd)
                error = truncate(stderr.decode("utf-8"),
                                 max_lines=20,
                                 max_line_len=100)
                raise BenchmarkInitError(f"Compilation job failed!\n"
                                         f"Command: {compile_cmd}\n"
                                         f"Error: {error}")
            if not bitcode_path.is_file():
                raise BenchmarkInitError(
                    f"Compilation job failed to produce output file!\nCommand: {compile_cmd}"
                )

        return BenchmarkWithSource.create(uri, bitcode_path, "source.cc",
                                          cc_file_path)
Esempio n. 3
0
    def benchmark_from_parsed_uri(self, uri: BenchmarkUri) -> Benchmark:
        self.install()

        benchmark_name = uri.path[1:]
        if not benchmark_name:
            raise LookupError(f"No benchmark specified: {uri}")

        # The absolute path of the file, without an extension.
        path_stem = os.path.normpath(f"{self.dataset_root}/{uri.path}")

        bc_path, cl_path = Path(f"{path_stem}.bc"), Path(f"{path_stem}.cl")

        # If the file does not exist, compile it on-demand.
        if not bc_path.is_file():
            if not cl_path.is_file():
                raise LookupError(
                    f"Benchmark not found: {uri} (file not found: {cl_path}, path_stem {path_stem})"
                )

            # Compile the OpenCL kernel into a bitcode file.
            with atomic_file_write(bc_path) as tmp_bc_path:
                compile_command: List[str] = ClangInvocation.from_c_file(
                    cl_path,
                    copt=[
                        "-isystem",
                        str(self.libclc_dir),
                        "-include",
                        str(self.opencl_h_path),
                        "-target",
                        "nvptx64-nvidia-nvcl",
                        "-ferror-limit=1",  # Stop on first error.
                        "-w",  # No warnings.
                    ],
                ).command(outpath=tmp_bc_path)
                logger.debug("Exec %s", compile_command)
                try:
                    with Popen(
                            compile_command,
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                    ) as clang:
                        _, stderr = communicate(clang, timeout=300)
                except subprocess.TimeoutExpired:
                    raise BenchmarkInitError(
                        f"Benchmark compilation timed out: {uri}")

            if clang.returncode:
                compile_command = " ".join(compile_command)
                error = truncate(stderr.decode("utf-8"),
                                 max_lines=20,
                                 max_line_len=20000)
                raise BenchmarkInitError(f"Compilation job failed!\n"
                                         f"Command: {compile_command}\n"
                                         f"Error: {error}")

        return BenchmarkWithSource.create(uri, bc_path, "kernel.cl", cl_path)
Esempio n. 4
0
    def benchmark_from_parsed_uri(self, uri: BenchmarkUri) -> Benchmark:
        self.install()

        benchmark_name = uri.path[1:]
        if not benchmark_name:
            raise LookupError(f"No benchmark specified: {uri}")

        bitcode_abspath = self.dataset_root / f"{benchmark_name}.bc"

        # Most of the source files are named after the parent directory, but not
        # all.
        c_file_name = {
            "blowfish": "bf.c",
            "motion": "mpeg2.c",
            "sha": "sha_driver.c",
            "jpeg": "main.c",
        }.get(benchmark_name, f"{benchmark_name}.c")
        c_file_abspath = self.dataset_root / benchmark_name / c_file_name

        # If the file does not exist, compile it on-demand.
        if not bitcode_abspath.is_file():
            if not c_file_abspath.is_file():
                raise LookupError(
                    f"Benchmark not found: {uri} (file not found: {c_file_abspath})"
                )

            with atomic_file_write(bitcode_abspath) as tmp_path:
                compile_cmd = ClangInvocation.from_c_file(
                    c_file_abspath,
                    copt=[
                        "-ferror-limit=1",  # Stop on first error.
                        "-w",  # No warnings.
                    ],
                ).command(outpath=tmp_path)
                subprocess.check_call(compile_cmd, timeout=300)

        return BenchmarkWithSource.create(uri, bitcode_abspath, "function.c",
                                          c_file_abspath)
Esempio n. 5
0
    def benchmark_from_parsed_uri(self, uri: BenchmarkUri) -> Benchmark:
        self.install()

        # The absolute path of the file, without an extension.
        path_stem = os.path.normpath(f"{self.dataset_root}/{uri.path}")

        # If the file does not exist, compile it on-demand.
        bitcode_path = Path(f"{path_stem}.bc")
        cc_file_path = Path(f"{path_stem}.txt")

        if not bitcode_path.is_file():
            if not cc_file_path.is_file():
                raise LookupError(
                    f"Benchmark not found: {uri} (file not found: {cc_file_path})"
                )

            # Load the C++ source into memory and pre-process it.
            with open(cc_file_path) as f:
                src = self.preprocess_poj104_source(f.read())

            # Compile the C++ source into a bitcode file.
            with atomic_file_write(bitcode_path) as tmp_bitcode_path:
                compile_cmd = ClangInvocation.from_c_file(
                    "-",
                    copt=[
                        "-xc++",
                        "-ferror-limit=1",  # Stop on first error.
                        "-w",  # No warnings.
                        # Some of the programs use the gets() function that was
                        # deprecated in C++11 and removed in C++14.
                        "-std=c++11",
                    ],
                ).command(outpath=tmp_bitcode_path)
                logger.debug("Exec %s", compile_cmd)
                try:
                    with Popen(
                        compile_cmd,
                        stdin=subprocess.PIPE,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                    ) as clang:
                        _, stderr = clang.communicate(
                            input=src.encode("utf-8"), timeout=300
                        )
                except subprocess.TimeoutExpired:
                    raise BenchmarkInitError(f"Benchmark compilation timed out: {uri}")

            if clang.returncode:
                compile_cmd = " ".join(compile_cmd)
                error = truncate(stderr.decode("utf-8"), max_lines=20, max_line_len=100)
                if tmp_bitcode_path.is_file():
                    tmp_bitcode_path.unlink()
                raise BenchmarkInitError(
                    f"Compilation job failed!\n"
                    f"Command: {compile_cmd}\n"
                    f"Error: {error}"
                )

            if not bitcode_path.is_file():
                raise BenchmarkInitError(
                    f"Compilation job failed to produce output file!\nCommand: {compile_cmd}"
                )

        return BenchmarkWithSource.create(uri, bitcode_path, "source.cc", cc_file_path)