Intel® oneAPI Data Analytics Library
Learn from community members on how to build compute-intensive applications that run efficiently on Intel® architecture.

Concerning calling DAAL from .Net

alexey_k_2
Beginner
514 Views

Good day!

I am experiencing problems calling daal methods exported by dll wrapping dall. Nothing unusual - C exported functions are called by Platform Invoke C# methods. Just do not show them. To hold space. I ll show them if needed. But i am sure i have many developed native dlls - i have well testd and strong C# wrappers so no problems with them.

Dll is builded with staticall lnking, no dll not found exceptions etc....

Obviuosly i run it under Visual Studio 2015 Enterpize and Windows 10 Enteprize. Would like to port to Azure...But cant to it without help and guidance.

1. When debug in mixed mode. When i switch my dll project to debug mode i cant compile and have following linker errors:

Error:    LNK2019    unresolved symbol:  _CrtDbgReportW в функции "public: bool __cdecl std::_Tree_const_iterator<class std::_Tree_val<struct std::_Tree_simple_types<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,struct std::pair<int,int> > > > >::operator==(class std::_Tree_const_iterator<class std::_Tree_val<struct std::_Tree_simple_types<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,struct std::pair<int,int> > > > > const &)const " (??8?$_Tree_const_iterator@V?$_Tree_val@U?$_Tree_simple_types@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@U?$pair@HH@2@@std@@@std@@@std@@@std@@QEBA_NAEBV01@@Z)    CPP_DAAL_Lib    E:\Projects2015\TF\CPP_DAAL_Lib\DAAL_SVM_OneClass.obj    1    

- Error:    LNK2001    unresolved symbol: "_CrtDbgReportW"    CPP_DAAL_Lib    E:\Projects2015\TF\CPP_DAAL_Lib\DAAL_SVM_OneClass_Callers.obj    1    

2. Disposing exported class object i have memory access violation error.  But no problems with creating svm class, kernel objects. Problems begin on disposing. So in facts problem is in Dispose_DaalSVMOneClass or TryDispose_DaalSVMOneClass. They are similar - second is just returning c# string with exception text.

If i do not use operator delete pObject - no error, but certainly object is not destroyed.I tried multithreaded and sequential version with same result. May be daal is using special methods and i need to perform some additional cleaning and freeing. Unfortunately didnt get how and cant debug as i told earlier.

All C++ examples are firing without any problem. 

So help is needed - how to debug and how to fire dll. Thanks in advance.

I have following C++ code.

DAAL_SVM_OneClass_Callers.h:

#include "DAAL_SVM_OneClass.cpp"
extern "C"
{
	__declspec (dllexport) DaalSVMOneClass* Create_DaalSVMOneClass
	(
		int featuresSize_,
		double gamma_
	);

	__declspec (dllexport) void Dispose_DaalSVMOneClass(DaalSVMOneClass* pObject);

	__declspec (dllexport) wchar_t* TryDispose_DaalSVMOneClass(DaalSVMOneClass* pObject);

	__declspec (dllexport) void TrainBatch_DaalSVMOneClass
	(
		DaalSVMOneClass* pObject,
		const double* samplesArray,
		const double* labelsArray,
		int M,
		int N
	);

	__declspec (dllexport) wchar_t* TryTrainBatch_DaalSVMOneClass
	(
		DaalSVMOneClass* pObject,
		const double* samplesArray,
		const double* labelsArray,
		int M,
		int N
	);

	__declspec (dllexport) double ComputeFunction_DaalSVMOneClass
	(
		DaalSVMOneClass* pObject,
		const double* x,
		int sizeOfX
	);
}

DAAL_SVM_OneClass_Callers.cpp:

#define WIN32_LEAN_AND_MEAN	
#include "DAAL_SVM_OneClass_Callers.h"

extern "C"
{
	__declspec (dllexport) DaalSVMOneClass* Create_DaalSVMOneClass
	(
		int featuresSize_,
		double gamma_
	)
	{
		return new DaalSVMOneClass(featuresSize_, gamma_);
	}

	__declspec (dllexport) void Dispose_DaalSVMOneClass(DaalSVMOneClass* pObject)
	{
		if (pObject != NULL)
		{
			delete pObject;
		}
	}

	__declspec (dllexport) wchar_t* TryDispose_DaalSVMOneClass(DaalSVMOneClass* pObject)
	{
		try
		{
			if (pObject != NULL)
			{
				delete pObject;
			}
		}
		catch (std::exception& ex)
		{
			return GetAllocatedWString(ex.what());
		}
		return GetAllocatedWString(DEFAULT_ALLOCATE_RESULT);
	}

	__declspec (dllexport) void TrainBatch_DaalSVMOneClass
	(
		DaalSVMOneClass* pObject,
		const double* samplesArray,
		const double* labelsArray,
		int M,
		int N
	)
	{
		if (pObject != NULL)
			pObject->TrainBatch(samplesArray, labelsArray, M, N);
	}

	__declspec (dllexport) wchar_t* TryTrainBatch_DaalSVMOneClass
	(
		DaalSVMOneClass* pObject,
		const double* samplesArray,
		const double* labelsArray,
		int M,
		int N
	)
	{
		if (pObject != NULL)
			return pObject->TryTrainBatch(samplesArray, labelsArray, M, N);
	}

	__declspec (dllexport) double ComputeFunction_DaalSVMOneClass
	(
		DaalSVMOneClass* pObject,
		const double* x,
		int sizeOfX
	)
	{
		if (pObject != NULL)
			return pObject->ComputeFunction(x, sizeOfX);
	}
}

And Daal_SVM_OneClass.cpp - no loading just trying to let constructor and destructor work (#include "StringInteropUtils.h" self explains - just char to wchar_t transformation and specific memory allocation to return pointer to C# string.. It is strong and well tested. no problems here):

#include "daal.h"

//#include "service.h"
#include "StringInteropUtils.h"

using namespace std;

using namespace daal;
using namespace daal::algorithms;

class DaalSVMOneClass
{
private:
	const int m_featuresSize;
	const double m_gamma;

	svm::training::Batch<> m_algorithm;
	const int c_cache_size = 600000000;
	services::SharedPtr<kernel_function::KernelIface> m_kernel;
	//kernel_function::linear::Batch<double, kernel_function::linear::fastCSR> m_kernelFunction;// = kernel_function::linear::Batch<double, kernel_function::linear::fastCSR>(); // не уверен по поводу fast csr
	
	//* Model object for the SVM algorithm */
	//services::SharedPtr<svm::training::Result> trainingResult;

	
public:
	DaalSVMOneClass(
		const int featuresSize_,
		const double gamma_
	) : // ? cache as parameter
		m_featuresSize(featuresSize_),
		m_gamma(gamma_),
		m_kernel(new kernel_function::linear::Batch<double, kernel_function::linear::fastCSR>())
	{
		
		//auto m_kernelFunction = kernel_function::linear::Batch<double, kernel_function::linear::fastCSR>();//  fast csr
		//m_kernel = services::SharedPtr<kernel_function::KernelIface>
		//	(
		//		//m_kernelFunction
		//		//new kernel_function::linear::Batch<double, kernel_function::linear::fastCSR>() // fast csr
		//	);
		
		m_algorithm = svm::training::Batch<>();
		m_algorithm.parameter.kernel = m_kernel;
		m_algorithm.parameter.cacheSize = c_cache_size;
	}

	~DaalSVMOneClass()
	{

	}

	void TrainBatch(const double* samplesArray, const double* labelsArray, const int M, const int N)
	{
		if (m_featuresSize == N)
		{
			
		}
	}

	wchar_t* TryTrainBatch(const double* samplesArray, const double* labelsArray, const int rows, const int cols)
	{
		try
		{
			TrainBatch(samplesArray, labelsArray, rows, cols);
		}
		catch (std::exception& ex)
		{
			return GetAllocatedWString(ex.what());
		}

		return GetAllocatedWString(DEFAULT_ALLOCATE_RESULT);
	}

	double ComputeFunction(const double* x, const int sizeOfX) const
	{
		if (m_featuresSize == sizeOfX) // should be number of rows
		{
			  // Just for test of Linking
		}
		/*else
			throw exception("m_featuresSize != size");*/
		return 1000.0;
	}
};

 

 

0 Kudos
4 Replies
alexey_k_2
Beginner
514 Views

So i have set break points and debug of c++ dll project.

During debug visual studio pointed me to line 107 of svm_train.h:

virtual ~Batch() {}

So if i understand right here is resources freeing overrided by inherited classes.

Would appreciate assistance how to free them.

Thanks in advance.

0 Kudos
Andrey_N_Intel
Employee
514 Views

Hi Alexey, we are analyzing your report and will let you know about results. Thanks, Andrey

0 Kudos
Daria_K_Intel
Employee
514 Views

Hi Alexey,

After our investigation we found that the problem is in incorrectly working copy constructor at line 43 of Daal_SVM_OneClass.cpp. 
m_algorithm = svm::training::Batch<>();

We will fix this issue in one of our future releases but in the meantime you can safely remove this line and your code should work.
Let us know if this helps you and if you have any other questions.

Thanks for your comment,
Daria

0 Kudos
alexey_k_2
Beginner
514 Views

I ve found this mistake too.

I tested multiple objects creation and destruction in .Net thread pool and seems no memory leaks.

Thank you for the answer.

0 Kudos
Reply