fml  0.1-0
Fused Matrix Library
unimat.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__INTERNALS_MATRIX_H
6 #define FML__INTERNALS_MATRIX_H
7 #pragma once
8 
9 
10 #ifdef __CUDACC__
11 #include <cublas.h>
12 #endif
13 
14 #include <stdexcept>
15 
16 #include "print.hh"
17 #include "types.hh"
18 
19 
23 namespace fml
24 {
29  template <typename REAL>
30  class unimat
31  {
32  public:
34  bool is_square() const {return (this->m==this->n);};
36  len_t nrows() const {return m;};
38  len_t ncols() const {return n;};
40  REAL* data_ptr() {return data;};
41  REAL* data_ptr() const {return data;};
42 
43  protected:
44  len_t m;
45  len_t n;
46  REAL *data;
47  bool free_data;
48 
49  bool should_free() const {return free_data;};
50  void check_index(const len_t i) const;
51  void check_index(const len_t i, const len_t j) const;
52  void printval(const REAL val, uint8_t ndigits) const;
53  };
54 }
55 
56 
57 
58 template <typename REAL>
59 void fml::unimat<REAL>::check_index(const len_t i) const
60 {
61  if (i < 0 || i >= (this->m * this->n))
62  throw std::runtime_error("index out of bounds");
63 }
64 
65 template <typename REAL>
66 void fml::unimat<REAL>::check_index(const len_t i, const len_t j) const
67 {
68  if (i < 0 || i >= this->m || j < 0 || j >= this->n)
69  throw std::runtime_error("index out of bounds");
70 }
71 
72 
73 
74 template <>
75 inline void fml::unimat<int>::printval(const int val, uint8_t ndigits) const
76 {
77  (void)ndigits;
78  fml::print::printf("%d ", val);
79 }
80 
81 #ifdef __CUDACC__
82 template <>
83 inline void fml::unimat<__half>::printval(const __half val, uint8_t ndigits) const
84 {
85  fml::print::printf("%.*f ", ndigits, (float)val);
86 }
87 #endif
88 
89 template <typename REAL>
90 void fml::unimat<REAL>::printval(const REAL val, uint8_t ndigits) const
91 {
92  fml::print::printf("%.*f ", ndigits, val);
93 }
94 
95 
96 #endif
fml::unimat
Base matrix class. Not meant for direct use. Instead see cpumat, gpumat, and mpimat.
Definition: unimat.hh:30
fml::unimat::is_square
bool is_square() const
Is the matrix square?
Definition: unimat.hh:34
fml::unimat::nrows
len_t nrows() const
Number of rows.
Definition: unimat.hh:36
fml::unimat::ncols
len_t ncols() const
Number of columns.
Definition: unimat.hh:38
fml::unimat::data_ptr
REAL * data_ptr()
Pointer to the internal array.
Definition: unimat.hh:40
fml
Core namespace.
Definition: dimops.hh:10