Пример #1
0
        private static void VectorApply(float[] ret, float[] categoriesByOrigin, float[] friction, float[] dStar, float[] columnTotals, int categories)
        {
            var numberOfZones = columnTotals.Length;

            Parallel.For(0, columnTotals.Length, new ParallelOptions()
            {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            },
                         () => new float[columnTotals.Length],
                         (int i, ParallelLoopState state, float[] localTotals) =>
            {
                for (int k = 0; k < categories; k++)
                {
                    var catByOrigin = categoriesByOrigin[i + k * numberOfZones];
                    if (catByOrigin <= 0)
                    {
                        continue;
                    }
                    int index = (k * numberOfZones * numberOfZones) + (i * numberOfZones);
                    var sumAF = VectorHelper.MultiplyAndSum(friction, index, dStar, 0, numberOfZones);
                    if (sumAF <= 0)
                    {
                        continue;
                    }
                    VectorHelper.Multiply2Scalar1AndColumnSum(ret, index, friction, index, dStar, 0, catByOrigin / sumAF, localTotals, 0, numberOfZones);
                }
                return(localTotals);
            },
                         (float[] localTotals) =>
            {
                lock (columnTotals)
                {
                    VectorHelper.Add(columnTotals, 0, columnTotals, 0, localTotals, 0, columnTotals.Length);
                }
            });
        }
Пример #2
0
 private void VectorProcessFlow(float[] columnTotals, float[][] flatFlows)
 {
     Parallel.For(0, Productions.GetFlatData().Length, new ParallelOptions()
     {
         MaxDegreeOfParallelism = Environment.ProcessorCount
     },
                  () => new float[columnTotals.Length],
                  (int flatOrigin, ParallelLoopState state, float[] localTotals) =>
     {
         var flatProductions = Productions.GetFlatData();
         // check to see if there is no production, if not skip this
         if (flatProductions[flatOrigin] > 0)
         {
             var flatFriction    = Friction.GetFlatData();
             var flatAStar       = AttractionsStar.GetFlatData();
             var flatAttractions = Attractions.GetFlatData();
             var flatFrictionRow = flatFriction[flatOrigin];
             var sumAF           = VectorHelper.Multiply3AndSum(flatFrictionRow, 0, flatAttractions, 0, flatAStar, 0, flatFriction.Length);
             sumAF = (flatProductions[flatOrigin] / sumAF);
             if (float.IsInfinity(sumAF) | float.IsNaN(sumAF))
             {
                 // this needs to be 0f, otherwise we will be making the attractions have to be balanced higher
                 sumAF = 0f;
             }
             VectorHelper.Multiply3Scalar1AndColumnSum(flatFlows[flatOrigin], 0, flatFrictionRow, 0, flatAttractions, 0, flatAStar, 0, sumAF, localTotals, 0, flatFriction.Length);
         }
         return(localTotals);
     },
                  (float[] localTotals) =>
     {
         lock (columnTotals)
         {
             VectorHelper.Add(columnTotals, 0, columnTotals, 0, localTotals, 0, columnTotals.Length);
         }
     });
 }