forked from SparseLinearAlgebra/GraphBLAS-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.fs
156 lines (122 loc) · 4.89 KB
/
Helpers.fs
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
namespace rec GraphBLAS.FSharp.Benchmarks
namespace GraphBLAS.FSharp.Benchmarks
open Brahma.FSharp
open Brahma.FSharp.OpenCL.Translator
open Brahma.FSharp.OpenCL.Translator.QuotationTransformers
open OpenCL.Net
open System.IO
open System.Text.RegularExpressions
open GraphBLAS.FSharp.Tests
open FsCheck
open Expecto
module Utils =
type BenchmarkContext =
{ ClContext: Brahma.FSharp.ClContext
Queue: RawCommandQueue }
let getMatricesFilenames configFilename =
let getFullPathToConfig filename =
Path.Combine [| __SOURCE_DIRECTORY__
"Configs"
filename |]
|> Path.GetFullPath
configFilename
|> getFullPathToConfig
|> File.ReadLines
|> Seq.filter (fun line -> not <| line.StartsWith "!")
let getFullPathToMatrix datasetsFolder matrixFilename =
Path.Combine [| __SOURCE_DIRECTORY__
"Datasets"
datasetsFolder
matrixFilename |]
let availableContexts =
let pathToConfig =
Path.Combine [| __SOURCE_DIRECTORY__
"Configs"
"Context.txt" |]
|> Path.GetFullPath
use reader = new StreamReader(pathToConfig)
let platformRegex = Regex <| reader.ReadLine()
let deviceType =
match reader.ReadLine() with
| "Cpu" -> DeviceType.Cpu
| "Gpu" -> DeviceType.Gpu
| "Default" -> DeviceType.Default
| _ -> failwith "Unsupported"
let workGroupSizes =
reader.ReadLine()
|> (fun s -> s.Split ' ')
|> Seq.map int
let mutable e = ErrorCode.Unknown
let contexts =
Cl.GetPlatformIDs &e
|> Array.collect (fun platform -> Cl.GetDeviceIDs(platform, deviceType, &e))
|> Seq.ofArray
|> Seq.distinctBy
(fun device ->
Cl
.GetDeviceInfo(device, DeviceInfo.Name, &e)
.ToString())
|> Seq.filter
(fun device ->
let platform =
Cl
.GetDeviceInfo(device, DeviceInfo.Platform, &e)
.CastTo<Platform>()
let platformName =
Cl
.GetPlatformInfo(platform, PlatformInfo.Name, &e)
.ToString()
platformRegex.IsMatch platformName)
|> Seq.map
(fun device ->
let platform =
Cl
.GetDeviceInfo(device, DeviceInfo.Platform, &e)
.CastTo<Platform>()
let clPlatform =
Cl
.GetPlatformInfo(platform, PlatformInfo.Name, &e)
.ToString()
|> Platform.Custom
let device =
ClDevice.GetFirstAppropriateDevice(clPlatform)
let translator = FSQuotationToOpenCLTranslator device
let context =
Brahma.FSharp.ClContext(device, translator)
let queue =
RawCommandQueue(context.ClDevice.Device, context.Context, context.Translator)
{ ClContext = context; Queue = queue })
seq {
for wgSize in workGroupSizes do
for context in contexts do
yield (context, wgSize)
}
let nextSingle (random: System.Random) =
let buffer = Array.zeroCreate<byte> 4
random.NextBytes buffer
System.BitConverter.ToSingle(buffer, 0)
let normalFloatGenerator =
(Arb.Default.NormalFloat()
|> Arb.toGen
|> Gen.map float)
let fIsEqual x y =
abs (x - y) < Accuracy.medium.absolute
|| x.Equals y
let nextInt (random: System.Random) = random.Next()
module VectorGenerator =
let private pairOfVectorsOfEqualSize (valuesGenerator: Gen<'a>) createVector =
gen {
let! length = Gen.sized <| Gen.constant
let! leftArray = Gen.arrayOfLength length valuesGenerator
let! rightArray = Gen.arrayOfLength length valuesGenerator
return (createVector leftArray, createVector rightArray)
}
let intPair format =
fun array -> Utils.createVectorFromArray format array ((=) 0)
|> pairOfVectorsOfEqualSize Arb.generate<int32>
let floatPair format =
let fIsEqual x y =
abs (x - y) < Accuracy.medium.absolute || x = y
let createVector array =
Utils.createVectorFromArray format array (fIsEqual 0.0)
pairOfVectorsOfEqualSize Utils.normalFloatGenerator createVector