-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADDON] Allow piggy back nvcc compiler and code (#35)
- Loading branch information
Showing
11 changed files
with
171 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Addon utilities to python""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Util to compile with NVCC""" | ||
import os | ||
import sys | ||
import tempfile | ||
import subprocess | ||
|
||
def compile_source(code, target="cubin"): | ||
"""Compile cuda code with NVCC from env. | ||
Parameters | ||
---------- | ||
code : str | ||
The cuda code. | ||
target: str | ||
The target format | ||
Return | ||
------ | ||
cubin : bytearray | ||
The bytearray of the cubin | ||
""" | ||
temp_dir = tempfile.mkdtemp() | ||
if target not in ["cubin", "ptx", "fatbin"]: | ||
raise ValueError("target must be in cubin, ptx, fatbin") | ||
path_code = os.path.join(temp_dir, "my_kernel.cu") | ||
path_target = os.path.join(temp_dir, "my_kernel.%s" % target) | ||
|
||
with open(path_code, "w") as out_file: | ||
out_file.write(code) | ||
|
||
cmd = ["nvcc"] | ||
cmd += ["--%s" % target, "-O3"] | ||
cmd += ["-o", path_target] | ||
cmd += [path_code] | ||
args = ' '.join(cmd) | ||
|
||
proc = subprocess.Popen( | ||
args, shell=True, | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT) | ||
(out, _) = proc.communicate() | ||
|
||
if proc.returncode != 0: | ||
sys.stderr.write("Compilation error:\n") | ||
sys.stderr.write(out) | ||
sys.stderr.flush() | ||
cubin = None | ||
else: | ||
cubin = bytearray(open(path_target, "rb").read()) | ||
os.remove(path_code) | ||
if os.path.exists(path_target): | ||
os.remove(path_target) | ||
os.rmdir(temp_dir) | ||
return cubin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters