mirror of
https://github.com/andreinechaev/nvcc4jupyter.git
synced 2026-06-13 18:50:47 +05:30
supporting multiple files
This commit is contained in:
+73
-1
@@ -5,6 +5,8 @@ import argparse
|
|||||||
import tempfile
|
import tempfile
|
||||||
import subprocess
|
import subprocess
|
||||||
import IPython.core.magic as ipym
|
import IPython.core.magic as ipym
|
||||||
|
from IPython.core.magic_arguments import (argument, magic_arguments,
|
||||||
|
parse_argstring)
|
||||||
|
|
||||||
compiler = '/usr/local/cuda/bin/nvcc'
|
compiler = '/usr/local/cuda/bin/nvcc'
|
||||||
ext = '.cu'
|
ext = '.cu'
|
||||||
@@ -30,7 +32,7 @@ class NVCCPlugin(ipym.Magics):
|
|||||||
|
|
||||||
def run(self, file_path, timeit=False):
|
def run(self, file_path, timeit=False):
|
||||||
if timeit:
|
if timeit:
|
||||||
stmt = "subprocess.check_output(['{}'], stderr=subprocess.STDOUT)".format(file_path + ".out")
|
stmt = f"subprocess.check_output(['{file_path}.out'], stderr=subprocess.STDOUT)"
|
||||||
output = self.shell.run_cell_magic(magic_name="timeit", line="-q -o import subprocess", cell=stmt)
|
output = self.shell.run_cell_magic(magic_name="timeit", line="-q -o import subprocess", cell=stmt)
|
||||||
else:
|
else:
|
||||||
output = subprocess.check_output([file_path + ".out"], stderr=subprocess.STDOUT)
|
output = subprocess.check_output([file_path + ".out"], stderr=subprocess.STDOUT)
|
||||||
@@ -58,6 +60,76 @@ class NVCCPlugin(ipym.Magics):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
out = "result.out"
|
||||||
|
|
||||||
|
|
||||||
|
@ipym.magics_class
|
||||||
|
class NVCCPluginV2(ipym.Magics):
|
||||||
|
|
||||||
|
def __init__(self, shell):
|
||||||
|
super(NVCCPluginV2, self).__init__(shell)
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
self.output_dir = os.path.join(tmp, str(uuid.uuid4()))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def compile(file_path):
|
||||||
|
subprocess.check_output([compiler, file_path, "-o", out], stderr=subprocess.STDOUT)
|
||||||
|
|
||||||
|
def run(self, file_path, timeit=False):
|
||||||
|
if timeit:
|
||||||
|
stmt = f"subprocess.check_output(['{out}'], stderr=subprocess.STDOUT)"
|
||||||
|
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
|
||||||
|
|
||||||
|
@magic_arguments
|
||||||
|
@argument('-n', '--name', type=str, help='file name that will be produced by the cell. must end with .cu extension')
|
||||||
|
@argument('-c', '--compile', type=bool, help='Should be compiled?')
|
||||||
|
@ipym.cell_magic
|
||||||
|
def cuda(self, line='', cell=None):
|
||||||
|
args = parse_argstring(self.cuda, line)
|
||||||
|
if args.name[:-3] != '.cu':
|
||||||
|
raise Exception('name must end with .cu')
|
||||||
|
|
||||||
|
file_path = os.path.join(self.tmp_dir, args.name)
|
||||||
|
with open(file_path, "w") as f:
|
||||||
|
f.write(cell)
|
||||||
|
|
||||||
|
if args.compile:
|
||||||
|
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
|
||||||
|
else:
|
||||||
|
output = f'File written in {file_path}'
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
@ipym.cell_magic
|
||||||
|
def cuda_run(self, line='', cell=None):
|
||||||
|
try:
|
||||||
|
args = self.argparser.parse_args(line.split())
|
||||||
|
except SystemExit:
|
||||||
|
self.argparser.print_help()
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.compile('*.cu')
|
||||||
|
output = self.run(out, timeit=args.timeit)
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
print(e.output.decode("utf8"))
|
||||||
|
output = None
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
def load_ipython_extension(ip):
|
def load_ipython_extension(ip):
|
||||||
nvcc_plugin = NVCCPlugin(ip)
|
nvcc_plugin = NVCCPlugin(ip)
|
||||||
ip.register_magics(nvcc_plugin)
|
ip.register_magics(nvcc_plugin)
|
||||||
|
|
||||||
|
nvcc_plugin_v2 = NVCCPluginV2(ip)
|
||||||
|
ip.register_magics(nvcc_plugin_v2)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from distutils.core import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='NVCCPlugin',
|
name='NVCCPlugin',
|
||||||
version='0.0.1',
|
version='0.0.2',
|
||||||
author='Andrei Nechaev',
|
author='Andrei Nechaev',
|
||||||
author_email='lyfaradey@yahoo.com',
|
author_email='lyfaradey@yahoo.com',
|
||||||
py_modules=['nvcc_plugin'],
|
py_modules=['nvcc_plugin'],
|
||||||
|
|||||||
Reference in New Issue
Block a user