Skip to content

Commit 2c184c5

Browse files
alexfiklinducer
authored andcommitted
examples: reformat
1 parent d583819 commit 2c184c5

File tree

3 files changed

+46
-61
lines changed

3 files changed

+46
-61
lines changed

examples/demo.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import cgen as c
22

33
from codepy.bpl import BoostPythonModule
4+
from codepy.toolchain import guess_toolchain
45

56

67
mod = BoostPythonModule()
7-
88
mod.add_function(
99
c.FunctionBody(
1010
c.FunctionDeclaration(c.Const(c.Pointer(c.Value("char", "greet"))), []),
1111
c.Block([c.Statement('return "hello world"')])
1212
))
1313

14-
from codepy.toolchain import guess_toolchain
15-
16-
17-
cmod = mod.compile(guess_toolchain())
14+
toolchain = guess_toolchain()
15+
cmod = mod.compile(toolchain)
1816

1917
print(cmod.greet())

examples/demo_plain.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
from codepy.jit import extension_from_string
2+
from codepy.libraries import add_boost_python
3+
from codepy.toolchain import guess_toolchain
4+
5+
16
MODULE_CODE = """
27
#include <boost/python.hpp>
38
@@ -15,19 +20,8 @@
1520
}
1621
"""
1722

18-
from codepy.toolchain import guess_toolchain
19-
20-
2123
toolchain = guess_toolchain()
22-
23-
from codepy.libraries import add_boost_python
24-
25-
2624
add_boost_python(toolchain)
2725

28-
from codepy.jit import extension_from_string
29-
30-
3126
cmod = extension_from_string(toolchain, "module", MODULE_CODE)
32-
3327
print(cmod.greet())

examples/nvcc-test.py

+38-45
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1+
import math
2+
import sys
3+
4+
import numpy as np
5+
import pycuda.autoinit
6+
import pycuda.driver
7+
import pycuda.gpuarray
8+
19
import cgen as c
210
from cgen.cuda import CudaGlobal
311

412
from codepy.bpl import BoostPythonModule
513
from codepy.cuda import CudaModule
14+
from codepy.toolchain import guess_nvcc_toolchain, guess_toolchain
615

716

817
# This file tests the ability to use compile and link CUDA code into the
918
# Python interpreter. Running this test requires PyCUDA
1019
# as well as CUDA 3.0beta (or greater)
1120

12-
1321
# The host module should include a function which is callable from Python
1422
host_mod = BoostPythonModule()
1523

16-
import math
17-
18-
# Are we on a 32 or 64 bit platform?
19-
import sys
20-
21-
2224
bitness = math.log(sys.maxsize) + 1
2325
ptr_sz_uint_conv = "K" if bitness > 32 else "I"
2426

@@ -49,15 +51,14 @@
4951
"PyObject* remoteResult = PyObject_Call(GPUArrayClass, args, kwargs)",
5052
"return remoteResult"]
5153

52-
5354
host_mod.add_function(
5455
c.FunctionBody(
55-
c.FunctionDeclaration(c.Pointer(c.Value("PyObject", "adjacentDifference")),
56-
[c.Pointer(c.Value("PyObject", "gpuArray"))]),
56+
c.FunctionDeclaration(
57+
c.Pointer(c.Value("PyObject", "adjacentDifference")),
58+
[c.Pointer(c.Value("PyObject", "gpuArray"))]),
5759
c.Block([c.Statement(x) for x in statements])))
5860
host_mod.add_to_preamble([c.Include("boost/python/extract.hpp")])
5961

60-
6162
cuda_mod = CudaModule(host_mod)
6263
cuda_mod.add_to_preamble([c.Include("cuda.h")])
6364

@@ -72,55 +73,46 @@
7273

7374
diff = [
7475
c.Template("typename T",
75-
CudaGlobal(c.FunctionDeclaration(c.Value("void", "diffKernel"),
76-
[c.Value("T*", "inputPtr"),
77-
c.Value("int", "length"),
78-
c.Value("T*", "outputPtr")]))),
79-
c.Block([c.Statement(global_index),
80-
c.If("index == 0",
81-
c.Statement("outputPtr[0] = inputPtr[0]"),
82-
c.If("index < length",
83-
c.Statement(compute_diff),
84-
c.Statement("")))]),
76+
CudaGlobal(c.FunctionDeclaration(c.Value("void", "diffKernel"),
77+
[c.Value("T*", "inputPtr"),
78+
c.Value("int", "length"),
79+
c.Value("T*", "outputPtr")]))),
80+
c.Block([
81+
c.Statement(global_index),
82+
c.If("index == 0",
83+
c.Statement("outputPtr[0] = inputPtr[0]"),
84+
c.If("index < length",
85+
c.Statement(compute_diff),
86+
c.Statement("")))]),
8587

8688
c.Template("typename T",
87-
c.FunctionDeclaration(c.Value("CUdeviceptr", "difference"),
88-
[c.Value("CUdeviceptr", "inputPtr"),
89-
c.Value("int", "length")])),
89+
c.FunctionDeclaration(c.Value("CUdeviceptr", "difference"),
90+
[c.Value("CUdeviceptr", "inputPtr"),
91+
c.Value("int", "length")])),
9092
c.Block([c.Statement(x) for x in launch])]
91-
9293
cuda_mod.add_to_module(diff)
94+
95+
# CudaModule.add_function also adds a declaration of this function to the
96+
# BoostPythonModule which is responsible for the host function.
97+
9398
diff_instance = c.FunctionBody(
9499
c.FunctionDeclaration(c.Value("CUdeviceptr", "diffInstance"),
95-
[c.Value("CUdeviceptr", "inputPtr"),
96-
c.Value("int", "length")]),
100+
[c.Value("CUdeviceptr", "inputPtr"),
101+
c.Value("int", "length")]),
97102
c.Block([c.Statement("return difference<int>(inputPtr, length)")]))
98-
99-
# CudaModule.add_function also adds a declaration of this
100-
# function to the BoostPythonModule which
101-
# is responsible for the host function.
102103
cuda_mod.add_function(diff_instance)
103104

104-
import codepy.jit
105-
import codepy.toolchain
106-
107-
108-
gcc_toolchain = codepy.toolchain.guess_toolchain()
109-
nvcc_toolchain = codepy.toolchain.guess_nvcc_toolchain()
110-
105+
gcc_toolchain = guess_toolchain()
106+
nvcc_toolchain = guess_nvcc_toolchain()
111107
module = cuda_mod.compile(gcc_toolchain, nvcc_toolchain, debug=True)
112-
import numpy as np
113-
import pycuda.autoinit
114-
import pycuda.driver
115-
import pycuda.gpuarray
116-
117108

118-
length = 25
119-
constant_value = 2
120109
# This is a strange way to create a GPUArray, but is meant to illustrate
121110
# how to construct a GPUArray if the GPU buffer it owns has been
122111
# created by something else
123112

113+
length = 25
114+
constant_value = 2
115+
124116
pointer = pycuda.driver.mem_alloc(length * 4)
125117
pycuda.driver.memset_d32(pointer, constant_value, length)
126118
a = pycuda.gpuarray.GPUArray((length,), np.int32, gpudata=pointer)
@@ -129,6 +121,7 @@
129121
golden = [constant_value] + [0] * (length - 1)
130122
difference = [(x-y)*(x-y) for x, y in zip(b, golden, strict=True)]
131123
error = sum(difference)
124+
132125
if error == 0:
133126
print("Test passed!")
134127
else:

0 commit comments

Comments
 (0)