int main( int argc, char* argv[] ) {
 
    // Size of vectors
    int n = 1000000;
 
    // Input vectors
    double *restrict a;
    double *restrict b;
    // Output vector
    double *restrict c;
 
    // Size, in bytes, of each vector
    size_t bytes = n*sizeof(double);
 
    // Allocate memory for each vector
    a = (double*)malloc(bytes);
    b = (double*)malloc(bytes);
    c = (double*)malloc(bytes);
    #pragma omp target data map(alloc:a[0:n],b[0:n],c[0:n]) map(from:c)
    {

    // Initialize content of input vectors, vector a[i] = sin(i)^2 vector b[i] = cos(i)^2
    int i;
    #pragma omp target 
    #pragma omp parallel for
    for(i=0; i<n; i++) {
        a[i] = sin_squared(i/M_PI);;
        b[i] = cos_squared(i/M_PI);
    }   

    // sum component wise and save result into vector c
    #pragma omp target
    #pragma omp parallel for 
    for(i=0; i<n; i++) {
        c[i] = a[i] + b[i];
    }
 
    // Sum up vector c and print result divided by n, this should equal 1 within error
    double sum = 0.0;
    #pragma omp target
    #pragma omp parallel for reduction(+:sum)
    for(i=0; i<n; i++) {
        sum += c[i];
    }
    
    
    sum /= n;
    if(sum - 1.0 < 0.0000001)
      printf("result correct: %f\n",sum);
    else
      printf("result incorrect!\n");
 
    }
    // Release memory
    free(a);
    free(b);
    free(c);
 
    return 0;
}
Esempio n. 2
0
int main( int argc, char* argv[] ) {
 
    // Size of vectors
    const int n = 1000000;
 
    // Input vectors
    std::vector<double> a(n);
    std::vector<double> b(n);
    // Output vector
    std::vector<double> c(n);
 
    // Initialize content of input vectors, vector a[i] = sin(i)^2 vector b[i] = cos(i)^2
    for(int i=0; i<n; i++) {
        a[i] = sin_squared(i/M_PI);
        b[i] = cos_squared(i/M_PI);
    }   
 
    // sum component wise and save result into vector c
    for(int i=0; i<n; i++) {
        c[i] = a[i] + b[i];
    }
 
    // Sum up vector c and print result divided by n, this should equal 1 within error
    double sum = 0.0;
    for(int i=0; i<n; i++) {
        sum += c[i];
    }
    sum /= n;
    if(sum - 1.0 < 0.0000001)
      std::cout<<"result correct\n";
    else
      std::cout<<"result incorrect!\n";
 
    return 0;
}