-
Notifications
You must be signed in to change notification settings - Fork 4
Overloading functions to allow custom allocators #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MikaelHuppunen
wants to merge
5
commits into
kstppd:master
Choose a base branch
from
MikaelHuppunen:overload
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
169de66
Overloading functions to allow custom allocators
MikaelHuppunen da7f0ec
Using default allocator as a default argument in template
MikaelHuppunen 615b048
Added unit test for custom allocator
MikaelHuppunen 2c677ce
updated unit test to correct input type
MikaelHuppunen 3b72b9b
Custom allocator test now only tests things that were actually implem…
MikaelHuppunen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| #include <iostream> | ||
| #include <stdlib.h> | ||
| #include <chrono> | ||
| #include <limits> | ||
| #include <random> | ||
| #include <gtest/gtest.h> | ||
| #include "../../include/splitvector/splitvec.h" | ||
| #include "../../include/splitvector/split_tools.h" | ||
| #include "../../include/common.h" | ||
| #include "../../include/splitvector/archMacros.h" | ||
| #define expect_true EXPECT_TRUE | ||
| #define expect_false EXPECT_FALSE | ||
| #define expect_eq EXPECT_EQ | ||
| #define TARGET 1 | ||
|
|
||
| /** | ||
| * @brief Custom allocator for unified memory (GPU and CPU accessible). | ||
| * | ||
| * This class provides an allocator for unified memory, which can be accessed | ||
| * by both the GPU and the CPU. It allocates and deallocates memory using split_gpuMallocManaged | ||
| * and split_gpuFree functions, while also providing constructors and destructors for objects. | ||
| * | ||
| * @tparam T Type of the allocated objects. | ||
| */ | ||
| template <class T> | ||
| class customAllocator { | ||
| public: | ||
| typedef T value_type; | ||
| typedef value_type* pointer; | ||
| typedef const value_type* const_pointer; | ||
| typedef value_type& reference; | ||
| typedef const value_type& const_reference; | ||
| typedef ptrdiff_t difference_type; | ||
| typedef size_t size_type; | ||
| template <class U> | ||
| struct rebind { | ||
| typedef customAllocator<U> other; | ||
| }; | ||
| /** | ||
| * @brief Default constructor. | ||
| */ | ||
| customAllocator() throw() {} | ||
|
|
||
| /** | ||
| * @brief Copy constructor with different type. | ||
| */ | ||
| template <class U> | ||
| customAllocator(customAllocator<U> const&) throw() {} | ||
| pointer address(reference x) const { return &x; } | ||
| const_pointer address(const_reference x) const { return &x; } | ||
|
|
||
| pointer allocate(size_type n, const void* /*hint*/ = 0) { | ||
| T* ret; | ||
| assert(n && "allocate 0"); | ||
| SPLIT_CHECK_ERR(split_gpuMallocManaged((void**)&ret, n * sizeof(value_type))); | ||
| if (ret == nullptr) { | ||
| throw std::bad_alloc(); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| static void* allocate_raw(size_type n, const void* /*hint*/ = 0) { | ||
| void* ret; | ||
| SPLIT_CHECK_ERR(split_gpuMallocManaged((void**)&ret, n)); | ||
| if (ret == nullptr) { | ||
| throw std::bad_alloc(); | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| void deallocate(pointer p, size_type n) { | ||
| if (n != 0 && p != 0) { | ||
| SPLIT_CHECK_ERR(split_gpuFree(p)); | ||
| } | ||
| } | ||
| static void deallocate(void* p, size_type n) { | ||
| if (n != 0 && p != 0) { | ||
| SPLIT_CHECK_ERR(split_gpuFree(p)); | ||
| } | ||
| } | ||
|
|
||
| size_type max_size() const throw() { | ||
| size_type max = static_cast<size_type>(-1) / sizeof(value_type); | ||
| return (max > 0 ? max : 1); | ||
| } | ||
|
|
||
| template <typename U, typename... Args> | ||
| __host__ __device__ void construct(U* p, Args&&... args) { | ||
| ::new (p) U(std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| void destroy(pointer p) { p->~value_type(); } | ||
| }; | ||
|
|
||
| typedef uint32_t int_type ; | ||
| typedef struct{ | ||
| int_type num; | ||
| int_type flag; | ||
| } test_t; | ||
| typedef split::SplitVector<test_t> vector; | ||
| typedef split::SplitVector<test_t,customAllocator<test_t>> customAllocatorVector; | ||
| size_t count = 0; | ||
|
|
||
| void fill_vec(vector& v, size_t targetSize){ | ||
| count=0; | ||
| size_t st=0; | ||
| std::random_device rd; | ||
| std::mt19937 gen(rd()); | ||
| std::uniform_int_distribution<int_type> dist(1, std::numeric_limits<int_type>::max()); | ||
| v.clear(); | ||
| while (v.size() < targetSize) { | ||
| int_type val =++st;// dist(gen); | ||
| v.push_back(test_t{val,(val%2==0)}); | ||
| if (val%2 == 0){count++;}; | ||
| } | ||
| } | ||
|
|
||
| bool checkFlags(const customAllocatorVector& v,const int_type target){ | ||
| for (const auto& i:v){ | ||
| if (i.flag!=target){return false;} | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool run_test_small_loop_variant(size_t size){ | ||
| // std::cout<<"Testing with vector size: "<<size<<std::endl; | ||
| vector* v=new vector(); | ||
| fill_vec(*v,size); | ||
|
|
||
| auto predicate_on =[]__host__ __device__ (test_t element)->bool{ return element.flag == 1 ;}; | ||
| auto predicate_off =[]__host__ __device__ (test_t element)->bool{ return element.flag == 0 ;}; | ||
| customAllocatorVector* output1=new customAllocatorVector(nextPow2(2*v->size())); | ||
| customAllocatorVector* output2=new customAllocatorVector(nextPow2(2*v->size())); | ||
|
|
||
| split::tools::copy_if_loop(*v,*output1,predicate_on); | ||
| split::tools::copy_if_loop(*v,*output2,predicate_off); | ||
| SPLIT_CHECK_ERR( split_gpuDeviceSynchronize() ); | ||
|
|
||
| bool sane1 = checkFlags(*output1,1); | ||
| bool sane2 = checkFlags(*output2,0); | ||
| bool sane3 = ((output1->size()+output2->size())==v->size()); | ||
| bool sane4 =( output1->size() ==count ); | ||
| bool sane5 = ( output2->size() ==v->size()-count ); | ||
| // printf( " %d - %d - %d - %d - %d\n",sane1,sane2,sane3,sane4,sane5 ); | ||
| bool retval = sane1 && sane2 && sane3 && sane4 && sane5; | ||
| return retval; | ||
| } | ||
|
|
||
| TEST(StremCompaction , Compaction_Tests_Linear_Loop_Variant){ | ||
| for (size_t s=32; s< 1024; s++ ){ | ||
| bool a = run_test_small_loop_variant(s); | ||
| expect_true(a); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| int main(int argc, char* argv[]){ | ||
| ::testing::InitGoogleTest(&argc, argv); | ||
| return RUN_ALL_TESTS(); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question for all these pretty much
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The functions would then break if ALLOCATOR is defined but BLOCKSIZE and WARP are not (or the other way around depending on the order of the arguments).