Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SimpleForDirective.cpp
- * F=OpenMpDemo && g++ -c $F.cpp -o $F.o -fopenmp -std=c++0x && g++ $F.o -o $F.exe -fopenmp && ./$F.exe
- */
- #include <iostream>
- #include <iterator>
- #include <algorithm>
- #include <vector>
- #include <stdio.h>
- using namespace std;
- void sequential(int *a, int size, int *b)
- {
- int i;
- for (i = 0; i < size; i++) {
- b[i] = a[i] * i;
- }
- }
- void parallel_for(int *a, int size, int *b)
- {
- #pragma omp parallel for
- for (int i = 0; i < size; i++) {
- b[i] = a[i] * i;
- }
- }
- void parallel_for_with_an_usage_error(int *a, int size, int *b)
- {
- int j;
- #pragma omp parallel for
- for (int i = 0; i < size; i++) {
- /*int*/ j = a[i]; //To be correct j should be declared here, in-loop to be private !
- j *= i;
- b[i] = j;
- }
- }
- #define SIZE 9999
- //#define DUMP_ARRAY
- int main(int ac, char** av)
- {
- cout << "OpenMP Demo Simple For Directive and sharing example....." << endl;
- int a[SIZE]; int root = 1; generate(a, a+SIZE, [&root](){return ++root;});
- int b[SIZE]; fill(b, b+SIZE, 0);
- sequential(&a[0], SIZE, b);
- #ifdef DUMP_ARRAY
- copy(b, b + SIZE, ostream_iterator<int>(cout, " ")), cout << endl;
- #endif
- int reference[SIZE]; copy(b, b + SIZE, reference);
- /*parallel_for(&a[0], SIZE, b);
- #ifdef DUMP_ARRAY
- copy(b, b + SIZE, ostream_iterator<int>(cout, " ")), cout << endl;
- #endif
- //*/
- parallel_for_with_an_usage_error(&a[0], SIZE, b);
- #ifdef DUMP_ARRAY
- copy(b, b + SIZE, ostream_iterator<int>(cout, " ")), cout << endl;
- #endif
- //*/
- if(!equal(b, b+SIZE, reference)){
- cout << "error .... " << endl;
- } else {
- cout << "ok .... " << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement