我试图平行我正在使用的程序并得到以下问题.如果多个线程需要在同一个向量上读取/写入但向量的不同元素,我会失去性能吗?我感觉这就是我的程序在平行化时几乎没有得到更快的原因.请使用以下代码:
#include <vector>
int main(){
vector<double> numbers;
vector<double> results(10);
double x;
//write 10 values in vector numbers
for (int i =0; i<10; i++){
numbers.push_back(cos(i));
}
#pragma omp parallel for \
private(x) \
shared(numbers, results)
for(int j = 0; j < 10; j++){
x = 2 * numbers[j] + 5;
#pragma omp critical // do I need this ?
{
results[j] = x;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显然,实际的程序执行的操作要昂贵得多,但这个例子只能解释我的问题.那么for循环可以快速完全并行完成,还是不同的线程必须等待彼此,因为一次只有一个线程可以访问矢量号,尽管它们都是读取矢量的不同元素?
与写操作相同的问题:我是否需要关键编译指示或者没有问题,因为每个线程写入向量结果的不同元素?我很满意我能得到的每一个帮助,也很高兴知道是否有更好的方法来做到这一点(也许根本不使用矢量,但简单的数组和指针等?)我也读过矢量不是在某些情况下线程安全,建议使用指针:OpenMP和STL向量
非常感谢你的帮助!
我正在使用Google的C++测试框架Gtest.我想针对其执行时间测试函数,例如函数foo()如果执行时间超过3ms则失败.我找不到ASSERT语句来实现这一目标.gtest不包含这样的功能吗?
我在Linux下有一个小的C++项目.当我尝试使用gdb调试可执行文件时,我收到以下错误:
../../gdb/dwarf2read.c:16760: internal-error: follow_die_offset:
Assertion 'dwarf2_per_objfile->reading_partial_symbols' failed.
A problem internal to GDB has been detected,
further debugging may prove unreliable.
Run Code Online (Sandbox Code Playgroud)
我已经将项目强烈简化为以下代码并仍然得到相同的错误:
BH:
#ifndef B_H_
#define B_H_
#include <vector>
class B {
public:
B();
std::vector<double> p;
};
#endif /* B_H_ */
Run Code Online (Sandbox Code Playgroud)
B.cpp:
#include "B.h"
B::B() {}
Run Code Online (Sandbox Code Playgroud)
DH:
#ifndef D_H_
#define D_H_
#include "E.h"
class D: public E {
public:
D();
};
#endif /* D_H_ */
Run Code Online (Sandbox Code Playgroud)
D.cpp:
#include "D.h"
D::D() : E() {}
Run Code Online (Sandbox Code Playgroud)
诶:
#ifndef E_H_
#define E_H_
#include <functional>
class E …Run Code Online (Sandbox Code Playgroud) 我有一个全类,它通过unique_ptr持有类Part的对象。由于我没有为Whole提供复制构造函数,因此由于unique_ptr成员而删除了复制构造函数。这是代码:
class Part {
};
class Whole {
public:
Whole(std::unique_ptr<Part> part) : part(std::move(part)) {
}
private:
const std::unique_ptr<Part> part; //yields error later!
//std::unique_ptr<Part> part; //ok!
};
Run Code Online (Sandbox Code Playgroud)
我想通过工厂函数build()创建一个Whole实例。
Whole build() {
auto part = std::unique_ptr<Part>{new Part{}};
return Whole{std::move(part)};
}
Run Code Online (Sandbox Code Playgroud)
我想这样使用:
int main() {
auto whole = build();
}
Run Code Online (Sandbox Code Playgroud)
只要未将Whole的Part成员的unique_ptr声明为const,此方法就起作用。据我了解,这是由于返回值优化导致无法创建和复制临时文件。但是,如果我将Whole :: part声明为const,我的编译器会抱怨删除的copy-constructor被调用。为什么不能使用const声明,或者此代码仍然存在问题?
我正在使用GNU编译器版本:(Ubuntu 4.8.4-2ubuntu1〜14.04.1)4.8.4
我想做以下事情.但不知道如何:
//have two vectors: vector1 (full of numbers), vector2 (empty)
//with vectors, I mean STL vectors
//outer loop
{
//inner loop
{
//vector2 gets written more and more over iterations of inner loop
//elements of vector1 are needed for this
} //end of inner loop
//now data of vector1 is not needed anymore and vector2 takes the role of
//vector 1 in the next iteration of the outer loop
//old approach (costly):
//clear vector1 ,copy vector2's data to vector1, clear …Run Code Online (Sandbox Code Playgroud) c++ ×5
stl ×2
assert ×1
gdb ×1
googletest ×1
linux ×1
openmp ×1
performance ×1
reference ×1
rename ×1
swap ×1
unique-ptr ×1