added 33 characters in body
Source Link
Jamal
  • 34.8k
  • 13
  • 131
  • 235

I wanted to write a multi-threaded sort, but unfortunately I don't know much about threads, especially in C++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code:

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

Some of the tests I did sorting integers:

Sizesize is the number of intints sorted, and the time is in seconds.

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706
size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, and the results were at least twice as long as that, any. Any reasons why?

I wanted to write a multi-threaded sort, unfortunately I don't know much about threads, especially in C++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code:

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

Some of the tests I did sorting integers:

Size is the number of int sorted, and the time is in seconds.

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, the results were at least twice as long as that, any reasons why?

I wanted to write a multi-threaded sort, but unfortunately I don't know much about threads, especially in C++11. I managed to make something work, but I would be highly surprised if it was the best way to do it.

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

Some of the tests I did sorting integers:

size is the number of ints sorted, and the time is in seconds.

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, and the results were at least twice as long as that. Any reasons why?

Tweeted twitter.com/#!/StackCodeReview/status/302360498278653952
added 5 characters in body; edited title
Source Link
palacsint
  • 29.8k
  • 9
  • 79
  • 156

multi Multi-threaded sort

I wanted to write a multi-threaded sort, unfortunately I don't know much about threads, especially in c++11C++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code:

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

someSome of the tests I did sorting integers:

sizeSize is the number of int sorted, and the time is in seconds.

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsortstd::qsort instead of std::sortstd::sort, the results were at least twice as long as that, any reasons why?

multi-threaded sort

I wanted to write a multi-threaded sort, unfortunately I don't know much about threads, especially in c++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code:

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

some of the tests I did sorting integers:

size is the number of int sorted, and the time is in seconds

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, the results were at least twice as long as that, any reasons why?

Multi-threaded sort

I wanted to write a multi-threaded sort, unfortunately I don't know much about threads, especially in C++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code:

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

Some of the tests I did sorting integers:

Size is the number of int sorted, and the time is in seconds.

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, the results were at least twice as long as that, any reasons why?

removed tag from title
Source Link
Adam
  • 5.1k
  • 1
  • 27
  • 45

multi-threaded sort c++

I wanted to write a multi-threaded sort, unfortunately I don't know much about threads, especially in c++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code  :

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

some of the tests I did sorting integers:

size is the number of int sorted, and the time is in seconds

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, the results were at least twice as long as that, any reasons why  ?

Thanks for your help.

multi-threaded sort c++

I wanted to write a multi-threaded sort, unfortunately I don't know much about threads, especially in c++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code  :

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

some of the tests I did sorting integers:

size is the number of int sorted, and the time is in seconds

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, the results were at least twice as long as that, any reasons why  ?

Thanks for your help.

multi-threaded sort

I wanted to write a multi-threaded sort, unfortunately I don't know much about threads, especially in c++11. I managed to make something work, but I would be highly surprised if it was the best way to do it. Here is my code:

template<class T>
void ___sort(T *data, int len, int nbThreads){
    if(nbThreads<2){
        std::sort(data, (&(data[len])), std::less<T>());
    }
    else{

        std::future<void> b = std::async(std::launch::async,___sort<T>, data, len/2, nbThreads-2);
        std::future<void> a = std::async(std::launch::async,___sort<T>, &data[len/2], len/2, nbThreads-2);
    
        a.wait();
        b.wait();

        std::inplace_merge (data,&data[len/2],&data[len],std::less<T>());
    }
}

some of the tests I did sorting integers:

size is the number of int sorted, and the time is in seconds

size : 2097152
time with 1 thread : 4.961
time with 2 thread : 3.191
time with 4 thread : 2.377

size : 4194304
time with 1 thread : 10.002
time with 2 thread : 6.214
time with 4 thread : 4.689

size : 8388608
time with 1 thread : 19.975
time with 2 thread : 12.332
time with 4 thread : 9.29

size : 16777216
time with 1 thread : 38.712
time with 2 thread : 25.257
time with 4 thread : 18.706

Also, I tried using std::qsort instead of std::sort, the results were at least twice as long as that, any reasons why?

Source Link
Simon-Okp
  • 173
  • 1
  • 4
Loading