1

I am new to parallel programming. I am using Parallel.Invoke() to execute 6 functions that are independent and can run parallel to each other.

I am trying to monitor the CPU and memory Utilization for my code looking at task manager. I have Intel CORE i3 system and 4GB RAM.

What I notice is maximum CPU Usage goes to 30-40% and even Memory usage is 30% at max.

How can I utilize more CPU and memory? I am not running any other app. How Parallel.Invoke() is handled by TPL and how much usage will increase for more available cores and RAM.?

My Code sample:

  int concurrencyLevel = Environment.ProcessorCount*2;
        ParallelOptions p=new ParallelOptions{MaxDegreeOfParallelism=concurrencyLevel};
        Parallel.Invoke(p,() =>
        {
            if (elementForm.ImplementationcheckBox.Checked)
            {                   
              ImplementationDataType();
            }
        }, () =>
        {
            if (elementForm.PrimitivecheckBox.Checked)
            {

                ApplicationPrimitiveDataType();//No need to add in diagram.

            }
            //Empty diagram
        }, () =>
        {
            if (elementForm.ArraycheckBox.Checked)
            {
                 ApplicationArrayDataType();//No need to add in diagram.
                                }
        }, () =>
        {
            if (elementForm.RecordcheckBox.Checked)
            {
                                   ApplicationRecordDataType();
            }
        }, () =>
        {
            if (elementForm.CompositioncheckBox.Checked)
            {
                SenderReciverInterface();

                ApplicationSWComponentType();

                CompositionSwComponentType();

            }
        }, () =>
        {

            if (elementForm.MappingcheckBox.Checked)
            {
                DataTypeMappingSet();

            }
        }); 

where each function is as below:

 private void DataTypeMappingSet()
    {
        List<int> indexes = getPackageAndElementsIndex("DATA-TYPE-MAPPING-SET");
        if (indexes != null)
        {
            try
            {
                    ElementTag elementTag = null;
                    int elementId = 0;
                    string elementName = null;
                    String elementNameAndId = null;
                    List<int> elementChildElements = null;
                    ElementTag ParentDiagramtag = null;
                    string parentDiagramName = null;
                    ElementTag parentPackagetag = null;
                    string parentPackageName = null;

                    EA.Package parentPackage;
                    EA.Diagram parentDiagram = null; ;
                    EA.Element DataTypeMappingSetElement;
                foreach (int index in indexes)
                   {
                    elementTag = parsedFile.store_element[index];
                    elementId = elementTag.Element_id;
                    elementName = parsedFile.duplicate_element_array     [elementTag.Tag_array_ref].Key;
                    elementChildElements = elementTag.Child_index;
                    ParentDiagramtag = parsedFile.store_element[elementTag.Parent_index];
                    parentDiagramName = parsedFile.duplicate_element_array[ParentDiagramtag.Tag_array_ref].Key;
                    parentDiagramName = parentDiagramName + ParentDiagramtag.Element_id;


                    parentPackagetag = parsedFile.store_element[ParentDiagramtag.Parent_index];
                    parentPackageName = parsedFile.duplicate_element_array[parentPackagetag.Tag_array_ref].Key;
                    parentPackageName = parentPackageName + parentPackagetag.Element_id;

                    parentPackage = getPackageObjByName[parentPackageName];
                    parentDiagram = getDiagramObjByName[parentDiagramName];
                    DataTypeMappingSetElement = (EA.Element)parentPackage.Elements.AddNew("", "Data-Type-Mapping-Set");
                    DataTypeMappingSetElement.Update();

                    elementNameAndId = elementName + elementId;
                    getElementObjByName.TryAdd(elementNameAndId, DataTypeMappingSetElement);

                    AddTagValues(DataTypeMappingSetElement, elementChildElements);

                    //EA.DiagramObject 
DiagOb = (EA.DiagramObject)parentDiagram.DiagramObjects.AddNew("", "");
                    //DiagOb.ElementID = DataTypeMappingSetElement.ElementID;
                    //DiagOb.Update();

                    createDataTypeMap(elementTag.Child_index, DataTypeMappingSetElement, parentDiagramName, parentPackage);
                });
            }
            catch (Exception)
            {

                // throw;
            }
        }
    }
Akanksha
  • 135
  • 1
  • 9
  • What do you do with the results of your functions? Does your processing involve any I/O that would induce waiting? – Jeroen Mostert Sep 19 '16 at 09:46
  • Functions create elements and diagrams for the object present in concurrent lists and dictionaries, which are then inserted to Enterprise Architect Repository. – Akanksha Sep 19 '16 at 10:18
  • That sounds like there *may* be I/O involved, which would limit scalability, but I'm still not quite clear. In any case, you can use the [overload of `Parallel.Invoke` that includes options](https://msdn.microsoft.com/library/dd783942) to set the `MaxDegreeOfParallelism` higher. This may not result in better performance -- beyond a certain point, putting more threads to work just results in more overhead waiting. This can at least result in higher CPU utilization -- how much RAM is used depends entirely on the tasks and isn't explicitly managed by `Parallel.Invoke`. – Jeroen Mostert Sep 19 '16 at 10:33

0 Answers0