How can I compile this very big, but boring C source?

痞子三分冷 提交于 2019-12-13 15:26:27

问题


The central function in my code looks like this (everything else is vanilla input and output):

const int n = 40000;

double * foo (double const * const x)
{
    double * y = malloc (n*sizeof(double));

    y[0] = x[0] + (0.2*x[1]*x[0] - x[2]*x[2]);
    y[1] = x[1] + (0.2*x[1]*x[0] - x[2]*x[2]);
    // …
    // 39997 lines of similar code
    // that cannot be simplified to fewer lines
    // …
    y[40000] = 0.5*x[40000] - x[12345] + 5*x[0];

    return y;
}

Assume for the purpose of this question that hard-coding these 40000 lines like this (or very similar) is really necessary. All these lines only contain basic arithmetic operations with fixed numbers and entries of x (forty per line on average); no functions are called. The total size of the source is 14 MB.

When trying to compile this code I face an extensive memory usage by the compiler. I could get Clang to compile it with -O0 (which takes only 20 s), but I failed with the GCC (even with -O0) or with -O1.

While there is little that can be optimised on the code side or on a global scale (i.e., by computing the individual lines in another order), I am confident that a compiler will find some things to optimise on a local scale (e.g., calculating the bracketed term needed to calculate y[0] and y[1]).

My questions are thus:

  • Are there some compiler flags that activate only optimisations that do not require much additional memory?
  • Are there some other ways to make the compiler handle this source better (without losing more speed than gained through optimisation)?

回答1:


The following comment by Lee Daniel Crocker solved the problem:

I suspect the limit you're running into is the size of the structures needed for a single stack frame/block/function. Try breaking it up into, say, 100 functions of 400 lines each and see if that does better.

When using functions of 100 lines each (and calling all of them in a row), I obtained a program that I could compile with -O2 without any problem.




回答2:


You can add swap space as much as required to get the compiler to compile the code with optimizations enabled. Using this technique, the compilation process will become much slower. However, the amount of memory available to the compiler would be limited only by the size of the virtual address space. Another less convenient option is to install more RAM. Also make sure that the process in which the compiler is running doesn't have limits on the amount of memory it can allocate. Regarding compiler flags, I don't think there are flags that you can use to directly control the memory usage of the compiler and let the compiler adjust itself to the specified limit.




回答3:


Write it in assembly.

I assume you have a tool that is generating this C file. Why not have it spit out assembly code instead?



来源:https://stackoverflow.com/questions/33815231/how-can-i-compile-this-very-big-but-boring-c-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!