Visual Studio 11 Development Guide (19) C ++ 11 Update-parallel mode library and proxy Library

Source: Internet
Author: User
Visual Studio 11, Parallel Mode LibraryAnd Proxy LibraryAnd more easily develop parallel code running on multi-core processors.

The main examples of these libraries are based on the task andConcurrent Runtime Library, custom scheduling program for processing.

 

So far, the concept prototype for processing tasks has been usedTask_handle ●Type:

 

1.task_group task;     2.    int i=0;     3.    auto  t1=make_task([&i]() 4.        { 5.             6.            i=42; 7.        }); 8.    int z=0; 9.    auto t2=make_task([&z]() 10.        { 11.             12.            z=42; 13.        }); 14.  15.    task.run_and_wait (t1);     16.    task.run_and_wait (t2);     17.    std::wcout << i+z << std::endl; 18.    std::wcout << "Fini" << std::endl;

 

 

The calculated I + Z is a required Lambda expression to reference variables. Then these two tasks are done in parallel.

Visual Studio 11 can be used directlyTaskThe concept of type is as follows:

1.task<int> t1=task<int>([]()->int 2.        { 3.            return 42; 4.        }); 5.     6.    task<int> t2=task<int>([]()->int 7.        { 8.            return 42; 9.        }); 10.     11.    std::wcout << t1.get() +t2.get () << std::endl;

 

To simplify the syntax, capture variable references for each task.Wait ()For each task and return a roleGet (), get the status for processing.

Asynchronous tasks of the task class follow the set running rules without waiting.TN depends on Tn-1 taskAnd the previous task has been completed or not.

Asynchronous operations will be summarized to avoid zombie user interfaces. It is very important to simplify the development process. Previously, vs often got stuck when loading controls.

To implement this concept,Task classImplementedThen ()Method.

1.task<int> t1=task<int>([]()->int 2.    { 3.        return 42; 4.    }).then ([](int i)->int 5.        { 6.         7.            return i+42;     8.        }); 9.  10.task<int> t2=task<int>([]()->int 11.    { 12.        //simule une charge 13.        std::this_thread::sleep_for (chrono::seconds (1));         14.        return 42; 15.    }).then ([](int i)->int 16.        { 17.             18.            return i+42;     19.        });             20.std::wcout << t1.get() + t2.get() << std::endl;

 

The return value of each task is passed as a parameter, for exampleThen ()Method.

Parallel Algorithms also include (Parallel_for, parallel_for_each, parallel_invoke))

Parallel Algorithm attempts

Parallel_sort,Parallel_radixsort,Parallel_buffered_sortStandard if the command runs only in parallel mode, the algorithm is used in the same way.

 

1.vector<unsigned int> v3; 2.   parallel_sort(begin(v3),end(v3))

 

 

Although in most cases this is a simple class integer, the performance is smooth enough (the sample vector integer is 40 000 000 to get a sequence(STD: Sort)AndParallel_sort (),Allows better control and improves performance)

Each algorithm is used for reload. This method allows you to set the sorting of strings and the size of the partitions to be used.

1.vector<unsigned int> v2; 2.      parallel_sort(begin(v2),end(v2),[](const unsigned int& l,const unsigned int& r)->bool     3.   { 4.        5.       return (l<r); 6.   },4096);   

 

 

The default partition size is 2048, which is sufficient in most cases. AdjustableLastThe size of a partition to improve performance. If the partition size is equal to the vector size, any method is used.STD: Sort (). (By default, if the number of elements in the vector is always <= 2048STD: Sort ()Version ).

1.inline void parallel_sort(const _Random_iterator &_Begin, const _Random_iterator &_End, const _Function &_Func, const size_t _Chunk_size = 2048) 2.{ 3.    _CONCRT_ASSERT(_Chunk_size > 0); 4.  5.    // We make the guarantee that if the sort is part of a tree that has been canceled before starting, it will 6.    // not begin at all. 7.    if (is_current_task_group_canceling()) 8.    { 9.        return; 10.    } 11.  12.    size_t _Size = _End - _Begin; 13.    size_t _Core_num = Concurrency::details::_CurrentScheduler::_GetNumberOfVirtualProcessors(); 14.  15.    if (_Size <= _Chunk_size || _Core_num < 2) 16.    { 17.        return std::sort(_Begin, _End, _Func); 18.    } 19.  20.    _Parallel_quicksort_impl(_Begin, _Size, _Func, _Core_num * _MAX_NUM_TASKS_PER_CORE, _Chunk_size, 0); 21.}

 

 

The system will at least not use the STD: Sort () virtual processor in any way.

Try other algorithms as needed. For example, in the example, the base sorting

 

1.vector<unsigned int> v4; 2.   parallel_radixsort(begin(v4),end(v4));

 

 

Provide better results 197.09 for over 40 000 projects. The default partition size is 256*256.

Parallel_buffered_sortTo 631

1. parallel_buffered_sort(begin(v5),end(v5));

 

 

Decrease

The minimum number of threads applied. The maximum number of all members in a group is generally reduced. The application can reduce the number of parallel operations or round-robin of other floating point operations involving contention conditions or problems.

For example, for a loop of two parallel rows, one method of the amount of loops is to useConcurrency: combinable <t>Object

 

 

 

1.combinable<double> sumReduction;     2.   parallel_for_each(begin(v1),end(v1),[&sumReduction](double i) 3.   {      4.       sumReduction.local ()+=i;        5.   }); 6.   sum=sumReduction.combine (std::plus<double>());

Visual Studio 11, now you can use algorithmsParallel_reduce (),Higher efficiency than type Algorithms(Concurrency: combinable ).

Its usage is relativeProcessing Algorithm (STD: Accumulate)

 

vector<double>::const_iterator deb=begin(v1); 2.   vector<double>::const_iterator fin=end(v1); 3.   auto identity=std::iterator_traits<vector<double>::const_iterator>::value_type();    4.   sum= parallel_reduce(deb,fin,identity,std::plus<double>());

 

Transformation

The final algorithm is processing.Parallel_transformation ()Algorithm

 

1.parallel_transform(begin(v1),end(v1),begin(v2),[](double value)->double 2.{ 3.    return value*2; 4.}); 

 

Download vs11 now

Http://www.microsoft.com/click/services/Redirect2.ashx? Cr_cc = 200098144

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.