Skip to content

Commit 65c9ce2

Browse files
committed
Add reader for int matrices
1 parent 5e52a3b commit 65c9ce2

7 files changed

Lines changed: 64 additions & 6 deletions

File tree

src/GraphBLAS-sharp/IO/MtxReader.fs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,68 @@ type MtxReader(pathToFile: string) =
159159
| Coordinate -> matrixFromCoordinateFormat ()
160160
| Array -> failwith "Unsupported matrix format"
161161

162+
member this.ReadMatrixInteger(converter: string -> 'a) : Matrix<'a> =
163+
if object <> MtxMatrix then failwith "Object is not matrix"
164+
if field <> Integer then failwith "Field is not real"
165+
166+
use streamReader = new StreamReader(pathToFile)
167+
while streamReader.Peek() = int '%' do
168+
streamReader.ReadLine() |> ignore
169+
170+
let matrixFromCoordinateFormat () =
171+
let size =
172+
streamReader.ReadLine().Split(' ')
173+
|> Array.map int
174+
175+
let n = size.[0]
176+
let m = size.[1]
177+
let nnz = size.[2]
178+
179+
let pack x y = (uint64 x <<< 32) ||| (uint64 y)
180+
let unpack x = int ((x &&& 0xFFFFFFFF0000000UL) >>> 32), int (x &&& 0xFFFFFFFUL)
181+
182+
let sortedData =
183+
[0 .. nnz - 1]
184+
|> List.map (fun _ -> streamReader.ReadLine().Split(' '))
185+
|> Array.ofList
186+
|> Array.Parallel.map
187+
(fun line ->
188+
let i = int line.[0]
189+
let j = int line.[1]
190+
let v = converter line.[2]
191+
struct(pack i j, v)
192+
)
193+
|> Array.sortBy (fun struct(packedIndex, _) -> packedIndex)
194+
195+
let rows = Array.zeroCreate sortedData.Length
196+
let cols = Array.zeroCreate sortedData.Length
197+
let values = Array.zeroCreate sortedData.Length
198+
199+
Array.Parallel.iteri (fun i struct(packedIndex, value) ->
200+
let (rowIdx, columnIdx) = unpack packedIndex
201+
// in mtx indecies start at 1
202+
rows.[i] <- rowIdx - 1
203+
cols.[i] <- columnIdx - 1
204+
values.[i] <- value
205+
) sortedData
206+
207+
MatrixCOO {
208+
Rows = rows
209+
Columns = cols
210+
Values = values
211+
RowCount = n
212+
ColumnCount = m
213+
}
214+
215+
match format with
216+
| Coordinate -> matrixFromCoordinateFormat ()
217+
| Array -> failwith "Unsupported matrix format"
218+
162219
member this.ReadMatrix(converter: string -> 'a) : Matrix<'a> =
163220
match field with
164221
| Real -> this.ReadMatrixReal(converter)
165222
| Pattern -> this.ReadMatrixBoolean(converter)
223+
| Integer -> this.ReadMatrixInteger(converter)
166224
| _ -> failwith "Not implemented"
167225

168226
and MtxObject =

tests/GraphBLAS-sharp.Tests/MatrixOperationsTests/EWiseAddTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ let testFixtures case = [
144144

145145
case
146146
|> correctnessGenericTest<bool> AnyAll.bool (=)
147-
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
147+
|> ptestPropertyWithConfig config (getCorrectnessTestName "bool")
148148
]
149149

150150
let tests =

tests/GraphBLAS-sharp.Tests/MatrixOperationsTests/GetTuplesTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ let testFixtures case = [
126126

127127
case
128128
|> correctnessGenericTest<bool> (=) false
129-
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
129+
|> ptestPropertyWithConfig config (getCorrectnessTestName "bool")
130130
]
131131

132132
let tests =

tests/GraphBLAS-sharp.Tests/MatrixOperationsTests/MxmTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ let testFixtures case = [
159159

160160
case
161161
|> correctnessGenericTest<bool> AnyAll.bool (=)
162-
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
162+
|> ptestPropertyWithConfig config (getCorrectnessTestName "bool")
163163
]
164164

165165
let tests =

tests/GraphBLAS-sharp.Tests/MatrixOperationsTests/MxvTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ let testFixtures case = [
179179

180180
case
181181
|> correctnessGenericTest<bool> AnyAll.bool (=)
182-
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
182+
|> ptestPropertyWithConfig config (getCorrectnessTestName "bool")
183183

184184
testCase (sprintf "Explicit zero test on %A" case) <| fun () ->
185185
let matrix = array2D [

tests/GraphBLAS-sharp.Tests/MatrixOperationsTests/TransposeTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ let testFixtures case = [
131131

132132
case
133133
|> correctnessGenericTest<bool> (=) false
134-
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
134+
|> ptestPropertyWithConfig config (getCorrectnessTestName "bool")
135135
]
136136

137137
let tests =

tests/GraphBLAS-sharp.Tests/MatrixOperationsTests/VxmTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ let testFixtures case = [
164164

165165
case
166166
|> correctnessGenericTest<bool> AnyAll.bool (=)
167-
|> testPropertyWithConfig config (getCorrectnessTestName "bool")
167+
|> ptestPropertyWithConfig config (getCorrectnessTestName "bool")
168168
]
169169

170170
let tests =

0 commit comments

Comments
 (0)