Cl :: vector vs std :: vector: behavior of different iterators

EDIT: Added debug output with memory cells, as suggested by PlasmaHH.

I don't understand the different behavior of cl :: vector <> in C ++ bindings for OpenCL. Consider the following code:

Top.hpp header:

 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 redefinition of std :: vector. Output signal

 0x6021c0 setBool(1) 0x6021c0 getBool() returns 1 0x7fffae671d60 setBool(0) 0x6021c0 getBool() returns 1 

So the place returned by the iterator is definitely wrong.

Using the above with std::vector (and, of course, changing namespaces to std ), however, gives the expected result:

 0x1be0010 setBool(1) 0x1be0010 getBool() returns 1 0x1be0010 setBool(0) 0x1be0010 getBool() returns 0 

This iterator acts differently, but it should replace std :: vector to avoid compatibility issues. Did I miss something?

+4
source share
2 answers

Not an expert in OpenCL on any part of the imagination, but I'm curious, so I moved on to CUDA / OpenCL Computing . I see that their * operator returns a copy, not a link:

 00706 T operator *() 00707 { 00708 return vec_[index_]; 00709 } 

While the operator (first, not const const) vector [] returns a link:

 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 it gives different results. If so, you can enable the error report (check first), as this is an unexpected behavior for the * operator.

+4
source

Judging by the addresses, I suspect that this is a 64-bit assembly and that the cl operator* vector iterator is returned by value, not by reference, not allowing access to the source element. As an experiment, you can try using -> instead of i->setBool(false); to make sure that this is implemented.

+1
source

Source: https://habr.com/ru/post/1394003/


All Articles