private SparseTwinIndex <float> CreateRelativeDifference <T>(SparseTwinIndex <float> runData, SparseTwinIndex <float> baseData, SparseArray <T> refernceArray, Func <IZone, int> getAgg) { var ret = refernceArray.CreateSquareTwinArray <float>(); var truth = refernceArray.CreateSquareTwinArray <float>(); var zones = Root.ZoneSystem.ZoneArray.GetFlatData(); var flatRun = runData.GetFlatData(); var flatBaseData = baseData.GetFlatData(); for (int i = 0; i < flatRun.Length; i++) { var oAgg = getAgg(zones[i]); for (int j = 0; j < flatRun[i].Length; j++) { var jAgg = getAgg(zones[j]); ret[oAgg, jAgg] += flatRun[i][j]; truth[oAgg, jAgg] += flatBaseData[i][j]; } } var factor = Sum(truth) / Sum(ret); var flatRetData = ret.GetFlatData(); var flatTruthData = truth.GetFlatData(); for (int i = 0; i < flatRetData.Length; i++) { for (int j = 0; j < flatRetData[i].Length; j++) { flatRetData[i][j] = (flatRetData[i][j] * factor) / flatTruthData[i][j]; } } return(ret); }
public SparseTwinIndex <float> ProcessFlow(SparseArray <float> O, SparseArray <float> D, int[] validIndexes, SparseArray <float> attractionStar = null) { int length = validIndexes.Length; Productions = O; Attractions = D; if (attractionStar == null) { AttractionsStar = D.CreateSimilarArray <float>(); } else { AttractionsStar = attractionStar; } FlowMatrix = Productions.CreateSquareTwinArray <float>(); if (Friction == null) { InitializeFriction(length); } var flatAttractionStar = AttractionsStar.GetFlatData(); float[] oldTotal = new float[flatAttractionStar.Length]; var flatAttractions = Attractions.GetFlatData(); for (int i = 0; i < length; i++) { flatAttractionStar[i] = 1f; oldTotal[i] = flatAttractions[i]; } int iteration = 0; float[] columnTotals = new float[length]; var balanced = false; do { if (ProgressCallback != null) { // this doesn't go to 100%, but that is alright since when we end, the progress // of the calling model should assume we hit 100% ProgressCallback(iteration / (float)MaxIterations); } Array.Clear(columnTotals, 0, columnTotals.Length); if (Vector.IsHardwareAccelerated) { VectorProcessFlow(columnTotals, FlowMatrix.GetFlatData()); } else { ProcessFlow(columnTotals); } balanced = Balance(columnTotals, oldTotal); } while((++iteration) < MaxIterations && !balanced); if (ProgressCallback != null) { ProgressCallback(1f); } return(FlowMatrix); }
public void IterationStarting(int iteration, int totalIterations) { Iteration = iteration; lock (this) { ZoneSystem = Root.ZoneSystem.ZoneArray; if (RecordedData == null) { RecordedData = ZoneSystem.CreateSquareTwinArray <float>().GetFlatData(); } else { for (int i = 0; i < RecordedData.Length; i++) { Array.Clear(RecordedData[i], 0, RecordedData[i].Length); } } for (int i = 0; i < AnalyzeModes.Length; i++) { ValidModeNames.Add(AnalyzeModes[i].ModeName); } Activities = ActivitiesToCapture.Select(a => a.Activity).ToArray(); } }
public static SparseTwinIndex <float> Process(SparseArray <float> production, float[] friction) { var ret = production.CreateSquareTwinArray <float>(); var flatRet = ret.GetFlatData(); var flatProduction = production.GetFlatData(); var numberOfZones = flatProduction.Length; try { // Make all of the frictions to the power of E Parallel.For(0, friction.Length, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(int i) { friction[i] = (float)Math.Exp(friction[i]); }); Parallel.For(0, numberOfZones, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(int i) { float sum = 0f; var iIndex = i * numberOfZones; // gather the sum of the friction for (int j = 0; j < numberOfZones; j++) { sum += friction[iIndex + j]; } if (sum <= 0) { return; } sum = 1f / sum; for (int j = 0; j < numberOfZones; j++) { flatRet[i][j] = flatProduction[i] * (friction[iIndex + j] * sum); } }); } catch (AggregateException e) { if (e.InnerException is XTMFRuntimeException) { throw new XTMFRuntimeException(e.InnerException.Message); } else { throw new XTMFRuntimeException(e.InnerException.Message + "\r\n" + e.InnerException.StackTrace); } } return(ret); }
public static SparseTwinIndex<float> Process(SparseArray<float> production, float[] friction) { var ret = production.CreateSquareTwinArray<float>(); var flatRet = ret.GetFlatData(); var flatProduction = production.GetFlatData(); var numberOfZones = flatProduction.Length; try { // Make all of the frictions to the power of E Parallel.For( 0, friction.Length, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(int i) { friction[i] = (float)Math.Exp( friction[i] ); } ); Parallel.For( 0, numberOfZones, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(int i) { float sum = 0f; var iIndex = i * numberOfZones; // gather the sum of the friction for ( int j = 0; j < numberOfZones; j++ ) { sum += friction[iIndex + j]; } if ( sum <= 0 ) { return; } sum = 1f / sum; for ( int j = 0; j < numberOfZones; j++ ) { flatRet[i][j] = flatProduction[i] * ( friction[iIndex + j] * sum ); } } ); } catch ( AggregateException e ) { if ( e.InnerException is XTMFRuntimeException ) { throw new XTMFRuntimeException( e.InnerException.Message ); } else { throw new XTMFRuntimeException( e.InnerException.Message + "\r\n" + e.InnerException.StackTrace ); } } return ret; }
public void IterationStarting(int iteration) { ZoneSystem = Root.ZoneSystem.ZoneArray; if (Data == null) { Data = ZoneSystem.CreateSquareTwinArray <float>().GetFlatData(); } else { for (int i = 0; i < Data.Length; i++) { Array.Clear(Data[i], 0, Data[i].Length); } } Activities = ActivitiesToCapture.Select(a => a.Activity).ToArray(); }
private void InitializeFriction(int length) { Friction = Productions.CreateSquareTwinArray <float>(); var flatFriction = Friction.GetFlatData(); Parallel.For(0, length, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(int i) { for (int j = 0; j < length; j++) { flatFriction[i][j] = (float)FrictionFunction(Productions.GetSparseIndex(i), Attractions.GetSparseIndex(j)); } }); }
public SparseTwinIndex <float> ProcessFlow(float[] Friction, SparseArray <float> O, SparseArray <float> D) { float[] o = O.GetFlatData(); float[] d = D.GetFlatData(); var oLength = o.Length; var dLength = d.Length; var squareSize = oLength * dLength; Stopwatch watch = new Stopwatch(); watch.Start(); gravityModelShader.NumberOfXThreads = length; gravityModelShader.NumberOfYThreads = 1; gravityModelShader.ThreadGroupSizeX = 64; gravityModelShader.ThreadGroupSizeY = 1; float[] balanced = new float[] { 0, this.Epsilon }; int iterations = 0; var step1 = new int[] { oLength, 0, this.MaxIterations }; var step2 = new int[] { oLength, 1, this.MaxIterations }; if (flows == null || flows.Length != o.Length * d.Length) { flows = new float[squareSize]; } SparseTwinIndex <float> ret = null; Task createReturn = new Task(delegate() { ret = O.CreateSquareTwinArray <float>(); }); createReturn.Start(); FillAndLoadBuffers(o, d, Friction, balanced); iterations = Balance(gpu, gravityModelShader, balancedBuffer, parameters, balanced, iterations, step1, step2); gpu.Read(flowsBuffer, flows); gravityModelShader.RemoveAllBuffers(); createReturn.Wait(); BuildDistribution(ret, O, D, oLength, flows); watch.Stop(); using (StreamWriter writer = new StreamWriter("GPUPerf.txt", true)) { writer.Write("Iterations:"); writer.WriteLine(iterations); writer.Write("Time(ms):"); writer.WriteLine(watch.ElapsedMilliseconds); } return(ret); }
private void ComputeDistances(SparseArray <IZone> zoneSparseArray) { var distances = zoneSparseArray.CreateSquareTwinArray <float>(); var flatDistnaces = distances.GetFlatData(); var zones = zoneSparseArray.GetFlatData(); var length = zones.Length; // build all of the distances in parallel Parallel.For(0, length, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(int i) { for (int j = 0; j < length; j++) { flatDistnaces[i][j] = (i == j) ? zones[i].InternalDistance : CalcDistance(zones[i], zones[j]); } }); Distances = distances; }
public void IterationStarting(int iteration) { ZoneSystem = Root.ZoneSystem.ZoneArray; if(Data == null) { Data = ZoneSystem.CreateSquareTwinArray<float>().GetFlatData(); } else { for(int i = 0; i < Data.Length; i++) { Array.Clear(Data[i], 0, Data[i].Length); } } Activities = ActivitiesToCapture.Select(a => a.Activity).ToArray(); }
public SparseTwinIndex<float> ProcessFlow(float[] Friction, SparseArray<float> O, SparseArray<float> D) { float[] o = O.GetFlatData(); float[] d = D.GetFlatData(); var oLength = o.Length; var dLength = d.Length; var squareSize = oLength * dLength; Stopwatch watch = new Stopwatch(); watch.Start(); gravityModelShader.NumberOfXThreads = length; gravityModelShader.NumberOfYThreads = 1; gravityModelShader.ThreadGroupSizeX = 64; gravityModelShader.ThreadGroupSizeY = 1; float[] balanced = new float[] { 0, this.Epsilon }; int iterations = 0; var step1 = new int[] { oLength, 0, this.MaxIterations }; var step2 = new int[] { oLength, 1, this.MaxIterations }; if ( flows == null || flows.Length != o.Length * d.Length ) { flows = new float[squareSize]; } SparseTwinIndex<float> ret = null; Task createReturn = new Task( delegate() { ret = O.CreateSquareTwinArray<float>(); } ); createReturn.Start(); FillAndLoadBuffers( o, d, Friction, balanced ); iterations = Balance( gpu, gravityModelShader, balancedBuffer, parameters, balanced, iterations, step1, step2 ); gpu.Read( flowsBuffer, flows ); gravityModelShader.RemoveAllBuffers(); createReturn.Wait(); BuildDistribution( ret, O, D, oLength, flows ); watch.Stop(); using ( StreamWriter writer = new StreamWriter( "GPUPerf.txt", true ) ) { writer.Write( "Iterations:" ); writer.WriteLine( iterations ); writer.Write( "Time(ms):" ); writer.WriteLine( watch.ElapsedMilliseconds ); } return ret; }
public SparseTwinIndex<float> ProcessFlow(SparseArray<float> O, SparseArray<float> D, int[] validIndexes, SparseArray<float> attractionStar = null) { int length = validIndexes.Length; Productions = O; Attractions = D; if(attractionStar == null) { AttractionsStar = D.CreateSimilarArray<float>(); } else { AttractionsStar = attractionStar; } FlowMatrix = Productions.CreateSquareTwinArray<float>(); if(Friction == null) { InitializeFriction(length); } var flatAttractionStar = AttractionsStar.GetFlatData(); float[] oldTotal = new float[flatAttractionStar.Length]; var flatAttractions = Attractions.GetFlatData(); for(int i = 0; i < length; i++) { flatAttractionStar[i] = 1f; oldTotal[i] = flatAttractions[i]; } int iteration = 0; float[] columnTotals = new float[length]; var balanced = false; do { if(ProgressCallback != null) { // this doesn't go to 100%, but that is alright since when we end, the progress // of the calling model should assume we hit 100% ProgressCallback(iteration / (float)MaxIterations); } Array.Clear(columnTotals, 0, columnTotals.Length); if(Vector.IsHardwareAccelerated) { VectorProcessFlow(columnTotals, FlowMatrix.GetFlatData()); } else { ProcessFlow(columnTotals); } balanced = Balance(columnTotals, oldTotal); } while((++iteration) < MaxIterations && !balanced); if(ProgressCallback != null) { ProgressCallback(1f); } return FlowMatrix; }
private void ComputeDistances(SparseArray<IZone> zoneSparseArray) { var distances = zoneSparseArray.CreateSquareTwinArray<float>(); var flatDistnaces = distances.GetFlatData(); var zones = zoneSparseArray.GetFlatData(); var length = zones.Length; // build all of the distances in parallel Parallel.For( 0, length, new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount }, delegate(int i) { for ( int j = 0; j < length; j++ ) { flatDistnaces[i][j] = ( i == j ) ? zones[i].InternalDistance : CalcDistance( zones[i], zones[j] ); } } ); Distances = distances; }