Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 46 additions & 64 deletions vermeer/algorithms/clustering_coeffcient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package algorithms
import (
"vermeer/apps/common"
"vermeer/apps/compute"
"vermeer/apps/options"
"vermeer/apps/serialize"
)

Expand Down Expand Up @@ -49,86 +50,67 @@ func (cc *ClusteringCoefficientMaker) DataNeeded() []string {

type ClusteringCoefficientWorker struct {
compute.WorkerComputerBase
ccValues []serialize.SFloat32
totalNeighborSet []serialize.MapUint32Struct
selfNeighborSet []map[serialize.SUint32]struct{}
ccValues []serialize.SFloat32
degrees []serialize.SUint32
orientedNeighbors []serialize.SliceUint32
localNeighbors []serialize.SliceUint32
}

func (ccw *ClusteringCoefficientWorker) VertexValue(i uint32) serialize.MarshalAble {
if ccw.WContext.Step == 1 {
return &ccw.totalNeighborSet[i]
func (ccw *ClusteringCoefficientWorker) VertexValue(vertexID uint32) serialize.MarshalAble {
if ccw.WContext.Output {
return &ccw.ccValues[vertexID]
}
switch ccw.WContext.Step {
case 1:
return &ccw.degrees[vertexID]
case 2:
return &ccw.orientedNeighbors[vertexID]
default:
return &noTriangleScatterValue
}
return &ccw.ccValues[i]
}

func (ccw *ClusteringCoefficientWorker) Init() error {
ccw.ccValues = make([]serialize.SFloat32, ccw.WContext.GraphData.Vertex.TotalVertexCount())
ccw.totalNeighborSet = make([]serialize.MapUint32Struct, ccw.WContext.GraphData.Vertex.TotalVertexCount())
ccw.selfNeighborSet = make([]map[serialize.SUint32]struct{}, ccw.WContext.GraphData.VertexCount)
if err := validateTriangleMaxStep(options.GetInt(ccw.WContext.Params,
"compute.max_step")); err != nil {
return err
}
totalVertices := ccw.WContext.GraphData.Vertex.TotalVertexCount()
localVertices := ccw.WContext.GraphData.VertexCount
ccw.ccValues = make([]serialize.SFloat32, totalVertices)
ccw.degrees = make([]serialize.SUint32, totalVertices)
ccw.orientedNeighbors = make([]serialize.SliceUint32, totalVertices)
ccw.localNeighbors = make([]serialize.SliceUint32, localVertices)
return nil
}

func (ccw *ClusteringCoefficientWorker) Compute(vertexID uint32, pID int) {
_ = pID
vertID := vertexID - ccw.WContext.GraphData.VertIDStart
func (ccw *ClusteringCoefficientWorker) Compute(vertexID uint32, _ int) {
localID := vertexID - ccw.WContext.GraphData.VertIDStart
switch ccw.WContext.Step {
case 1:
inEdges := ccw.WContext.GraphData.Edges.GetInEdges(vertID)
outEdges := ccw.WContext.GraphData.Edges.GetOutEdges(vertID)
ccw.selfNeighborSet[vertID] = make(map[serialize.SUint32]struct{},
len(inEdges)+len(outEdges))
ccw.totalNeighborSet[vertexID] = make(map[serialize.SUint32]struct{},
(len(inEdges)+len(outEdges))/2)
for _, nID := range inEdges {
if uint32(nID) == vertexID {
continue
}
ccw.selfNeighborSet[vertID][nID] = struct{}{}
if uint32(nID) < vertexID {
ccw.totalNeighborSet[vertexID][nID] = struct{}{}
}
}
for _, nID := range outEdges {
if uint32(nID) == vertexID {
continue
}
ccw.selfNeighborSet[vertID][nID] = struct{}{}
if uint32(nID) < vertexID {
ccw.totalNeighborSet[vertexID][nID] = struct{}{}
}
}
neighbors := sortedUndirectedNeighbors(vertexID,
uint32(len(ccw.degrees)),
ccw.WContext.GraphData.Edges.GetInEdges(localID),
ccw.WContext.GraphData.Edges.GetOutEdges(localID))
ccw.localNeighbors[localID] = neighbors
ccw.degrees[vertexID] = serialize.SUint32(len(neighbors))
case 2:
length := len(ccw.selfNeighborSet[vertID])
if length > 1 {
var triangleCount serialize.SUint32
for fID := range ccw.selfNeighborSet[vertID] {
triangleCount += ccw.findCross(ccw.selfNeighborSet[vertID], ccw.totalNeighborSet[fID])
}
ccw.ccValues[vertexID] = 2 * serialize.SFloat32(triangleCount) /
serialize.SFloat32((length)*(length-1))
} else {
ccw.orientedNeighbors[vertexID] = orientedNeighbors(vertexID,
ccw.localNeighbors[localID], ccw.degrees)
case 3:
degree := len(ccw.localNeighbors[localID])
if degree < 2 {
ccw.ccValues[vertexID] = 0
return
}
triangles := countVertexTriangles(ccw.localNeighbors[localID],
ccw.orientedNeighbors)
denominator := float64(degree) * float64(degree-1)
ccw.ccValues[vertexID] = serialize.SFloat32(
2 * float64(triangles) / denominator)
}
}

func (ccw *ClusteringCoefficientWorker) findCross(a map[serialize.SUint32]struct{}, b map[serialize.SUint32]struct{}) serialize.SUint32 {
var count serialize.SUint32 = 0
if len(a) > len(b) {
for k := range b {
if _, ok := a[k]; ok {
count++
}
}
} else {
for k := range a {
if _, ok := b[k]; ok {
count++
}
}
}
return count
}
func (ccw *ClusteringCoefficientWorker) OutputValueType() string {
return common.HgValueTypeFloat
}
Expand All @@ -138,5 +120,5 @@ type ClusteringCoefficientMaster struct {
}

func (ccm *ClusteringCoefficientMaster) Compute() bool {
return ccm.MContext.Step != 2
return ccm.MContext.Step != triangleComputeSteps
}
119 changes: 44 additions & 75 deletions vermeer/algorithms/triangle_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package algorithms
import (
"vermeer/apps/common"
"vermeer/apps/compute"
"vermeer/apps/options"
"vermeer/apps/serialize"
)

Expand Down Expand Up @@ -49,102 +50,70 @@ func (tc *TriangleCountMaker) DataNeeded() []string {

type TriangleCountWorker struct {
compute.WorkerComputerBase
triangleCount []serialize.SUint32
totalNeighborSet []serialize.MapUint32Struct
selfNeighborSet []map[serialize.SUint32]struct{}
triangleCount []serialize.SInt64
degrees []serialize.SUint32
orientedNeighbors []serialize.SliceUint32
localNeighbors []serialize.SliceUint32
}

func (tcw *TriangleCountWorker) VertexValue(i uint32) serialize.MarshalAble {
if tcw.WContext.Step == 1 {
return &tcw.totalNeighborSet[i]
func (tcw *TriangleCountWorker) VertexValue(vertexID uint32) serialize.MarshalAble {
if tcw.WContext.Output {
return &tcw.triangleCount[vertexID]
}
switch tcw.WContext.Step {
case 1:
return &tcw.degrees[vertexID]
case 2:
return &tcw.orientedNeighbors[vertexID]
default:
return &noTriangleScatterValue
}
return &tcw.triangleCount[i]
}

func (tcw *TriangleCountWorker) Init() error {
tcw.triangleCount = make([]serialize.SUint32, tcw.WContext.GraphData.Vertex.TotalVertexCount())
tcw.totalNeighborSet = make([]serialize.MapUint32Struct, tcw.WContext.GraphData.Vertex.TotalVertexCount())
tcw.selfNeighborSet = make([]map[serialize.SUint32]struct{}, tcw.WContext.GraphData.VertexCount)
if err := validateTriangleMaxStep(options.GetInt(tcw.WContext.Params,
"compute.max_step")); err != nil {
return err
}
totalVertices := tcw.WContext.GraphData.Vertex.TotalVertexCount()
localVertices := tcw.WContext.GraphData.VertexCount
tcw.triangleCount = make([]serialize.SInt64, totalVertices)
tcw.degrees = make([]serialize.SUint32, totalVertices)
tcw.orientedNeighbors = make([]serialize.SliceUint32, totalVertices)
tcw.localNeighbors = make([]serialize.SliceUint32, localVertices)
return nil
}

func (tcw *TriangleCountWorker) Compute(vertexID uint32, pID int) {
_ = pID
vertID := vertexID - tcw.WContext.GraphData.VertIDStart
func (tcw *TriangleCountWorker) Compute(vertexID uint32, _ int) {
localID := vertexID - tcw.WContext.GraphData.VertIDStart
switch tcw.WContext.Step {
case 1:
//if tcw.WContext.GraphData.BothEdges != nil {
// tcw.selfNeighborSet[vertID] = make(map[serialize.SUint32]struct{},
// len(tcw.WContext.GraphData.BothEdges[vertID]))
// tcw.totalNeighborSet[vertexID] = make(map[serialize.SUint32]struct{},
// len(tcw.WContext.GraphData.BothEdges[vertID])/2)
// for _, nID := range tcw.WContext.GraphData.BothEdges[vertID] {
// if uint32(nID) == vertexID {
// continue
// }
// tcw.selfNeighborSet[vertID][nID] = struct{}{}
// if uint32(nID) < vertexID {
// tcw.totalNeighborSet[vertexID][nID] = struct{}{}
// }
// }
//} else {
inEdges := tcw.WContext.GraphData.Edges.GetInEdges(vertID)
outEdges := tcw.WContext.GraphData.Edges.GetOutEdges(vertID)
tcw.selfNeighborSet[vertID] = make(map[serialize.SUint32]struct{},
len(inEdges)+len(outEdges))
tcw.totalNeighborSet[vertexID] = make(map[serialize.SUint32]struct{},
(len(inEdges)+len(outEdges))/2)
for _, nID := range inEdges {
if uint32(nID) == vertexID {
continue
}
tcw.selfNeighborSet[vertID][nID] = struct{}{}
if uint32(nID) < vertexID {
tcw.totalNeighborSet[vertexID][nID] = struct{}{}
}
}
for _, nID := range outEdges {
if uint32(nID) == vertexID {
continue
}
tcw.selfNeighborSet[vertID][nID] = struct{}{}
if uint32(nID) < vertexID {
tcw.totalNeighborSet[vertexID][nID] = struct{}{}
}
}
//}
neighbors := sortedUndirectedNeighbors(vertexID,
uint32(len(tcw.degrees)),
tcw.WContext.GraphData.Edges.GetInEdges(localID),
tcw.WContext.GraphData.Edges.GetOutEdges(localID))
tcw.localNeighbors[localID] = neighbors
tcw.degrees[vertexID] = serialize.SUint32(len(neighbors))
case 2:
for fID := range tcw.selfNeighborSet[vertID] {
tcw.triangleCount[vertexID] += tcw.findCross(tcw.selfNeighborSet[vertID], tcw.totalNeighborSet[fID])
}
tcw.orientedNeighbors[vertexID] = orientedNeighbors(vertexID,
tcw.localNeighbors[localID], tcw.degrees)
case 3:
// The largest simple graph addressable by uint32 vertex IDs has fewer
// than MaxInt64 triangles incident to one vertex, so this cast is safe.
tcw.triangleCount[vertexID] = serialize.SInt64(
countVertexTriangles(tcw.localNeighbors[localID],
tcw.orientedNeighbors))
}
}

func (tcw *TriangleCountWorker) findCross(a map[serialize.SUint32]struct{}, b map[serialize.SUint32]struct{}) serialize.SUint32 {
var count serialize.SUint32 = 0
if len(a) > len(b) {
for k := range b {
if _, ok := a[k]; ok {
count++
}
}
} else {
for k := range a {
if _, ok := b[k]; ok {
count++
}
}
}
return count
}
func (tcw *TriangleCountWorker) OutputValueType() string {
return common.HgValueTypeInt
return common.HgValueTypeLong
}

type TriangleCountMaster struct {
compute.MasterComputerBase
}

func (tcm *TriangleCountMaster) Compute() bool {
return tcm.MContext.Step != 2
return tcm.MContext.Step != triangleComputeSteps
}
Loading
Loading