EDIT: Added debugging output with memory locations as suggested by PlasmaHH.
I don’t understand the different behaviour of the cl::vector<> in the C++ bindings for OpenCL. Consider the following code:
Header Top.hpp:
class Top {
public:
void setBool(bool b);
bool getBool();
private:
bool status;
};
Source 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. 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:
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. 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. I appears that their * operator returns a copy rather than a reference:
Whereas the (first, non-const) vector [] operator returns a reference:
Try iterating through the vector directly (using the old “int i = 0, …”) and see if that gives different results. If so, you might want to put in a bug report (check first) since this is unexpected behavior for the * operator.