Thursday 15 January 2015

c++ - Handling #ifdef's that were used to create multiple versions of an algorithm -


I am trying to benchmark several (approximately 25) variations of the algorithm written in C ++

I have implemented these methods using a combination of three methods:

  1. To copy and make minor changes to the copied version <

  2. using #ifdef s to switch between the snippets of code

    Variations arising from options 1 and 2 are OK Because I can select which variation of the algorithm in the configuration file, I can then iterate through different configuration files and keep a record of "configuration: result" pair - keeping these records very important for my work is.

    Let me currently #ifdef s because I have to compile more than one version of code to access these variables, to run automated experiment scripts and It is very difficult to keep accurate records. However, #ifdef s is very useful because if I get a copy of the code in error, then I do not have to remember to correct this mistake to improve many copies.

    #ifdef has extended six forms, which I have both copied code and 24 total variations (4 variations for each basic change) both sub-closing Created by

    Here's an example - Mostly I am using #ifdef s to avoid duplication of code:

      .... double lasso_gam = * gamma; * Lasso_idx = -1; For (int aj = 0; aj & lt; (int) a_idx.size (); aj ++) {int j = a_idx [aj]; Emphasis (J & lt; = C * L); Double Inc = W [AG] * (* Gamma) * Icon [AJ]; If ((beta_sp (j)> gt; 0 and beta_sp (j) + inc <0) #ifdef ALLOW_NEG_LARS || (beta_p (j) <0 & amp; beta_p (j) + inc & gt; ; 0) # other || (beta_p (j) == 0 and BBA_SP (J) + Ink & Lt; 0) #Endif) {double TMP_Gim = = BITASP (J) / WA [AJ] * Icon [AJ]; If (tmp_gam> = 0 & amp; amp; tmp_gam & lt; lasso_gam) {* lasso_idx = aj; * Next_active = j; Lasso_gam = tmp_gam; }}} If (lasso_idx> = 0) {* gamma = lasso_gam; }.   

    Question: What is the best way to allow multiple variations of the algorithm, which is currently available in #ifdef A configuration file is given to run s, which specifies the difference in how to run the algorithm.

    Ideally, I want to compile the code only once and use the config file to select algorithm variation on the runtime.

    If your #if s is scattered all around and one The row changes the code, or here its all the #if s is changed on the basis of the ANIM run in the function in if s, for which the variation can be run and Hope the compiler does a great job optimizing on optimization to decide to run it, leaving the single run time condition apart from the function And defined to be roughly the same code. No promise

    If you add a block of code to the #if algorithm, then you divide the algorithm into smaller functions, which can call different implementations of your different algorithm. This is clearly impractical if your #if s is so intrusive that you will end up with 50 functions.

No comments:

Post a Comment