This file is indexed.

/usr/include/viennacl/backend/mem_handle.hpp is in libviennacl-dev 1.5.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef VIENNACL_BACKEND_MEM_HANDLE_HPP
#define VIENNACL_BACKEND_MEM_HANDLE_HPP

/* =========================================================================
   Copyright (c) 2010-2014, Institute for Microelectronics,
                            Institute for Analysis and Scientific Computing,
                            TU Wien.
   Portions of this software are copyright by UChicago Argonne, LLC.

                            -----------------
                  ViennaCL - The Vienna Computing Library
                            -----------------

   Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at

   (A list of authors and contributors can be found in the PDF manual)

   License:         MIT (X11), see file LICENSE in the base directory
============================================================================= */

/** @file viennacl/backend/mem_handle.hpp
    @brief Implements the multi-memory-domain handle
*/

#include <vector>
#include <cassert>
#include "viennacl/forwards.h"
#include "viennacl/tools/shared_ptr.hpp"
#include "viennacl/backend/cpu_ram.hpp"

#ifdef VIENNACL_WITH_OPENCL
  #include "viennacl/backend/opencl.hpp"
#endif

#ifdef VIENNACL_WITH_CUDA
  #include "viennacl/backend/cuda.hpp"
#endif


namespace viennacl
{
  namespace backend
  {


// if a user compiles with CUDA, it is reasonable to expect that CUDA should be the default
#ifdef VIENNACL_WITH_CUDA
    inline memory_types default_memory_type() { return CUDA_MEMORY; }
#elif defined(VIENNACL_WITH_OPENCL)
    inline memory_types default_memory_type() { return OPENCL_MEMORY; }
#else
    inline memory_types default_memory_type() { return MAIN_MEMORY; }
#endif


    /** @brief Main abstraction class for multiple memory domains. Represents a buffer in either main RAM, an OpenCL context, or a CUDA device.
     *
     * The idea is to wrap all possible handle types inside this class so that higher-level code does not need to be cluttered with preprocessor switches.
     * Instead, this class collects all the necessary conditional compilations.
     *
     */
    class mem_handle
    {
      public:
        typedef viennacl::tools::shared_ptr<char>      ram_handle_type;
        typedef viennacl::tools::shared_ptr<char>      cuda_handle_type;

        /** @brief Default CTOR. No memory is allocated */
        mem_handle() : active_handle_(MEMORY_NOT_INITIALIZED), size_in_bytes_(0) {}

        /** @brief Returns the handle to a buffer in CPU RAM. NULL is returned if no such buffer has been allocated. */
        ram_handle_type       & ram_handle()       { return ram_handle_; }
        /** @brief Returns the handle to a buffer in CPU RAM. NULL is returned if no such buffer has been allocated. */
        ram_handle_type const & ram_handle() const { return ram_handle_; }

#ifdef VIENNACL_WITH_OPENCL
        /** @brief Returns the handle to an OpenCL buffer. The handle contains NULL if no such buffer has been allocated. */
        viennacl::ocl::handle<cl_mem>       & opencl_handle()       { return opencl_handle_; }
        /** @brief Returns the handle to an OpenCL buffer. The handle contains NULL if no such buffer has been allocated. */
        viennacl::ocl::handle<cl_mem> const & opencl_handle() const { return opencl_handle_; }
#endif

#ifdef VIENNACL_WITH_CUDA
        /** @brief Returns the handle to a CUDA buffer. The handle contains NULL if no such buffer has been allocated. */
        cuda_handle_type       & cuda_handle()       { return cuda_handle_; }
        /** @brief Returns the handle to a CUDA buffer. The handle contains NULL if no such buffer has been allocated. */
        cuda_handle_type const & cuda_handle() const { return cuda_handle_; }
#endif

        /** @brief Returns an ID for the currently active memory buffer. Other memory buffers might contain old or no data. */
        memory_types get_active_handle_id() const { return active_handle_; }

        /** @brief Switches the currently active handle. If no support for that backend is provided, an exception is thrown. */
        void switch_active_handle_id(memory_types new_id)
        {
          if (new_id != active_handle_)
          {
            if (active_handle_ == MEMORY_NOT_INITIALIZED)
              active_handle_ = new_id;
            else if (active_handle_ == MAIN_MEMORY)
            {
              active_handle_ = new_id;
            }
            else if (active_handle_ == OPENCL_MEMORY)
            {
#ifdef VIENNACL_WITH_OPENCL
              active_handle_ = new_id;
#else
              throw "compiled without OpenCL suppport!";
#endif
            }
            else if (active_handle_ == CUDA_MEMORY)
            {
#ifdef VIENNACL_WITH_CUDA
              active_handle_ = new_id;
#else
              throw "compiled without CUDA suppport!";
#endif
            }
            else
              throw "invalid new memory region!";
          }
        }

        /** @brief Compares the two handles and returns true if the active memory handles in the two mem_handles point to the same buffer. */
        bool operator==(mem_handle const & other) const
        {
          if (active_handle_ != other.active_handle_)
            return false;

          switch (active_handle_)
          {
            case MAIN_MEMORY:
              return ram_handle_.get() == other.ram_handle_.get();
#ifdef VIENNACL_WITH_OPENCL
            case OPENCL_MEMORY:
              return opencl_handle_.get() == other.opencl_handle_.get();
#endif
#ifdef VIENNACL_WITH_CUDA
            case CUDA_MEMORY:
              return cuda_handle_.get() == other.cuda_handle_.get();
#endif
            default: break;
          }

          return false;
        }

        /** @brief Compares the two handles and returns true if the active memory handles in the two mem_handles point a buffer with inferior address
         * useful to store handles into a map, since they naturally have strong ordering
         */
        bool operator<(mem_handle const & other) const
        {
          if (active_handle_ != other.active_handle_)
            return false;

          switch (active_handle_)
          {
            case MAIN_MEMORY:
              return ram_handle_.get() < other.ram_handle_.get();
#ifdef VIENNACL_WITH_OPENCL
            case OPENCL_MEMORY:
              return opencl_handle_.get() < other.opencl_handle_.get();
#endif
#ifdef VIENNACL_WITH_CUDA
            case CUDA_MEMORY:
              return cuda_handle_.get() < other.cuda_handle_.get();
#endif
            default: break;
          }

          return false;
        }


        bool operator!=(mem_handle const & other) const { return !(*this == other); }

        /** @brief Implements a fast swapping method. No data is copied, only the handles are exchanged. */
        void swap(mem_handle & other)
        {
          // swap handle type:
          memory_types active_handle_tmp = other.active_handle_;
          other.active_handle_ = active_handle_;
          active_handle_ = active_handle_tmp;

          // swap ram handle:
          ram_handle_type ram_handle_tmp = other.ram_handle_;
          other.ram_handle_ = ram_handle_;
          ram_handle_ = ram_handle_tmp;

          // swap OpenCL handle:
#ifdef VIENNACL_WITH_OPENCL
          opencl_handle_.swap(other.opencl_handle_);
#endif
#ifdef VIENNACL_WITH_CUDA
          cuda_handle_type cuda_handle_tmp = other.cuda_handle_;
          other.cuda_handle_ = cuda_handle_;
          cuda_handle_ = cuda_handle_tmp;
#endif
        }

        /** @brief Returns the number of bytes of the currently active buffer */
        vcl_size_t raw_size() const               { return size_in_bytes_; }

        /** @brief Sets the size of the currently active buffer. Use with care! */
        void        raw_size(vcl_size_t new_size) { size_in_bytes_ = new_size; }

      private:
        memory_types active_handle_;
        ram_handle_type ram_handle_;
#ifdef VIENNACL_WITH_OPENCL
        viennacl::ocl::handle<cl_mem> opencl_handle_;
#endif
#ifdef VIENNACL_WITH_CUDA
        cuda_handle_type        cuda_handle_;
#endif
        vcl_size_t size_in_bytes_;
    };


  } //backend


} //viennacl
#endif