Skip to content

Commit 4fae94a

Browse files
committed
Fixed incorrect work of function, added console application for tests
1 parent 7fa89ce commit 4fae94a

4 files changed

Lines changed: 148 additions & 27 deletions

File tree

GraphBLAS-sharp.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "GraphBLAS-sharp.Benchmarks"
1717
EndProject
1818
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "docsTool", "docsTool\docsTool.fsproj", "{8855EC73-F6A1-43D3-AFBC-04A3E09F9BD9}"
1919
EndProject
20+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "App", "src\App\App.fsproj", "{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}"
21+
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2224
Debug|Any CPU = Debug|Any CPU
@@ -78,10 +80,23 @@ Global
7880
{4C6EB3D0-B6BF-4FF5-BC77-CC7CB3F307E6}.Release|x64.Build.0 = Release|Any CPU
7981
{4C6EB3D0-B6BF-4FF5-BC77-CC7CB3F307E6}.Release|x86.ActiveCfg = Release|Any CPU
8082
{4C6EB3D0-B6BF-4FF5-BC77-CC7CB3F307E6}.Release|x86.Build.0 = Release|Any CPU
83+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
84+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
85+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Debug|x64.ActiveCfg = Debug|Any CPU
86+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Debug|x64.Build.0 = Debug|Any CPU
87+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Debug|x86.ActiveCfg = Debug|Any CPU
88+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Debug|x86.Build.0 = Debug|Any CPU
89+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
90+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Release|Any CPU.Build.0 = Release|Any CPU
91+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Release|x64.ActiveCfg = Release|Any CPU
92+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Release|x64.Build.0 = Release|Any CPU
93+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Release|x86.ActiveCfg = Release|Any CPU
94+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6}.Release|x86.Build.0 = Release|Any CPU
8195
EndGlobalSection
8296
GlobalSection(NestedProjects) = preSolution
8397
{5D30E174-2538-47AC-8443-318C8C5DC2C9} = {C397A34C-84F1-49E7-AEBC-2F9F2B196216}
8498
{1CA2E092-2320-451D-A4F0-9ED7C7C528CA} = {ACBEE43C-7A88-4FB1-9B06-DB064D22B29F}
8599
{4C6EB3D0-B6BF-4FF5-BC77-CC7CB3F307E6} = {DEF656DE-BCED-4C49-B5ED-950D4A29B78B}
100+
{8B791266-DD0A-4A3A-B3B0-FAC5D8A364F6} = {C397A34C-84F1-49E7-AEBC-2F9F2B196216}
86101
EndGlobalSection
87102
EndGlobal

src/App/App.fsproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<Compile Include="Program.fs" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\GraphBLAS-sharp\GraphBLAS-sharp.fsproj" />
14+
</ItemGroup>
15+
16+
</Project>

src/App/Program.fs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
open System
2+
open GraphBLAS.FSharp
3+
open GraphBLAS.FSharp.Predefined
4+
open Brahma.OpenCL
5+
open Brahma.FSharp.OpenCL.Core
6+
open Brahma.FSharp.OpenCL.Extensions
7+
open GlobalContext
8+
open Helpers
9+
open FSharp.Quotations.Evaluator
10+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Basic
11+
open Brahma.FSharp.OpenCL.WorkflowBuilder.Evaluation
12+
13+
[<EntryPoint>]
14+
let main argv =
15+
16+
// let leftVector = SparseVector<float>(30, [| 0;2;4;6;8;10;12;14 |], [| 4.3; 5.5; 32.4; 56.43; 54.67; 563.43; 765.43; 23.43 |])
17+
// let rightVector = SparseVector<float>(30, [| 1;3;5;7;9;11;13;15 |], [| -4.3; -5.5; -32.4; -0.43; 32.4; 56.43; 54.67; 563.43 |])
18+
19+
let leftVector = SparseVector<int>(100, [| 1;2;3;4;5 |], [| 5; 32; 56; 54; 563 |])
20+
let rightVector = SparseVector<int>(100, [| 1;4;5;6;7;8;9 |], [| 32; -54; 54; 563; 563; 765; 23 |])
21+
22+
// let command =
23+
// <@
24+
// fun (ndRange: _1D)
25+
// (array: int[]) ->
26+
27+
// let i = ndRange.GlobalID0
28+
// array.[i] <- if i % 2 = 0 then 42 else 43
29+
// @>
30+
31+
// let wf =
32+
// opencl {
33+
// let xs = [| 0 .. 10 |]
34+
// let binder kernelP =
35+
// let ndRange = _1D(xs.Length)
36+
// kernelP
37+
// ndRange
38+
// xs
39+
// do! RunCommand command binder
40+
// return! ToHost xs
41+
// }
42+
43+
// let ys = oclContext.RunSync wf
44+
// printf "ys:"
45+
// for i in 0 .. ys.Length - 1 do
46+
// printf "%i " ys.[i]
47+
// printfn ""
48+
49+
let workflow =
50+
opencl {
51+
let! resultVector = leftVector.EWiseAdd rightVector None IntegerSemiring.addMult
52+
return! resultVector.ToHost()
53+
}
54+
55+
let resultVector : SparseVector<int> = downcast oclContext.RunSync workflow
56+
57+
let indices = resultVector.Indices
58+
let values = resultVector.Values
59+
60+
for i in 0 .. indices.Length - 1 do
61+
printfn "(%i, %A)" indices.[i] values.[i]
62+
63+
0

src/GraphBLAS-sharp/Implementations.fs

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,12 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
127127
override this.Copy () = failwith "Not Implemented"
128128
override this.Resize a = failwith "Not Implemented"
129129
override this.GetNNZ () = failwith "Not Implemented"
130-
override this.GetTuples () = failwith "Not Implemented"
130+
131+
override this.GetTuples () =
132+
opencl {
133+
return {| Indices = this.Indices; Values = this.Values |}
134+
}
135+
131136
override this.GetMask(?isComplemented: bool) =
132137
let isComplemented = defaultArg isComplemented false
133138
failwith "Not Implemented"
@@ -151,7 +156,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
151156
member internal this.CalcPrefixSum
152157
(inputArray: int[]) =
153158

154-
let outputArray = Array.copy inputArray
159+
let outputArray = Array.zeroCreate inputArray.Length
155160

156161
if inputArray.Length = 1
157162
then
@@ -177,6 +182,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
177182
}
178183
else
179184
let intermediateArray = Array.zeroCreate ((inputArray.Length + 1) / 2)
185+
let inputArrayLength = inputArray.Length
180186

181187
let fillIntermediateArray =
182188
<@
@@ -185,7 +191,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
185191
(intermediateArrayBuffer: int[]) ->
186192

187193
let i = ndRange.GlobalID0
188-
if 2 * i + 1 < inputArrayBuffer.Length
194+
if 2 * i + 1 < inputArrayLength
189195
then intermediateArrayBuffer.[i] <- inputArrayBuffer.[2 * i] + inputArrayBuffer.[2 * i + 1]
190196
else intermediateArrayBuffer.[i] <- inputArrayBuffer.[2 * i]
191197
@>
@@ -208,10 +214,13 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
208214
(inputArrayBuffer: int[])
209215
(outputArrayBuffer: int[]) ->
210216

211-
let i = ndRange.GlobalID0 + 1
217+
let i = ndRange.GlobalID0
212218
let j = (i - 1) / 2
213219
if i % 2 = 0
214-
then outputArrayBuffer.[i] <- auxiliaryPrefixSumArrayBuffer.[j] + inputArrayBuffer.[i]
220+
then
221+
if i = 0
222+
then outputArrayBuffer.[i] <- inputArrayBuffer.[i]
223+
else outputArrayBuffer.[i] <- auxiliaryPrefixSumArrayBuffer.[j] + inputArrayBuffer.[i]
215224
else outputArrayBuffer.[i] <- auxiliaryPrefixSumArrayBuffer.[j]
216225
@>
217226

@@ -220,7 +229,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
220229
let! auxiliaryPrefixSumArray = this.CalcPrefixSum intermediateArray
221230

222231
let binder kernelP =
223-
let ndRange = _1D(inputArray.Length - 1)
232+
let ndRange = _1D(inputArray.Length)
224233
kernelP
225234
ndRange
226235
auxiliaryPrefixSumArray
@@ -245,9 +254,6 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
245254
then this.Indices, this.Values, vector.Indices, vector.Values, append
246255
else vector.Indices, vector.Values, this.Indices, this.Values, <@ fun x y -> (%append) y x @>
247256

248-
let longSide = firstIndices.Length
249-
let shortSide = secondIndices.Length
250-
251257
let filterThroughMask =
252258
opencl {
253259
//TODO
@@ -257,6 +263,9 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
257263
let allIndices = Array.zeroCreate <| firstIndices.Length + secondIndices.Length
258264
let allValues = Array.init (firstIndices.Length + secondIndices.Length) (fun _ -> zero)
259265

266+
let longSide = firstIndices.Length
267+
let shortSide = secondIndices.Length
268+
260269
let createSortedConcatenation =
261270
<@
262271
fun (ndRange: _1D)
@@ -269,8 +278,23 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
269278

270279
let i = ndRange.GlobalID0
271280

272-
let mutable leftEdge = max 0 (i + 1 - shortSide)
273-
let mutable rightEdge = min i (longSide - 1)
281+
let f n =
282+
if 0 > n + 1 - shortSide
283+
then 0
284+
else n + 1 - shortSide
285+
let mutable leftEdge = f i
286+
// if 0 > i + 1 - shortSide
287+
// then 0
288+
// else i + 1 - shortSide
289+
290+
let g n =
291+
if n > longSide - 1
292+
then longSide - 1
293+
else n
294+
let mutable rightEdge = g i
295+
// if i > longSide - 1
296+
// then longSide - 1
297+
// else i
274298

275299
while leftEdge <= rightEdge do
276300
let middleIdx = (leftEdge + rightEdge) / 2
@@ -304,7 +328,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
304328
do! RunCommand createSortedConcatenation binder
305329
}
306330

307-
let auxiliaryArray = Array.zeroCreate allIndices.Length
331+
let auxiliaryArray = Array.init allIndices.Length (fun _ -> 1)
308332

309333
let fillAuxiliaryArray =
310334
<@
@@ -320,7 +344,6 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
320344
auxiliaryArrayBuffer.[i + 1] <- 0
321345
//Prepare to drop explicit zeroes
322346
allValuesBuffer.[i] <- (%plus) allValuesBuffer.[i] allValuesBuffer.[i + 1]
323-
else auxiliaryArrayBuffer.[i + 1] <- 1
324347
@>
325348

326349
let fillAuxiliaryArray =
@@ -372,7 +395,7 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
372395

373396
if auxiliaryArrayBuffer.[i] = 1
374397
then
375-
let index = prefixSumArrayBuffer.[i]
398+
let index = prefixSumArrayBuffer.[i] - 1
376399

377400
resultIndicesBuffer.[index] <- allIndicesBuffer.[i]
378401
resultValuesBuffer.[index] <- allValuesBuffer.[i]
@@ -381,24 +404,28 @@ and SparseVector<'a when 'a : struct and 'a : equality>(size: int, indices: int[
381404
let resultIndices = Array.zeroCreate allIndices.Length
382405
let resultValues = Array.init allValues.Length (fun _ -> zero)
383406

407+
let createUnion =
408+
opencl {
409+
let! prefixSumArray = this.CalcPrefixSum auxiliaryArray
410+
let binder kernelP =
411+
let ndRange = _1D(auxiliaryArray.Length)
412+
kernelP
413+
ndRange
414+
allIndices
415+
allValues
416+
auxiliaryArray
417+
prefixSumArray
418+
resultIndices
419+
resultValues
420+
do! RunCommand createUnion binder
421+
}
422+
384423
opencl {
385424
do! createSortedConcatenation
386425
do! filterThroughMask
387426
do! fillAuxiliaryArray
388427
do! dropExplicitZeroes
389-
390-
let! prefixSumArray = this.CalcPrefixSum auxiliaryArray
391-
let binder kernelP =
392-
let ndRange = _1D(auxiliaryArray.Length)
393-
kernelP
394-
ndRange
395-
allIndices
396-
allValues
397-
auxiliaryArray
398-
prefixSumArray
399-
resultIndices
400-
resultValues
401-
do! RunCommand createUnion binder
428+
do! createUnion
402429

403430
return upcast SparseVector<'a>(this.Size, resultIndices, resultValues)
404431
}

0 commit comments

Comments
 (0)