Search in sources :

Example 1 with DefaultMemoryRequestServer

use of org.apache.datasketches.memory.DefaultMemoryRequestServer in project sketches-core by DataSketches.

the class DirectQuantilesMemoryRequestTest method checkLimitedMemoryScenarios.

@Test
public void checkLimitedMemoryScenarios() {
    // Requesting application
    final int k = 128;
    final int u = 40 * k;
    // just the BB
    final int initBytes = ((2 * k) + 4) << 3;
    // This part would actually be part of the Memory owning implemention so it is faked here
    try (WritableHandle wdh = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        final WritableMemory wmem = wdh.getWritable();
        println("Initial mem size: " + wmem.getCapacity());
        // ########## Receiving Application
        // The receiving application has been given wmem to use for a sketch,
        // but alas, it is not ultimately large enough.
        final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(wmem);
        assertTrue(usk1.isEmpty());
        // Load the sketch
        for (int i = 0; i < u; i++) {
            // The sketch uses The MemoryRequest, acquired from wmem, to acquire more memory as
            // needed, and requests via the MemoryRequest to free the old allocations.
            usk1.update(i);
        }
        final double result = usk1.getQuantile(0.5);
        println("Result: " + result);
        // Success
        assertEquals(result, u / 2.0, 0.05 * u);
        // ########## Owning Implementation
        // The actual Memory has been re-allocated several times,
        // so the above wmem reference is invalid.
        println("\nFinal mem size: " + wmem.getCapacity());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) Test(org.testng.annotations.Test)

Example 2 with DefaultMemoryRequestServer

use of org.apache.datasketches.memory.DefaultMemoryRequestServer in project sketches-core by DataSketches.

the class DirectQuantilesMemoryRequestTest method checkGrowBaseBuf.

@Test
public void checkGrowBaseBuf() {
    final int k = 128;
    // don't need the BB to fill here
    final int u = 32;
    // not enough to hold everything
    final int initBytes = (4 + (u / 2)) << 3;
    try (WritableHandle memHandler = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        // final MemoryManager memMgr = new MemoryManager();
        // final WritableMemory mem1 = memMgr.request(initBytes);
        final WritableMemory mem1 = memHandler.getWritable();
        println("Initial mem size: " + mem1.getCapacity());
        final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(mem1);
        for (int i = 1; i <= u; i++) {
            usk1.update(i);
        }
        final int currentSpace = usk1.getCombinedBufferItemCapacity();
        println("curCombBufItemCap: " + currentSpace);
        assertEquals(currentSpace, 2 * k);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) Test(org.testng.annotations.Test)

Example 3 with DefaultMemoryRequestServer

use of org.apache.datasketches.memory.DefaultMemoryRequestServer in project sketches-core by DataSketches.

the class DirectQuantilesMemoryRequestTest method checkGrowFromWrappedEmptySketch.

@Test
public void checkGrowFromWrappedEmptySketch() {
    final int k = 16;
    final int n = 0;
    // 8 bytes
    final int initBytes = DoublesSketch.getUpdatableStorageBytes(k, n);
    final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build();
    final Memory origSketchMem = Memory.wrap(usk1.toByteArray());
    try (WritableHandle memHandle = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        WritableMemory mem = memHandle.getWritable();
        origSketchMem.copyTo(0, mem, 0, initBytes);
        UpdateDoublesSketch usk2 = DirectUpdateDoublesSketch.wrapInstance(mem);
        assertTrue(mem.isSameResource(usk2.getMemory()));
        assertEquals(mem.getCapacity(), initBytes);
        assertTrue(mem.isDirect());
        assertTrue(usk2.isEmpty());
        // update the sketch forcing it to grow on-heap
        for (int i = 1; i <= 5; i++) {
            usk2.update(i);
        }
        assertEquals(usk2.getN(), 5);
        WritableMemory mem2 = usk2.getMemory();
        assertFalse(mem.isSameResource(mem2));
        // should now be on-heap
        assertFalse(mem2.isDirect());
        final int expectedSize = COMBINED_BUFFER + ((2 * k) << 3);
        assertEquals(mem2.getCapacity(), expectedSize);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) Memory(org.apache.datasketches.memory.Memory) WritableMemory(org.apache.datasketches.memory.WritableMemory) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) Test(org.testng.annotations.Test)

Example 4 with DefaultMemoryRequestServer

use of org.apache.datasketches.memory.DefaultMemoryRequestServer in project sketches-core by DataSketches.

the class DirectQuantilesMemoryRequestTest method checkGrowCombBuf.

@Test
public void checkGrowCombBuf() {
    final int k = 128;
    // just to fill the BB
    final int u = (2 * k) - 1;
    // just room for BB
    final int initBytes = ((2 * k) + 4) << 3;
    try (WritableHandle memHandler = WritableMemory.allocateDirect(initBytes, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        // final MemoryManager memMgr = new MemoryManager();
        // final WritableMemory mem1 = memMgr.request(initBytes);
        final WritableMemory mem1 = memHandler.getWritable();
        println("Initial mem size: " + mem1.getCapacity());
        final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(mem1);
        for (int i = 1; i <= u; i++) {
            usk1.update(i);
        }
        final int currentSpace = usk1.getCombinedBufferItemCapacity();
        println("curCombBufItemCap: " + currentSpace);
        final double[] newCB = usk1.growCombinedBuffer(currentSpace, 3 * k);
        final int newSpace = usk1.getCombinedBufferItemCapacity();
        println("newCombBurItemCap: " + newSpace);
        assertEquals(newCB.length, 3 * k);
    // memMgr.free(mem1);
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) Test(org.testng.annotations.Test)

Example 5 with DefaultMemoryRequestServer

use of org.apache.datasketches.memory.DefaultMemoryRequestServer in project sketches-core by DataSketches.

the class DoublesSketchTest method directSketchShouldMoveOntoHeapEventually.

@Test
public void directSketchShouldMoveOntoHeapEventually() {
    try (WritableHandle wdh = WritableMemory.allocateDirect(1000, ByteOrder.nativeOrder(), new DefaultMemoryRequestServer())) {
        WritableMemory mem = wdh.getWritable();
        UpdateDoublesSketch sketch = DoublesSketch.builder().build(mem);
        Assert.assertTrue(sketch.isSameResource(mem));
        for (int i = 0; i < 1000; i++) {
            sketch.update(i);
        }
        Assert.assertFalse(sketch.isSameResource(mem));
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : WritableHandle(org.apache.datasketches.memory.WritableHandle) WritableMemory(org.apache.datasketches.memory.WritableMemory) DefaultMemoryRequestServer(org.apache.datasketches.memory.DefaultMemoryRequestServer) Test(org.testng.annotations.Test)

Aggregations

DefaultMemoryRequestServer (org.apache.datasketches.memory.DefaultMemoryRequestServer)9 Test (org.testng.annotations.Test)9 WritableHandle (org.apache.datasketches.memory.WritableHandle)7 WritableMemory (org.apache.datasketches.memory.WritableMemory)7 ResizeFactor (org.apache.datasketches.ResizeFactor)2 MemoryRequestServer (org.apache.datasketches.memory.MemoryRequestServer)2 Family (org.apache.datasketches.Family)1 SketchesStateException (org.apache.datasketches.SketchesStateException)1 Memory (org.apache.datasketches.memory.Memory)1 PreambleUtil.insertLgResizeFactor (org.apache.datasketches.theta.PreambleUtil.insertLgResizeFactor)1