lead-free
lead-free

Reputation: 128

clang execution header not found

I would like to parallelize some std::for_each loops using the new execution policies. No problem with gcc 9.3 on ubuntu 20.04, but clang 11 on macos 11 is complaining that #include <execution> not found. Have they not included execution yet?

My flags are -std=c++17 -lstdc++ -ltbb

Tried with both -lstdc++ and -libc++ and read a similar post.

Upvotes: 3

Views: 2484

Answers (3)

Konchog
Konchog

Reputation: 2188

On MacOS, as long as you don't mind being platform-specific, you can access the grand central dispatch to manage all your parallel stuff pretty easily.

Here is an example that calculates a polynomial against a file of x y values. (In RL I use a massive vector of polynomials also - this is just a fragment).

You need to mark any variables that will receive data with __block (to prevent obvious errors). dispatch_apply is a great alternative wrapper for a suitable high-process for loop. Typical speedup is 700% on my machine.

#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include <sstream>
#include <dispatch/dispatch.h>

void load_problem(const std::string file, std::vector<std::pair<double,double>>& repo) {
    repo.clear();
    std::ifstream ifs(file);
    if (ifs.is_open()) {
        std::string line;
        while(getline(ifs, line)) {
            double x= std::nan("");
            double y= std::nan("");
            std::istringstream istr(line);
            istr >> std::skipws >> x >> y;
            if (!isnan(x) && !isnan(y)) {
                repo.push_back({x, y});
            };
        }
        ifs.close();
    }
}

int main() {
    // n-factor polynomial - test against a given problem provided as a set of space delimited x y values in 2d.txt
    std::vector<std::pair<double,double>> problem;
    std::vector<double> args = {0.653398943958799,0.575258222088993,-5.54870756928019,-3.56273265353563,12.4189944179562,1.53213505629763,-4.09124685229838,5.7925805708932};
    load_problem("2d.txt",problem); //a list of space delimited doubles representing x, y.
    
    __block std::vector<double> answers;
    answers.reserve(problem.size());
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    size_t a_count = args.size();
    dispatch_apply(problem.size(), queue, ^(size_t i) {
        double score = 0.0;
        for (size_t j=0; j < a_count - 1; j++) {
            score += args[j]*pow(problem[i].first,a_count - i - 1);
        }
        answers[i] = score;
    });
    double final = 0;

    for (size_t i=0; i < problem.size(); i++) {
        final += answers[i];
    }
    std::cout << final << std::endl;
}

Upvotes: 1

Wongboo
Wongboo

Reputation: 132

I already have a way to use it.
download pstl.
1.include that directory
2.rename __pstl_config_site.in to __pstl_config_site and you delete

#cmakedefine _PSTL_PAR_BACKEND_SERIAL
#cmakedefine _PSTL_PAR_BACKEND_TBB
#cmakedefine _PSTL_HIDE_FROM_ABI_PER_T

add choose a right one between these, I choose TBB

#define _PSTL_PAR_BACKEND_TBB

3.link library TBB(or other backend)
4.in your main file define this before

#define _LIBCPP_HAS_PARALLEL_ALGORITHMS

test:


    int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) {
        vector A {1, 2, 3, 6, 5, 7};
        vector B {2, 3, 4, 8, 9, 10};
        cout << transform_reduce(execution::par, A.cbegin(), A.cend(), B.cbegin(), 0);
    }

output:
183
note some bug: pstl

Upvotes: 2

Ranoiaetep
Ranoiaetep

Reputation: 6637

Based on C++ compiler support, "Standardization of Parallelism TS" is not included in either Clang or Apple Clang.


Side note:

I thought the default Apple Clang version is 12 for macOS 11.

Either way, in Apple Clang 12, there is <execution> header, which recquires a _LIBCPP_HAS_PARALLEL_ALGORITHMS custom flag to take you to <__pstl_execution>. However <__pstl_execution> is not there implemented, or at least I couldn't find it.

Upvotes: 6

Related Questions