commit 208e67cf07d375171de81a1ced1563f9b06d4eab Author: Andrei Nechaev Date: Fri May 4 10:26:11 2018 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d72576 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f32a582 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +## NVCC Plugin for Jupyter notebook diff --git a/nvcc_plugin.py b/nvcc_plugin.py new file mode 100644 index 0000000..7f531c9 --- /dev/null +++ b/nvcc_plugin.py @@ -0,0 +1,62 @@ +import os +import uuid +import timeit +import argparse +import tempfile +import subprocess +import IPython.core.magic as ipym + +ext = '.cu' + + +def get_argparser(): + parser = argparse.ArgumentParser(description='NVCCPlugin params') + parser.add_argument("-t", "--timeit", action='store_true', + help='flag to return timeit result instead of stdout') + return parser + + +@ipym.magics_class +class NVCCPlugin(ipym.Magics): + + def __init__(self, shell): + super(NVCCPlugin, self).__init__(shell) + self.argparser = get_argparser() + + @staticmethod + def compile(file_path): + subprocess.check_output(["nvcc", file_path + ext, "-o", file_path + ".out"], stderr=subprocess.STDOUT) + + def run(self, file_path, timeit=False): + if timeit: + stmt = "subprocess.check_output(['{}'], stderr=subprocess.STDOUT)".format(file_path + ".out") + output = self.shell.run_cell_magic(magic_name="timeit", line="-q -o import subprocess", cell=stmt) + else: + output = subprocess.check_output([file_path + ".out"], stderr=subprocess.STDOUT) + output = output.decode('utf8') + return output + + @ipym.cell_magic + def cu(self, line, cell): + try: + args = self.argparser.parse_args(line.split()) + except SystemExit as e: + self.argparser.print_help() + return + + with tempfile.TemporaryDirectory() as tmp_dir: + file_path = os.path.join(tmp_dir, str(uuid.uuid4())) + with open(file_path + ext, "w") as f: + f.write(cell) + try: + self.compile(file_path) + output = self.run(file_path, timeit=args.timeit) + except subprocess.CalledProcessError as e: + print(e.output.decode("utf8")) + output = None + return output + + +def load_ipython_extension(ip): + nvcc_plugin = NVCCPlugin(ip) + ip.register_magics(nvcc_plugin) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..3d3f20e --- /dev/null +++ b/setup.py @@ -0,0 +1,13 @@ +from distutils.core import setup + +setup( + name='NVCCPlugin', + version='0.0.1', + author='Andrei Nechaev', + author_email='lyfaradey@yahoo.com', + py_modules=['nvcc_plugin'], + url='htpps://github.com/andreinechaev/nvcc4jupyter', + license='LICENSE', + description='Jupyter notebook plugin to run CUDA C/C++ code', + # long_description=open('README.md').read(), +)