fml  0.1-0
Fused Matrix Library
gpuscalar.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_GPU_INTERNALS_GPUSCALAR_H
6 #define FML_GPU_INTERNALS_GPUSCALAR_H
7 #pragma once
8 
9 
10 #include "../card.hh"
11 
12 
13 namespace fml
14 {
15  template <typename T>
16  class gpuscalar
17  {
18  public:
19  gpuscalar(std::shared_ptr<card> gpu);
20  gpuscalar(std::shared_ptr<card> gpu, const T v);
21  ~gpuscalar();
22 
23  void set_zero();
24  void set_val(const T v);
25  void get_val(T *v);
26 
27  T* data_ptr() {return data;};
28  T* data_ptr() const {return data;};
29 
30  protected:
31  std::shared_ptr<card> c;
32  T *data;
33  };
34 }
35 
36 
37 
38 template <typename T>
39 fml::gpuscalar<T>::gpuscalar(std::shared_ptr<fml::card> gpu)
40 {
41  c = gpu;
42  data = (T*) c->mem_alloc(sizeof(T));
43 }
44 
45 
46 
47 template <typename T>
48 fml::gpuscalar<T>::gpuscalar(std::shared_ptr<fml::card> gpu, const T v)
49 {
50  c = gpu;
51  data = (T*) c->mem_alloc(sizeof(T));
52  c->mem_cpu2gpu(data, &v, sizeof(T));
53 }
54 
55 
56 
57 template <typename T>
59 {
60  c->mem_free(data);
61  data = NULL;
62 }
63 
64 
65 
66 template <typename T>
68 {
69  c->mem_set(data, 0, sizeof(T));
70 }
71 
72 
73 
74 template <typename T>
75 void fml::gpuscalar<T>::set_val(const T v)
76 {
77  c->mem_set(data, &v, sizeof(T));
78 }
79 
80 
81 
82 template <typename T>
84 {
85  c->mem_gpu2cpu(v, data, sizeof(T));
86 }
87 
88 
89 #endif
fml
Core namespace.
Definition: dimops.hh:10
fml::gpuscalar
Definition: gpuscalar.hh:16