Search in sources :

Example 1 with WritableDirectHandle

use of com.yahoo.memory.WritableDirectHandle in project sketches-core by DataSketches.

the class PreambleUtilTest method checkInsertsAndExtracts.

@Test
public void checkInsertsAndExtracts() {
    final int bytes = 32;
    try (WritableDirectHandle offHeapMemHandler = WritableMemory.allocateDirect(bytes)) {
        final WritableMemory offHeapMem = offHeapMemHandler.get();
        final WritableMemory onHeapMem = WritableMemory.wrap(new byte[bytes]);
        final Object onHeapMemObj = onHeapMem.getArray();
        final Object offHeapMemObj = offHeapMem.getArray();
        final long onHeapCumOffset = onHeapMem.getCumulativeOffset(0L);
        final long offHeapCumOffset = offHeapMem.getCumulativeOffset(0L);
        onHeapMem.clear();
        offHeapMem.clear();
        //BYTES
        int v = 0XFF;
        int onH, offH;
        //PREAMBLE_LONGS_BYTE;
        insertPreLongs(onHeapMemObj, onHeapCumOffset, v);
        onH = extractPreLongs(onHeapMem);
        assertEquals(onH, v);
        insertPreLongs(offHeapMemObj, offHeapCumOffset, v);
        offH = extractPreLongs(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        //SER_VER_BYTE;
        insertSerVer(onHeapMemObj, onHeapCumOffset, v);
        onH = extractSerVer(onHeapMem);
        assertEquals(onH, v);
        insertSerVer(offHeapMemObj, offHeapCumOffset, v);
        offH = extractSerVer(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        //FAMILY_BYTE;
        insertFamilyID(onHeapMemObj, onHeapCumOffset, v);
        onH = extractFamilyID(onHeapMem);
        assertEquals(onH, v);
        insertFamilyID(offHeapMemObj, offHeapCumOffset, v);
        offH = extractFamilyID(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        //FLAGS_BYTE;
        insertFlags(onHeapMemObj, onHeapCumOffset, v);
        onH = extractFlags(onHeapMem);
        assertEquals(onH, v);
        insertFlags(offHeapMemObj, offHeapCumOffset, v);
        offH = extractFlags(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        //SHORTS
        v = 0XFFFF;
        //K_SHORT;
        insertK(onHeapMemObj, onHeapCumOffset, v);
        onH = extractK(onHeapMem);
        assertEquals(onH, v);
        insertK(offHeapMemObj, offHeapCumOffset, v);
        offH = extractK(offHeapMem);
        assertEquals(offH, v);
        onHeapMem.clear();
        offHeapMem.clear();
        //LONGS
        //N_LONG;
        long onHL, offHL, vL = 1L << 30;
        insertN(onHeapMemObj, onHeapCumOffset, vL);
        onHL = extractN(onHeapMem);
        assertEquals(onHL, vL);
        insertN(offHeapMemObj, offHeapCumOffset, vL);
        offHL = extractN(offHeapMem);
        assertEquals(offHL, vL);
        onHeapMem.clear();
        offHeapMem.clear();
        //DOUBLES
        //MIN_DOUBLE;
        double onHD, offHD, vD = 1L << 40;
        insertMinDouble(onHeapMemObj, onHeapCumOffset, vD);
        onHD = extractMinDouble(onHeapMem);
        assertEquals(onHD, vD);
        insertMinDouble(offHeapMemObj, offHeapCumOffset, vD);
        offHD = extractMinDouble(offHeapMem);
        assertEquals(offHD, vD);
        onHeapMem.clear();
        offHeapMem.clear();
        //MAX_DOUBLE;
        insertMaxDouble(onHeapMemObj, onHeapCumOffset, vD);
        onHD = extractMaxDouble(onHeapMem);
        assertEquals(onHD, vD);
        insertMaxDouble(offHeapMemObj, offHeapCumOffset, vD);
        offHD = extractMaxDouble(offHeapMem);
        assertEquals(offHD, vD);
        onHeapMem.clear();
        offHeapMem.clear();
    }
}
Also used : WritableDirectHandle(com.yahoo.memory.WritableDirectHandle) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 2 with WritableDirectHandle

use of com.yahoo.memory.WritableDirectHandle 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 (WritableDirectHandle memHandler = WritableMemory.allocateDirect(initBytes)) {
        //final MemoryManager memMgr = new MemoryManager();
        //final WritableMemory mem1 = memMgr.request(initBytes);
        final WritableMemory mem1 = memHandler.get();
        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);
    }
}
Also used : WritableDirectHandle(com.yahoo.memory.WritableDirectHandle) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 3 with WritableDirectHandle

use of com.yahoo.memory.WritableDirectHandle in project sketches-core by DataSketches.

the class DirectQuickSelectSketchTest method checkEstModeNativeMemory.

@Test
public void checkEstModeNativeMemory() {
    int k = 4096;
    int u = 2 * k;
    int memCapacity = (k << 4) + (Family.QUICKSELECT.getMinPreLongs() << 3);
    try (WritableDirectHandle memHandler = WritableMemory.allocateDirect(memCapacity)) {
        UpdateSketch usk = UpdateSketch.builder().setNominalEntries(k).build(memHandler.get());
        //for internal checks
        DirectQuickSelectSketch sk1 = (DirectQuickSelectSketch) usk;
        assertTrue(usk.isEmpty());
        for (int i = 0; i < u; i++) {
            usk.update(i);
        }
        double est = usk.getEstimate();
        println("" + est);
        assertEquals(usk.getEstimate(), u, u * .05);
        assertTrue(sk1.getRetainedEntries(false) > k);
    }
}
Also used : WritableDirectHandle(com.yahoo.memory.WritableDirectHandle) Test(org.testng.annotations.Test)

Example 4 with WritableDirectHandle

use of com.yahoo.memory.WritableDirectHandle 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 (WritableDirectHandle memHandler = WritableMemory.allocateDirect(initBytes)) {
        //final MemoryManager memMgr = new MemoryManager();
        //final WritableMemory mem1 = memMgr.request(initBytes);
        final WritableMemory mem1 = memHandler.get();
        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);
    }
}
Also used : WritableDirectHandle(com.yahoo.memory.WritableDirectHandle) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Example 5 with WritableDirectHandle

use of com.yahoo.memory.WritableDirectHandle 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 (WritableDirectHandle memHandler = WritableMemory.allocateDirect(initBytes)) {
        final WritableMemory mem1 = memHandler.get();
        println("Initial mem size: " + mem1.getCapacity());
        //########## Receiving Application
        // The receiving application has been given mem1 to use for a sketch,
        // but alas, it is not ultimately large enough.
        final UpdateDoublesSketch usk1 = DoublesSketch.builder().setK(k).build(mem1);
        assertTrue(usk1.isEmpty());
        //Load the sketch
        for (int i = 0; i < u; i++) {
            // The sketch uses <></>he MemoryRequest, acquired from mem1, 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 mem1 reference is invalid.
        println("\nFinal mem size: " + mem1.getCapacity());
    }
}
Also used : WritableDirectHandle(com.yahoo.memory.WritableDirectHandle) WritableMemory(com.yahoo.memory.WritableMemory) Test(org.testng.annotations.Test)

Aggregations

WritableDirectHandle (com.yahoo.memory.WritableDirectHandle)6 Test (org.testng.annotations.Test)6 WritableMemory (com.yahoo.memory.WritableMemory)5 Memory (com.yahoo.memory.Memory)1