Prefetching for dual class requires casting to char *?
I have a class that I am using _mm_prefetch()
to pre-request a cacheline containing a class member of type double:
class MyClass{
double getDouble(){
return dbl;
}
//other members
double dbl;
//other members
};
_mm_prefetch()
signature:
void _mm_prefetch (char const* p, int i)
But when I do this:
_mm_prefetch((char*)(myOb.getDouble()), _MM_HINT_T0);
GCC complains:
error: invalid cast from type 'double' for input 'char *'
So how do I prefetch this class member?
source to share
If you've read the description for _mm_prefetch()
from the site you linked to, follow these steps:
void _mm_prefetch (char const* p, int i)
Retrieve the string of data from memory that contains address p to the location in the cache hierarchy given by the locality hint.
So, you need to pass the address of the variable you want. To do this, you need to pass the function either the address of the class member reference or a pointer to it.
The simplest solution is change getDouble()
to get the link back and then you can use:
_mm_prefetch((char*)(&myOb.getDouble()), _MM_HINT_T0);
source to share
_mm_prefetch
loads a cache line by memory addresses into the cache. Trying to cast a double pointer to a pointer doesn't work as they are not compatible. In addition, you cannot simply call _mm_prefetch
on the return value of getDouble, since it will not be the address of the instance you are looking for, it will be a local or temporary address (presumably already "close" to the processor). Also, the action of simply calling getDouble will load the address because you are accessing the member inside the function.
Chances are your best option (depending on the size MyClass
and size of your target cache) is to load the entire object into the cache, e.g .:
_mm_prefetch(&myOb, _MM_HINT_T0);
Prefetching is also most efficient when you can do it somewhat before the operation you are performing on a memory address. For example, doing:
_mm_prefetch(&myOb, _MM_HINT_T0);
myObj.getDouble();
Will not benefit performance (it can actually hurt).
source to share