Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用不同常数制作多个版本函数的模板_C++_Templates_Optimization_Nested Loops - Fatal编程技术网

C++ C++;使用不同常数制作多个版本函数的模板

C++ C++;使用不同常数制作多个版本函数的模板,c++,templates,optimization,nested-loops,C++,Templates,Optimization,Nested Loops,我可以使用模板来创建某个函数的多个实例化,只在某些常量参数中有所不同吗?此参数的备选方案数是固定的。 例如 我不想重写(其中上限为1..32的二次幂) 变成一套 funct_with_upper_is_1(param) // upper =1 { manually_copied_code...heavy(1) } funct_with_upper_is_2(param) // upper =2 { manually_copied_code...heavy(2) } funct_with_up

我可以使用模板来创建某个函数的多个实例化,只在某些常量参数中有所不同吗?此参数的备选方案数是固定的。 例如

我不想重写(其中上限为1..32的二次幂)

变成一套

funct_with_upper_is_1(param) // upper =1
 { manually_copied_code...heavy(1) }
funct_with_upper_is_2(param) // upper =2
 { manually_copied_code...heavy(2) }
funct_with_upper_is_4(param) // upper =4
 { manually_copied_code...heavy(4) }
funct_with_upper_is_8(param) // upper =8
 { manually_copied_code...heavy(8) }
但是变成了模板

template<int upper>
 funct_with_fixed_upper(param)
 { the_original_code....heavy(upper) }
模板
具有固定上限的函数(参数)
{原始代码…重(上)}
然后

template<upper=1> funct_with_fixed_upper(param);
template<upper=2> funct_with_fixed_upper(param);
template<upper=4> funct_with_fixed_upper(param);
template<upper=8> funct_with_fixed_upper(param);
具有固定上限的模板函数(参数);
具有固定上限的模板函数(参数);
具有固定上限的模板函数(参数);
具有固定上限的模板函数(参数);

这是否可能与C++ TEMPALTES?

==详细模式打开==

<>我有很多C++文件,代码类似于

function_typical(long long double ***A, long long double ***B, int const_1, int const_2)
// the type "long long double" here is very correct, this is extension of compiler
{

   for(int i=1;i<100000-1;i++)
      for(int j=1;j<100000-1;j++)
         for(int k=0;k<const_1;k++)
            for(int l=k;l<const_2;l++) {
                // some cray work with array like
                A[i][j][l-k]+=(B[i][j][l-k]+A[i+1][j][l-k]+A[i][j+1][l-k]-A[i-1][j][k]-A[i][j-1][l-k]/2.2)/88.3;
                if(A[i][j][l-k]>sin_lld(A[i][j-1][l-k])){B[i][j][l-k]=A[i][j][k]*4;}
            }
 }
函数\u典型(长双精度***A,长双精度***B,整数常数1,整数常数2)
//这里的“long-long-double”类型非常正确,这是编译器的扩展
{

对于(int i=1;i绝对而言,这是完全可能的。如果按模板获取int,则它在常量表达式有效的任何地方都有效

template<int const_1, int const_2> function_typical(long long double ***A, long long double ***B)
// the type "long long double" here is very correct, this is extension of compiler
{

   for(int i=1;i<100000-1;i++)
      for(int j=1;j<100000-1;j++)
         for(int k=0;k<const_1;k++)
            for(int l=k;l<const_2;l++) {
                // some cray work with array like
                A[i][j][l-k]+=(B[i][j][l-k]+A[i+1][j][l-k]+A[i][j+1][l-k]-A[i-1][j][k]-A[i][j-1][l-k]/2.2)/88.3;
                if(A[i][j][l-k]>sin_lld(A[i][j][l-k])){B[i][j][l-k]=A[i][j][k]*4;}
            }
 }
模板功能\u典型(长双***A、长双***B)
//这里的“long-long-double”类型非常正确,这是编译器的扩展
{

对于(int i=1;i绝对而言,这是完全可能的。如果按模板获取int,则它在常量表达式有效的任何地方都有效

template<int const_1, int const_2> function_typical(long long double ***A, long long double ***B)
// the type "long long double" here is very correct, this is extension of compiler
{

   for(int i=1;i<100000-1;i++)
      for(int j=1;j<100000-1;j++)
         for(int k=0;k<const_1;k++)
            for(int l=k;l<const_2;l++) {
                // some cray work with array like
                A[i][j][l-k]+=(B[i][j][l-k]+A[i+1][j][l-k]+A[i][j+1][l-k]-A[i-1][j][k]-A[i][j-1][l-k]/2.2)/88.3;
                if(A[i][j][l-k]>sin_lld(A[i][j][l-k])){B[i][j][l-k]=A[i][j][k]*4;}
            }
 }
模板功能\u典型(长双***A、长双***B)
//这里的“long-long-double”类型非常正确,这是编译器的扩展
{

对于(int i=1;i提供了您的原始模板:

template<int upper> 
  funct_with_fixed_upper(param)
  { the_original_code....heavy(upper) }
模板
具有固定上限的函数(参数)
{原始代码…重(上)}
那么当你调用它时,你会这样做:

 funct_with_fixed_upper<1>(param);
 funct_with_fixed_upper<2>(param);`
 template<> funct_with_fixed_upper<1>(param) { // code here };
具有固定上限的函数(参数); 具有固定上限的函数(参数)`

如果您需要根据常量专门化其中任何一个,您可以这样做:

 funct_with_fixed_upper<1>(param);
 funct_with_fixed_upper<2>(param);`
 template<> funct_with_fixed_upper<1>(param) { // code here };
template funct_,带有固定的上(参数){//code here};

正如其他人已经说过的,这将简化代码维护,但不会真正减少已编译代码的大小,因为编译器仍会将其扩展…

提供原始模板:

template<int upper> 
  funct_with_fixed_upper(param)
  { the_original_code....heavy(upper) }
模板
具有固定上限的函数(参数)
{原始代码…重(上)}
那么当你调用它时,你会这样做:

 funct_with_fixed_upper<1>(param);
 funct_with_fixed_upper<2>(param);`
 template<> funct_with_fixed_upper<1>(param) { // code here };
具有固定上限的函数(参数); 具有固定上限的函数(参数)`

如果您需要根据常量专门化其中任何一个,您可以这样做:

 funct_with_fixed_upper<1>(param);
 funct_with_fixed_upper<2>(param);`
 template<> funct_with_fixed_upper<1>(param) { // code here };
template funct_,带有固定的上(参数){//code here};


正如其他人已经说过的,这将简化代码维护,但不会真正减少已编译代码的大小,因为编译器仍会将其扩展…

“更改调用站点”这不是个问题,但“所有翻译单元的完整源代码”是个问题。这类典型函数几乎有1兆字节。好消息是,所有调用都来自一组非常小的网络接口函数,每个函数只使用部分典型函数。如果由于重复,您有1兆字节的这类“典型”函数,模板方法将简化维护和代码库大小,但不简化编译代码大小-模板实例化将扩展到相同大小。如果“函数”为“典型”外部循环编号始终相同,内部循环编号基于模板,最内部的工作由函数定义,您也可以对函数调用进行模板化,从而进一步减少维护。但是,它仍然具有相同的总体编译大小。或者,您有一个带有“可变常量”的函数或者您必须生成所有可能的函数。无论是由您手动完成还是由编译器使用模板自动完成,它们都必须存在。您可以同时拥有这两个功能。“更改调用站点”这不是个问题,但“所有翻译单元的完整源代码”是个问题。这类典型函数几乎有1兆字节。好消息是,所有调用都来自一组非常小的网络接口函数,每个函数只使用部分典型函数。如果由于重复,您有1兆字节的这类“典型”函数,模板方法将简化维护和代码库大小,但不简化编译代码大小-模板实例化将扩展到相同大小。如果“函数”为“典型”外部循环编号始终相同,内部循环编号基于模板,最内部的工作由函数定义,您也可以对函数调用进行模板化,从而进一步减少维护。但是,它仍然具有相同的总体编译大小。或者,您有一个带有“可变常量”的函数或者你必须生成所有可能的函数。无论是由你手动完成的,还是由编译器使用模板自动完成的,它们都必须存在。两者都可以。UPD:所有典型函数的总大小为2MB,但每个函数的中位数约为10KB。你所要求的更改将有助于提高性能,但这并不会影响你的工作。正如w但是,每个数组访问需要3个间接寻址,每个维度一个。如果您在某个维度中进行顺序访问,编译器可能会为您取消一些取消引用,但您确实应该停止对内存的纠缠。关于模板基础的好幻灯片www.jonhoyle.com/Presentations/PDFs/templates.pdfNovelocrat,我有A和B l此外,内部工作是在少量数组上完成的:a[i][j]、B[i][j]、a[i][j+1]等等,每一个数组都是逐字节存储的;嵌套循环在访问内存或对指针时有小问题(在5-6个指针上只有指针算术,而我的arch有十几个寄存器来存储这些指针)。问题实际上在于优化编译器中的2个嵌套循环;这一点已被几位专家证明适用于此代码。Novelocrat、triple Indection由cach避免