1 /* =========================================================================
2    Copyright (c) 2010-2014, Institute for Microelectronics,
3                             Institute for Analysis and Scientific Computing,
4                             TU Wien.
5    Portions of this software are copyright by UChicago Argonne, LLC.
6 
7                             -----------------
8                   ViennaCL - The Vienna Computing Library
9                             -----------------
10 
11    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
12 
13    (A list of authors and contributors can be found in the PDF manual)
14 
15    License:         MIT (X11), see file LICENSE in the base directory
16 ============================================================================= */
17 
18 #include "viennacl.hpp"
19 #include "viennacl/backend/mem_handle.hpp"
20 
21 
22 
init_cuda_vector(viennacl::backend::mem_handle & h,ViennaCLVector x)23 static ViennaCLStatus init_cuda_vector(viennacl::backend::mem_handle & h, ViennaCLVector x)
24 {
25 #ifdef VIENNACL_WITH_CUDA
26   h.switch_active_handle_id(viennacl::CUDA_MEMORY);
27   h.cuda_handle().reset(x->cuda_mem);
28   h.cuda_handle().inc();
29   if (x->precision == ViennaCLFloat)
30     h.raw_size(static_cast<viennacl::vcl_size_t>(x->inc) * x->size * sizeof(float)); // not necessary, but still set for conciseness
31   else if (x->precision == ViennaCLDouble)
32     h.raw_size(static_cast<viennacl::vcl_size_t>(x->inc) * x->size * sizeof(double)); // not necessary, but still set for conciseness
33   else
34     return ViennaCLGenericFailure;
35 
36   return ViennaCLSuccess;
37 #else
38   (void)h;
39   (void)x;
40   return ViennaCLGenericFailure;
41 #endif
42 }
43 
init_opencl_vector(viennacl::backend::mem_handle & h,ViennaCLVector x)44 static ViennaCLStatus init_opencl_vector(viennacl::backend::mem_handle & h, ViennaCLVector x)
45 {
46 #ifdef VIENNACL_WITH_OPENCL
47   h.switch_active_handle_id(viennacl::OPENCL_MEMORY);
48   h.opencl_handle() = x->opencl_mem;
49   h.opencl_handle().inc();
50   if (x->precision == ViennaCLFloat)
51     h.raw_size(static_cast<viennacl::vcl_size_t>(x->inc) * static_cast<viennacl::vcl_size_t>(x->size) * sizeof(float)); // not necessary, but still set for conciseness
52   else if (x->precision == ViennaCLDouble)
53     h.raw_size(static_cast<viennacl::vcl_size_t>(x->inc) * static_cast<viennacl::vcl_size_t>(x->size) * sizeof(double)); // not necessary, but still set for conciseness
54   else
55     return ViennaCLGenericFailure;
56 
57   return ViennaCLSuccess;
58 #else
59   (void)h;
60   (void)x;
61   return ViennaCLGenericFailure;
62 #endif
63 }
64 
65 
init_host_vector(viennacl::backend::mem_handle & h,ViennaCLVector x)66 static ViennaCLStatus init_host_vector(viennacl::backend::mem_handle & h, ViennaCLVector x)
67 {
68   h.switch_active_handle_id(viennacl::MAIN_MEMORY);
69   h.ram_handle().reset(x->host_mem);
70   h.ram_handle().inc();
71   if (x->precision == ViennaCLFloat)
72     h.raw_size(static_cast<viennacl::vcl_size_t>(x->inc) * static_cast<viennacl::vcl_size_t>(x->size) * sizeof(float)); // not necessary, but still set for conciseness
73   else if (x->precision == ViennaCLDouble)
74     h.raw_size(static_cast<viennacl::vcl_size_t>(x->inc) * static_cast<viennacl::vcl_size_t>(x->size) * sizeof(double)); // not necessary, but still set for conciseness
75   else
76     return ViennaCLGenericFailure;
77 
78   return ViennaCLSuccess;
79 }
80 
81 
init_vector(viennacl::backend::mem_handle & h,ViennaCLVector x)82 static ViennaCLStatus init_vector(viennacl::backend::mem_handle & h, ViennaCLVector x)
83 {
84   switch (x->backend->backend_type)
85   {
86     case ViennaCLCUDA:
87       return init_cuda_vector(h, x);
88 
89     case ViennaCLOpenCL:
90       return init_opencl_vector(h, x);
91 
92     case ViennaCLHost:
93       return init_host_vector(h, x);
94 
95     default:
96       return ViennaCLGenericFailure;
97   }
98 }
99 
100 
101 
102