Exemplo n.º 1
0
  protected void read(DataInputStream s) {
    try {
      ref = new WeakReference<DataBuffer>(this, Nd4j.bufferRefQueue());
      referencing = Collections.synchronizedSet(new HashSet<String>());
      dirty = new AtomicBoolean(false);
      allocationMode = AllocationMode.valueOf(s.readUTF());
      length = s.readInt();
      Type t = Type.valueOf(s.readUTF());
      if (t == Type.DOUBLE) {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // double -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // double -> double
            doubleData = new double[length()];
          } else {
            // double -> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readDouble());
          }
        }
      } else {
        if (allocationMode == AllocationMode.HEAP) {
          if (this.dataType() == Type.FLOAT) { // DataBuffer type
            // float -> float
            floatData = new float[length()];
          } else if (this.dataType() == Type.DOUBLE) {
            // float -> double
            doubleData = new double[length()];
          } else {
            // float-> int
            intData = new int[length()];
          }
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        } else {
          wrappedBuffer = ByteBuffer.allocateDirect(length() * getElementSize());
          wrappedBuffer.order(ByteOrder.nativeOrder());
          for (int i = 0; i < length(); i++) {
            put(i, s.readFloat());
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Exemplo n.º 2
0
  @Override
  public void read(DataInputStream s) {
    try {
      referencing = Collections.synchronizedSet(new HashSet<String>());
      dirty = new AtomicBoolean(false);
      allocationMode = AllocationMode.valueOf(s.readUTF());
      length = s.readInt();
      Type currentType = Type.valueOf(s.readUTF());
      type = globalType;
      if (currentType == Type.DOUBLE) elementSize = 8;
      else if (currentType == Type.FLOAT || currentType == Type.INT) elementSize = 4;
      if (currentType != globalType && currentType != Type.INT) {
        log.warn(
            "Loading a data stream with type different from what is set globally. Expect precision loss");
        if (globalType == Type.INT) log.warn("Int to float/double widening UNSUPPORTED!!!");
      }
      pointerIndexerByGlobalType(currentType);
      if (currentType == Type.DOUBLE) {
        for (int i = 0; i < length(); i++) {
          putByGlobalType(i, s.readDouble());
        }
      } else if (currentType == Type.FLOAT) {
        for (int i = 0; i < length(); i++) {
          putByGlobalType(i, s.readFloat());
        }
      } else {
        for (int i = 0; i < length(); i++) {
          putByGlobalType(i, s.readInt());
        }
      }
      wrappedBuffer = pointer.asByteBuffer();

    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }