正如标题所示.我需要做这样的大量计算:
re = (x^a + y^a + z^a)^(1/a).
Run Code Online (Sandbox Code Playgroud)
其中{ x,y,z }> = 0.更具体地,a是正浮点常数,x,y,z是浮点数.这^是一个取幂运算符.
目前,我不想使用SIMD,但希望有其他一些技巧来加速它.
static void heavy_load(void) {
static struct xyz_t {
float x,y,z;
};
struct xyz_t xyzs[10000];
float re[10000] = {.0f};
const float a = 0.2;
/* here fill xyzs using some random positive floating point values */
for (i = 0; i < 10000; ++ i) {
/* here is what we need to …Run Code Online (Sandbox Code Playgroud) 我使用 clang-format 作为我的代码库的自动格式化工具。但它的一些功能让我很烦恼。
例如,我不希望它格式化我的宏定义,因为在大多数情况下,手动格式化它们更清晰。但我不知道如何以 clang 格式禁用它。
另一个小问题是指针对齐。有时,很明显让它左对齐,有时是右对齐。所以我宁愿自己动手。但是从 clang-format 中禁用它似乎是不可能的?
对这些问题有什么帮助吗?
我有一个二进制图像,二进制值是0或255.图像数据的类型是unsigned char.在这里,我需要对此图像进行中值滤波.
我认为使用直方图找到中位数应该很快.使用一些代码来解释:
unsigned int hist[2] = {0, 0};
for (int i = 0; i < kernel_h; ++i) {
for (int j = 0; j < kernel_w; ++j) {
if (image(i,j) == 0) {
hist[0]++;
}
else {
hist[1]++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我们可以非常快地得到中值.但由于这种情况,代码仍然可以改进:
int counter = 0;
for (int i = 0; i < kernel_h; ++i) {
for (int j = 0; j < kernel_w; ++j) {
if (image(i,j) == 0) { …Run Code Online (Sandbox Code Playgroud) 首先,我得到了一个N*N距离矩阵,对于每个点,我计算了它的最近邻居,所以我们有一个N*2矩阵,看起来像这样:
Run Code Online (Sandbox Code Playgroud)0 -> 1 1 -> 2 2 -> 3 3 -> 2 4 -> 2 5 -> 6 6 -> 7 7 -> 6 8 -> 6 9 -> 8
第二列是最近邻居的索引.所以这是一种特殊的有向图,每个顶点都有,只有一个外度.
当然,我们可以先将N*2矩阵转换为标准图形表示,然后执行BFS/DFS以获取连接的组件.
但是,鉴于这个特殊图表的特点,还有其他快速的方法来完成这项工作吗?
我将非常感激.
更新:
我在这里为这种情况 实现了一个简单的算法.
看,我没有使用union-find算法,因为数据结构可能会让事情变得那么容易,我怀疑它是否是我案例中最快的方法(我的意思是实际上).
您可能会认为_merge过程可能很耗时,但如果我们在分配新标签时将边缘交换到连续位置,则合并可能成本很低,但需要另外N个空格来跟踪原始索引.
以下是代码,可以正常工作g++,但在下面给出错误VC++ 2014:
template <class A>
struct Expression
{
public:
static const int status = A::status_;
};
struct Foo : public Expression<Foo>
{
static const int status_ = 0;
};
int main(void) {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么?谢谢!
错误消息是:
错误C2039:'status_':不是'Foo'的成员
错误C2065:'status_':未声明的标识符
错误C2131:表达式未计算为常量
在这里,我尝试使用一些伪代码自我解释CUDA启动参数模型(或执行配置模型),但我不知道是否存在一些重大错误,所以希望有人帮助查看它,并给我一些建议.谢谢先进.
这里是:
/*
normally, we write kernel function like this.
note, __global__ means this function will be called from host codes,
and executed on device. and a __global__ function could only return void.
if there's any parameter passed into __global__ function, it should be stored
in shared memory on device. so, kernel function is so different from the *normal*
C/C++ functions. if I was the CUDA authore, I should make the kernel function more
different from a normal …Run Code Online (Sandbox Code Playgroud) 这是代码:
char s[8];
int i = 0;
void **p = &s;
for (; i < 8; ++i) {
putchar( ((char*)(*p))[i] );
}
Run Code Online (Sandbox Code Playgroud)
上面的代码可以工作,但给了一些垃圾字符.所以我做的只是初始化s[8]:
char s[8] = {0};
Run Code Online (Sandbox Code Playgroud)
然后分段错误,但如果我使用VC++编译并运行它,它工作正常.奇怪的.有人可以解释一下吗?谢谢!
更新:很多人说上面的代码是愚蠢的......这个更新适合你.原始代码的样子:
static void*
copy_and_move(void **dst, int dsz, const void **src, int ssz) {
const int sz = ssz > dsz ? dsz : ssz;
memcpy(*dst, *src, sz);
return *dst + sz;
}
Run Code Online (Sandbox Code Playgroud)
然后是主叫代码:
char d[10], s[8];
copy_and_move(&d, sizeof(char) * 10, &s, sizeof(char) * 8);
Run Code Online (Sandbox Code Playgroud) 我正在尝试做这样的事情:
struct Foo {
int _val;
Foo(int v) : _val(v){}
};
struct Bar {
const std::string &_name;
Bar(const std::string &name) : _name(name) {}
};
template<typename T>
struct Universal {
T _t;
Universal(...) : _t(...) {}
};
// I want to use Universal for Foo abd Bar in the same way:
Universal<Foo> UF(9); // 9 is for Foo
Universal<Bar> UB("hello"); // "hello" is for bar
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我想将Universal的构造函数中的所有参数转发给T的构造函数。
我怎样才能做到呢?
c++ ×5
c ×4
optimization ×2
algorithm ×1
clang-format ×1
cuda ×1
gcc ×1
graph-theory ×1
math ×1