Skip to content

Commit 786e6b3

Browse files
committed
Min-plus semiring. Cmd args
1 parent 7761ebd commit 786e6b3

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/MatrixMultiplication/Matrices.fs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module ImageProcessing.Matrices
22

33
open Brahma.FSharp
44

5+
type Kernels = K1 = 1 | K2 = 2
56
let rand = new System.Random()
67

78
let getRandomMatrix (n: uint) init =
@@ -21,7 +22,9 @@ let check opAdd opMult zero (m1 : array<array<_>>) (m2: array<array<_>>) (m3:arr
2122

2223

2324
let getRandomIntMatrix n= getRandomMatrix n (fun i -> rand.Next(-10,10))
24-
let getRandomFloatMatrix n= getRandomMatrix n (fun i -> rand.NextDouble())
25+
let getRandomByteMatrix n= getRandomMatrix n (fun i -> rand.Next() |> byte)
26+
let getRandomFloat32Matrix n= getRandomMatrix n (fun i -> rand.NextSingle())
27+
let getRandomOptionIntMatrix n= getRandomMatrix n (fun i -> let x = rand.Next(-10,10) in if x % 3 = 0 then Some x else None)
2528

2629
let multiplyKernel2 (clContext: ClContext) (localWorkSize:uint) opAdd opMult zero =
2730
let localWorkSize = int localWorkSize
@@ -100,38 +103,40 @@ let multiplyKernel1 (clContext: ClContext) (localWorkSize: uint) opAdd opMult ze
100103
commandQueue.Post(Msg.CreateRunMsg<_, _> kernel)
101104
m3
102105

103-
let applyMultiplyGPU<'a,'b,'e,'f> (clContext: ClContext) localWorkSize (opAdd:Quotations.Expr<'a -> 'b -> 'a>) (opMult:Quotations.Expr<'e -> 'f -> 'b>) (zero:'a) =
106+
let applyMultiplyGPU<'a,'b,'e,'f> (kernel:Kernels) (clContext: ClContext) localWorkSize (opAdd:Quotations.Expr<'a -> 'b -> 'a>) (opMult:Quotations.Expr<'e -> 'f -> 'b>) (zero:'a) =
104107
//let kernel = multiplyKernel1 clContext localWorkSize opAdd opMult zero
105-
let kernel = multiplyKernel2 clContext localWorkSize opAdd opMult zero
108+
let kernel =
109+
match kernel with
110+
| Kernels.K1 -> multiplyKernel1 clContext localWorkSize opAdd opMult zero
111+
| Kernels.K2 -> multiplyKernel2 clContext localWorkSize opAdd opMult zero
112+
| x -> failwithf $"Unexpected kernel {x}."
106113
let queue = clContext.QueueProvider.CreateQueue()
107114

108115
fun (m1: 'e[][]) (m2: 'f[][]) ->
109-
printfn "!!!1!!!"
110-
116+
111117
let m1_gpu =
112118
clContext.CreateClArray<_>(Array.concat m1, HostAccessMode.NotAccessible)
113-
printfn "!!!2!!!"
119+
114120
let m2_gpu =
115121
clContext.CreateClArray<_>(Array.concat m2, HostAccessMode.NotAccessible)
116-
printfn "!!!3!!!"
117-
122+
118123
let m3_gpu =
119124
clContext.CreateClArray(
120125
m1.Length * m1.Length,
121126
HostAccessMode.NotAccessible,
122127
allocationMode = AllocationMode.Default
123128
)
124-
printfn "!!!4!!!"
129+
125130
let x = kernel queue m1_gpu m2_gpu m3_gpu m1.Length
126-
printfn "!!!5!!!"
131+
127132
let result : 'a[] = Array.zeroCreate(m1.Length * m1.Length)
128-
printfn "!!!6!!!"
133+
129134
let result = queue.PostAndReply(fun ch -> Msg.CreateToHostMsg(m3_gpu, result, ch))
130-
printfn "!!!7!!!"
135+
131136
queue.Post(Msg.CreateFreeMsg m1_gpu)
132-
printfn "!!!8!!!"
137+
133138
queue.Post(Msg.CreateFreeMsg m2_gpu)
134-
printfn "!!!9!!!"
139+
135140
queue.Post(Msg.CreateFreeMsg m3_gpu)
136-
printfn "!!!10!!!"
141+
137142
result

0 commit comments

Comments
 (0)