简体   繁体   中英

How to convert this code to Task Parallel Library

I'm new to multithread programming in c# .net, I need help to convert this code show bellow to Task parallel in C#.

    private void thisIsMethodA()
    {
        //Vertical database
        Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
        // for each item
        foreach (KeyValuePair<int, Bitmap> entry in verticalDB)
        {
            // We call the depth first search method 
            dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
        }
    }
    private void dfsPruning(Prefix prefix, Bitmap prefixBitmap, List<int> sn, List<int> inl, int hasToBeGreaterThanForIStep, int m)
    {
        int maximumPatternLength = 100;

        for (int k = 0; k < sn.Count; k++)
        {

            if (maximumPatternLength > m)
            {
                dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1);
            }
        }
        for (int k = 0; k < inl.Count; k++)
        {
            if (maximumPatternLength > m)
            {
                dfsPruning(prefixIStep, newBitmap, sTemp, iTemp, item, m + 1);
            }
        }

    }

Should I use code below?

Is it Right to implement using Task?, Can you suggest me what more can I do and how can i improve the efficiency more?

private void thisIsMethodA()
{
    //Vertical database
    Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
    // for each item
    Parallel.ForEach(verticalDB, (entry) =>
        {

            // We call the depth first search method 
            dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
        });
}
private void dfsPruning(Prefix prefix, Bitmap prefixBitmap, List<int> sn, List<int> inl, int hasToBeGreaterThanForIStep, int m)
{
    int maximumPatternLength = 100;
    var tasks = new List<Task>();
    for (int k = 0; k < sn.Count; k++)
    {

        if (maximumPatternLength > m)
        {
            tasks.Add(Task.Factory.StartNew(() => dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1)));
        }
    }
    for (int k = 0; k < inl.Count; k++)
    {
        if (maximumPatternLength > m)
        {
            tasks.Add(Task.Factory.StartNew(() => dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1)));
        }
    }
    Task.WaitAll(tasks.ToArray());

}

So my question is, How to convert this code to Task Parallel Library. So that .net only take care to run this tasks parallel. dfsPruning() method is recursive, I need help to convert this method to task parallel.

Thank you very much for taking the time to read my letter and I look forward to anything I may receive.

One simple thing you can do is to use Parallel.ForEach to parallelize the thisIsMethodA rather than dfsPruning . This will process each entry on a different thread depending on your execution environment,but you need to be careful with data being modified from different threads

private void thisIsMethodA()
{
    //Vertical database
    Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
    // for each item
    Parallel.ForEach (vertocalDB, (entry) => 
    {
        // We call the depth first search method 
        dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
    });
}

This can be a candidate for Parallel.For

As the loop might be very recursive I recommed to set MaxDegreeOfParallelism based on your server configuration, so that there should not be task created and goes on waiting for threads or processor to release.

private void thisIsMethodA()
{
    //Vertical database
    Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
    // for each item
    Parallel.ForEach (vertocalDB, (entry) => 
    {
        // We call the depth first search method 
        dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
    }
}

private void dfsPruning(Prefix prefix, Bitmap prefixBitmap, List<int> sn, List<int> inl, int hasToBeGreaterThanForIStep, int m)
{
    int maximumPatternLength = 100;

    Parallel.For(0,sn.Count,  new ParallelOptions { MaxDegreeOfParallelism = 10 }, (k) =>
    {
        if (maximumPatternLength > m)
        {
            dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1);
        }
    });

    Parallel.For(0,inl.Count, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (k) => 
    {
        if (maximumPatternLength > m)
        {
            dfsPruning(prefixIStep, newBitmap, sTemp, iTemp, item, m + 1);
        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM