~ubuntu-branches/ubuntu/trusty/libthrust/trusty

« back to all changes in this revision

Viewing changes to device_malloc_allocator.h

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Beckmann
  • Date: 2011-05-28 09:32:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110528093248-np3euv5sj7fw3nyv
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright 2008-2011 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
 
 
18
/*! \file device_malloc_allocator.h
 
19
 *  \brief Defines the interface to a
 
20
 *         standard C++ allocator class for
 
21
 *         allocating device memory with device_malloc.
 
22
 */
 
23
 
 
24
#pragma once
 
25
 
 
26
#include <thrust/detail/config.h>
 
27
#include <thrust/device_ptr.h>
 
28
#include <thrust/device_reference.h>
 
29
#include <thrust/device_malloc.h>
 
30
#include <thrust/device_free.h>
 
31
#include <limits>
 
32
#include <stdexcept>
 
33
 
 
34
namespace thrust
 
35
{
 
36
 
 
37
// forward declarations to WAR circular #includes
 
38
template<typename> class device_ptr;
 
39
template<typename T> device_ptr<T> device_malloc(const std::size_t n);
 
40
 
 
41
/*! \addtogroup memory_management Memory Management
 
42
 *  \addtogroup memory_management_classes Memory Management Classes
 
43
 *  \ingroup memory_management
 
44
 *  \{
 
45
 */
 
46
 
 
47
/*! \p device_malloc_allocator is a device memory allocator that employs the
 
48
 *  \p device_malloc function for allocation.
 
49
 *
 
50
 *  \see device_malloc
 
51
 *  \see device_ptr
 
52
 *  \see http://www.sgi.com/tech/stl/Allocators.html
 
53
 */
 
54
template<typename T>
 
55
  class device_malloc_allocator
 
56
{
 
57
  public:
 
58
    typedef T                                 value_type;
 
59
    typedef device_ptr<T>                     pointer;
 
60
    typedef device_ptr<const T>               const_pointer;
 
61
    typedef device_reference<T>               reference;
 
62
    typedef device_reference<const T>         const_reference;
 
63
    typedef std::size_t                       size_type;
 
64
    typedef typename pointer::difference_type difference_type;
 
65
 
 
66
    // convert a device_malloc_allocator<T> to device_malloc_allocator<U>
 
67
    template<typename U>
 
68
      struct rebind
 
69
    {
 
70
      typedef device_malloc_allocator<U> other;
 
71
    }; // end rebind
 
72
 
 
73
    __host__ __device__
 
74
    inline device_malloc_allocator() {}
 
75
 
 
76
    __host__ __device__
 
77
    inline ~device_malloc_allocator() {}
 
78
 
 
79
    __host__ __device__
 
80
    inline device_malloc_allocator(device_malloc_allocator const&) {}
 
81
 
 
82
    template<typename U>
 
83
    __host__ __device__
 
84
    inline device_malloc_allocator(device_malloc_allocator<U> const&) {}
 
85
 
 
86
    // address
 
87
    __host__ __device__
 
88
    inline pointer address(reference r) { return &r; }
 
89
    
 
90
    __host__ __device__
 
91
    inline const_pointer address(const_reference r) { return &r; }
 
92
 
 
93
    // memory allocation
 
94
    __host__
 
95
    inline pointer allocate(size_type cnt,
 
96
                            const_pointer = const_pointer(static_cast<T*>(0)))
 
97
    {
 
98
      if(cnt > this->max_size())
 
99
      {
 
100
        throw std::bad_alloc();
 
101
      } // end if
 
102
 
 
103
      return pointer(device_malloc<T>(cnt));
 
104
    } // end allocate()
 
105
 
 
106
    __host__
 
107
    inline void deallocate(pointer p, size_type cnt)
 
108
    {
 
109
      device_free(p);
 
110
    } // end deallocate()
 
111
 
 
112
    inline size_type max_size() const
 
113
    {
 
114
      return (std::numeric_limits<size_type>::max)() / sizeof(T);
 
115
    } // end max_size()
 
116
 
 
117
    __host__ __device__
 
118
    inline bool operator==(device_malloc_allocator const&) { return true; }
 
119
 
 
120
    __host__ __device__
 
121
    inline bool operator!=(device_malloc_allocator const &a) {return !operator==(a); }
 
122
}; // end device_malloc_allocator
 
123
 
 
124
/*! \}
 
125
 */
 
126
 
 
127
} // end thrust
 
128
 
 
129