Search code examples
c++parallel-processingsyclintel-oneapidpc++

Error facing with range function in DPC++


I'm new to Sycl/DPC++ language. I wrote a sample vector addition code using Unified shared memory (USM):

#include<CL/sycl.hpp>
#include<iostream>
#include<chrono>
using namespace sycl;
int main()
{
    int n=100;
    int i;
    queue q{ };
    range<1>(n);
    int *a=malloc_shared<int>(n,q);
    int *b=malloc_shared<int>(n,q);
    int *c=malloc_shared<int>(n,q);
    for(i=0;i<n;i++)
    {
        a[i]=i;
        b[i]=n-i;
    }
    q.parallel_for(n,[=](auto &i){
        c[i]=a[i]+b[i];
    }).wait();

    for(i=0;i<n;i++){
        std::cout<<c[i]<<std::endl;
    }
 
    free(a,q);
    free(b,q);
    free(c,q);
    return 0;
}

When I compile it I get the following error:

warning: parentheses were disambiguated as redundant parentheses around declaration of variable named 'n' [-Wvexing-parse]
range<1>(n);
        ^~~
vec_add.cpp:11:1: note: add enclosing parentheses to perform a function-style cast
range<1>(n);
^
(          )
vec_add.cpp:11:9: note: remove parentheses to silence this warning
range<1>(n);
        ^ ~
vec_add.cpp:11:10: error: redefinition of 'n' with a different type: 'range<1>' vs 'int'
range<1>(n);
         ^
vec_add.cpp:8:5: note: previous definition is here
int n=100;
    ^
1 warning and 1 error generated.

How to fix this error?


Solution

  • error: redefinition of 'n' with a different type: 'range<1>' vs 'int'

    Two variables with the same name within the same scope create confusion to the compiler, so it might be the reason for the error which you are getting. You can try defining the value of n globally say for eg: #define N 100 in this case, set

    range<1>(n);
    

    to

    range<1> (N);
    

    and use that in your code.

    If you want to declare the size locally then assign another variable (r) to the range as

    range<1> r (n);
    

    Now you can directly pass the 'r' variable as a parameter to the parallel_for.