Mass addition - Speed up component

I am using Mass Addition to sum up long branches in data trees, which leads to long computation times for the component. I was wondering if there is a way to speedup computation, maybe have the component only compute the mass addition result and skip the “partial results”. Anyone knows how to do that? Thanks!

you can try a Python component with a = sum(x)
x with list access, not casted to any type

mass_pyddition.gh (5.3 KB)

2 Likes

Using the System.Linq Sum function in C# performs about the same as Python, however you can pick up a bit more speed with a compiled task-capable component.

20220924_TaskMassAdd.gh (12.5 KB)

  private void RunScript(List<object> x, ref object A)
  {
    A = x.Sum(list => (double) list);
  }

C# task capable component code
    public class TaskMassAdd : GH_TaskCapableComponent<TaskMassAdd.SolveResultsMassAdd>
    {
        public TaskMassAdd()
          : base("TaskMassAdd", "TMA", "Description", "Heron", "Subcategory")
        {
        }

        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddGenericParameter("Input", "I", "", GH_ParamAccess.list);
        }

        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddNumberParameter("Result", "R", "", GH_ParamAccess.item);
        }

        public class SolveResultsMassAdd
        {
            public GH_Number sumResult { get; set; }
        }

        SolveResultsMassAdd Compute (List<object> numList)
        {
            var rc = new SolveResultsMassAdd();

            ///Rework below for other cases of GH Types which are valid input to the OOTB Mass Addition (ie Point3d, Vector, Transform, etc.)
            double total = 0.0;
            foreach (var n in numList)
            {
                if (n.GetType().Name == "GH_Number") 
                {
                    GH_Number d = (GH_Number) n;
                    total = total + d.Value;
                }
                else if (n.GetType().Name == "GH_Integer")
                {
                    GH_Integer i = (GH_Integer) n;
                    total = total + i.Value;
                }
            }
            rc.sumResult = new GH_Number (total);
            

            return rc;
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
            if (InPreSolve)
            {
                ///First pass; collect data and construct tasks
                List<object> numList = new List<object>();

                Task<SolveResultsMassAdd> tskMassAdd = null;

                if (DA.GetDataList<object>(0, numList))
                {
                    tskMassAdd = Task.Run(() => Compute(numList), CancelToken);
                }

                ///Add a null task even if data collection fails.  This keeps the list size in sync with the iterations
                TaskList.Add(tskMassAdd);
                return;
            }

            if (!GetSolveResults(DA, out var results))
            {
                ///Compute right here, right now.
                ///1. Collect
                List<object> numsList = new List<object>();

                if (!DA.GetDataList<object>(0, numsList)) { return; }

                ///2. Compute
                results = Compute(numsList);
            }

            ///3. Set
            if (results != null)
            {
                DA.SetData(0, results.sumResult);
            }
        }

        protected override System.Drawing.Bitmap Icon
        {
            get
            {
                //You can add image files to your project resources and access them like this:
                // return Resources.IconForThisComponent;
                return null;
            }
        }

        public override Guid ComponentGuid
        {
            get { return new Guid("2783F387-8F94-489C-BAAD-7C2F326C4B3D"); }
        }
    }

-Brian

3 Likes

Thank you! Works like charm!

Thank you! Exactly what I was looking for!