-
Notifications
You must be signed in to change notification settings - Fork 2
/
SFData.java
1002 lines (909 loc) · 31.9 KB
/
SFData.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* For Copyright and License see LICENSE.txt and COPYING.txt in the root directory */
/**
*
*/
package com.nerdscentral.audio.core;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteOrder;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.util.Stack;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.LongFunction;
import com.nerdscentral.audio.Messages;
import com.nerdscentral.data.OffHeapArray;
import com.nerdscentral.data.UnsafeProvider;
import com.nerdscentral.sython.SFPL_RuntimeException;
import sun.misc.Cleaner;
import sun.misc.Unsafe;
/**
* @author a1t
*
*/
public class SFData extends SFSignal implements Serializable
{
// JIT time alias in effect, maybe remove in the future
static final Unsafe unsafe = UnsafeProvider.unsafe;
private static class MemoryZoneStack extends ThreadLocal<Stack<SFMemoryZone>>
{
@Override
protected Stack<SFMemoryZone> initialValue()
{
return new Stack<>();
}
}
static class ByteBufferWrapper
{
private long address = 0;
private final FileChannel underLyingFile;
private final long fileOffset;
// buffer is used to hold a reference to the buffer
// but its data is read via unsafe
private MappedByteBuffer buffer;
private static long _adOff = 0;
static
{
try
{
_adOff = unsafe.objectFieldOffset(ByteBufferWrapper.class.getDeclaredField("address")); //$NON-NLS-1$
}
catch (NoSuchFieldException | SecurityException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private final static long adOff = _adOff;
private final static boolean isUnmapped(ByteBufferWrapper obj)
{
return unsafe.getLongVolatile(obj, adOff) == 0;
}
private final static long setAddress(ByteBufferWrapper obj, long value)
{
return unsafe.getAndSetLong(obj, adOff, value);
}
public long getAddress()
{
if (isUnmapped(this)) remap();
return address;
}
static Method getAddressMethod;
synchronized void remap()
{
// Already done by another thread;
if (!isUnmapped(this)) return;
address = 0; // Stops warnings this should be final etc.
try
{
buffer = underLyingFile.map(MapMode.READ_WRITE, fileOffset, CHUNK_LEN << 3l);
if (getAddressMethod == null)
{
getAddressMethod = buffer.getClass().getMethod("address"); //$NON-NLS-1$
getAddressMethod.setAccessible(true);
}
buffer.order(ByteOrder.nativeOrder());
setAddress(this, (long) getAddressMethod.invoke(buffer));
}
catch (IOException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e)
{
throw new RuntimeException(e);
}
}
public ByteBufferWrapper(FileChannel file, long offsetInFile) throws SecurityException, IllegalArgumentException
{
fileOffset = offsetInFile;
underLyingFile = file;
// Map the data so the underlying channel will create the disk storage for it and
// so the file size etc continues to make sense.
remap();
}
static Field cleanerField = null;
public synchronized void release()
{
setAddress(this, 0);
if (isUnmapped(this)) return;
try
{
if (cleanerField == null)
{
cleanerField = buffer.getClass().getDeclaredField("cleaner"); //$NON-NLS-1$
cleanerField.setAccessible(true);
}
Cleaner cleaner = (Cleaner) cleanerField.get(buffer);
cleaner.clean();
}
catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static class NotCollectedException extends Exception
{
private static final long serialVersionUID = 1L;
}
// $NON-NLS-1$
static final long CHUNK_SHIFT = 13;
static final long CHUNK_LEN = (long) Math.pow(2, CHUNK_SHIFT);
static final long CHUNK_MASK = CHUNK_LEN - 1;
// How frequently to print of memory state once this many samples has been allocated and/or freed.
private static final long MEM_REPORT_THRESHOLD = (long) SFConstants.SAMPLE_RATE
* SFConstants.HEART_BEAT;
private static final long serialVersionUID = 1L;
private final int length;
ByteBufferWrapper[] chunks;
private final long chunkIndex;
// shadows chunkIndex for memory management as chunkIndex must
// be final to enable optimisation
private long allocked;
private static final AtomicLong totalCount = new AtomicLong();
private static final AtomicLong freeCount = new AtomicLong();
private static final AtomicLong reportTicker = new AtomicLong();
private static ConcurrentLinkedDeque<ByteBufferWrapper> freeChunks = new ConcurrentLinkedDeque<>();
private static ConcurrentLinkedDeque<ByteBufferWrapper> allChunks = new ConcurrentLinkedDeque<>();
@SuppressWarnings("synthetic-access")
private static final MemoryZoneStack memoryZoneStack = new MemoryZoneStack();
private final AtomicBoolean kept = new AtomicBoolean(false);
private final AtomicBoolean dead = new AtomicBoolean(false);
private final AtomicBoolean pinned = new AtomicBoolean(false);
private final static LinkedBlockingDeque<Runnable> heartBeatTasks = new LinkedBlockingDeque<>();
// TODO: The file channels are thread local but the chunks are not. Thus over time chunks will disperse
// across threads and the locality will be broken. If we can make some mechanism to allow this were needed
// but migrate chunks back to being thread local where possible to improve disk data locallity this would
// be good.
private static ConcurrentHashMap<File, FileChannel> fileMap = new ConcurrentHashMap<>();
protected static FileChannel getRotatingChannel()
{
String pid = ManagementFactory.getRuntimeMXBean().getName();
try
{
File dir = SFConstants.getRotatingTempDir();
FileChannel channel = fileMap.get(dir);
if (channel != null) return channel;
// OK, we do not have an existing swap file in this dir to make a new one.
File coreFile = File.createTempFile("SonicFieldSwap" + pid, ".mem", dir); //$NON-NLS-1$//$NON-NLS-2$
coreFile.deleteOnExit();
// Now create the actual file
@SuppressWarnings("resource")
RandomAccessFile rfile = new RandomAccessFile(coreFile, "rw"); //$NON-NLS-1$
channel = rfile.getChannel();
fileMap.put(dir, channel);
return channel;
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
private static void startHeartBeat()
{
Thread hbRunner = new Thread(new Runnable()
{
@Override
public void run()
{
while (true)
{
try
{
Runnable task = heartBeatTasks.takeLast();
System.out.println("Performing heart beat task: " + task);
task.run();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
});
hbRunner.setDaemon(true);
hbRunner.setName("HeartBeat");
hbRunner.start();
}
private static void enqueueHeartBeatTask(Runnable task)
{
if (!heartBeatTasks.offerFirst(task))
{
throw new RuntimeException("Heartbeat task queue overflow (which should be impossible.");
}
}
static
{
startHeartBeat();
}
private static Runnable doGcTask = new Runnable()
{
@Override
public void run()
{
System.gc();
}
@Override
public String toString()
{
return "System garbage collection task";
}
};
private static void dataHeartBeat(int count)
{
/* Periodically perform actions based on when a particular amount of data has been allocated to SFData objects
* or freed from them.
*/
if (reportTicker.addAndGet(count) > MEM_REPORT_THRESHOLD)
{
reportTicker.set(0);
final LongFunction<Long> toGigs = chunks -> chunks * CHUNK_LEN * 8 / SFConstants.ONE_MEG;
System.out.println(Messages.getString("SFData.20") + toGigs.apply(totalCount.get()) //$NON-NLS-1$
+ Messages.getString("SFData.21") + toGigs.apply((freeCount.get()))); //$NON-NLS-1$
enqueueHeartBeatTask(doGcTask);
}
}
private void makeMap(long size) throws SecurityException, IllegalArgumentException, IOException
{
long countDown = size;
int chunkCount = 0;
while (countDown > 0)
{
++chunkCount;
countDown -= CHUNK_LEN;
}
countDown = size;
chunks = new ByteBufferWrapper[chunkCount];
chunkCount = 0;
while (countDown > 0)
{
ByteBufferWrapper chunk = freeChunks.pollFirst();
if (chunk == null) break;
chunks[chunkCount] = chunk;
freeCount.decrementAndGet();
++chunkCount;
countDown -= CHUNK_LEN;
}
if (countDown > 0)
{
// If the GC has found something, make sure the finalizers are run here so we get
// the chunks freed.
System.runFinalization();
mapMoreData(countDown, chunkCount);
}
getChunkAddresses();
}
private void getChunkAddresses()
{
// now we have the chunks we get the address of the underlying memory
// of each and place that in the off heap lookup so we no longer reference
// them via objects but purely as raw memory
long offSet = 0;
for (ByteBufferWrapper chunk : chunks)
{
unsafe.putAddress(chunkIndex + offSet, chunk.getAddress());
offSet += 8;
}
}
private void mapMoreData(long countDown, int chunkCount) throws IOException
{
synchronized (fileMap)
{
// System.out.println("Adding " + (countDown / CHUNK_LEN) + " chunks");
while (countDown > 0)
{
@SuppressWarnings("resource")
FileChannel channel = getRotatingChannel();
long from = channel.size();
ByteBufferWrapper chunk = chunks[chunkCount] = new ByteBufferWrapper(channel, from);
allChunks.add(chunk);
++chunkCount;
totalCount.addAndGet(1);
countDown -= CHUNK_LEN;
}
}
}
@Override
public void release()
{
checkLive();
if (pinned.get()) throw new RuntimeException("Trying to release pinned memory."); //$NON-NLS-1$
dataHeartBeat(getLength());
dead.set(true);
synchronized (fileMap)
{
// If we are already released, do nothing;
if (chunks == null) return;
for (ByteBufferWrapper chunk : chunks)
{
chunk.release();
freeChunks.push(chunk);
freeCount.incrementAndGet();
}
chunks = null;
freeUnsafeMemory();
}
}
@Override
public boolean isReleased()
{
return dead.get();
}
private void checkLive()
{
if (dead.get())
{
throw new RuntimeException(Messages.getString("SFData.22")); //$NON-NLS-1$
}
}
private void freeUnsafeMemory()
{
synchronized (unsafe)
{
if (allocked != 0) unsafe.freeMemory(chunkIndex);
allocked = 0;
}
}
private SFData(int l)
{
allocked = 0;
try
{
try
{
// Allocate the memory for the index - final so do it here
long size = (1 + (l >> CHUNK_SHIFT)) << 3;
allocked = chunkIndex = unsafe.allocateMemory(size);
if (allocked == 0)
{
throw new RuntimeException("Out of memory allocating " + size); //$NON-NLS-1$
}
makeMap(l);
}
catch (Exception e)
{
throw new RuntimeException(e);
}
this.length = l;
}
catch (Throwable t)
{
if (allocked != 0)
{
unsafe.freeMemory(allocked);
allocked = 0;
}
throw t;
}
Stack<SFMemoryZone> stack = memoryZoneStack.get();
if (stack.size() > 0)
{
SFMemoryZone zone = stack.peek();
synchronized (zone.localData)
{
zone.localData.add(this);
}
}
dataHeartBeat(l);
}
public final static SFData build(int l, boolean clear)
{
SFData ret = new SFData(l);
if (clear)
{
for (long i = 0; i < ret.chunks.length; ++i)
{
unsafe.setMemory(unsafe.getAddress(ret.chunkIndex + (i << 3l)), CHUNK_LEN << 3l, (byte) 0);
}
}
return ret;
}
/* (non-Javadoc)
* @see com.nerdscentral.audio.SFSignal#replicate()
*/
@Override
public final SFSignal replicate()
{
checkLive();
SFSignal data1 = new SFData(this.getLength());
int l = this.getLength();
for (int i = 0; i < l; ++i)
{
data1.setSample(i, this.getSample(i));
}
return data1;
}
private long getAddress(long index)
{
if (SFConstants.CHECK_MEMORY)
{
if (index < 0 || index > length) throw new RuntimeException("Access out of bounds."); //$NON-NLS-1$
}
long pos = index & CHUNK_MASK;
long bufPos = index >> CHUNK_SHIFT;
long address = chunkIndex + (bufPos << 3);
long addr = unsafe.getAddress(address) + (pos << 3l);
if (SFConstants.CHECK_MEMORY)
{
if (addr == 0) throw new RuntimeException("Accessing released memory."); //$NON-NLS-1$
}
return addr;
}
/* (non-Javadoc)
* @see com.nerdscentral.audio.SFSignal#getSample(int)
*/
@Override
public final double getSample(int index)
{
return unsafe.getDouble(getAddress(index));
}
/* (non-Javadoc)
* @see com.nerdscentral.audio.SFSignal#setSample(int, double)
*/
@Override
public final double setSample(int index, double value)
{
unsafe.putDouble(getAddress(index), value);
return value;
}
/* (non-Javadoc)
* @see com.nerdscentral.audio.SFSignal#getLength()
*/
@Override
public final int getLength()
{
return this.length;
}
public static final SFSignal build(float[] input)
{
// No nead to clear as we set the entire buffer.
SFSignal data = new SFData(input.length);
for (int i = 0; i < input.length; ++i)
{
data.setSample(i, input[i]);
}
return data;
}
public static SFSignal build(double[] input)
{
SFSignal data = new SFData(input.length);
for (int i = 0; i < input.length; ++i)
{
data.setSample(i, input[i]);
}
return data;
}
public static final SFSignal build(double[] input, int j)
{
SFSignal data = SFData.build(j, false);
for (int i = 0; i < j; ++i)
{
data.setSample(i, input[i]);
}
return data;
}
public static final SFSignal build(OffHeapArray input, int j)
{
SFSignal data = SFData.build(j, false);
input.checkBoundsDouble(0, j);
for (int i = 0; i < j; ++i)
{
data.setSample(i, input.getDouble(i));
}
return data;
}
/* (non-Javadoc)
* @see com.nerdscentral.audio.SFSignal#setAt(int, com.nerdscentral.audio.SFData)
*/
@Override
public void setAt(int pos, SFSignal data2) throws SFPL_RuntimeException
{
checkLive();
int pos2 = pos;
if (pos2 + data2.getLength() > length)
{
System.out.println(Messages.getString("SFData.9") + pos2 + Messages.getString("SFData.10") + data2.getLength() //$NON-NLS-1$ //$NON-NLS-2$
+ Messages.getString("SFData.11") + length); //$NON-NLS-1$
throw new SFPL_RuntimeException(Messages.getString("SFData.0")); //$NON-NLS-1$
}
long end = pos2 + data2.getLength();
for (int index = pos2; index < end; ++index)
{
setSample(index, data2.getSample(index - pos2));
}
}
/* (non-Javadoc)
* @see com.nerdscentral.audio.SFSignal#setFrom(int, com.nerdscentral.audio.SFData)
*/
@Override
public void setFrom(int pos, SFSignal data2) throws SFPL_RuntimeException
{
checkLive();
int pos2 = pos;
if (pos2 + length > data2.getLength()) throw new SFPL_RuntimeException(Messages.getString("SFData.1")); //$NON-NLS-1$
for (int index = 0; index < length; ++index)
{
setSample(index, data2.getSample(index - pos2));
}
}
/* (non-Javadoc)
* @see com.nerdscentral.audio.SFSignal#toString()
*/
@Override
public String toString()
{
checkLive();
return Messages.getString("SFData.2") + length + Messages.getString("SFData.3"); //$NON-NLS-1$ //$NON-NLS-2$
}
public static SFSignal realise(SFSignal in)
{
if (in instanceof SFData) return in;
int len = in.getLength();
SFSignal output = new SFData(len);
for (int i = 0; i < len; ++i)
{
output.setSample(i, in.getSample(i));
}
return output;
}
@Override
public double[] getDataInternalOnly()
{
double[] ret = new double[length];
for (int index = 0; index < length; ++index)
{
ret[index] = getSample(index);
}
return ret;
}
@Override
public boolean isRealised()
{
checkLive();
return true;
}
private static long getAbsLen(SFSignal a, long aOffset, SFSignal b, long bOffset)
{
// Is there enough room irrespective of chunks
// How much space left in b
long absLen = b.getLength() - bOffset;
// How much we can copy from a
long wantLen = a.getLength() - aOffset;
// abs len is the amount we could copy ignoring chunks
if (wantLen < absLen) absLen = wantLen;
// Find the end of the next chunks
long aEnd = CHUNK_LEN + (aOffset & ~CHUNK_MASK);
long bEnd = CHUNK_LEN + (bOffset & ~CHUNK_MASK);
long aDif = aEnd - aOffset;
long bDif = bEnd - bOffset;
long maxChunkLen = aDif > bDif ? bDif : aDif;
if (absLen > maxChunkLen)
{
absLen = maxChunkLen;
}
return absLen;
}
// Mixes a onto b for as long as that can be done without flipping chuncks
// returns how many doubles it managed to mix, this can be call repeatedly
// to then mix two entire buffers. If return is zero then the end of the
// buffer has been reached.
private static long addChunks(SFData a, long aOffset, SFData b, long bOffset)
{
long absLen = getAbsLen(a, aOffset, b, bOffset);
// Not absLen is the longest continuous mix we can make from
// a to b.
long aAddr = a.getAddress(aOffset);
long bAddr = b.getAddress(bOffset);
long end = absLen << 3l;
for (long pos = 0; pos < end; pos += 8)
{
double aValue = unsafe.getDouble(aAddr + pos);
long putAddr = bAddr + pos;
double bValue = unsafe.getDouble(putAddr);
// Due to no compile time polymorphism we have to duplicate
// this method for each operator in the next line or suffer
// virtual dispatch :|
unsafe.putDouble(putAddr, aValue + bValue);
}
return absLen;
}
private static long copyChunks(SFData a, long aOffset, SFData b, long bOffset)
{
long absLen = getAbsLen(a, aOffset, b, bOffset);
// Not absLen is the longest continuous mix we can make from
// a to b.
long aAddr = a.getAddress(aOffset);
long bAddr = b.getAddress(bOffset);
long end = absLen << 3l;
for (long pos = 0; pos < end; pos += 8)
{
double aValue = unsafe.getDouble(aAddr + pos);
long putAddr = bAddr + pos;
// Due to no compile time polymorphism we have to duplicate
// this method for each operator in the next line or suffer
// virtual dispatch :|
unsafe.putDouble(putAddr, aValue);
}
return absLen;
}
private static long multiplyChunks(SFData a, long aOffset, SFData b, long bOffset)
{
long absLen = getAbsLen(a, aOffset, b, bOffset);
// Not absLen is the longest continuous mix we can make from
// a to b.
long aAddr = a.getAddress(aOffset);
long bAddr = b.getAddress(bOffset);
long end = absLen << 3l;
for (long pos = 0; pos < end; pos += 8)
{
double aValue = unsafe.getDouble(aAddr + pos);
long putAddr = bAddr + pos;
double bValue = unsafe.getDouble(putAddr);
// Due to no compile time polymorphism we have to duplicate
// this method for each operator in the next line or suffer
// virtual dispatch :|
unsafe.putDouble(putAddr, aValue * bValue);
}
return absLen;
}
private static long divideChunks(SFData a, long aOffset, SFData b, long bOffset)
{
long absLen = getAbsLen(a, aOffset, b, bOffset);
// Not absLen is the longest continuous mix we can make from
// a to b.
long aAddr = a.getAddress(aOffset);
long bAddr = b.getAddress(bOffset);
long end = absLen << 3l;
for (long pos = 0; pos < end; pos += 8)
{
double aValue = unsafe.getDouble(aAddr + pos);
long putAddr = bAddr + pos;
double bValue = unsafe.getDouble(putAddr);
// Due to no compile time polymorphism we have to duplicate
// this method for each operator in the next line or suffer
// virtual dispatch :|
unsafe.putDouble(putAddr, aValue / bValue);
}
return absLen;
}
private static long subtractChunks(SFData a, long aOffset, SFData b, long bOffset)
{
long absLen = getAbsLen(a, aOffset, b, bOffset);
// Not absLen is the longest continuous mix we can make from
// a to b.
long aAddr = a.getAddress(aOffset);
long bAddr = b.getAddress(bOffset);
long end = absLen << 3l;
for (long pos = 0; pos < end; pos += 8)
{
double aValue = unsafe.getDouble(aAddr + pos);
long putAddr = bAddr + pos;
double bValue = unsafe.getDouble(putAddr);
// Due to no compile time polymorphism we have to duplicate
// this method for each operator in the next line or suffer
// virtual dispatch :|
unsafe.putDouble(putAddr, aValue - bValue);
}
return absLen;
}
/**
* Stores useful information about a SFData signal so it can be returned as a single return from an optimised analysis run.
*
*/
public static class Stats
{
public double maxValue = 0;
public double minValue = 0;
public double totalValue = 0;
}
private static long scanChunks(SFData b, long bOffset, Stats stats)
{
long absLen = b.getLength() - bOffset;
// Not absLen is the longest continuous mix we can make from
// a to b.
long bAddr = b.getAddress(bOffset);
for (long pos = 0; pos < absLen; pos += 8)
{
long putAddr = bAddr + pos;
double bValue = unsafe.getDouble(putAddr);
if (bValue > stats.maxValue) stats.maxValue = bValue;
if (bValue < stats.minValue) stats.minValue = bValue;
stats.totalValue += bValue;
}
return absLen;
}
public Stats getStats()
{
checkLive();
Stats stats = new Stats();
long done = 0;
long thisAt = 0;
while ((done = scanChunks(this, thisAt, stats)) != 0)
{
thisAt += done;
}
return stats;
}
public enum OPERATION
{
ADD, COPY, MULTIPLY, DIVIDE, SUBTRACT
}
// Note that this is laboriously laid out to give
// separate call sites for each operation to help give
// static dispatch after optimisation
public void operateOnto(int at, SFSignal in, OPERATION operation)
{
checkLive();
if (in.isRealised())
{
// TODO this is a bit ambiguous around the meaning of isRealise() but
// in the current implementation where only SFData returns true for
// isRealise() we are OK.
long thisAt = at;
long otherAt = 0;
long done = 0;
SFData data = (SFData) in;
switch (operation)
{
case ADD:
while ((done = addChunks(data, otherAt, this, thisAt)) != 0)
{
thisAt += done;
otherAt += done;
}
break;
case COPY:
while ((done = copyChunks(data, otherAt, this, thisAt)) != 0)
{
thisAt += done;
otherAt += done;
}
break;
case MULTIPLY:
while ((done = multiplyChunks(data, otherAt, this, thisAt)) != 0)
{
thisAt += done;
otherAt += done;
}
break;
case DIVIDE:
while ((done = divideChunks(data, otherAt, this, thisAt)) != 0)
{
thisAt += done;
otherAt += done;
}
break;
case SUBTRACT:
while ((done = subtractChunks(data, otherAt, this, thisAt)) != 0)
{
thisAt += done;
otherAt += done;
}
break;
}
}
else
{
int len = in.getLength();
if (len > getLength()) len = getLength();
switch (operation)
{
case ADD:
for (int index = 0; index < len; ++index)
{
long address = getAddress(index + at);
double value = unsafe.getDouble(address);
unsafe.putDouble(address, in.getSample(index) + value);
}
break;
case COPY:
for (int index = 0; index < len; ++index)
{
long address = getAddress(index + at);
unsafe.putDouble(address, in.getSample(index));
}
break;
case MULTIPLY:
for (int index = 0; index < len; ++index)
{
long address = getAddress(index + at);
double value = unsafe.getDouble(address);
unsafe.putDouble(address, in.getSample(index) * value);
}
break;
case DIVIDE:
for (int index = 0; index < len; ++index)
{
long address = getAddress(index + at);
double value = unsafe.getDouble(address);
unsafe.putDouble(address, in.getSample(index) / value);
}
break;
case SUBTRACT:
for (int index = 0; index < len; ++index)
{
long address = getAddress(index + at);
double value = unsafe.getDouble(address);
unsafe.putDouble(address, in.getSample(index) - value);
}
break;
}
}
}
public static SFMemoryZone popZone()
{
// Release everything in the head of the zone thread local stack.
SFMemoryZone zone = memoryZoneStack.get().pop();
// System.out.println("Popped: " + zone);
synchronized (zone.localData)
{
for (SFData data : zone.localData)
{
// Pass
ByteBufferWrapper[] someChunks = data.chunks;
if (someChunks != null)
{
// Not already released.
if (data.kept.get())
{
data.kept.set(false);
}
else
{
if (!data.pinned.get())
{
data.release();
}
}
}
}
zone.localData.clear();
}
return zone;
}
public static SFMemoryZone unstackZone()
{
return memoryZoneStack.get().pop();
}
public static void stackZone(SFMemoryZone sfMemoryZone)
{
pushZone(sfMemoryZone);
}
public static SFMemoryZone peekZone()
{
Stack<SFMemoryZone> zone = memoryZoneStack.get();
if (zone.size() == 0) return null;
return zone.peek();
}
public static void pushZone(SFMemoryZone sfMemoryZone)
{
// Push to a thread local stack a collection which holds all new SFData objects.
// So that they can be released explicitly.
// System.out.println("Pushing: " + sfMemoryZone);
memoryZoneStack.get().push(sfMemoryZone);
}
@Override
public SFSignal pin()
{
checkLive();
pinned.set(true);
return this;
}
@Override
public SFSignal keep()
{
checkLive();
kept.set(true);
return this;