From 1cd240e402a0920d1ea4c9ca0a1a11b8fc7ce76c Mon Sep 17 00:00:00 2001 From: Monika vos Mueller Date: Wed, 28 Mar 2018 16:10:13 -0700 Subject: [PATCH 1/8] cpp DAAL implementation - wrapper for raw pointer input --- src/Makefile | 8 ++++ src/cpu/daal/include/debug.hpp | 39 +++++++++++++++ src/cpu/daal/include/iinput.hpp | 85 +++++++++++++++++++++++++++++++++ src/daal.mk | 12 +++++ 4 files changed, 144 insertions(+) create mode 100644 src/cpu/daal/include/debug.hpp create mode 100644 src/cpu/daal/include/iinput.hpp create mode 100644 src/daal.mk diff --git a/src/Makefile b/src/Makefile index 8fbccd9a4..2cecb8a98 100644 --- a/src/Makefile +++ b/src/Makefile @@ -8,11 +8,18 @@ SHELL := /bin/bash # force avoidance of dash as shell include config2.mk NVCC := $(shell command -v nvcc 2> /dev/null) +DAAL := $(shell ldconfig -p | grep daal_core) ifndef NVCC $(info ** No CUDA found, only build with CPU.) endif +ifndef DAAL +$(info ** NO DAAL found. running without DAAL library.) +else +-include daal.mk +endif + # Bulid directory OBJDIR=build @@ -136,6 +143,7 @@ CPU_HDR=\ cpu/include/cgls.h \ cpu/include/equil_helper.h \ cpu/include/projector_helper.h + CPU_MTX_OBJ=\ $(OBJDIR)/cpu/matrix/matrix_sparse.o \ $(OBJDIR)/cpu/matrix/matrix_dense.o diff --git a/src/cpu/daal/include/debug.hpp b/src/cpu/daal/include/debug.hpp new file mode 100644 index 000000000..a3672469a --- /dev/null +++ b/src/cpu/daal/include/debug.hpp @@ -0,0 +1,39 @@ +/*! + * Copyright 2017 H2O.ai, Inc. + * License Apache License Version 2.0 (see LICENSE for details) + */ +#ifndef SRC_CPU_DAAL_INCLUDE_DEBUG_HPP_ +#define SRC_CPU_DAAL_INCLUDE_DEBUG_HPP_ + +#include +#include +#include +#include +#include + +#ifdef __GNUC__ /* if clang and GCC, use demangle for debug information */ +# include +#endif /* __GNUC__ */ + +template std::string type_name() { + typedef typename std::remove_reference::type TR; + std::unique_ptr self + ( +#ifdef __GNUC__ + abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr), +#else + nullptr, +#endif + std::free + ); + + std::string rname = self != nullptr ? self.get() : typeid(TR).name(); + if (std::is_const::value) rname += " const"; + if (std::is_volatile::value) rname += " volatile"; + if (std::is_lvalue_reference::value) rname += "&"; + if (std::is_rvalue_reference::value) rname += "&&"; + return rname; +} + + +#endif /* SRC_CPU_DAAL_INCLUDE_DEBUG_HPP_ */ diff --git a/src/cpu/daal/include/iinput.hpp b/src/cpu/daal/include/iinput.hpp new file mode 100644 index 000000000..6ecd7fa52 --- /dev/null +++ b/src/cpu/daal/include/iinput.hpp @@ -0,0 +1,85 @@ +/*! + * Copyright 2017 H2O.ai, Inc. + * License Apache License Version 2.0 (see LICENSE for details) + * purpose: Interface for entering input to DAAL library + * from raw pointers and csv file to NumericTable (needed for DAAL) calculation + * copy ctor and eq. operator based on default behavior (shared_ptr points to the same object) + */ + +#ifndef SRC_CPU_DAAL_INCLUDE_IINPUT_HPP_ +#define SRC_CPU_DAAL_INCLUDE_IINPUT_HPP_ + +#include +#include // for primitive data types +#include +#include +#include + +namespace H2O4GPU { +namespace DAAL { + +using namespace daal; +using namespace daal::services; +using namespace daal::data_management; + +/* IInput interface */ +template +class IInput { +protected: + SharedPtr _inputData; +public: + virtual SharedPtr getNumericTable() { + return this->_inputData; + } + virtual ~IInput() {} +}; + +/* HomogenousDaalData implementation for primitive data type: int, float, double */ +template +class HomogenousDaalData : public IInput +{ +public: + HomogenousDaalData(Input* const inputArray, size_t m_dim, size_t n_dim) { + if (!std::is_arithmetic::value) + throw "Input data is not artithmetic type!"; + this->_inputData = SharedPtr(new Matrix(m_dim, n_dim, inputArray)); + } + virtual ~HomogenousDaalData() {} +}; + +/* HomogenousDaalData implementation for csv file input */ +template <> +class HomogenousDaalData : public IInput +{ +public: + HomogenousDaalData(const std::string &filename) { + if (!this->fileExists(filename)) + throw "Input file doesn't exist!"; + if ("csv" != this->getFileExt(filename)) + throw "Input file isn't csv file format!"; + FileDataSource dataSource(fileName, + DataSource::doAllocateNumericTable, + DataSource::doDictionaryFromContext); + // retrieve data from the input file + dataSource.loadDataBlock(); + this->_inputData = dataSource.getNumericTable(); + } + + inline bool fileExists(const std::string &filename) { + struct stat buffer; + return (stat (filename.c_str(), &buffer) == 0); + } + + inline std::string getFileExt(const std::string &filename) { + size_t i = filename.rfind('.', filename.length()); + if (i != std::string::npos) { + return(filename.substr(i+1, filename.length() - i)); + } + return(""); + } +} + +} /* end of DAAL namespace */ +} /* end of H2O4GPU namespace */ + +#endif /* SRC_CPU_DAAL_INCLUDE_IINPUT_HPP_ */ diff --git a/src/daal.mk b/src/daal.mk new file mode 100644 index 000000000..018bd0de8 --- /dev/null +++ b/src/daal.mk @@ -0,0 +1,12 @@ +location = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) +WHERE_ART_THOU := $(location) +$(info ** -> $(WHERE_ART_THOU)) +$(info ** ------------------------------------------------------------------ **) + +DAAL_LIBS := -ldaal_core -ldaal_sequential -lpthread -lm + +DAAL_INCLUDE := -I$HOME/daal/include + +DAAL_HDR = \ + cpu/daal/include/debug.hpp \ + cpu/daal/include/iinput.hpp From 77ee0294e2a4a781d3b1f74da47c9a8973561f0a Mon Sep 17 00:00:00 2001 From: Monika vos Mueller Date: Thu, 29 Mar 2018 16:41:51 -0700 Subject: [PATCH 2/8] Interface for input/output from daal --- src/cpu/daal/{include => }/iinput.hpp | 22 +- src/cpu/daal/svd.hpp | 62 ++ src/cpu/daal/{include => utils}/debug.hpp | 0 src/cpu/daal/utils/error_handling.h | 89 ++ src/cpu/daal/utils/service.h | 992 ++++++++++++++++++++++ src/daal.mk | 2 + 6 files changed, 1166 insertions(+), 1 deletion(-) rename src/cpu/daal/{include => }/iinput.hpp (76%) create mode 100644 src/cpu/daal/svd.hpp rename src/cpu/daal/{include => utils}/debug.hpp (100%) create mode 100644 src/cpu/daal/utils/error_handling.h create mode 100644 src/cpu/daal/utils/service.h diff --git a/src/cpu/daal/include/iinput.hpp b/src/cpu/daal/iinput.hpp similarity index 76% rename from src/cpu/daal/include/iinput.hpp rename to src/cpu/daal/iinput.hpp index 6ecd7fa52..635ae4d72 100644 --- a/src/cpu/daal/include/iinput.hpp +++ b/src/cpu/daal/iinput.hpp @@ -28,7 +28,7 @@ class IInput { protected: SharedPtr _inputData; public: - virtual SharedPtr getNumericTable() { + virtual const SharedPtr& getNumericTable() const { return this->_inputData; } virtual ~IInput() {} @@ -79,6 +79,26 @@ class HomogenousDaalData : public IInput } } +/* convert numericTable to raw pointer -> memory released in daal */ +template +const Output* getRawOutput(const NumericTablePtr& nt) { + BlockDescriptor block; + (*nt.get()).getBlockOfRows(0, rows, readOnly, block); + const Output *array = block.getBlockPtr(); + (*nt.get()).releaseBlockOfRows(block); + return array; +} + +template +const Output* getRawOutput(const NumericTablePtr& nt) { + BlockDescriptor block; + auto rows = (*nt.get()).getNumberOfRows(); + (*nt.get()).getBlockOfRows(0, rows, readOnly, block); + const double *array = block.getBlockPtr(); + (*nt.get()).releaseBlockOfRows(block); + return array; +} + } /* end of DAAL namespace */ } /* end of H2O4GPU namespace */ diff --git a/src/cpu/daal/svd.hpp b/src/cpu/daal/svd.hpp new file mode 100644 index 000000000..d23bac8e1 --- /dev/null +++ b/src/cpu/daal/svd.hpp @@ -0,0 +1,62 @@ +/*! + * Copyright 2017 H2O.ai, Inc. + * License Apache License Version 2.0 (see LICENSE for details) + */ + +#ifndef SRC_CPU_DAAL_INCLUDE_SVD_H_ +#define SRC_CPU_DAAL_INCLUDE_SVD_H_ + +#include +#include "iinput.hpp" + +namespace H2O4GPU { +namespace DAAL { + +using namespace daal; +using namespace daal::algorithms; + +class SVD { +public: + typedef svd::ResultPtr resultPtr; + + SharedPtr _origData; + resultPtr _result; + NumericTablePtr _singularValues; + NumericTablePtr _rightSingularMatrix; + NumericTablePtr _leftSingularMatrix; + + template + SVD(const IInput& input) { + this->_origData; + } + + void fit() { + svd::Batch<> algorithm; + algorithm.input.set(svd::data, this->_origData); + + // Compute SVD + algorithm.compute(); + this->_result = algorithm.getResult(); + this->_result->get(svd::singularValues); + } + + const NumericTablePtr getSingularValues() const + { + return this->_result->get(svd::singularValues); + } + + const NumericTablePtr getRightSingularMatrix() const + { + return this->_result->get(svd::rightSingularMatrix); + } + + const NumericTablePtr getLeftSingularMatrix() const + { + return this->_result->get(svd::leftSingularMatrix); + } +}; + +} /* end of namespace DAAL +} /* end of namespace H2O4GPU + +#endif /* SRC_CPU_DAAL_INCLUDE_SVD_H_ */ diff --git a/src/cpu/daal/include/debug.hpp b/src/cpu/daal/utils/debug.hpp similarity index 100% rename from src/cpu/daal/include/debug.hpp rename to src/cpu/daal/utils/debug.hpp diff --git a/src/cpu/daal/utils/error_handling.h b/src/cpu/daal/utils/error_handling.h new file mode 100644 index 000000000..9ce9cd600 --- /dev/null +++ b/src/cpu/daal/utils/error_handling.h @@ -0,0 +1,89 @@ +/* file: error_handling.h */ +/******************************************************************************* +* Copyright 2014-2017 Intel Corporation +* All Rights Reserved. +* +* If this software was obtained under the Intel Simplified Software License, +* the following terms apply: +* +* The source code, information and material ("Material") contained herein is +* owned by Intel Corporation or its suppliers or licensors, and title to such +* Material remains with Intel Corporation or its suppliers or licensors. The +* Material contains proprietary information of Intel or its suppliers and +* licensors. The Material is protected by worldwide copyright laws and treaty +* provisions. No part of the Material may be used, copied, reproduced, +* modified, published, uploaded, posted, transmitted, distributed or disclosed +* in any way without Intel's prior express written permission. No license under +* any patent, copyright or other intellectual property rights in the Material +* is granted to or conferred upon you, either expressly, by implication, +* inducement, estoppel or otherwise. Any license under such intellectual +* property rights must be express and approved by Intel in writing. +* +* Unless otherwise agreed by Intel in writing, you may not remove or alter this +* notice or any other notice embedded in Materials by Intel or Intel's +* suppliers or licensors in any way. +* +* +* If this software was obtained under the Apache License, Version 2.0 (the +* "License"), the following terms apply: +* +* You may not use this file except in compliance with the License. You may +* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +* +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +/* +! Content: +! Auxiliary error-handling functions used in C++ examples +!******************************************************************************/ + + +#ifndef _ERROR_HANDLING_H +#define _ERROR_HANDLING_H + +const int fileError = -1001; + +void checkAllocation(void *ptr) +{ + if (!ptr) + { + std::cout << "Error: Memory allocation failed" << std::endl; + exit(-1); + } +} + +void checkPtr(void *ptr) +{ + if (!ptr) + { + std::cout << "Error: NULL pointer" << std::endl; + exit(-2); + } +} + +void fileOpenError(const char *filename) +{ + std::cout << "Unable to open file '" << filename << "'" << std::endl; + exit(fileError); +} + +void fileReadError() +{ + std::cout << "Unable to read next line" << std::endl; + exit(fileError); +} + +void sparceFileReadError() +{ + std::cout << "Incorrect format of file" << std::endl; + exit(fileError); +} + +#endif diff --git a/src/cpu/daal/utils/service.h b/src/cpu/daal/utils/service.h new file mode 100644 index 000000000..cae588226 --- /dev/null +++ b/src/cpu/daal/utils/service.h @@ -0,0 +1,992 @@ +/* file: service.h */ +/******************************************************************************* +* Copyright 2014-2017 Intel Corporation +* All Rights Reserved. +* +* If this software was obtained under the Intel Simplified Software License, +* the following terms apply: +* +* The source code, information and material ("Material") contained herein is +* owned by Intel Corporation or its suppliers or licensors, and title to such +* Material remains with Intel Corporation or its suppliers or licensors. The +* Material contains proprietary information of Intel or its suppliers and +* licensors. The Material is protected by worldwide copyright laws and treaty +* provisions. No part of the Material may be used, copied, reproduced, +* modified, published, uploaded, posted, transmitted, distributed or disclosed +* in any way without Intel's prior express written permission. No license under +* any patent, copyright or other intellectual property rights in the Material +* is granted to or conferred upon you, either expressly, by implication, +* inducement, estoppel or otherwise. Any license under such intellectual +* property rights must be express and approved by Intel in writing. +* +* Unless otherwise agreed by Intel in writing, you may not remove or alter this +* notice or any other notice embedded in Materials by Intel or Intel's +* suppliers or licensors in any way. +* +* +* If this software was obtained under the Apache License, Version 2.0 (the +* "License"), the following terms apply: +* +* You may not use this file except in compliance with the License. You may +* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +* +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +/* +! Content: +! Auxiliary functions used in C++ examples +!******************************************************************************/ + +#ifndef _SERVICE_H +#define _SERVICE_H + +#include "daal.h" + +using namespace daal::data_management; + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "error_handling.h" + +size_t readTextFile(const std::string &datasetFileName, daal::byte **data) +{ + std::ifstream file(datasetFileName .c_str(), std::ios::binary | std::ios::ate); + if (!file.is_open()) + { + fileOpenError(datasetFileName .c_str()); + } + + std::streampos end = file.tellg(); + file.seekg(0, std::ios::beg); + + size_t fileSize = static_cast(end); + + (*data) = new daal::byte[fileSize]; + checkAllocation(data); + + if (!file.read((char *)(*data), fileSize)) + { + delete[] data; + fileReadError(); + } + + return fileSize; +} + +template +void readRowUnknownLength(char* line, std::vector& data) +{ + size_t n = 0; + const char* prevDelim = line - 1; + char* ptr = line; + for(; *ptr; ++ptr) + { + if(*ptr == ',' || *ptr == '\r') + { + if(prevDelim != ptr - 1) + ++n; + *ptr = ' '; + prevDelim = ptr; + } + } + if(prevDelim != ptr - 1) + ++n; + data.resize(n); + std::stringstream iss(line); + for(size_t i = 0; i < n; ++i) + { + iss >> data[i]; + } +} + +template +CSRNumericTable *createSparseTable(const std::string& datasetFileName) +{ + std::ifstream file(datasetFileName .c_str()); + + if (!file.is_open()) + { + fileOpenError(datasetFileName .c_str()); + } + + std::string str; + + //read row offsets + std::getline(file, str); + std::vector rowOffsets; + readRowUnknownLength(&str[0], rowOffsets); + if(!rowOffsets.size()) + return NULL; + const size_t nVectors = rowOffsets.size() - 1; + + //read cols indices + std::getline(file, str); + std::vector colIndices; + readRowUnknownLength(&str[0], colIndices); + + //read values + std::getline(file, str); + std::vector data; + readRowUnknownLength(&str[0], data); + const size_t nNonZeros = data.size(); + + size_t maxCol = 0; + for(size_t i = 0; i < colIndices.size(); ++i) + { + if(colIndices[i] > maxCol) + maxCol = colIndices[i]; + } + const size_t nFeatures = maxCol; + + if(!nFeatures || !nVectors || colIndices.size() != nNonZeros || + nNonZeros != (rowOffsets[nVectors] - 1)) + { + sparceFileReadError(); + } + + size_t *resultRowOffsets = NULL; + size_t *resultColIndices = NULL; + item_type *resultData = NULL; + CSRNumericTable *numericTable = new CSRNumericTable(resultData, resultColIndices, resultRowOffsets, nFeatures, nVectors); + numericTable->allocateDataMemory(nNonZeros); + numericTable->getArrays(&resultData, &resultColIndices, &resultRowOffsets); + for (size_t i = 0; i < nNonZeros; ++i) + { + resultData[i] = data[i]; + resultColIndices[i] = colIndices[i]; + } + for (size_t i = 0; i < nVectors + 1; ++i) + { + resultRowOffsets[i] = rowOffsets[i]; + } + return numericTable; +} + +void printAprioriItemsets(NumericTablePtr largeItemsetsTable, + NumericTablePtr largeItemsetsSupportTable, + size_t nItemsetToPrint = 20) +{ + size_t largeItemsetCount = largeItemsetsSupportTable->getNumberOfRows(); + size_t nItemsInLargeItemsets = largeItemsetsTable->getNumberOfRows(); + + BlockDescriptor block1; + largeItemsetsTable->getBlockOfRows(0, nItemsInLargeItemsets, readOnly, block1); + int *largeItemsets = block1.getBlockPtr(); + + BlockDescriptor block2; + largeItemsetsSupportTable->getBlockOfRows(0, largeItemsetCount, readOnly, block2); + int *largeItemsetsSupportData = block2.getBlockPtr(); + + std::vector > largeItemsetsVector; + largeItemsetsVector.resize(largeItemsetCount); + + for (size_t i = 0; i < nItemsInLargeItemsets; i++) + { + largeItemsetsVector[largeItemsets[2 * i]].push_back(largeItemsets[2 * i + 1]); + } + + std::vector supportVector; + supportVector.resize(largeItemsetCount); + + for (size_t i = 0; i < largeItemsetCount; i++) + { + supportVector[largeItemsetsSupportData[2 * i]] = largeItemsetsSupportData[2 * i + 1]; + } + + std::cout << std::endl << "Apriori example program results" << std::endl; + + std::cout << std::endl << "Last " << nItemsetToPrint << " large itemsets: " << std::endl; + std::cout << std::endl << "Itemset" << "\t\t\tSupport" << std::endl; + + size_t iMin = (((largeItemsetCount > nItemsetToPrint) && (nItemsetToPrint != 0)) ? + largeItemsetCount - nItemsetToPrint : 0); + for (size_t i = iMin; i < largeItemsetCount; i++) + { + std::cout << "{"; + for(size_t l = 0; l < largeItemsetsVector[i].size() - 1; l++) + { + std::cout << largeItemsetsVector[i][l] << ", "; + } + std::cout << largeItemsetsVector[i][largeItemsetsVector[i].size() - 1] << "}\t\t"; + + std::cout << supportVector[i] << std::endl; + } + + largeItemsetsTable->releaseBlockOfRows(block1); + largeItemsetsSupportTable->releaseBlockOfRows(block2); +} + +void printAprioriRules(NumericTablePtr leftItemsTable, + NumericTablePtr rightItemsTable, + NumericTablePtr confidenceTable, + size_t nRulesToPrint = 20) +{ + size_t nRules = confidenceTable->getNumberOfRows(); + size_t nLeftItems = leftItemsTable->getNumberOfRows(); + size_t nRightItems = rightItemsTable->getNumberOfRows(); + + BlockDescriptor block1; + leftItemsTable->getBlockOfRows(0, nLeftItems, readOnly, block1); + int *leftItems = block1.getBlockPtr(); + + BlockDescriptor block2; + rightItemsTable->getBlockOfRows(0, nRightItems, readOnly, block2); + int *rightItems = block2.getBlockPtr(); + + BlockDescriptor block3; + confidenceTable->getBlockOfRows(0, nRules, readOnly, block3); + DAAL_DATA_TYPE *confidence = block3.getBlockPtr(); + + std::vector > leftItemsVector; + leftItemsVector.resize(nRules); + + if (nRules == 0) + { + std::cout << std::endl << "No association rules were found " << std::endl; + return; + } + + for (size_t i = 0; i < nLeftItems; i++) + { + leftItemsVector[leftItems[2 * i]].push_back(leftItems[2 * i + 1]); + } + + std::vector > rightItemsVector; + rightItemsVector.resize(nRules); + + for (size_t i = 0; i < nRightItems; i++) + { + rightItemsVector[rightItems[2 * i]].push_back(rightItems[2 * i + 1]); + } + + std::vector confidenceVector; + confidenceVector.resize(nRules); + + for (size_t i = 0; i < nRules; i++) + { + confidenceVector[i] = confidence[i]; + } + + std::cout << std::endl << "Last " << nRulesToPrint << " association rules: " << std::endl; + std::cout << std::endl << "Rule" << "\t\t\t\tConfidence" << std::endl; + size_t iMin = (((nRules > nRulesToPrint) && (nRulesToPrint != 0)) ? (nRules - nRulesToPrint) : 0); + + for (size_t i = iMin; i < nRules; i++) + { + std::cout << "{"; + for(size_t l = 0; l < leftItemsVector[i].size() - 1; l++) + { + std::cout << leftItemsVector[i][l] << ", "; + } + std::cout << leftItemsVector[i][leftItemsVector[i].size() - 1] << "} => {"; + + for(size_t l = 0; l < rightItemsVector[i].size() - 1; l++) + { + std::cout << rightItemsVector[i][l] << ", "; + } + std::cout << rightItemsVector[i][rightItemsVector[i].size() - 1] << "}\t\t"; + + std::cout << confidenceVector[i] << std::endl; + } + + leftItemsTable->releaseBlockOfRows(block1); + rightItemsTable->releaseBlockOfRows(block2); + confidenceTable->releaseBlockOfRows(block3); +} + +bool isFull(NumericTableIface::StorageLayout layout) +{ + int layoutInt = (int) layout; + if (packed_mask & layoutInt) + { + return false; + } + return true; +} + +bool isUpper(NumericTableIface::StorageLayout layout) +{ + if (layout == NumericTableIface::upperPackedSymmetricMatrix || + layout == NumericTableIface::upperPackedTriangularMatrix) + { + return true; + } + return false; +} + +bool isLower(NumericTableIface::StorageLayout layout) +{ + if (layout == NumericTableIface::lowerPackedSymmetricMatrix || + layout == NumericTableIface::lowerPackedTriangularMatrix) + { + return true; + } + return false; +} + +template +void printArray(T *array, const size_t nPrintedCols, const size_t nPrintedRows, const size_t nCols, std::string message, + size_t interval = 10) +{ + std::cout << std::setiosflags(std::ios::left); + std::cout << message << std::endl; + for (size_t i = 0; i < nPrintedRows; i++) + { + for(size_t j = 0; j < nPrintedCols; j++) + { + std::cout << std::setw(interval) << std::setiosflags(std::ios::fixed) << std::setprecision(3); + std::cout << array[i * nCols + j]; + } + std::cout << std::endl; + } + std::cout << std::endl; +} + +template +void printArray(T *array, const size_t nCols, const size_t nRows, std::string message, size_t interval = 10) +{ + printArray(array, nCols, nRows, nCols, message, interval); +} + +template +void printLowerArray(T *array, const size_t nPrintedRows, std::string message, size_t interval = 10) +{ + std::cout << std::setiosflags(std::ios::left); + std::cout << message << std::endl; + int ind = 0; + for (size_t i = 0; i < nPrintedRows; i++) + { + for(size_t j = 0; j <= i ; j++) + { + std::cout << std::setw(interval) << std::setiosflags(std::ios::fixed) << std::setprecision(3); + std::cout << array[ind++]; + } + std::cout << std::endl; + } + std::cout << std::endl; +} + +template +void printUpperArray(T *array, const size_t nPrintedCols, const size_t nPrintedRows, const size_t nCols, + std::string message, size_t interval = 10) +{ + std::cout << std::setiosflags(std::ios::left); + std::cout << message << std::endl; + int ind = 0; + for (size_t i = 0; i < nPrintedRows; i++) + { + for(size_t j = 0; j < i; j++) + { + std::cout << " "; + } + for(size_t j = i; j < nPrintedCols; j++) + { + std::cout << std::setw(interval) << std::setiosflags(std::ios::fixed) << std::setprecision(3); + std::cout << array[ind++]; + } + for(size_t j = nPrintedCols; j < nCols; j++) + { + ind++; + } + std::cout << std::endl; + } + std::cout << std::endl; +} + +void printNumericTable(NumericTable *dataTable, const char *message = "", + size_t nPrintedRows = 0, size_t nPrintedCols = 0, size_t interval = 10) +{ + size_t nRows = dataTable->getNumberOfRows(); + size_t nCols = dataTable->getNumberOfColumns(); + NumericTableIface::StorageLayout layout = dataTable->getDataLayout(); + + if(nPrintedRows != 0) + { + nPrintedRows = std::min(nRows, nPrintedRows); + } + else + { + nPrintedRows = nRows; + } + + if(nPrintedCols != 0) + { + nPrintedCols = std::min(nCols, nPrintedCols); + } + else + { + nPrintedCols = nCols; + } + + BlockDescriptor block; + if(isFull(layout) || layout == NumericTableIface::csrArray) + { + dataTable->getBlockOfRows(0, nRows, readOnly, block); + printArray(block.getBlockPtr(), nPrintedCols, nPrintedRows, nCols, message, interval); + dataTable->releaseBlockOfRows(block); + } + else + { + PackedArrayNumericTableIface *packedTable = + dynamic_cast(dataTable); + packedTable->getPackedArray(readOnly, block); + if(isLower(layout)) + { + printLowerArray(block.getBlockPtr(), nPrintedRows, message, interval); + } + else if(isUpper(layout)) + { + printUpperArray(block.getBlockPtr(), nPrintedCols, nPrintedRows, nCols, message, interval); + } + packedTable->releasePackedArray(block); + } +} + + +void printNumericTable(NumericTable &dataTable, const char *message = "", + size_t nPrintedRows = 0, size_t nPrintedCols = 0, size_t interval = 10) +{ + printNumericTable(&dataTable, message, nPrintedRows, nPrintedCols, interval); +} + +void printNumericTable(const NumericTablePtr &dataTable, const char *message = "", + size_t nPrintedRows = 0, size_t nPrintedCols = 0, size_t interval = 10) +{ + printNumericTable(dataTable.get(), message, nPrintedRows, nPrintedCols, interval); +} + +void printPackedNumericTable(NumericTable *dataTable, size_t nFeatures, const char *message = "", size_t interval = 10) +{ + BlockDescriptor block; + + dataTable->getBlockOfRows(0, 1, readOnly, block); + + DAAL_DATA_TYPE *data = block.getBlockPtr(); + + std::cout << std::setiosflags(std::ios::left); + std::cout << message << std::endl; + size_t index = 0; + for (size_t i = 0; i < nFeatures; i++) + { + for(size_t j = 0; j <= i; j++, index++) + { + std::cout << std::setw(interval) << std::setiosflags(std::ios::fixed) << std::setprecision(3); + std::cout << data[index]; + } + std::cout << std::endl; + } + std::cout << std::endl; + + dataTable->releaseBlockOfRows(block); +} + +void printPackedNumericTable(NumericTable &dataTable, size_t nFeatures, const char *message = "", size_t interval = 10) +{ + printPackedNumericTable(&dataTable, nFeatures, message); +} + +template +void printNumericTables(NumericTable *dataTable1, + NumericTable *dataTable2, + const char *title1 = "", const char *title2 = "", const char *message = "", size_t nPrintedRows = 0, size_t interval = 15) +{ + size_t nRows1 = dataTable1->getNumberOfRows(); + size_t nRows2 = dataTable2->getNumberOfRows(); + size_t nCols1 = dataTable1->getNumberOfColumns(); + size_t nCols2 = dataTable2->getNumberOfColumns(); + + BlockDescriptor block1; + BlockDescriptor block2; + + size_t nRows = std::min(nRows1, nRows2); + if (nPrintedRows != 0) + { + nRows = std::min(std::min(nRows1, nRows2), nPrintedRows); + } + + dataTable1->getBlockOfRows(0, nRows, readOnly, block1); + dataTable2->getBlockOfRows(0, nRows, readOnly, block2); + + type1 *data1 = block1.getBlockPtr(); + type2 *data2 = block2.getBlockPtr(); + + std::cout << std::setiosflags(std::ios::left); + std::cout << message << std::endl; + std::cout << std::setw(interval * nCols1) << title1; + std::cout << std::setw(interval * nCols2) << title2 << std::endl; + for (size_t i = 0; i < nRows; i++) + { + for (size_t j = 0; j < nCols1; j++) + { + std::cout << std::setw(interval) << std::setiosflags(std::ios::fixed) << std::setprecision(3); + std::cout << data1[i * nCols1 + j]; + } + for (size_t j = 0; j < nCols2; j++) + { + std::cout << std::setprecision(0) << std::setw(interval) << data2[i * nCols2 + j]; + } + std::cout << std::endl; + } + std::cout << std::endl; + + dataTable1->releaseBlockOfRows(block1); + dataTable2->releaseBlockOfRows(block2); +} + +template +void printNumericTables(NumericTable *dataTable1, + NumericTable &dataTable2, + const char *title1 = "", const char *title2 = "", const char *message = "", size_t nPrintedRows = 0, size_t interval = 10) +{ + printNumericTables(dataTable1, &dataTable2, title1, title2, message, nPrintedRows, interval); +} + +void printNumericTables(NumericTable *dataTable1, + NumericTable *dataTable2, + const char *title1 = "", const char *title2 = "", const char *message = "", size_t nPrintedRows = 0, size_t interval = 10) +{ + size_t nRows1 = dataTable1->getNumberOfRows(); + size_t nRows2 = dataTable2->getNumberOfRows(); + size_t nCols1 = dataTable1->getNumberOfColumns(); + size_t nCols2 = dataTable2->getNumberOfColumns(); + + BlockDescriptor block1; + BlockDescriptor block2; + + size_t nRows = std::min(nRows1, nRows2); + if (nPrintedRows != 0) + { + nRows = std::min(std::min(nRows1, nRows2), nPrintedRows); + } + + dataTable1->getBlockOfRows(0, nRows, readOnly, block1); + dataTable2->getBlockOfRows(0, nRows, readOnly, block2); + + DAAL_DATA_TYPE *data1 = block1.getBlockPtr(); + DAAL_DATA_TYPE *data2 = block2.getBlockPtr(); + + std::cout << std::setiosflags(std::ios::left); + std::cout << message << std::endl; + std::cout << std::setw(interval * nCols1) << title1; + std::cout << std::setw(interval * nCols2) << title2 << std::endl; + for (size_t i = 0; i < nRows; i++) + { + for (size_t j = 0; j < nCols1; j++) + { + std::cout << std::setw(interval) << std::setiosflags(std::ios::fixed) << std::setprecision(3); + std::cout << data1[i * nCols1 + j]; + } + for (size_t j = 0; j < nCols2; j++) + { + std::cout << std::setprecision(0) << std::setw(interval) << data2[i * nCols2 + j]; + } + std::cout << std::endl; + } + std::cout << std::endl; + + dataTable1->releaseBlockOfRows(block1); + dataTable2->releaseBlockOfRows(block2); +} + +void printNumericTables(NumericTable *dataTable1, + NumericTable &dataTable2, + const char *title1 = "", const char *title2 = "", const char *message = "", size_t nPrintedRows = 0, size_t interval = 10) +{ + printNumericTables(dataTable1, &dataTable2, title1, title2, message, nPrintedRows, interval); +} + +template +void printNumericTables(NumericTablePtr dataTable1, + NumericTablePtr dataTable2, + const char *title1 = "", const char *title2 = "", const char *message = "", size_t nPrintedRows = 0, size_t interval = 10) +{ + printNumericTables(dataTable1.get(), dataTable2.get(), title1, title2, message, nPrintedRows, interval); +} + +bool checkFileIsAvailable(std::string filename, bool needExit = false) +{ + std::ifstream file(filename.c_str()); + if(file.good()) + { + return true; + } + else + { + std::cout << "Can't open file " << filename << std::endl; + if(needExit) + { + exit(fileError); + } + return false; + } +} + +void checkArguments(int argc, char *argv[], int count, ...) +{ + std::string **filelist = new std::string*[count]; + va_list ap; + va_start(ap, count); + for (int i = 0; i < count; i++) + { + filelist[i] = va_arg(ap, std::string *); + } + va_end(ap); + if(argc == 1) + { + for (int i = 0; i < count; i++) + { + checkFileIsAvailable(*(filelist[i]), true); + } + } + else if(argc == (count + 1)) + { + bool isAllCorrect = true; + for (int i = 0; i < count; i++) + { + if(!checkFileIsAvailable(argv[i + 1])) + { + isAllCorrect = false; + break; + } + } + if(isAllCorrect == true) + { + for(int i = 0; i < count; i++) + { + (*filelist[i]) = argv[i + 1]; + } + } + else + { + std::cout << "Warning: Try to open default datasetFileNames" << std::endl; + for (int i = 0; i < count; i++) + { + checkFileIsAvailable(*(filelist[i]), true); + } + } + } + else + { + std::cout << "Usage: " << argv[0] << " [ "; + for(int i = 0; i < count; i++) + { + std::cout << " "; + } + std::cout << "]" << std::endl; + std::cout << "Warning: Try to open default datasetFileNames" << std::endl; + for (int i = 0; i < count; i++) + { + checkFileIsAvailable(*(filelist[i]), true); + } + } + delete [] filelist; +} + +void copyBytes(daal::byte *dst, daal::byte *src, size_t size) +{ + for(size_t i = 0; i < size; i++) + { + dst[i] = src[i]; + } +} + +size_t checkBytes(daal::byte *dst, daal::byte *src, size_t size) +{ + for(size_t i = 0; i < size; i++) + { + if(dst[i] != src[i]) { return i + 1; } + } + return 0; +} + +static const unsigned int crcRem[] = +{ + 0x00000000, 0x741B8CD6, 0xE83719AC, 0x9C2C957A, 0xA475BF8E, 0xD06E3358, 0x4C42A622, 0x38592AF4, + 0x3CF0F3CA, 0x48EB7F1C, 0xD4C7EA66, 0xA0DC66B0, 0x98854C44, 0xEC9EC092, 0x70B255E8, 0x04A9D93E, + 0x79E1E794, 0x0DFA6B42, 0x91D6FE38, 0xE5CD72EE, 0xDD94581A, 0xA98FD4CC, 0x35A341B6, 0x41B8CD60, + 0x4511145E, 0x310A9888, 0xAD260DF2, 0xD93D8124, 0xE164ABD0, 0x957F2706, 0x0953B27C, 0x7D483EAA, + 0xF3C3CF28, 0x87D843FE, 0x1BF4D684, 0x6FEF5A52, 0x57B670A6, 0x23ADFC70, 0xBF81690A, 0xCB9AE5DC, + 0xCF333CE2, 0xBB28B034, 0x2704254E, 0x531FA998, 0x6B46836C, 0x1F5D0FBA, 0x83719AC0, 0xF76A1616, + 0x8A2228BC, 0xFE39A46A, 0x62153110, 0x160EBDC6, 0x2E579732, 0x5A4C1BE4, 0xC6608E9E, 0xB27B0248, + 0xB6D2DB76, 0xC2C957A0, 0x5EE5C2DA, 0x2AFE4E0C, 0x12A764F8, 0x66BCE82E, 0xFA907D54, 0x8E8BF182, + 0x939C1286, 0xE7879E50, 0x7BAB0B2A, 0x0FB087FC, 0x37E9AD08, 0x43F221DE, 0xDFDEB4A4, 0xABC53872, + 0xAF6CE14C, 0xDB776D9A, 0x475BF8E0, 0x33407436, 0x0B195EC2, 0x7F02D214, 0xE32E476E, 0x9735CBB8, + 0xEA7DF512, 0x9E6679C4, 0x024AECBE, 0x76516068, 0x4E084A9C, 0x3A13C64A, 0xA63F5330, 0xD224DFE6, + 0xD68D06D8, 0xA2968A0E, 0x3EBA1F74, 0x4AA193A2, 0x72F8B956, 0x06E33580, 0x9ACFA0FA, 0xEED42C2C, + 0x605FDDAE, 0x14445178, 0x8868C402, 0xFC7348D4, 0xC42A6220, 0xB031EEF6, 0x2C1D7B8C, 0x5806F75A, + 0x5CAF2E64, 0x28B4A2B2, 0xB49837C8, 0xC083BB1E, 0xF8DA91EA, 0x8CC11D3C, 0x10ED8846, 0x64F60490, + 0x19BE3A3A, 0x6DA5B6EC, 0xF1892396, 0x8592AF40, 0xBDCB85B4, 0xC9D00962, 0x55FC9C18, 0x21E710CE, + 0x254EC9F0, 0x51554526, 0xCD79D05C, 0xB9625C8A, 0x813B767E, 0xF520FAA8, 0x690C6FD2, 0x1D17E304, + 0x5323A9DA, 0x2738250C, 0xBB14B076, 0xCF0F3CA0, 0xF7561654, 0x834D9A82, 0x1F610FF8, 0x6B7A832E, + 0x6FD35A10, 0x1BC8D6C6, 0x87E443BC, 0xF3FFCF6A, 0xCBA6E59E, 0xBFBD6948, 0x2391FC32, 0x578A70E4, + 0x2AC24E4E, 0x5ED9C298, 0xC2F557E2, 0xB6EEDB34, 0x8EB7F1C0, 0xFAAC7D16, 0x6680E86C, 0x129B64BA, + 0x1632BD84, 0x62293152, 0xFE05A428, 0x8A1E28FE, 0xB247020A, 0xC65C8EDC, 0x5A701BA6, 0x2E6B9770, + 0xA0E066F2, 0xD4FBEA24, 0x48D77F5E, 0x3CCCF388, 0x0495D97C, 0x708E55AA, 0xECA2C0D0, 0x98B94C06, + 0x9C109538, 0xE80B19EE, 0x74278C94, 0x003C0042, 0x38652AB6, 0x4C7EA660, 0xD052331A, 0xA449BFCC, + 0xD9018166, 0xAD1A0DB0, 0x313698CA, 0x452D141C, 0x7D743EE8, 0x096FB23E, 0x95432744, 0xE158AB92, + 0xE5F172AC, 0x91EAFE7A, 0x0DC66B00, 0x79DDE7D6, 0x4184CD22, 0x359F41F4, 0xA9B3D48E, 0xDDA85858, + 0xC0BFBB5C, 0xB4A4378A, 0x2888A2F0, 0x5C932E26, 0x64CA04D2, 0x10D18804, 0x8CFD1D7E, 0xF8E691A8, + 0xFC4F4896, 0x8854C440, 0x1478513A, 0x6063DDEC, 0x583AF718, 0x2C217BCE, 0xB00DEEB4, 0xC4166262, + 0xB95E5CC8, 0xCD45D01E, 0x51694564, 0x2572C9B2, 0x1D2BE346, 0x69306F90, 0xF51CFAEA, 0x8107763C, + 0x85AEAF02, 0xF1B523D4, 0x6D99B6AE, 0x19823A78, 0x21DB108C, 0x55C09C5A, 0xC9EC0920, 0xBDF785F6, + 0x337C7474, 0x4767F8A2, 0xDB4B6DD8, 0xAF50E10E, 0x9709CBFA, 0xE312472C, 0x7F3ED256, 0x0B255E80, + 0x0F8C87BE, 0x7B970B68, 0xE7BB9E12, 0x93A012C4, 0xABF93830, 0xDFE2B4E6, 0x43CE219C, 0x37D5AD4A, + 0x4A9D93E0, 0x3E861F36, 0xA2AA8A4C, 0xD6B1069A, 0xEEE82C6E, 0x9AF3A0B8, 0x06DF35C2, 0x72C4B914, + 0x766D602A, 0x0276ECFC, 0x9E5A7986, 0xEA41F550, 0xD218DFA4, 0xA6035372, 0x3A2FC608, 0x4E344ADE +}; + +unsigned int getCRC32( daal::byte *input, unsigned int prevRes, size_t len) +{ + size_t i; + daal::byte *p; + + unsigned int res, highDigit, nextDigit; + const unsigned int crcPoly = 0xBA0DC66B; + + p = input; + + res = prevRes; + + for ( i = 0; i < len; i++) + { + highDigit = res >> 24; + nextDigit = (unsigned int)(p[len - 1 - i]); + res = (res << 8) ^ nextDigit; + res = res ^ crcRem[highDigit]; + } + + if (res >= crcPoly) + { + res = res ^ crcPoly; + } + + return res; +} + +void printALSRatings(NumericTablePtr usersOffsetTable, NumericTablePtr itemsOffsetTable, + NumericTablePtr ratings) +{ + size_t nUsers = ratings->getNumberOfRows(); + size_t nItems = ratings->getNumberOfColumns(); + + BlockDescriptor block1; + ratings->getBlockOfRows(0, nUsers, readOnly, block1); + DAAL_DATA_TYPE *ratingsData = block1.getBlockPtr(); + + size_t usersOffset, itemsOffset; + BlockDescriptor block; + usersOffsetTable->getBlockOfRows(0, 1, readOnly, block); + usersOffset = (size_t)((block.getBlockPtr())[0]); + usersOffsetTable->releaseBlockOfRows(block); + + itemsOffsetTable->getBlockOfRows(0, 1, readOnly, block); + itemsOffset = (size_t)((block.getBlockPtr())[0]); + itemsOffsetTable->releaseBlockOfRows(block); + + std::cout << " User ID, Item ID, rating" << std::endl; + for (size_t i = 0; i < nUsers; i++) + { + for (size_t j = 0; j < nItems; j++) + { + std::cout << i + usersOffset << ", " << j + itemsOffset << ", " << ratingsData[i * nItems + j] << std::endl; + } + } + ratings->releaseBlockOfRows(block1); +} + +void printTensor(TensorPtr dataTable, const char *message = "", + size_t nPrintedRows = 0, size_t nPrintedCols = 0, size_t interval = 10) +{ + const daal::services::Collection &dims = dataTable->getDimensions(); + size_t nRows = dims[0]; + + if(nPrintedRows != 0) + { + nPrintedRows = std::min(nRows, nPrintedRows); + } + else + { + nPrintedRows = nRows; + } + + SubtensorDescriptor block; + + dataTable->getSubtensor(0, 0, 0, nPrintedRows, readOnly, block); + + size_t nCols = block.getSize() / nPrintedRows; + + if(nPrintedCols != 0) + { + nPrintedCols = std::min(nCols, nPrintedCols); + } + else + { + nPrintedCols = nCols; + } + + printArray(block.getPtr(), nPrintedCols, nPrintedRows, nCols, message, interval); + + dataTable->releaseSubtensor(block); +} + +template +void printTensors(TensorPtr dataTable1, + TensorPtr dataTable2, + const char *title1 = "", const char *title2 = "", const char *message = "", size_t nPrintedRows = 0, size_t interval = 15) +{ + const daal::services::Collection &dims1 = dataTable1->getDimensions(); + size_t nRows1 = dims1[0]; + + if(nPrintedRows != 0) { nPrintedRows = std::min(nRows1, nPrintedRows); } + else { nPrintedRows = nRows1; } + + SubtensorDescriptor block1; + dataTable1->getSubtensor(0, 0, 0, nPrintedRows, readOnly, block1); + size_t nCols1 = block1.getSize() / nPrintedRows; + + const daal::services::Collection &dims2 = dataTable2->getDimensions(); + size_t nRows2 = dims2[0]; + + if(nPrintedRows != 0) { nPrintedRows = std::min(nRows2, nPrintedRows); } + else { nPrintedRows = nRows2; } + + SubtensorDescriptor block2; + dataTable2->getSubtensor(0, 0, 0, nPrintedRows, readOnly, block2); + size_t nCols2 = block2.getSize() / nPrintedRows; + + type1 *dataType1 = block1.getPtr(); + type2 *dataType2 = block2.getPtr(); + + std::cout << std::setiosflags(std::ios::left); + std::cout << message << std::endl; + std::cout << std::setw(interval * nCols1) << title1; + std::cout << std::setw(interval * nCols2) << title2 << std::endl; + for (size_t i = 0; i < nPrintedRows; i++) + { + for (size_t j = 0; j < nCols1; j++) + { + std::cout << std::setw(interval) << std::setiosflags(std::ios::fixed) << std::setprecision(3); + std::cout << dataType1[i * nCols1 + j]; + } + for (size_t j = 0; j < nCols2; j++) + { + std::cout << std::setprecision(3) << std::setw(interval / 2) << dataType2[i * nCols2 + j]; + } + std::cout << std::endl; + } + std::cout << std::endl; + + dataTable1->releaseSubtensor(block1); + dataTable2->releaseSubtensor(block2); +} + +void printTensor3d(TensorPtr dataTable, const char *message = "", + size_t nFirstDim = 0, size_t nSecondDim = 0, size_t interval = 10) +{ + const daal::services::Collection &dims = dataTable->getDimensions(); + size_t nRows = dims[0]; + size_t nCols = dims[1]; + + if(nFirstDim != 0) + { + nFirstDim = std::min(nRows, nFirstDim); + } + else + { + nFirstDim = nRows; + } + + if(nSecondDim != 0) + { + nSecondDim = std::min(nCols, nSecondDim); + } + else + { + nSecondDim = nCols; + } + + SubtensorDescriptor block; + + std::cout << message << std::endl; + for (size_t i = 0; i < nFirstDim; i++) + { + dataTable->getSubtensor(1, &i, 0, nSecondDim, readOnly, block); + + size_t nThirdDim = block.getSize() / nSecondDim; + + printArray(block.getPtr(), nThirdDim, nSecondDim, nThirdDim, "", interval); + + dataTable->releaseSubtensor(block); + fflush(0); + } +} + +TensorPtr readTensorFromCSV(const std::string &datasetFileName, bool allowOneColumn = false) +{ + FileDataSource dataSource(datasetFileName, DataSource::doAllocateNumericTable, DataSource::doDictionaryFromContext); + dataSource.loadDataBlock(); + + daal::services::SharedPtr > ntPtr = + daal::services::staticPointerCast, NumericTable>(dataSource.getNumericTable()); + + daal::services::Collection dims; + dims.push_back(ntPtr->getNumberOfRows()); + size_t size = dims[0]; + if (ntPtr->getNumberOfColumns() > 1 || allowOneColumn) + { + dims.push_back(ntPtr->getNumberOfColumns()); + size *= dims[1]; + } + + HomogenTensor *tensor = new HomogenTensor( dims, Tensor::doAllocate ); + DAAL_DATA_TYPE *tensorData = tensor->getArray(); + DAAL_DATA_TYPE *ntData = ntPtr->getArray(); + + for(size_t i = 0; i < size; i++) + { + tensorData[i] = ntData[i]; + } + + TensorPtr tensorPtr(tensor); + + return tensorPtr; +} + +TensorPtr getNextSubtensor(TensorPtr inputTensor, size_t startPos, size_t nElements) +{ + daal::services::Collection dims = inputTensor->getDimensions(); + dims[0] = nElements; + + SubtensorDescriptor subtensorBlock; + inputTensor->getSubtensor(0, 0, startPos, nElements, readOnly, subtensorBlock); + + HomogenTensor *subtensor = new HomogenTensor(dims, Tensor::doAllocate); + DAAL_DATA_TYPE *subtensorData = subtensor->getArray(); + DAAL_DATA_TYPE *inputData = subtensorBlock.getPtr(); + + for(size_t i = 0; i < subtensor->getSize(); i++) + { + subtensorData[i] = inputData[i]; + } + + inputTensor->releaseSubtensor(subtensorBlock); + TensorPtr subtensorPtr(subtensor); + return subtensorPtr; +} + +#endif diff --git a/src/daal.mk b/src/daal.mk index 018bd0de8..2026e274e 100644 --- a/src/daal.mk +++ b/src/daal.mk @@ -10,3 +10,5 @@ DAAL_INCLUDE := -I$HOME/daal/include DAAL_HDR = \ cpu/daal/include/debug.hpp \ cpu/daal/include/iinput.hpp + +DAAL_DEF = DDAAL_DEF From bf6fbe0a6d2c0d8e6d3f334a1d203c9c9f325260 Mon Sep 17 00:00:00 2001 From: Monika vos Mueller Date: Wed, 4 Apr 2018 15:24:38 -0700 Subject: [PATCH 3/8] c interface to daal --- src/cpu/daal/h2o4gpu_daal_c.cpp | 137 +++++++++++++++++++++++++++++ src/cpu/daal/h2o4gpu_daal_c.h | 87 ++++++++++++++++++ src/cpu/daal/iinput.hpp | 66 ++++++++++---- src/cpu/daal/linear_regression.hpp | 54 ++++++++++++ src/cpu/daal/regression.hpp | 52 +++++++++++ src/cpu/daal/ridge_regression.hpp | 55 ++++++++++++ src/cpu/daal/svd.hpp | 24 +++-- src/cpu/daal/utils/defines.h | 46 ++++++++++ 8 files changed, 498 insertions(+), 23 deletions(-) create mode 100644 src/cpu/daal/h2o4gpu_daal_c.cpp create mode 100644 src/cpu/daal/h2o4gpu_daal_c.h create mode 100644 src/cpu/daal/linear_regression.hpp create mode 100644 src/cpu/daal/regression.hpp create mode 100644 src/cpu/daal/ridge_regression.hpp create mode 100644 src/cpu/daal/utils/defines.h diff --git a/src/cpu/daal/h2o4gpu_daal_c.cpp b/src/cpu/daal/h2o4gpu_daal_c.cpp new file mode 100644 index 000000000..13ffe1f50 --- /dev/null +++ b/src/cpu/daal/h2o4gpu_daal_c.cpp @@ -0,0 +1,137 @@ +/* + * h2o4gpu_daal_c.cpp + * + * Created on: Apr 2, 2018 + * Author: monika + */ + +#include "h2o4gpu_daal_c.h" +#include "iinput.hpp" +#include "regression.hpp" + + +extern "C" { + // Daal input + void* CreateDaalInput(double *pData, size_t m_dim, size_t n_dim) { + return new(std::nothrow) HomogenousDaalData(pData, m_dim, n_dim); + } + void* CreateDaalInput(double* featuresData, size_t m_features, size_t n_features, + double* dependentData, size_t m_dependent, size_t n_dependent) { + return new(std::nothrow) HomogenousDaalData(featuersData, m_features, n_features, + dependentData, m_dependent, n_dependent); + } + void* CreateDaalInput(const char* filename) { + return new(std::nothrow) HomogenousDaalData(filename); + } + void* CreateDaalInput(const char* filename, size_t features, size_t dependentVariables) { + return new(std;:nothrow) HomogenousDaalData(filename, features, dependentVariables); + } + void DeleteDaalInput_d(void* input) { + DeleteDaalInput(input); + } + void printDaalInput_d(void *input,c onst char* msg) { + printDaalInput(input, msg); + } + void *getDaalNT_d(const void* input) { + return getDaalNT(input); + } + void printDaalNT(const void* input, const char* msg, size_t rows=0, size_t columns=0) { + try { + auto p = static_cast(input); + printNumericTable(p, msg, rows, columns); + } + } + + // Singular Value Decomposition + void* CreateDaalSVD(const void* input) { + try { + auto in = static_cast(input); + } CATCH + return new(std::nothrow) SVD(*in); + } + void* CreateDaalSVD(double* input, size_t m_dim, size_t n_dim) { + return CreateSVD(input, m_dim, n_dim); + } + void DeleteSVD(void* pSVD) { + delete static_cast(pSVD); + } + const void fitDaalSVD(const void* svd) { + try { + auto psvd = static_cast(svd); + psvd->fit(); + } CATCH + } + const void* getDaalSigmas(const void* svd) const { + try { + auto psvd = static_cast(svd); + return psvd->getSingularValues(); + } CATCH + return nullptr; + } + const void* getDaalRightSingularMatrix(const void* svd) const { + try { + auto psvd = static_cast(svd); + return psvd->getRightSingularMatrix(); + } CATCH + return nullptr; + } + const void* getDaalLeftSingularMatrix(const void* svd) const { + try { + auto psvd = static_cast(svd); + return psvd->getLeftSingularMatrix(); + } CATCH + return nullptr; + } + // Regression + void* CreateDaalRegression(const void* input) { + try { + auto in = static_cast(input); + } CATCH + return new(std::nothrow) Regression(*in); + } + void* CreateDaalRegression() { + return new(std::nothrow) Regression(); + } + void DeleteDaalRegression(void* r) { + delete static_cast(r); + } + const void* train(Regression::Type type, const void* regression) { + try { + Regression* reg= nullptr; + if (type == Regression::type.linear) + reg = dynamic_cast(regression); + else if(type == Regression::type.rigid) + reg = dynamic_cast(regression); + else + throw "Either linear or rigid regression is possible."; + return reg->train(); + } CATCH + return nullptr; + } + const void* predict(Regression::Type type, const void* regression, const void* input) { + try { + Regression* reg = nullptr; + auto in = static_cast(input); + if (type == Regression::type.linear) + reg = dynamic_cast(regression); + else if (type == Regression::type.rigid) + reg = dynamic_cast(regression); + else throw "Either linear or rigid regression is possible."; + return reg->predict(*in); + } CATCH + return nullptr; + } + void setInput(Regression::Type type, const void* regression, const void* input) { + try { + auto in = static_cast(input); + Regression* reg = nullptr; + if (type == Regression::type.linear) + reg = dynamic_cast(regression); + else if (type == Regression::type.rigid) + reg = dynamic_cast(regression); + else throw "Either linear or rigid regression is possible."; + return reg->setInput(*in); + } + } +} + diff --git a/src/cpu/daal/h2o4gpu_daal_c.h b/src/cpu/daal/h2o4gpu_daal_c.h new file mode 100644 index 000000000..c8689a476 --- /dev/null +++ b/src/cpu/daal/h2o4gpu_daal_c.h @@ -0,0 +1,87 @@ +/* + * h2o4gpu_daal_c.h + * + * Created on: Apr 2, 2018 + * Author: monika + */ + +#ifndef SRC_CPU_DAAL_H2O4GPU_DAAL_C_H_ +#define SRC_CPU_DAAL_H2O4GPU_DAAL_C_H_ + +#include +#include // for size_t +#include +#include +#include +#include +#include "utils/defines.h" +#include "iinput.hpp" +#include "svd.hpp" +#include + + +using namespace H2O4GPU::DAAL; + +#define CATCH_DAAL \ + catch(const std::runtime_error &e) { \ + fprintf(stderr, "Runtime error: %s in %s at line %d", e.what(), __FILE__, __LINE__); \ + } catch(const std::exception &e) { \ + fprintf(stderr, "Error occurred: %s in %s at line %d", e.what(), __FILE__, __LINE__); \ + } catch (...) { \ + fprintf(stderr, "Unknown failure occurred. Possible memory corruption."); \ + } + +//input to enter algorithms +void *CreateDaalInput(double*, size_t, size_t); +void *CreateDaalInput(double*, size_t, size_t, double*, size_t, size_t); +void *CreateDaalInput(const std::string&); +void *CreateDaalInput(const std::string&, size_t size_t) +void *CreateDaalInput(const std::string&, size_t, size_t, size_t, size_t); + +template +void DeleteDaalInput(void* input) { + delete static_cast *>(input); +} +template +void printDaalInput(void* input, const char* msg) { + try { + auto di = static_cast *>(input); + auto pnt = di->getNumericTable(); + printNumericTable(pnt, msg); + } CATCH_DAAL +} +template +void* getDaalNT(const void* daalInput) { + try { + auto di = static_cast *>(daalInput); + auto pnt = di->getNumericTable(); + return pnt; + } CATCH_DAAL + return nullptr; +} +void printDaalNT(const void*,const char*, size_t, size_t); + + +// Singular value decomposition algorithm +void* CreateDaalSVD(const void*); +void* CreateDaalSVD(double*, size_t, size_t); +void DeleteDaalSVD(void*); +void fitDaalSVD(const void*); +const void* getDaalSigmas(const void*) const; +const void* getDaalRightSingularMatrix(const void*) const; +const void* getDaalLeftSingularMatrix(const void*) const; + +// Regression +void* CreateDaalRegression(const void*); +void* CreateDaalRegression(); +void DeleteDaalRegression(); +const void* train(Regression::Type); +const void* predict(Regression::Type, const void*); +void setInput(const void*); + + + + + + +#endif /* SRC_CPU_DAAL_H2O4GPU_DAAL_C_H_ */ diff --git a/src/cpu/daal/iinput.hpp b/src/cpu/daal/iinput.hpp index 635ae4d72..589d16c73 100644 --- a/src/cpu/daal/iinput.hpp +++ b/src/cpu/daal/iinput.hpp @@ -14,6 +14,7 @@ #include #include #include +#include "./utils/defines.h" namespace H2O4GPU { namespace DAAL { @@ -22,27 +23,52 @@ using namespace daal; using namespace daal::services; using namespace daal::data_management; +/* print out base class */ +class PrintNumericTable { +protected: + void print(const NumericTablePtr& input, const std::string& msg) const { + printNumericTable(input, msg); + } +}; + /* IInput interface */ template class IInput { protected: - SharedPtr _inputData; + NumericTablePtr _inputData; + NumericTablePtr _featuresData; + NumericTablePtr _dependentData; public: virtual const SharedPtr& getNumericTable() const { return this->_inputData; } + virtual const NumericTablePtr& getFeaturesTable() const { + return this->_featuresData; + } + virtual const NumericTablePtr& getDependentTable() const { + return this->_dependentData; + } + virtual ~IInput() {} }; /* HomogenousDaalData implementation for primitive data type: int, float, double */ -template +template class HomogenousDaalData : public IInput { public: - HomogenousDaalData(Input* const inputArray, size_t m_dim, size_t n_dim) { + HomogenousDaalData(Input* inputArray, size_t m_dim, size_t n_dim) { if (!std::is_arithmetic::value) throw "Input data is not artithmetic type!"; this->_inputData = SharedPtr(new Matrix(m_dim, n_dim, inputArray)); + this->_featuresData = this->_inputData; + } + HomogenousDaalData(Input* features, size_t m_features, size_t n_features, + Input* dependentVariables, size_t m_dependent, size_t n_dependent) { + if (!std::is_arithmetic::value) + throw "Input data is not artithmetic type!"; + this->_featuresData = NumericTablePtr(new Matrix(m_features, n_features)); + this->_dependentDAta = NuemricTablePtr(new Matrix(m_dependent, n_dependent)); } virtual ~HomogenousDaalData() {} }; @@ -62,7 +88,24 @@ class HomogenousDaalData : public IInput DataSource::doDictionaryFromContext); // retrieve data from the input file dataSource.loadDataBlock(); - this->_inputData = dataSource.getNumericTable(); + this->_featuresData = this->_inputData = dataSource.getNumericTable(); + } + + HomogenousDaalData(const std::string & filename, size_t features, size_t dependentVariables) { + if (!this->fileExists(filename)) + throw "Input file doesn't exist!"; + if ("csv" != this->getFileExt(filename)) + throw "Input file isn't csv file format!"; + FileDataSource dataSource(fileName, + DataSource::doAllocateNumericTable, + DataSource::doDictionaryFromContext); + this->_featuresData = + new HomogenNumericTable<>(features, 0, NumericTable::doNotAllocate); + this->_dependentData = + new HomogenNumericTable<>(dependentVariables, 0, NumericTable::doNotAllocate); + NumericTablePtr mergedData(new MergedNumericTable(featuresData, dependentData)); + dataSource.loadDataBlock(mergedData.get()); + this->_featuresData = this->_inputData = dataSource.getNumericTable(); } inline bool fileExists(const std::string &filename) { @@ -77,11 +120,12 @@ class HomogenousDaalData : public IInput } return(""); } -} +}; /* convert numericTable to raw pointer -> memory released in daal */ -template +template const Output* getRawOutput(const NumericTablePtr& nt) { + rows = (rows == 0) ? (*nt.get()).getNumberOfRows() : rows; BlockDescriptor block; (*nt.get()).getBlockOfRows(0, rows, readOnly, block); const Output *array = block.getBlockPtr(); @@ -89,17 +133,9 @@ const Output* getRawOutput(const NumericTablePtr& nt) { return array; } -template -const Output* getRawOutput(const NumericTablePtr& nt) { - BlockDescriptor block; - auto rows = (*nt.get()).getNumberOfRows(); - (*nt.get()).getBlockOfRows(0, rows, readOnly, block); - const double *array = block.getBlockPtr(); - (*nt.get()).releaseBlockOfRows(block); - return array; -} } /* end of DAAL namespace */ } /* end of H2O4GPU namespace */ + #endif /* SRC_CPU_DAAL_INCLUDE_IINPUT_HPP_ */ diff --git a/src/cpu/daal/linear_regression.hpp b/src/cpu/daal/linear_regression.hpp new file mode 100644 index 000000000..3ea0cfd8a --- /dev/null +++ b/src/cpu/daal/linear_regression.hpp @@ -0,0 +1,54 @@ +/* + * LinearRegression.hpp + * + * Created on: Apr 4, 2018 + * Author: monika + */ + +#ifndef SRC_CPU_DAAL_LINEAR_REGRESSION_HPP_ +#define SRC_CPU_DAAL_LINEAR_REGRESSION_HPP_ + +#include +#include // for size_t +#include +#include +#include "iinput.hpp" +#include "./utils/defines.h" +#include "regression.hpp" + +namespace H2O4GPU { +namespace DAAL { + +using namespace daal; +using namespace daal::algorithms::linear_regression; + + +template +class LinearRegression : public Regression { +public: + DlL_PUBLIC virtual const trainingResult& train() { + training::Batch<> algorithm; + algorithm.input.set(training::data, this->_featuresData); + algorithm.input.set(training::dependentVariables, this->_dependentData); + // compute + algorithm.compute(); + this->_trainingResult = algorithm.getResult(); + return this->_trainingResult; + } + + DLL_PUBLIC virtual const predictionResult& predict(const IInput& test) { + NumericTablePtr testData = std::move(test.getFeaturesTable()); + predicton::Batch<> algorithm; + algorithm.input.set(prediction::data, testData); + algorithm.iput.set(prediction::model, this->_trainingResult->get(training::model)); + // compute + algorithm.compute(); + predictionResult result = algorithm.getResult(); + return result->get(prediction::prediction); + } +}; + +}} // H2O4GPU::DAAL + + +#endif /* SRC_CPU_DAAL_LINEAR_REGRESSION_HPP_ */ diff --git a/src/cpu/daal/regression.hpp b/src/cpu/daal/regression.hpp new file mode 100644 index 000000000..045797380 --- /dev/null +++ b/src/cpu/daal/regression.hpp @@ -0,0 +1,52 @@ +/* + * regression.hpp + * + * Created on: Apr 4, 2018 + * Author: monika + */ + +#ifndef SRC_CPU_DAAL_REGRESSION_HPP_ +#define SRC_CPU_DAAL_REGRESSION_HPP_ + +#include +#include // for size_t +#include +#include +#include "iinput.hpp" +#include "./utils/defines.h" + +namespace H2O4GPU { +namespace DAAL { + +using namespace daal; +using namespace daal::algorithms::regression; + +template +class Regression : public PrintNumericTable { +public: + enum Type { linear, ridge }; + typedef training::ResultPtr trainingResult; + typedef prediction::ResultPtr predictionResult; + virtual ~Regresion() {} + DLL_PUBLIC Regression() {} + DLL_PUBLIC Regression(const IInput& input) { + this->_featuresData = input.getFeaturesTable(); + this->_dependentData = input.getDependentTable(); + } + DLL_PUBLIC virtual void SetInput(IInput& input) { + this->_featuresData = std::move(input.getFeaturesTable()); + this->_dependentData = std::move(input.getDependentTable()); + } + DLL_PUBLIC virtual const trainingResult& train() = 0; + DLL_PUBLIC virtual const predictionResult& predict(const IInput& input) = 0; +protected: + NumericTablePtr _featuresData; + NumericTablePtr _dependentData; + NumericTablePtr _beta; + trainingResult _trainingResult; + predictionResult _predictionResult; +}; + + + +#endif /* SRC_CPU_DAAL_REGRESSION_HPP_ */ diff --git a/src/cpu/daal/ridge_regression.hpp b/src/cpu/daal/ridge_regression.hpp new file mode 100644 index 000000000..48ad44172 --- /dev/null +++ b/src/cpu/daal/ridge_regression.hpp @@ -0,0 +1,55 @@ +/* + * regression.hpp + * + * Created on: Apr 4, 2018 + * Author: monika + */ + +#ifndef SRC_CPU_DAAL_RIDGE_REGRESSION_HPP_ +#define SRC_CPU_DAAL_RIDGE_REGRESSION_HPP_ + +#include +#include // for size_t +#include +#include +#include "iinput.hpp" +#include "./utils/defines.h" + +namespace H2O4GPU { +namespace DAAL { + +using namespace daal; +using namespace daal::algorithms::ridge_regression; + +template +class RidgeRegression : public Regression { +public: + DLL_PUBLIC virtual const trainingResult& train() { + training::Batch algorithm; + algorithm.input.set(training::data, this->_featuresData); + algorithm.input.set(training::dependentVariables, this->_dependentData); + // compute + algorithm.compute(); + this->_trainingResult = algorithm.getResult(); + return this->_trainingResult; + } + + DLL_PUBLIC const NumericTablePtr& getBeta() const { + this->_trainingResult->get(training::model)->getBeta(); + return this->_trainingResult; + } + DLL_PUBLIC virtual const predictionResult& predict(IInput& input) { + NumericTablePtr testData = std::move(input.getFeaturesTable()); + prediction::Batch<> algorithm; + algorithm.input.set(prediction::data, testData); + algorithm.input.set(prediction::model, this->_trainingResult->get(training::model)); + // compute + algorithm.compute(); + predictionResult result = algorithm.getResult(); + return result->get(prediction::prediction); + } +}; + + + +#endif /* SRC_CPU_DAAL_RIDGE_REGRESSION_HPP_ */ diff --git a/src/cpu/daal/svd.hpp b/src/cpu/daal/svd.hpp index d23bac8e1..6d64271c6 100644 --- a/src/cpu/daal/svd.hpp +++ b/src/cpu/daal/svd.hpp @@ -7,7 +7,10 @@ #define SRC_CPU_DAAL_INCLUDE_SVD_H_ #include +#include // for size_t +#include #include "iinput.hpp" +#include "./utils/defines.h" namespace H2O4GPU { namespace DAAL { @@ -25,32 +28,37 @@ class SVD { NumericTablePtr _rightSingularMatrix; NumericTablePtr _leftSingularMatrix; - template - SVD(const IInput& input) { - this->_origData; + template + DLL_PUBLIC SVD(const IInput& input) { + this->_origData = input.getNumericTable(); } - void fit() { + template + DLL_PUBLIC SVD(Input* inputArray, size_t m_dim, size_t n_dim) { + auto table_nt = std::make_shared >(inputArray, m_dim, n_dim); + this->_origData = table_nt->getNumericTable(); + } + + DLL_PUBLIC void fit() { svd::Batch<> algorithm; algorithm.input.set(svd::data, this->_origData); // Compute SVD algorithm.compute(); this->_result = algorithm.getResult(); - this->_result->get(svd::singularValues); } - const NumericTablePtr getSingularValues() const + DLL_PUBLIC const NumericTablePtr& getSingularValues() const { return this->_result->get(svd::singularValues); } - const NumericTablePtr getRightSingularMatrix() const + DLL_PUBLIC const NumericTablePtr& getRightSingularMatrix() const { return this->_result->get(svd::rightSingularMatrix); } - const NumericTablePtr getLeftSingularMatrix() const + DLL_PUBLIC const NumericTablePtr& getLeftSingularMatrix() const { return this->_result->get(svd::leftSingularMatrix); } diff --git a/src/cpu/daal/utils/defines.h b/src/cpu/daal/utils/defines.h new file mode 100644 index 000000000..c90863aa2 --- /dev/null +++ b/src/cpu/daal/utils/defines.h @@ -0,0 +1,46 @@ +/* + * defines.h + * + * Created on: Mar 30, 2018 + * Author: monika + */ + +#ifndef SRC_CPU_DAAL_UTILS_DEFINES_H_ +#define SRC_CPU_DAAL_UTILS_DEFINES_H_ + +#include // for size_t +#include +#include + +#ifdef __cplusplus +# define DLLEXPORT extern "C" __declspec(dllexport) +#else +# define DLLEXPORT +#endif + +#if __GNUC__ >= 4 + #define DLL_PUBLIC __attribute__ ((visibility ("default"))) + #define DLL_LOCAL __attribute__ ((visibility ("hidden"))) +#else + #define DLL_PUBLIC + #define DLL_LOCAL +#endif + +namespace H2O4GPU { +namespace DAAL { +#define double DEF_TYPE; + +template +struct Either { + union { + Left left_value; + Right right_value; + }; + bool is_left; +}; + +} // end of DAAL namespace +} // end of H2O4GPU namespace + + +#endif /* SRC_CPU_DAAL_UTILS_DEFINES_H_ */ From 7f3043b90be308d97c894aa8094879ebb05579f9 Mon Sep 17 00:00:00 2001 From: Monika vos Mueller Date: Sun, 8 Apr 2018 13:24:10 -0700 Subject: [PATCH 4/8] Daal cpp finalized --- src/cpu/daal/Makefile | 90 ++++++++++ src/cpu/daal/h2o4gpu_daal_c.cpp | 270 ++++++++++++++++------------- src/cpu/daal/h2o4gpu_daal_c.h | 86 ++++----- src/cpu/daal/iinput.cpp | 179 +++++++++++++++++++ src/cpu/daal/iinput.h | 105 +++++++++++ src/cpu/daal/iinput.hpp | 141 --------------- src/cpu/daal/linear_regression.cpp | 60 +++++++ src/cpu/daal/linear_regression.h | 52 ++++++ src/cpu/daal/linear_regression.hpp | 2 +- src/cpu/daal/regression.hpp | 52 ------ src/cpu/daal/ridge_regression.cpp | 56 ++++++ src/cpu/daal/ridge_regression.h | 53 ++++++ src/cpu/daal/ridge_regression.hpp | 55 ------ src/cpu/daal/svd.cpp | 44 +++++ src/cpu/daal/svd.h | 51 ++++++ src/cpu/daal/svd.hpp | 70 -------- src/cpu/daal/utils/defines.h | 34 ++-- src/daal.mk | 14 -- 18 files changed, 893 insertions(+), 521 deletions(-) create mode 100644 src/cpu/daal/Makefile create mode 100644 src/cpu/daal/iinput.cpp create mode 100644 src/cpu/daal/iinput.h delete mode 100644 src/cpu/daal/iinput.hpp create mode 100644 src/cpu/daal/linear_regression.cpp create mode 100644 src/cpu/daal/linear_regression.h delete mode 100644 src/cpu/daal/regression.hpp create mode 100644 src/cpu/daal/ridge_regression.cpp create mode 100644 src/cpu/daal/ridge_regression.h delete mode 100644 src/cpu/daal/ridge_regression.hpp create mode 100644 src/cpu/daal/svd.cpp create mode 100644 src/cpu/daal/svd.h delete mode 100644 src/cpu/daal/svd.hpp delete mode 100644 src/daal.mk diff --git a/src/cpu/daal/Makefile b/src/cpu/daal/Makefile new file mode 100644 index 000000000..22d2cdbf4 --- /dev/null +++ b/src/cpu/daal/Makefile @@ -0,0 +1,90 @@ +location = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) +WHERE_ART_THOU := $(location) +$(info ** -> $(WHERE_ART_THOU)) +$(info ** ------------------------------------------------------------------ **) + +DAAL := $(shell ldconfig -p | grep daal_core) + +ifndef DAAL +$(warning ** NO DAAL found. running without DAAL library.) +else +$(info ** DAAL librar installed on your system.) +endif + +# detect Operating System +CFLAGS += -c +OSFLAG := +ifeq ($(OS),Windows_NT) + OSFLAG += -D WIN32 + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + OSFLAG += -D AMD64 + endif + ifeq ($(PROCESSOR_ARCHITECTURE),x86) + OSFLAG += -D IA32 + endif + TARGET = libh2o4gpu_daal.dll + CFLAGS += -O2 -std=c++11 -stdlib=libc++ +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S),Linux) + OSFLAG += -D LINUX + ifndef CC + export CC = gcc + endif + ifndef CXX + export CXX = g++ + endif + endif + ifeq ($(UNAME_S),Darwin) + OSFLAG += -D OSX + ifndef CC + export CC = $(if $(shell which clang), clang, gcc) + endif + ifndef CXX + export CXX = $(if $(shell which clang++), clang++, g++) + endif + endif + UNAME_P := $(shell uname -p) + ifeq ($(UNAME_P),x86_64) + OSFLAG += -D AMD64 + endif + ifneq ($(filter %86,$(UNAME_P)),) + OSFLAG += -D IA32 + endif + ifneq ($(filter arm%,$(UNAME_P)),) + OSFLAG += -D ARM + endif + TARGET = libh2o4gpu_daal.so + CFLAGS += -O2 -std=gnu++11 -fPIC +endif + + +PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) + +SRC_DIR := $(PROJECT_ROOT) +OBJ_DIR := $(PROJECT_DIR)obj +$(shell mkdir -p $(OBJ_DIR)) +SRC_FILES := $(wildcard $(SRC_DIR)*.cpp) +OBJ_FILES := $(patsubst $(SRC_DIR)%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES)) + +DAALINCLUDE := -I$(HOME)/daal/include +DAALLIB := -ldaal_core -ldaal_sequential -lpthread -lm + +ifdef DAAL +all: $(TARGET) + +$(TARGET): $(OBJ_FILES) + @echo "--- Linking Daal ---" + $(CXX) -shared $(DAALLIB) -o $@ $^ + +$(OBJ_DIR)/%.o: $(SRC_DIR)%.cpp + $(CXX) $(CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(DAALINCLUDE) -o $@ $< $(OSFLAGS) + +clean: + @echo "--- Cleaning Daal ---" + rm -rf $(TARGET) $(OBJ_FILES) + +endif + + + diff --git a/src/cpu/daal/h2o4gpu_daal_c.cpp b/src/cpu/daal/h2o4gpu_daal_c.cpp index 13ffe1f50..b1dbee48d 100644 --- a/src/cpu/daal/h2o4gpu_daal_c.cpp +++ b/src/cpu/daal/h2o4gpu_daal_c.cpp @@ -4,134 +4,158 @@ * Created on: Apr 2, 2018 * Author: monika */ - +#include +#include +#include +#include +#include #include "h2o4gpu_daal_c.h" -#include "iinput.hpp" -#include "regression.hpp" +#include "iinput.h" +#include "svd.h" +#include "ridge_regression.h" +#include "linear_regression.h" +using namespace H2O4GPU::DAAL; -extern "C" { // Daal input - void* CreateDaalInput(double *pData, size_t m_dim, size_t n_dim) { - return new(std::nothrow) HomogenousDaalData(pData, m_dim, n_dim); - } - void* CreateDaalInput(double* featuresData, size_t m_features, size_t n_features, - double* dependentData, size_t m_dependent, size_t n_dependent) { - return new(std::nothrow) HomogenousDaalData(featuersData, m_features, n_features, - dependentData, m_dependent, n_dependent); - } - void* CreateDaalInput(const char* filename) { - return new(std::nothrow) HomogenousDaalData(filename); - } - void* CreateDaalInput(const char* filename, size_t features, size_t dependentVariables) { - return new(std;:nothrow) HomogenousDaalData(filename, features, dependentVariables); - } - void DeleteDaalInput_d(void* input) { - DeleteDaalInput(input); - } - void printDaalInput_d(void *input,c onst char* msg) { - printDaalInput(input, msg); - } - void *getDaalNT_d(const void* input) { - return getDaalNT(input); - } - void printDaalNT(const void* input, const char* msg, size_t rows=0, size_t columns=0) { - try { - auto p = static_cast(input); - printNumericTable(p, msg, rows, columns); - } - } +void* CreateDaalInput(float *pData, size_t m_dim, size_t n_dim) { + return new(std::nothrow) HomogenousDaalData(pData, m_dim, n_dim); +} - // Singular Value Decomposition - void* CreateDaalSVD(const void* input) { - try { - auto in = static_cast(input); - } CATCH +void* CreateDaalInputFeaturesDependent(float* featuresData, size_t m_features, size_t n_features, + float* dependentData, size_t m_dependent, size_t n_dependent) { + return new(std::nothrow) HomogenousDaalData(featuresData, m_features, n_features, + dependentData, m_dependent, n_dependent); +} +void* CreateDaalInputFile(const char* filename) { + return new(std::nothrow) HomogenousDaalData(filename); +} +void* CreateDaalInputFileFeaturesDependent(const char* filename, size_t features, size_t dependentVariables) { + return new(std::nothrow) HomogenousDaalData(filename, features, dependentVariables); +} +void DeleteDaalInput(void* input) { + delete static_cast *>(input); +} +void PrintDaalNumericTablePtr(float *input,const char* msg, size_t rows, size_t cols) { + try { + const NumericTablePtr* input = static_cast(input); + PrintTable pt; + pt.print(*input, std::string(msg), rows, cols); + } CATCH_DAAL +} +// Singular Value Decomposition +void* CreateDaalSVD(void* input) { + try { + IInput* in = static_cast *>(input); return new(std::nothrow) SVD(*in); - } - void* CreateDaalSVD(double* input, size_t m_dim, size_t n_dim) { - return CreateSVD(input, m_dim, n_dim); - } - void DeleteSVD(void* pSVD) { - delete static_cast(pSVD); - } - const void fitDaalSVD(const void* svd) { - try { - auto psvd = static_cast(svd); - psvd->fit(); - } CATCH - } - const void* getDaalSigmas(const void* svd) const { - try { - auto psvd = static_cast(svd); - return psvd->getSingularValues(); - } CATCH - return nullptr; - } - const void* getDaalRightSingularMatrix(const void* svd) const { - try { - auto psvd = static_cast(svd); - return psvd->getRightSingularMatrix(); - } CATCH - return nullptr; - } - const void* getDaalLeftSingularMatrix(const void* svd) const { - try { - auto psvd = static_cast(svd); - return psvd->getLeftSingularMatrix(); - } CATCH - return nullptr; - } - // Regression - void* CreateDaalRegression(const void* input) { - try { - auto in = static_cast(input); - } CATCH - return new(std::nothrow) Regression(*in); - } - void* CreateDaalRegression() { - return new(std::nothrow) Regression(); - } - void DeleteDaalRegression(void* r) { - delete static_cast(r); - } - const void* train(Regression::Type type, const void* regression) { - try { - Regression* reg= nullptr; - if (type == Regression::type.linear) - reg = dynamic_cast(regression); - else if(type == Regression::type.rigid) - reg = dynamic_cast(regression); - else - throw "Either linear or rigid regression is possible."; - return reg->train(); - } CATCH - return nullptr; - } - const void* predict(Regression::Type type, const void* regression, const void* input) { - try { - Regression* reg = nullptr; - auto in = static_cast(input); - if (type == Regression::type.linear) - reg = dynamic_cast(regression); - else if (type == Regression::type.rigid) - reg = dynamic_cast(regression); - else throw "Either linear or rigid regression is possible."; - return reg->predict(*in); - } CATCH - return nullptr; - } - void setInput(Regression::Type type, const void* regression, const void* input) { - try { - auto in = static_cast(input); - Regression* reg = nullptr; - if (type == Regression::type.linear) - reg = dynamic_cast(regression); - else if (type == Regression::type.rigid) - reg = dynamic_cast(regression); - else throw "Either linear or rigid regression is possible."; - return reg->setInput(*in); - } - } + } CATCH_DAAL + return nullptr; +} +void DeleteDaalSVD(void* input) { + delete static_cast(input); +} +void fitDaalSVD(void* svd) { + try { + auto psvd = static_cast(svd); + psvd->fit(); + } CATCH_DAAL +} +const void* getDaalSVDSigma(void* svd) { + try { + auto psvd = static_cast(svd); + return static_cast(&psvd->getSingularValues()); + } CATCH_DAAL + return nullptr; +} +const void* getDaalRightSingularMatrix(void* svd) { + try { + auto psvd = static_cast(svd); + return static_cast(&psvd->getRightSingularMatrix()); + } CATCH_DAAL + return nullptr; +} +const void* getDaalLeftSingularMatrix(void* svd) { + try { + auto psvd = static_cast(svd); + return static_cast(&psvd->getLeftSingularMatrix()); + } CATCH_DAAL + return nullptr; +} +// Regression +void* CreateDaalRidgeRegression(void* input) { + try { + auto in = static_cast *>(input); + return new(std::nothrow) RidgeRegression(*in); + } CATCH_DAAL + return nullptr; } +void DeleteDaalRidgeRegression(void* regression) { + delete static_cast(regression); +} +void TrainDaalRidgeRegression(void *regression) { + try { + auto ridgeRegression = static_cast(regression); + ridgeRegression->train(); + } CATCH_DAAL +} +void PredictDaalRidgeRegression(void* regression, void* input) { + try { + RidgeRegression* reg = static_cast(regression); + auto in = static_cast *>(input); + reg->predict(*in); + } CATCH_DAAL +} +const void* GetDaalRidgeRegressionBeta(void* regression) { + try { + RidgeRegression* reg = static_cast(regression); + return ®->getBeta(); + } CATCH_DAAL + return nullptr; +} +const void* GetDaalRidgeRegressionPredictionData(void* regression) { + try { + RidgeRegression* reg = static_cast(regression); + return ®->getPredictionData(); + } CATCH_DAAL + return nullptr; +} +// Linear Regression +void* CreateDaalLinearRegression(void* input) { + try { + IInput* in = static_cast *>(input); + return new(std::nothrow) LinearRegression(*in); + } CATCH_DAAL + return nullptr; +} +void DeleteDaalLinearRegression(void* regression) { + delete static_cast(regression); +} +void TrainDaalLinearRegression(void *regression) { + try { + auto ridgeRegression = static_cast(regression); + ridgeRegression->train(); + } CATCH_DAAL +} +void PredictDaalLinearRegression(void* regression, void* input) { + try { + LinearRegression* reg = static_cast(regression); + auto in = static_cast *>(input); + reg->predict(*in); + } CATCH_DAAL +} +const void* GetDaalLinearRegressionBeta(void* regression) { + try { + LinearRegression* reg = static_cast(regression); + return ®->getBeta(); + } CATCH_DAAL + return nullptr; +} +const void* GetDaalLinearRegressionPredictionData(void* regression) { + try { + LinearRegression* reg = static_cast(regression); + return ®->getPredictionData(); + } CATCH_DAAL + return nullptr; +} + diff --git a/src/cpu/daal/h2o4gpu_daal_c.h b/src/cpu/daal/h2o4gpu_daal_c.h index c8689a476..9ee2e6f98 100644 --- a/src/cpu/daal/h2o4gpu_daal_c.h +++ b/src/cpu/daal/h2o4gpu_daal_c.h @@ -14,14 +14,8 @@ #include #include #include -#include "utils/defines.h" -#include "iinput.hpp" -#include "svd.hpp" #include - -using namespace H2O4GPU::DAAL; - #define CATCH_DAAL \ catch(const std::runtime_error &e) { \ fprintf(stderr, "Runtime error: %s in %s at line %d", e.what(), __FILE__, __LINE__); \ @@ -31,57 +25,43 @@ using namespace H2O4GPU::DAAL; fprintf(stderr, "Unknown failure occurred. Possible memory corruption."); \ } -//input to enter algorithms -void *CreateDaalInput(double*, size_t, size_t); -void *CreateDaalInput(double*, size_t, size_t, double*, size_t, size_t); -void *CreateDaalInput(const std::string&); -void *CreateDaalInput(const std::string&, size_t size_t) -void *CreateDaalInput(const std::string&, size_t, size_t, size_t, size_t); - -template -void DeleteDaalInput(void* input) { - delete static_cast *>(input); -} -template -void printDaalInput(void* input, const char* msg) { - try { - auto di = static_cast *>(input); - auto pnt = di->getNumericTable(); - printNumericTable(pnt, msg); - } CATCH_DAAL -} -template -void* getDaalNT(const void* daalInput) { - try { - auto di = static_cast *>(daalInput); - auto pnt = di->getNumericTable(); - return pnt; - } CATCH_DAAL - return nullptr; -} -void printDaalNT(const void*,const char*, size_t, size_t); +#ifdef __cplusplus +extern "C" { +#endif +//input to enter algorithms +void *CreateDaalInput(float*, size_t, size_t); +void *CreateDaalInputFeaturesDependent(float*, size_t, size_t, float*, size_t, size_t); +void *CreateDaalInputFile(const std::string&); +void *CreateDaalInputFileFeaturesDependent(const std::string&, size_t size_t); +void DeleteDaalInput(void* input); +void PrintDaalNumericTablePtr(float* input, const char* msg="", size_t rows=0, size_t cols = 0); // Singular value decomposition algorithm -void* CreateDaalSVD(const void*); -void* CreateDaalSVD(double*, size_t, size_t); +void* CreateDaalSVD(void*); void DeleteDaalSVD(void*); -void fitDaalSVD(const void*); -const void* getDaalSigmas(const void*) const; -const void* getDaalRightSingularMatrix(const void*) const; -const void* getDaalLeftSingularMatrix(const void*) const; - +void fitDaalSVD(void*); +const void* getDaalSVDSigma(void*); +const void* getDaalRightSingularMatrix(void*); +const void* getDaalLeftSingularMatrix(void*); // Regression -void* CreateDaalRegression(const void*); -void* CreateDaalRegression(); -void DeleteDaalRegression(); -const void* train(Regression::Type); -const void* predict(Regression::Type, const void*); -void setInput(const void*); - - - - - +void* CreateDaalRidgeRegression(void*); +void DeleteDaalRidgeRegression(); +void TrainDaalRidgeRegression(void *regression); +void PredictDaalRidgeRegression(void* regression, void* input); +const void* GetDaalRidgeRegressionBeta(void* regression); +const void* GetDaalRidgeRegressionPredictionData(void* regression); +// Linear Regression + +void* CreateDaalLinearRegression(void*); +void DeleteDaalLinearRegression(void*); +void TrainDaalLinearRegression(void *regression); +void PredictDaalLinearRegression(void* regression, void* input); +const void* GetDaalLinearRegressionBeta(void* regression); +const void* GetDaalLinearRegressionPredictionData(void* regression); + +#ifdef __cplusplus +} +#endif #endif /* SRC_CPU_DAAL_H2O4GPU_DAAL_C_H_ */ diff --git a/src/cpu/daal/iinput.cpp b/src/cpu/daal/iinput.cpp new file mode 100644 index 000000000..cf34de3b4 --- /dev/null +++ b/src/cpu/daal/iinput.cpp @@ -0,0 +1,179 @@ +/* + * iinput.cpp + * + * Created on: Apr 4, 2018 + * Author: monika + */ + + +#include "iinput.h" + +#include +#include +#include +#include +#include +#include +#include +#include "utils/service.h" +#include "utils/defines.h" +#include "utils/debug.hpp" + +using namespace H2O4GPU::DAAL; +using namespace daal::services; +using namespace daal::data_management; + +/* Base print class */ +void PrintTable::print(const NumericTablePtr& input, const std::string& msg, size_t rows, size_t cols) const { + printNumericTable(input, msg.c_str(), rows, cols); +} + +void PrintTable::print(NumericTable& input, const std::string& msg, size_t rows, size_t cols) { + printNumericTable(input, msg.c_str(), rows, cols); +} + +/* generic IInput interface */ +template +const typename IInput::NTPtr& IInput::getNumericTable() const { + return this->_inputData; +} +const NumericTablePtr& IInput::getNumericTable() const { + return this->_inputData; +} +template +const typename IInput::NTPtr& IInput::getFeaturesTable() const { + return this->_featuresData; +} +const NumericTablePtr& IInput::getFeaturesTable() const { + return this->_featuresData; +} +template +const typename IInput::NTPtr& IInput::getDependentTable() const { + return this->_dependentData; +} +const NumericTablePtr& IInput::getDependentTable() const { + return this->_dependentData; +} +/* HomogenousDaalData specialized */ +template +HomogenousDaalData::HomogenousDaalData(Input* inputArray, size_t m_dim, size_t n_dim) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + using NTPointer = typename HomogenousDaalData::NTPtr; + this->_inputData = + NTPointer(new Matrix(m_dim, n_dim, inputArray)); +} +template +HomogenousDaalData::HomogenousDaalData(const Input* inputArray, size_t m_dim, size_t n_dim) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + using NTPointer = typename HomogenousDaalData::NTPtr; + auto input = const_cast(inputArray); + this->_inputData = + NTPointer(new Matrix(m_dim, n_dim, input)); +} +template +HomogenousDaalData::HomogenousDaalData(Input* features, size_t m_features, size_t n_features, + Input* dependentData, size_t m_data, size_t n_data) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + using NTPointer = typename HomogenousDaalData::NTPtr; + this->_featuresData = + NTPointer(new Matrix(m_features, n_features, features)); + this->_dependentData = + NTPointer(new Matrix(m_data, n_data, dependentData)); + //this->_inputData = NTPointer(new MergedNumericTable(this->_featuresData, this->_dependentData)); +} +template +HomogenousDaalData::HomogenousDaalData(const Input* features, size_t m_features, size_t n_features, + const Input* dependentData, size_t m_data, size_t n_data) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + using NTPointer = typename HomogenousDaalData::NTPtr; + this->_featuresData = + NTPointer(new Matrix(m_features, n_features, const_cast(features))); + this->_dependentData = + NTPointer(new Matrix(m_data, n_data, const_cast(dependentData))); + //this->_inputData = NTPointer(new MergedNumericTable(this->_featuresData, this->_dependentData)); +} +template +HomogenousDaalData::HomogenousDaalData(std::vector& input, size_t m_dim, size_t n_dim) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + auto inputArray = input.data(); + using NTPointer = typename HomogenousDaalData::NTPtr; + this->_inputData = + NTPointer(new Matrix(m_dim, n_dim, inputArray)); +} +template +HomogenousDaalData::HomogenousDaalData(std::vector& features, size_t m_features, size_t n_features, + std::vector& dependentData, size_t m_data, size_t n_data) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + using NTPointer = typename HomogenousDaalData::NTPtr; + this->_featuresData = + NTPointer(new Matrix(m_features, n_features, features.data())); + this->_dependentData = + NTPointer(new Matrix(m_data, n_data, dependentData.data())); +} +template +HomogenousDaalData::HomogenousDaalData(const std::vector& input, size_t m_dim, size_t n_dim) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + auto inputArray = const_cast *>(&input)->data(); + using NTPointer = typename HomogenousDaalData::NTPtr; + this->_featuresData = + NTPointer(new Matrix(m_dim, n_dim, inputArray)); +} +template +HomogenousDaalData::HomogenousDaalData(const std::vector& features, size_t m_features, size_t n_features, + const std::vector& dependentData, size_t m_data, size_t n_data) { + if (!std::is_arithmetic::value) + throw "Input type is not arithmetic!"; + using NTPointer = typename HomogenousDaalData::NTPtr; + auto featuresArray = const_cast *>(&features)->data(); + auto dependentDataArray = const_cast *>(&dependentData)->data(); + this->_featuresData = + NTPointer(new Matrix(m_features, n_features, featuresArray)); + this->_dependentData = + NTPointer(new Matrix(m_data, n_data, dependentDataArray)); +} +/* IInput interface accepting CSV files or arrays */ +HomogenousDaalData::HomogenousDaalData(const std::string& filename) { + if (!this->fileExists(filename)) + throw "Input file doesn't exist!"; + if ("csv" != this->getFileExt(filename)) + throw "Input file isn't in csv file format!"; + FileDataSource dataSource(filename, + DataSource::doAllocateNumericTable, + DataSource::doDictionaryFromContext); + // retrieve data from the input file + dataSource.loadDataBlock(); + this->_inputData = dataSource.getNumericTable(); +} +HomogenousDaalData::HomogenousDaalData(const std::string & filename, size_t features, size_t dependentVariables) +{ + if (!this->fileExists(filename)) + throw "Input file doesn't exist!"; + if ("csv" != this->getFileExt(filename)) + throw "Input file isn't in csv file format!"; + FileDataSource dataSource(filename, + DataSource::doAllocateNumericTable, + DataSource::doDictionaryFromContext); + + this->_featuresData = NumericTablePtr( + new HomogenNumericTable<>(features, 0, NumericTable::doNotAllocate)); + this->_dependentData = NumericTablePtr( + new HomogenNumericTable<>(dependentVariables, 0, NumericTable::doNotAllocate)); + this->_inputData = NumericTablePtr( + new MergedNumericTable(this->_featuresData, this->_dependentData)); + + dataSource.loadDataBlock(this->_inputData.get()); +} + + +template class IInput; +template class IInput; +template class HomogenousDaalData; +template class HomogenousDaalData; +template class HomogenousDaalData; diff --git a/src/cpu/daal/iinput.h b/src/cpu/daal/iinput.h new file mode 100644 index 000000000..86c6d4425 --- /dev/null +++ b/src/cpu/daal/iinput.h @@ -0,0 +1,105 @@ +/* + * iinput.h + * + * Created on: Apr 4, 2018 + * Author: monika + */ + +#ifndef CPU_DAAL_IINPUT_H_ +#define CPU_DAAL_IINPUT_H_ + +#include +#include +#include +#include +#include +//#include "utils/service.h" +#include "utils/defines.h" + +namespace H2O4GPU { +namespace DAAL { + +typedef float FLOAT_TYPE; + +using namespace daal; +using namespace daal::data_management; +using namespace daal::services; + +/* Base class to print numeric table */ +class PUBLIC PrintTable { +public: + virtual void print(const NumericTablePtr& input, const std::string& ="", size_t =0, size_t =0) const; + virtual void print(NumericTable& input, const std::string& ="", size_t =0, size_t =0); + virtual ~PrintTable() {} +}; + +/* array (numpy) input */ +template +class PUBLIC IInput : public PrintTable { +public: + typedef SharedPtr > NTPtr; +protected: + NTPtr _inputData; + NTPtr _featuresData; + NTPtr _dependentData; +public: + const NTPtr& getNumericTable() const; + const NTPtr& getFeaturesTable() const; + const NTPtr& getDependentTable() const; +}; + +template<> +class PUBLIC IInput : public PrintTable { +public: + typedef NumericTablePtr NTPtr; +protected: + NTPtr _inputData; + NTPtr _featuresData; + NTPtr _dependentData; +public: + const NTPtr& getNumericTable() const; + const NTPtr& getFeaturesTable() const; + const NTPtr& getDependentTable() const; +}; + +template +class PUBLIC HomogenousDaalData : public IInput +{ +public: + typedef Input Type; + HomogenousDaalData(Input* inputArray, size_t m_dim, size_t n_dim); + HomogenousDaalData(const Input* inputArray, size_t m_dim, size_t n_dim); + HomogenousDaalData(Input* features, size_t m_features, size_t n_features, + Input* dependentData, size_t m_data, size_t n_data); + HomogenousDaalData(const Input* features, size_t m_features, size_t n_features, + const Input* dependentData, size_t m_data, size_t n_data); + HomogenousDaalData(std::vector& input, size_t m_dim, size_t n_dim); + HomogenousDaalData(std::vector&, size_t, size_t, std::vector&, size_t, size_t); + HomogenousDaalData(const std::vector&, size_t, size_t); + HomogenousDaalData(const std::vector&, size_t, size_t, const std::vector&, size_t, size_t); +}; + +/* IInput interface accepting CSV files or arrays */ +template<> +class PUBLIC HomogenousDaalData : public IInput +{ +public: + typedef std::string Type; + HomogenousDaalData(const std::string& filename); + HomogenousDaalData(const std::string & filename, size_t features, size_t dependentVariables); + inline bool fileExists(const std::string & filename) { + struct stat buffer; + return (stat (filename.c_str(), &buffer) == 0); + } + inline std::string getFileExt(const std::string &filename) { + size_t i = filename.rfind('.', filename.length()); + if (i != std::string::npos) { + return(filename.substr(i+1, filename.length() - i)); + } + return(""); + } +}; +}} // H2O4GPU::DAAL + + +#endif /* CPU_DAAL_IINPUT_H_ */ diff --git a/src/cpu/daal/iinput.hpp b/src/cpu/daal/iinput.hpp deleted file mode 100644 index 589d16c73..000000000 --- a/src/cpu/daal/iinput.hpp +++ /dev/null @@ -1,141 +0,0 @@ -/*! - * Copyright 2017 H2O.ai, Inc. - * License Apache License Version 2.0 (see LICENSE for details) - * purpose: Interface for entering input to DAAL library - * from raw pointers and csv file to NumericTable (needed for DAAL) calculation - * copy ctor and eq. operator based on default behavior (shared_ptr points to the same object) - */ - -#ifndef SRC_CPU_DAAL_INCLUDE_IINPUT_HPP_ -#define SRC_CPU_DAAL_INCLUDE_IINPUT_HPP_ - -#include -#include // for primitive data types -#include -#include -#include -#include "./utils/defines.h" - -namespace H2O4GPU { -namespace DAAL { - -using namespace daal; -using namespace daal::services; -using namespace daal::data_management; - -/* print out base class */ -class PrintNumericTable { -protected: - void print(const NumericTablePtr& input, const std::string& msg) const { - printNumericTable(input, msg); - } -}; - -/* IInput interface */ -template -class IInput { -protected: - NumericTablePtr _inputData; - NumericTablePtr _featuresData; - NumericTablePtr _dependentData; -public: - virtual const SharedPtr& getNumericTable() const { - return this->_inputData; - } - virtual const NumericTablePtr& getFeaturesTable() const { - return this->_featuresData; - } - virtual const NumericTablePtr& getDependentTable() const { - return this->_dependentData; - } - - virtual ~IInput() {} -}; - -/* HomogenousDaalData implementation for primitive data type: int, float, double */ -template -class HomogenousDaalData : public IInput -{ -public: - HomogenousDaalData(Input* inputArray, size_t m_dim, size_t n_dim) { - if (!std::is_arithmetic::value) - throw "Input data is not artithmetic type!"; - this->_inputData = SharedPtr(new Matrix(m_dim, n_dim, inputArray)); - this->_featuresData = this->_inputData; - } - HomogenousDaalData(Input* features, size_t m_features, size_t n_features, - Input* dependentVariables, size_t m_dependent, size_t n_dependent) { - if (!std::is_arithmetic::value) - throw "Input data is not artithmetic type!"; - this->_featuresData = NumericTablePtr(new Matrix(m_features, n_features)); - this->_dependentDAta = NuemricTablePtr(new Matrix(m_dependent, n_dependent)); - } - virtual ~HomogenousDaalData() {} -}; - -/* HomogenousDaalData implementation for csv file input */ -template <> -class HomogenousDaalData : public IInput -{ -public: - HomogenousDaalData(const std::string &filename) { - if (!this->fileExists(filename)) - throw "Input file doesn't exist!"; - if ("csv" != this->getFileExt(filename)) - throw "Input file isn't csv file format!"; - FileDataSource dataSource(fileName, - DataSource::doAllocateNumericTable, - DataSource::doDictionaryFromContext); - // retrieve data from the input file - dataSource.loadDataBlock(); - this->_featuresData = this->_inputData = dataSource.getNumericTable(); - } - - HomogenousDaalData(const std::string & filename, size_t features, size_t dependentVariables) { - if (!this->fileExists(filename)) - throw "Input file doesn't exist!"; - if ("csv" != this->getFileExt(filename)) - throw "Input file isn't csv file format!"; - FileDataSource dataSource(fileName, - DataSource::doAllocateNumericTable, - DataSource::doDictionaryFromContext); - this->_featuresData = - new HomogenNumericTable<>(features, 0, NumericTable::doNotAllocate); - this->_dependentData = - new HomogenNumericTable<>(dependentVariables, 0, NumericTable::doNotAllocate); - NumericTablePtr mergedData(new MergedNumericTable(featuresData, dependentData)); - dataSource.loadDataBlock(mergedData.get()); - this->_featuresData = this->_inputData = dataSource.getNumericTable(); - } - - inline bool fileExists(const std::string &filename) { - struct stat buffer; - return (stat (filename.c_str(), &buffer) == 0); - } - - inline std::string getFileExt(const std::string &filename) { - size_t i = filename.rfind('.', filename.length()); - if (i != std::string::npos) { - return(filename.substr(i+1, filename.length() - i)); - } - return(""); - } -}; - -/* convert numericTable to raw pointer -> memory released in daal */ -template -const Output* getRawOutput(const NumericTablePtr& nt) { - rows = (rows == 0) ? (*nt.get()).getNumberOfRows() : rows; - BlockDescriptor block; - (*nt.get()).getBlockOfRows(0, rows, readOnly, block); - const Output *array = block.getBlockPtr(); - (*nt.get()).releaseBlockOfRows(block); - return array; -} - - -} /* end of DAAL namespace */ -} /* end of H2O4GPU namespace */ - - -#endif /* SRC_CPU_DAAL_INCLUDE_IINPUT_HPP_ */ diff --git a/src/cpu/daal/linear_regression.cpp b/src/cpu/daal/linear_regression.cpp new file mode 100644 index 000000000..e5bbc8715 --- /dev/null +++ b/src/cpu/daal/linear_regression.cpp @@ -0,0 +1,60 @@ +/* + * linear_regression.cpp + * + * Created on: Apr 7, 2018 + * Author: monika + */ + + +#include +#include "linear_regression.h" + +using namespace H2O4GPU::DAAL; +using namespace daal; +using namespace daal::algorithms::linear_regression; + +template +LinearRegression::LinearRegression(IInput& input) { + this->_featuresData = std::move(input.getFeaturesTable()); + this->_dependentData = std::move(input.getDependentTable()); +} +template +LinearRegression::LinearRegression(const IInput& input) { + this->_featuresData = input.getFeaturesTable(); + this->_dependentData = input.getDependentTable(); +} +void LinearRegression::train() { + training::Batch<> algorithm; + algorithm.input.set(training::data, this->_featuresData); + algorithm.input.set(training::dependentVariables, this->_dependentData); + algorithm.compute(); + this->_trainingModel = algorithm.getResult(); +} +const typename LinearRegression::TrainingResultPtr& LinearRegression::getModel() const { + return this->_trainingModel; +} +template +void LinearRegression::predict(IInput& input) { + auto testData = std::move(input.getFeaturesTable()); + prediction::Batch<> algorithm; + algorithm.input.set(daal::algorithms::linear_regression::prediction::data, testData); + algorithm.input.set(daal::algorithms::linear_regression::prediction::model, this->_trainingModel->get(daal::algorithms::linear_regression::training::model)); + algorithm.compute(); + auto predictionResult = algorithm.getResult(); + this->_predictionData = predictionResult->get(prediction::prediction); +} +const NumericTablePtr& LinearRegression::getPredictionData() const { + return this->_predictionData; +} +const NumericTablePtr& LinearRegression::getBeta() { + this->_beta = this->_trainingModel->get(training::model)->getBeta(); + return this->_beta; +} +template LinearRegression::LinearRegression(IInput &); +template LinearRegression::LinearRegression(IInput &); +template LinearRegression::LinearRegression(const IInput &); +template LinearRegression::LinearRegression(const IInput &); +template void LinearRegression::predict(IInput&); +template void LinearRegression::predict(IInput&); + + diff --git a/src/cpu/daal/linear_regression.h b/src/cpu/daal/linear_regression.h new file mode 100644 index 000000000..b7cd71ffb --- /dev/null +++ b/src/cpu/daal/linear_regression.h @@ -0,0 +1,52 @@ +/* + * linear_regression.h + * + * Created on: Apr 7, 2018 + * Author: monika + */ + +#ifndef CPU_DAAL_LINEAR_REGRESSION_H_ +#define CPU_DAAL_LINEAR_REGRESSION_H_ + +#include +#include "iinput.h" + +namespace H2O4GPU { +namespace DAAL { + +using namespace daal; +using namespace daal::algorithms::linear_regression; + +class PUBLIC LinearRegression { +public: + typedef daal::algorithms::linear_regression::training::ResultPtr TrainingResultPtr; + + template + LinearRegression(IInput& input); + template + LinearRegression(const IInput& input); + const inline NumericTablePtr& getTrainingFeatures() const { + return this->_featuresData; + } + const inline NumericTablePtr& getTrainingDependentData() const { + return this->_dependentData; + } + void train(); + const NumericTablePtr& getBeta(); + const TrainingResultPtr& getModel() const; + template + void predict(IInput& input); + const NumericTablePtr& getPredictionData() const; + +protected: + TrainingResultPtr _trainingModel; + NumericTablePtr _predictionData; + NumericTablePtr _beta; + NumericTablePtr _featuresData; + NumericTablePtr _dependentData; +}; + + +}} // H2O4GPU::DAAL namespace + +#endif /* CPU_DAAL_LINEAR_REGRESSION_H_ */ diff --git a/src/cpu/daal/linear_regression.hpp b/src/cpu/daal/linear_regression.hpp index 3ea0cfd8a..1f8675b82 100644 --- a/src/cpu/daal/linear_regression.hpp +++ b/src/cpu/daal/linear_regression.hpp @@ -12,8 +12,8 @@ #include // for size_t #include #include -#include "iinput.hpp" #include "./utils/defines.h" +#include "iinput.hpp_" #include "regression.hpp" namespace H2O4GPU { diff --git a/src/cpu/daal/regression.hpp b/src/cpu/daal/regression.hpp deleted file mode 100644 index 045797380..000000000 --- a/src/cpu/daal/regression.hpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * regression.hpp - * - * Created on: Apr 4, 2018 - * Author: monika - */ - -#ifndef SRC_CPU_DAAL_REGRESSION_HPP_ -#define SRC_CPU_DAAL_REGRESSION_HPP_ - -#include -#include // for size_t -#include -#include -#include "iinput.hpp" -#include "./utils/defines.h" - -namespace H2O4GPU { -namespace DAAL { - -using namespace daal; -using namespace daal::algorithms::regression; - -template -class Regression : public PrintNumericTable { -public: - enum Type { linear, ridge }; - typedef training::ResultPtr trainingResult; - typedef prediction::ResultPtr predictionResult; - virtual ~Regresion() {} - DLL_PUBLIC Regression() {} - DLL_PUBLIC Regression(const IInput& input) { - this->_featuresData = input.getFeaturesTable(); - this->_dependentData = input.getDependentTable(); - } - DLL_PUBLIC virtual void SetInput(IInput& input) { - this->_featuresData = std::move(input.getFeaturesTable()); - this->_dependentData = std::move(input.getDependentTable()); - } - DLL_PUBLIC virtual const trainingResult& train() = 0; - DLL_PUBLIC virtual const predictionResult& predict(const IInput& input) = 0; -protected: - NumericTablePtr _featuresData; - NumericTablePtr _dependentData; - NumericTablePtr _beta; - trainingResult _trainingResult; - predictionResult _predictionResult; -}; - - - -#endif /* SRC_CPU_DAAL_REGRESSION_HPP_ */ diff --git a/src/cpu/daal/ridge_regression.cpp b/src/cpu/daal/ridge_regression.cpp new file mode 100644 index 000000000..3ff6b691b --- /dev/null +++ b/src/cpu/daal/ridge_regression.cpp @@ -0,0 +1,56 @@ +/* + * ridge_regression.cpp + * + * Created on: Apr 7, 2018 + * Author: monika + */ + +#include +#include "ridge_regression.h" + +using namespace H2O4GPU::DAAL; + +template +RidgeRegression::RidgeRegression(IInput& input) { + this->_featuresData = std::move(input.getFeaturesTable()); + this->_dependentData = std::move(input.getDependentTable()); +} +template +RidgeRegression::RidgeRegression(const IInput& input) { + this->_featuresData = input.getFeaturesTable(); + this->_dependentData = input.getDependentTable(); +} +void RidgeRegression::train() { + training::Batch<> algorithm; + algorithm.input.set(training::data, this->_featuresData); + algorithm.input.set(training::dependentVariables, this->_dependentData); + algorithm.compute(); + this->_trainingModel = algorithm.getResult(); +} +const NumericTablePtr& RidgeRegression::getBeta() { + this->_beta = this->_trainingModel->get(training::model)->getBeta(); + return this->_beta; +} +const typename RidgeRegression::TrainingResultPtr& RidgeRegression::getModel() const { + return this->_trainingModel; +} +template +void RidgeRegression::predict(IInput& input) { + auto testData = std::move(input.getFeaturesTable()); + prediction::Batch<> algorithm; + algorithm.input.set(prediction::data, testData); + algorithm.input.set(prediction::model, this->_trainingModel->get(training::model)); + algorithm.compute(); + auto predictionResult = algorithm.getResult(); + this->_predictionData = predictionResult->get(prediction::prediction); +} +const NumericTablePtr& RidgeRegression::getPredictionData() const { + return this->_predictionData; +} + +template RidgeRegression::RidgeRegression(IInput &); +template RidgeRegression::RidgeRegression(IInput &); +template RidgeRegression::RidgeRegression(const IInput &); +template RidgeRegression::RidgeRegression(const IInput &); +template void RidgeRegression::predict(IInput&); +template void RidgeRegression::predict(IInput&); diff --git a/src/cpu/daal/ridge_regression.h b/src/cpu/daal/ridge_regression.h new file mode 100644 index 000000000..ba6aaa81e --- /dev/null +++ b/src/cpu/daal/ridge_regression.h @@ -0,0 +1,53 @@ +/* + * ridge_regression.h + * + * Created on: Apr 7, 2018 + * Author: monika + */ + +#ifndef CPU_DAAL_RIDGE_REGRESSION_H_ +#define CPU_DAAL_RIDGE_REGRESSION_H_ + +#include +#include +#include +#include "utils/defines.h" +#include "iinput.h" + +namespace H2O4GPU { +namespace DAAL { + +using namespace daal; +using namespace daal::algorithms::ridge_regression; + +class PUBLIC RidgeRegression { +public: + typedef daal::algorithms::ridge_regression::training::ResultPtr TrainingResultPtr; + + template + RidgeRegression(IInput& input); + template + RidgeRegression(const IInput& input); + const inline NumericTablePtr& getTrainingFeatures() const { + return this->_featuresData; + } + const inline NumericTablePtr& getTrainingDependentData() const { + return this->_dependentData; + } + void train(); + const NumericTablePtr& getBeta(); + const TrainingResultPtr& getModel() const; + template + void predict(IInput& input); + const NumericTablePtr& getPredictionData() const; + +protected: + TrainingResultPtr _trainingModel; + NumericTablePtr _predictionData; + NumericTablePtr _beta; + NumericTablePtr _featuresData; + NumericTablePtr _dependentData; +}; + +}} // H2O4GPU::DAAL namespace +#endif /* CPU_DAAL_RIDGE_REGRESSION_H_ */ diff --git a/src/cpu/daal/ridge_regression.hpp b/src/cpu/daal/ridge_regression.hpp deleted file mode 100644 index 48ad44172..000000000 --- a/src/cpu/daal/ridge_regression.hpp +++ /dev/null @@ -1,55 +0,0 @@ -/* - * regression.hpp - * - * Created on: Apr 4, 2018 - * Author: monika - */ - -#ifndef SRC_CPU_DAAL_RIDGE_REGRESSION_HPP_ -#define SRC_CPU_DAAL_RIDGE_REGRESSION_HPP_ - -#include -#include // for size_t -#include -#include -#include "iinput.hpp" -#include "./utils/defines.h" - -namespace H2O4GPU { -namespace DAAL { - -using namespace daal; -using namespace daal::algorithms::ridge_regression; - -template -class RidgeRegression : public Regression { -public: - DLL_PUBLIC virtual const trainingResult& train() { - training::Batch algorithm; - algorithm.input.set(training::data, this->_featuresData); - algorithm.input.set(training::dependentVariables, this->_dependentData); - // compute - algorithm.compute(); - this->_trainingResult = algorithm.getResult(); - return this->_trainingResult; - } - - DLL_PUBLIC const NumericTablePtr& getBeta() const { - this->_trainingResult->get(training::model)->getBeta(); - return this->_trainingResult; - } - DLL_PUBLIC virtual const predictionResult& predict(IInput& input) { - NumericTablePtr testData = std::move(input.getFeaturesTable()); - prediction::Batch<> algorithm; - algorithm.input.set(prediction::data, testData); - algorithm.input.set(prediction::model, this->_trainingResult->get(training::model)); - // compute - algorithm.compute(); - predictionResult result = algorithm.getResult(); - return result->get(prediction::prediction); - } -}; - - - -#endif /* SRC_CPU_DAAL_RIDGE_REGRESSION_HPP_ */ diff --git a/src/cpu/daal/svd.cpp b/src/cpu/daal/svd.cpp new file mode 100644 index 000000000..24e6be43d --- /dev/null +++ b/src/cpu/daal/svd.cpp @@ -0,0 +1,44 @@ +/* + * svd.cpp + * + * Created on: Apr 7, 2018 + * Author: monika + */ +#include +#include "svd.h" + +using namespace H2O4GPU::DAAL; + +template +SVD::SVD(const IInput& input) { + this->_origData = input.getNumericTable(); +} +template +SVD::SVD(IInput& input) { + this->_origData = std::move(input.getNumericTable()); +} +void SVD::fit() { + svd::Batch<> algorithm; + algorithm.input.set(svd::data, this->_origData); + algorithm.compute(); + this->_result = algorithm.getResult(); +} + +const typename SVD::resultPtr& SVD::getResult() const { + return this->_result; +} +const NumericTablePtr& SVD::getSingularValues() { + this->_singularValues = this->_result->get(svd::singularValues); + return this->_singularValues; +} +const NumericTablePtr& SVD::getLeftSingularMatrix() { + this->_leftSingularMatrix = this->_result->get(svd::leftSingularMatrix); + return this->_leftSingularMatrix; +} +const NumericTablePtr& SVD::getRightSingularMatrix() { + this->_rightSingularMatrix = this->_result->get(svd::rightSingularMatrix); + return this->_rightSingularMatrix; +} + +template SVD::SVD(const IInput&); +template SVD::SVD(const IInput&); diff --git a/src/cpu/daal/svd.h b/src/cpu/daal/svd.h new file mode 100644 index 000000000..5febabf8c --- /dev/null +++ b/src/cpu/daal/svd.h @@ -0,0 +1,51 @@ +/* + * svd.h + * + * Created on: Apr 7, 2018 + * Author: monika + */ + +#ifndef CPU_DAAL_SVD_H_ +#define CPU_DAAL_SVD_H_ + +#include +#include +#include "utils/defines.h" +#include "iinput.h" + +namespace H2O4GPU { +namespace DAAL { + +typedef float FLOAT_TYPE; + +using namespace daal; +using namespace daal::algorithms; +using namespace daal::data_management; + +class PUBLIC SVD { +public: + typedef svd::ResultPtr resultPtr; + + template + SVD(const IInput& input); + template + SVD(IInput& input); + void fit(); + const resultPtr& getResult() const; + const NumericTablePtr& getSingularValues(); + const NumericTablePtr& getRightSingularMatrix(); + const NumericTablePtr& getLeftSingularMatrix(); + +protected: + NumericTablePtr _origData; + resultPtr _result; + NumericTablePtr _singularValues; + NumericTablePtr _rightSingularMatrix; + NumericTablePtr _leftSingularMatrix; +}; + +}} // H2O4GPU::DAAL namespace + + + +#endif /* CPU_DAAL_SVD_H_ */ diff --git a/src/cpu/daal/svd.hpp b/src/cpu/daal/svd.hpp deleted file mode 100644 index 6d64271c6..000000000 --- a/src/cpu/daal/svd.hpp +++ /dev/null @@ -1,70 +0,0 @@ -/*! - * Copyright 2017 H2O.ai, Inc. - * License Apache License Version 2.0 (see LICENSE for details) - */ - -#ifndef SRC_CPU_DAAL_INCLUDE_SVD_H_ -#define SRC_CPU_DAAL_INCLUDE_SVD_H_ - -#include -#include // for size_t -#include -#include "iinput.hpp" -#include "./utils/defines.h" - -namespace H2O4GPU { -namespace DAAL { - -using namespace daal; -using namespace daal::algorithms; - -class SVD { -public: - typedef svd::ResultPtr resultPtr; - - SharedPtr _origData; - resultPtr _result; - NumericTablePtr _singularValues; - NumericTablePtr _rightSingularMatrix; - NumericTablePtr _leftSingularMatrix; - - template - DLL_PUBLIC SVD(const IInput& input) { - this->_origData = input.getNumericTable(); - } - - template - DLL_PUBLIC SVD(Input* inputArray, size_t m_dim, size_t n_dim) { - auto table_nt = std::make_shared >(inputArray, m_dim, n_dim); - this->_origData = table_nt->getNumericTable(); - } - - DLL_PUBLIC void fit() { - svd::Batch<> algorithm; - algorithm.input.set(svd::data, this->_origData); - - // Compute SVD - algorithm.compute(); - this->_result = algorithm.getResult(); - } - - DLL_PUBLIC const NumericTablePtr& getSingularValues() const - { - return this->_result->get(svd::singularValues); - } - - DLL_PUBLIC const NumericTablePtr& getRightSingularMatrix() const - { - return this->_result->get(svd::rightSingularMatrix); - } - - DLL_PUBLIC const NumericTablePtr& getLeftSingularMatrix() const - { - return this->_result->get(svd::leftSingularMatrix); - } -}; - -} /* end of namespace DAAL -} /* end of namespace H2O4GPU - -#endif /* SRC_CPU_DAAL_INCLUDE_SVD_H_ */ diff --git a/src/cpu/daal/utils/defines.h b/src/cpu/daal/utils/defines.h index c90863aa2..59f6396d0 100644 --- a/src/cpu/daal/utils/defines.h +++ b/src/cpu/daal/utils/defines.h @@ -12,23 +12,33 @@ #include #include -#ifdef __cplusplus -# define DLLEXPORT extern "C" __declspec(dllexport) +#if defined _WIN32 || defined __CYGWIN__ + #ifdef BUILDING_DLL + #ifdef __GNUC__ + #define PUBLIC __attribute__ ((dllexport)) + #else + #define PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax. + #endif + #else + #ifdef __GNUC__ + #define PUBLIC __attribute__ ((dllimport)) + #else + #define PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax. + #endif + #endif + #define LOCAL #else -# define DLLEXPORT -#endif - -#if __GNUC__ >= 4 - #define DLL_PUBLIC __attribute__ ((visibility ("default"))) - #define DLL_LOCAL __attribute__ ((visibility ("hidden"))) -#else - #define DLL_PUBLIC - #define DLL_LOCAL + #if __GNUC__ >= 4 + #define PUBLIC __attribute__ ((visibility ("default"))) + #define LOCAL __attribute__ ((visibility ("hidden"))) + #else + #define PUBLIC + #define LOCAL + #endif #endif namespace H2O4GPU { namespace DAAL { -#define double DEF_TYPE; template struct Either { diff --git a/src/daal.mk b/src/daal.mk deleted file mode 100644 index 2026e274e..000000000 --- a/src/daal.mk +++ /dev/null @@ -1,14 +0,0 @@ -location = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) -WHERE_ART_THOU := $(location) -$(info ** -> $(WHERE_ART_THOU)) -$(info ** ------------------------------------------------------------------ **) - -DAAL_LIBS := -ldaal_core -ldaal_sequential -lpthread -lm - -DAAL_INCLUDE := -I$HOME/daal/include - -DAAL_HDR = \ - cpu/daal/include/debug.hpp \ - cpu/daal/include/iinput.hpp - -DAAL_DEF = DDAAL_DEF From 9450c0662b83c2fc442b96deac07ea17416c8764 Mon Sep 17 00:00:00 2001 From: Monika vos Mueller Date: Sun, 8 Apr 2018 13:53:26 -0700 Subject: [PATCH 5/8] Makefile for local installation --- src/cpu/daal/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cpu/daal/Makefile b/src/cpu/daal/Makefile index 22d2cdbf4..664416b57 100644 --- a/src/cpu/daal/Makefile +++ b/src/cpu/daal/Makefile @@ -82,7 +82,7 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)%.cpp clean: @echo "--- Cleaning Daal ---" - rm -rf $(TARGET) $(OBJ_FILES) + rm -rf $(TARGET) $(OBJ_FILES) $(OBJ_DIR) endif From 08278d8dff28512ea86f66d6eee21b0c695b7b52 Mon Sep 17 00:00:00 2001 From: Monika vos Mueller Date: Tue, 10 Apr 2018 12:28:43 -0700 Subject: [PATCH 6/8] merged with master --- Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2801d18cb..9cd175b07 100644 --- a/Makefile +++ b/Makefile @@ -442,8 +442,13 @@ testxgboost: # liblightgbm (assumes one installs lightgdm yourself or run make l # install daal install_daal_x86_64: - @echo "----- Install Daal Python library -----" + @echo "--- Install Daal Python library -----" bash scripts/daal/install_daal_locally.sh + @echo "----- Compile Daal C-library -----" + cd src/cpu/daal && $(MAKE) +clean_daal: + @echo "--- Cleaning Daal library ---" + cd src/cpu/daal && $(MAKE) clean ################ From 8fb9a7ec59885146907a9a1d2f4b2eb7f460b995 Mon Sep 17 00:00:00 2001 From: mdymczyk Date: Wed, 11 Apr 2018 21:21:45 +0900 Subject: [PATCH 7/8] Move DAAL sources to a separate directory. Move building of the object to CMake. --- CMakeLists.txt | 35 ++++++++- Makefile | 4 +- src/cpu/daal/Makefile | 90 ----------------------- src/{cpu => }/daal/h2o4gpu_daal_c.cpp | 0 src/{cpu => }/daal/h2o4gpu_daal_c.h | 0 src/{cpu => }/daal/iinput.cpp | 0 src/{cpu => }/daal/iinput.h | 0 src/{cpu => }/daal/linear_regression.cpp | 0 src/{cpu => }/daal/linear_regression.h | 0 src/{cpu => }/daal/linear_regression.hpp | 0 src/{cpu => }/daal/ridge_regression.cpp | 0 src/{cpu => }/daal/ridge_regression.h | 0 src/{cpu => }/daal/svd.cpp | 0 src/{cpu => }/daal/svd.h | 0 src/{cpu => }/daal/utils/debug.hpp | 0 src/{cpu => }/daal/utils/defines.h | 0 src/{cpu => }/daal/utils/error_handling.h | 0 src/{cpu => }/daal/utils/service.h | 0 18 files changed, 34 insertions(+), 95 deletions(-) delete mode 100644 src/cpu/daal/Makefile rename src/{cpu => }/daal/h2o4gpu_daal_c.cpp (100%) rename src/{cpu => }/daal/h2o4gpu_daal_c.h (100%) rename src/{cpu => }/daal/iinput.cpp (100%) rename src/{cpu => }/daal/iinput.h (100%) rename src/{cpu => }/daal/linear_regression.cpp (100%) rename src/{cpu => }/daal/linear_regression.h (100%) rename src/{cpu => }/daal/linear_regression.hpp (100%) rename src/{cpu => }/daal/ridge_regression.cpp (100%) rename src/{cpu => }/daal/ridge_regression.h (100%) rename src/{cpu => }/daal/svd.cpp (100%) rename src/{cpu => }/daal/svd.h (100%) rename src/{cpu => }/daal/utils/debug.hpp (100%) rename src/{cpu => }/daal/utils/defines.h (100%) rename src/{cpu => }/daal/utils/error_handling.h (100%) rename src/{cpu => }/daal/utils/service.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 95cca67a1..5f9e8d260 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,8 +43,7 @@ FILE(GLOB_RECURSE COMMON_SOURCES src/common/*.cpp src/common/*.h src/interface_c/*.cpp - src/interface_c/*.h - ) + src/interface_c/*.h) INCLUDE_DIRECTORIES( src/include @@ -59,13 +58,43 @@ TARGET_INCLUDE_DIRECTORIES (commonh2o4gpu PUBLIC ) #============= BUILD COMMON CPU/GPU CODE +#============= DAAL +EXECUTE_PROCESS(COMMAND ldconfig -p | grep daal_core + OUTPUT_VARIABLE DAAL + OUTPUT_STRIP_TRAILING_WHITESPACE) + +if(${DAAL}) + MESSAGE(STATUS "Building with DAAL support.") + + FILE(GLOB_RECURSE DAAL_SOURCES + src/daal/*.cpp + src/daal/*.h) + + ADD_LIBRARY(daalh2o4gpu OBJECT ${DAAL_SOURCES}) + + TARGET_INCLUDE_DIRECTORIES (daalh2o4gpu PUBLIC + $(HOME)/daal/include + ) + + TARGET_LINK_LIBRARIES(daalh2o4gpu + daal_core + daal_sequential + pthread + m) + + SET(daal_obj $) +else() + MESSAGE(STATUS "Building without DAAL support. Please install DAAL first.") +endif() +#============= DAAL + #============= BUILD CPU LIBRARY FILE(GLOB_RECURSE CPU_SOURCES src/cpu/*.cpp src/cpu/*.h ) -ADD_LIBRARY(cpuh2o4gpu STATIC ${CPU_SOURCES} $) +ADD_LIBRARY(cpuh2o4gpu STATIC ${CPU_SOURCES} $ ${daal_obj}) TARGET_LINK_LIBRARIES(cpuh2o4gpu ${BLAS_LIBRARIES}) #============= BUILD CPU LIBRARY diff --git a/Makefile b/Makefile index 9cd175b07..911fca6b0 100644 --- a/Makefile +++ b/Makefile @@ -445,10 +445,10 @@ install_daal_x86_64: @echo "--- Install Daal Python library -----" bash scripts/daal/install_daal_locally.sh @echo "----- Compile Daal C-library -----" - cd src/cpu/daal && $(MAKE) + cd src/daal && $(MAKE) clean_daal: @echo "--- Cleaning Daal library ---" - cd src/cpu/daal && $(MAKE) clean + cd src/daal && $(MAKE) clean ################ diff --git a/src/cpu/daal/Makefile b/src/cpu/daal/Makefile deleted file mode 100644 index 664416b57..000000000 --- a/src/cpu/daal/Makefile +++ /dev/null @@ -1,90 +0,0 @@ -location = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) -WHERE_ART_THOU := $(location) -$(info ** -> $(WHERE_ART_THOU)) -$(info ** ------------------------------------------------------------------ **) - -DAAL := $(shell ldconfig -p | grep daal_core) - -ifndef DAAL -$(warning ** NO DAAL found. running without DAAL library.) -else -$(info ** DAAL librar installed on your system.) -endif - -# detect Operating System -CFLAGS += -c -OSFLAG := -ifeq ($(OS),Windows_NT) - OSFLAG += -D WIN32 - ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) - OSFLAG += -D AMD64 - endif - ifeq ($(PROCESSOR_ARCHITECTURE),x86) - OSFLAG += -D IA32 - endif - TARGET = libh2o4gpu_daal.dll - CFLAGS += -O2 -std=c++11 -stdlib=libc++ -else - UNAME_S := $(shell uname -s) - ifeq ($(UNAME_S),Linux) - OSFLAG += -D LINUX - ifndef CC - export CC = gcc - endif - ifndef CXX - export CXX = g++ - endif - endif - ifeq ($(UNAME_S),Darwin) - OSFLAG += -D OSX - ifndef CC - export CC = $(if $(shell which clang), clang, gcc) - endif - ifndef CXX - export CXX = $(if $(shell which clang++), clang++, g++) - endif - endif - UNAME_P := $(shell uname -p) - ifeq ($(UNAME_P),x86_64) - OSFLAG += -D AMD64 - endif - ifneq ($(filter %86,$(UNAME_P)),) - OSFLAG += -D IA32 - endif - ifneq ($(filter arm%,$(UNAME_P)),) - OSFLAG += -D ARM - endif - TARGET = libh2o4gpu_daal.so - CFLAGS += -O2 -std=gnu++11 -fPIC -endif - - -PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) - -SRC_DIR := $(PROJECT_ROOT) -OBJ_DIR := $(PROJECT_DIR)obj -$(shell mkdir -p $(OBJ_DIR)) -SRC_FILES := $(wildcard $(SRC_DIR)*.cpp) -OBJ_FILES := $(patsubst $(SRC_DIR)%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES)) - -DAALINCLUDE := -I$(HOME)/daal/include -DAALLIB := -ldaal_core -ldaal_sequential -lpthread -lm - -ifdef DAAL -all: $(TARGET) - -$(TARGET): $(OBJ_FILES) - @echo "--- Linking Daal ---" - $(CXX) -shared $(DAALLIB) -o $@ $^ - -$(OBJ_DIR)/%.o: $(SRC_DIR)%.cpp - $(CXX) $(CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(DAALINCLUDE) -o $@ $< $(OSFLAGS) - -clean: - @echo "--- Cleaning Daal ---" - rm -rf $(TARGET) $(OBJ_FILES) $(OBJ_DIR) - -endif - - - diff --git a/src/cpu/daal/h2o4gpu_daal_c.cpp b/src/daal/h2o4gpu_daal_c.cpp similarity index 100% rename from src/cpu/daal/h2o4gpu_daal_c.cpp rename to src/daal/h2o4gpu_daal_c.cpp diff --git a/src/cpu/daal/h2o4gpu_daal_c.h b/src/daal/h2o4gpu_daal_c.h similarity index 100% rename from src/cpu/daal/h2o4gpu_daal_c.h rename to src/daal/h2o4gpu_daal_c.h diff --git a/src/cpu/daal/iinput.cpp b/src/daal/iinput.cpp similarity index 100% rename from src/cpu/daal/iinput.cpp rename to src/daal/iinput.cpp diff --git a/src/cpu/daal/iinput.h b/src/daal/iinput.h similarity index 100% rename from src/cpu/daal/iinput.h rename to src/daal/iinput.h diff --git a/src/cpu/daal/linear_regression.cpp b/src/daal/linear_regression.cpp similarity index 100% rename from src/cpu/daal/linear_regression.cpp rename to src/daal/linear_regression.cpp diff --git a/src/cpu/daal/linear_regression.h b/src/daal/linear_regression.h similarity index 100% rename from src/cpu/daal/linear_regression.h rename to src/daal/linear_regression.h diff --git a/src/cpu/daal/linear_regression.hpp b/src/daal/linear_regression.hpp similarity index 100% rename from src/cpu/daal/linear_regression.hpp rename to src/daal/linear_regression.hpp diff --git a/src/cpu/daal/ridge_regression.cpp b/src/daal/ridge_regression.cpp similarity index 100% rename from src/cpu/daal/ridge_regression.cpp rename to src/daal/ridge_regression.cpp diff --git a/src/cpu/daal/ridge_regression.h b/src/daal/ridge_regression.h similarity index 100% rename from src/cpu/daal/ridge_regression.h rename to src/daal/ridge_regression.h diff --git a/src/cpu/daal/svd.cpp b/src/daal/svd.cpp similarity index 100% rename from src/cpu/daal/svd.cpp rename to src/daal/svd.cpp diff --git a/src/cpu/daal/svd.h b/src/daal/svd.h similarity index 100% rename from src/cpu/daal/svd.h rename to src/daal/svd.h diff --git a/src/cpu/daal/utils/debug.hpp b/src/daal/utils/debug.hpp similarity index 100% rename from src/cpu/daal/utils/debug.hpp rename to src/daal/utils/debug.hpp diff --git a/src/cpu/daal/utils/defines.h b/src/daal/utils/defines.h similarity index 100% rename from src/cpu/daal/utils/defines.h rename to src/daal/utils/defines.h diff --git a/src/cpu/daal/utils/error_handling.h b/src/daal/utils/error_handling.h similarity index 100% rename from src/cpu/daal/utils/error_handling.h rename to src/daal/utils/error_handling.h diff --git a/src/cpu/daal/utils/service.h b/src/daal/utils/service.h similarity index 100% rename from src/cpu/daal/utils/service.h rename to src/daal/utils/service.h From 12f31f3a92cf5cf8f081f1a07a1e1259983ad368 Mon Sep 17 00:00:00 2001 From: Monika vos Mueller Date: Mon, 23 Apr 2018 04:28:14 -0700 Subject: [PATCH 8/8] Raw C-interface for java - template --- src/daal/Makefile | 91 +++++++++++++++++ src/daal/h2o4gpu_daal_c.cpp | 98 ++++++++++++------- src/daal/h2o4gpu_daal_c.h | 30 +++--- src/daal/iinput.cpp | 29 +++--- src/daal/iinput.h | 10 +- src/daal/linear_regression.cpp | 28 +++--- src/daal/linear_regression.h | 1 + src/daal/linear_regression.hpp | 54 ---------- src/daal/ridge_regression.cpp | 16 +-- src/daal/ridge_regression.h | 1 + src/daal/svd.cpp | 12 ++- src/daal/svd.h | 1 + .../h2o4gpu/daal_c_interface/__init__.py | 1 + .../h2o4gpu/daal_c_interface/daal_conf.py | 62 ++++++++++++ .../daal_linear_regression.py | 64 ++++++++++++ .../h2o4gpu/daal_c_interface/daal_nt.py | 85 ++++++++++++++++ .../daal_c_interface/daal_ridge_regression.py | 64 ++++++++++++ .../h2o4gpu/daal_c_interface/daal_svd.py | 54 ++++++++++ 18 files changed, 565 insertions(+), 136 deletions(-) create mode 100644 src/daal/Makefile delete mode 100644 src/daal/linear_regression.hpp create mode 100644 src/interface_py/h2o4gpu/daal_c_interface/__init__.py create mode 100644 src/interface_py/h2o4gpu/daal_c_interface/daal_conf.py create mode 100644 src/interface_py/h2o4gpu/daal_c_interface/daal_linear_regression.py create mode 100644 src/interface_py/h2o4gpu/daal_c_interface/daal_nt.py create mode 100644 src/interface_py/h2o4gpu/daal_c_interface/daal_ridge_regression.py create mode 100644 src/interface_py/h2o4gpu/daal_c_interface/daal_svd.py diff --git a/src/daal/Makefile b/src/daal/Makefile new file mode 100644 index 000000000..6bd7b2e0c --- /dev/null +++ b/src/daal/Makefile @@ -0,0 +1,91 @@ +location = $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) +WHERE_ART_THOU := $(location) +$(info ** -> $(WHERE_ART_THOU)) +$(info ** ------------------------------------------------------------------ **) + +DAAL := $(shell ldconfig -p | grep daal_core) + +ifndef DAAL +$(warning ** NO DAAL found. running without DAAL library.) +else +$(info ** DAAL library installed on your system.) +endif + +# detect Operating System +CFLAGS += -c +OSFLAG := +ifeq ($(OS),Windows_NT) + OSFLAG += -D WIN32 + ifeq ($(PROCESSOR_ARCHITECTURE),AMD64) + OSFLAG += -D AMD64 + endif + ifeq ($(PROCESSOR_ARCHITECTURE),x86) + OSFLAG += -D IA32 + endif + TARGET = libh2o4gpu_daal.dll + CFLAGS += -O2 -std=c++11 -stdlib=libc++ +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S),Linux) + OSFLAG += -D LINUX + ifndef CC + export CC = gcc + endif + ifndef CXX + export CXX = g++ + endif + endif + ifeq ($(UNAME_S),Darwin) + OSFLAG += -D OSX + ifndef CC + export CC = $(if $(shell which clang), clang, gcc) + endif + ifndef CXX + export CXX = $(if $(shell which clang++), clang++, g++) + endif + endif + UNAME_P := $(shell uname -p) + ifeq ($(UNAME_P),x86_64) + OSFLAG += -D AMD64 + endif + ifneq ($(filter %86,$(UNAME_P)),) + OSFLAG += -D IA32 + endif + ifneq ($(filter arm%,$(UNAME_P)),) + OSFLAG += -D ARM + endif + TARGET = libh2o4gpu_daal.so + CFLAGS += -O2 -std=c++11 -fPIC +endif + + +PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) + +SRC_DIR := $(PROJECT_ROOT) +OBJ_DIR := $(PROJECT_DIR)obj +$(shell mkdir -p $(OBJ_DIR)) +SRC_FILES := $(wildcard $(SRC_DIR)*.cpp) +OBJ_FILES := $(patsubst $(SRC_DIR)%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES)) + +DAALINCLUDE := -I$(HOME)/daal/include +DAALLIB := -ldaal_core -ldaal_sequential -lpthread -lm + +ifdef DAAL +all: $(TARGET) install + +install: + mv $(TARGET) $(PROJECT_ROOT)../interface_py/h2o4gpu/daal_c_interface/ + +$(TARGET): $(OBJ_FILES) + @echo "--- Linking Daal ---" + $(CXX) -shared $^ -o $@ $(DAALLIB) + +$(OBJ_DIR)/%.o: $(SRC_DIR)%.cpp + $(CXX) $(CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(DAALINCLUDE) -o $@ $< $(OSFLAGS) + +clean: + @echo "--- Cleaning Daal ---" + rm -rf $(PROJECT_ROOT)../interface_py/h2o4gpu/daal_c_interface/$(TARGET) $(OBJ_FILES) $(OBJ_DIR) + +endif + diff --git a/src/daal/h2o4gpu_daal_c.cpp b/src/daal/h2o4gpu_daal_c.cpp index b1dbee48d..7d38ef152 100644 --- a/src/daal/h2o4gpu_daal_c.cpp +++ b/src/daal/h2o4gpu_daal_c.cpp @@ -12,21 +12,31 @@ #include "h2o4gpu_daal_c.h" #include "iinput.h" #include "svd.h" -#include "ridge_regression.h" #include "linear_regression.h" +#include "ridge_regression.h" using namespace H2O4GPU::DAAL; - // Daal input -void* CreateDaalInput(float *pData, size_t m_dim, size_t n_dim) { - return new(std::nothrow) HomogenousDaalData(pData, m_dim, n_dim); +// Daal input +void* CreateDaalInput(double *pData, size_t m_dim, size_t n_dim) { + std::cout << "prvni pdata: "<< pData[0] << ", " << pData[1] << std::endl; + return new(std::nothrow) HomogenousDaalData(pData, m_dim, n_dim); } -void* CreateDaalInputFeaturesDependent(float* featuresData, size_t m_features, size_t n_features, - float* dependentData, size_t m_dependent, size_t n_dependent) { - return new(std::nothrow) HomogenousDaalData(featuresData, m_features, n_features, +void* CreateDaalInputFeaturesDependent(double* featuresData, size_t m_features, size_t n_features, + double* dependentData, size_t m_dependent, size_t n_dependent) { + return new(std::nothrow) HomogenousDaalData(featuresData, m_features, n_features, dependentData, m_dependent, n_dependent); } +void* GetFeaturesData(void* input) { + auto fd = static_cast *>(input); + return const_cast(static_cast(&fd->getFeaturesTable())); +} +void* GetDependentTable(void* input) { + auto dt = static_cast *>(input); + return const_cast(static_cast(&dt->getDependentTable())); +} + void* CreateDaalInputFile(const char* filename) { return new(std::nothrow) HomogenousDaalData(filename); } @@ -34,58 +44,74 @@ void* CreateDaalInputFileFeaturesDependent(const char* filename, size_t features return new(std::nothrow) HomogenousDaalData(filename, features, dependentVariables); } void DeleteDaalInput(void* input) { - delete static_cast *>(input); + delete static_cast *>(input); } -void PrintDaalNumericTablePtr(float *input,const char* msg, size_t rows, size_t cols) { +void PrintDaalNumericTablePtr(void *input,const char* msg, size_t rows, size_t cols) { + std::cout << "try to print\n"; try { - const NumericTablePtr* input = static_cast(input); + auto hdd = static_cast *>(input); PrintTable pt; - pt.print(*input, std::string(msg), rows, cols); + std::cout << "test pt\n"; + pt.print(hdd->getNumericTable(), std::string(msg), rows, cols); } CATCH_DAAL } + +void PrintNTP(void* input, const char* msg, size_t rows, size_t cols) { + std::cout << "PrintNTP\n"; + try { + NumericTablePtr* ntp = static_cast(input); + PrintTable pt; + pt.print(*ntp, std::string(msg), rows, cols); + } CATCH_DAAL +} + // Singular Value Decomposition void* CreateDaalSVD(void* input) { try { - IInput* in = static_cast *>(input); - return new(std::nothrow) SVD(*in); + IInput* in = static_cast *>(input); + return new(std::nothrow) SVD(in); } CATCH_DAAL return nullptr; } + void DeleteDaalSVD(void* input) { delete static_cast(input); } -void fitDaalSVD(void* svd) { +void FitDaalSVD(void* svd) { try { auto psvd = static_cast(svd); psvd->fit(); } CATCH_DAAL } -const void* getDaalSVDSigma(void* svd) { +void* GetDaalSVDSigma(void* svd) { try { auto psvd = static_cast(svd); - return static_cast(&psvd->getSingularValues()); + return const_cast + (static_cast(&psvd->getSingularValues())); } CATCH_DAAL return nullptr; } -const void* getDaalRightSingularMatrix(void* svd) { +const void* GetDaalRightSingularMatrix(void* svd) { try { auto psvd = static_cast(svd); return static_cast(&psvd->getRightSingularMatrix()); } CATCH_DAAL return nullptr; } -const void* getDaalLeftSingularMatrix(void* svd) { +const void* GetDaalLeftSingularMatrix(void* svd) { try { auto psvd = static_cast(svd); return static_cast(&psvd->getLeftSingularMatrix()); } CATCH_DAAL return nullptr; } + + // Regression void* CreateDaalRidgeRegression(void* input) { try { - auto in = static_cast *>(input); - return new(std::nothrow) RidgeRegression(*in); + auto in = static_cast *>(input); + return new(std::nothrow) RidgeRegression(in); } CATCH_DAAL return nullptr; } @@ -100,25 +126,33 @@ void TrainDaalRidgeRegression(void *regression) { } void PredictDaalRidgeRegression(void* regression, void* input) { try { - RidgeRegression* reg = static_cast(regression); + auto reg = static_cast(regression); auto in = static_cast *>(input); + std::cout << "predictdaalridgeregression ;\n"; reg->predict(*in); } CATCH_DAAL } const void* GetDaalRidgeRegressionBeta(void* regression) { try { - RidgeRegression* reg = static_cast(regression); - return ®->getBeta(); + auto reg = static_cast(regression); + + return static_cast(®->getBeta()); + //auto beta = static_cast(®->getBeta()); + + //typedef std::remove_cv::type unconst_type; + //return const_cast(beta); } CATCH_DAAL return nullptr; } -const void* GetDaalRidgeRegressionPredictionData(void* regression) { +void* GetDaalRidgeRegressionPredictionData(void* regression) { try { - RidgeRegression* reg = static_cast(regression); - return ®->getPredictionData(); + auto reg = static_cast(regression); + return const_cast( + static_cast(®->getPredictionData())); } CATCH_DAAL return nullptr; } + // Linear Regression void* CreateDaalLinearRegression(void* input) { try { @@ -132,30 +166,28 @@ void DeleteDaalLinearRegression(void* regression) { } void TrainDaalLinearRegression(void *regression) { try { - auto ridgeRegression = static_cast(regression); - ridgeRegression->train(); + auto reg = static_cast(regression); + reg->train(); } CATCH_DAAL } void PredictDaalLinearRegression(void* regression, void* input) { try { - LinearRegression* reg = static_cast(regression); + auto reg = static_cast(regression); auto in = static_cast *>(input); reg->predict(*in); } CATCH_DAAL } const void* GetDaalLinearRegressionBeta(void* regression) { try { - LinearRegression* reg = static_cast(regression); + auto reg = static_cast(regression); return ®->getBeta(); } CATCH_DAAL return nullptr; } const void* GetDaalLinearRegressionPredictionData(void* regression) { try { - LinearRegression* reg = static_cast(regression); + auto reg = static_cast(regression); return ®->getPredictionData(); } CATCH_DAAL return nullptr; } - - diff --git a/src/daal/h2o4gpu_daal_c.h b/src/daal/h2o4gpu_daal_c.h index 9ee2e6f98..12c3ee8d2 100644 --- a/src/daal/h2o4gpu_daal_c.h +++ b/src/daal/h2o4gpu_daal_c.h @@ -16,6 +16,7 @@ #include #include + #define CATCH_DAAL \ catch(const std::runtime_error &e) { \ fprintf(stderr, "Runtime error: %s in %s at line %d", e.what(), __FILE__, __LINE__); \ @@ -30,29 +31,34 @@ extern "C" { #endif //input to enter algorithms -void *CreateDaalInput(float*, size_t, size_t); -void *CreateDaalInputFeaturesDependent(float*, size_t, size_t, float*, size_t, size_t); -void *CreateDaalInputFile(const std::string&); -void *CreateDaalInputFileFeaturesDependent(const std::string&, size_t size_t); +void *CreateDaalInput(double*, size_t, size_t); + +void *CreateDaalInputFeaturesDependent(double*, size_t, size_t, double*, size_t, size_t); +void* GetFeaturesData(void* input); +void* GetDependentTable(void* input); +void *CreateDaalInputFile(const char*); +void *CreateDaalInputFileFeaturesDependent(const char* filename, size_t, size_t); void DeleteDaalInput(void* input); -void PrintDaalNumericTablePtr(float* input, const char* msg="", size_t rows=0, size_t cols = 0); +void PrintDaalNumericTablePtr(void* input, const char* msg="", size_t rows=0, size_t cols = 0); +void PrintNTP(void*, const char * msg="", size_t rows=0, size_t cols=0); // Singular value decomposition algorithm void* CreateDaalSVD(void*); void DeleteDaalSVD(void*); -void fitDaalSVD(void*); -const void* getDaalSVDSigma(void*); -const void* getDaalRightSingularMatrix(void*); -const void* getDaalLeftSingularMatrix(void*); +void FitDaalSVD(void*); +void* GetDaalSVDSigma(void*); +const void* GetDaalRightSingularMatrix(void*); +const void* GetDaalLeftSingularMatrix(void*); + // Regression void* CreateDaalRidgeRegression(void*); -void DeleteDaalRidgeRegression(); +void DeleteDaalRidgeRegression(void *); void TrainDaalRidgeRegression(void *regression); void PredictDaalRidgeRegression(void* regression, void* input); const void* GetDaalRidgeRegressionBeta(void* regression); -const void* GetDaalRidgeRegressionPredictionData(void* regression); -// Linear Regression +void* GetDaalRidgeRegressionPredictionData(void* regression); +// Linear Regression void* CreateDaalLinearRegression(void*); void DeleteDaalLinearRegression(void*); void TrainDaalLinearRegression(void *regression); diff --git a/src/daal/iinput.cpp b/src/daal/iinput.cpp index cf34de3b4..a367e09ba 100644 --- a/src/daal/iinput.cpp +++ b/src/daal/iinput.cpp @@ -23,13 +23,23 @@ using namespace H2O4GPU::DAAL; using namespace daal::services; using namespace daal::data_management; +void printNT(NumericTable& input, const std::string& msg, size_t rows, size_t cols) { + printNumericTable(input, msg.c_str(), rows, cols); +} + /* Base print class */ void PrintTable::print(const NumericTablePtr& input, const std::string& msg, size_t rows, size_t cols) const { + std::cout << "PrintTable::print test\n"; + std::cout << type_name()<()< const typename IInput::NTPtr& IInput::getNumericTable() const { return this->_inputData; } -const NumericTablePtr& IInput::getNumericTable() const { - return this->_inputData; -} + template const typename IInput::NTPtr& IInput::getFeaturesTable() const { return this->_featuresData; } -const NumericTablePtr& IInput::getFeaturesTable() const { - return this->_featuresData; -} + template const typename IInput::NTPtr& IInput::getDependentTable() const { return this->_dependentData; } -const NumericTablePtr& IInput::getDependentTable() const { - return this->_dependentData; -} + + /* HomogenousDaalData specialized */ template HomogenousDaalData::HomogenousDaalData(Input* inputArray, size_t m_dim, size_t n_dim) { if (!std::is_arithmetic::value) throw "Input type is not arithmetic!"; + //std::cout << type_name() << "decltype(INput)\n"; using NTPointer = typename HomogenousDaalData::NTPtr; this->_inputData = NTPointer(new Matrix(m_dim, n_dim, inputArray)); + auto m = SharedPtr(new Matrix(m_dim, n_dim, inputArray)); } template HomogenousDaalData::HomogenousDaalData(const Input* inputArray, size_t m_dim, size_t n_dim) { @@ -82,7 +89,6 @@ HomogenousDaalData::HomogenousDaalData(Input* features, size_t m_features NTPointer(new Matrix(m_features, n_features, features)); this->_dependentData = NTPointer(new Matrix(m_data, n_data, dependentData)); - //this->_inputData = NTPointer(new MergedNumericTable(this->_featuresData, this->_dependentData)); } template HomogenousDaalData::HomogenousDaalData(const Input* features, size_t m_features, size_t n_features, @@ -94,7 +100,6 @@ HomogenousDaalData::HomogenousDaalData(const Input* features, size_t m_fe NTPointer(new Matrix(m_features, n_features, const_cast(features))); this->_dependentData = NTPointer(new Matrix(m_data, n_data, const_cast(dependentData))); - //this->_inputData = NTPointer(new MergedNumericTable(this->_featuresData, this->_dependentData)); } template HomogenousDaalData::HomogenousDaalData(std::vector& input, size_t m_dim, size_t n_dim) { @@ -174,6 +179,8 @@ HomogenousDaalData::HomogenousDaalData(const std::string & filename template class IInput; template class IInput; +template class IInput; template class HomogenousDaalData; template class HomogenousDaalData; +template class HomogenousDaalData; template class HomogenousDaalData; diff --git a/src/daal/iinput.h b/src/daal/iinput.h index 86c6d4425..64b138bb6 100644 --- a/src/daal/iinput.h +++ b/src/daal/iinput.h @@ -26,6 +26,8 @@ using namespace daal::data_management; using namespace daal::services; /* Base class to print numeric table */ +void printNT(NumericTable& input, const std::string& = "", size_t rows=0, size_t cols=0); + class PUBLIC PrintTable { public: virtual void print(const NumericTablePtr& input, const std::string& ="", size_t =0, size_t =0) const; @@ -49,7 +51,7 @@ class PUBLIC IInput : public PrintTable { }; template<> -class PUBLIC IInput : public PrintTable { +class PUBLIC IInput : public PrintTable { public: typedef NumericTablePtr NTPtr; protected: @@ -57,9 +59,9 @@ class PUBLIC IInput : public PrintTable { NTPtr _featuresData; NTPtr _dependentData; public: - const NTPtr& getNumericTable() const; - const NTPtr& getFeaturesTable() const; - const NTPtr& getDependentTable() const; + const NTPtr& getNumericTable() const { return this->_inputData; } + const NTPtr& getFeaturesTable() const { return this->_featuresData; } + const NTPtr& getDependentTable() const { return this->_dependentData; } }; template diff --git a/src/daal/linear_regression.cpp b/src/daal/linear_regression.cpp index e5bbc8715..1011ad0ff 100644 --- a/src/daal/linear_regression.cpp +++ b/src/daal/linear_regression.cpp @@ -5,14 +5,15 @@ * Author: monika */ - #include #include "linear_regression.h" using namespace H2O4GPU::DAAL; -using namespace daal; -using namespace daal::algorithms::linear_regression; +LinearRegression::LinearRegression(IInput* input) { + this->_featuresData = std::move(input->getFeaturesTable()); + this->_dependentData = std::move(input->getDependentTable()); +} template LinearRegression::LinearRegression(IInput& input) { this->_featuresData = std::move(input.getFeaturesTable()); @@ -30,6 +31,10 @@ void LinearRegression::train() { algorithm.compute(); this->_trainingModel = algorithm.getResult(); } +const NumericTablePtr& LinearRegression::getBeta() { + this->_beta = this->_trainingModel->get(training::model)->getBeta(); + return this->_beta; +} const typename LinearRegression::TrainingResultPtr& LinearRegression::getModel() const { return this->_trainingModel; } @@ -46,15 +51,12 @@ void LinearRegression::predict(IInput& input) { const NumericTablePtr& LinearRegression::getPredictionData() const { return this->_predictionData; } -const NumericTablePtr& LinearRegression::getBeta() { - this->_beta = this->_trainingModel->get(training::model)->getBeta(); - return this->_beta; -} -template LinearRegression::LinearRegression(IInput &); -template LinearRegression::LinearRegression(IInput &); -template LinearRegression::LinearRegression(const IInput &); -template LinearRegression::LinearRegression(const IInput &); -template void LinearRegression::predict(IInput&); -template void LinearRegression::predict(IInput&); +template LinearRegression::LinearRegression(IInput &); +template LinearRegression::LinearRegression(IInput &); +template LinearRegression::LinearRegression(const IInput &); +template LinearRegression::LinearRegression(const IInput &); +template void LinearRegression::predict(IInput&); +template void LinearRegression::predict(IInput&); + diff --git a/src/daal/linear_regression.h b/src/daal/linear_regression.h index b7cd71ffb..58ddb253e 100644 --- a/src/daal/linear_regression.h +++ b/src/daal/linear_regression.h @@ -21,6 +21,7 @@ class PUBLIC LinearRegression { public: typedef daal::algorithms::linear_regression::training::ResultPtr TrainingResultPtr; + LinearRegression(IInput* input); template LinearRegression(IInput& input); template diff --git a/src/daal/linear_regression.hpp b/src/daal/linear_regression.hpp deleted file mode 100644 index 1f8675b82..000000000 --- a/src/daal/linear_regression.hpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * LinearRegression.hpp - * - * Created on: Apr 4, 2018 - * Author: monika - */ - -#ifndef SRC_CPU_DAAL_LINEAR_REGRESSION_HPP_ -#define SRC_CPU_DAAL_LINEAR_REGRESSION_HPP_ - -#include -#include // for size_t -#include -#include -#include "./utils/defines.h" -#include "iinput.hpp_" -#include "regression.hpp" - -namespace H2O4GPU { -namespace DAAL { - -using namespace daal; -using namespace daal::algorithms::linear_regression; - - -template -class LinearRegression : public Regression { -public: - DlL_PUBLIC virtual const trainingResult& train() { - training::Batch<> algorithm; - algorithm.input.set(training::data, this->_featuresData); - algorithm.input.set(training::dependentVariables, this->_dependentData); - // compute - algorithm.compute(); - this->_trainingResult = algorithm.getResult(); - return this->_trainingResult; - } - - DLL_PUBLIC virtual const predictionResult& predict(const IInput& test) { - NumericTablePtr testData = std::move(test.getFeaturesTable()); - predicton::Batch<> algorithm; - algorithm.input.set(prediction::data, testData); - algorithm.iput.set(prediction::model, this->_trainingResult->get(training::model)); - // compute - algorithm.compute(); - predictionResult result = algorithm.getResult(); - return result->get(prediction::prediction); - } -}; - -}} // H2O4GPU::DAAL - - -#endif /* SRC_CPU_DAAL_LINEAR_REGRESSION_HPP_ */ diff --git a/src/daal/ridge_regression.cpp b/src/daal/ridge_regression.cpp index 3ff6b691b..3f0b9ced5 100644 --- a/src/daal/ridge_regression.cpp +++ b/src/daal/ridge_regression.cpp @@ -10,6 +10,10 @@ using namespace H2O4GPU::DAAL; +RidgeRegression::RidgeRegression(IInput* input) { + this->_featuresData = std::move(input->getFeaturesTable()); + this->_dependentData = std::move(input->getDependentTable()); +} template RidgeRegression::RidgeRegression(IInput& input) { this->_featuresData = std::move(input.getFeaturesTable()); @@ -48,9 +52,9 @@ const NumericTablePtr& RidgeRegression::getPredictionData() const { return this->_predictionData; } -template RidgeRegression::RidgeRegression(IInput &); -template RidgeRegression::RidgeRegression(IInput &); -template RidgeRegression::RidgeRegression(const IInput &); -template RidgeRegression::RidgeRegression(const IInput &); -template void RidgeRegression::predict(IInput&); -template void RidgeRegression::predict(IInput&); +template RidgeRegression::RidgeRegression(IInput &); +template RidgeRegression::RidgeRegression(const IInput &); +template RidgeRegression::RidgeRegression(IInput &); +template RidgeRegression::RidgeRegression(const IInput &); +template void RidgeRegression::predict(IInput &); +template void RidgeRegression::predict(IInput &); diff --git a/src/daal/ridge_regression.h b/src/daal/ridge_regression.h index ba6aaa81e..c946dd2b2 100644 --- a/src/daal/ridge_regression.h +++ b/src/daal/ridge_regression.h @@ -24,6 +24,7 @@ class PUBLIC RidgeRegression { public: typedef daal::algorithms::ridge_regression::training::ResultPtr TrainingResultPtr; + RidgeRegression(IInput* input); template RidgeRegression(IInput& input); template diff --git a/src/daal/svd.cpp b/src/daal/svd.cpp index 24e6be43d..3f58ab902 100644 --- a/src/daal/svd.cpp +++ b/src/daal/svd.cpp @@ -9,12 +9,17 @@ using namespace H2O4GPU::DAAL; +SVD::SVD(IInput* input) { + this->_origData = input->getNumericTable(); +} template SVD::SVD(const IInput& input) { - this->_origData = input.getNumericTable(); + this->_origData = std::move(input.getNumericTable()); } template SVD::SVD(IInput& input) { + auto m = input.getNumericTable(); + input.print(m); this->_origData = std::move(input.getNumericTable()); } void SVD::fit() { @@ -40,5 +45,6 @@ const NumericTablePtr& SVD::getRightSingularMatrix() { return this->_rightSingularMatrix; } -template SVD::SVD(const IInput&); -template SVD::SVD(const IInput&); + +template SVD::SVD(const IInput&); +template SVD::SVD(IInput&); diff --git a/src/daal/svd.h b/src/daal/svd.h index 5febabf8c..09db83b86 100644 --- a/src/daal/svd.h +++ b/src/daal/svd.h @@ -26,6 +26,7 @@ class PUBLIC SVD { public: typedef svd::ResultPtr resultPtr; + SVD(IInput* input); template SVD(const IInput& input); template diff --git a/src/interface_py/h2o4gpu/daal_c_interface/__init__.py b/src/interface_py/h2o4gpu/daal_c_interface/__init__.py new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/src/interface_py/h2o4gpu/daal_c_interface/__init__.py @@ -0,0 +1 @@ + diff --git a/src/interface_py/h2o4gpu/daal_c_interface/daal_conf.py b/src/interface_py/h2o4gpu/daal_c_interface/daal_conf.py new file mode 100644 index 000000000..9a86fbd61 --- /dev/null +++ b/src/interface_py/h2o4gpu/daal_c_interface/daal_conf.py @@ -0,0 +1,62 @@ +from ctypes import cdll, POINTER, c_int, c_double, c_void_p, c_char_p +import os +c_int_p = POINTER(c_int) +c_double_p = POINTER(c_double) + + +realpath = os.path.dirname(__file__) +libpath = "{}/{}".format(realpath, 'libh2o4gpu_daal.so') +print("libpath: ", libpath) +daal_lib = cdll.LoadLibrary(libpath) + +daal_lib.CreateDaalInput.restype = c_void_p +daal_lib.CreateDaalInput.argtypes = [c_double_p, c_int, c_int] +daal_lib.CreateDaalInputFeaturesDependent.restype = c_void_p +daal_lib.CreateDaalInputFeaturesDependent.argtypes = [c_double_p, c_int, c_int, c_double_p, c_int, c_int] +daal_lib.GetFeaturesData.restype = c_void_p +daal_lib.GetFeaturesData.argtypes = [c_void_p] +daal_lib.GetDependentTable.restype = c_void_p +daal_lib.GetDependentTable.argtypes = [c_void_p] +daal_lib.DeleteDaalInput.argtypes = [c_void_p] +daal_lib.PrintDaalNumericTablePtr.argtypes = [c_void_p, c_char_p, c_int, c_int] +daal_lib.CreateDaalInputFile.restype = c_void_p +daal_lib.CreateDaalInputFile.argtypes = [c_char_p] +daal_lib.CreateDaalInputFileFeaturesDependent.restype = c_void_p +daal_lib.CreateDaalInputFileFeaturesDependent.argtypes = [c_char_p, c_int, c_int] +daal_lib.PrintNTP.argtypes = [c_void_p, c_char_p, c_int, c_int] +# SVD +daal_lib.CreateDaalSVD.restype = c_void_p +daal_lib.CreateDaalSVD.argtypes = [c_void_p] +daal_lib.DeleteDaalSVD.argtypes = [c_void_p] +daal_lib.FitDaalSVD.argtypes = [c_void_p] +daal_lib.GetDaalSVDSigma.restype = c_void_p +daal_lib.GetDaalSVDSigma.argtypes = [c_void_p] +daal_lib.GetDaalRightSingularMatrix.restype = c_void_p +daal_lib.GetDaalRightSingularMatrix.argtypes = [c_void_p] +daal_lib.GetDaalLeftSingularMatrix.restype = c_void_p +daal_lib.GetDaalLeftSingularMatrix.argtypes = [c_void_p] +# Ridge Rebression +daal_lib.CreateDaalRidgeRegression.restype = c_void_p +daal_lib.CreateDaalRidgeRegression.argtypes = [c_void_p] +daal_lib.DeleteDaalRidgeRegression.argtypes = [c_void_p] +daal_lib.TrainDaalRidgeRegression.argtypes = [c_void_p] +daal_lib.GetDaalRidgeRegressionBeta.restype = c_void_p +daal_lib.GetDaalRidgeRegressionBeta.argtypes = [c_void_p] +daal_lib.PredictDaalRidgeRegression.argtypes = [c_void_p, c_void_p] +daal_lib.GetDaalRidgeRegressionPredictionData.restype = c_void_p +daal_lib.GetDaalRidgeRegressionPredictionData.argtypes = [c_void_p] +# Linear Regression +daal_lib.CreateDaalLinearRegression.restype = c_void_p +daal_lib.CreateDaalLinearRegression.argtypes = [c_void_p] +daal_lib.DeleteDaalLinearRegression.argtypes = [c_void_p] +daal_lib.TrainDaalLinearRegression.argtypes = [c_void_p] +daal_lib.GetDaalLinearRegressionBeta.argtypes = [c_void_p] +daal_lib.GetDaalLinearRegressionBeta.restype = c_void_p +daal_lib.PredictDaalLinearRegression.argtypes = [c_void_p, c_void_p] +daal_lib.GetDaalLinearRegressionPredictionData.restype = c_void_p +daal_lib.GetDaalLinearRegressionPredictionData.argtypes = [c_void_p] + +def PrintNTP(NTP, msg = "", rows = 0, cols = 0): + daal_lib.PrintNTP(NTP, bytes(msg, "UTF-8"), rows, cols) + +__all__ = ['c_double_p','daal_lib','realpath', 'PrintNTP'] \ No newline at end of file diff --git a/src/interface_py/h2o4gpu/daal_c_interface/daal_linear_regression.py b/src/interface_py/h2o4gpu/daal_c_interface/daal_linear_regression.py new file mode 100644 index 000000000..3e5f9c812 --- /dev/null +++ b/src/interface_py/h2o4gpu/daal_c_interface/daal_linear_regression.py @@ -0,0 +1,64 @@ +import numpy as np +from daal_conf import (daal_lib,realpath,PrintNTP) +from daal_nt import DaalNTs + +class DaalLinearRegression(object): + def __init__(self, features, f_rows=0, f_columns=0, dependent=None, d_rows=0, d_columns=0): + if not dependent and features: + if isinstance(features, (DaalNTs)): + f_obj = features.obj + self.features = f_obj.getFeaturesData() + self.dependent = f_obj.getDependentData() + self.NT = features + self.obj = daal_lib.CreateDaalLinearRegression(f_obj) + elif isinstance(features, str): + print('handling traing file') + self.training_file = features + self.features = f_rows + self.dependent = f_columns + NT = DaalNTs(features, f_rows, f_columns) + self.NT = NT + self.obj = daal_lib.CreateDaalLinearRegression(NT.obj) + elif dependent is not None and features is not None: + if isinstance(dependent, (np.ndarray, np.generic)) and isinstance(features, (np.ndarray, np.generic)): + NTs = DaalNTs(features, f_rows, f_columns, dependent, d_rows, d_columns) + f_obj = NTs.obj + self.NT = NTs + self.obj = daal_lib.CreateDaalLinearRegression(f_obj) + else: + print("Unsupported constructor for numeric table!") + + def __del__(self): + return daal_lib.DeleteDaalLinearRegression(self.obj) + + def train(self): + daal_lib.TrainDaalLinearRegression(self.obj) + + def getBeta(self): + return daal_lib.GetDaalLinearRegressionBeta(self.obj) + + def predict(self, dependent_file, n_features, n_dependent): + if isinstance(dependent_file, str): + NT = DaalNTs(dependent_file, n_features, n_dependent) + daal_lib.PredictDaalLinearRegression(self.obj, NT.obj) + + def getPrediction(self): + return daal_lib.GetDaalLinearRegressionPredictionData(self.obj) + +if __name__ == '__main__': + print("Testing Linear Regression") + + training_data = '{}/../datasets/data/linear_regression_train.csv'.format(realpath) + testing_data = '{}/../datasets/data/linear_regression_test.csv'.format(realpath) + n_features = 10 + n_dependent = 2 + dlr = DaalLinearRegression(training_data, n_features, n_dependent) + dlr.train() + beta = dlr.getBeta() + print(beta) + PrintNTP(beta, "Calculated Beta") + dlr.predict(testing_data, n_features, n_dependent) + prediction_data = dlr.getPrediction() + PrintNTP(prediction_data, "Predicted Data") + + \ No newline at end of file diff --git a/src/interface_py/h2o4gpu/daal_c_interface/daal_nt.py b/src/interface_py/h2o4gpu/daal_c_interface/daal_nt.py new file mode 100644 index 000000000..46674214f --- /dev/null +++ b/src/interface_py/h2o4gpu/daal_c_interface/daal_nt.py @@ -0,0 +1,85 @@ +import numpy as np +from daal_conf import (c_double_p,daal_lib,realpath,PrintNTP) + +class DaalNT(object): + def __init__(self, array, rows=0, columns=0): + if isinstance(array, (np.ndarray, np.generic)): + p_array = array.ctypes.data_as(c_double_p) + self.array = array + self.rows = rows + self.columns = columns + self.obj = daal_lib.CreateDaalInput(p_array, rows, columns) + elif isinstance(array, str): + b_str = bytes(array, 'UTF-8') + self.obj = daal_lib.CreateDaalInputFile(b_str) + else: + print("Unsupported constructor for numeric table!") + + def __del__(self): + return daal_lib.DeleteDaalInput(self.obj) + + def print_NT(self, msg="", rows = 0, columns = 0): + daal_lib.PrintDaalNumericTablePtr(self.obj, bytes(msg, 'UTF-8'), rows, columns) + +# with features and dependent data +class DaalNTs(object): + def __init__(self, features, f_rows, f_cols, dependent=None, d_rows=0, d_cols=0): + if dependent is not None: + if isinstance(features, (np.ndarray, np.generic)) and isinstance(dependent, (np.ndarray, np.generic)): + self.features = features + self.dependent = dependent + self.features_rows = f_rows + self.features_cols = f_cols + self.dependent_rows = d_rows + self.dependent_cols = d_cols + p_features = features.ctypes.data_as(c_double_p) + p_deps = dependent.ctypes.data_as(c_double_p) + + self.obj = daal_lib.CreateDaalInputFeaturesDependent(p_features, f_rows, f_cols, p_deps, d_rows, d_cols) + else: + raise("Invalid DaalNTs constructor") + else: + if isinstance(features, str): + b_str = bytes(features, "UTF-8") + self.obj = daal_lib.CreateDaalInputFileFeaturesDependent(b_str, f_rows, f_cols) + + def __del__(self): + return daal_lib.DeleteDaalInput(self.obj) + + def getFeaturesData(self): + return daal_lib.GetFeaturesData(self.obj) + + def getDependentData(self): + return daal_lib.GetDependentTable(self.obj) + + +if __name__ == '__main__': + print("1 Testing DaalNT class:") + print("1.1 Testing Numeric Table Object") + array = np.array([[1.,2.,3.,],[4.,5.,6.],[7.,8.,9.]], dtype = np.double) + + NT = DaalNT(array, array.shape[0], array.shape[1]) + NT.print_NT("Numeric table object from numpy Array") + + filename = '{}/../datasets/data/svd.csv'.format(realpath) + print("parsed filename: {}".format(filename)) + NT_f = DaalNT(filename) + NT_f.print_NT("Numeric Table object from file", 10, 10) + + print("1.2 Testing Numeric Tables with Features and Dependent data") + array_d = np.array([[1.,1.5],[1.5,1.5],[2,2.5]], dtype = np.double) + + NT2 = DaalNTs(array, array.shape[0], array.shape[1], array_d, array_d.shape[0], array_d.shape[1]) + features = NT2.getFeaturesData() + PrintNTP(features, "features data") + dependent = NT2.getDependentData() + PrintNTP(dependent, "dependent data") + + print("1.3 Testing Numeric Tables with CSV file") + training_data = '{}/../datasets/data/linear_regression_train.csv'.format(realpath) + NT3 = DaalNTs(training_data, 10, 2) + PrintNTP(NT3.getDependentData(), "dependent data from csv - 2 cols") + PrintNTP(NT3.getFeaturesData(), "features data from csv - 10 cols") + + + \ No newline at end of file diff --git a/src/interface_py/h2o4gpu/daal_c_interface/daal_ridge_regression.py b/src/interface_py/h2o4gpu/daal_c_interface/daal_ridge_regression.py new file mode 100644 index 000000000..a233de507 --- /dev/null +++ b/src/interface_py/h2o4gpu/daal_c_interface/daal_ridge_regression.py @@ -0,0 +1,64 @@ +import numpy as np +from daal_conf import (daal_lib,realpath,PrintNTP) +from daal_nt import DaalNTs + +class DaalRidgeRegression(object): + def __init__(self, features, f_rows=0, f_columns=0, dependent=None, d_rows=0, d_columns=0): + if not dependent and features: + if isinstance(features, (DaalNTs)): + f_obj = features.obj + self.features = f_obj.getFeaturesData() + self.dependent = f_obj.getDependentData() + self.NT = features + self.obj = daal_lib.CreateDaalRidgeRegression(f_obj) + elif isinstance(features, str): + print('handling traing file') + self.training_file = features + self.features = f_rows + self.dependent = f_columns + NT = DaalNTs(features, f_rows, f_columns) + self.NT = NT + self.obj = daal_lib.CreateDaalRidgeRegression(NT.obj) + elif dependent is not None and features is not None: + if isinstance(dependent, (np.ndarray, np.generic)) and isinstance(features, (np.ndarray, np.generic)): + NTs = DaalNTs(features, f_rows, f_columns, dependent, d_rows, d_columns) + f_obj = NTs.obj + self.NT = NTs + self.obj = daal_lib.CreateDaalRidgeRegression(f_obj) + else: + print("Unsupported constructor for numeric table!") + + def __del__(self): + return daal_lib.DeleteDaalRidgeRegression(self.obj) + + def train(self): + daal_lib.TrainDaalRidgeRegression(self.obj) + + def getBeta(self): + return daal_lib.GetDaalRidgeRegressionBeta(self.obj) + + def predict(self, dependent_file, n_features, n_dependent): + if isinstance(dependent_file, str): + NT = DaalNTs(dependent_file, n_features, n_dependent) + daal_lib.PredictDaalRidgeRegression(self.obj, NT.obj) + + def getPrediction(self): + return daal_lib.GetDaalRidgeRegressionPredictionData(self.obj) + +if __name__ == '__main__': + print("Testing Ridge Regression") + + training_data = '{}/../datasets/data/linear_regression_train.csv'.format(realpath) + testing_data = '{}/../datasets/data/linear_regression_test.csv'.format(realpath) + n_features = 10 + n_dependent = 2 + drr = DaalRidgeRegression(training_data, n_features, n_dependent) + drr.train() + beta = drr.getBeta() + print(beta) + PrintNTP(beta, "Calculated Beta") + drr.predict(testing_data, n_features, n_dependent) + prediction_data = drr.getPrediction() + PrintNTP(prediction_data, "Predicted Data") + + \ No newline at end of file diff --git a/src/interface_py/h2o4gpu/daal_c_interface/daal_svd.py b/src/interface_py/h2o4gpu/daal_c_interface/daal_svd.py new file mode 100644 index 000000000..5a3c3ee6b --- /dev/null +++ b/src/interface_py/h2o4gpu/daal_c_interface/daal_svd.py @@ -0,0 +1,54 @@ +import numpy as np +from daal_conf import (daal_lib,realpath,PrintNTP) +from daal_nt import DaalNT + +class DaalSVD(object): + def __init__(self, array, rows = 0, columns = 0): + print("type array : ", type(array)) + if isinstance(array, (DaalNT)): + self.NT = array + self.obj = daal_lib.CreateDaalSVD(self.NT.obj) + elif isinstance(array, (np.ndarray, np.generic)): + print("numeric table is it SVD") + self.NT = DaalNT(array, rows, columns) + print("type of self.NT: ", type(self.NT)) + self.obj = daal_lib.CreateDaalSVD(self.NT.obj) + elif isinstance(array, str): + NT = DaalNT(array) + self.NT = NT + self.obj = daal_lib.CreateDaalSVD(self.NT.obj) + else: + print("Unsupported constructor for numeric table!") + + def __del__(self): + return daal_lib.DeleteDaalSVD(self.obj) + + def fit(self): + daal_lib.FitDaalSVD(self.obj) + + def getSigma(self): + return daal_lib.GetDaalSVDSigma(self.obj) + + def getRightSingularMatrix(self): + return daal_lib.GetDaalRightSingularMatrix(self.obj) + + def getLeftSingularMatrix(self): + return daal_lib.GetDaalLeftSingularMatrix(self.obj) + +if __name__ == '__main__': + print("Testing Numeric SVD") + + array = np.array([[1.,2.,3.,],[4.,5.,6.],[7.,8.,9.]], dtype= np.double) + SVD = DaalSVD(array, array.shape[0], array.shape[1]) + filename = '{}/../datasets/data/svd.csv'.format(realpath) + print("parsed filename: {}".format(filename)) + SVD_f = DaalSVD(filename) + SVD_f.fit() + sigmas = SVD_f.getSigma() + PrintNTP(sigmas, "Calculated Sigma") + right_singular_matrix = SVD_f.getRightSingularMatrix() + PrintNTP(right_singular_matrix, "Right Singular Matrix") + left_singular_matrix = SVD_f.getLeftSingularMatrix() + PrintNTP(left_singular_matrix, "Left Singular Matrix") + + \ No newline at end of file