Skip to content

Commit

Permalink
[SYNC] Release 1.2.0 (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
andyyehoo committed Sep 13, 2017
1 parent e170beb commit c54fafa
Show file tree
Hide file tree
Showing 8 changed files with 1,086 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.tencent.angel.ml.math.VectorType;
import com.tencent.angel.protobuf.generated.MLProtos;

/**
* Sparse double vector, it only contains the element indexes as the values are always 1.
*/
public class SparseDummyVector extends TAbstractVector {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
* The interface parameter server protocol.
*/
public interface PSProtocol extends VersionedProtocol ,MasterPSService.BlockingInterface{
/**
* The VERSION.
*/
static long VERSION = 0L;

static long VERSION = 0L;
/**
* The interface Async protocol.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static MatrixClient get(String matrixName, int taskIndex) throws InvalidP
* Get a matrix client.
*
* @param matrixId matrix id
* @param taskIndex task index
* @param taskId task id
* @return MatrixClient matrix client
* @throws InvalidParameterException matrix does not exist
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Tencent is pleased to support the open source community by making Angel available.
*
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/

package com.tencent.angel.spark.math.vector

import scala.util.Random

import com.tencent.angel.spark.client.PSClient
import com.tencent.angel.spark.context.{AngelPSContext, PSContext}
import com.tencent.angel.spark.{PSFunSuite, SharedPSContext}

class DensePSVectorSuite extends PSFunSuite with SharedPSContext {

private val dim = 10
private val capacity = 10
private var _psContext: PSContext = _
private var _psVector: DensePSVector = _

override def beforeAll(): Unit = {
super.beforeAll()
_psContext = PSContext.instance()
_psVector = PSVector.dense(dim, capacity)
}

override def afterAll(): Unit = {
_psContext.destroyVectorPool(_psVector)
super.afterAll()
}

test("fill with value") {
val dVector = PSVector.duplicate(_psVector).fill(3.14)

dVector.toRemote.pull().foreach { element =>
assert(element == 3.14)
}
}

test("fill with array") {
val rand = new Random()
val localArray = (0 until dim).toArray.map { i =>
rand.nextDouble()
}
val dVector = PSVector.duplicate(_psVector).fill(localArray)

val remoteArray = dVector.toRemote.pull()

(0 until dim).foreach { index =>
assert(math.abs(remoteArray(index) - localArray(index)) < 1e-6)
}
}

test("randomUniform") {
val dVector = PSVector.duplicate(_psVector).randomUniform(0.0, 1.0)

var isCorrect = true
dVector.toRemote.pull().foreach(x => if (x < 0.0 || x > 1.0) isCorrect = false )
assert(isCorrect)
}

test("randomNormal") {

val dVector = PSVector.dense(10000, 2).randomNormal(0.0, 1.0)

val array = dVector.toRemote.pull()
val mean = array.sum / array.length
val variety = array.map(x => math.pow(x - mean, 2.0)).sum / (array.length - 1)

val tol = 0.1
assert(math.abs(mean - 0.0) < tol)
assert(math.abs(math.sqrt(variety) - 1.0) < tol)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Tencent is pleased to support the open source community by making Angel available.
*
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
*/

package com.tencent.angel.spark.math.vector

import com.tencent.angel.spark.client.PSClient
import com.tencent.angel.spark.context.{AngelPSContext, PSContext}
import com.tencent.angel.spark.{PSFunSuite, SharedPSContext}

class PSVectorSuite extends PSFunSuite with SharedPSContext {

private val dim = 10
private val capacity = 10
private var _psContext: PSContext = _
private var _psVector: PSVector = _

override def beforeAll(): Unit = {
super.beforeAll()
_psContext = PSContext.instance()
_psVector = PSVector.dense(dim, capacity)
}

override def afterAll(): Unit = {
_psContext.destroyVectorPool(_psVector)
super.afterAll()
}

test("toRemote") {
val remoteVector = _psVector.toRemote

assert(PSClient.instance().pull(_psVector).sameElements(remoteVector.pull()))
}

test("toBreeze") {
val brzVector = _psVector.toBreeze

assert(PSClient.instance().pull(_psVector).sameElements(brzVector.toRemote.pull()))
}

test("delete") {
val pool = PSContext.instance().asInstanceOf[AngelPSContext]
.getPool(_psVector.poolId)

val poolSize = pool.size

val ps = PSVector.duplicate(_psVector)

assert(pool.size == poolSize + 1)

ps.delete()

assert(pool.size == poolSize)
}

test("duplicate Vector") {
val dVector = PSVector.duplicate(_psVector)
assert(dVector.poolId == _psVector.poolId)
assert(dVector.id != _psVector.id)

}

test("new dense vector") {
val dVector = PSVector.dense(dim, capacity)
assert(dVector.poolId != _psVector.poolId)
}

test("new sparse vector") {
val sVector = PSVector.sparse(dim, capacity)
assert(sVector.isInstanceOf[SparsePSVector])
}


}
Loading

0 comments on commit c54fafa

Please sign in to comment.