mirror of
https://github.com/andreinechaev/nvcc4jupyter.git
synced 2026-06-13 18:50:47 +05:30
print out multiline output
This commit is contained in:
@@ -6,3 +6,8 @@ def get_argparser():
|
|||||||
parser.add_argument("-t", "--timeit", action='store_true',
|
parser.add_argument("-t", "--timeit", action='store_true',
|
||||||
help='flag to return timeit result instead of stdout')
|
help='flag to return timeit result instead of stdout')
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
|
def print_out(out: str):
|
||||||
|
for l in out.split('\n'):
|
||||||
|
print(l)
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ import tempfile
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from IPython.core.magic import Magics, cell_magic, magics_class
|
from IPython.core.magic import Magics, cell_magic, magics_class
|
||||||
|
from common import helper
|
||||||
|
|
||||||
compiler = '/usr/local/cuda/bin/nvcc -Wno-deprecated-gpu-targets'
|
compiler = '/usr/local/cuda/bin/nvcc'
|
||||||
ext = '.cu'
|
ext = '.cu'
|
||||||
|
|
||||||
|
|
||||||
@@ -14,21 +15,26 @@ class NVCCPlugin(Magics):
|
|||||||
|
|
||||||
def __init__(self, shell):
|
def __init__(self, shell):
|
||||||
super(NVCCPlugin, self).__init__(shell)
|
super(NVCCPlugin, self).__init__(shell)
|
||||||
from common import helper
|
|
||||||
self.argparser = helper.get_argparser()
|
self.argparser = helper.get_argparser()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def compile(file_path):
|
def compile(file_path):
|
||||||
subprocess.check_output([compiler, file_path + ext, "-o", file_path + ".out"], stderr=subprocess.STDOUT)
|
subprocess.check_output(
|
||||||
|
[compiler, file_path + ext, "-o", file_path + ".out", '-Wno-deprecated-gpu-targets'], stderr=subprocess.STDOUT)
|
||||||
|
|
||||||
def run(self, file_path, timeit=False):
|
def run(self, file_path, timeit=False):
|
||||||
if timeit:
|
if timeit:
|
||||||
stmt = f"subprocess.check_output(['{file_path}.out'], stderr=subprocess.STDOUT)"
|
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)
|
||||||
output = output.decode('utf8')
|
output = output.decode('utf8')
|
||||||
return output
|
|
||||||
|
helper.print_out(output)
|
||||||
|
return None
|
||||||
|
|
||||||
@cell_magic
|
@cell_magic
|
||||||
def cu(self, line, cell):
|
def cu(self, line, cell):
|
||||||
@@ -46,6 +52,6 @@ class NVCCPlugin(Magics):
|
|||||||
self.compile(file_path)
|
self.compile(file_path)
|
||||||
output = self.run(file_path, timeit=args.timeit)
|
output = self.run(file_path, timeit=args.timeit)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(e.output.decode("utf8"))
|
helper.print_out(e.output.decode("utf8"))
|
||||||
output = None
|
output = None
|
||||||
return output
|
return output
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ from IPython.core.magic import Magics, cell_magic, magics_class
|
|||||||
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
|
from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
|
||||||
from common import helper
|
from common import helper
|
||||||
|
|
||||||
compiler = '/usr/local/cuda/bin/nvcc --Wno-deprecated-gpu-targets'
|
compiler = '/usr/local/cuda/bin/nvcc'
|
||||||
|
|
||||||
|
|
||||||
@magics_class
|
@magics_class
|
||||||
class NVCCPluginV2(Magics):
|
class NVCCPluginV2(Magics):
|
||||||
@@ -26,18 +27,22 @@ class NVCCPluginV2(Magics):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def compile(output_dir, file_paths, out):
|
def compile(output_dir, file_paths, out):
|
||||||
res = subprocess.check_output([compiler, '-I' + output_dir, file_paths, "-o", out], stderr=subprocess.STDOUT)
|
res = subprocess.check_output(
|
||||||
print(res)
|
[compiler, '-I' + output_dir, file_paths, "-o", out, '-Wno-deprecated-gpu-targets'], stderr=subprocess.STDOUT)
|
||||||
|
helper.print_out(res)
|
||||||
|
|
||||||
def run(self, timeit=False):
|
def run(self, timeit=False):
|
||||||
if timeit:
|
if timeit:
|
||||||
stmt = f"subprocess.check_output(['{self.out}'], stderr=subprocess.STDOUT)"
|
stmt = f"subprocess.check_output(['{self.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([self.out], stderr=subprocess.STDOUT)
|
output = subprocess.check_output(
|
||||||
|
[self.out], stderr=subprocess.STDOUT)
|
||||||
output = output.decode('utf8')
|
output = output.decode('utf8')
|
||||||
|
|
||||||
return output
|
helper.print_out(output)
|
||||||
|
return None
|
||||||
|
|
||||||
@magic_arguments()
|
@magic_arguments()
|
||||||
@argument('-n', '--name', type=str, help='file name that will be produced by the cell. must end with .cu extension')
|
@argument('-n', '--name', type=str, help='file name that will be produced by the cell. must end with .cu extension')
|
||||||
@@ -67,7 +72,7 @@ class NVCCPluginV2(Magics):
|
|||||||
self.compile(self.output_dir, file_path, self.out)
|
self.compile(self.output_dir, file_path, self.out)
|
||||||
output = self.run(timeit=args.timeit)
|
output = self.run(timeit=args.timeit)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(e.output.decode("utf8"))
|
helper.print_out(e.output.decode("utf8"))
|
||||||
output = None
|
output = None
|
||||||
else:
|
else:
|
||||||
output = f'File written in {file_path}'
|
output = f'File written in {file_path}'
|
||||||
@@ -84,12 +89,13 @@ class NVCCPluginV2(Magics):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
cuda_src = os.listdir(self.output_dir)
|
cuda_src = os.listdir(self.output_dir)
|
||||||
cuda_src = [os.path.join(self.output_dir, x) for x in cuda_src if x[-3:] == '.cu']
|
cuda_src = [os.path.join(self.output_dir, x)
|
||||||
|
for x in cuda_src if x[-3:] == '.cu']
|
||||||
print(f'found sources: {cuda_src}')
|
print(f'found sources: {cuda_src}')
|
||||||
self.compile(self.output_dir, ' '.join(cuda_src), self.out)
|
self.compile(self.output_dir, ' '.join(cuda_src), self.out)
|
||||||
output = self.run(timeit=args.timeit)
|
output = self.run(timeit=args.timeit)
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(e.output.decode("utf8"))
|
helper.print_out(e.output.decode("utf8"))
|
||||||
output = None
|
output = None
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|||||||
Reference in New Issue
Block a user