Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit 1ae42b9

Browse files
committed
Expose publicly get_temporary_buffer and return_temporary_buffer.
Test get_temporary_buffer & malloc Fixes #160 Fixes #168 Fixes #1 Fixes #2
1 parent 3aab573 commit 1ae42b9

File tree

9 files changed

+176
-95
lines changed

9 files changed

+176
-95
lines changed

testing/memory.cu

Lines changed: 89 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,37 @@
33
#include <thrust/sort.h>
44
#include <thrust/memory.h>
55
#include <thrust/pair.h>
6+
#include <thrust/fill.h>
7+
#include <thrust/logical.h>
8+
9+
10+
struct my_system : thrust::device_system<my_system> {};
611

7-
struct my_tag : thrust::device_system<my_tag> {};
812

913
template<typename T1, typename T2>
1014
bool is_same(const T1 &, const T2 &)
1115
{
1216
return false;
1317
}
1418

19+
1520
template<typename T>
1621
bool is_same(const T &, const T &)
1722
{
1823
return true;
1924
}
2025

26+
2127
void TestSelectSystemDifferentTypes()
2228
{
2329
using thrust::system::detail::generic::select_system;
2430

25-
// select_system(my_tag, device_system_tag) should return device_system_tag (the minimum tag)
26-
bool is_device_system_tag = is_same(thrust::device_system_tag(), select_system(my_tag(), thrust::device_system_tag()));
31+
// select_system(my_system, device_system_tag) should return device_system_tag (the minimum tag)
32+
bool is_device_system_tag = is_same(thrust::device_system_tag(), select_system(my_system(), thrust::device_system_tag()));
2733
ASSERT_EQUAL(true, is_device_system_tag);
2834

2935
// select_system(device_system_tag, my_tag) should return device_system_tag (the minimum tag)
30-
is_device_system_tag = is_same(thrust::device_system_tag(), select_system(thrust::device_system_tag(), my_tag()));
36+
is_device_system_tag = is_same(thrust::device_system_tag(), select_system(thrust::device_system_tag(), my_system()));
3137
ASSERT_EQUAL(true, is_device_system_tag);
3238
}
3339
DECLARE_UNITTEST(TestSelectSystemDifferentTypes);
@@ -45,42 +51,91 @@ void TestSelectSystemSameTypes()
4551
bool is_device_system_tag = is_same(thrust::device_system_tag(), select_system(thrust::device_system_tag(), thrust::device_system_tag()));
4652
ASSERT_EQUAL(true, is_device_system_tag);
4753

48-
// select_system(my_tag, my_tag) should return my_tag
49-
bool is_my_tag = is_same(my_tag(), select_system(my_tag(), my_tag()));
50-
ASSERT_EQUAL(true, is_my_tag);
54+
// select_system(my_system, my_system) should return my_system
55+
bool is_my_system = is_same(my_system(), select_system(my_system(), my_system()));
56+
ASSERT_EQUAL(true, is_my_system);
5157
}
5258
DECLARE_UNITTEST(TestSelectSystemSameTypes);
5359

5460

55-
//template<typename T>
56-
// thrust::pair<thrust::pointer<T,my_tag>, std::ptrdiff_t>
57-
// get_temporary_buffer(my_tag, std::ptrdiff_t n)
58-
//{
59-
// // communicate that my version of get_temporary_buffer
60-
// // was correctly dispatched
61-
// throw my_tag();
62-
//}
63-
//
61+
void TestGetTemporaryBuffer()
62+
{
63+
const size_t n = 9001;
64+
65+
thrust::device_system_tag dev_tag;
66+
typedef thrust::pointer<int, thrust::device_system_tag> pointer;
67+
thrust::pair<pointer, std::ptrdiff_t> ptr_and_sz = thrust::get_temporary_buffer<int>(dev_tag, n);
68+
69+
ASSERT_EQUAL(ptr_and_sz.second, n);
70+
71+
const int ref_val = 13;
72+
thrust::device_vector<int> ref(n, ref_val);
73+
74+
thrust::fill_n(ptr_and_sz.first, n, ref_val);
75+
76+
ASSERT_EQUAL(true, thrust::all_of(ptr_and_sz.first, ptr_and_sz.first + n, thrust::placeholders::_1 == ref_val));
77+
78+
thrust::return_temporary_buffer(dev_tag, ptr_and_sz.first);
79+
}
80+
DECLARE_UNITTEST(TestGetTemporaryBuffer);
81+
82+
83+
void TestMalloc()
84+
{
85+
const size_t n = 9001;
86+
87+
thrust::device_system_tag dev_tag;
88+
typedef thrust::pointer<int, thrust::device_system_tag> pointer;
89+
pointer ptr = pointer(static_cast<int*>(thrust::malloc(dev_tag, sizeof(int) * n).get()));
90+
91+
const int ref_val = 13;
92+
thrust::device_vector<int> ref(n, ref_val);
93+
94+
thrust::fill_n(ptr, n, ref_val);
95+
96+
ASSERT_EQUAL(true, thrust::all_of(ptr, ptr + n, thrust::placeholders::_1 == ref_val));
97+
98+
thrust::free(dev_tag, ptr);
99+
}
100+
DECLARE_UNITTEST(TestMalloc);
101+
102+
103+
static bool g_correctly_dispatched;
104+
105+
106+
template<typename T>
107+
thrust::pair<thrust::pointer<T,my_system>, std::ptrdiff_t>
108+
get_temporary_buffer(my_system sys, std::ptrdiff_t n)
109+
{
110+
// communicate that my version of get_temporary_buffer
111+
// was correctly dispatched
112+
g_correctly_dispatched = true;
113+
114+
thrust::device_system_tag device_sys;
115+
thrust::pair<thrust::pointer<T, thrust::device_system_tag>, std::ptrdiff_t> result = thrust::get_temporary_buffer<T>(device_sys, n);
116+
return thrust::make_pair(thrust::pointer<T,my_system>(result.first.get()), result.second);
117+
}
118+
119+
64120
void TestGetTemporaryBufferDispatchImplicit()
65121
{
66-
KNOWN_FAILURE;
67-
68-
// bool correctly_dispatched = false;
69-
//
70-
// try
71-
// {
72-
// thrust::device_vector<int> vec(2);
73-
//
74-
// // call something we know will invoke get_temporary_buffer
75-
// thrust::sort(thrust::retag<my_tag>(vec.begin()),
76-
// thrust::retag<my_tag>(vec.end()));
77-
// }
78-
// catch(my_tag)
79-
// {
80-
// correctly_dispatched = true;
81-
// }
82-
//
83-
// ASSERT_EQUAL(true, correctly_dispatched);
122+
if(is_same(thrust::device_system_tag(), thrust::system::cpp::tag()))
123+
{
124+
// XXX cpp uses the internal scalar backend, which currently elides user tags
125+
KNOWN_FAILURE;
126+
}
127+
else
128+
{
129+
g_correctly_dispatched = false;
130+
131+
thrust::device_vector<int> vec(9001);
132+
133+
// call something we know will invoke get_temporary_buffer
134+
my_system sys;
135+
thrust::sort(sys, vec.begin(), vec.end());
136+
137+
ASSERT_EQUAL(true, g_correctly_dispatched);
138+
}
84139
}
85140
DECLARE_UNITTEST(TestGetTemporaryBufferDispatchImplicit);
86141

thrust/detail/allocator/temporary_allocator.inl

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,21 @@
1616

1717
#include <thrust/detail/config.h>
1818
#include <thrust/detail/allocator/temporary_allocator.h>
19-
#include <thrust/system/detail/generic/select_system.h>
20-
#include <thrust/system/detail/generic/memory.h>
19+
#include <thrust/detail/temporary_buffer.h>
2120
#include <thrust/system/detail/bad_alloc.h>
22-
#include <thrust/pair.h>
23-
#include <thrust/detail/raw_pointer_cast.h>
2421

2522
namespace thrust
2623
{
2724
namespace detail
2825
{
29-
namespace temporary_allocator_detail
30-
{
31-
32-
template<typename T, typename System, typename Pair>
33-
thrust::pair<thrust::pointer<T,System>, typename thrust::pointer<T,System>::difference_type>
34-
down_cast_pair(Pair p)
35-
{
36-
// XXX should use a hypothetical thrust::static_pointer_cast here
37-
thrust::pointer<T,System> ptr = thrust::pointer<T,System>(static_cast<T*>(thrust::raw_pointer_cast(p.first)));
38-
39-
typedef thrust::pair<thrust::pointer<T,System>, typename thrust::pointer<T,System>::difference_type> result_type;
40-
return result_type(ptr, p.second);
41-
} // end down_cast_pair()
42-
43-
44-
} // end temporary_allocator_detail
4526

4627

4728
template<typename T, typename System>
4829
typename temporary_allocator<T,System>::pointer
4930
temporary_allocator<T,System>
5031
::allocate(typename temporary_allocator<T,System>::size_type cnt)
5132
{
52-
using thrust::system::detail::generic::get_temporary_buffer;
53-
54-
// XXX use thrust::get_temporary_buffer here should we add it
55-
pointer_and_size result = temporary_allocator_detail::down_cast_pair<T,System>(get_temporary_buffer<T>(m_system.derived(), cnt));
33+
pointer_and_size result = thrust::get_temporary_buffer<T>(m_system, cnt);
5634

5735
// handle failure
5836
if(result.second < cnt)
@@ -71,12 +49,10 @@ template<typename T, typename System>
7149
void temporary_allocator<T,System>
7250
::deallocate(typename temporary_allocator<T,System>::pointer p, typename temporary_allocator<T,System>::size_type n)
7351
{
74-
using thrust::system::detail::generic::return_temporary_buffer;
75-
76-
// XXX use thrust::return_temporary_buffer here should we add it
77-
return return_temporary_buffer(m_system.derived(), p);
52+
return thrust::return_temporary_buffer(m_system, p);
7853
} // end temporary_allocator
7954

55+
8056
} // end detail
8157
} // end thrust
8258

thrust/detail/malloc_and_free.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace thrust
2727
{
2828

2929
template<typename System>
30-
pointer<void,System> malloc(thrust::dispatchable<System> &system, std::size_t n)
30+
pointer<void,System> malloc(thrust::detail::dispatchable_base<System> &system, std::size_t n)
3131
{
3232
using thrust::system::detail::generic::malloc;
3333

@@ -55,7 +55,7 @@ void free(int *volatile ptr)
5555
#endif // THRUST_DEVICE_COMPILER
5656

5757
template<typename System, typename Pointer>
58-
void free(thrust::dispatchable<System> &system, Pointer ptr)
58+
void free(thrust::detail::dispatchable_base<System> &system, Pointer ptr)
5959
{
6060
using thrust::system::detail::generic::free;
6161

thrust/detail/temporary_buffer.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2008-2012 NVIDIA Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <thrust/detail/config.h>
20+
#include <thrust/detail/dispatchable.h>
21+
#include <thrust/pair.h>
22+
#include <thrust/detail/pointer.h>
23+
#include <thrust/detail/raw_pointer_cast.h>
24+
#include <thrust/system/detail/generic/memory.h>
25+
26+
namespace thrust
27+
{
28+
namespace detail
29+
{
30+
namespace get_temporary_buffer_detail
31+
{
32+
33+
34+
template<typename T, typename System, typename Pair>
35+
thrust::pair<thrust::pointer<T,System>, typename thrust::pointer<T,System>::difference_type>
36+
down_cast_pair(Pair p)
37+
{
38+
// XXX should use a hypothetical thrust::static_pointer_cast here
39+
thrust::pointer<T,System> ptr = thrust::pointer<T,System>(static_cast<T*>(thrust::raw_pointer_cast(p.first)));
40+
41+
typedef thrust::pair<thrust::pointer<T,System>, typename thrust::pointer<T,System>::difference_type> result_type;
42+
return result_type(ptr, p.second);
43+
} // end down_cast_pair()
44+
45+
46+
} // end get_temporary_buffer_detail
47+
} // end detail
48+
49+
50+
template<typename T, typename System>
51+
thrust::pair<thrust::pointer<T,System>, typename thrust::pointer<T,System>::difference_type>
52+
get_temporary_buffer(thrust::detail::dispatchable_base<System> &system, typename thrust::pointer<T,System>::difference_type n)
53+
{
54+
using thrust::system::detail::generic::get_temporary_buffer;
55+
56+
return thrust::detail::get_temporary_buffer_detail::down_cast_pair<T,System>(get_temporary_buffer<T>(system.derived(), n));
57+
} // end get_temporary_buffer()
58+
59+
60+
template<typename System, typename Pointer>
61+
void return_temporary_buffer(thrust::detail::dispatchable_base<System> &system, Pointer p)
62+
{
63+
using thrust::system::detail::generic::return_temporary_buffer;
64+
65+
return return_temporary_buffer(system.derived(), p);
66+
} // end return_temporary_buffer()
67+
68+
69+
} // end thrust
70+

thrust/memory.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <thrust/detail/raw_pointer_cast.h>
2727
#include <thrust/detail/raw_reference_cast.h>
2828
#include <thrust/detail/malloc_and_free.h>
29+
#include <thrust/detail/temporary_buffer.h>
2930

3031
namespace thrust
3132
{

thrust/system/cpp/detail/malloc_and_free.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ namespace detail
3434
// note that malloc returns a raw pointer to avoid
3535
// depending on the heavyweight thrust/system/cpp/memory.h header
3636
template<typename System>
37-
inline void *malloc(thrust::system::cpp::detail::dispatchable<System> &, std::size_t n)
37+
void *malloc(dispatchable<System> &, std::size_t n)
3838
{
3939
return std::malloc(n);
4040
} // end malloc()
4141

4242

4343
template<typename System, typename Pointer>
44-
inline void free(thrust::system::cpp::detail::dispatchable<System> &, Pointer ptr)
44+
void free(dispatchable<System> &, Pointer ptr)
4545
{
4646
std::free(thrust::raw_pointer_cast(ptr));
4747
} // end free()

thrust/system/cuda/detail/malloc_and_free.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace detail
3636

3737
// note that malloc returns a raw pointer to avoid
3838
// depending on the heavyweight thrust/system/cuda/memory.h header
39-
inline void *malloc(tag, std::size_t n)
39+
template<typename System>
40+
void *malloc(dispatchable<System> &, std::size_t n)
4041
{
4142
void *result = 0;
4243

@@ -51,8 +52,8 @@ inline void *malloc(tag, std::size_t n)
5152
} // end malloc()
5253

5354

54-
template<typename Pointer>
55-
inline void free(tag, Pointer ptr)
55+
template<typename System, typename Pointer>
56+
void free(dispatchable<System> &, Pointer ptr)
5657
{
5758
cudaError_t error = cudaFree(thrust::raw_pointer_cast(ptr));
5859

thrust/system/cuda/detail/memory.inl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ void swap(reference<T> a, reference<T> b)
7171

7272
pointer<void> malloc(std::size_t n)
7373
{
74-
return pointer<void>(thrust::system::cuda::detail::malloc(tag(), n));
74+
tag cuda_tag;
75+
return pointer<void>(thrust::system::cuda::detail::malloc(cuda_tag, n));
7576
} // end malloc()
7677

7778
void free(pointer<void> ptr)
7879
{
79-
return thrust::system::cuda::detail::free(tag(), ptr.get());
80+
tag cuda_tag;
81+
return thrust::system::cuda::detail::free(cuda_tag, ptr.get());
8082
} // end free()
8183

8284
} // end cuda

0 commit comments

Comments
 (0)