Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

std::sort( std::execution::par, v2.begin(), v2.end()); // parallel sort

39 views
Skip to first unread message

gdo...@gmail.com

unread,
Nov 19, 2022, 4:02:21 PM11/19/22
to
from C++20 for programmers, a Deitel book
...
std::sort( std::execution::par, v2.begin(), v2.end()); // parallel sort
...

std::execution::par, is it usable in c++20?

Öö Tiib

unread,
Nov 19, 2022, 6:09:16 PM11/19/22
to

gdo...@gmail.com

unread,
Nov 19, 2022, 7:54:14 PM11/19/22
to
the code comes from C++20 for programmers, a Deitel book
I tried to compile the following code:

// fig17_01.cpp
// Profiling sequential and parallel sorting with the std::sort algorithm.

#include <algorithm>
#include <chrono> // for timing operations
#include <execution> // for execution policies
#include <iostream>
#include <iterator>
#include <random>
#include <vector>

int main()
{
// set up random-number generation

std::random_device rd;
std::default_random_engine engine{rd()};
std::uniform_int_distribution ints{};

std::cout << "Creating a vector v1 to hold 100,000,000 ints\n";
std::vector<int> v1(100'000'000); // 100,000,000 element vector

std::cout << "Filling vector v1 with random ints\n";
std::generate(v1.begin(), v1.end(), [&](){return ints(engine);});

// copy v1 to create identical data sets for each sort demonstration

std::cout << "Copying v1 to vector v2 to create identical data sets\n";
std::vector v2{v1};

// <chrono> library features we'll use for timing

using std::chrono::steady_clock;
using std::chrono::duration_cast;
using std::chrono::milliseconds;

// sequentially sort v1

std::cout << "\nSorting 100,000,000 ints sequentially\n";
auto start1 { steady_clock::now() }; // get current time
std::sort(v1.begin(), v1.end()); // sequential sort
auto end1 { steady_clock::now() }; // get current time

// calculate and display time in milliseconds

auto time1{duration_cast<milliseconds>(end1 - start1)};
std::cout << "Time: " << (time1.count() / 1000.0) << " seconds\n";

// parallel sort v2

std::cout << "\nSorting the same 100,000,000 ints in parallel\n";
auto start2{steady_clock::now()}; // get current time
std::sort(std::execution::par, v2.begin(), v2.end()); // parallel sort
auto end2{steady_clock::now()}; // get current time

// calculate and display time in milliseconds

auto time2{duration_cast<milliseconds>(end2 - start2)};
std::cout << "Time: " << (time2.count() / 1000.0) << " seconds\n";
}

clang++ -std=c++2a fig17_01.cpp
g++ -std=c++2a fig17.01.cpp

I receive errors relating to std::execution::par

how do I compile this?

Öö Tiib

unread,
Nov 19, 2022, 9:11:43 PM11/19/22
to
On Sunday, 20 November 2022 at 02:54:14 UTC+2, gdo...@gmail.com wrote:
> the code comes from C++20 for programmers, a Deitel book
> I tried to compile the following code:
>

> clang++ -std=c++2a fig17_01.cpp
> g++ -std=c++2a fig17.01.cpp
>
> I receive errors relating to std::execution::par
>
> how do I compile this?

You read the text of errors and make corrections based on those. It can
be some compiler switch (like -ltbb) is missing ... or it can be that your
toolchain is not set up properly.

gdo...@gmail.com

unread,
Nov 19, 2022, 10:30:25 PM11/19/22
to


> You read the text of errors and make corrections based on those. It can
> be some compiler switch (like -ltbb) is missing ... or it can be that your
> toolchain is not set up properly.

using g++:

fig17_01.cpp:53:22: error: no member named 'execution' in namespace 'std'; did you mean 'exception'?
std::sort(std::execution::par, v2.begin(), v2.end()); // parallel sort
~~~~~^~~~~~~~~
exception
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/exception:99:29: note: 'exception' declared here
class _LIBCPP_EXCEPTION_ABI exception
^
fig17_01.cpp:53:33: error: no member named 'par' in 'std::exception'
std::sort(std::execution::par, v2.begin(), v2.end()); // parallel sort
~~~~~~~~~~~~~~~~^
2 errors generated.


using clang++:

fig17_01.cpp:53:22: error: no member named 'execution' in namespace 'std'; did you mean 'exception'?
std::sort(std::execution::par, v2.begin(), v2.end()); // parallel sort
~~~~~^~~~~~~~~
exception
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/exception:99:29: note: 'exception' declared here
class _LIBCPP_EXCEPTION_ABI exception
^
fig17_01.cpp:53:33: error: no member named 'par' in 'std::exception'
std::sort(std::execution::par, v2.begin(), v2.end()); // parallel sort
~~~~~~~~~~~~~~~~^
2 errors generated.


so <execution> has been included. but apparently it's not there. so it must be as you say, something, some library, needs to be -I included in the compile command?

Paavo Helde

unread,
Nov 20, 2022, 3:10:33 AM11/20/22
to
Compiles fine with g++ 10.2. Maybe you have too old gcc. If it doesn't
have -std=c++20 and only has -std=c++2a, then it's probably already a
sign it's too old.



gdo...@gmail.com

unread,
Nov 20, 2022, 4:47:24 AM11/20/22
to
Xcode 14.1, set target 20

GNU++20 [-std=gnu++20]

Bonita Montero

unread,
Nov 20, 2022, 5:37:39 AM11/20/22
to
Here's an example of std::sort with parallel execution
as well as single threaded, especially written for you:

#include <iostream>
#include <algorithm>
#include <execution>
#include <vector>
#include <random>
#include <chrono>
#include <charconv>

using namespace std;
using namespace chrono;

int main( int argc, char **argv )
{
if( argc < 2 )
return EXIT_FAILURE;
size_t n;
if( from_chars_result fcr = from_chars( argv[1], argv[1] + strlen(
argv[1] ), n ); (bool)fcr.ec || *fcr.ptr )
return EXIT_FAILURE;
vector<size_t> vs( n );
mt19937_64 mt;
uniform_int_distribution<size_t> uid( 0, -1 );
for( size_t &s : vs )
s = uid( mt );
auto start = high_resolution_clock::now();
if( argc > 2 )
sort( execution::parallel_policy(), vs.begin(), vs.end() );
else
sort( vs.begin(), vs.end() );
cout << n << ": " <<
duration_cast<nanoseconds>(high_resolution_clock::now() - start).count()
/ 1.0e9 << endl;
}

The first parameter is the number of elements in the vector to be
sorted. If there's an arbitrary second parameter the vector is sorted
on all processor cores.

Bonita Montero

unread,
Nov 20, 2022, 6:19:23 AM11/20/22
to
This modified program shows at which point parallel sorting
becomes more efficient than single-threaded sorting. For my
Zen2-CPU this is 0x1000 elements.

#include <iostream>
#include <algorithm>
#include <execution>
#include <vector>
#include <random>
#include <chrono>
#include <charconv>

#if defined(_MSC_VER)
#pragma warning(disable: 4996)
#endif

using namespace std;
using namespace chrono;

int main( int argc, char **argv )
{
if( argc < 2 )
return EXIT_FAILURE;
size_t upper;
bool b16 = strnicmp( argv[1], "0x", 2 ) == 0;
argv[1] += b16 * 2;
if( from_chars_result fcr = from_chars( argv[1], argv[1] + strlen(
argv[1] ), upper, b16 ? 16 : 10 ); (bool)fcr.ec || *fcr.ptr )
return EXIT_FAILURE;
vector<size_t> vs;
vs.reserve( upper );
size_t size = 1;
do
{
size = size * 2 <= upper ? size * 2 : upper;
vs.resize( size );
mt19937_64 mt;
uniform_int_distribution<size_t> uid( 0, -1 );
auto perf = [&]( auto fn )
{
for( size_t &s : vs )
s = uid( mt );
auto start = high_resolution_clock::now();
fn();
return duration_cast<nanoseconds>( high_resolution_clock::now() -
start ).count() / 1.0e9;
};
double
tParallel = perf( [&]() { sort( execution::parallel_policy(),
vs.begin(), vs.end() ); } ),
tSerial = perf( [&]() { sort( vs.begin(), vs.end() ); } );
cout << "0x" << hex << size << ": " << tSerial / tParallel << endl;
} while( size < upper );
}

gdo...@gmail.com

unread,
Nov 20, 2022, 3:31:11 PM11/20/22
to
On Sunday, November 20, 2022 at 4:47:24 AM UTC-5, gdo...@gmail.com wrote:
> Xcode 14.1, set target 20
>
> GNU++20 [-std=gnu++20]

if someone who is using a Mac OS, Xcode 14.1, please check if there is an "execution" inside std::
or try to compile the code. maybe my Xcode has been corrupted, but I tried this code on https://www.onlinegdb.com/online_c++_compiler
and it gave me a similar error about std::execution:par ...

Öö Tiib

unread,
Nov 20, 2022, 3:55:12 PM11/20/22
to
Will check tomorrow ... no Mac here. But it can be that Apple hasn't implemented
it as most of their focus is on Swift, not C++.

Paavo Helde

unread,
Nov 20, 2022, 5:14:23 PM11/20/22
to
20.11.2022 11:47 gdo...@gmail.com kirjutas:
> Xcode 14.1, set target 20

XCode is just an IDE which will call some compiler underneath (clang or
g++). I've not used it in last 15 years, but I'm sure some googling will
help how to find out the version of g++ it's using, and how to upgrade
it if necessary.

Michael S

unread,
Nov 20, 2022, 5:52:00 PM11/20/22
to
I heard that Apple tools by default come with no gcc/g++.
In their environment gcc/g++ are links to clang/clang++.
0 new messages