-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexStream.cs
139 lines (126 loc) · 3.93 KB
/
ComplexStream.cs
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
using System;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Numerics;
namespace GigaFFT
{
public class Complex4Ks : IDisposable
{
public static long totalRAM = 3L * 1024 * 1024 * 1024;
private static string namePrefix = Guid.NewGuid().ToString();
private static long nameSuffix = 0;
private static Object nameSuffixLock = new Object();
private static Object totalRAMLock = new Object();
private MemoryMappedFile file;
private readonly long suffix;
private const int fourK = 4096;
private readonly long length4ks;
private Complex[][] array;
public Complex4Ks(long complexEntryCount)
{
Debug.Assert(complexEntryCount > fourK);
Debug.Assert(complexEntryCount % 0x1000 == 0);
Debug.Assert((complexEntryCount != 0) && ((complexEntryCount & (complexEntryCount - 1)) == 0), "complexEntryCount must be power of 2");
this.length4ks = complexEntryCount / fourK;
Debug.Assert(complexEntryCount == fourK * this.length4ks);
bool ramIsLow = this.length4ks > 1024;
if (!ramIsLow)
{
lock (totalRAMLock)
{
ramIsLow = totalRAM <= 0L;
}
}
if (ramIsLow)
{
lock (nameSuffixLock)
{
this.suffix = nameSuffix++;
}
this.file = MemoryMappedFile.CreateFromFile(FileName, FileMode.CreateNew, namePrefix + "-" + suffix, complexEntryCount * 16L, MemoryMappedFileAccess.ReadWrite);
}
else
{
lock (totalRAMLock)
{
totalRAM -= this.length4ks * 4096 * 16;
}
this.array = new Complex[this.length4ks][];
}
}
public string FileName
{
get
{
return this.array == null ? @"d:\temp\" + namePrefix + "-" + suffix : null;
}
}
public Complex[] this[long index]
{
get
{
if (this.array != null)
{
return this.array[index];
}
using (MemoryMappedViewAccessor accessor = this.file.CreateViewAccessor(index * fourK * 16L, fourK * 16L))
{
Complex[] result = new Complex[fourK];
accessor.ReadArray<Complex>(0, result, 0, 4096);
return result;
}
}
set
{
if (this.array != null)
{
this.array[index] = value;
}
else
{
using (MemoryMappedViewAccessor accessor = this.file.CreateViewAccessor(index * fourK * 16L, fourK * 16L))
{
accessor.WriteArray<Complex>(0, value, 0, 4096);
}
}
}
}
public long Length4ks
{
get
{
return this.length4ks;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (file != null)
{
if (disposing)
{
file.Dispose();
File.Delete(FileName);
file = null;
}
}
if (array != null)
{
array = null;
lock (totalRAMLock)
{
totalRAM += this.length4ks * 4096 * 16;
}
}
}
~Complex4Ks()
{
Dispose(false);
}
}
}