Parity ordering (without Cuda) based on c++11 CPU threads __c++

Source: Internet
Author: User

Writing is not necessarily right. Wrong, please. Preface

This article is made of c++11 thread.
Compiling is probably

g++ Sort.cpp-o3-pthread-std=c++14-o Sort

Actually, I haven't learned c++14.

Recently began to learn Cuda, feel thread specific how to use and hardware is directly related to different architectures AH different precision AH the way to use threads should be different. This blog is an experiment, using CPU to achieve parity sorting for future comparisons with Cuda

has been the CPU and the GPU to learn together. Both support multiple threads, but they are not a thread. CPU threads are more suitable for coarse-grained tasks, while GPU threads are more suitable for fine-grained tasks. and the Cuda minimum dispatch unit is the line Cheng (warp).

CPU thread creation, destruction, switching, or more time-consuming. That is, the reason that odd and even ordering is not suitable for CPU multithreading. N Factory GPU Each thread has its own register, which is very powerful, there is no context switching overhead, just dial the switch lightly. Cuda Programming One idea is to use a mass of threads to conceal the latency of the visit. Odd-even ordering

The odd and even sort idea is not too difficult, originally is for the parallel design algorithm. The problem is to split the data by parity, open almost half the length of the array of threads to and adjacent comparisons. If you can parallel any number of threads, then you only need to: I compare once, odd compare once, I ... A flag is set up to indicate whether the sort is complete, in full order.

Pictures from wiki, odd-even ordering process

Learn the spirit of the algorithm class, specifically with vanilla quick sort,bubble sort do compare and timing (in fact, not necessary, parity sort is too slow = =) Test results

LEN = 10000
Vanilla Quick Sort 0.002025s
Odd even sort 857.425s

LEN = 1000

Vanilla Quick Sort 0.000101s
Odd even sort 8.55487s

LEN = 100
Vanilla Quick Sort 5e-06s
Bubble sort 2.5e-05s
Odd even sort 0.072577s main code

BOOL Sorted = false;

Template<typename t=int>
void Sortswap (t* data,int index,int right)
{

    if (index = right)
        return ;
    if (data[index]>data[index+1])
    {
        swap (data[index],data[index+1]);
        Sorted = false;
    }
}
Template<typename t=int>
void Oddevensort (t* data,int left = 0,int right = LEN-1)
{

    int latch = (righ T-left)/2 + (right-left)%2;
    Thread Threads[latch];
    int start = left;
    while (! Sorted)
    {

        Sorted = true;

        for (int i = START,J = 0;j<latch;i+=2,j++)
            threads[j] = thread (sortswap<t>,data,i,right);

        for (int i = 0;i<latch;i++)
            threads[i].join ();

        if (start = = left)
            start = left +1;
        else
            start = left;
    }
}
All Code
#include <thread> #include <atomic> #include <ctime> #include <iostream> #include <ctime>

#include <cstdlib> using namespace std;
using TYPE = int;

const int LEN = 100;

type* data = new Type[len];
    Template<typename t=int> void disp (t* data,int L) {//cout<<__func__<< ":";
        for (int i = 0;i<l;i++) {if (i!=0) && (i%10==0)) cout<<endl;
    cout<<data[i]<< "";
} cout<<endl;
    } template<typename t=int> void init (t* data,int len) {srand (unsigned (time (0)));
for (int i = 0;i<len;i++) data[i] = (T) rand (); } template<typename t=int> void Bubblesort (t* data,int left = 0,int right = LEN-1) {for (int i = left; I < RI Ght i++) for (int j = i + 1; j < right; J +) if (Data[i] > Data[j]) Swap (data[  
I], data[j]); } template<typename t=int> void Qsort (t* data,int left = 0,int right = LEN-1{if (left < right) {int L = left;
        int h = right;
        T pivot = Data[left];
            while (L

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.