-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUtils.fs
More file actions
51 lines (44 loc) · 1.55 KB
/
Copy pathUtils.fs
File metadata and controls
51 lines (44 loc) · 1.55 KB
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
namespace Tensor.Benchmark
open System
open System.Reflection
open System.IO
open System.Collections.Generic
open Tensor
type Cache (name: string) =
let cacheDir = Path.Combine(Path.GetTempPath(), "TensorBenchmarkCache")
do if not (Directory.Exists cacheDir) then Directory.CreateDirectory cacheDir |> ignore
let path = (Path.Combine (cacheDir, name) + ".h5") |> Path.GetFullPath
//do printfn "Cache %s is at %s" name path
let onDisk =
try if File.Exists path then Some (HDF5.OpenRead path) else None
with _ -> None
let contents = Dictionary<string, ITensor> ()
let mutable changed = false
member __.Get name createFn =
match onDisk with
| _ when contents.ContainsKey name ->
//printfn "Cache hit: %s" name
contents.[name] :?> Tensor<_>
| Some d when d.Exists name ->
//printfn "Cache hit: %s" name
let v = HostTensor.read d name
contents.[name] <- v
v
| _ ->
//printfn "Cache miss: %s" name
changed <- true
let v = createFn ()
contents.[name] <- v
v
member __.Dispose () =
match onDisk with
| Some d -> d.Dispose ()
| _ -> ()
if changed then
try
use toDisk = HDF5.OpenWrite path
for KeyValue (name, data) in contents do
HostTensor.write toDisk name data
with _ -> ()
interface IDisposable with
member this.Dispose () = this.Dispose ()