Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
spark
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cs525-sp18-g07
spark
Commits
2d666b9d
Commit
2d666b9d
authored
12 years ago
by
Imran Rashid
Committed by
Matei Zaharia
12 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add some functionality to Vector, delete copy in AccumulatorSuite
parent
edc6972f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/src/main/scala/spark/util/Vector.scala
+31
-1
31 additions, 1 deletion
core/src/main/scala/spark/util/Vector.scala
core/src/test/scala/spark/AccumulatorSuite.scala
+2
-112
2 additions, 112 deletions
core/src/test/scala/spark/AccumulatorSuite.scala
with
33 additions
and
113 deletions
core/src/main/scala/spark/util/Vector.scala
+
31
−
1
View file @
2d666b9d
...
@@ -29,7 +29,37 @@ class Vector(val elements: Array[Double]) extends Serializable {
...
@@ -29,7 +29,37 @@ class Vector(val elements: Array[Double]) extends Serializable {
return
ans
return
ans
}
}
def
*
(
scale
:
Double
)
:
Vector
=
Vector
(
length
,
i
=>
this
(
i
)
*
scale
)
/**
* return (this + plus) dot other, but without creating any intermediate storage
* @param plus
* @param other
* @return
*/
def
plusDot
(
plus
:
Vector
,
other
:
Vector
)
:
Double
=
{
if
(
length
!=
other
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
if
(
length
!=
plus
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
var
ans
=
0.0
var
i
=
0
while
(
i
<
length
)
{
ans
+=
(
this
(
i
)
+
plus
(
i
))
*
other
(
i
)
i
+=
1
}
return
ans
}
def
+=
(
other
:
Vector
)
{
if
(
length
!=
other
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
var
ans
=
0.0
var
i
=
0
while
(
i
<
length
)
{
elements
(
i
)
+=
other
(
i
)
i
+=
1
}
}
def
*
(
scale
:
Double
)
:
Vector
=
Vector
(
length
,
i
=>
this
(
i
)
*
scale
)
def
*
(
scale
:
Double
)
:
Vector
=
Vector
(
length
,
i
=>
this
(
i
)
*
scale
)
def
/
(
d
:
Double
)
:
Vector
=
this
*
(
1
/
d
)
def
/
(
d
:
Double
)
:
Vector
=
this
*
(
1
/
d
)
...
...
This diff is collapsed.
Click to expand it.
core/src/test/scala/spark/AccumulatorSuite.scala
+
2
−
112
View file @
2d666b9d
...
@@ -64,8 +64,7 @@ class AccumulatorSuite extends FunSuite with ShouldMatchers {
...
@@ -64,8 +64,7 @@ class AccumulatorSuite extends FunSuite with ShouldMatchers {
test
(
"value readable in tasks"
)
{
test
(
"value readable in tasks"
)
{
import
Vector.VectorAccumParam._
import
spark.util.Vector
import
Vector._
//stochastic gradient descent with weights stored in accumulator -- should be able to read value as we go
//stochastic gradient descent with weights stored in accumulator -- should be able to read value as we go
//really easy data
//really easy data
...
@@ -121,113 +120,4 @@ class AccumulatorSuite extends FunSuite with ShouldMatchers {
...
@@ -121,113 +120,4 @@ class AccumulatorSuite extends FunSuite with ShouldMatchers {
}
}
}
}
}
}
\ No newline at end of file
//ugly copy and paste from examples ...
class
Vector
(
val
elements
:
Array
[
Double
])
extends
Serializable
{
def
length
=
elements
.
length
def
apply
(
index
:
Int
)
=
elements
(
index
)
def
+
(
other
:
Vector
)
:
Vector
=
{
if
(
length
!=
other
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
return
Vector
(
length
,
i
=>
this
(
i
)
+
other
(
i
))
}
def
-
(
other
:
Vector
)
:
Vector
=
{
if
(
length
!=
other
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
return
Vector
(
length
,
i
=>
this
(
i
)
-
other
(
i
))
}
def
dot
(
other
:
Vector
)
:
Double
=
{
if
(
length
!=
other
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
var
ans
=
0.0
var
i
=
0
while
(
i
<
length
)
{
ans
+=
this
(
i
)
*
other
(
i
)
i
+=
1
}
return
ans
}
def
plusDot
(
plus
:
Vector
,
other
:
Vector
)
:
Double
=
{
if
(
length
!=
other
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
if
(
length
!=
plus
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
var
ans
=
0.0
var
i
=
0
while
(
i
<
length
)
{
ans
+=
(
this
(
i
)
+
plus
(
i
))
*
other
(
i
)
i
+=
1
}
return
ans
}
def
+=
(
other
:
Vector
)
{
if
(
length
!=
other
.
length
)
throw
new
IllegalArgumentException
(
"Vectors of different length"
)
var
ans
=
0.0
var
i
=
0
while
(
i
<
length
)
{
elements
(
i
)
+=
other
(
i
)
i
+=
1
}
}
def
*
(
scale
:
Double
)
:
Vector
=
Vector
(
length
,
i
=>
this
(
i
)
*
scale
)
def
/
(
d
:
Double
)
:
Vector
=
this
*
(
1
/
d
)
def
unary_-
=
this
*
-
1
def
sum
=
elements
.
reduceLeft
(
_
+
_
)
def
squaredDist
(
other
:
Vector
)
:
Double
=
{
var
ans
=
0.0
var
i
=
0
while
(
i
<
length
)
{
ans
+=
(
this
(
i
)
-
other
(
i
))
*
(
this
(
i
)
-
other
(
i
))
i
+=
1
}
return
ans
}
def
dist
(
other
:
Vector
)
:
Double
=
math
.
sqrt
(
squaredDist
(
other
))
override
def
toString
=
elements
.
mkString
(
"("
,
", "
,
")"
)
}
object
Vector
{
def
apply
(
elements
:
Array
[
Double
])
=
new
Vector
(
elements
)
def
apply
(
elements
:
Double*
)
=
new
Vector
(
elements
.
toArray
)
def
apply
(
length
:
Int
,
initializer
:
Int
=>
Double
)
:
Vector
=
{
val
elements
=
new
Array
[
Double
](
length
)
for
(
i
<-
0
until
length
)
elements
(
i
)
=
initializer
(
i
)
return
new
Vector
(
elements
)
}
def
zeros
(
length
:
Int
)
=
new
Vector
(
new
Array
[
Double
](
length
))
def
ones
(
length
:
Int
)
=
Vector
(
length
,
_
=>
1
)
class
Multiplier
(
num
:
Double
)
{
def
*
(
vec
:
Vector
)
=
vec
*
num
}
implicit
def
doubleToMultiplier
(
num
:
Double
)
=
new
Multiplier
(
num
)
implicit
object
VectorAccumParam
extends
spark
.
AccumulatorParam
[
Vector
]
{
def
addInPlace
(
t1
:
Vector
,
t2
:
Vector
)
=
t1
+
t2
def
zero
(
initialValue
:
Vector
)
=
Vector
.
zeros
(
initialValue
.
length
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment