-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathProgram.fs
More file actions
317 lines (257 loc) · 8.56 KB
/
Copy pathProgram.fs
File metadata and controls
317 lines (257 loc) · 8.56 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
open System.Diagnostics
open System
open System.Runtime.InteropServices
open Tensor
open Tensor.Utils
module Util =
/// true if running on Windows
let onWindows =
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
/// true if running on Linux
let onLinux =
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
[<Flags>]
type ErrorModes =
| SYSTEM_DEFAULT = 0x0
| SEM_FAILCRITICALERRORS = 0x0001
| SEM_NOALIGNMENTFAULTEXCEPT = 0x0004
| SEM_NOGPFAULTERRORBOX = 0x0002
| SEM_NOOPENFILEERRORBOX = 0x8000
[<DllImport("kernel32.dll")>]
extern ErrorModes private SetErrorMode(ErrorModes mode)
/// disables the Windows WER dialog box on crash of this application
let disableCrashDialog () =
if onWindows then
SetErrorMode(ErrorModes.SEM_NOGPFAULTERRORBOX |||
ErrorModes.SEM_FAILCRITICALERRORS |||
ErrorModes.SEM_NOOPENFILEERRORBOX)
|> ignore
let testCuda () =
let shape = [5L; 5L]
//Tensor.Cuda.Backend.Cfg.DebugCompile <- true
Tensor.Cuda.Cfg.Stacktrace <- true
let a = HostTensor.ones<single> shape
printfn "a=\n%A" a
printfn "copy to cuda..."
let ca = a |> CudaTensor.transfer
printfn "ca=\n%A" ca.Full
printfn "copy to host..."
let ha = ca |> HostTensor.transfer
printfn "ha=\n%A" ha
printfn "copy cuda to cuda..."
let cb = Tensor.copy ca
printfn "cb=\n%A" cb
printfn "fill cuda..."
cb.FillConst(-5.5f)
printfn "cb=\n%A" cb
printfn "cuda diagonal * 3..."
let cdiag = 3.0f * CudaTensor.identity 3L
printfn "cdiag=\n%A" cdiag
printfn "cuda invert..."
//let cinv = Tensor.invert cb
let cinv = Tensor.invert cdiag
printfn "cinv=\n%A" cinv
printfn "cuda abs..."
let cb = abs cb
printfn "cb=\n%A" cb
printfn "cuda ca + cb..."
let cc = ca + cb
printfn "cc=\n%A" cc
printfn "cuda sumAxis cc..."
let ci = Tensor.sumAxis 0 cc
printfn "ci=\n%A" ci
printfn "cuda maxAxis cc..."
let cg = Tensor.maxAxis 0 cc
printfn "cg=\n%A" cg
printfn "cuda argMinAxis cc..."
let cj = Tensor.argMinAxis 1 cc
printfn "cj=\n%A" cj
printfn "cuda convert to int..."
let cgInt = Tensor<int>.convert cg
printfn "cgInt=\n%A" cgInt
printfn "cuda ca <<== cb..."
let cd = ca <<== cb
printfn "cd=\n%A" cd
printfn "not cd..."
let ce = ~~~~cd
printfn "ce=\n%A" ce
printfn "ifthenelse..."
let ck = Tensor.ifThenElse ce ca cc
printfn "ck=\n%A" ck
printfn "cd or ce..."
let cf = cd |||| ce
printfn "cf=\n%A" cf
printfn "all cf..."
let ch = Tensor.allAxis 0 cf
printfn "ch=\n%A" ch
printfn "cuda gather..."
let idxs0 = CudaTensor.zeros<int64> [3L; 3L]
let cgather = Tensor.gather [Some idxs0; None] cb
printfn "cgather=\n%A" cgather
printfn "cuda scatter..."
let idxs1 = CudaTensor.ones<int64> shape
let cscatter = Tensor.scatter [Some idxs1; None] [6L; 6L] cb
printfn "cscatter=\n%A" cscatter
printfn "cuda dot..."
let c1 = cscatter .* cscatter
printfn "c1=\n%A" c1
printfn "cuda replicate..."
let cscatterRep = Tensor.replicate 0 2L cscatter.[NewAxis, *, *]
printfn "cscatterRep=\n%A" cscatterRep
printfn "cuda batched dot..."
let c2 = cscatterRep .* cscatterRep
printfn "c2=\n%A" c2
[<EntryPoint>]
let main argv =
Util.disableCrashDialog ()
let dev =
match List.ofArray argv with
| ["cuda"] -> CudaTensor.Dev
| ["host"] | [] -> HostTensor.Dev
| _ ->
printfn "unknown device"
exit 1
let cudaCtx =
if dev = CudaTensor.Dev then Some (new ManagedCuda.CudaContext(createNew=false))
else None
let shape = [10000L; 1000L]
let sync () =
if dev = CudaTensor.Dev then
cudaCtx.Value.Synchronize()
let collect () =
()
printfn "On device %A" dev
printfn "Shape: %A" shape
let a : Tensor<single> = Tensor.zeros dev shape
let b : Tensor<single> = Tensor.ones dev shape
let mutable c : Tensor<single> = Tensor.zeros dev shape
let mutable d : Tensor<bool> = Tensor.falses dev shape
a.[[0L; 0L]] <- -1.0f
a.[[0L; 1L]] <- 3.0f
b.[[0L; 0L]] <- 2.0f
let iters = 30
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- a + b
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Plus time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- abs a
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Abs time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- a.T .* b
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Dot time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- sin a
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Sin time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- exp a
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Exp time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- sqrt a
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Sqrt time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- sgn a
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Sgn time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- a / b
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Divide time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- a % b
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Modulo time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
c <- a ** b
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Power time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
d <- a <<<< b
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Less time per iteration: %.3f ms" timePerIter
collect()
//printfn "a:\n%A" a
//printfn "b:\n%A" b
//printfn "c:\n%A" c
//printfn "d:\n%A" d
//printfn "sgn -1.0f: %f sgn 0.0f: %f" (sgn -1.0f) (sgn 0.0f)
//printfn "ndims a: %d" (Tensor.nDims a)
let m : Tensor<bool> = Tensor.falses dev shape
let n : Tensor<bool> = Tensor.trues dev shape
let mutable k : Tensor<bool> = Tensor.falses dev shape
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
k <- ~~~~m
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Not time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
k <- m &&&& n
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "And time per iteration: %.3f ms" timePerIter
collect()
let startTime = Stopwatch.StartNew()
for i in 1 .. iters do
k <- m |||| n
sync ()
let duration = startTime.ElapsedMilliseconds
let timePerIter = float duration / float iters
printfn "Or time per iteration: %.3f ms" timePerIter
collect()
//printfn "m:\n%A" m
//printfn "n:\n%A" n
//printfn "k:\n%A" k
0