I have a question regarding proper coding standard related to classes that needs to maintain an internal concurrency::array.
I want to be able to send a class over to the GPU such that I can use it in my p_f_e lambda. My problem is in order to follow proper object oriented coding standards, the class should maintain the internal array (creation and destruction).
I have provided a very simple code sample to show what I'm trying to achieve.
class DeviceArrayCollection { private: int _dim1; int _dim2; int _dim3; array<unsigned int,1>& _deviceArrays; array<unsigned int,1>& allocateArray(int dim1, int dim2, int dim3) { return *(new array<unsigned int,1>(((dim1 * dim2 * dim3)+3)/4)); } public: DeviceArrayCollection(int dim1, int dim2, int dim3) : _deviceArrays(allocateArray(dim1, dim2, dim3)), _dim1(dim1), _dim2(dim2), _dim3(dim3) { } ~DeviceArrayCollection(void) { delete &_deviceArrays; } int GetDim1() const { return _dim1; } int GetDim2() const { return _dim2; } int GetDim3() const { return _dim3; } void SetData(unsigned char* images) { copy(reinterpret_cast<unsigned int*>(images), _deviceArrays); }
unsigned int operator () (int dim1, int dim2, int dim3) const restrict(cpu,amp) { int idx = dim3 + dim2 * _dim3 + dim1 * _dim2 * _dim3; return (_deviceArrays[idx >> 2] & (0xFF << ((idx & 0x3) << 3))) >> ((idx & 0x3) << 3); } };
The class is now callable from the code by:
DeviceArrayCollection dA(10,1000,1000);
And then can be passed to the lambda and used inside the lambda (e.g. reading a value from a 3D index)
unsigned int readVal = dA(2,55,66);
The above code compiles and works as expected, but it's not proper coding practice. See e.g.http://stackoverflow.com/questions/3233987/deleting-a-reference
So what I'am wondering about is how I should do it correctly? The problem is of course that a class passed to ap_f_e lambda cannot contain a pointer to a concurrency::array, only a reference. And also, I don't want to create the actual array on the outside of the class and then send it as a reference into the class. I want the class to be able to maintain the array, and hide how it is constructed from the user.
Any suggestions? How can this be handled better?