File size: 5,895 Bytes
66c9c8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright (c) 2023 NVIDIA CORPORATION.  All rights reserved.
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

###########################################################################
# Bechmarks for kernel launches with different types of args
###########################################################################

import warp as wp


@wp.struct
class S0:
    pass


@wp.struct
class Sf:
    x: float
    y: float
    z: float


@wp.struct
class Sv:
    u: wp.vec3
    v: wp.vec3
    w: wp.vec3


@wp.struct
class Sm:
    M: wp.mat33
    N: wp.mat33
    O: wp.mat33


@wp.struct
class Sa:
    a: wp.array(dtype=float)
    b: wp.array(dtype=float)
    c: wp.array(dtype=float)


@wp.struct
class Sz:
    a: wp.array(dtype=float)
    b: wp.array(dtype=float)
    c: wp.array(dtype=float)
    x: float
    y: float
    z: float
    u: wp.vec3
    v: wp.vec3
    w: wp.vec3


@wp.kernel
def k0():
    tid = wp.tid()


@wp.kernel
def kf(x: float, y: float, z: float):
    tid = wp.tid()


@wp.kernel
def kv(u: wp.vec3, v: wp.vec3, w: wp.vec3):
    tid = wp.tid()


@wp.kernel
def km(M: wp.mat33, N: wp.mat33, O: wp.mat33):
    tid = wp.tid()


@wp.kernel
def ka(a: wp.array(dtype=float), b: wp.array(dtype=float), c: wp.array(dtype=float)):
    tid = wp.tid()


@wp.kernel
def kz(
    a: wp.array(dtype=float),
    b: wp.array(dtype=float),
    c: wp.array(dtype=float),
    x: float,
    y: float,
    z: float,
    u: wp.vec3,
    v: wp.vec3,
    w: wp.vec3,
):
    tid = wp.tid()


@wp.kernel
def ks0(s: S0):
    tid = wp.tid()


@wp.kernel
def ksf(s: Sf):
    tid = wp.tid()


@wp.kernel
def ksv(s: Sv):
    tid = wp.tid()


@wp.kernel
def ksm(s: Sm):
    tid = wp.tid()


@wp.kernel
def ksa(s: Sa):
    tid = wp.tid()


@wp.kernel
def ksz(s: Sz):
    tid = wp.tid()


wp.init()

wp.build.clear_kernel_cache()

devices = wp.get_devices()
num_launches = 100000

for device in devices:
    with wp.ScopedDevice(device):
        print(f"\n=================== Device '{device}' ===================")

        wp.force_load(device)

        n = 1
        a = wp.zeros(n, dtype=float)
        b = wp.zeros(n, dtype=float)
        c = wp.zeros(n, dtype=float)
        x = 17.0
        y = 42.0
        z = 99.0
        u = wp.vec3(1, 2, 3)
        v = wp.vec3(10, 20, 30)
        w = wp.vec3(100, 200, 300)
        M = wp.mat33()
        N = wp.mat33()
        O = wp.mat33()

        s0 = S0()

        sf = Sf()
        sf.x = x
        sf.y = y
        sf.z = z

        sv = Sv()
        sv.u = u
        sv.v = v
        sv.w = w

        sm = Sm()
        sm.M = M
        sm.N = N
        sm.O = O

        sa = Sa()
        sa.a = a
        sa.b = b
        sa.c = c

        sz = Sz()
        sz.a = a
        sz.b = b
        sz.c = c
        sz.x = x
        sz.y = y
        sz.z = z
        sz.u = u
        sz.v = v
        sz.w = w

        tk0 = wp.ScopedTimer("k0")
        tkf = wp.ScopedTimer("kf")
        tkv = wp.ScopedTimer("kv")
        tkm = wp.ScopedTimer("km")
        tka = wp.ScopedTimer("ka")
        tkz = wp.ScopedTimer("kz")

        ts0 = wp.ScopedTimer("s0")
        tsf = wp.ScopedTimer("sf")
        tsv = wp.ScopedTimer("sv")
        tsm = wp.ScopedTimer("sm")
        tsa = wp.ScopedTimer("sa")
        tsz = wp.ScopedTimer("sz")

        wp.synchronize_device()

        with tk0:
            for _ in range(num_launches):
                wp.launch(k0, dim=1, inputs=[])

        wp.synchronize_device()

        with tkf:
            for _ in range(num_launches):
                wp.launch(kf, dim=1, inputs=[x, y, z])

        wp.synchronize_device()

        with tkv:
            for _ in range(num_launches):
                wp.launch(kv, dim=1, inputs=[u, v, w])

        wp.synchronize_device()

        with tkm:
            for _ in range(num_launches):
                wp.launch(km, dim=1, inputs=[M, N, O])

        wp.synchronize_device()

        with tka:
            for _ in range(num_launches):
                wp.launch(ka, dim=1, inputs=[a, b, c])

        wp.synchronize_device()

        with tkz:
            for _ in range(num_launches):
                wp.launch(kz, dim=1, inputs=[a, b, c, x, y, z, u, v, w])

        # structs

        wp.synchronize_device()

        with ts0:
            for _ in range(num_launches):
                wp.launch(ks0, dim=1, inputs=[s0])

        wp.synchronize_device()

        with tsf:
            for _ in range(num_launches):
                wp.launch(ksf, dim=1, inputs=[sf])

        wp.synchronize_device()

        with tsv:
            for _ in range(num_launches):
                wp.launch(ksv, dim=1, inputs=[sv])

        wp.synchronize_device()

        with tsm:
            for _ in range(num_launches):
                wp.launch(ksm, dim=1, inputs=[sm])

        wp.synchronize_device()

        with tsa:
            for _ in range(num_launches):
                wp.launch(ksa, dim=1, inputs=[sa])

        wp.synchronize_device()

        with tsz:
            for _ in range(num_launches):
                wp.launch(ksz, dim=1, inputs=[sz])

        wp.synchronize_device()

        timers = [
            [tk0, ts0],
            [tkf, tsf],
            [tkv, tsv],
            [tkm, tsm],
            [tka, tsa],
            [tkz, tsz],
        ]

        print("--------------------------------")
        print(f"| args |    direct |    struct |")
        print("--------------------------------")
        for tk, ts in timers:
            print(f"|  {tk.name}  |{tk.elapsed:10.0f} |{ts.elapsed:10.0f} |")
        print("--------------------------------")