C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 1

row_merged_numeric_table.h
1 /* file: row_merged_numeric_table.h */
2 /*******************************************************************************
3 * Copyright 2014-2017 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 /*
43 //++
44 // Implementation of row merged numeric table.
45 //--
46 */
47 
48 
49 #ifndef __ROW_MERGED_NUMERIC_TABLE_H__
50 #define __ROW_MERGED_NUMERIC_TABLE_H__
51 
52 #include "data_management/data/numeric_table.h"
53 #include "services/daal_memory.h"
54 #include "services/daal_defines.h"
55 #include "data_management/data/data_serialize.h"
56 
57 namespace daal
58 {
59 namespace data_management
60 {
61 
62 namespace interface1
63 {
72 class DAAL_EXPORT RowMergedNumericTable : public NumericTable
73 {
74 public:
75  DECLARE_SERIALIZABLE_TAG();
76  DECLARE_SERIALIZABLE_IMPL();
77 
82  RowMergedNumericTable();
83 
89  RowMergedNumericTable(NumericTablePtr table);
90 
95  static services::SharedPtr<RowMergedNumericTable> create(services::Status *stat = NULL);
96 
102  static services::SharedPtr<RowMergedNumericTable> create(const NumericTablePtr &nestedTable,
103  services::Status *stat = NULL);
104 
109  services::Status addNumericTable(NumericTablePtr table)
110  {
111  if (table->getDataLayout() & csrArray)
112  return services::Status(services::ErrorIncorrectTypeOfInputNumericTable);
113 
114  size_t ncols = getNumberOfColumns();
115  size_t cols = table->getNumberOfColumns();
116 
117  if (ncols != 0 && ncols != cols)
118  return services::Status(services::ErrorIncorrectNumberOfFeatures);
119 
120  _tables->push_back(table);
121 
122  if (ncols == 0)
123  {
124  DictionaryIface::FeaturesEqual featuresEqual = table->getDictionarySharedPtr()->getFeaturesEqual();
125  services::Status s;
126  _ddict = NumericTableDictionary::create(ncols, featuresEqual, &s);
127  if(!s)
128  return s;
129  s = setNumberOfColumnsImpl(cols);
130  if(!s)
131  return s;
132  if (featuresEqual == DictionaryIface::equal)
133  {
134  NumericTableFeature &f = table->getDictionarySharedPtr()->operator[](0);
135  _ddict->setFeature(f, 0);
136  }
137  else
138  {
139  for (size_t i = 0; i < cols; i++)
140  {
141  NumericTableFeature &f = table->getDictionarySharedPtr()->operator[](i);
142  _ddict->setFeature(f, i);
143  }
144  }
145  }
146 
147  size_t obs = table->getNumberOfRows();
148  return setNumberOfRowsImpl(_obsnum + obs);
149  }
150 
151  services::Status resize(size_t nrows) DAAL_C11_OVERRIDE
152  {
153  return services::Status(services::throwIfPossible(services::ErrorMethodNotSupported));
154  }
155 
156  MemoryStatus getDataMemoryStatus() const DAAL_C11_OVERRIDE
157  {
158  if (_tables->size() == 0)
159  {
160  return notAllocated;
161  }
162 
163  for (size_t i = 0;i < _tables->size(); i++)
164  {
165  NumericTable* nt = (NumericTable*)(_tables->operator[](i).get());
166  if (nt->getDataMemoryStatus() == notAllocated)
167  {
168  return notAllocated;
169  }
170  }
171 
172  return internallyAllocated;
173  }
174 
175  services::Status getBlockOfRows(size_t vector_idx, size_t vector_num,
176  ReadWriteMode rwflag, BlockDescriptor<double>& block) DAAL_C11_OVERRIDE
177  {
178  return getTBlock<double>(vector_idx, vector_num, rwflag, block);
179  }
180  services::Status getBlockOfRows(size_t vector_idx, size_t vector_num,
181  ReadWriteMode rwflag, BlockDescriptor<float>& block) DAAL_C11_OVERRIDE
182  {
183  return getTBlock<float>(vector_idx, vector_num, rwflag, block);
184  }
185  services::Status getBlockOfRows(size_t vector_idx, size_t vector_num,
186  ReadWriteMode rwflag, BlockDescriptor<int>& block) DAAL_C11_OVERRIDE
187  {
188  return getTBlock<int>(vector_idx, vector_num, rwflag, block);
189  }
190 
191  services::Status releaseBlockOfRows(BlockDescriptor<double>& block) DAAL_C11_OVERRIDE
192  {
193  return releaseTBlock<double>(block);
194  }
195  services::Status releaseBlockOfRows(BlockDescriptor<float>& block) DAAL_C11_OVERRIDE
196  {
197  return releaseTBlock<float>(block);
198  }
199  services::Status releaseBlockOfRows(BlockDescriptor<int>& block) DAAL_C11_OVERRIDE
200  {
201  return releaseTBlock<int>(block);
202  }
203 
204  services::Status getBlockOfColumnValues(size_t feature_idx, size_t vector_idx, size_t value_num,
205  ReadWriteMode rwflag, BlockDescriptor<double>& block) DAAL_C11_OVERRIDE
206  {
207  return getTFeature<double>(feature_idx, vector_idx, value_num, rwflag, block);
208  }
209  services::Status getBlockOfColumnValues(size_t feature_idx, size_t vector_idx, size_t value_num,
210  ReadWriteMode rwflag, BlockDescriptor<float>& block) DAAL_C11_OVERRIDE
211  {
212  return getTFeature<float>(feature_idx, vector_idx, value_num, rwflag, block);
213  }
214  services::Status getBlockOfColumnValues(size_t feature_idx, size_t vector_idx, size_t value_num,
215  ReadWriteMode rwflag, BlockDescriptor<int>& block) DAAL_C11_OVERRIDE
216  {
217  return getTFeature<int>(feature_idx, vector_idx, value_num, rwflag, block);
218  }
219 
220  services::Status releaseBlockOfColumnValues(BlockDescriptor<double>& block) DAAL_C11_OVERRIDE
221  {
222  return releaseTFeature<double>(block);
223  }
224  services::Status releaseBlockOfColumnValues(BlockDescriptor<float>& block) DAAL_C11_OVERRIDE
225  {
226  return releaseTFeature<float>(block);
227  }
228  services::Status releaseBlockOfColumnValues(BlockDescriptor<int>& block) DAAL_C11_OVERRIDE
229  {
230  return releaseTFeature<int>(block);
231  }
232 
233 protected:
234 
235  template<typename Archive, bool onDeserialize>
236  services::Status serialImpl( Archive *arch )
237  {
238  NumericTable::serialImpl<Archive, onDeserialize>( arch );
239 
240  arch->setSharedPtrObj(_tables);
241 
242  return services::Status();
243  }
244 
245 private:
246  template<typename T>
247  void internal_inner_repack( size_t idx, size_t rows, size_t ncols, T *src, T *dst )
248  {
249  size_t i, j;
250 
251  for(i = 0; i < rows; i++)
252  {
253  for(j = 0; j < ncols; j++)
254  {
255  dst[(idx + i) * ncols + j] = src[i * ncols + j];
256  }
257  }
258  }
259 
260  template<typename T>
261  void internal_outer_repack( size_t idx, size_t rows, size_t ncols, T *src, T *dst )
262  {
263  size_t i, j;
264 
265  for(i = 0; i < rows; i++)
266  {
267  for(j = 0; j < ncols; j++)
268  {
269  dst[i * ncols + j] = src[(i + idx) * ncols + j];
270  }
271  }
272  }
273 
274 protected:
275  template <typename T>
276  services::Status getTBlock(size_t idx, size_t nrows, int rwFlag, BlockDescriptor<T>& block)
277  {
278  services::Status s;
279  size_t ncols = getNumberOfColumns();
280  size_t nobs = getNumberOfRows();
281  block.setDetails( 0, idx, rwFlag );
282 
283  if (idx >= nobs)
284  {
285  block.resizeBuffer( ncols, 0 );
286  return services::Status();
287  }
288 
289  nrows = ( idx + nrows < nobs ) ? nrows : nobs - idx;
290 
291  if( !block.resizeBuffer( ncols, nrows ) )
292  return services::Status(services::ErrorMemoryAllocationFailed);
293 
294  if( rwFlag & (int)readOnly )
295  {
296  size_t rows = 0;
297  BlockDescriptor<T> innerBlock;
298  for (size_t k = 0; k < _tables->size() && rows < idx + nrows; k++)
299  {
300  NumericTable* nt = (NumericTable*)(_tables->operator[](k).get());
301  size_t lrows = nt->getNumberOfRows();
302 
303  if (rows + lrows > idx)
304  {
305  size_t idxBegin = (rows < idx) ? idx : rows;
306  size_t idxEnd = (rows + lrows < idx + nrows) ? rows + lrows : idx + nrows;
307  s |= nt->getBlockOfRows(idxBegin - rows, idxEnd - idxBegin, readOnly, innerBlock);
308 
309  internal_inner_repack<T>( idxBegin - idx, idxEnd - idxBegin, ncols, innerBlock.getBlockPtr(), block.getBlockPtr());
310 
311  s |= nt->releaseBlockOfRows(innerBlock);
312  }
313 
314  rows += lrows;
315  }
316  }
317  return s;
318  }
319 
320  template <typename T>
321  services::Status releaseTBlock(BlockDescriptor<T>& block)
322  {
323  services::Status s;
324  if(block.getRWFlag() & (int)writeOnly)
325  {
326  size_t ncols = getNumberOfColumns();
327  size_t nrows = block.getNumberOfRows();
328  size_t idx = block.getRowsOffset();
329  size_t rows = 0;
330  BlockDescriptor<T> innerBlock;
331  for (size_t k = 0; k < _tables->size() && rows < idx + nrows; k++)
332  {
333  NumericTable* nt = (NumericTable*)(_tables->operator[](k).get());
334  size_t lrows = nt->getNumberOfRows();
335 
336  if (rows + lrows > idx)
337  {
338  size_t idxBegin = (rows < idx) ? idx : rows;
339  size_t idxEnd = (rows + lrows < idx + nrows) ? rows + lrows : idx + nrows;
340  s |= nt->getBlockOfRows(idxBegin - rows, idxEnd - idxBegin, writeOnly, innerBlock);
341 
342  internal_outer_repack<T>( idxBegin - idx, idxEnd - idxBegin, ncols, block.getBlockPtr(), innerBlock.getBlockPtr());
343 
344  s |= nt->releaseBlockOfRows(innerBlock);
345  }
346 
347  rows += lrows;
348  }
349  }
350  block.reset();
351  return s;
352  }
353 
354  template <typename T>
355  services::Status getTFeature(size_t feat_idx, size_t idx, size_t nrows, int rwFlag, BlockDescriptor<T>& block)
356  {
357  services::Status s;
358  size_t ncols = getNumberOfColumns();
359  size_t nobs = getNumberOfRows();
360  block.setDetails( feat_idx, idx, rwFlag );
361 
362  if (idx >= nobs)
363  {
364  block.resizeBuffer( 1, 0 );
365  return services::Status();
366  }
367 
368  nrows = ( idx + nrows < nobs ) ? nrows : nobs - idx;
369  if( !block.resizeBuffer( 1, nrows ) )
370  return services::Status(services::ErrorMemoryAllocationFailed);
371 
372  if( rwFlag & (int)readOnly )
373  {
374  T* buffer = block.getBlockPtr();
375  size_t rows = 0;
376  for (size_t k = 0; k < _tables->size() && rows < idx + nrows; k++)
377  {
378  NumericTable* nt = (NumericTable*)(_tables->operator[](k).get());
379  size_t lrows = nt->getNumberOfRows();
380 
381  if (rows + lrows > idx)
382  {
383  size_t idxBegin = (rows < idx) ? idx : rows;
384  size_t idxEnd = (rows + lrows < idx + nrows) ? rows + lrows : idx + nrows;
385 
386  BlockDescriptor<T> innerBlock;
387  s |= nt->getBlockOfColumnValues(feat_idx, idxBegin - rows, idxEnd - idxBegin, readOnly, innerBlock);
388  T* location = innerBlock.getBlockPtr();
389  for (size_t i = idxBegin; i < idxEnd; i++)
390  {
391  buffer[i] = location[i - idxBegin];
392  }
393  s |= nt->releaseBlockOfColumnValues(innerBlock);
394  }
395 
396  rows += lrows;
397  }
398  }
399  return s;
400  }
401 
402  template <typename T>
403  services::Status releaseTFeature( BlockDescriptor<T>& block )
404  {
405  services::Status s;
406  if (block.getRWFlag() & (int)writeOnly)
407  {
408  size_t feat_idx = block.getColumnsOffset();
409  size_t idx = block.getRowsOffset();
410  size_t nrows = block.getNumberOfRows();
411  size_t rows = 0;
412  T* buffer = block.getBlockPtr();
413  for (size_t k = 0; k < _tables->size() && rows < idx + nrows; k++)
414  {
415  NumericTable* nt = (NumericTable*)(_tables->operator[](k).get());
416  size_t lrows = nt->getNumberOfRows();
417 
418  if (rows + lrows > idx)
419  {
420  size_t idxBegin = (rows < idx) ? idx : rows;
421  size_t idxEnd = (rows + lrows < idx + nrows) ? rows + lrows : idx + nrows;
422 
423  BlockDescriptor<T> innerBlock;
424  s |= nt->getBlockOfColumnValues(feat_idx, idxBegin - rows, idxEnd - idxBegin, writeOnly, innerBlock);
425  T* location = innerBlock.getBlockPtr();
426  for (size_t i = idxBegin; i < idxEnd; i++)
427  {
428  location[i - idxBegin] = buffer[i];
429  }
430  s |= nt->releaseBlockOfColumnValues(innerBlock);
431  }
432 
433  rows += lrows;
434  }
435  }
436  block.reset();
437  return s;
438  }
439 
440  services::Status setNumberOfColumnsImpl(size_t ncols) DAAL_C11_OVERRIDE;
441  services::Status allocateDataMemoryImpl(daal::MemType type = daal::dram) DAAL_C11_OVERRIDE;
442  void freeDataMemoryImpl() DAAL_C11_OVERRIDE;
443 
444 protected:
445  DataCollectionPtr _tables;
446 
447  RowMergedNumericTable(services::Status &st);
448 
449  RowMergedNumericTable(const NumericTablePtr &table, services::Status &st);
450 };
451 typedef services::SharedPtr<RowMergedNumericTable> RowMergedNumericTablePtr;
453 } // namespace interface1
454 using interface1::RowMergedNumericTable;
455 using interface1::RowMergedNumericTablePtr;
456 
457 } // namespace data_management
458 } // namespace daal
459 
460 #endif
daal
Definition: algorithm_base_common.h:57
daal::data_management::interface1::RowMergedNumericTable::getBlockOfRows
services::Status getBlockOfRows(size_t vector_idx, size_t vector_num, ReadWriteMode rwflag, BlockDescriptor< int > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:185
daal::data_management::interface1::RowMergedNumericTable::addNumericTable
services::Status addNumericTable(NumericTablePtr table)
Definition: row_merged_numeric_table.h:109
daal::data_management::interface1::RowMergedNumericTable::releaseBlockOfColumnValues
services::Status releaseBlockOfColumnValues(BlockDescriptor< double > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:220
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:170
daal::data_management::interface1::RowMergedNumericTable::getBlockOfColumnValues
services::Status getBlockOfColumnValues(size_t feature_idx, size_t vector_idx, size_t value_num, ReadWriteMode rwflag, BlockDescriptor< double > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:204
daal::dram
Definition: daal_defines.h:158
daal::services::ErrorIncorrectNumberOfFeatures
Definition: error_indexes.h:96
daal_defines.h
daal::algorithms::multivariate_outlier_detection::location
Definition: outlier_detection_multivariate_types.h:98
daal::data_management::interface1::RowMergedNumericTable::getBlockOfColumnValues
services::Status getBlockOfColumnValues(size_t feature_idx, size_t vector_idx, size_t value_num, ReadWriteMode rwflag, BlockDescriptor< int > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:214
daal::data_management::interface1::RowMergedNumericTable::releaseBlockOfColumnValues
services::Status releaseBlockOfColumnValues(BlockDescriptor< int > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:228
daal::data_management::interface1::NumericTableIface::MemoryStatus
MemoryStatus
Enumeration to specify the status of memory related to the Numeric Table.
Definition: numeric_table.h:298
daal::data_management::interface1::NumericTable
Class for a data management component responsible for representation of data in the numeric format...
Definition: numeric_table.h:600
daal::data_management::interface1::RowMergedNumericTable::releaseBlockOfRows
services::Status releaseBlockOfRows(BlockDescriptor< int > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:199
daal::data_management::interface1::RowMergedNumericTable::getBlockOfColumnValues
services::Status getBlockOfColumnValues(size_t feature_idx, size_t vector_idx, size_t value_num, ReadWriteMode rwflag, BlockDescriptor< float > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:209
daal::data_management::interface1::RowMergedNumericTable::getDataMemoryStatus
MemoryStatus getDataMemoryStatus() const DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:156
daal::services::ErrorIncorrectTypeOfInputNumericTable
Definition: error_indexes.h:117
daal::data_management::interface1::RowMergedNumericTable::getBlockOfRows
services::Status getBlockOfRows(size_t vector_idx, size_t vector_num, ReadWriteMode rwflag, BlockDescriptor< float > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:180
daal::data_management::interface1::BlockDescriptor
Base class that manages buffer memory for read/write operations required by numeric tables...
Definition: numeric_table.h:81
daal::data_management::interface1::DictionaryIface::FeaturesEqual
FeaturesEqual
Enumeration to specify whether the Data Dictionary contains equal features.
Definition: data_dictionary.h:171
daal::data_management::interface1::RowMergedNumericTable::getBlockOfRows
services::Status getBlockOfRows(size_t vector_idx, size_t vector_num, ReadWriteMode rwflag, BlockDescriptor< double > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:175
daal::data_management::interface1::RowMergedNumericTable
Class that provides methods to access a collection of numeric tables as if they are joined by rows...
Definition: row_merged_numeric_table.h:72
daal::data_management::interface1::Dictionary::create
static services::SharedPtr< Dictionary > create(size_t nfeat, FeaturesEqual featuresEqual=notEqual, services::Status *stat=NULL)
Definition: data_dictionary.h:209
daal::data_management::interface1::RowMergedNumericTable::releaseBlockOfRows
services::Status releaseBlockOfRows(BlockDescriptor< float > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:195
daal::data_management::interface1::NumericTableFeature
Data structure describes the Numeric Table feature.
Definition: data_dictionary.h:74
daal::data_management::interface1::NumericTable::getDataMemoryStatus
virtual MemoryStatus getDataMemoryStatus() const
Definition: numeric_table.h:745
daal::MemType
MemType
Definition: daal_defines.h:156
daal::data_management::interface1::RowMergedNumericTable::resize
services::Status resize(size_t nrows) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:151
daal::data_management::interface1::RowMergedNumericTable::releaseBlockOfColumnValues
services::Status releaseBlockOfColumnValues(BlockDescriptor< float > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:224
daal::services::ErrorMethodNotSupported
Definition: error_indexes.h:95
daal::data_management::interface1::RowMergedNumericTable::releaseBlockOfRows
services::Status releaseBlockOfRows(BlockDescriptor< double > &block) DAAL_C11_OVERRIDE
Definition: row_merged_numeric_table.h:191

For more complete information about compiler optimizations, see our Optimization Notice.