-
Notifications
You must be signed in to change notification settings - Fork 15
YAKL_SCOPE
Matt Norman edited this page Dec 11, 2021
·
1 revision
In C++, lambdas only capture by value variables defined in local scope. This is a problem in two separate cases: this->var
and ::var
. In each of these cases, since they are not in local scope, C++ lambdas access them by referencing them from the CPU. This will cause an invalid memory address error inside a device kernel. To alleviate this, please use YAKL_SCOPE( var , this->var );
or YAKL_SCOPE( var , ::var );
to place the variable into local scope so that C++ lambdas copy it by value, making it valid in device memory when used in a device kernel. This statement goes before the parallel_for
. For instance:
class Chicken {
int liver;
void peck() {
YAKL_SCOPE( liver , this->liver );
parallel_for( 1 , YAKL_LABMDA (int dummy) {
liver++;
});
}
};
What the YAKL_SCOPE()
macro does is create a local reference to the variable, e.g., auto &var = this->var;