Exemple #1
0
  @Override
  public DataBuffer dup() {
    DataBuffer ret = create(length);
    for (int i = 0; i < ret.length(); i++) ret.put(i, getDouble(i));

    return ret;
  }
Exemple #2
0
 @Override
 public void copyAtStride(
     DataBuffer buf, long n, long stride, long yStride, long offset, long yOffset) {
   if (dataType() == Type.FLOAT) {
     for (int i = 0; i < n; i++) {
       put(offset + i * stride, buf.getFloat(yOffset + i * yStride));
     }
   } else {
     for (int i = 0; i < n; i++) {
       put(offset + i * stride, buf.getDouble(yOffset + i * yStride));
     }
   }
 }
Exemple #3
0
  @Override
  public void assign(DataBuffer data) {
    if (data.length() != length())
      throw new IllegalArgumentException(
          "Unable to assign buffer of length "
              + data.length()
              + " to this buffer of length "
              + length());

    for (int i = 0; i < data.length(); i++) {
      put(i, data.getDouble(i));
    }
  }
Exemple #4
0
  @Override
  public boolean equals(Object o) {
    if (o instanceof DataBuffer) {
      DataBuffer d = (DataBuffer) o;
      if (d.length() != length()) return false;
      for (int i = 0; i < length(); i++) {
        double eps = Math.abs(getDouble(i) - d.getDouble(i));
        if (eps > Nd4j.EPS_THRESHOLD) return false;
      }
    }

    return true;
  }
Exemple #5
0
  @Override
  public boolean equals(Object o) {
    // FIXME: this is BAD. it takes too long to work, and it breaks general equals contract
    if (o instanceof DataBuffer) {
      DataBuffer d = (DataBuffer) o;
      if (d.length() != length()) return false;
      for (int i = 0; i < length(); i++) {
        double eps = Math.abs(getDouble(i) - d.getDouble(i));
        if (eps > 1e-12) return false;
      }
    }

    return true;
  }
Exemple #6
0
  @Override
  public DataBuffer dup() {
    if (floatData != null) {
      return create(floatData);
    } else if (doubleData != null) {
      return create(doubleData);
    } else if (intData != null) {
      return create(intData);
    }

    DataBuffer ret = create(length);
    for (int i = 0; i < ret.length(); i++) ret.put(i, getDouble(i));

    return ret;
  }
  /**
   * Returns a ByteBuffer of BufferedImage data. Ensure BufferedImage is of 4BYTE_ABGR type. If
   * imageFormat is set to GL_RGBA, byte stream will be converted.
   */
  private ByteBuffer getByteBuffer(final BufferedImage _image) {
    final DataBuffer buffer = _image.getRaster().getDataBuffer();
    final int type = buffer.getDataType();

    if (type == DataBuffer.TYPE_BYTE) {
      final byte[] data = ((DataBufferByte) buffer).getData();
      if (imageFormat == GL3.GL_RGBA) {
        convertABGRtoRGBA(data);
      }

      return ByteBuffer.wrap(data);
    }

    System.out.println("Failed to determine DataBuffer type.");
    return null;
  }
Exemple #8
0
  /** Requests an HTTP resource and returns its response as a string */
  public static String request(String httpUrl, Transaction tx) throws IOException {
    checkTransaction(tx);

    DataBuffer buffer = new DataBuffer(256, false);
    InputStream is = null;
    Connection conn = null;
    try {
      // append connection suffix
      httpUrl += getConnectionSuffix();
      Logger.log("Opening URL: " + httpUrl);
      conn = Connector.open(httpUrl);
      tx.setNetworkOperation(conn, is);
      if (conn instanceof HttpConnection) {
        HttpConnection httpConn = (HttpConnection) conn;
        int responseCode = httpConn.getResponseCode();
        is = httpConn.openInputStream();
        tx.setNetworkOperation(conn, is);
        int length = is.read(buffer.getArray());
        buffer.setLength(length);

        String response =
            new String(buffer.getArray(), buffer.getArrayStart(), buffer.getArrayLength());
        if (responseCode == 200) {
          Logger.log("HTTP response: " + response);
          return response;
        } else {
          Logger.warn("HTTP error response: " + response);
          throw new IOException("Http error: " + responseCode + ", " + response);
        }
      } else {
        throw new IOException("Can not make HTTP connection for URL '" + httpUrl + "'");
      }
    } finally {
      PushUtils.close(conn, is, null);
      tx.clearNetworkOperation();
    }
  }
  public void run() {

    StreamConnection stream = null;
    InputStream input = null;
    MDSPushInputStream pushInputStream = null;

    while (!_stop) {
      try {

        // Synchronize here so that we don't end up creating a connection that is never closed.
        synchronized (this) {
          // Open the connection once (or re-open after an IOException),  so we don't end up
          // in a race condition, where a push is lost if it comes in before the connection
          // is open again. We open the url with a parameter that indicates that we should
          // always use MDS when attempting to connect.
          int port = RhoConf.getInstance().getInt("push_port");
          if (port == 0) port = 100;
          _notify = (StreamConnectionNotifier) Connector.open(URL + port + ";deviceside=false");
        }

        while (!_stop) {

          // NOTE: the following will block until data is received.
          LOG.TRACE("Block push thread until data is recieved");
          stream = _notify.acceptAndOpen();
          LOG.TRACE("Recieved push data");

          try {
            input = stream.openInputStream();
            pushInputStream = new MDSPushInputStream((HttpServerConnection) stream, input);

            // Extract the data from the input stream.

            DataBuffer db = new DataBuffer();
            byte[] data = new byte[CHUNK_SIZE];
            int chunk = 0;

            while (-1 != (chunk = input.read(data))) {
              db.write(data, 0, chunk);
            }

            processPushMessage(data);

            // This method is called to accept the push.
            pushInputStream.accept();

            input.close();
            stream.close();

            data = db.getArray();

          } catch (IOException e1) {
            // A problem occurred with the input stream , however, the original
            // StreamConnectionNotifier is still valid.
            System.err.println(e1.toString());

            if (input != null) {
              try {
                input.close();
              } catch (IOException e2) {
              }
            }

            if (stream != null) {
              try {
                stream.close();
              } catch (IOException e2) {
              }
            }
          }
        }

        _notify.close();
        _notify = null;

      } catch (IOException ioe) {
        LOG.TRACE("Exception thrown by _notify.acceptAndOpen() - exiting push thread");

        // Likely the stream was closed. Catches the exception thrown by
        // _notify.acceptAndOpen() when this program exits.

        _stop = true;

        if (_notify != null) {
          try {
            _notify.close();
            _notify = null;
          } catch (IOException e) {
          }
        }
      }
    }
  }
Exemple #10
0
 @Override
 public boolean sameUnderlyingData(DataBuffer buffer) {
   return pointer() == buffer.pointer();
 }
Exemple #11
0
  /**
   * Meant for creating another view of a buffer
   *
   * @param underlyingBuffer the underlying buffer to create a view from
   * @param length the length of the view
   * @param offset the offset for the view
   */
  protected BaseDataBuffer(DataBuffer underlyingBuffer, long length, long offset) {
    if (length < 1) throw new IllegalArgumentException("Length must be >= 1");
    initTypeAndSize();
    this.length = length;
    this.offset = offset;
    this.allocationMode = underlyingBuffer.allocationMode();
    this.elementSize = underlyingBuffer.getElementSize();
    this.underlyingLength = underlyingBuffer.underlyingLength();
    this.wrappedDataBuffer = underlyingBuffer;

    // Adding link to original databuffer
    if (underlyingBuffer.originalDataBuffer() == null) {
      this.originalBuffer = underlyingBuffer;
      this.originalOffset = offset;
    } else {

      this.originalBuffer = underlyingBuffer.originalDataBuffer();

      // FIXME: please don't remove this comment, since there's probably a bug in current offset()
      // impl,
      // and this line will change originalOffset accroding to proper offset() impl
      // FIXME: [email protected]
      this.originalOffset = offset; // + underlyingBuffer.originalOffset();
    }

    if (underlyingBuffer.dataType() == Type.DOUBLE) {
      pointer = underlyingBuffer.pointer();
      indexer = underlyingBuffer.indexer();
    } else if (underlyingBuffer.dataType() == Type.FLOAT) {
      pointer = underlyingBuffer.pointer();
      indexer = underlyingBuffer.indexer();
    } else if (underlyingBuffer.dataType() == Type.INT) {
      pointer = underlyingBuffer.pointer();
      indexer = underlyingBuffer.indexer();
    }
  }
Exemple #12
0
  /**
   * Meant for creating another view of a buffer
   *
   * @param underlyingBuffer the underlying buffer to create a view from
   * @param length the length of the view
   * @param offset the offset for the view
   */
  protected BaseDataBuffer(DataBuffer underlyingBuffer, int length, int offset) {
    this.length = length;
    this.offset = offset;
    this.allocationMode = underlyingBuffer.allocationMode();
    this.elementSize = underlyingBuffer.getElementSize();
    this.underlyingLength = underlyingBuffer.underlyingLength() - offset;
    this.wrappedDataBuffer = underlyingBuffer;

    if (underlyingBuffer.dataType() == Type.DOUBLE) {
      if (underlyingBuffer.allocationMode() == AllocationMode.HEAP) {
        double[] underlyingArray = (double[]) underlyingBuffer.array();
        this.doubleData = underlyingArray;
      } else {
        ByteBuffer underlyingBuff = underlyingBuffer.asNio();
        this.wrappedBuffer = underlyingBuff;
      }
    } else if (underlyingBuffer.dataType() == Type.FLOAT) {
      if (underlyingBuffer.allocationMode() == AllocationMode.HEAP) {
        float[] underlyingArray = (float[]) underlyingBuffer.array();
        this.floatData = underlyingArray;
      } else {
        ByteBuffer underlyingBuff = underlyingBuffer.asNio();
        this.wrappedBuffer = underlyingBuff;
      }
    } else if (underlyingBuffer.dataType() == Type.INT) {
      if (underlyingBuffer.allocationMode() == AllocationMode.HEAP) {
        int[] underlyingArray = (int[]) underlyingBuffer.array();
        this.intData = underlyingArray;
      } else {
        ByteBuffer underlyingBuff = underlyingBuffer.asNio();
        this.wrappedBuffer = underlyingBuff;
      }
    }
  }