feat: Implement a custom MetaAllocator for uLib::Vector to enable GPU memory management and integrate CUDA support into the build system.
This commit is contained in:
@@ -20,6 +20,7 @@ set( TESTS
|
||||
TypeIntrospectionTraversal
|
||||
OptionsTest
|
||||
PingPongTest
|
||||
VectorMetaAllocatorTest
|
||||
)
|
||||
|
||||
set(LIBRARIES
|
||||
|
||||
71
src/Core/testing/VectorMetaAllocatorTest.cpp
Normal file
71
src/Core/testing/VectorMetaAllocatorTest.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/*//////////////////////////////////////////////////////////////////////////////
|
||||
// CMT Cosmic Muon Tomography project //////////////////////////////////////////
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Copyright (c) 2014, Universita' degli Studi di Padova, INFN sez. di Padova
|
||||
All rights reserved
|
||||
|
||||
Authors: Andrea Rigoni Garola < andrea.rigoni@pd.infn.it >
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
#include "testing-prototype.h"
|
||||
#include <Core/Vector.h>
|
||||
|
||||
int main() {
|
||||
BEGIN_TESTING(VectorMetaAllocator);
|
||||
|
||||
uLib::Vector<int> v;
|
||||
std::cout << "Pushing elements...\n";
|
||||
v << 1, 2, 3, 4, 5;
|
||||
|
||||
std::cout << "Initial RAM contents: ";
|
||||
for (size_t i = 0; i < v.size(); ++i) {
|
||||
std::cout << v[i] << " ";
|
||||
if (v[i] != (int)(i + 1)) {
|
||||
std::cout << "\nError: Value mismatch at index " << i << "\n";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
std::cout << "\n";
|
||||
|
||||
#ifdef USE_CUDA
|
||||
std::cout << "Moving to VRAM...\n";
|
||||
v.MoveToVRAM();
|
||||
|
||||
int *vram_ptr = v.GetVRAMData();
|
||||
if (vram_ptr) {
|
||||
std::cout << "Successfully obtained VRAM pointer: " << vram_ptr << "\n";
|
||||
} else {
|
||||
std::cout << "Error: Failed to obtain VRAM pointer!\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
std::cout << "Moving back to RAM...\n";
|
||||
v.MoveToRAM();
|
||||
|
||||
std::cout << "RAM contents after VRAM trip: ";
|
||||
for (size_t i = 0; i < v.size(); ++i) {
|
||||
std::cout << v[i] << " ";
|
||||
if (v[i] != (int)(i + 1)) {
|
||||
std::cout << "\nError: Data corrupted after RAM->VRAM->RAM trip at index "
|
||||
<< i << "\n";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
std::cout << "\n";
|
||||
#else
|
||||
std::cout << "USE_CUDA not defined, skipping VRAM tests.\n";
|
||||
#endif
|
||||
|
||||
std::cout << "Scaling vector...\n";
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
v[i] *= 10;
|
||||
|
||||
std::cout << "Final contents: ";
|
||||
for (size_t i = 0; i < v.size(); ++i)
|
||||
std::cout << v[i] << " ";
|
||||
std::cout << "\n";
|
||||
|
||||
END_TESTING;
|
||||
}
|
||||
@@ -23,62 +23,47 @@
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////*/
|
||||
|
||||
|
||||
|
||||
#include <Core/Vector.h>
|
||||
#include "testing-prototype.h"
|
||||
#include <Core/Vector.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
template < typename T >
|
||||
struct __Cmp {
|
||||
bool operator()(const T &data, const float value) {
|
||||
return data <= value;
|
||||
}
|
||||
template <typename T> struct __Cmp {
|
||||
bool operator()(const T &data, const float value) { return data <= value; }
|
||||
};
|
||||
|
||||
template <typename _Tp, typename _CmpT>
|
||||
inline const unsigned long VectorSplice(const _Tp &_it, const _Tp &_end,
|
||||
const float value, _CmpT _comp) {
|
||||
|
||||
|
||||
|
||||
template<typename _Tp, typename _CmpT>
|
||||
inline const unsigned long
|
||||
VectorSplice(const _Tp &_it, const _Tp &_end, const float value, _CmpT _comp)
|
||||
{
|
||||
|
||||
_Tp it = _it;
|
||||
_Tp end = _end-1;
|
||||
for(it; it != end; )
|
||||
{
|
||||
if ( _comp(*it, value) ) it++;
|
||||
else if( _comp(*end, value) )
|
||||
{
|
||||
std::swap(*it,*end--);
|
||||
}
|
||||
else --end;
|
||||
}
|
||||
return it - _it;
|
||||
_Tp it = _it;
|
||||
_Tp end = _end - 1;
|
||||
for (it; it != end;) {
|
||||
if (_comp(*it, value))
|
||||
it++;
|
||||
else if (_comp(*end, value)) {
|
||||
std::swap(*it, *end--);
|
||||
} else
|
||||
--end;
|
||||
}
|
||||
return it - _it;
|
||||
}
|
||||
|
||||
int main() {
|
||||
BEGIN_TESTING(Vector);
|
||||
|
||||
int main()
|
||||
{
|
||||
BEGIN_TESTING(Vector);
|
||||
uLib::Vector<float> v;
|
||||
v << 5, 4, 3, 2, 6, 1, 2, 3, 65, 7, 32, 23, 4, 3, 45, 4, 34, 3, 4, 4, 3, 3, 4,
|
||||
2, 2, 3;
|
||||
|
||||
uLib::Vector<float> v;
|
||||
v << 5,4,3,2,6,1,2,3,65,7,32,23,4,3,45,4,34,3,4,4,3,3,4,2,2,3;
|
||||
int id = ::VectorSplice(v.begin(), v.end(), 3, __Cmp<float>());
|
||||
|
||||
std::cout << "id: " << id << "\n";
|
||||
std::cout << "vector: ";
|
||||
for (uLib::Vector<float>::Iterator it = v.begin(); it != v.end(); it++)
|
||||
std::cout << *it << " ";
|
||||
std::cout << std::endl;
|
||||
// std::sort(v.begin(),v.end(),LT<float>());
|
||||
|
||||
|
||||
int id = VectorSplice(v.begin(),v.end(),3,__Cmp<float>());
|
||||
|
||||
std::cout << "id: " << id << "\n";
|
||||
std::cout << "vector: ";
|
||||
for(uLib::Vector<float>::Iterator it = v.begin(); it!=v.end(); it++)
|
||||
std::cout << *it <<" ";
|
||||
std::cout << std::endl;
|
||||
// std::sort(v.begin(),v.end(),LT<float>());
|
||||
|
||||
|
||||
END_TESTING;
|
||||
END_TESTING;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user