Fréchet View  1.6.0
A Tool for Exploring Fréchet Distance Algorithms
matrix_pool.cpp
Go to the documentation of this file.
1 
2 #include <matrix_pool.h>
3 #include <clm4rm/clm4rm.h>
4 
5 #include <concurrency.h>
6 #include <qdebug.h>
7 
8 #if defined(__cplusplus) && !defined(MSC_VER)
9 extern "C" {
10 #endif
11 #include <m4ri/mzd.h>
12 #if defined(__cplusplus) && !defined(MSC_VER)
13 }
14 #endif
15 
16 using namespace frechet;
17 using namespace data;
18 
20  : free_mzd(),free_clmatrix()
21 { }
22 
24  clear();
25 }
26 
28  Key k (rows,cols);
29  auto i = free_mzd.find(k);
30  if (i==free_mzd.end()) {
31  i = free_mzd.insert(std::make_pair(k,mzdList())).first;
32  }
33  return i->second;
34 }
35 
37  Key k (rows,cols);
38  auto i = free_clmatrix.find(k);
39  if (i==free_clmatrix.end()) {
40  i = free_clmatrix.insert(std::make_pair(k,clmatrixList())).first;
41  }
42  return i->second;
43 }
44 
45 
46 mzd_t* MatrixPool::new_mzd_t(int rows, int cols)
47 {
48  mzd_t* result;
49  mzdList& free_list = getMzdFreeList(rows,cols);
50 
51  if (! free_list.empty()) {
52  result = free_list.back();
53  free_list.pop_back();
54  }
55  else {
56  result = new_mzd(rows, cols, nullptr);
57  // Note: already filled with zero
58  }
59 
60  //mzd_set_ui(result,0);
61  Q_ASSERT(result->nrows == rows);
62  Q_ASSERT(result->ncols == cols);
63  return result;
64 }
65 
67 {
68  clmatrix_t* result;
69  clmatrixList& free_list = getClmatrixFreeList(rows,cols);
70 
71  if (! free_list.empty()) {
72  result = free_list.back();
73  free_list.pop_back();
74  }
75  else {
76  result = new_clmatrix(rows,cols, nullptr, cond);
77  // TODO fill with zero !! ?? !!
78  }
79  Q_ASSERT(result->nrows == rows);
80  Q_ASSERT(result->ncols == cols);
81  return result;
82 }
83 
84 void MatrixPool::reclaim(mzd_t* M) {
85  mzdList& free_list = getMzdFreeList(M->nrows,M->ncols);
86  free_list.push_back(M);
87 }
88 
90  clmatrixList& free_list = getClmatrixFreeList(M->nrows,M->ncols);
91  free_list.push_back(M);
92 }
93 
95 
96  for(auto i : free_mzd)
97  for(mzd_t* M : i.second)
98  mzd_free(M);
99 
100  for(auto i : free_clmatrix)
101  for(clmatrix_t* M : i.second)
102  clm4rm_free(M);
103 
104  free_mzd.clear();
105  free_clmatrix.clear();
106 }
107 
108 
109 mzd_t* frechet::data::new_mzd(int rows, int cols, MatrixPool* pool) {
110  if (pool)
111  return pool->new_mzd_t(rows,cols);
112  else
113  return mzd_init(rows,cols);
114 }
115 
117  if (pool) {
118  return pool->new_clmatrix_t(rows, cols, cond);
119  }
120  else {
121  clmatrix_t* M = clm4rm_create(rows, cols, 32, false, app::ConcurrencyContext::clContext());
122  // fill with zero !?
124  return M;
125  }
126 }
127 
128 void frechet::data::reclaim(mzd_t* M, MatrixPool* pool) {
129  if (pool)
130  pool->reclaim(M);
131  else
132  mzd_free(M);
133 }
134 
136  if (pool)
137  pool->reclaim(M);
138  else
139  clm4rm_free(M);
140 }
void clm4rm_zero_fill(clmatrix_t *gpu_matrix, cl_command_queue queue, clm4rm_conditions *cond)
Fill a matrix with zero data. The operation is scheduled for asynchronous execution of the GPU....
Definition: clm4rm.cpp:364
clmatrix_t * new_clmatrix(int rows, int cols, MatrixPool *pool, clm4rm_conditions *cond)
allocate a new clmatrix_t structure (a matrix for the CLM4RM algorithms)
OpenCL boolean matrix data structure. Data is arranged in 32 bit words.
Definition: clm4rm.h:98
mzdMap free_mzd
set of recyclable mzd_t objects
Definition: matrix_pool.h:36
void clm4rm_free(clmatrix_t *gpu_matrix)
release memory (CPU and GPU)
std::pair< int, int > Key
Definition: matrix_pool.h:28
global definitions for all algorithms.
MatrixPool()
empty constructor
Definition: matrix_pool.cpp:19
std::list< mzd_t * > mzdList
Definition: matrix_pool.h:29
clmatrix_t * new_clmatrix_t(int rows, int cols, clm4rm_conditions *cond)
allocate a new clmatrix_t structure (a matrix for the CLM4RM algorithms)
Definition: matrix_pool.cpp:66
Manages OpenCL event dependencies; necessary when the queue is out-of-order; dependencies must be est...
Definition: clm4rm.h:227
mzd_t * new_mzd(int rows, int cols, MatrixPool *pool)
allocate a new mzd_t structure (a matrix for the M4RI algorithms)
rci_t ncols
Number of columns.
Definition: clm4rm.h:101
void clear()
release all resources
Definition: matrix_pool.cpp:94
std::list< clmatrix_t * > clmatrixList
Definition: matrix_pool.h:30
clmatrix_t * clm4rm_create(rci_t rows, rci_t cols, int rowpadding, int read_only, cl_context ctx)
create an empty matrix
Definition: clm4rm.cpp:233
mzd_t * new_mzd_t(int rows, int cols)
allocate a new mzd_t structure (a matrix for the M4RI algorithms)
Definition: matrix_pool.cpp:46
clmatrixList & getClmatrixFreeList(int rows, int cols)
Definition: matrix_pool.cpp:36
memory pool for matrix objects (M4RI matrices mzd_t* and OpenCL matrices clm4rm_t*)
Definition: matrix_pool.h:26
void reclaim(mzd_t *m, MatrixPool *pool)
reclaim an object (i.e. put it into the recycling list)
~MatrixPool()
destructor; releases all resources
Definition: matrix_pool.cpp:23
clmatrixMap free_clmatrix
set of recyclable clmatrix_t objects
Definition: matrix_pool.h:38
void reclaim(mzd_t *)
reclaim an object (i.e. put it into the recycling list)
Definition: matrix_pool.cpp:84
rci_t nrows
Number of rows.
Definition: clm4rm.h:99
static cl_context clContext()
Definition: concurrency.cpp:71
static cl_command_queue clQueue()
Definition: concurrency.cpp:76
mzdList & getMzdFreeList(int rows, int cols)
Definition: matrix_pool.cpp:27