Limit pointers and nesting

I tried using limited qualified pointers and I ran into a problem. The program below is just to introduce the problem.

The calc_function function uses three pointers, which are limited, so they "MUST" not be aliases with each other. When compiling this code in visual studio, the function will be built-in, so for no reason Visual Studio 2010 ignores qualifiers. If I turn off inlining, the code will run more than six times faster (from 2200 to 360 ms). But I don’t want to disable the attachment in the whole project and the whole file (because then it will be an overhead for a call, for example, for all getters and setters, which would be terrible).

(Can the only solution disable the attachment of this function only?)

I tried to create time limits for qualified pointers in a function, both on top and in the inner loop, in order to try to tell the compiler that I promise that there are no aliases, but the compiler will not believe me and this will not work. I also tried setting the compiler options, but the only one I found that works is to disable inlining.

I would appreciate help in solving this optimization problem.

To run the program (in realeasemode), remember to use the arguments 0 1000 2000. Why use the arguments of userinput / program to make sure that the compiler cannot know whether or not anti-aliasing exists between the a, b, and c pointers.

#include <cstdlib> #include <cstdio> #include <ctime> // Data-table where a,b,c will point into, so the compiler cant know if they alias. const size_t listSize = 10000; int data[listSize]; //void calc_function(int * a, int * b, int * c){ void calc_function(int *__restrict a, int *__restrict b, int *__restrict c){ for(size_t y=0; y<1000*1000; ++y){ // <- Extra loop to be able to messure the time. for(size_t i=0; i<1000; ++i){ *a += *b; *c += *a; } } } int main(int argc, char *argv[]){ // argv SHALL be "0 1000 2000" (with no quotes) // init for(size_t i=0; i<listSize; ++i) data[i] = i; // get a, b and c from argv(0,1000,2000) int *a,*b,*c; sscanf(argv[1],"%d",&a); sscanf(argv[2],"%d",&b); sscanf(argv[3],"%d",&c); a = data + int(a); // a, b and c will (after the specified argv) be, b = data + int(b); // a = &data[0], b = &data[1000], c = &data[2000], c = data + int(c); // So they will not alias, and the compiler cant know. // calculate and take time time_t start = clock(); funcResticted(a,b,c); time_t end = clock(); time_t t = (end-start); printf("funcResticted %u (microSec)\n", t); system("PAUSE"); return EXIT_SUCCESS; } 
+7
source share
1 answer

If you declare a function using __declspec(noinline) , this causes it to not be nested:

http://msdn.microsoft.com/en-us/library/kxybs02x%28v=vs.80%29.aspx

You can use this to manually disable embedding based on each function.


As for restrict , the compiler can use it only when it wants. Thus, messing around with different versions of the same code is somewhat inevitable when trying to β€œtrick” compilers to perform such optimizations.

+3
source

All Articles