===== Sample code 1 ===== #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include using namespace std; typedef struct { int start; int end; } RANGE; int main(int argc, char * argv[]) { string filename = argv[1]; int nw = atoi(argv[2]); // reading string from file string text = ""; chrono::system_clock::time_point start = std::chrono::system_clock::now(); std::ifstream t(argv[1]); string line; while(getline(t,line)){ text.append(line); } t.close(); chrono::system_clock::time_point end = std::chrono::system_clock::now(); cout << "reading " << chrono::duration_cast(end - start).count() << " "; // keeping the original string original = text; // the traduttore function auto traduttore = [](char c){ // this_thread::sleep_for(chrono::microseconds(1)); return toupper(c); }; // sequential : std::transform start = std::chrono::system_clock::now(); transform(//execution::parallel_policy, text.begin(), text.end(), text.begin(), traduttore ); end = std::chrono::system_clock::now(); cout << "transform " << chrono::duration_cast(end - start).count() << " " ; //this_thread::sleep_for(chrono::microseconds(8000000)); // sequential : classical loop text = original; start = std::chrono::system_clock::now(); for(unsigned int i=0; i(end - start).count() << " " ; // parallel : openmp text = original; start = std::chrono::system_clock::now(); // to be compiled with -fopenmp #pragma omp parallel for num_threads(nw) for(unsigned int i=0; i(end - start).count() << " "; // parallel : c++ threads text = original; start = std::chrono::system_clock::now(); vector ranges(nw); int m = text.size(); int delta { m / nw }; vector tids; for(int i=0; i(end - start).count() << " " ; // cilk: parallel for (you need -fcilkplus) text = original; start = std::chrono::system_clock::now(); __cilkrts_set_param("nworkers",argv[2]); { cilk_for(int i=0; i(end - start).count() << " " ; // grppi text = original; start = std::chrono::system_clock::now(); auto thr = grppi::parallel_execution_native{nw}; auto omp = grppi::parallel_execution_omp{}; // omp.set_concurrency_degree(nw); auto seq = grppi::sequential_execution{}; grppi::map(seq, text.begin(), text.end(), text.begin(), traduttore); end = std::chrono::system_clock::now(); cout << "grppi map thr " << chrono::duration_cast(end - start).count() << " " ; cout << "nw " << nw << endl; // cout << text << endl; return 0; }