我最近做了一些挖掘内存以及如何正确使用它.当然,我也偶然发现了预取,以及如何让CPU更轻松.
我运行了一些基准测试,以了解正确存储/访问数据和指令的实际好处.这些基准测试不仅显示了帮助CPU预取的预期好处,还表明预取还可以在运行时加速进程.在大约100个程序循环之后,CPU似乎已经计算出来并相应地优化了缓存.这样每个周期可节省高达200.000个刻度,数量从750.000降至550.000.我使用qTestLib获得了这些数字.
现在问题:有没有一种安全的方法来使用这个运行时加速,让它热身,可以这么说?或者,根本不应该计算这个并且只是从启动时构建更快的代码?
我想数值计算numpy数组Y上的FFT.为了测试,我使用高斯函数Y = exp(-x ^ 2).(符号)傅立叶变换是Y'=常数*exp(-k ^ 2/4).
import numpy
X = numpy.arange(-100,100)
Y = numpy.exp(-(X/5.0)**2)
Run Code Online (Sandbox Code Playgroud)
天真的方法失败了:
from numpy.fft import *
from matplotlib import pyplot
def plotReIm(x,y):
f = pyplot.figure()
ax = f.add_subplot(111)
ax.plot(x, numpy.real(y), 'b', label='R()')
ax.plot(x, numpy.imag(y), 'r:', label='I()')
ax.plot(x, numpy.abs(y), 'k--', label='abs()')
ax.legend()
Y_k = fftshift(fft(Y))
k = fftshift(fftfreq(len(Y)))
plotReIm(k,Y_k)
Run Code Online (Sandbox Code Playgroud)
实数(Y_k)在正值和负值之间跳跃,这对应于跳跃阶段,这在符号结果中不存在.这当然是不可取的.(结果在技术上是正确的,因为abs(Y_k)给出了预期ifft(Y_k)为Y的幅度.)
这里,函数fftshift()使数组k单调递增并相应地改变Y_k.通过对两个向量应用此操作,不会更改对zip(k,Y_k).
此更改似乎可以解决此问题:
Y_k = fftshift(fft(ifftshift(Y)))
k = fftshift(fftfreq(len(Y)))
plotReIm(k,Y_k)
Run Code Online (Sandbox Code Playgroud)
如果需要单调Y和Y_k,这是使用fft()函数的正确方法吗?
以上的反向操作是:
Yx = fftshift(ifft(ifftshift(Y_k)))
x = fftshift(fftfreq(len(Y_k), k[1] - k[0]))
plotReIm(x,Yx)
Run Code Online (Sandbox Code Playgroud)
对于这种情况,文档清楚地指出Y_k必须与fft()和fftfreq()的输出进行排序,我们可以通过应用ifftshift()来实现.
那些问题一直困扰着我很长时间:fft()和ifft()的输出和输入数组是否总是如此a[0] should …
我使用Howard Hinnants date.h库编写了以下代码,以计算当前时间的一年中的小数天。我想知道是否有更短的方法,因为我的代码感觉像是对std::chrono和date调用的过度杀伤。我是否可以直接计算自年初以来的小数天数(以微秒为单位),并避免采用两步法?
#include <iostream>
#include <chrono>
#include "date.h"
int main()
{
// Get actual time.
auto now = std::chrono::system_clock::now();
// Get the number of days since start of the year.
auto ymd = date::year_month_day( date::floor<date::days>(now) );
auto ymd_ref = date::year{ymd.year()}/1/1;
int days = (date::sys_days{ymd} - date::sys_days{ymd_ref}).count();
// Get the fractional number of seconds of the day.
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(now - date::floor<date::days>(now));
double seconds_since_midnight = 1e-6*microseconds.count();
// Get fractional day number.
std::cout << "Fractional …Run Code Online (Sandbox Code Playgroud) bool如何在 C++ 中创建连续的数量向量?我读到了许多反对使用的警告std::vector<bool>,我想将掩码存储到std::vector<double>.
为了使我的目的更加明确,我想对向量中的那些真实std::vector<double>元素应用统计数据。bool为了以最佳性能做到这一点,我希望向量是连续的。
我有一个模板化的 C++ 函数,我希望能够使用其中的两种类型。由于Python不支持重载,我对如何解决这个问题有点困惑。我有一个.pyx如下所示。如何在floatand中使用 C++ 函数double?
import cython
import numpy as np
cimport numpy as np
# declare the interface to the C code
cdef extern from "diff_cpp.cpp" namespace "diff":
cdef void diff_cpp[float] (float* at, const float* a, const float visc,
const float dxidxi, const float dyidyi, const float dzidzi,
const int itot, const int jtot, const int ktot)
cdef extern from "diff_cpp.cpp" namespace "diff":
cdef void diff_cpp[double] (double* at, const double* a, const double …Run Code Online (Sandbox Code Playgroud) 为什么C++在Parent课前破坏了这个Child类?当一个对象超出范围首先破坏shared_ptrs然后自毁时,它是否更合乎逻辑?在我的工作流程中,这会导致问题,因为我的Parent类正在管理Child类使用的接口.
#include <iostream>
#include <memory>
class Child;
class Parent
{
public:
Parent() :
child(std::make_shared<Child>())
{
std::cout << "Constructing parent" << std::endl;
}
~Parent() { std::cout << "Destructing parent" << std::endl; }
private:
std::shared_ptr<Child> child;
};
class Child
{
public:
Child()
{
std::cout << "Constructing child" << std::endl;
}
~Child() { std::cout << "Destructing child" << std::endl; }
};
int main()
{
Parent parent;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑
根据评论,我觉得我的问题需要更多的解释.我的子类都被分配std::shared_ptr,当父类超出范围时释放.我的主程序是CUDA程序,父程序可以访问GPU设备.如果父项被删除,我将无法再访问GPU.然而,孩子们的析构函数需要释放他们的GPU内存,因此我希望在父母退出范围之前采取这一行动.但这意味着我必须手动删除智能指针,在我看来,这有点挫败了他们的目的.
所以我对 linspace 有一点问题。我想生成一个数字数组,例如:
[0.000001, 0.00001, 0.0001 , 0.001 ,0 .01 , 0.1]
所以我尝试了以下代码:
alphas = np.linspace(0.0000001,10,num=11)
print(alphas)
Run Code Online (Sandbox Code Playgroud)
并得到结果:
[ 1.00000000e-07 1.00000009e+00 2.00000008e+00 3.00000007e+00
4.00000006e+00 5.00000005e+00 6.00000004e+00 7.00000003e+00
8.00000002e+00 9.00000001e+00 1.00000000e+01]
Run Code Online (Sandbox Code Playgroud)
我认为这一定是显示和格式的问题,但经过尝试
if(alphas[0]>1):
print("yes the first number is greater than 1")
if(alphas[1]>1):
print("yes the second number is greater than 1")
Run Code Online (Sandbox Code Playgroud)
第二个数字确实大于一
所以我的问题是出了什么问题?因为 linspace 应该“返回指定间隔内均匀分布的数字”。
假设我有一个这样的 np.array :
data = np.array([[1,2],[2,4],[3,5],[4,5]])
Run Code Online (Sandbox Code Playgroud)
我想绘制从第一个点到第二个点然后从第二个点到第三个点的路径等等......我该如何管理。我知道如何链接所有点,但不知道如何使用指定的顺序。
我有一个函数,它的指针为doubleas 参数,被限定为restrict. 请注意,英特尔编译器使用restrict,但__restrict__在 GCC 的情况下,我们将限定符替换为。
void Stats::calc_flux_2nd(double* restrict data,
double* restrict datamean,
double* restrict w)
{
// ...
// Set a pointer to the field that contains w,
// either interpolated or the original
double* restrict calcw = w;
// ...
Run Code Online (Sandbox Code Playgroud)
使用 GCC 或 Clang 编译此代码没有任何问题,但 IBM BlueGene 编译器给出以下错误:
(W) Incorrect assignment of a restrict qualified pointer.
Only outer-to-inner scope assignments between restrict pointers are
allowed. This may result in incorrect program …Run Code Online (Sandbox Code Playgroud) 是否可以借助初始化程序列表或其他C++功能将两个初始化行合并为单个语句?向量值始终以1递增,但大小n不固定.
#include <numeric>
#include <vector>
#include <iostream>
int main()
{
int n = 10;
// Can the two lines below be combined into a single statement?
std::vector<int> v(n);
std::iota(v.begin(), v.end(), 1);
for (int i : v)
std::cout << i << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)