Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpp DAAL implementation - wrapper for raw pointer input #527

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
35 changes: 32 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 $<TARGET_OBJECTS:daalh2o4gpu>)
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} $<TARGET_OBJECTS:commonh2o4gpu>)
ADD_LIBRARY(cpuh2o4gpu STATIC ${CPU_SOURCES} $<TARGET_OBJECTS:commonh2o4gpu> ${daal_obj})
TARGET_LINK_LIBRARIES(cpuh2o4gpu ${BLAS_LIBRARIES})
#============= BUILD CPU LIBRARY

Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/daal && $(MAKE)
clean_daal:
@echo "--- Cleaning Daal library ---"
cd src/daal && $(MAKE) clean

################

Expand Down
91 changes: 91 additions & 0 deletions src/daal/Makefile
Original file line number Diff line number Diff line change
@@ -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

193 changes: 193 additions & 0 deletions src/daal/h2o4gpu_daal_c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* h2o4gpu_daal_c.cpp
*
* Created on: Apr 2, 2018
* Author: monika
*/
#include <string>
#include <iostream>
#include <exception>
#include <stdexcept>
#include <sstream>
#include "h2o4gpu_daal_c.h"
#include "iinput.h"
#include "svd.h"
#include "linear_regression.h"
#include "ridge_regression.h"

using namespace H2O4GPU::DAAL;

// 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<double>(pData, m_dim, n_dim);
}

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<double>(featuresData, m_features, n_features,
dependentData, m_dependent, n_dependent);
}
void* GetFeaturesData(void* input) {
auto fd = static_cast<IInput<double> *>(input);
return const_cast<void *>(static_cast<const void *>(&fd->getFeaturesTable()));
}
void* GetDependentTable(void* input) {
auto dt = static_cast<IInput<double> *>(input);
return const_cast<void *>(static_cast<const void *>(&dt->getDependentTable()));
}

void* CreateDaalInputFile(const char* filename) {
return new(std::nothrow) HomogenousDaalData<std::string>(filename);
}
void* CreateDaalInputFileFeaturesDependent(const char* filename, size_t features, size_t dependentVariables) {
return new(std::nothrow) HomogenousDaalData<std::string>(filename, features, dependentVariables);
}
void DeleteDaalInput(void* input) {
delete static_cast<IInput<double> *>(input);
}
void PrintDaalNumericTablePtr(void *input,const char* msg, size_t rows, size_t cols) {
std::cout << "try to print\n";
try {
auto hdd = static_cast<HomogenousDaalData<double> *>(input);
PrintTable pt;
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<NumericTablePtr *>(input);
PrintTable pt;
pt.print(*ntp, std::string(msg), rows, cols);
} CATCH_DAAL
}

// Singular Value Decomposition
void* CreateDaalSVD(void* input) {
try {
IInput<double>* in = static_cast<IInput<double> *>(input);
return new(std::nothrow) SVD(in);
} CATCH_DAAL
return nullptr;
}

void DeleteDaalSVD(void* input) {
delete static_cast<SVD *>(input);
}
void FitDaalSVD(void* svd) {
try {
auto psvd = static_cast<SVD* >(svd);
psvd->fit();
} CATCH_DAAL
}
void* GetDaalSVDSigma(void* svd) {
try {
auto psvd = static_cast<SVD* >(svd);
return const_cast<void *>
(static_cast<const void*>(&psvd->getSingularValues()));
} CATCH_DAAL
return nullptr;
}
const void* GetDaalRightSingularMatrix(void* svd) {
try {
auto psvd = static_cast<SVD* >(svd);
return static_cast<const void*>(&psvd->getRightSingularMatrix());
} CATCH_DAAL
return nullptr;
}
const void* GetDaalLeftSingularMatrix(void* svd) {
try {
auto psvd = static_cast<SVD* >(svd);
return static_cast<const void*>(&psvd->getLeftSingularMatrix());
} CATCH_DAAL
return nullptr;
}


// Regression
void* CreateDaalRidgeRegression(void* input) {
try {
auto in = static_cast<IInput<double> *>(input);
return new(std::nothrow) RidgeRegression(in);
} CATCH_DAAL
return nullptr;
}
void DeleteDaalRidgeRegression(void* regression) {
delete static_cast<RidgeRegression *>(regression);
}
void TrainDaalRidgeRegression(void *regression) {
try {
auto ridgeRegression = static_cast<RidgeRegression *>(regression);
ridgeRegression->train();
} CATCH_DAAL
}
void PredictDaalRidgeRegression(void* regression, void* input) {
try {
auto reg = static_cast<RidgeRegression* >(regression);
auto in = static_cast<IInput<float> *>(input);
std::cout << "predictdaalridgeregression ;\n";
reg->predict(*in);
} CATCH_DAAL
}
const void* GetDaalRidgeRegressionBeta(void* regression) {
try {
auto reg = static_cast<RidgeRegression *>(regression);

return static_cast<const void* >(&reg->getBeta());
//auto beta = static_cast<const void*>(&reg->getBeta());

//typedef std::remove_cv<decltype(beta)>::type unconst_type;
//return const_cast<void *>(beta);
} CATCH_DAAL
return nullptr;
}
void* GetDaalRidgeRegressionPredictionData(void* regression) {
try {
auto reg = static_cast<RidgeRegression *>(regression);
return const_cast<void *>(
static_cast<const void*>(&reg->getPredictionData()));
} CATCH_DAAL
return nullptr;
}

// Linear Regression
void* CreateDaalLinearRegression(void* input) {
try {
IInput<float>* in = static_cast<IInput<float> *>(input);
return new(std::nothrow) LinearRegression(*in);
} CATCH_DAAL
return nullptr;
}
void DeleteDaalLinearRegression(void* regression) {
delete static_cast<LinearRegression *>(regression);
}
void TrainDaalLinearRegression(void *regression) {
try {
auto reg = static_cast<LinearRegression *>(regression);
reg->train();
} CATCH_DAAL
}
void PredictDaalLinearRegression(void* regression, void* input) {
try {
auto reg = static_cast<LinearRegression *>(regression);
auto in = static_cast<IInput<float> *>(input);
reg->predict(*in);
} CATCH_DAAL
}
const void* GetDaalLinearRegressionBeta(void* regression) {
try {
auto reg = static_cast<LinearRegression *>(regression);
return &reg->getBeta();
} CATCH_DAAL
return nullptr;
}
const void* GetDaalLinearRegressionPredictionData(void* regression) {
try {
auto reg = static_cast<LinearRegression *>(regression);
return &reg->getPredictionData();
} CATCH_DAAL
return nullptr;
}
Loading