[英]cl::vector vs std::vector: different iterator behaviour
EDIT: Added debugging output with memory locations as suggested by PlasmaHH. 编辑:根据PlasmaHH的建议添加了具有内存位置的调试输出。
I don't understand the different behaviour of the cl::vector<> in the C++ bindings for OpenCL. 我不明白在OpenCL的C ++绑定中cl :: vector <>的不同行为。 Consider the following code:
请考虑以下代码:
Header Top.hpp
: 标题
Top.hpp
:
class Top {
public:
void setBool(bool b);
bool getBool();
private:
bool status;
};
Source Top.cpp
: 来源
Top.cpp
:
#include "Top.hpp"
void Top::setBool(bool b) {
std::cout << (void*)this << " setBool("<< b<< ")\n";
status = b;
}
bool Top::getBool() {
std::cout << (void*)this << " getBool() returns " << status << std::endl;
return status;
}
Use the above: 使用以上:
#define __NO_STD_VECTOR
#include <iostream>
#include "CL/cl.hpp"
#include "Top.hpp"
using namespace cl;
using namespace std;
cl::vector<Top> js;
int main() {
js.push_back(Top());
js[0].setBool(true);
cout << js[0].getBool() << endl;
for(cl::vector<Top>::iterator i = js.begin(); i != js.end(); ++i) {
(*i).setBool(false);
}
cout << js[0].getBool() << endl;
}
With __NO_STD_VECTOR
the std::vector is overridden. 使用
__NO_STD_VECTOR
可以覆盖std :: vector。 The output is 输出是
0x6021c0 setBool(1)
0x6021c0 getBool() returns 1
0x7fffae671d60 setBool(0)
0x6021c0 getBool() returns 1
So the location returned by the iterator is definitely wrong. 所以迭代器返回的位置肯定是错误的。
Using the above with the std::vector
(and changing the namespaces to std
of course) however gives the expected output: 使用上面的
std::vector
(并将命名空间更改为std
)当然会给出预期的输出:
0x1be0010 setBool(1)
0x1be0010 getBool() returns 1
0x1be0010 setBool(0)
0x1be0010 getBool() returns 0
This iterator is acting differently, but it's supposed to replace the std::vector to avoid compatibility issues. 这个迭代器的行为不同,但它应该替换std :: vector以避免兼容性问题。 Am I missing something?
我错过了什么吗?
Not an expert at OpenCL by any stretch of the imagination, but I'm interested so I went over to CUDA/OpenCL Computing . 在任何想象中都不是OpenCL的专家,但我很感兴趣,所以我去了CUDA / OpenCL Computing 。 I appears that their * operator returns a copy rather than a reference:
我看来他们的*运算符返回一个副本而不是一个引用:
00706 T operator *()
00707 {
00708 return vec_[index_];
00709 }
Whereas the (first, non-const) vector [] operator returns a reference: 而(first,non-const)vector []运算符返回一个引用:
00621 T& operator[](int index)
00622 {
00623 return data_[index];
00624 }
00625
00626 T operator[](int index) const
00627 {
00628 return data_[index];
00629 }
Try iterating through the vector directly (using the old "int i = 0, ...") and see if that gives different results. 尝试直接迭代向量(使用旧的“int i = 0,...”)并查看是否给出了不同的结果。 If so, you might want to put in a bug report (check first) since this is unexpected behavior for the * operator.
如果是这样,您可能需要输入错误报告(先检查),因为这是*运算符的意外行为。
Judging from the addresses I suspect that this is a 64-bit build and that the cl vector's iterator's operator*
is returning by value rather than by reference, not allowing access to the original element. 从地址判断我怀疑这是一个64位构建,并且cl向量的迭代器的
operator*
是按值而不是通过引用返回,不允许访问原始元素。 As an experiment you could try using the ->
operator instead i->setBool(false);
作为实验,你可以尝试使用
->
运算符而不是i->setBool(false);
to see if that's implemented sanely. 看看是否实现了这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.