mirror of
https://github.com/andreinechaev/nvcc4jupyter.git
synced 2026-06-15 19:50:50 +05:30
Add tests for choosing the profiler
This commit is contained in:
Vendored
+13
@@ -1,9 +1,11 @@
|
|||||||
|
import argparse
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from IPython.core.interactiveshell import InteractiveShell
|
from IPython.core.interactiveshell import InteractiveShell
|
||||||
|
|
||||||
|
from nvcc4jupyter.parsers import Profiler
|
||||||
from nvcc4jupyter.plugin import NVCCPlugin
|
from nvcc4jupyter.plugin import NVCCPlugin
|
||||||
|
|
||||||
|
|
||||||
@@ -70,3 +72,14 @@ def multiple_source_fpaths(fixtures_path: str):
|
|||||||
pattern_h = os.path.join(fixtures_path, "multiple_files", "*.h")
|
pattern_h = os.path.join(fixtures_path, "multiple_files", "*.h")
|
||||||
pattern_cu = os.path.join(fixtures_path, "multiple_files", "*.cu")
|
pattern_cu = os.path.join(fixtures_path, "multiple_files", "*.cu")
|
||||||
return list(glob.glob(pattern_h)) + list(glob.glob(pattern_cu))
|
return list(glob.glob(pattern_h)) + list(glob.glob(pattern_cu))
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="session")
|
||||||
|
def default_args():
|
||||||
|
return argparse.Namespace(
|
||||||
|
timeit=False,
|
||||||
|
profile=True,
|
||||||
|
profiler=lambda: Profiler.NCU,
|
||||||
|
profiler_args=lambda: "",
|
||||||
|
compiler_args=lambda: "",
|
||||||
|
)
|
||||||
|
|||||||
Vendored
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "[NCU]"
|
||||||
|
|
||||||
# this is a mock of nsight compute cli tool that just executes the program
|
# this is a mock of nsight compute cli tool that just executes the program
|
||||||
# given as the last argument
|
# given as the last argument
|
||||||
"${@: -1}"
|
"${@: -1}"
|
||||||
|
|
||||||
echo "==WARNING== No kernels were profiled"
|
|
||||||
|
|||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "[NSYS]"
|
||||||
|
|
||||||
|
# this is a mock of nsight systems cli tool that just executes the program
|
||||||
|
# given as the last argument
|
||||||
|
"${@: -1}"
|
||||||
+42
-58
@@ -1,29 +1,26 @@
|
|||||||
import argparse
|
|
||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from argparse import ArgumentParser, Namespace
|
||||||
|
from copy import deepcopy
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from nvcc4jupyter.parsers import get_parser_cuda, set_defaults
|
from nvcc4jupyter.parsers import Profiler, get_parser_cuda, set_defaults
|
||||||
from nvcc4jupyter.plugin import NVCCPlugin
|
from nvcc4jupyter.plugin import NVCCPlugin
|
||||||
|
|
||||||
|
|
||||||
def check_profiler_output(output: str):
|
def check_profiler_output(output: str, profiler: str = "[NCU]"):
|
||||||
# the profiler output will be a line of "Hello World!" along with some
|
# the output from the profiler will first be a line containing only
|
||||||
# warning lines which start with "==WARNING=="
|
# "[NCU]" or "[NSYS]" depending on what profiler was used and another
|
||||||
|
# line containing the string "Hello World!"
|
||||||
lines = output.strip().split("\n")
|
lines = output.strip().split("\n")
|
||||||
warn_count = 0
|
assert len(lines) >= 2
|
||||||
for line in lines:
|
assert lines[0] == profiler
|
||||||
if not line.startswith("==WARNING=="):
|
assert lines[1] == "Hello World!"
|
||||||
assert line == "Hello World!"
|
|
||||||
else:
|
|
||||||
warn_count += 1
|
|
||||||
assert warn_count >= 1
|
|
||||||
assert warn_count == len(lines) - 1
|
|
||||||
|
|
||||||
|
|
||||||
def copy_source_to_group(
|
def copy_source_to_group(
|
||||||
@@ -46,7 +43,7 @@ def before_all(scripts_path: str):
|
|||||||
@pytest.fixture(autouse=True, scope="function")
|
@pytest.fixture(autouse=True, scope="function")
|
||||||
def before_each(plugin: NVCCPlugin):
|
def before_each(plugin: NVCCPlugin):
|
||||||
# BEFORE TESTS
|
# BEFORE TESTS
|
||||||
set_defaults(compiler_args="", profiler_args="")
|
set_defaults(profiler=Profiler.NCU, compiler_args="", profiler_args="")
|
||||||
shutil.rmtree(plugin.workdir, ignore_errors=True)
|
shutil.rmtree(plugin.workdir, ignore_errors=True)
|
||||||
yield
|
yield
|
||||||
# AFTER TESTS
|
# AFTER TESTS
|
||||||
@@ -101,6 +98,7 @@ def test_compile(
|
|||||||
def test_compile_args(
|
def test_compile_args(
|
||||||
plugin: NVCCPlugin,
|
plugin: NVCCPlugin,
|
||||||
compiler_cpp_17_fpath: str,
|
compiler_cpp_17_fpath: str,
|
||||||
|
default_args: Namespace,
|
||||||
):
|
):
|
||||||
gname = "test_compile_args"
|
gname = "test_compile_args"
|
||||||
copy_source_to_group(compiler_cpp_17_fpath, gname, plugin.workdir)
|
copy_source_to_group(compiler_cpp_17_fpath, gname, plugin.workdir)
|
||||||
@@ -112,21 +110,16 @@ def test_compile_args(
|
|||||||
with pytest.raises(subprocess.CalledProcessError):
|
with pytest.raises(subprocess.CalledProcessError):
|
||||||
exec_fpath = plugin._compile(gname, compiler_args="--std c++14")
|
exec_fpath = plugin._compile(gname, compiler_args="--std c++14")
|
||||||
|
|
||||||
output = plugin._compile_and_run(
|
args = deepcopy(default_args)
|
||||||
group_name=gname,
|
args.compiler_args = lambda: "--std c++14"
|
||||||
args=argparse.Namespace(
|
output = plugin._compile_and_run(group_name=gname, args=args)
|
||||||
timeit=False,
|
|
||||||
profile=True,
|
|
||||||
profiler_args=lambda: "",
|
|
||||||
compiler_args=lambda: "--std c++14",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
assert "errors detected in the compilation of" in output
|
assert "errors detected in the compilation of" in output
|
||||||
|
|
||||||
|
|
||||||
def test_compile_opencv(
|
def test_compile_opencv(
|
||||||
plugin: NVCCPlugin,
|
plugin: NVCCPlugin,
|
||||||
compiler_opencv_fpath: str,
|
compiler_opencv_fpath: str,
|
||||||
|
default_args: Namespace,
|
||||||
):
|
):
|
||||||
gname = "test_compile_opencv"
|
gname = "test_compile_opencv"
|
||||||
copy_source_to_group(compiler_opencv_fpath, gname, plugin.workdir)
|
copy_source_to_group(compiler_opencv_fpath, gname, plugin.workdir)
|
||||||
@@ -134,23 +127,14 @@ def test_compile_opencv(
|
|||||||
# check that "pkg-config" exists
|
# check that "pkg-config" exists
|
||||||
assert subprocess.check_call(["which", "pkg-config"]) == 0
|
assert subprocess.check_call(["which", "pkg-config"]) == 0
|
||||||
|
|
||||||
|
pkg_config_args = ["pkg-config", "--cflags", "--libs", "opencv4"]
|
||||||
opencv_compile_options = (
|
opencv_compile_options = (
|
||||||
subprocess.check_output(
|
subprocess.check_output(args=pkg_config_args).decode().strip()
|
||||||
args=["pkg-config", "--cflags", "--libs", "opencv4"]
|
|
||||||
)
|
|
||||||
.decode()
|
|
||||||
.strip()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
output = plugin._compile_and_run(
|
args = deepcopy(default_args)
|
||||||
group_name=gname,
|
args.compiler_args = lambda: opencv_compile_options
|
||||||
args=argparse.Namespace(
|
output = plugin._compile_and_run(group_name=gname, args=args)
|
||||||
timeit=False,
|
|
||||||
profile=True,
|
|
||||||
profiler_args=lambda: "",
|
|
||||||
compiler_args=lambda: opencv_compile_options,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
assert "General configuration for OpenCV" in output
|
assert "General configuration for OpenCV" in output
|
||||||
|
|
||||||
|
|
||||||
@@ -199,7 +183,9 @@ def test_run_profile(plugin: NVCCPlugin, sample_cuda_fpath: str):
|
|||||||
|
|
||||||
|
|
||||||
def test_compile_and_run_multiple_files(
|
def test_compile_and_run_multiple_files(
|
||||||
plugin: NVCCPlugin, multiple_source_fpaths: List[str]
|
plugin: NVCCPlugin,
|
||||||
|
multiple_source_fpaths: List[str],
|
||||||
|
default_args: Namespace,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Compiles and executes 3 cuda source files from
|
Compiles and executes 3 cuda source files from
|
||||||
@@ -208,20 +194,14 @@ def test_compile_and_run_multiple_files(
|
|||||||
gname = "test_compile_and_run_multiple_files"
|
gname = "test_compile_and_run_multiple_files"
|
||||||
for fpath in multiple_source_fpaths:
|
for fpath in multiple_source_fpaths:
|
||||||
copy_source_to_group(fpath, gname, plugin.workdir)
|
copy_source_to_group(fpath, gname, plugin.workdir)
|
||||||
output = plugin._compile_and_run(
|
output = plugin._compile_and_run(group_name=gname, args=default_args)
|
||||||
group_name=gname,
|
|
||||||
args=argparse.Namespace(
|
|
||||||
timeit=False,
|
|
||||||
profile=True,
|
|
||||||
profiler_args=lambda: "",
|
|
||||||
compiler_args=lambda: "",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
check_profiler_output(output)
|
check_profiler_output(output)
|
||||||
|
|
||||||
|
|
||||||
def test_compile_and_run_multiple_files_shared(
|
def test_compile_and_run_multiple_files_shared(
|
||||||
plugin: NVCCPlugin, multiple_source_fpaths: List[str]
|
plugin: NVCCPlugin,
|
||||||
|
multiple_source_fpaths: List[str],
|
||||||
|
default_args: Namespace,
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Compiles and executes 3 cuda source files from
|
Compiles and executes 3 cuda source files from
|
||||||
@@ -236,20 +216,12 @@ def test_compile_and_run_multiple_files_shared(
|
|||||||
copy_source_to_group(fpath, gname, plugin.workdir)
|
copy_source_to_group(fpath, gname, plugin.workdir)
|
||||||
else:
|
else:
|
||||||
copy_source_to_group(fpath, "shared", plugin.workdir)
|
copy_source_to_group(fpath, "shared", plugin.workdir)
|
||||||
output = plugin._compile_and_run(
|
output = plugin._compile_and_run(group_name=gname, args=default_args)
|
||||||
group_name=gname,
|
|
||||||
args=argparse.Namespace(
|
|
||||||
timeit=False,
|
|
||||||
profile=True,
|
|
||||||
profiler_args=lambda: "",
|
|
||||||
compiler_args=lambda: "",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
check_profiler_output(output)
|
check_profiler_output(output)
|
||||||
|
|
||||||
|
|
||||||
def test_read_args(plugin: NVCCPlugin):
|
def test_read_args(plugin: NVCCPlugin):
|
||||||
parser = argparse.ArgumentParser()
|
parser = ArgumentParser()
|
||||||
parser.add_argument("-a", type=str, required=True)
|
parser.add_argument("-a", type=str, required=True)
|
||||||
parser.add_argument("-b", type=float, required=True)
|
parser.add_argument("-b", type=float, required=True)
|
||||||
args = plugin._read_args(
|
args = plugin._read_args(
|
||||||
@@ -292,6 +264,18 @@ def test_magic_cuda(
|
|||||||
check_profiler_output(capsys.readouterr().out)
|
check_profiler_output(capsys.readouterr().out)
|
||||||
|
|
||||||
|
|
||||||
|
def test_magic_cuda_set_default_profiler(
|
||||||
|
capsys,
|
||||||
|
plugin: NVCCPlugin,
|
||||||
|
sample_cuda_code: str,
|
||||||
|
sample_magic_cu_line: str,
|
||||||
|
):
|
||||||
|
# set the default profiler to Nsight Systems
|
||||||
|
set_defaults(profiler=Profiler.NSYS)
|
||||||
|
plugin.cuda(sample_magic_cu_line, sample_cuda_code)
|
||||||
|
check_profiler_output(capsys.readouterr().out, profiler="[NSYS]")
|
||||||
|
|
||||||
|
|
||||||
def test_magic_cuda_bad_args(
|
def test_magic_cuda_bad_args(
|
||||||
capsys,
|
capsys,
|
||||||
plugin: NVCCPlugin,
|
plugin: NVCCPlugin,
|
||||||
|
|||||||
Reference in New Issue
Block a user