Skip to content

DPC++ capture this pointer in the kernal function #580

Description

@Maccchiatooo

When I want to capture the this pointer or outside variable in DPC++ code, I have to write it in my lambda function explicitly

    class abc{
    queue Q{};
    std::array<int, constsize> e;
    std::array<double, constsize>t;
    abc(){
    ua = malloc_shared<double>(local_size, this->Q);
    }
    
    void b();
    
  }

 void abc::b(){
      for(int i=0;i<constsize;i++){
      e[i]=i;
      t[i]=2*i; 
      }

    buffer<int> ee{e};
    buffer<double> tt{t};
auto ini2 = this->Q.submit([&,uq=this->ua](handler &h)
            { accessor eee{ee, h, read_only};
              accessor ttt{tt, h, read_only};
                                        
              h.parallel_for(range{size1, size2, size3}, [=](id<3> idx)
              double eu=uq[id[0]];
              int aa=eee[id[1]];
              double cc=ttt[id[2]];
             }

 }

But, now I have a lot of variables that need to be captured in the lambda function, do I have to use the same method to write them explicitly? Or there are some other tricks to make this?

Activity

  1. reble commented on May 24, 2022

    @reble
    Contributor

    Is a by-copy capture with exceptions by-reference what you're looking for:
    [=, &size1,&size2,&size3](handler&h){};
    Btw, where are those variables sizeX coming from? Can't find them in the code above.
    Also your code won't compile. I've created a cleaned up version here:
    https://godbolt.org/z/6eenrvjzj
    Let us know if you have any more questions. Happy to help.

  2. locked and limited conversation to collaborators on May 24, 2022
  3. converted this issue into a discussion #585 on May 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions