void CreateGraphNodeThreadProc(object par) { NodeCreationThreadObject p = (NodeCreationThreadObject)par; long start = -1; long end = -1; long ele = nodeCount / p.threadCount; if (p.threadCount != p.threadIndex + 1) { start = p.threadIndex * ele + 1; end = start + ele; } else//最后一个 { start = p.threadIndex * ele + 1; end = nodeCount + 1; } int loopTimes = (int)Math.Log(nodeCount, 2); for (long i = start; i < end; i++) { long begin = 1; long finish = nodeCount; long capacity = edgeCount; double up = p1 + p2; double down = p3 + p4; for (int j = 0; j < loopTimes; j++)//如何决定capacity? { if (i >= begin && i <= finish - ((finish - begin + 1) / 2)) { finish = finish - ((finish - begin + 1) / 2); capacity = (long)(capacity * up); } else if (i >= begin + ((finish - begin + 1) / 2) && i <= finish) { begin = begin + ((finish - begin + 1) / 2); capacity = (long)(capacity * down); } } List <long> reservationList = new List <long>((int)capacity); //向末尾添加元素 2*capacity 个 reservationList.AddRange(Enumerable.Range(-(int)(capacity + 1), (int)capacity).Select(x => (long)x)); index = randomArray[p.threadIndex].Next(0, labelSet.Count); //cellId , edgeNumber, label don't match the cellId, GraphNode node = new GraphNode(i, 0, labelSet[index], reservationList); Global.LocalStorage.SaveGraphNode(node); } }
public void CreatGraph() { int threadCount = Environment.ProcessorCount; Thread[] threadNum = new Thread[threadCount]; ////////////////////////////////PHASE 1/////////////////////////////// Console.WriteLine("Generating ID values"); Stopwatch s = new Stopwatch(); s.Start(); TrinityConfig.CurrentRunningMode = RunningMode.Embedded; TrinityConfig.DefragInterval = 2500; for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) { NodeCreationThreadObject p = new NodeCreationThreadObject(threadCount, threadIndex); threadNum[threadIndex] = new Thread(CreateGraphNodeThreadProc); threadNum[threadIndex].Start(p); } for (int inde = 0; inde < threadCount; inde++) { threadNum[inde].Join(); } s.Stop(); Console.WriteLine("add idValue cost time:{0}", s.ElapsedMilliseconds); ////////////////////////////////PHASE 2/////////////////////////////// Console.WriteLine("add edges"); Stopwatch ss = new Stopwatch(); ss.Start(); for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) { EdgeCreationThreadObject p = new EdgeCreationThreadObject(threadCount, threadIndex); threadNum[threadIndex] = new Thread(CreateEdgeThreadProc); threadNum[threadIndex].Start(p); } for (int inde = 0; inde < threadCount; inde++) { threadNum[inde].Join(); } ss.Stop(); Console.WriteLine("add outlinks cost time:{0}", ss.ElapsedMilliseconds); ///////////////Remove dirty outlinks////////////// Stopwatch watch = new Stopwatch(); watch.Start(); for (int threadIndex = 0; threadIndex < threadCount; threadIndex++) { CellCleaningThreadObject p = new CellCleaningThreadObject(threadCount, threadIndex, nodeCount); threadNum[threadIndex] = new Thread(CellCleaningThreadProc); threadNum[threadIndex].Start(p); } for (int inde = 0; inde < threadCount; inde++) { threadNum[inde].Join(); } watch.Stop(); Console.WriteLine(watch.ElapsedMilliseconds); TrinityConfig.DefragInterval = 100; /////////////////////////////////////////////// Stopwatch sss = new Stopwatch(); sss.Start(); Global.LocalStorage.SaveStorage(); sss.Stop(); Console.WriteLine("saveStorage cost time:{0}", sss.ElapsedMilliseconds); //foreach (var node in Global.LocalStorage.GraphNode_Selector()) //{ // Console.WriteLine(node); //} //var results = from node in Global.LocalStorage.GraphNode_Accessor_Selector() // select node; //Console.WriteLine(results.Distinct()); }