循环展开依赖循环

时间:2013-06-12 15:14:49

标签: c loops loop-unrolling

我正在开发一个大型应用程序,我需要在某个过程的后续依赖循环上执行循环展开。我在下面写了一小段代码来复制更大的版本。

考虑原始代码:

void main()
{

 int a[20] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
 int b[20] = {10,9,8,7,6,5,4,3,2,1,20,19,18,17,16,15,14,13,12,11};
 int i,j,k,l;
 int nab =4, vab =10;
 int dimi, dimj, dimij, dimk, diml, dimkl, dimijkl;
 int count = 0;

 for (i = nab+1; i< nab+vab; i++) 
 {
   dimi = a[i];
   for (j = i; j< nab+vab; j++)
   {
    dimj = b[j];
    dimij = dimi*dimj;
    count = count +1;

    for (k = nab+1; k< nab+vab; k++)
    {
     dimk = a[k-1];
     for (l =k; l< nab+vab; l++)
     {
      diml = a[l-1];
      dimkl = dimk*diml;
      dimijkl = dimij * dimkl;
     }
    }
   }
  }
 printf ("Final dimension:%d \n ", dimijkl);
 printf ("Count:%d \n ", count);
}

现在我将循环i展开2倍:

for (i = nab+1; i< nab+vab; i+=2)
{
  dimi = a[i];
  for (j = i; j< nab+vab; j++)
  {
   dimj = b[j];
   dimij = dimi*dimj;
   count = count +1;

   for (k = nab+1; k< nab+vab; k++)
   {
     dimk = a[k-1];
     for (l =k; l< nab+vab; l++)
     {
      diml = a[l-1];
      dimkl = dimk*diml;
      dimijkl = dimij * dimkl;
     }
    }
  }

  dimi = a[i+1];
  for (j = i+1; j< nab+vab; j++)
  {
    dimj = b[j];
    dimij = dimi*dimj;
    count = count +1;

     for (k = nab+1; k< nab+vab; k++)
     {
      dimk = a[k-1];
      for (l =k; l< nab+vab; l++)
      {
        diml = a[l-1];
        dimkl = dimk*diml;
        dimijkl = dimij * dimkl;
      }    
     }
    }
   }
   printf ("Final dimension:%d \n ", dimijkl);
   printf ("Count:%d \n ", count);

现在我希望将循环ij展开2倍,但由于循环j取决于循环i,我有点不确定我应该如何写它。如何重写代码以将ij展开2倍。 随着我增加展开因素,代码也将变得越来越笨拙。有没有一种聪明的方法可以手动展开它,而不会让代码变得太丑陋。

在这种特殊情况下,我不能使用编译器标志(例如:-funroll-loops)。我想通过手动循环展开来接近它。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

也许不是最优雅的解决方案,但您可以使用宏:

#define INNER_L_LOOP(l) \
    { \
        diml = a[l-1]; \
        dimkl = dimk*diml; \
        dimijkl = dimij * dimkl; \
    } 

#define INNER_K_LOOP(k) \
    { \
        dimk = a[k-1]; \
        for (l =k; l< nab+vab; l++) \
        { \
            INNER_L_LOOP(l) \
        } \
    } 

#define INNER_J_LOOP(j) \
    { \
        dimj = b[j]; \
        dimij = dimi*dimj; \
        count = count +1; \
        \
        for (k = nab+1; k< nab+vab; k += 2) \
        { \
            INNER_K_LOOP(k) \
            if (k+1 < nab+vab) \
                INNER_K_LOOP(k+1) \
        } \
    } 

#define INNER_I_LOOP(i) \
    { \
        dimi = a[i]; \
        for (j = i; j< nab+vab; j+= 2) \
        { \
            INNER_J_LOOP(j) \
            if (j+1 < nab+vab) \
                INNER_J_LOOP(j+1) \
        } \
    } 

 for (i = nab+1; i< nab+vab; i+=2) 
 {
     INNER_I_LOOP(i)
     INNER_I_LOOP(i+1)
 }

这里我已经将i,j和k循环展开了2倍,你会注意到最里面的循环代码不必重复8次(这就是你想要的如果我理解正确的话,请避免使用。)

另外你会注意到对于j和k循环,我正在检查索引是否超过数组边界。此检查会对性能产生轻微影响,我会让您优化。 ;)