My Goal
I am using an OpenCL Wrapper for GPU processing in C++. This library creates memory objects as:
Memory<float>(gpuDevice, sizeOfMemoryObject)
I need to create several lists of these each within a vector<Memory<float>>
. It is not important to me whether anything is an object or pointer as long as it is smart, safe, and works. At no point am I intending or wanting to copy the actual objects around.
In simple terms, if it wasn’t for pointers issues (pseudo-code of C++/C#), what I would want to do (in the best words I can express it):
private:
//define the main objects/pointers I will access throughout my code
std::vector<Memory<float>> memoryVecA;
std::vector<Memory<float>> memoryVecB;
std::vector<Memory<float>> memoryVecC;
... //more, D, E, F, etc.
On initialization, I want to first add these to a temp parent vector. This is to allow iteration through them to initialize them and their contents:
//temporary list to iterate through just for construction
std::vector<std::vector<Memory<float>> tempMemoryVec;
//add the main objects to it
tempMemoryVec.emplace_back(memoryVecA);
tempMemoryVec.emplace_back(memoryVecB);
tempMemoryVec.emplace_back(memoryVecC);
Then right after that, I want to iterate through this temp list to fill it all in:
int numMemoriesPerVector = 10; // number of Memory<float>'s to add to each memoryVec
int numSamplesPerMemory = 100; // construction parameter for size of each Memory<float>
for (int i=0; i < tempMemoryVec.size(); i++){
//fill in memoryVecA/B/C by accessing them from the tempMemoryVec
for (int j=0; j < numMemoriesPerVector; j++){
//create the memory objects and add them to memoryVecA/B/C
Memory<float> newMemory = Memory<float>(gpuDevice, numSamplesPerMemory);
tempMemoryVec[i].emplace_back(newMemory); //add Memory<float> to vector<Memory<float>>
//set all memory values to zero
for (int k=0; k < numSamplesPerMemory; k++){
newMemory[k] = 0;
}
//copy data to GPU inside Memory<float> object (internal function of it)
newMemory.write_to_device();
}
}
tempMemoryList.clear(); //no longer need this - was just temporary list to make initialization easier
At the end, I need want my Vector<Memory<float>> memoryVecA
etc. (pointers or objects) where each Vector has 10 Memory<float>
entries, each of which is then filled with 100 zeros.
I then must access these Vectors and their contained Memory<float>
objects easily and routinely from there.
My Failed Attempts
I have spent hours today trying to make this work. I have tried all sorts of pointers, passing things in by reference, make_shared
, etc. but it is just getting so complicated with all the nested brackets and asterixes etc. I don’t have the knowledge of how it is working and what I am doing.
I am not sure when I am copying an object or just the pointer, etc. I keep just getting errors and I can’t spend another 2-3 days just failing on something trivial like this. The Memory objects are expensive so I can’t be copying them around unnecessarily and it will break function.
Is there anyone who would be very kind as to help me write some version of what I am trying to do above in a way that doesn’t become overly complicated but allows creation and initialization of the vector<Memory<float>>
objects I want (and/or some variant of pointers/objects for these?)
I often want to do something like this but then I struggle. C++ pointers and references have always been difficult for me. I’d appreciate some help. This is a common situation for me, so it would be good to learn from.
Thanks for any help.