Java Code Examples for io.netty.util.internal.PlatformDependent#putInt()

The following examples show how to use io.netty.util.internal.PlatformDependent#putInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseSingleAccumulator.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
public static void fillInts(long addr, long length, int value) {
  if (length == 0) {
    return;
  }

  Preconditions.checkArgument((length & 3) == 0, "Error: length should be aligned at 4-byte boundary");
  /* optimize by writing word at a time */
  long valueAsLong = (((long)value) << 32) | (value & 0xFFFFFFFFL);
  long nLong = length >>>3;
  int remaining = (int) length & 7;
  for (long i = nLong; i > 0; i--) {
    PlatformDependent.putLong(addr, valueAsLong);
    addr += 8;
  }
  if (remaining > 0) {
    /* assert is not necessary but just in case */
    Preconditions.checkArgument(remaining == 4, "Error: detected incorrect remaining length");
    PlatformDependent.putInt(addr, value);
  }
}
 
Example 2
Source File: OffHeapRowWriter.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void endRow() {
  long rowHeaderPos = recordStartAddr();
  // curOffset is equivalent to a byte length of this row.
  PlatformDependent.putInt(rowHeaderPos, curOffset);

  //forward (record offset + fields offset)
  rowHeaderPos += SizeOf.SIZE_OF_INT + curFieldOffset;
  // set remain header field length
  for (int i = curFieldIdx; i < dataTypes.length; i++) {
    PlatformDependent.putInt(rowHeaderPos, MemoryRowBlock.NULL_FIELD_OFFSET);
    rowHeaderPos += SizeOf.SIZE_OF_INT;
  }
  curOffset = 0;
}
 
Example 3
Source File: MinAccumulators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH_INPUT));
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* store the accumulated values(new min or existing) at the target location of accumulation vector */
    PlatformDependent.putLong(minAddr, min(PlatformDependent.getLong(minAddr), newVal, bitVal));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 4
Source File: MinAccumulators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final double newVal = Double.longBitsToDouble(PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH_INPUT)));
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* store the accumulated values(new min or existing) at the target location of accumulation vector */
    PlatformDependent.putLong(minAddr, Double.doubleToLongBits(min(Double.longBitsToDouble(PlatformDependent.getLong(minAddr)), newVal, bitVal)));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 5
Source File: MaxAccumulators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxMemAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxMemAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH_INPUT));
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* store the accumulated values(new max or existing) at the target location of accumulation vector */
    PlatformDependent.putLong(maxAddr, max(PlatformDependent.getLong(maxAddr), newVal, bitVal));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 6
Source File: MaxAccumulators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxMemAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxMemAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final int newVal = PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH_INPUT));
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* store the accumulated values(new max or existing) at the target location of accumulation vector */
    PlatformDependent.putInt(maxAddr, max(PlatformDependent.getInt(maxAddr), newVal, bitVal));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 7
Source File: FieldBufferCopier6.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void copy(long srcAddr, int count) {
  final Reallocator realloc = this.realloc;

  final long maxSrcAddr = srcAddr + count * BUILD_RECORD_LINK_SIZE;
  final long srcOffsetAddrs[] = this.srcOffsetAddrs;
  final long srcDataAddrs[] = this.srcDataAddrs;

  targetAlt.allocateNew(AVG_VAR_WIDTH * count, count);
  long dstOffsetAddr = target.getOffsetBufferAddress() + 4;
  long initDataAddr = realloc.addr();
  long curDataAddr = realloc.addr();
  long maxDataAddr = realloc.max();
  int lastOffset = 0;

  for(; srcAddr < maxSrcAddr; srcAddr += BUILD_RECORD_LINK_SIZE, dstOffsetAddr += 4){
    final int batchIndex = PlatformDependent.getInt(srcAddr);
    final int batchOffset = Short.toUnsignedInt(PlatformDependent.getShort(srcAddr+ 4));

    final long startAndEnd = PlatformDependent.getLong(srcOffsetAddrs[batchIndex] + batchOffset * 4);
    final int firstOffset = (int) startAndEnd;
    final int secondOffset = (int) (startAndEnd >> 32);
    final int len = secondOffset - firstOffset;
    if(curDataAddr + len > maxDataAddr){
      initDataAddr = realloc.ensure(lastOffset + len);
      curDataAddr = initDataAddr + lastOffset;
      maxDataAddr = realloc.max();
    }

    lastOffset += len;
    PlatformDependent.putInt(dstOffsetAddr, lastOffset);
    com.dremio.sabot.op.common.ht2.Copier.copy(srcDataAddrs[batchIndex] + firstOffset, curDataAddr, len);
    curDataAddr += len;
  }

  realloc.setCount(count);
}
 
Example 8
Source File: UnsafeByteBufUtil.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
static void setInt(byte[] array, int index, int value) {
    if (UNALIGNED) {
        PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value));
    } else {
        PlatformDependent.putByte(array, index, (byte) (value >>> 24));
        PlatformDependent.putByte(array, index + 1, (byte) (value >>> 16));
        PlatformDependent.putByte(array, index + 2, (byte) (value >>> 8));
        PlatformDependent.putByte(array, index + 3, (byte) value);
    }
}
 
Example 9
Source File: MinAccumulators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final float newVal = Float.intBitsToFloat(PlatformDependent.getInt(incomingValue + (incomingIndex * WIDTH_INPUT)));
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* store the accumulated values(new min or existing) at the target location of accumulation vector */
    PlatformDependent.putInt(minAddr, Float.floatToIntBits(min(Float.intBitsToFloat(PlatformDependent.getInt(minAddr)), newVal, bitVal)));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 10
Source File: MinAccumulatorsNoSpill.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count) {
  final long maxAddr = memoryAddr + count * WIDTH_ORDINAL;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;

  int incomingIndex = 0;
  for(long ordinalAddr = memoryAddr; ordinalAddr < maxAddr; ordinalAddr += WIDTH_ORDINAL, incomingIndex++){
    final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH_INPUT));
    final int tableIndex = PlatformDependent.getInt(ordinalAddr);
    int chunkIndex = tableIndex >>> LBlockHashTableNoSpill.BITS_IN_CHUNK;
    int chunkOffset = tableIndex & LBlockHashTableNoSpill.CHUNK_OFFSET_MASK;
    final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    // first 4 bytes are the number of days (in little endian, that's the bottom 32 bits)
    // second 4 bytes are the number of milliseconds (in little endian, that's the top 32 bits)
    final int newDays = (int) newVal;
    final int newMillis = (int)(newVal >>> 32);
    // To compare the pairs of day/milli, we swap them, with days getting the most significant bits
    // The incoming value is updated to either be MAX (if incoming is null), or keep as is (if the value is not null)
    final long newSwappedVal = ((((long)newDays) << 32) | newMillis) * bitVal + Long.MAX_VALUE * (bitVal ^ 1);
    final long minVal = PlatformDependent.getLong(minAddr);
    final int minDays = (int) minVal;
    final int minMillis = (int)(minVal >>> 32);
    final long minSwappedVal = (((long)minDays) << 32) | minMillis;
    PlatformDependent.putLong(minAddr, (minSwappedVal < newSwappedVal) ? minVal : newVal);
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 11
Source File: OffHeapRowWriter.java    From tajo with Apache License 2.0 5 votes vote down vote up
@Override
public void putInt4(int val) {
  ensureSize(SizeOf.SIZE_OF_INT);
  long addr = currentAddr();

  PlatformDependent.putInt(addr, val);
  putFieldHeader(addr, curOffset);
  forwardField(SizeOf.SIZE_OF_INT);
}
 
Example 12
Source File: SumAccumulators.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int scale = ((DecimalVector) inputVector).getScale();
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    java.math.BigDecimal newVal = DecimalUtils.getBigDecimalFromLEBytes(incomingValue + (incomingIndex * WIDTH_INPUT), valBuf, scale);
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long sumAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* store the accumulated values at the target location of accumulation vector */
    PlatformDependent.putLong(sumAddr, Double.doubleToLongBits(Double.longBitsToDouble(PlatformDependent.getLong(sumAddr)) + newVal.doubleValue() * bitVal));
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 13
Source File: DrillBuf.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public ByteBuf writeInt(int value) {
  BoundsChecking.ensureWritable(this, 4);
  PlatformDependent.putInt(addr(writerIndex), value);
  writerIndex += 4;
  return this;
}
 
Example 14
Source File: DrillBuf.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public ByteBuf setInt(int index, int value) {
  chk(index, 4);
  PlatformDependent.putInt(addr(index), value);
  return this;
}
 
Example 15
Source File: UnsafeDirectLittleEndian.java    From Bats with Apache License 2.0 4 votes vote down vote up
private void _setInt(int index, int value) {
  PlatformDependent.putInt(addr(index), value);
}
 
Example 16
Source File: MinAccumulators.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  final long maxOrdinalAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;

  // Like every accumulator, the code below essentially implements:
  //   accumulators[ordinals[i]] += inputs[i]
  // with the only complication that both accumulators and inputs are bits.
  // There's nothing we can do about the locality of the accumulators, but inputs can be processed a word at a time.
  // Algorithm:
  // - get 64 bits worth of inputs, until all inputs exhausted. For each long:
  //   - find the accumulator word it pertains to
  //   - read/update/write the accumulator bit
  // Unfortunately, there is no locality: the incoming partition+ordinal array has been ordered by partition, which
  // removes the ability to process input bits a word at a time
  // In the code below:
  // - input* refers to the data values in the incoming batch
  // - ordinal* refers to the temporary table that hashAgg passes in, identifying which hash table entry each input matched to
  // - min* refers to the accumulator
  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxOrdinalAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    final long minBitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);  // 32-bit read-update-write
    // Update rules:
    // min of two boolean values boils down to doing a bitwise AND on the two
    // If the input bit is set, we update both the accumulator value and its bit
    //    -- the accumulator is AND-ed with the value of the input bit
    //    -- the accumulator bit is OR-ed with 1 (since the input is valid)
    // If the input bit is not set, we update neither the accumulator nor its bit
    //    -- the accumulator is AND-ed with a 1 (thus remaining unchanged)
    //    -- the accumulator bit is OR-ed with 0 (thus remaining unchanged)
    // Thus, the logical function for updating the accumulator is: oldVal AND (NOT(inputBit) OR inputValue)
    // Thus, the logical function for updating the accumulator is: oldBitVal OR inputBit
    final int inputBitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> BITS_PER_BYTE_SHIFT))) >>> (incomingIndex & (BITS_PER_BYTE - 1))) & 1;
    final int inputVal = (PlatformDependent.getByte(incomingValue + ((incomingIndex >>> BITS_PER_BYTE_SHIFT))) >>> (incomingIndex & (BITS_PER_BYTE - 1))) & 1;
    final int minBitUpdateVal = inputBitVal << (chunkOffset & 31);
    // NB: ~inputBitVal will set all the bits to 1, with only the LSB set to 0 or 1. Shifting left leaves zeroes
    // in the bottom (chunkOffset & 31) bits. They need to get set to 1s too
    int minUpdateVal = ((~inputBitVal) | inputVal) << (chunkOffset & 31); // see note above
    minUpdateVal = minUpdateVal | ((1 << (chunkOffset & 31)) - 1);
    final long minAddr = valueAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);  // 32-bit read-update-write
    PlatformDependent.putInt(minAddr, PlatformDependent.getInt(minAddr) & minUpdateVal);
    PlatformDependent.putInt(minBitUpdateAddr, PlatformDependent.getInt(minBitUpdateAddr) | minBitUpdateVal);
  }
}
 
Example 17
Source File: MinAccumulators.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    // no point continuing.
    if (bitVal == 0) {
      continue;
    }
    /* get the corresponding data from input vector -- source data for accumulation */
    long addressOfInput = incomingValue + (incomingIndex * WIDTH_INPUT);
    long newValLow = PlatformDependent.getLong(addressOfInput);
    long newValHigh = PlatformDependent.getLong(addressOfInput + DecimalUtils.LENGTH_OF_LONG);
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long minAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    /* Get current value and compare */
    long curValLow = PlatformDependent.getLong(minAddr);
    long curValHigh = PlatformDependent.getLong(minAddr + DecimalUtils.LENGTH_OF_LONG);
    int compare = DecimalUtils.compareDecimalsAsTwoLongs(newValHigh, newValLow, curValHigh,
      curValLow);

    if (compare < 0) {
      /* store the accumulated values(new min or existing) at the target location of accumulation vector */
      PlatformDependent.putLong(minAddr, newValLow);
      PlatformDependent.putLong(minAddr + DecimalUtils.LENGTH_OF_LONG, newValHigh);
      PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
    }

  }
}
 
Example 18
Source File: MaxAccumulators.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void accumulate(final long memoryAddr, final int count,
                       final int bitsInChunk, final int chunkOffsetMask) {
  final long maxMemAddr = memoryAddr + count * PARTITIONINDEX_HTORDINAL_WIDTH;
  FieldVector inputVector = getInput();
  final long incomingBit = inputVector.getValidityBufferAddress();
  final long incomingValue = inputVector.getDataBufferAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;
  final int maxValuesPerBatch = super.maxValuesPerBatch;

  for (long partitionAndOrdinalAddr = memoryAddr; partitionAndOrdinalAddr < maxMemAddr; partitionAndOrdinalAddr += PARTITIONINDEX_HTORDINAL_WIDTH) {
    /* get the hash table ordinal */
    final int tableIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + HTORDINAL_OFFSET);
    /* get the index of data in input vector */
    final int incomingIndex = PlatformDependent.getInt(partitionAndOrdinalAddr + KEYINDEX_OFFSET);
    /* get the corresponding data from input vector -- source data for accumulation */
    final long newVal = PlatformDependent.getLong(incomingValue + (incomingIndex * WIDTH_INPUT));
    final int bitVal = (PlatformDependent.getByte(incomingBit + ((incomingIndex >>> 3))) >>> (incomingIndex & 7)) & 1;
    /* get the hash table batch index */
    final int chunkIndex = tableIndex >>> bitsInChunk;
    final int chunkOffset = tableIndex & chunkOffsetMask;
    /* get the target addresses of accumulation vector */
    final long maxAddr = valueAddresses[chunkIndex] + (chunkOffset) * WIDTH_ACCUMULATOR;
    final long bitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
    final int bitUpdateVal = bitVal << (chunkOffset & 31);
    // first 4 bytes are the number of days (in little endian, that's the bottom 32 bits)
    // second 4 bytes are the number of milliseconds (in little endian, that's the top 32 bits)
    final int newDays = (int) newVal;
    final int newMillis = (int)(newVal >>> 32);
    // To compare the pairs of day/milli, we swap them, with days getting the most significant bits
    // The incoming value is updated to either be MAX (if incoming is null), or keep as is (if the value is not null)
    final long newSwappedVal = ((((long)newDays) << 32) | newMillis) * bitVal + Long.MAX_VALUE * (bitVal ^ 1);
    final long maxVal = PlatformDependent.getLong(maxAddr);
    final int maxDays = (int) maxVal;
    final int maxMillis = (int)(maxVal >>> 32);
    final long maxSwappedVal = (((long)maxDays) << 32) | maxMillis;
    /* store the accumulated values(new min or existing) at the target location of accumulation vector */
    PlatformDependent.putLong(maxAddr, (maxSwappedVal > newSwappedVal) ? maxVal : newVal);
    PlatformDependent.putInt(bitUpdateAddr, PlatformDependent.getInt(bitUpdateAddr) | bitUpdateVal);
  }
}
 
Example 19
Source File: MaxAccumulatorsNoSpill.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public void accumulate(final long memoryAddr, final int count) {
  List<ArrowBuf> buffers = getInput().getFieldBuffers();
  final long incomingBit = buffers.get(0).memoryAddress();
  final long incomingValue = buffers.get(1).memoryAddress();
  final long[] bitAddresses = this.bitAddresses;
  final long[] valueAddresses = this.valueAddresses;

  final long numWords = (count + (BITS_PER_LONG-1)) >>> BITS_PER_LONG_SHIFT; // rounded up number of words that cover 'count' bits
  final long maxInputAddr = incomingValue + numWords * WIDTH_LONG;
  final long maxOrdinalAddr = memoryAddr + count * WIDTH_ORDINAL;

  // Like every accumulator, the code below essentially implements:
  //   accumulators[ordinals[i]] += inputs[i]
  // with the only complication that both accumulators and inputs are bits.
  // There's nothing we can do about the locality of the accumulators, but inputs can be processed a word at a time.
  // Algorithm:
  // - get 64 bits worth of inputs, until all inputs exhausted. For each long:
  //   - find the accumulator word it pertains to
  //   - read/update/write the accumulator bit
  // In the code below:
  // - input* refers to the data values in the incoming batch
  // - ordinal* refers to the temporary table that hashAgg passes in, identifying which hash table entry each input matched to
  // - min* refers to the accumulator
  for (long inputAddr = incomingValue, inputBitAddr = incomingBit, batchCount = 0;
       inputAddr < maxInputAddr;
       inputAddr += WIDTH_LONG, inputBitAddr += WIDTH_LONG, batchCount++) {
    final long inputBatch = PlatformDependent.getLong(inputAddr);
    final long inputBits = PlatformDependent.getLong(inputBitAddr);
    long ordinalAddr = memoryAddr + (batchCount << BITS_PER_LONG_SHIFT);
    for (long bitNum = 0; bitNum < BITS_PER_LONG && ordinalAddr < maxOrdinalAddr; bitNum++, ordinalAddr += WIDTH_ORDINAL) {
      final int tableIndex = PlatformDependent.getInt(ordinalAddr);
      int chunkIndex = tableIndex >>> LBlockHashTableNoSpill.BITS_IN_CHUNK;
      int chunkOffset = tableIndex & LBlockHashTableNoSpill.CHUNK_OFFSET_MASK;
      final long minBitUpdateAddr = bitAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
      // Update rules:
      // max of two boolean values boils down to doing a bitwise OR on the two
      // If the input bit is set, we update both the accumulator value and its bit
      //    -- the accumulator is OR-ed with the value of the input bit
      //    -- the accumulator bit is OR-ed with 1 (since the input is valid)
      // If the input bit is not set, we update neither the accumulator nor its bit
      //    -- the accumulator is OR-ed with a 0 (thus remaining unchanged)
      //    -- the accumulator bit is OR-ed with 0 (thus remaining unchanged)
      // Thus, the logical function for updating the accumulator is: oldVal OR (inputBit AND inputValue)
      // Thus, the logical function for updating the accumulator is: oldBitVal OR inputBit
      // Because the operations are all done in a word length (and not on an individual bit), the AND value for
      // updating the accumulator must have all its other bits set to 1
      final int inputBitVal = (int)((inputBits >>> bitNum) & 0x01);
      final int inputVal = (int)((inputBatch >>> bitNum) & 0x01);
      final int minBitUpdateVal = inputBitVal << (chunkOffset & 31);
      int minUpdateVal = (inputBitVal & inputVal) << (chunkOffset & 31);
      final long minAddr = valueAddresses[chunkIndex] + ((chunkOffset >>> 5) * 4);
      PlatformDependent.putInt(minAddr, PlatformDependent.getInt(minAddr) | minUpdateVal);
      PlatformDependent.putInt(minBitUpdateAddr, PlatformDependent.getInt(minBitUpdateAddr) | minBitUpdateVal);
    }
  }
}
 
Example 20
Source File: LBlockHashTableEight.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private final void rehash(int newCapacity) {
  final long free = this.freeValue;
  // grab old references.

  final long[] oldFixedAddrs = this.tableFixedAddresses;

  final int oldNullKeyOrdinal = nullKeyOrdinal;
  if (oldNullKeyOrdinal != NO_MATCH) {
    removeNull();
  }

  try(RollbackCloseable closer = new RollbackCloseable()){ // Close old control blocks when done rehashing.
    for(FixedBlockVector cb : this.fixedBlocks){
      closer.add(cb);
    }

    internalInit(newCapacity);
    final int capacityMask = this.capacityMask;
    final long[] newFixedAddrs = this.tableFixedAddresses;

    for(int batch = 0; batch < oldFixedAddrs.length; batch++){
      long addr = oldFixedAddrs[batch];
      final long max = addr + (MAX_VALUES_PER_BATCH * BLOCK_WIDTH);
      for(long oldFixedAddr = addr; oldFixedAddr < max; oldFixedAddr += BLOCK_WIDTH){
        long key = PlatformDependent.getLong(oldFixedAddr);

        if(key != free){
          int newIndex = hash(key) & capacityMask; // get previously computed hash and slice it.
          long newFixedAddr = newFixedAddrs[newIndex >>> BITS_IN_CHUNK] + ((newIndex & CHUNK_OFFSET_MASK) * BLOCK_WIDTH);

          if (PlatformDependent.getLong(newFixedAddr) != free) {
            while (true) {
              newIndex = (newIndex - 1) & capacityMask;
              newFixedAddr = newFixedAddrs[newIndex >>> BITS_IN_CHUNK] + ((newIndex & CHUNK_OFFSET_MASK) * BLOCK_WIDTH);
              if (PlatformDependent.getLong(newFixedAddr) == free) {
                break;
              }
            }
          }

          final int ordinalValue = PlatformDependent.getInt(oldFixedAddr + KEY_WIDTH);
          PlatformDependent.putLong(newFixedAddr, key);
          PlatformDependent.putInt(newFixedAddr + KEY_WIDTH, ordinalValue);

        }
      }
    }

  } catch (Exception e) {
    throw Throwables.propagate(e);
  }

  if (oldNullKeyOrdinal != NO_MATCH) {
    insertNull(oldNullKeyOrdinal);
  }
}