use of com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasFloatMatrix mat, boolean trans, IntDummyVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
float[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new float[n];
} else {
assert n == v.getDim();
resArr = new float[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
float[] data = mat.getData();
if (trans) {
for (int j = 0; j < c; j++) {
int[] idxs = v.getIndices();
for (int i : idxs) {
resArr[j] += data[i * c + j];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getIndices();
for (int j : idxs) {
resArr[i] += data[i * c + j];
}
}
}
IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
use of com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasFloatMatrix mat, boolean trans, IntFloatVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
float[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new float[n];
} else {
assert n == v.getDim();
resArr = new float[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
float[] data = mat.getData();
if (v.isDense()) {
float[] tempArray = v.getStorage().getValues();
if (trans) {
blas.sgemv("N", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
} else {
blas.sgemv("T", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
}
} else if (v.isSparse()) {
if (trans) {
for (int j = 0; j < c; j++) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getFloatValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getFloatValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
float[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[j] += data[idxs[k] * c + j] * vals[k];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getStorage().getIndices();
float[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[i] += data[i * c + idxs[k]] * vals[k];
}
}
}
}
IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
use of com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Vector apply(BlasFloatMatrix mat, boolean trans, IntIntVector v) {
int m = mat.getNumRows(), n = mat.getNumCols();
float[] resArr;
if (trans) {
assert m == v.getDim();
resArr = new float[n];
} else {
assert n == v.getDim();
resArr = new float[m];
}
int r = mat.getNumRows(), c = mat.getNumCols();
float[] data = mat.getData();
if (v.isDense()) {
float[] tempArray = ArrayCopy.copy(v.getStorage().getValues(), new float[v.getDim()]);
if (trans) {
blas.sgemv("N", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
} else {
blas.sgemv("T", c, r, 1.0f, data, c, tempArray, 1, 0.0f, resArr, 1);
}
} else if (v.isSparse()) {
if (trans) {
for (int j = 0; j < c; j++) {
ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
resArr[j] += data[i * c + j] * entry.getIntValue();
}
}
} else {
for (int i = 0; i < r; i++) {
ObjectIterator<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
resArr[i] += data[i * c + j] * entry.getIntValue();
}
}
}
} else {
// sorted
if (trans) {
for (int j = 0; j < r; j++) {
int[] idxs = v.getStorage().getIndices();
int[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[j] += data[idxs[k] * c + j] * vals[k];
}
}
} else {
for (int i = 0; i < r; i++) {
int[] idxs = v.getStorage().getIndices();
int[] vals = v.getStorage().getValues();
for (int k = 0; k < idxs.length; k++) {
resArr[i] += data[i * c + idxs[k]] * vals[k];
}
}
}
}
IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
return new IntFloatVector(v.getMatrixId(), v.getClock(), 0, resArr.length, storage);
}
use of com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage in project angel by Tencent.
the class UpdatePSFTest method testDenseFloatUDF.
public void testDenseFloatUDF() throws Exception {
Worker worker = LocalClusterContext.get().getWorker(workerAttempt0Id).getWorker();
MatrixClient client1 = worker.getPSAgent().getMatrixClient(DENSE_FLOAT_MAT, 0);
int matrixW1Id = client1.getMatrixId();
int[] index = genIndexs(feaNum, nnz);
IntFloatVector deltaVec = new IntFloatVector(feaNum, new IntFloatDenseVectorStorage(feaNum));
for (int i = 0; i < feaNum; i++) {
deltaVec.set(i, i);
}
deltaVec.setRowId(0);
Vector[] updates = new Vector[1];
updates[0] = deltaVec;
client1.asyncUpdate(new IncrementRows(new IncrementRowsParam(matrixW1Id, updates))).get();
IntFloatVector row = (IntFloatVector) client1.getRow(0);
for (int id : index) {
Assert.assertTrue(row.get(id) == deltaVec.get(id));
}
Assert.assertTrue(feaNum == row.size());
}
use of com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage in project angel by Tencent.
the class HashRouterUtils method splitIntFloatVector.
public static void splitIntFloatVector(KeyHash hasher, MatrixMeta matrixMeta, IntFloatVector vector, KeyValuePart[] dataParts) {
int dataPartNum = dataParts.length;
int dataPartNumMinus1 = dataPartNum - 1;
if (isPow2(dataPartNum)) {
IntFloatVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
IntFloatSparseVectorStorage sparseStorage = (IntFloatSparseVectorStorage) storage;
ObjectIterator<Int2FloatMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getIntKey()) & dataPartNumMinus1;
((HashIntKeysFloatValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getFloatValue());
}
} else if (storage.isDense()) {
// Get values
IntFloatDenseVectorStorage denseStorage = (IntFloatDenseVectorStorage) storage;
float[] values = denseStorage.getValues();
for (int i = 0; i < values.length; i++) {
int partId = computeHashCode(hasher, i) & dataPartNumMinus1;
((HashIntKeysFloatValuesPart) dataParts[partId]).add(i, values[i]);
}
} else {
// Key and value array pair
IntFloatSortedVectorStorage sortStorage = (IntFloatSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
float[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) & dataPartNumMinus1;
((HashIntKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
} else {
IntFloatVectorStorage storage = vector.getStorage();
if (storage.isSparse()) {
// Use iterator
IntFloatSparseVectorStorage sparseStorage = (IntFloatSparseVectorStorage) storage;
ObjectIterator<Int2FloatMap.Entry> iter = sparseStorage.entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry keyValue = iter.next();
int partId = computeHashCode(hasher, keyValue.getIntKey()) % dataPartNum;
((HashIntKeysFloatValuesPart) dataParts[partId]).add(keyValue.getIntKey(), keyValue.getFloatValue());
}
} else if (storage.isDense()) {
// Get values
IntFloatDenseVectorStorage denseStorage = (IntFloatDenseVectorStorage) storage;
float[] values = denseStorage.getValues();
for (int i = 0; i < values.length; i++) {
int partId = computeHashCode(hasher, i) % dataPartNum;
((HashIntKeysFloatValuesPart) dataParts[partId]).add(i, values[i]);
}
} else {
// Key and value array pair
IntFloatSortedVectorStorage sortStorage = (IntFloatSortedVectorStorage) storage;
int[] keys = sortStorage.getIndices();
float[] values = sortStorage.getValues();
for (int i = 0; i < keys.length; i++) {
int partId = computeHashCode(hasher, keys[i]) % dataPartNum;
((HashIntKeysFloatValuesPart) dataParts[partId]).add(keys[i], values[i]);
}
}
}
}
Aggregations