From 8088a2b140e9554eaa6758159be95ba75070a63e Mon Sep 17 00:00:00 2001 From: dmitrypa Date: Mon, 23 Sep 2019 21:03:39 +0300 Subject: [PATCH 1/2] SDL: err-memory-handling --- .../assoc_rules_apriori_discover_impl.i | 27 ++++-- .../assocrules/assoc_rules_apriori_impl.i | 23 +++-- .../assocrules/assoc_rules_apriori_itemset.i | 15 ++- .../assocrules/assoc_rules_apriori_kernel.h | 16 ++-- .../assoc_rules_apriori_mine_impl.i | 91 ++++++++++++++++--- .../assocrules/assoc_rules_apriori_tree.i | 52 ++++++++--- .../assocrules/assoc_rules_apriori_types.i | 69 +++++++++++++- algorithms/kernel/dtrees/dtrees_model.cpp | 1 + .../df_classification_model.cpp | 6 +- .../df_classification_model_builder.cpp | 7 +- .../forest/regression/df_regression_model.cpp | 6 +- .../gbt_classification_model_builder.cpp | 5 +- algorithms/kernel/dtrees/gbt/gbt_model.cpp | 5 +- algorithms/kernel/dtrees/gbt/gbt_model_impl.h | 16 +++- .../dtrees/gbt/gbt_train_tree_builder.i | 3 +- .../gbt_regression_model_builder.cpp | 7 +- ...assification_predict_dense_default_batch.h | 2 +- ...ication_predict_dense_default_batch_impl.i | 15 ++- ..._classification_train_dense_default_impl.i | 53 +++++++++-- .../kdtree_knn_classification_train_kernel.h | 5 +- .../k_nearest_neighbors/kdtree_knn_impl.i | 11 ++- .../kmeans/inner/kmeans_init_container_v1.h | 1 + .../kernel/kmeans/kmeans_init_container.h | 1 + .../lasso_regression_training_result_fpt.cpp | 1 + .../low_order_moments_impl.i | 4 +- .../multiclassclassifier_model.cpp | 2 + .../naivebayes/naivebayes_train_container.h | 1 + .../iterative_solver_kernel.h | 2 + .../kernel/qr/qr_dense_default_distr_step2.h | 4 +- .../kernel/qr/qr_dense_default_online.h | 7 +- algorithms/kernel/stump/stump_train_impl.i | 1 + .../kernel/svd/svd_dense_default_online.h | 7 +- algorithms/kernel/svm/svm_predict_impl.i | 4 +- algorithms/kernel/svm/svm_train_boser_cache.i | 8 +- 34 files changed, 382 insertions(+), 96 deletions(-) mode change 100644 => 100755 algorithms/kernel/assocrules/assoc_rules_apriori_kernel.h mode change 100644 => 100755 algorithms/kernel/dtrees/forest/classification/df_classification_model.cpp mode change 100644 => 100755 algorithms/kernel/dtrees/forest/classification/df_classification_model_builder.cpp mode change 100644 => 100755 algorithms/kernel/dtrees/forest/regression/df_regression_model.cpp mode change 100644 => 100755 algorithms/kernel/dtrees/gbt/classification/gbt_classification_model_builder.cpp mode change 100644 => 100755 algorithms/kernel/dtrees/gbt/gbt_model.cpp mode change 100644 => 100755 algorithms/kernel/dtrees/gbt/gbt_model_impl.h mode change 100644 => 100755 algorithms/kernel/dtrees/gbt/gbt_train_tree_builder.i mode change 100644 => 100755 algorithms/kernel/dtrees/gbt/regression/gbt_regression_model_builder.cpp mode change 100644 => 100755 algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch.h mode change 100644 => 100755 algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_kernel.h mode change 100644 => 100755 algorithms/kernel/lasso_regression/lasso_regression_training_result_fpt.cpp mode change 100644 => 100755 algorithms/kernel/qr/qr_dense_default_distr_step2.h mode change 100644 => 100755 algorithms/kernel/qr/qr_dense_default_online.h mode change 100644 => 100755 algorithms/kernel/stump/stump_train_impl.i mode change 100644 => 100755 algorithms/kernel/svd/svd_dense_default_online.h mode change 100644 => 100755 algorithms/kernel/svm/svm_predict_impl.i mode change 100644 => 100755 algorithms/kernel/svm/svm_train_boser_cache.i diff --git a/algorithms/kernel/assocrules/assoc_rules_apriori_discover_impl.i b/algorithms/kernel/assocrules/assoc_rules_apriori_discover_impl.i index 362ee17e28a..456044837b5 100755 --- a/algorithms/kernel/assocrules/assoc_rules_apriori_discover_impl.i +++ b/algorithms/kernel/assocrules/assoc_rules_apriori_discover_impl.i @@ -112,7 +112,7 @@ void AssociationRulesKernel::setIntersection(cons * */ template -void AssociationRulesKernel::firstPass( +services::Status AssociationRulesKernel::firstPass( double minConfidence, ItemSetList *L, size_t itemSetSize, const size_t *items, size_t itemsSupport, size_t *leftItems, AssocRule *R, size_t& numRules, @@ -128,7 +128,13 @@ void AssociationRulesKernel::firstPass( for (size_t j = i + 1; j <= itemSetSize; ++j) { leftItems[j - 1] = items[j]; } const assocrules_itemset *left_iset = findItemSet(itemSetSize, leftItems, L_prev); + DAAL_CHECK(left_iset, services::ErrorNullInput); + DAAL_CHECK_STATUS_OK(left_iset->ok(), left_iset->getLastStatus()); + const assocrules_itemset *right_iset = findItemSet(1, &items[i], L_0); + DAAL_CHECK(right_iset, services::ErrorNullInput); + DAAL_CHECK_STATUS_OK(right_iset->ok(), right_iset->getLastStatus()); + double confidence = (double)itemsSupport / (double)(left_iset->support.get()); if (confidence >= minConfidence) { @@ -138,6 +144,7 @@ void AssociationRulesKernel::firstPass( } } numRulesFound = numRules - oldNumRules; + return services::Status(); } /** @@ -158,7 +165,7 @@ void AssociationRulesKernel::firstPass( * */ template -void AssociationRulesKernel::nextPass(double minConfidence, ItemSetList *L, +services::Status AssociationRulesKernel::nextPass(double minConfidence, ItemSetList *L, size_t right_size, size_t itemsSupport, size_t *leftItems, AssocRule *R, size_t& numRules, size_t& numLeft, size_t& numRight, size_t& numRulesFound, bool& found) @@ -189,6 +196,8 @@ void AssociationRulesKernel::nextPass(double minC } assocrules_itemset iset(right_size, first_items, second_items[right_size - 2]); + DAAL_CHECK_STATUS_OK(iset.ok(), iset.getLastStatus()); + const assocrules_itemset *right_iset = findItemSet(right_size, iset.items, L[right_size - 1]); first_items = R[firstIdx ].left->items; second_items = R[secondIdx].left->items; @@ -211,6 +220,7 @@ void AssociationRulesKernel::nextPass(double minC } } numRulesFound = numRules - oldNumRules; + return services::Status(); } /** @@ -227,7 +237,7 @@ void AssociationRulesKernel::nextPass(double minC * */ template -bool AssociationRulesKernel::generateRules(double minConfidence, size_t minItemsetSize, +services::Status AssociationRulesKernel::generateRules(double minConfidence, size_t minItemsetSize, size_t L_size, ItemSetList *L, AssocRule *R, size_t& numRules, size_t& numLeft, size_t& numRight) { @@ -238,7 +248,7 @@ bool AssociationRulesKernel::generateRules(double TArray leftItemsAr(L_size); size_t *leftItems = leftItemsAr.get(); if(!leftItems) - return false; + return services::ErrorMemoryAllocationFailed; /* Generate all association rules */ size_t startItemsetSize = 1; @@ -254,19 +264,22 @@ bool AssociationRulesKernel::generateRules(double size_t n_rules_prev = 0; /* Find rules that have 1 item in the right part */ - firstPass(minConfidence, L, iset_size, items, itemsSupport, + services::Status statFirstPass = firstPass(minConfidence, L, iset_size, items, itemsSupport, leftItems, R, numRules, numLeft, numRight, n_rules_prev); + DAAL_CHECK_STATUS_OK(statFirstPass.ok(), statFirstPass); bool found = (n_rules_prev > 0); for (size_t right_size = 2; right_size <= iset_size && found; ++right_size) { /* Find rules that have right_size items in the right part */ - nextPass(minConfidence, L, right_size, itemsSupport, + services::Status statNextPass = nextPass(minConfidence, L, right_size, itemsSupport, leftItems, R, numRules, numLeft, numRight, n_rules_prev, found); + + DAAL_CHECK_STATUS_OK(statNextPass.ok(), statNextPass); } } } - return true; + return services::Status(); } template diff --git a/algorithms/kernel/assocrules/assoc_rules_apriori_impl.i b/algorithms/kernel/assocrules/assoc_rules_apriori_impl.i index 6ff96e34758..8ae00840137 100755 --- a/algorithms/kernel/assocrules/assoc_rules_apriori_impl.i +++ b/algorithms/kernel/assocrules/assoc_rules_apriori_impl.i @@ -58,6 +58,7 @@ Status AssociationRulesKernel::compute(const Nume /* Create association rules data set from input numeric table */ assocrules_dataset data(dataTable, parameter->nTransactions, parameter->nUniqueItems, minSupport); + DAAL_CHECK_STATUS_OK(data.ok(), data.getLastStatus()); TArray, cpu> L(data.numOfUniqueItems); DAAL_CHECK(L.get(), ErrorMemoryAllocationFailed); @@ -67,8 +68,10 @@ Status AssociationRulesKernel::compute(const Nume /* Find "large" itemsets */ size_t L_size = 0; size_t maxItemsetSize = ((parameter->maxItemsetSize == 0) ? (size_t) - 1 : parameter->maxItemsetSize); - DAAL_CHECK(findLargeItemsets((size_t)daal::internal::Math::sCeil(minSupport * data.numOfTransactions), - maxItemsetSize, data, L.get(), L_size), ErrorAprioriIncorrectInputData); + services::Status statLargeItemset = findLargeItemsets((size_t)daal::internal::Math::sCeil(minSupport * data.numOfTransactions), + maxItemsetSize, data, L.get(), L_size); + DAAL_CHECK_STATUS_OK(statLargeItemset.ok(), statLargeItemset); DAAL_ASSERT(L_size > 0); /* Allocate memory to store "large" itemsets */ @@ -99,7 +102,8 @@ Status AssociationRulesKernel::compute(const Nume size_t nLeft = 0; /*minConfidence; - DAAL_CHECK(generateRules(minConfidence, minItemsetSize, L_size, L.get(), R.get(), nRules, nLeft, nRight) && !!nRules, ErrorMemoryAllocationFailed); + services::Status statGenRules = generateRules(minConfidence, minItemsetSize, L_size, L.get(), R.get(), nRules, nLeft, nRight); + DAAL_CHECK_STATUS_OK(statGenRules.ok() && !!nRules, statGenRules); NumericTable *leftItemsTable = r[2]; NumericTable *rightItemsTable = r[3]; @@ -115,14 +119,14 @@ Status AssociationRulesKernel::compute(const Nume } template -bool AssociationRulesKernel::findLargeItemsets(size_t minSupport, size_t maxItemsetSize, +services::Status AssociationRulesKernel::findLargeItemsets(size_t minSupport, size_t maxItemsetSize, assocrules_dataset &data, ItemSetList *L, size_t& L_size) { /* Form list of "large" item sets of size 1 from the unique items which count is not less than minimum count 'imin_s' */ - if(!firstPass(minSupport, data, *L)) - return false; + services::Status statFirstPass = firstPass(minSupport, data, *L); + DAAL_CHECK_STATUS_OK(statFirstPass.ok(), statFirstPass); L_size = 1; /* Find "large" item sets of size k+1 from itemsets of size k */ @@ -131,13 +135,14 @@ bool AssociationRulesKernel::findLargeItemsets(si hash_tree *C_tree = NULL; do { - C_tree = nextPass(minSupport, k++, data, L, L_size, bFound, C_tree); + services::Status s; + C_tree = nextPass(minSupport, k++, data, L, L_size, bFound, C_tree, s); + DAAL_CHECK_STATUS_OK(s.ok(), s); } while(bFound && k < maxItemsetSize); delete C_tree; - C_tree = nullptr; - return L_size > 0; + return (L_size > 0) ? services::Status() : services::ErrorAprioriIncorrectInputData; } template diff --git a/algorithms/kernel/assocrules/assoc_rules_apriori_itemset.i b/algorithms/kernel/assocrules/assoc_rules_apriori_itemset.i index 2a9970762f7..c11878da3aa 100755 --- a/algorithms/kernel/assocrules/assoc_rules_apriori_itemset.i +++ b/algorithms/kernel/assocrules/assoc_rules_apriori_itemset.i @@ -95,11 +95,24 @@ struct assocrules_itemset size_t *items; /* : public Kernel /** Find "large" item sets and build association rules */ services::Status compute(const NumericTable *a, NumericTable *r[], const daal::algorithms::Parameter *parameter); protected: - bool findLargeItemsets(size_t minSupport, size_t maxItemsetSize, assocrules_dataset &data, ItemSetList *L, size_t& L_size); + services::Status findLargeItemsets(size_t minSupport, size_t maxItemsetSize, assocrules_dataset &data, ItemSetList *L, size_t& L_size); Status allocateItemsetsTableData(ItemSetList *L, size_t L_size, size_t minItemsetSize, NumericTable *largeItemsetsTable, NumericTable *largeItemsetsSupportTable, @@ -68,11 +68,11 @@ class AssociationRulesKernel : public Kernel */ /** Find "large" itemsets of size 1 */ - bool firstPass(size_t imin_s, assocrules_dataset &data, ItemSetList& l); + services::Status firstPass(size_t imin_s, assocrules_dataset &data, ItemSetList& l); /** Generate "large" item sets of size k+1 from "large" item sets of size k */ hash_tree * nextPass(size_t imin_s, size_t iset_size, assocrules_dataset &data, ItemSetList *L, size_t& L_size, - bool& bFound, hash_tree *C_tree); + bool& bFound, hash_tree *C_tree, services::Status& s); /** Test that all {n-1}-item subsets of {n}-item set are "large" item sets */ bool pruneCandidate(size_t iset_size, const size_t *cadidate, size_t *subset, hash_tree &C_tree); @@ -80,11 +80,11 @@ class AssociationRulesKernel : public Kernel size_t binarySearch(size_t nUniqueItems, assocRulesUniqueItem *uniqueItems, size_t itemID); assocrules_itemset * genCandidate(size_t iset_size, size_t *first_items, size_t second_item, - size_t *subset_buf, hash_tree *C_tree); + size_t *subset_buf, hash_tree *C_tree, services::Status& s); /** Generate candidate itemsets of size iset_size+1 from "large" itemsets of size iset_size */ bool genCandidates(size_t iset_size, ItemSetList *L, hash_tree *C_tree, - size_t nUniqueItems, assocRulesUniqueItem *uniqueItems); + size_t nUniqueItems, assocRulesUniqueItem *uniqueItems, services::Status& s); /** Generate all subsets of size iset_size from a transaction and hash those subsets using hash tree of candidate itemsets C_tree to increment support values of candidates */ @@ -106,18 +106,18 @@ class AssociationRulesKernel : public Kernel void setIntersection(const size_t *a, size_t aSize, const size_t *b, size_t bSize, size_t *c, size_t& cSize); /** Find rules containing 1 item on the right */ - void firstPass(double minConfidence, ItemSetList *L, size_t itemSetSize, const size_t *items, size_t itemsSupport, + services::Status firstPass(double minConfidence, ItemSetList *L, size_t itemSetSize, const size_t *items, size_t itemsSupport, size_t *leftItems, AssocRule *R, size_t& numRules, size_t& numLeft, size_t& numRight, size_t& numRulesFound); /** Generate rules that have k+1 items on the right from the rules that have k items on the right */ - void nextPass(double minConfidence, ItemSetList *L, size_t right_size, + services::Status nextPass(double minConfidence, ItemSetList *L, size_t right_size, size_t itemsSupport, size_t *leftItems, AssocRule *R, size_t& numRules, size_t& numLeft, size_t& numRight, size_t& numRulesFound, bool& found); /** Generate association rules from "large" item sets */ - bool generateRules(double minConfidence, size_t minItemsetSize, size_t L_size, ItemSetList *L, + services::Status generateRules(double minConfidence, size_t minItemsetSize, size_t L_size, ItemSetList *L, AssocRule *R, size_t& numRules, size_t& numLeft, size_t& numRight); /** Store association rules into continuous memory */ diff --git a/algorithms/kernel/assocrules/assoc_rules_apriori_mine_impl.i b/algorithms/kernel/assocrules/assoc_rules_apriori_mine_impl.i index b0efe041e4c..18810a8fda8 100755 --- a/algorithms/kernel/assocrules/assoc_rules_apriori_mine_impl.i +++ b/algorithms/kernel/assocrules/assoc_rules_apriori_mine_impl.i @@ -50,10 +50,10 @@ namespace internal * \param imin_s[in] minimum support * \param data[in] input data set * \param L[out] structure containing "large" itemsets - * \return true if added something + * \return Status object */ template -bool AssociationRulesKernel::firstPass(size_t imin_s, +services::Status AssociationRulesKernel::firstPass(size_t imin_s, assocrules_dataset &data, ItemSetList& l) { size_t numOfUniqueItems = data.numOfUniqueItems; @@ -64,9 +64,12 @@ bool AssociationRulesKernel::firstPass(size_t imi { size_t item_id = uniqueItems[i].itemID; size_t item_support = uniqueItems[i].support; - l.insert(new assocrules_itemset(item_id, item_support)); + assocrules_itemset* ai = new assocrules_itemset(item_id, item_support); + DAAL_CHECK_MALLOC(ai); + DAAL_CHECK_STATUS_OK(ai->ok(), ai->getLastStatus()); + l.insert(ai); } - return numOfUniqueItems > 0; + return (numOfUniqueItems > 0) ? services::Status() : services::ErrorAprioriIncorrectInputData; } /** @@ -102,11 +105,26 @@ bool AssociationRulesKernel::pruneCandidate(size_ template assocrules_itemset *AssociationRulesKernel::genCandidate( - size_t iset_size, size_t *first_items, size_t second_item, size_t *subset_buf, hash_tree *C_tree) + size_t iset_size, size_t *first_items, size_t second_item, size_t *subset_buf, hash_tree *C_tree, services::Status& s) { + s = services::Status(); /* Create candidate itemset */ assocrules_itemset *iset = new assocrules_itemset(iset_size + 1, first_items, second_item); + if (!iset) + { + s = services::ErrorMemoryAllocationFailed; + return nullptr; + } + + if (!iset->ok()) + { + s = iset->getLastStatus(); + delete iset; + iset = nullptr; + return nullptr; + } + /* Check apriori property for candidate itemset */ if (pruneCandidate(iset_size + 1, iset->items, subset_buf, *C_tree)) { @@ -141,12 +159,15 @@ size_t AssociationRulesKernel::binarySearch(size_ * \param iset_size[in] length of input itemsets * \param L[in] structure containing "large" itemsets * \return false if no candidates were generated, true otherwise + * \param outputStatus[out] Status object that indicates tehe result of memory allocation */ template bool AssociationRulesKernel::genCandidates(size_t iset_size, ItemSetList *L, hash_tree *C_tree, - size_t nUniqueItems, assocRulesUniqueItem *uniqueItems) + size_t nUniqueItems, assocRulesUniqueItem *uniqueItems, + services::Status& outputStatus) { + outputStatus = services::Status(); size_t new_iset_size = iset_size + 1; ItemSetList *L_prev = &(L[iset_size - 1]); ItemSetList *L_cur = &(L[iset_size]); @@ -164,8 +185,25 @@ bool AssociationRulesKernel::genCandidates(size_t { for (size_t j = i + 1; j < nUniqueItems; j++) { - L_cur->insert(new assocrules_itemset(new_iset_size, - &(uniqueItems[i].itemID), uniqueItems[j].itemID)); + services::Status s; + assocrules_itemset *ai = new assocrules_itemset(new_iset_size, + &(uniqueItems[i].itemID), uniqueItems[j].itemID); + + if (!ai) + { + outputStatus = services::ErrorMemoryAllocationFailed; + return false; + } + + if (!ai->ok()) + { + delete ai; + ai = nullptr; + outputStatus = ai->getLastStatus(); + return false; + } + + L_cur->insert(ai); } } } @@ -180,7 +218,15 @@ bool AssociationRulesKernel::genCandidates(size_t size_t start_item_id = binarySearch(nUniqueItems, uniqueItems, last_item); for (size_t k = start_item_id; k < nUniqueItems; k++) { - iset = genCandidate(iset_size, first_items, uniqueItems[k].itemID, subset_buf, C_tree); + services::Status s; + iset = genCandidate(iset_size, first_items, uniqueItems[k].itemID, subset_buf, C_tree, s); + + if (!s.ok()) + { + outputStatus = s; + return false; + } + if (iset) { L_cur->insert(iset); } } } @@ -349,20 +395,41 @@ void AssociationRulesKernel::prune(size_t imin_s, * \param L_size[out] size of the array L * \param bFound[out] flag. true, if at least 2 "large" item sets of size k+1 were found; * false, otherwise + * \param s[out] Status object that indicates the result of memory allocation */ template hash_tree *AssociationRulesKernel::nextPass(size_t imin_s, size_t iset_size, assocrules_dataset &data, ItemSetList *L, size_t& L_size, bool& bFound, - hash_tree *C_tree) + hash_tree *C_tree, services::Status& s) { - bFound = genCandidates(iset_size, L, C_tree, data.numOfUniqueItems, data.uniq_items); + s = services::Status(); + bFound = genCandidates(iset_size, L, C_tree, data.numOfUniqueItems, data.uniq_items, s); + + if (!s.ok()) + return nullptr; + delete C_tree; C_tree = nullptr; if(!bFound) return nullptr; hash_tree *C_tree_new = new hash_tree(iset_size + 1, L[iset_size]); - prune(imin_s, iset_size, data, L, C_tree_new); + if (!C_tree_new) + { + bFound = false; + s = services::ErrorMemoryAllocationFailed; + return nullptr; + } + if (!C_tree_new->ok()) + { + bFound = false; + s = C_tree_new->getLastStatus(); + delete C_tree_new; + C_tree_new = nullptr; + return nullptr; + } + + prune(imin_s, iset_size, data, L, C_tree_new); if (L[iset_size].size > 0) { ++L_size; } if (L[iset_size].size < 2) { bFound = false; } return C_tree_new; diff --git a/algorithms/kernel/assocrules/assoc_rules_apriori_tree.i b/algorithms/kernel/assocrules/assoc_rules_apriori_tree.i index 66ec7e8ffcc..451f9b7655c 100755 --- a/algorithms/kernel/assocrules/assoc_rules_apriori_tree.i +++ b/algorithms/kernel/assocrules/assoc_rules_apriori_tree.i @@ -87,21 +87,45 @@ struct hash_tree order_m1 = order - 1; n_nodes_i = (int *)daal::services::daal_malloc(height * sizeof(int)); - n_nodes_i[0] = 1; - n_nodes = 1; - for (int i = 1; i < height; i++) + if (n_nodes_i) { - n_nodes_i[i] = n_nodes_i[i - 1] * order; - n_nodes += n_nodes_i[i]; - } - n_leaves = n_nodes_i[height - 1] * order; + n_nodes_i[0] = 1; + n_nodes = 1; + for (int i = 1; i < height; i++) + { + n_nodes_i[i] = n_nodes_i[i - 1] * order; + n_nodes += n_nodes_i[i]; + } + n_leaves = n_nodes_i[height - 1] * order; - root = new hash_tree_node[n_nodes]; - leaves = new hash_tree_leaf[n_leaves]; + root = new hash_tree_node[n_nodes]; + leaves = new hash_tree_leaf[n_leaves]; - for (auto *current = L_cur.start; current; current = current->next()) + if (root && leaves) + { + for (auto *current = L_cur.start; current; current = current->next()) + { + insert_itemset(current->itemSet()); + } + + _status = services::Status(); + } + else + { + if (root) + delete[] root; + + if (leaves) + delete[] leaves; + + root = NULL; + leaves = NULL; + _status = services::ErrorMemoryAllocationFailed; + } + } + else { - insert_itemset(current->itemSet()); + _status = services::ErrorMemoryAllocationFailed; } } @@ -220,6 +244,12 @@ struct hash_tree static const int minLogOrder = 3; static const int maxHeight = 10; + bool ok() const { return _status.ok(); } + services::Status getLastStatus() const { return _status; } + +protected: + services::Status _status; + private: hash_tree(const hash_tree &) {}; }; diff --git a/algorithms/kernel/assocrules/assoc_rules_apriori_types.i b/algorithms/kernel/assocrules/assoc_rules_apriori_types.i index 26c758aaf66..40e4bf3eb70 100755 --- a/algorithms/kernel/assocrules/assoc_rules_apriori_types.i +++ b/algorithms/kernel/assocrules/assoc_rules_apriori_types.i @@ -204,6 +204,7 @@ struct assocrules_dataset assocrules_dataset(NumericTable *dataTable, size_t _numOfTransactions, size_t _numOfUniqueItems, double minSupport) : tran(nullptr), large_tran(nullptr), uniq_items(nullptr), numOfTransactions(0) { + _status = services::Status(); size_t data_len = dataTable->getNumberOfRows(); size_t itemsFullNumber; @@ -226,6 +227,13 @@ struct assocrules_dataset } size_t *supportVals = (size_t *)daal::services::internal::service_calloc(itemsFullNumber); + + if (!supportVals) + { + _status = services::ErrorMemoryAllocationFailed; + return; + } + for (size_t i = 0; i < data_len; i++) { supportVals[itemID[i]]++; @@ -237,6 +245,16 @@ struct assocrules_dataset if (supportVals[i] >= iMinSupport) { numOfUniqueItems++; } } uniq_items = new assocRulesUniqueItem[numOfUniqueItems]; + + if (!uniq_items) + { + daal::services::daal_free(supportVals); + supportVals = nullptr; + + _status = services::ErrorMemoryAllocationFailed; + return; + } + numOfUniqueItems = 0; for (size_t i = 0; i < itemsFullNumber; i++) { @@ -250,9 +268,49 @@ struct assocrules_dataset tran = new assocrules_transaction[numOfTransactions]; large_tran = new assocrules_transaction *[numOfTransactions]; + if (!tran) + { + daal::services::daal_free(supportVals); + delete[] uniq_items; + supportVals = nullptr; + uniq_items = nullptr; + + _status = services::ErrorMemoryAllocationFailed; + return; + } + + if (!large_tran) + { + daal::services::daal_free(supportVals); + delete[] tran; + delete[] uniq_items; + supportVals = nullptr; + tran = nullptr; + uniq_items = nullptr; + + _status = services::ErrorMemoryAllocationFailed; + return; + } + size_t numItems = 0; size_t *items = (size_t *)daal::services::daal_malloc(numOfUniqueItems * sizeof(size_t)); + if (!items) + { + daal::services::daal_free(supportVals); + delete[] tran; + delete[] large_tran; + delete[] uniq_items; + + supportVals = nullptr; + tran = nullptr; + large_tran = nullptr; + uniq_items = nullptr; + + _status = services::ErrorMemoryAllocationFailed; + return; + } + for (size_t i = 0; i < data_len; i++) { if (supportVals[itemID[i]] >= iMinSupport) @@ -303,8 +361,17 @@ struct assocrules_dataset assocRulesUniqueItem *uniq_items; /* treeTablePtr(new DecisionTreeTable(nNodes)); const size_t nRows = treeTablePtr->getNumberOfRows(); DecisionTreeNode* const pNodes = (DecisionTreeNode*)treeTablePtr->getArray(); + DAAL_CHECK_MALLOC(pNodes) pNodes[0].featureIndex = __NODE_RESERVED_ID; pNodes[0].leftIndexOrClass = 0; pNodes[0].featureValueOrResponse = 0; diff --git a/algorithms/kernel/dtrees/forest/classification/df_classification_model.cpp b/algorithms/kernel/dtrees/forest/classification/df_classification_model.cpp old mode 100644 new mode 100755 index 43ebc6563ef..58a18c54ed5 --- a/algorithms/kernel/dtrees/forest/classification/df_classification_model.cpp +++ b/algorithms/kernel/dtrees/forest/classification/df_classification_model.cpp @@ -229,12 +229,14 @@ bool ModelImpl::add(const TreeType& tree) size_t i = _nTree.inc(); const size_t nNode = tree.getNumberOfNodes(); - Status st; - auto pTbl = new DecisionTreeTable(nNode); auto impTbl = new HomogenNumericTable(1, nNode, NumericTable::doAllocate); auto nodeSamplesTbl = new HomogenNumericTable(1, nNode, NumericTable::doAllocate); + DAAL_CHECK_STATUS_VAR(pTbl) + DAAL_CHECK_STATUS_VAR(impTbl) + DAAL_CHECK_STATUS_VAR(nodeSamplesTbl) + tree.convertToTable(pTbl, impTbl, nodeSamplesTbl); (*_serializationData)[i - 1].reset(pTbl); diff --git a/algorithms/kernel/dtrees/forest/classification/df_classification_model_builder.cpp b/algorithms/kernel/dtrees/forest/classification/df_classification_model_builder.cpp old mode 100644 new mode 100755 index 853efce6029..a637a0985ea --- a/algorithms/kernel/dtrees/forest/classification/df_classification_model_builder.cpp +++ b/algorithms/kernel/dtrees/forest/classification/df_classification_model_builder.cpp @@ -43,15 +43,16 @@ namespace interface1 services::Status ModelBuilder::initialize(size_t nClasses, size_t nTrees) { - services::Status s; - _model.reset(new decision_forest::classification::internal::ModelImpl()); + auto modelImpl = new decision_forest::classification::internal::ModelImpl(); + DAAL_CHECK_MALLOC(modelImpl) + _model.reset(modelImpl); decision_forest::classification::internal::ModelImpl& modelImplRef = daal::algorithms::dtrees::internal::getModelRef(_model); modelImplRef.resize(nTrees); modelImplRef._impurityTables.reset(); modelImplRef._nNodeSampleTables.reset(); modelImplRef._nTree.set(nTrees); - return s; + return Status(); } services::Status ModelBuilder::createTreeInternal(size_t nNodes, TreeId& resId) diff --git a/algorithms/kernel/dtrees/forest/regression/df_regression_model.cpp b/algorithms/kernel/dtrees/forest/regression/df_regression_model.cpp old mode 100644 new mode 100755 index f1bed35e8d7..3b705359209 --- a/algorithms/kernel/dtrees/forest/regression/df_regression_model.cpp +++ b/algorithms/kernel/dtrees/forest/regression/df_regression_model.cpp @@ -178,12 +178,14 @@ bool ModelImpl::add(const TreeType& tree) size_t i = _nTree.inc(); const size_t nNode = tree.getNumberOfNodes(); - Status st; - auto pTbl = new DecisionTreeTable(nNode); auto impTbl = new HomogenNumericTable(1, nNode, NumericTable::doAllocate); auto nodeSamplesTbl = new HomogenNumericTable(1, nNode, NumericTable::doAllocate); + DAAL_CHECK_STATUS_VAR(pTbl) + DAAL_CHECK_STATUS_VAR(impTbl) + DAAL_CHECK_STATUS_VAR(nodeSamplesTbl) + tree.convertToTable(pTbl, impTbl, nodeSamplesTbl); (*_serializationData)[i - 1].reset(pTbl); diff --git a/algorithms/kernel/dtrees/gbt/classification/gbt_classification_model_builder.cpp b/algorithms/kernel/dtrees/gbt/classification/gbt_classification_model_builder.cpp old mode 100644 new mode 100755 index 77394449b70..26540f01367 --- a/algorithms/kernel/dtrees/gbt/classification/gbt_classification_model_builder.cpp +++ b/algorithms/kernel/dtrees/gbt/classification/gbt_classification_model_builder.cpp @@ -53,7 +53,9 @@ services::Status ModelBuilder::initialize(size_t nFeatures, size_t nIterations, services::Status s; _nClasses = (nClasses == 2) ? 1 : nClasses; _nIterations = nIterations; - _model.reset(new gbt::classification::internal::ModelImpl(nFeatures)); + auto modelImpl = new gbt::classification::internal::ModelImpl(nFeatures); + DAAL_CHECK_MALLOC(modelImpl) + _model.reset(modelImpl); gbt::classification::internal::ModelImpl& modelImplRef = daal::algorithms::dtrees::internal::getModelRef(_model); modelImplRef.resize(_nIterations*_nClasses); @@ -95,6 +97,7 @@ services::Status ModelBuilder::createTreeInternal(size_t nNodes, size_t clasLabe services::SharedPtr treeTablePtr(new DecisionTreeTable(nNodes));//DecisionTreeTable* const treeTable = new DecisionTreeTable(nNodes); const size_t nRows = treeTablePtr->getNumberOfRows(); DecisionTreeNode* const pNodes = (DecisionTreeNode*)treeTablePtr->getArray(); + DAAL_CHECK_MALLOC(pNodes) pNodes[0].featureIndex = __NODE_RESERVED_ID; pNodes[0].leftIndexOrClass = 0; pNodes[0].featureValueOrResponse = 0; diff --git a/algorithms/kernel/dtrees/gbt/gbt_model.cpp b/algorithms/kernel/dtrees/gbt/gbt_model.cpp old mode 100644 new mode 100755 index 467b4668f16..e423dca51a2 --- a/algorithms/kernel/dtrees/gbt/gbt_model.cpp +++ b/algorithms/kernel/dtrees/gbt/gbt_model.cpp @@ -171,10 +171,10 @@ void ModelImpl::traverseDFS(size_t iTree, tree_utils::regression::TreeNodeVisito traverseGbtDF(0, 0, gbtTree, onSplitNodeFunc, onLeafNodeFunc); } -void ModelImpl::treeToTable(TreeType& t, +services::Status ModelImpl::treeToTable(TreeType& t, gbt::internal::GbtDecisionTree** pTbl, HomogenNumericTable** pTblImp, HomogenNumericTable** pTblSmplCnt, size_t nFeat) { - t.convertGbtTreeToTable(pTbl, pTblImp, pTblSmplCnt, nFeat); + return (t.convertGbtTreeToTable(pTbl, pTblImp, pTblSmplCnt, nFeat)) ? services::Status() : services::ErrorMemoryAllocationFailed; } void ModelImpl::add(gbt::internal::GbtDecisionTree* pTbl, HomogenNumericTable* pTblImp, HomogenNumericTable* pTblSmplCnt) @@ -314,6 +314,7 @@ services::Status ModelImpl::convertDecisionTreesToGbtTrees(data_management::Data services::Status s; const size_t size = serializationData->size(); data_management::DataCollection* newTrees = new data_management::DataCollection(); + DAAL_CHECK_MALLOC(newTrees) for(size_t i = 0; i < size; ++i) { const DecisionTreeTable& tree = *(DecisionTreeTable*)(*(serializationData))[i].get(); diff --git a/algorithms/kernel/dtrees/gbt/gbt_model_impl.h b/algorithms/kernel/dtrees/gbt/gbt_model_impl.h old mode 100644 new mode 100755 index da6cfa2d830..0a2f88109f5 --- a/algorithms/kernel/dtrees/gbt/gbt_model_impl.h +++ b/algorithms/kernel/dtrees/gbt/gbt_model_impl.h @@ -232,21 +232,27 @@ class GbtTreeImpl : public dtrees::internal::TreeImpl typedef TAllocator Allocator; typedef TNodeType NodeType; - void convertGbtTreeToTable(GbtDecisionTree** pTbl, HomogenNumericTable** pTblImp, HomogenNumericTable** pTblSmplCnt, size_t nFeature) const + bool convertGbtTreeToTable(GbtDecisionTree** pTbl, HomogenNumericTable** pTblImp, HomogenNumericTable** pTblSmplCnt, size_t nFeature) const { size_t nLvls = 1; getMaxLvl(*super::top(), nLvls, static_cast(-1)); const size_t nNodes = getNumberOfNodesByLvls(nLvls); - *pTbl = new GbtDecisionTree(nNodes, nLvls, super::top()->numChildren() + 1); + *pTbl = new GbtDecisionTree(nNodes, nLvls, super::top()->numChildren() + 1); + *pTblImp = new HomogenNumericTable(1, nNodes, NumericTable::doAllocate); + *pTblSmplCnt = new HomogenNumericTable(1, nNodes, NumericTable::doAllocate); + + DAAL_CHECK_STATUS_VAR(*pTbl) + DAAL_CHECK_STATUS_VAR(*pTblImp) + DAAL_CHECK_STATUS_VAR(*pTblSmplCnt) - *pTblImp = new HomogenNumericTable(1, nNodes, NumericTable::doAllocate); - *pTblSmplCnt = new HomogenNumericTable(1, nNodes, NumericTable::doAllocate); if(super::top()) { GbtDecisionTree::internalTreeToGbtDecisionTree(*super::top(), nNodes, nLvls, *pTbl, (*pTblImp)->getArray(), (*pTblSmplCnt)->getArray(), nFeature); } + + return true; } @@ -300,7 +306,7 @@ class ModelImpl : protected dtrees::internal::ModelImpl void add(gbt::internal::GbtDecisionTree* pTbl, HomogenNumericTable* pTblImp, HomogenNumericTable* pTblSmplCnt); void traverseDFS(size_t iTree, tree_utils::regression::TreeNodeVisitor& visitor) const; void traverseBFS(size_t iTree, tree_utils::regression::TreeNodeVisitor& visitor) const; - static void treeToTable(TreeType& t, gbt::internal::GbtDecisionTree** pTbl, HomogenNumericTable** pTblImp, + static services::Status treeToTable(TreeType& t, gbt::internal::GbtDecisionTree** pTbl, HomogenNumericTable** pTblImp, HomogenNumericTable** pTblSmplCnt, size_t nFeature); diff --git a/algorithms/kernel/dtrees/gbt/gbt_train_tree_builder.i b/algorithms/kernel/dtrees/gbt/gbt_train_tree_builder.i old mode 100644 new mode 100755 index a63811c45f2..0a8ca5ee405 --- a/algorithms/kernel/dtrees/gbt/gbt_train_tree_builder.i +++ b/algorithms/kernel/dtrees/gbt/gbt_train_tree_builder.i @@ -368,7 +368,8 @@ services::Status TreeBuilder:: DAAL_CHECK_MALLOC(nd); _tree.reset(nd, false); - gbt::internal::ModelImpl::treeToTable(_tree, &pRes, &pTblImp, &pTblSmplCnt, _ctx.nFeatures()); + services::Status status = gbt::internal::ModelImpl::treeToTable(_tree, &pRes, &pTblImp, &pTblSmplCnt, _ctx.nFeatures()); + DAAL_CHECK_STATUS_VAR(status) if(_ctx.isBagging() && _tree.top()) _ctx.updateOOB(iTree, _tree); diff --git a/algorithms/kernel/dtrees/gbt/regression/gbt_regression_model_builder.cpp b/algorithms/kernel/dtrees/gbt/regression/gbt_regression_model_builder.cpp old mode 100644 new mode 100755 index 925fba376d4..0966a983522 --- a/algorithms/kernel/dtrees/gbt/regression/gbt_regression_model_builder.cpp +++ b/algorithms/kernel/dtrees/gbt/regression/gbt_regression_model_builder.cpp @@ -49,15 +49,16 @@ services::Status ModelBuilder::convertModelInternal() services::Status ModelBuilder::initialize(size_t nFeatures, size_t nIterations) { - services::Status s; - _model.reset(new gbt::regression::internal::ModelImpl(nFeatures)); + auto modelImpl = new gbt::regression::internal::ModelImpl(nFeatures); + DAAL_CHECK_MALLOC(modelImpl) + _model.reset(modelImpl); gbt::regression::internal::ModelImpl& modelImplRef = daal::algorithms::dtrees::internal::getModelRef(_model); modelImplRef.resize(nIterations); modelImplRef._impurityTables.reset(); modelImplRef._nNodeSampleTables.reset(); modelImplRef._nTree.set(nIterations); - return s; + return services::Status(); } services::Status ModelBuilder::createTreeInternal(size_t nNodes, TreeId& resId) diff --git a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch.h b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch.h old mode 100644 new mode 100755 index 682687cf5dd..315e8dba718 --- a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch.h +++ b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch.h @@ -69,7 +69,7 @@ class KNNClassificationPredictKernel : publi kdtree_knn_classification::internal::Stack, cpu> & stack, size_t k, algorithmFpType radius, const KDTreeTable & kdTreeTable, size_t rootTreeNodeIndex, const NumericTable & data); - void predict(algorithmFpType & predictedClass, const Heap, cpu> & heap, const NumericTable & labels, + services::Status predict(algorithmFpType & predictedClass, const Heap, cpu> & heap, const NumericTable & labels, size_t k); }; diff --git a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch_impl.i b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch_impl.i index 5a5d9568653..8e14eda90de 100755 --- a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch_impl.i +++ b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_predict_dense_default_batch_impl.i @@ -257,7 +257,8 @@ Status KNNClassificationPredictKernel:: const size_t yColumnCount = y->getNumberOfColumns(); const auto rowsPerBlock = (xRowCount + maxThreads - 1) / maxThreads; const auto blockCount = (xRowCount + rowsPerBlock - 1) / rowsPerBlock; - daal::threader_for(blockCount, blockCount, [=, &localTLS, &kdTreeTable, &data, &labels, &rowsPerBlock, &k](int iBlock) + SafeStatus safeStat; + daal::threader_for(blockCount, blockCount, [=, &localTLS, &kdTreeTable, &data, &labels, &rowsPerBlock, &k, &safeStat](int iBlock) { Local * const local = localTLS.local(); if (local) @@ -275,13 +276,16 @@ Status KNNClassificationPredictKernel:: for (size_t i = 0; i < last - first; ++i) { findNearestNeighbors(&dx[i * xColumnCount], local->heap, local->stack, k, radius, kdTreeTable, rootTreeNodeIndex, data); - predict(dy[i * yColumnCount], local->heap, labels, k); + services::Status s = predict(dy[i * yColumnCount], local->heap, labels, k); + DAAL_CHECK_STATUS_THR(s) } y->releaseBlockOfRows(yBD); const_cast(*x).releaseBlockOfRows(xBD); } } ); + DAAL_CHECK_SAFE_STATUS() + localTLS.reduce([=](Local * ptr) -> void { if (ptr) @@ -419,11 +423,12 @@ void KNNClassificationPredictKernel:: } template -void KNNClassificationPredictKernel:: +services::Status KNNClassificationPredictKernel:: predict(algorithmFpType & predictedClass, const Heap, cpu> & heap, const NumericTable & labels, size_t k) { const size_t heapSize = heap.size(); - if (heapSize < 1) { return; } + if (heapSize < 1) + return services::Status(); struct Voting { @@ -434,6 +439,7 @@ void KNNClassificationPredictKernel:: data_management::BlockDescriptor labelBD; algorithmFpType * classes = static_cast(daal_malloc(heapSize * sizeof(*classes))); + DAAL_CHECK_MALLOC(classes) for (size_t i = 0; i < heapSize; ++i) { const_cast(labels).getBlockOfColumnValues(0, heap[i].index, 1, readOnly, labelBD); @@ -464,6 +470,7 @@ void KNNClassificationPredictKernel:: predictedClass = winnerClass; daal_free(classes); classes = nullptr; + return services::Status(); } } // namespace internal diff --git a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_dense_default_impl.i b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_dense_default_impl.i index 874bb5bef18..91450ea4a37 100755 --- a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_dense_default_impl.i +++ b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_dense_default_impl.i @@ -156,6 +156,7 @@ Status KNNClassificationTrainBatchKernel(daal_malloc(xRowCount * sizeof(size_t))); + DAAL_CHECK_MALLOC(indexes) for (size_t i = 0; i < xRowCount; ++i) { indexes[i] = i; @@ -195,6 +196,7 @@ Status KNNClassificationTrainBatchKernel(daal_malloc(bboxSize * sizeof(BBox), sizeof(BBox))); + DAAL_CHECK_MALLOC(bboxQ) r.impl()->setLastNodeIndex(0); r.impl()->setRootNodeIndex(0); BBox * bboxCur = nullptr; @@ -218,6 +220,7 @@ Status KNNClassificationTrainBatchKernel(daal_malloc(subSampleCount * sizeof(algorithmFpType))); + DAAL_CHECK_MALLOC(subSamples) while (maxNodeCountForCurrentDepth < firstPartLeafNodeCount) { @@ -232,7 +235,9 @@ Status KNNClassificationTrainBatchKernelgetLastNodeIndex(); @@ -645,8 +650,9 @@ static ForwardIterator2 swapRanges(ForwardIterator1 first1, ForwardIterator1 las template size_t KNNClassificationTrainBatchKernel:: - adjustIndexesInParallel(size_t start, size_t end, size_t dimension, algorithmFpType median, const NumericTable & x, size_t * indexes) + adjustIndexesInParallel(size_t start, size_t end, size_t dimension, algorithmFpType median, const NumericTable & x, size_t * indexes, services::Status& status) { + status = services::Status(); const size_t xRowCount = x.getNumberOfRows(); data_management::BlockDescriptor columnBD; @@ -660,6 +666,12 @@ size_t KNNClassificationTrainBatchKernel(daal_malloc(idxMultiplier * (blockCount + 1) * sizeof(size_t))); size_t * rightSegmentStartPerBlock = static_cast(daal_malloc(idxMultiplier * blockCount * sizeof(size_t))); + if (!leftSegmentStartPerBlock || !rightSegmentStartPerBlock) + { + status = services::ErrorMemoryAllocationFailed; + return 0; + } + daal::threader_for(blockCount, blockCount, [=, &leftSegmentStartPerBlock, &rightSegmentStartPerBlock](int iBlock) { const size_t first = start + iBlock * rowsPerBlock; @@ -866,6 +878,7 @@ Status KNNClassificationTrainBatchKernel(daal_malloc(q.size() * sizeof(BuildNode))); + DAAL_CHECK_MALLOC(bnQ) size_t posQ = 0; while (q.size() > 0) { @@ -903,6 +916,7 @@ Status KNNClassificationTrainBatchKernel(daal_malloc((maxThreads + 1) * sizeof(*firstNodeIndex))); + DAAL_CHECK_MALLOC(firstNodeIndex) size_t nodeIndex = lastNodeIndex; for (size_t i = 0; i < maxThreads; ++i) { @@ -968,12 +982,14 @@ Status KNNClassificationTrainBatchKernelbuildStack.push(bn); + statStackPush = local->buildStack.push(bn); + DAAL_CHECK_STATUS_THR(statStackPush) this->copyBBox(&(local->bboxes[local->bboxPos * xColumnCount]), bboxCur, xColumnCount); ++local->bboxPos; if (local->bboxPos >= local->bboxesCapacity) @@ -1015,6 +1031,7 @@ Status KNNClassificationTrainBatchKernelfixupQueueCapacity * 2; size_t * const newQueue = static_cast(daal_malloc(newCapacity * sizeof(size_t))); + DAAL_CHECK_THR(newQueue, services::ErrorMemoryAllocationFailed); daal_memcpy_s(newQueue, newCapacity * sizeof(size_t), local->fixupQueue, local->fixupQueueIndex * sizeof(size_t)); size_t * oldQueue = local->fixupQueue; local->fixupQueue = newQueue; @@ -1028,10 +1045,12 @@ Status KNNClassificationTrainBatchKernelcomputeApproximatedMedianInSerial(bn.start, bn.end, d, bboxCur[d].upper, local->inSortValues, local->outSortValues, __KDTREE_INDEX_VALUE_PAIRS_PER_THREAD, x, - indexes, engineLocal.get()); + indexes, engineLocal.get(), statApproxMedian); + DAAL_CHECK_STATUS_THR(statApproxMedian) const auto idx = this->adjustIndexesInSerial(bn.start, bn.end, d, approximatedMedian, x, indexes); curNode->cutPoint = approximatedMedian; @@ -1082,7 +1101,8 @@ Status KNNClassificationTrainBatchKernelcopyBBox(bboxRight, bboxCur, xColumnCount); bboxRight[d].lower = approximatedMedian; bboxRight[d].upper = upperD; - local->buildStack.push(bnRight); + statStackPush = local->buildStack.push(bnRight); + DAAL_CHECK_STATUS_THR(statStackPush) bnLeft.start = bn.start; bnLeft.end = idx; bnLeft.nodePos = curNode->leftIndex; @@ -1105,7 +1125,8 @@ Status KNNClassificationTrainBatchKernelcopyBBox(bboxLeft, bboxCur, xColumnCount); bboxLeft[d].lower = lowerD; bboxLeft[d].upper = upperD; - local->buildStack.push(bnLeft); + statStackPush = local->buildStack.push(bnLeft); + DAAL_CHECK_STATUS_THR(statStackPush) } // if (bn.end - bn.start <= __KDTREE_LEAF_BUCKET_SIZE) } // while (local->buildStack.size() > 0) } // for (auto i = first; i < last; ++i) @@ -1220,8 +1241,9 @@ template algorithmFpType KNNClassificationTrainBatchKernel:: computeApproximatedMedianInSerial(size_t start, size_t end, size_t dimension, algorithmFpType upper, IndexValuePair * inSortValues, IndexValuePair * outSortValues, - size_t sortValueCount, const NumericTable & x, size_t * indexes, engines::BatchBase *engine) + size_t sortValueCount, const NumericTable & x, size_t * indexes, engines::BatchBase *engine, services::Status& status) { + status = services::Status(); size_t i, j; const auto xRowCount = x.getNumberOfRows(); data_management::BlockDescriptor columnBD; @@ -1269,6 +1291,11 @@ algorithmFpType KNNClassificationTrainBatchKernel(daal_malloc(sampleCount * sizeof(*samples))); + if (!samples) + { + status = services::ErrorMemoryAllocationFailed; + return 0; + } auto engineImpl = dynamic_cast(engine); daal::internal::RNGs rng; @@ -1283,6 +1310,12 @@ algorithmFpType KNNClassificationTrainBatchKernel(sampleCount, samples); size_t * hist = static_cast(daal_malloc(sampleCount * sizeof(*hist))); + if (!hist) + { + status = services::ErrorMemoryAllocationFailed; + return 0; + } + for (i = 0; i (daal_malloc(subSampleCount * sizeof(*subSamples))); + if (!subSamples) + { + status = services::ErrorMemoryAllocationFailed; + return 0; + } + size_t subSamplesPos = 0; for (size_t l = 0; l < sampleCount; l += __KDTREE_SEARCH_SKIP) { diff --git a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_kernel.h b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_kernel.h old mode 100644 new mode 100755 index 48d5b8929dd..c2e250fbea9 --- a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_kernel.h +++ b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_classification_train_kernel.h @@ -82,7 +82,7 @@ class KNNClassificationTrainBatchKernel * dest, const BoundingBox * src, size_t n); @@ -96,7 +96,8 @@ class KNNClassificationTrainBatchKernel * outSortValues, size_t sortValueCount, const NumericTable & x, - size_t * indexes, engines::BatchBase *engine); + size_t * indexes, engines::BatchBase *engine, + services::Status& status); size_t adjustIndexesInSerial(size_t start, size_t end, size_t dimension, algorithmFpType median, const NumericTable & x, size_t * indexes); diff --git a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i index 51c6261b595..a66e4a85541 100755 --- a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i +++ b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i @@ -105,16 +105,18 @@ public: _count = 0; } - DAAL_FORCEINLINE void push(const T & value) + DAAL_FORCEINLINE services::Status push(const T & value) { if (_count >= _size) { - grow(); + DAAL_CHECK(grow(), services::ErrorMemoryAllocationFailed); } _top = (_top + 1) & _sizeMinus1; _data[_top] = value; ++_count; + + return services::Status(); } DAAL_FORCEINLINE T pop() @@ -129,7 +131,7 @@ public: size_t size() const { return _count; } - void grow() + bool grow() { _size *= 2; T * const newData = static_cast(services::daal_malloc(_size * sizeof(T))); @@ -142,7 +144,8 @@ public: T * oldData = _data; _data = newData; services::daal_free(oldData); - oldData = nullptr; + oldData = nullptr; + return true; } private: diff --git a/algorithms/kernel/kmeans/inner/kmeans_init_container_v1.h b/algorithms/kernel/kmeans/inner/kmeans_init_container_v1.h index 36c1a8db71d..345c7fd2227 100755 --- a/algorithms/kernel/kmeans/inner/kmeans_init_container_v1.h +++ b/algorithms/kernel/kmeans/inner/kmeans_init_container_v1.h @@ -157,6 +157,7 @@ services::Status DistributedContainer size_t na = nPartials * 2; NumericTable **a = new NumericTable*[na]; + DAAL_CHECK_MALLOC(a) for(size_t i = 0; i < nPartials; i++) { PartialResult *inPres = static_cast((*dcInput)[i].get()); diff --git a/algorithms/kernel/kmeans/kmeans_init_container.h b/algorithms/kernel/kmeans/kmeans_init_container.h index 1d18165ae58..93b58cfeb75 100755 --- a/algorithms/kernel/kmeans/kmeans_init_container.h +++ b/algorithms/kernel/kmeans/kmeans_init_container.h @@ -137,6 +137,7 @@ services::Status DistributedContainer size_t na = nPartials * 2; NumericTable **a = new NumericTable*[na]; + DAAL_CHECK_MALLOC(a) for(size_t i = 0; i < nPartials; i++) { PartialResult *inPres = static_cast((*dcInput)[i].get()); diff --git a/algorithms/kernel/lasso_regression/lasso_regression_training_result_fpt.cpp b/algorithms/kernel/lasso_regression/lasso_regression_training_result_fpt.cpp old mode 100644 new mode 100755 index b3b5bf53bfe..61a2a57e63a --- a/algorithms/kernel/lasso_regression/lasso_regression_training_result_fpt.cpp +++ b/algorithms/kernel/lasso_regression/lasso_regression_training_result_fpt.cpp @@ -50,6 +50,7 @@ DAAL_EXPORT Status Result::allocate(const daal::algorithms::Input * input, const lasso_regression::internal::ModelImpl* mImpl = new lasso_regression::internal::ModelImpl(in->getNumberOfFeatures(), in->getNumberOfDependentVariables(), *parameter, dummy, s); + DAAL_CHECK_MALLOC(mImpl) set(model, lasso_regression::ModelPtr(mImpl)); if(parameter->optResultToCompute & computeGramMatrix) diff --git a/algorithms/kernel/low_order_moments/low_order_moments_impl.i b/algorithms/kernel/low_order_moments/low_order_moments_impl.i index 0b18b9d8aac..d3a102304e8 100755 --- a/algorithms/kernel/low_order_moments/low_order_moments_impl.i +++ b/algorithms/kernel/low_order_moments/low_order_moments_impl.i @@ -271,12 +271,12 @@ Status LowOrderMomentsOnlineTask::init(PartialResult *part stDev = (algorithmFPType *)daal_malloc(rowSize); variation = (algorithmFPType *)daal_malloc(rowSize); - DAAL_CHECK(mean && raw2Mom && variance && stDev &&variation, services::ErrorMemoryAllocationFailed); + DAAL_CHECK_MALLOC(mean && raw2Mom && variance && stDev && variation) if (isOnline) { prevSums = (algorithmFPType *)daal_malloc(rowSize); - DAAL_CHECK(prevSums, services::ErrorMemoryAllocationFailed); + DAAL_CHECK_MALLOC(prevSums) daal_memcpy_s(prevSums, rowSize, resultArray[(int)partialSum], rowSize); } return Status(); diff --git a/algorithms/kernel/multiclassclassifier/multiclassclassifier_model.cpp b/algorithms/kernel/multiclassclassifier/multiclassclassifier_model.cpp index 87e052f5d73..a1aa246b9df 100755 --- a/algorithms/kernel/multiclassclassifier/multiclassclassifier_model.cpp +++ b/algorithms/kernel/multiclassclassifier/multiclassclassifier_model.cpp @@ -91,6 +91,8 @@ classifier::ModelPtr *Model::getTwoClassClassifierModels() if(!_modelsArray) { _modelsArray = new classifier::ModelPtr[_models->size()]; + if (!_modelsArray) + return nullptr; for(size_t i = 0; i < _models->size(); i++) { _modelsArray[i] = services::staticPointerCast((*_models)[i]); diff --git a/algorithms/kernel/naivebayes/naivebayes_train_container.h b/algorithms/kernel/naivebayes/naivebayes_train_container.h index 0d7f0ef55cf..4874a26ce50 100755 --- a/algorithms/kernel/naivebayes/naivebayes_train_container.h +++ b/algorithms/kernel/naivebayes/naivebayes_train_container.h @@ -269,6 +269,7 @@ services::Status DistributedContainer size_t na = models->size(); PartialModel **a = new PartialModel*[na]; + DAAL_CHECK_MALLOC(a) for(size_t i = 0; i < na; i++) { diff --git a/algorithms/kernel/optimization_solver/iterative_solver_kernel.h b/algorithms/kernel/optimization_solver/iterative_solver_kernel.h index 640a5227ab5..adffaed9040 100755 --- a/algorithms/kernel/optimization_solver/iterative_solver_kernel.h +++ b/algorithms/kernel/optimization_solver/iterative_solver_kernel.h @@ -83,6 +83,7 @@ class IterativeSolverKernel : public Kernel daal::tls normTls( [ = ]()-> algorithmFPType* { algorithmFPType *normPtr = (algorithmFPType *)daal_malloc(sizeof(algorithmFPType)); + DAAL_CHECK_STATUS_VAR(normPtr) *normPtr = 0; return normPtr; } ); @@ -131,6 +132,7 @@ class IterativeSolverKernel : public Kernel daal::tls normTls( [ = ]()-> algorithmFPType* { algorithmFPType *normPtr = (algorithmFPType *)daal_malloc(sizeof(algorithmFPType)); + DAAL_CHECK_STATUS_VAR(normPtr) *normPtr = 0; return normPtr; } ); diff --git a/algorithms/kernel/qr/qr_dense_default_distr_step2.h b/algorithms/kernel/qr/qr_dense_default_distr_step2.h old mode 100644 new mode 100755 index 8554871e017..d2744aabe0f --- a/algorithms/kernel/qr/qr_dense_default_distr_step2.h +++ b/algorithms/kernel/qr/qr_dense_default_distr_step2.h @@ -94,7 +94,9 @@ DAAL_EXPORT Status DistributedPartialResult::setPartialResultStorage(data_manage size_t nodeSize = nodeCollection->size(); nBlocks += nodeSize; - data_management::DataCollectionPtr nodePartialResult(new data_management::DataCollection()); + auto pDataCollection = new data_management::DataCollection(); + DAAL_CHECK_MALLOC(pDataCollection) + data_management::DataCollectionPtr nodePartialResult(pDataCollection); for(size_t j = 0 ; j < nodeSize ; j++) { diff --git a/algorithms/kernel/qr/qr_dense_default_online.h b/algorithms/kernel/qr/qr_dense_default_online.h old mode 100644 new mode 100755 index 1c485cb436b..7de36f1589f --- a/algorithms/kernel/qr/qr_dense_default_online.h +++ b/algorithms/kernel/qr/qr_dense_default_online.h @@ -47,8 +47,11 @@ namespace interface1 template DAAL_EXPORT Status OnlinePartialResult::allocate(const daal::algorithms::Input *input, const daal::algorithms::Parameter *parameter, const int method) { - set(outputOfStep1ForStep3, DataCollectionPtr(new DataCollection())); - set(outputOfStep1ForStep2, DataCollectionPtr(new DataCollection())); + auto pDataCollection13 = new DataCollection(); + auto pDataCollection12 = new DataCollection(); + DAAL_CHECK_MALLOC(pDataCollection13 && pDataCollection12) + set(outputOfStep1ForStep3, DataCollectionPtr(pDataCollection13)); + set(outputOfStep1ForStep2, DataCollectionPtr(pDataCollection12)); return Status(); } diff --git a/algorithms/kernel/stump/stump_train_impl.i b/algorithms/kernel/stump/stump_train_impl.i old mode 100644 new mode 100755 index 9e571aae738..6ca60167583 --- a/algorithms/kernel/stump/stump_train_impl.i +++ b/algorithms/kernel/stump/stump_train_impl.i @@ -473,6 +473,7 @@ services::Status StumpTrainKernel::doStumpRegressi daal::tls tls( [ = ]()-> TGroupRes * { TGroupRes *g = new TGroupRes(); + DAAL_CHECK_STATUS_VAR(g) g->groupMinS = daal::services::internal::MaxVal::get(); return g; } ); diff --git a/algorithms/kernel/svd/svd_dense_default_online.h b/algorithms/kernel/svd/svd_dense_default_online.h old mode 100644 new mode 100755 index db530d1abae..86ed167f97e --- a/algorithms/kernel/svd/svd_dense_default_online.h +++ b/algorithms/kernel/svd/svd_dense_default_online.h @@ -49,8 +49,11 @@ using namespace daal::services; template DAAL_EXPORT Status OnlinePartialResult::allocate(const daal::algorithms::Input *input, const daal::algorithms::Parameter *parameter, const int method) { - set(outputOfStep1ForStep3, DataCollectionPtr(new DataCollection())); - set(outputOfStep1ForStep2, DataCollectionPtr(new DataCollection())); + auto pDataCollection13 = new DataCollection(); + auto pDataCollection12 = new DataCollection(); + DAAL_CHECK_MALLOC(pDataCollection13 && pDataCollection12) + set(outputOfStep1ForStep3, DataCollectionPtr(pDataCollection13)); + set(outputOfStep1ForStep2, DataCollectionPtr(pDataCollection12)); return Status(); } diff --git a/algorithms/kernel/svm/svm_predict_impl.i b/algorithms/kernel/svm/svm_predict_impl.i old mode 100644 new mode 100755 index 2efc27e11d6..6627f9093bf --- a/algorithms/kernel/svm/svm_predict_impl.i +++ b/algorithms/kernel/svm/svm_predict_impl.i @@ -94,7 +94,9 @@ struct SVMPredictImpl : public Kernel NumericTablePtr shResNT = HomogenNumericTableCPU::create(buf, nSV, nVectors, &s); DAAL_CHECK_STATUS_VAR(s); - kernel_function::ResultPtr shRes(new kernel_function::Result()); + auto kfResultPtr = new kernel_function::Result(); + DAAL_CHECK_MALLOC(kfResultPtr) + kernel_function::ResultPtr shRes(kfResultPtr); shRes->set(kernel_function::values, shResNT); kernel->setResult(shRes); kernel->getInput()->set(kernel_function::X, xTable); diff --git a/algorithms/kernel/svm/svm_train_boser_cache.i b/algorithms/kernel/svm/svm_train_boser_cache.i old mode 100644 new mode 100755 index df9f15aa5be..20f665a6787 --- a/algorithms/kernel/svm/svm_train_boser_cache.i +++ b/algorithms/kernel/svm/svm_train_boser_cache.i @@ -231,7 +231,9 @@ protected: _kernel->getInput()->set(kernel_function::X, xTable); _kernel->getInput()->set(kernel_function::Y, xTable); - kernel_function::ResultPtr shRes(new kernel_function::Result()); + auto kfResultPtr = new kernel_function::Result(); + DAAL_CHECK_MALLOC(kfResultPtr) + kernel_function::ResultPtr shRes(kfResultPtr); shRes->set(kernel_function::values, _cacheTable); _kernel->setResult(shRes); return _kernel->computeNoThrow(); @@ -338,7 +340,9 @@ protected: _kernel->getInput()->set(kernel_function::X, xTable); _kernel->getInput()->set(kernel_function::Y, xTable); - kernel_function::ResultPtr shRes(new kernel_function::Result()); + auto kfResultPtr = new kernel_function::Result(); + DAAL_CHECK_MALLOC(kfResultPtr) + kernel_function::ResultPtr shRes(kfResultPtr); shRes->set(kernel_function::values, _cacheTable); _kernel->setResult(shRes); return s; From e25bd3c36901fc494c5219a75031350d03397d7f Mon Sep 17 00:00:00 2001 From: dmitrypa Date: Tue, 24 Sep 2019 10:31:51 +0300 Subject: [PATCH 2/2] SDL: error memory handling. Fix clear code. --- algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i index a66e4a85541..eae91502737 100755 --- a/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i +++ b/algorithms/kernel/k_nearest_neighbors/kdtree_knn_impl.i @@ -144,7 +144,7 @@ public: T * oldData = _data; _data = newData; services::daal_free(oldData); - oldData = nullptr; + oldData = nullptr; return true; }