-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4c4394
commit 85e007f
Showing
1 changed file
with
38 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import ctypes | ||
import os | ||
from subprocess import run | ||
|
||
def compile_cuda_kernel(): | ||
kernel_code = """ | ||
extern "C" | ||
__global__ void testKernel() { | ||
printf("Hello from CUDA kernel!\\n"); | ||
} | ||
""" | ||
kernel_file = 'test_kernel.cu' | ||
binary_file = 'test_kernel' | ||
|
||
with open(kernel_file, 'w') as f: | ||
f.write(kernel_code) | ||
|
||
result = run(['nvcc', kernel_file, '-o', binary_file]) | ||
if result.returncode != 0: | ||
raise RuntimeError("CUDA kernel compilation failed") | ||
|
||
return binary_file | ||
|
||
def run_cuda_kernel(binary_file): | ||
result = run(['./' + binary_file]) | ||
if result.returncode != 0: | ||
raise RuntimeError("CUDA kernel execution failed") | ||
|
||
def test_cuda_compile_run(): | ||
binary_file = compile_cuda_kernel() | ||
try: | ||
run_cuda_kernel(binary_file) | ||
finally: | ||
os.remove(binary_file) | ||
os.remove('test_kernel.cu') | ||
|
||
if __name__ == "__main__": | ||
test_cuda_compile_run() |