使用相同编译器标志(或者)编译的以下代码clang运行速度几乎快60倍:gcc-O2-O3
#include <iostream>
#include <math.h>
#include <chrono>
#include <limits>
long double func(int num)
{
long double i=0;
long double k=0.7;
for(int t=1; t<num; t++){
for(int n=1; n<16; n++){
i += pow(k,n);
}
}
return i;
}
int main()
{
volatile auto num = 3000000; // avoid constant folding
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
auto i = func(num);
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed = end-start;
std::cout.precision(std::numeric_limits<long double>::max_digits10);
std::cout << "Result " << i << std::endl; …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用SSE,我很困惑如何获得a的最大整数值(max)__m128i.例如:
__m128i t = _mm_setr_ps(0,1,2,3);
// max(t) = 3;
Run Code Online (Sandbox Code Playgroud)
搜索引导我去MAXPS指导,但我似乎无法找到如何使用它"xmmintrin.h".
此外,是否有任何"xmmintrin.h"您建议的文档,而不是查看头文件本身?
我在使用时遇到对齐问题 ymm寄存器,一些代码片段对我来说似乎很好.这是一个最小的工作示例:
#include <iostream>
#include <immintrin.h>
inline void ones(float *a)
{
__m256 out_aligned = _mm256_set1_ps(1.0f);
_mm256_store_ps(a,out_aligned);
}
int main()
{
size_t ss = 8;
float *a = new float[ss];
ones(a);
delete [] a;
std::cout << "All Good!" << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当然,sizeof(float)是4在我的架构(英特尔(R)至强(R)CPU E5-2650 V2 @ 2.60GHz),我与编译gcc使用-O3 -march=native标志.当然,错误会随着未对齐的内存访问而消失,即指定_mm256_storeu_ps.我在xmm寄存器上也没有这个问题,即
inline void ones_sse(float *a)
{
__m128 out_aligned = _mm_set1_ps(1.0f);
_mm_store_ps(a,out_aligned);
}
Run Code Online (Sandbox Code Playgroud)
我做了什么愚蠢的事吗?解决这个问题的方法是什么?
我正在创建一些函数来做一些事情,比如负数和正数的分离和,kahan,成对和其他东西,其中我从矩阵中获取元素的顺序无关紧要,例如:
template <typename T, int R, int C>
inline T sum(const Eigen::Matrix<T,R,C>& xs)
{
T sumP(0);
T sumN(0);
for (size_t i = 0, nRows = xs.rows(), nCols = xs.cols(); i < nRows; ++i)
for (size_t j = 0; j < nCols; ++j)
{
if (xs(i,j)>0)
sumP += xs(i,j);
else if (xs(i,j)<0) //ignore 0 elements: improvement for sparse matrices I think
sumN += xs(i,j);
}
return sumP+sumN;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想尽可能提高效率,所以我的问题是,如上所述循环遍历每一行的每一列会更好,或者像下面这样做相反:
for (size_t i = 0, nRows = xs.rows(), nCols = xs.cols(); i …Run Code Online (Sandbox Code Playgroud) 我只是在使用递归函数C++,Fortran并且我意识到一个简单的递归函数Fortran几乎是它的等效C++函数的两倍.现在,在进入这个之前,我知道这里有类似的问题,具体来说:
不过,我有点更加具体和困惑的Fortran编译器似乎是在做什么,你可以实现asm volatile在gcc.为了给你一些上下文,让我们考虑以下递归Fibonacci number实现:
Fortran代码:
module test
implicit none
private
public fib
contains
! Fibonacci function
integer recursive function fib(n) result(r)
integer, intent(in) :: n
if (n < 2) then
r = n
else
r = fib(n-1) + fib(n-2)
end if
end function ! end of Fibonacci function
end module
program fibonacci
use test, only: fib
implicit …Run Code Online (Sandbox Code Playgroud) 给定具有可变参数的参数包,如何找到包中唯一值的数量.我正在寻找一些类似的东西
no_of_uniques<0,1,2,1,2,2>::value // should return 3
Run Code Online (Sandbox Code Playgroud)
我的基本实现看起来像这样
template <size_t ... all>
struct no_of_uniques;
// this specialisation exceeds -ftemplate-depth as it has no terminating condition
template <size_t one, size_t ... all>
struct no_of_uniques<one,all...> {
static const size_t value = no_of_uniques<one,all...>::value;
};
template <size_t one, size_t two, size_t three>
struct no_of_uniques<one,two,three> {
static const size_t value = (one==two && one==three && two==three) ? 1:
(one!=two && two==three) ? 2:
(one==two && one!=three) ? 2:
(one==three && two!=three) ? 2: 3;
};
template …Run Code Online (Sandbox Code Playgroud) 这是我求解线性系统方程的 4 个函数的 python 代码:
def inverse_solution(A, B):
inv_A = scipy.linalg.inv(A)
return [numpy.dot(inv_A, b) for b in B]
def scipy_standart_solution(A, B):
return [scipy.linalg.solve(A, b) for b in B]
def cholesky_solution(A, B):
K = scipy.linalg.cholesky(A, lower = True)
t_K = K.T
return [scipy.linalg.solve_triangular(t_K, scipy.linalg.solve_triangular(K, b, lower = True)) for b in B]
def scipy_cholesky_solution(A, B):
K = scipy.linalg.cho_factor(A)
return [scipy.linalg.cho_solve(K, b) for b in B]
Run Code Online (Sandbox Code Playgroud)
我知道第一个解决方案效率不高,如果中的元素数量b小,则第二个解决方案很好,如果b大,则解决方案 3 和 4 很好。
但我的测试结果恰恰相反
A = numpy.array([[1,0.000000001],[0.000000001,1]])
for length in …Run Code Online (Sandbox Code Playgroud)