Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add type neutral data buffer as well as jvm configuration via system …
…properties of run time elements of nd4js properties
- Loading branch information
agibsonccc
committed
Oct 12, 2014
1 parent
7db0c33
commit 0ec1c8f
Showing
68 changed files
with
4,507 additions
and
1,081 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
nd4j-api/src/main/java/org/nd4j/linalg/api/buffer/BaseDataBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
131
nd4j-api/src/main/java/org/nd4j/linalg/api/buffer/DataBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
129
nd4j-api/src/main/java/org/nd4j/linalg/api/buffer/DoubleBuffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.