Skip to content

Commit

Permalink
add type neutral data buffer as well as jvm configuration via system …
Browse files Browse the repository at this point in the history
…properties of run time elements of nd4js properties
  • Loading branch information
agibsonccc committed Oct 12, 2014
1 parent 7db0c33 commit 0ec1c8f
Show file tree
Hide file tree
Showing 68 changed files with 4,507 additions and 1,081 deletions.
Expand Up @@ -65,7 +65,7 @@ else if(val.realComponent().doubleValue() > 1)

for(int i = 0; i < linear.length(); i++) {

float val = linear.get(i);
float val = linear.getFloat(i);
if(val < -1 )
val = -1;
else if(val > 1)
Expand Down
Expand Up @@ -26,9 +26,9 @@ public void testSoftMax() {
INDArray columns = softMaxColumns.sum(0);
INDArray rows = softMaxRows.sum(1);
//softmax along columns: should be 1 in every cell ( note that there are 3 columns)
assertEquals(3,columns.sum(Integer.MAX_VALUE).get(0),1e-1);
assertEquals(3,columns.sum(Integer.MAX_VALUE).getFloat(0),1e-1);
//softmax along rows: should be 1 in every cell (note that there are 2 rows
assertEquals(2,rows.sum(Integer.MAX_VALUE).get(0),1e-1);
assertEquals(2,rows.sum(Integer.MAX_VALUE).getFloat(0),1e-1);

}

Expand All @@ -42,9 +42,9 @@ public void testSoftMaxCOrder() {
INDArray columns = softMaxColumns.sum(0);
INDArray rows = softMaxRows.sum(1);
//softmax along columns: should be 1 in every cell ( note that there are 3 columns)
assertEquals(3,columns.sum(Integer.MAX_VALUE).get(0),1e-1);
assertEquals(3,columns.sum(Integer.MAX_VALUE).getFloat(0),1e-1);
//softmax along rows: should be 1 in every cell (note that there are 2 rows
assertEquals(2,rows.sum(Integer.MAX_VALUE).get(0),1e-1);
assertEquals(2,rows.sum(Integer.MAX_VALUE).getFloat(0),1e-1);

}

Expand Down
@@ -0,0 +1,65 @@
package org.nd4j.linalg.api.buffer;

import java.nio.ByteBuffer;

/**
* Base class for a data buffer handling basic byte operations among other things.
* @author Adam Gibson
*/
public abstract class BaseDataBuffer implements DataBuffer {

protected int length;

protected BaseDataBuffer(int length) {
this.length = length;
}

@Override
public int length() {
return length;
}


public static byte[] toByteArray(double value) {
byte[] bytes = new byte[8];
ByteBuffer.wrap(bytes).putDouble(value);
return bytes;
}

public static byte[] toByteArray(float value) {
byte[] bytes = new byte[4];
ByteBuffer.wrap(bytes).putFloat(value);
return bytes;
}


public static byte[] toByteArray(int value) {
byte[] bytes = new byte[4];
ByteBuffer.wrap(bytes).putFloat(value);
return bytes;
}

public static double toDouble(byte[] bytes) {
return ByteBuffer.wrap(bytes).getDouble();
}

public static int toInt(byte[] bytes) {
return ByteBuffer.wrap(bytes).getInt();
}

public static float toFloat(byte[] bytes) {
return ByteBuffer.wrap(bytes).getFloat();
}

@Override
public <E> E getElement(int i) {
throw new UnsupportedOperationException();

}

@Override
public <E> void put(int i, E element) {
throw new UnsupportedOperationException();
}

}
131 changes: 131 additions & 0 deletions nd4j-api/src/main/java/org/nd4j/linalg/api/buffer/DataBuffer.java
@@ -0,0 +1,131 @@
package org.nd4j.linalg.api.buffer;

import java.io.Serializable;

/**
* A data buffer is an interface
* for handling storage and retrieval of data
* @author Adam Gibson
*/
public interface DataBuffer extends Serializable {


public final static String DOUBLE = "double";
public final static String FLOAT = "float";

/**
* Raw byte array storage
* @return the data represented as a raw byte array
*/
byte[] asBytes();

/**
* The data type of the buffer
* @return the data type of the buffer
*/
public String dataType();

/**
* Return the buffer as a float array
* Relative to the datatype, this will either be a copy
* or a reference. The reference is preferred for
* faster access of data and no copying
* @return the buffer as a float
*/
public float[] asFloat();

/**
* Return the buffer as a double array
* Relative to the datatype, this will either be a copy
* or a reference. The reference is preferred for
* faster access of data and no copying
* @return the buffer as a float
*/
public double[] asDouble();
/**
* Return the buffer as an int array
* Relative to the datatype, this will either be a copy
* or a reference. The reference is preferred for
* faster access of data and no copying
* @return the buffer as a float
*/
public int[] asInt();

/**
* Returns the element buffer of the specified type.
* @param <E>
* @return the element buffer of the specified type
*/
public <E> E[] asType();

/**
* Get element i in the buffer as a double
* @param i the element to getFloat
* @return the element at this index
*/
public double getDouble(int i);
/**
* Get element i in the buffer as a double
* @param i the element to getFloat
* @return the element at this index
*/
public float getFloat(int i);
/**
* Get element i in the buffer as a double
* @param i the element to getFloat
* @return the element at this index
*/
public Number getNumber(int i);
/**
* Get element i in the buffer as a double
* @param i the element to getFloat
* @return the element at this index
*/
public <E> E getElement(int i);

/**
* Assign an element in the buffer to the specified index
* @param i the index
* @param element the element to assign
*/
void put(int i,float element);
/**
* Assign an element in the buffer to the specified index
* @param i the index
* @param element the element to assign
*/
void put(int i,double element);
/**
* Assign an element in the buffer to the specified index
* @param i the index
* @param element the element to assign
*/
void put(int i,int element);
/**
* Assign an element in the buffer to the specified index
* @param i the index
* @param element the element to assign
*/
<E> void put(int i,E element);


/**
* Returns the length of the buffer
* @return the length of the buffer
*/
int length();

/**
* Get the int at the specified index
* @param ix the int at the specified index
* @return the int at the specified index
*/
int getInt(int ix);

/**
* Return a copy of this buffer
* @return a copy of this buffer
*/
DataBuffer dup();

}
129 changes: 129 additions & 0 deletions nd4j-api/src/main/java/org/nd4j/linalg/api/buffer/DoubleBuffer.java
@@ -0,0 +1,129 @@
package org.nd4j.linalg.api.buffer;


import com.google.common.primitives.Bytes;

import java.util.Arrays;

/**
* Double buffer implementation of data buffer
* @author Adam Gibson
*/
public class DoubleBuffer extends BaseDataBuffer {

private double[] buffer;

public DoubleBuffer(int length) {
super(length);
this.buffer = new double[length];
}
public DoubleBuffer(double[] buffer) {
super(buffer.length);
this.buffer = Arrays.copyOf(buffer,buffer.length);
}

@Override
public byte[] asBytes() {
byte[][] ret1 = new byte[length][];
for(int i = 0; i < length; i++) {
ret1[i] = toByteArray(buffer[i]);
}

return Bytes.concat(ret1);
}

@Override
public String dataType() {
return DataBuffer.DOUBLE;
}

@Override
public float[] asFloat() {
float[] ret = new float[length];
for(int i = 0; i < ret.length; i++) {
ret[i] = (float) buffer[i];
}
return ret;
}

@Override
public double[] asDouble() {
return buffer;
}

@Override
public int[] asInt() {
int[] ret = new int[length];
for(int i = 0; i < ret.length; i++) {
ret[i] = (int) buffer[i];
}
return ret;
}

@Override
public <E> E[] asType() {
return null;
}

@Override
public double getDouble(int i) {
return buffer[i];
}

@Override
public float getFloat(int i) {
return (float) buffer[i];
}

@Override
public Number getNumber(int i) {
return buffer[i];
}



@Override
public void put(int i, float element) {
buffer[i] = element;
}

@Override
public void put(int i, double element) {
buffer[i] = element;
}

@Override
public void put(int i, int element) {
buffer[i] = element;
}




@Override
public int getInt(int ix) {
return (int) buffer[ix];
}

@Override
public DataBuffer dup() {
return new DoubleBuffer(buffer);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DoubleBuffer)) return false;

DoubleBuffer that = (DoubleBuffer) o;

if (!Arrays.equals(buffer, that.buffer)) return false;

return true;
}

@Override
public int hashCode() {
return buffer != null ? Arrays.hashCode(buffer) : 0;
}
}

0 comments on commit 0ec1c8f

Please sign in to comment.