Skip to content

Commit 16fd21d

Browse files
committed
Interfaces are added.
1 parent 46af6d0 commit 16fd21d

File tree

6 files changed

+175
-4
lines changed

6 files changed

+175
-4
lines changed

2021-03-23-Note-10-09.xopp

952 KB
Binary file not shown.

src/ImgProcessing/ImgProcessing.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="AssemblyInfo.fs" />
2222
<Compile Include="ImgHelpers.fs" />
2323
<Compile Include="Streaming.fs" />
24+
<Compile Include="Matrices.fs" />
2425
<Compile Include="Main.fs" />
2526
<None Include="YC.OpenCL.NET.dll.config">
2627
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

src/ImgProcessing/Main.fs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module Main =
1919
let final = (System.DateTime.Now - start).TotalMilliseconds / 1000.0
2020
r,final
2121

22-
[<EntryPoint>]
23-
let main (argv: string array) =
22+
let imgPerf () =
2423
let img1 = "../../../Examples/sara-budhwani-h_P71-B8BPw-unsplash.jpg"
2524
let img2 = "../../../Examples/nick-gavrilov-F-rvSJl6qI0-unsplash.jpg"
2625
let inDir = "../../../Examples/input"
@@ -47,9 +46,22 @@ module Main =
4746
|> filter ImgHelpers.edgesKernel
4847
*)
4948
//let r,t = time r
50-
//let _do () = ImgPorcessing.Streaming.processAllFiles inDir outDir filters
51-
let _do () = ImgPorcessing.Streaming.processAllFilesAsync inDir outDir filters
49+
let _do () = ImgPorcessing.Streaming.processAllFiles inDir outDir filters
50+
//let _do () = ImgPorcessing.Streaming.processAllFilesAsync inDir outDir filters
5251
let t = time _do
5352
printfn "Execution time: %A seconds" t
5453
//ImgHelpers.save2DByteArrayAsImage r (img2 + "_out_blur_edg_10_gpu_2_br.jpg")
54+
55+
[<EntryPoint>]
56+
let main (argv: string array) =
57+
(*let platform, queue = Matrices.getCommandQueue "*Intel*"
58+
59+
let m1 = Matrices.getRandomMatrix 4096 4096
60+
let m2 = Matrices.getRandomMatrix 4096 4096
61+
62+
let res, time = time (fun () -> Matrices.multiply platform queue 16 m1 m2)
63+
64+
printfn "Time: %A" time
65+
*)
66+
imgPerf ()
5567
0

src/ImgProcessing/Matrices.fs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module ImgPorcessing.Matrices
2+
3+
open OpenCL.Net
4+
open Brahma.OpenCL
5+
open Brahma.FSharp.OpenCL.Core
6+
open Brahma.FSharp.OpenCL.Extensions
7+
8+
9+
let getCommandQueue platformName =
10+
let deviceType = DeviceType.Default
11+
12+
let provider =
13+
try ComputeProvider.Create(platformName, deviceType)
14+
with
15+
| ex -> failwith ex.Message
16+
17+
let mutable commandQueue = new CommandQueue(provider, provider.Devices |> Seq.head)
18+
19+
provider, commandQueue
20+
21+
let getRandomMatrix n m =
22+
let rnd = System.Random()
23+
let res = Array2D.zeroCreate n m
24+
for i in 0 .. n - 1 do
25+
for j in 0 .. m - 1 do
26+
res.[i,j] <- rnd.NextDouble()
27+
res
28+
let multiply (provider:ComputeProvider) (commandQueue:CommandQueue) localWorkSize (m1: float[,]) (m2: _[,]) =
29+
let m1Rows = m1.GetLength 0
30+
let m1Cols = m1.GetLength 1
31+
32+
let m2Rows = m2.GetLength 0
33+
let m2Cols = m2.GetLength 1
34+
35+
let resRows = m1Rows
36+
let resCols = m2Cols
37+
38+
let mToArr m =
39+
[| for x in 0 .. Array2D.length1 m - 1 do
40+
yield! [| for y in 0 .. Array2D.length2 m - 1 -> m.[x, y] |]
41+
|]
42+
43+
let m1Arr = mToArr m1
44+
let m2Arr = mToArr m2
45+
46+
let command =
47+
<@
48+
fun (r:_2D) (m1:array<_>) (m2:array<_>) (res:array<_>) ->
49+
let row = r.GlobalID0
50+
let col = r.GlobalID1
51+
let mutable buf = res.[row * resCols + col]
52+
for k in 0 .. m2Rows - 1 do
53+
//res.[row * resCols + col] <- res.[row * resCols + col] + (m1.[row * m1Cols + k] * m2.[k * m2Cols + col])
54+
buf <- buf + (m1.[row * m1Cols + k] * m2.[k * m2Cols + col])
55+
res.[row * resCols + col] <- buf
56+
@>
57+
58+
let kernel, kernelPrepare, kernelRun =
59+
provider.Compile command
60+
61+
let d = _2D(resRows, resCols, localWorkSize)
62+
let result' = Array.zeroCreate (resRows * resCols)
63+
64+
kernelPrepare d m1Arr m2Arr result'
65+
66+
commandQueue.Add(kernelRun()) |> ignore
67+
let _ = commandQueue.Add(result'.ToHost provider).Finish()
68+
69+
let result = Array2D.zeroCreate resRows resCols
70+
71+
Array.Parallel.iteri (fun x v -> result.[x / resCols, x % resCols] <- v) result'
72+
73+
provider.CloseAllBuffers()
74+
75+
result

src/Lecture10/Interfaces.fs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
module Lecture10.Interfaces
2+
3+
type IMatrix<'elem> =
4+
abstract Item: int * int -> 'elem with get, set
5+
abstract Size: int * int with get
6+
7+
type MyMatrix<'elem> (n,m) =
8+
let store = Array2D.zeroCreate n m
9+
interface IMatrix<'elem> with
10+
member this.Item
11+
with get (i, j) = store.[i, j]
12+
and set (i, j) v = store.[i, j] <- v
13+
14+
member this.Size = n,m
15+
16+
type ISmb<'t> =
17+
abstract PrettyString: string
18+
19+
type Smb<'t> =
20+
| Eps
21+
| Smb of 't
22+
interface ISmb<'t> with
23+
member this.PrettyString =
24+
match this with
25+
| Eps -> "Eps"
26+
| Smb t -> t.ToString()
27+
28+
[<AbstractClass>]
29+
type FA<'smb> () =
30+
abstract StartState: int with get//, set
31+
abstract FinalState: int with get//, set
32+
abstract TransitionsList : array<int * int * ISmb<'smb>>
33+
member this.ToDot (outFile) =
34+
let header =
35+
[
36+
"digraph nfa"
37+
"{"
38+
"rankdir = LR"
39+
"node [shape = circle];"
40+
sprintf "%A[shape = circle, label = \"%A_Start\"]" this.StartState this.StartState
41+
]
42+
43+
let footer =
44+
[
45+
sprintf "%A[shape = doublecircle]" this.FinalState
46+
"}"
47+
]
48+
49+
let content =
50+
[
51+
for (s,f,t) in this.TransitionsList do
52+
sprintf
53+
"%A -> %A [label = \"%s\"]"
54+
s
55+
f
56+
t.PrettyString
57+
]
58+
59+
System.IO.File.WriteAllLines (outFile, header @ content @ footer)
60+
61+
62+
type MyDFA<'smb> (transitions, startSate, finalState) =
63+
inherit FA<'smb>()
64+
override this.StartState = startSate
65+
override this.FinalState = finalState
66+
override this.TransitionsList =
67+
let n,m = this.Transitions.Size
68+
[|
69+
for i in 0 .. n - 1 do
70+
for j in 0 .. m - 1 do
71+
yield i, j, this.Transitions.[i,j]
72+
|]
73+
74+
member this.Transitions: IMatrix<ISmb<'smb>> = transitions
75+
76+
77+
78+
let m = new MyMatrix<_>(2,3) :> IMatrix<_>
79+
m.[1,2] <- Smb(0) :> ISmb<_>
80+
81+
let fa = new MyDFA<_>(m,1,0)
82+
fa.ToDot "file"

src/Lecture10/Lecture10.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<Compile Include="Automata.fs" />
2323
<Compile Include="Regexp.fs" />
2424
<Compile Include="Main.fs" />
25+
<Compile Include="Interfaces.fs" />
2526
</ItemGroup>
2627
<Import Project="..\..\.paket\Paket.Restore.targets" />
2728
</Project>

0 commit comments

Comments
 (0)