func()

in vermeer/algorithms/lpa.go [155:250]


func (lw *LpaWorker) Compute(vertexId uint32, pId int) {
	_ = pId
	vertexIdx := vertexId - lw.WContext.GraphData.VertIDStart

	//count key:自增label value:frequency
	count := make(map[serialize.SUint32]float64)
	//if lw.WContext.GraphData.BothEdges != nil {
	//	for _, node := range lw.WContext.GraphData.BothEdges[vertexIdx] {
	//		count[lw.oldLabels[node]]++
	//	}
	//} else {
	for _, inNode := range lw.WContext.GraphData.Edges.GetInEdges(vertexIdx) {
		if lw.updateMethod == 1 && lw.inSelfWorker(uint32(inNode)) {
			count[lw.newLabels[inNode]]++
		} else {
			count[lw.oldLabels[inNode]]++
		}
	}
	for _, outNode := range lw.WContext.GraphData.Edges.GetOutEdges(vertexIdx) {
		if lw.updateMethod == 1 && lw.inSelfWorker(uint32(outNode)) {
			count[lw.newLabels[outNode]]++
		} else {
			count[lw.oldLabels[outNode]]++
		}
	}
	//}
	if lw.useProperty {
		for node := range count {
			switch lw.propertyType {
			case structure.ValueTypeFloat32:
				count[node] *= float64(lw.WContext.GraphData.VertexProperty.GetFloat32Value(lw.vertexProperty, vertexId))
			case structure.ValueTypeInt32:
				count[node] *= float64(lw.WContext.GraphData.VertexProperty.GetInt32Value(lw.vertexProperty, vertexId))
			}
		}
	}

	//得到maxCount
	maxCount := float64(0)
	for _, labelCount := range count {
		if maxCount < labelCount {
			maxCount = labelCount
		}
	}

	//得到满足maxCount的所有自增label
	candidate := make([]serialize.SUint32, 0)
	for originLabel, labelCount := range count {
		if labelCount == maxCount {
			candidate = append(candidate, originLabel)
		}
	}
	if len(candidate) == 0 {
		return
	}

	// // 如果原label在候选中,则不更新
	// for _, can := range candidate {
	// 	if can == lw.oldLabels[vertexId] {
	// 		lw.newLabels[vertexId] = can
	// 		return
	// 	}
	// }

	// 比较originLabel
	if lw.compareOption == 0 {
		minLabel := candidate[0]
		for _, candiLabel := range candidate {
			originLabel := candiLabel
			if originLabel < minLabel {
				minLabel = originLabel
			}
		}
		lw.newLabels[vertexId] = minLabel
	} else if lw.compareOption == 1 {
		minLabel := lw.WContext.GraphData.Vertex.GetVertex(uint32(candidate[0])).ID
		for _, candiLabel := range candidate {
			originLabel := lw.WContext.GraphData.Vertex.GetVertex(uint32(candiLabel)).ID
			if strings.Compare(originLabel, minLabel) < 0 {
				minLabel = originLabel
			}
		}
		vertexIndex, ok := lw.WContext.GraphData.Vertex.GetVertexIndex(minLabel)
		if !ok {
			logrus.Errorf("unknown vertex %v", minLabel)
		}
		lw.newLabels[vertexId] = serialize.SUint32(vertexIndex)
	}
	// }
	if lw.newLabels[vertexId] != lw.oldLabels[vertexId] {
		lw.diffSum[pId]++
	}
	if lw.newLabels[vertexId] != lw.grandpaLabels[vertexId] {
		lw.grandpaDiffs[pId]++
	}
}