본 게시글은 "홍정모"님의 블로그 게시글을 참고해서 제작하였습니다.

(blog.naver.com/atelierjpro/220965203959)

 

Tensorflow를 C++에서 돌리고 싶을 때를 위해 미리 테스트를 했다.

Windows10 + Visual Studio 2017 (VC14.1)로 테스트를 했다.

Python 3.7 + tensorflow 2.2 버전이다.

boost는 1.73.0 버전에 python numpy 포함해서 직접 컴파일 하였다.

 

 

// ConsoleApplication1.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다.
//
#define BOOST_PYTHON_STATIC_LIB
#define BOOST_LIB_NAME "boost_numpy3"
#include <boost/config/auto_link.hpp>
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
#include <iostream>
#include <boost/thread.hpp>
#include <boost/python/extract.hpp>


namespace py = boost::python;
namespace np = boost::python::numpy;

using namespace boost::python;
using namespace boost::python::numpy;

#define BOOST_ERROR(msg) ( ::boost::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )


class MyTest
{
public:
	typedef boost::thread THREAD, *pTHREAD;

	int a_;

	MyTest(int a)
		: a_(a)
	{
	}

	int get()
	{
		return a_;
	}

	void set(int _a)
	{
		a_ = _a;
	}

	void threadjob(int tid)
	{
		std::cout << "Thread working! " << tid << std::endl;
	}

	void threadingtest()
	{
		const int num_threads_ = 10;
		pTHREAD *thread_list_;
		thread_list_ = new pTHREAD[num_threads_];
		for (int i = 0; i < num_threads_; i++) thread_list_[i] = 0;

		for (unsigned int thread_id = 0; thread_id < num_threads_; thread_id++)
			thread_list_[thread_id] = new THREAD(&MyTest::threadjob, this, thread_id);

		for (int i = 0; i < num_threads_; i++) thread_list_[i]->join();
	}

	void count()
	{
		a_++;

		std::cout << "A count " << std::endl;
	}

	void getArray(np::ndarray& np_array)
	{
		std::cout << "get array c++" << std::endl;

		double* ddd = (double*)np_array.get_data();

		std::cout << ddd[0] << std::endl;
		std::cout << ddd[1] << std::endl;
		std::cout << ddd[2] << std::endl;
	}

	void setArray(boost::python::numpy::ndarray data) {
		// Access a built-in type (an array)
		boost::python::numpy::ndarray a = data;
		// Need to <extract> array elements because their type is unknown
		std::cout << "First array item: " << extract<int>(a[0]) << std::endl;
	}

	void setPointer(double* ptr)
	{
		std::cout << "pointer " << ptr << std::endl;
	}

	void set_first_element(numpy::ndarray& y, float value)
	{
		y[0] = value;
	}

	void greet(boost::python::object& obj)
	{
		std::cout << "Greet C++" << std::endl;

		PyObject* pobj = obj.ptr();

		static Py_buffer pybuf;

		if (PyObject_GetBuffer(pobj, &pybuf, PyBUF_SIMPLE) != -1)
		{
			void *buf = pybuf.buf;
			double *p = (double*)buf;

			*p += 4.123;
			*(p + 1) += 5.12312;

			//PyBuffer_Release(&pybuf);
		}
	}

	void plusOne(np::ndarray& np_array)
	{
		std::cout << "get array c++" << std::endl;
		double* ddd = (double*)np_array.get_data();
		ddd[0] += 1.0;
	}
};


BOOST_PYTHON_MODULE(my_test)
{
	class_<MyTest>("MyTest", init<int>())
		.def("get", &MyTest::get)
		.def("threadingtest", &MyTest::threadingtest)
		.def("count", &MyTest::count)
		.def("setArray", &MyTest::setArray)
		.def("setPointer", &MyTest::setPointer)
		.def("set_first_element", &MyTest::set_first_element)
		.def("greet", &MyTest::greet)
		.def("getArray", &MyTest::getArray)
		.def("plusOne", &MyTest::plusOne)
		.add_property("value", &MyTest::get, &MyTest::set);
}



static const char * pycode = "import my_test\n"
"import numpy as np\n"
"import tensorflow.compat.v1 as tf\n"
"tf.disable_v2_behavior()\n"
"mt = my_test.MyTest(123)\n"
"mt.count()\n"
"print(mt.get())\n"
"\n"
"print(mt.value)\n"
"\n"
"mt.value = 12345\n"
"\n"
"print(mt.value)\n"
"\n"
"mt.threadingtest()\n"
"\n"
"b = np.array([10.0, 20.0, 30.0], dtype = np.float)\n"
"print(b)\n"
"mt.greet(b)\n"
"print(b)\n"
"mt.greet(b)\n"
"print(b)\n"
"mt.getArray(b)\n"
"#mt.setArray(b)\n"
"#my_arr = array('f', [1, 2, 3, 4, 5])\n"
"#mt.set_first_element(b.array, 1123.0)\n"
"#mt.setPointer(b.data)\n"
"\n"
"input1 = tf.placeholder(tf.float32)\n"
"input2 = tf.placeholder(tf.float32)\n"
"output = tf.multiply(input1, input2)\n"
"\n"
"x_input = np.array([2], dtype = np.float)\n"
"y_input = np.array([3], dtype = np.float)\n"
"\n"
"with tf.Session() as sess :\n"
"	print(\"Hello !\")\n"
"	print(sess.run([output], feed_dict = { input1:x_input, input2 : y_input }))\n"
"\n"
"mt.plusOne(x_input)\n"
"mt.plusOne(y_input)\n"
"\n"
"print(x_input)\n"
"print(y_input)\n"
"\n"
"with tf.Session() as sess :\n"
"	print(\"Hello !\")\n"
"	print(sess.run([output], feed_dict = { input1:x_input, input2 : y_input }))";
















void exec_test()
{
	// Retrieve the main module
	py::object main = py::import("__main__");

	// Retrieve the main module's namespace
	py::object global(main.attr("__dict__"));

	py::object result = py::exec(pycode,
		global, global);

	py::object print = py::import("__main__").attr("__builtins__").attr("print");
	print(result);
}




int main()
{
	using namespace std;
	//MyTest my_test(0);

	// 본인의 상황에 맞게 설정한다.
	// 나는 아나콘다 가상환경을 만들지 않았다.
	Py_SetPythonHome(L"C:\\ProgramData\\Anaconda3");

	// my_test 핸들러들을 파이썬에 등록한다.
	// Py_Initialize() 함수보다 먼저 와야한다.
	if (PyImport_AppendInittab("my_test", &PyInit_my_test) == -1)
		throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
			"builtin modules");

	Py_Initialize();
	np::initialize();

	if (py::handle_exception(exec_test))
	{
		if (PyErr_Occurred())
		{
			printf("Python Error detected");
			PyErr_Print();
		}
		else
		{
			printf("A C++ exception was thrown  for which "
				"there was no exception translator registered.");
		}
	}

	return 0;
}

 

 

 

위의 소스를 실행하면 아래와 같은 결과를 보게 된다.

 

Windows10 + Visual Studio 2017 (VC14.1)로 테스트를 했다.

Python 3.7 + tensorflow 2.2 버전이다.

boost는 1.73.0 버전에 python numpy 포함해서 직접 컴파일 하였다.

 

파이썬에서 C++ 코드를 실행하는 2가지 방법이다.

1. C++에서 Base 클래스를 만들고 이를 파이썬에서 상속받아 클래스를 구현할때 C++에서 코드가 실행되게 하는 방법.

2. C++ 클래스에서 함수들을 만들고 파이썬에서 이를 실행하는 방법.

 

본인은 Windows10에 아나콘다 가상환경을 설정하지 않았다.

그래서 아나콘다 파이썬 기본 경로가 "C:\\ProgramData\\Anaconda3" 이다.

혹시 아나콘다 가상환경을 구성한 경우 각자 상황에 맞게 아나콘다 경로를 변경한다.

 

// Copyright Stefan Seefeld 2005.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Boost 기본 예제 boost_1_73_0\libs\python\example\quickstart의 코드.
// 컴파일해서 실행하는 경우 제대로 동작하지 않아 직접수정하여 나중에 쓸려고 올려놓음.

#include <boost/python.hpp>

#include <boost/detail/lightweight_test.hpp>
#include <iostream>

namespace python = boost::python;

// An abstract base class
class Base : public boost::noncopyable
{
public:
  virtual ~Base() {};
  virtual std::string hello() = 0;
};

// C++ derived class
class CppDerived : public Base
{
public:
  virtual ~CppDerived() {}
  virtual std::string hello()
  { 
	  return "Hello from C++!";
  }
};

// Familiar Boost.Python wrapper class for Base
struct BaseWrap : Base, python::wrapper<Base>
{
  virtual std::string hello() 
  {
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
    // workaround for VC++ 6.x or 7.0, see
    // http://boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions
    return python::call<std::string>(this->get_override("hello").ptr());
#else
    return this->get_override("hello")();
#endif
  }
};


// Boost 기본 예제 boost_1_73_0\libs\python\example\quickstart의 코드
namespace { // Avoid cluttering the global namespace.

  // A friendly class.
	class hello
	{
	public:
		hello(const std::string& country) { this->country = country; }
		std::string greet() const { return "Hello from " + country; }
	private:
		std::string country;
	};

	// A function taking a hello object as an argument.
	std::string invite(const hello& w) {
		return w.greet() + "! Please come soon!";
	}
}


// Pack the Base class wrapper into a module
BOOST_PYTHON_MODULE(embedded_hello)
{
  python::class_<BaseWrap, boost::noncopyable> base("Base");
}


BOOST_PYTHON_MODULE(extending)
{
	using namespace boost::python;
	class_<hello>("hello", init<std::string>())
		// Add a regular member function.
		.def("greet", &hello::greet)
		// Add invite() as a member of hello!
		.def("invite", invite)
		;

	// Also add invite() as a regular function to the module.
	def("invite", invite);
}



void exec_test1()
{
	// Retrieve the main module
	python::object main = python::import("__main__");

	// Retrieve the main module's namespace
	python::object global(main.attr("__dict__"));

	// Define the derived class in Python.
	python::object result = python::exec(
		"from embedded_hello import *        \n"
		"class PythonDerived(Base):          \n"
		"    def hello(self):                \n"
		"        return 'Hello from Python!' \n",
		global, global);

	python::object PythonDerived = global["PythonDerived"];

	// Creating and using instances of the C++ class is as easy as always.
	CppDerived cpp;
	BOOST_TEST(cpp.hello() == "Hello from C++!");

	std::cout << "testing derived class from C++..." << std::endl;

	// But now creating and using instances of the Python class is almost
	// as easy!
	python::object py_base = PythonDerived();
	Base& py = python::extract<Base&>(py_base) BOOST_EXTRACT_WORKAROUND;

	// Make sure the right 'hello' method is called.
	BOOST_TEST(py.hello() == "Hello from Python!");

	std::cout << "success!" << std::endl;
}


void exec_test2()
{
	// Retrieve the main module
	python::object main = python::import("__main__");

	// Retrieve the main module's namespace
	python::object global(main.attr("__dict__"));

	python::object result = python::exec(
		"from extending import *        \n"
		"hi = hello('California')         \n"
		"hi.greet() \n",
		global, global);

	python::object print = python::import("__main__").attr("__builtins__").attr("print");
	print(result);
}

void exec_test_error()
{
    std::cout << "intentionally causing a python exception..." << std::endl;
    
    // Execute a statement that raises a python exception.
    python::dict global;
    python::object result = python::exec("print unknown \n", global, global);

	python::object print = python::import("__main__").attr("__builtins__").attr("print");
	print(result);
}

int main(int argc, char **argv)
{
	BOOST_TEST(argc == 2);
	std::string script = argv[1];


	// Register the module with the interpreter
	if (PyImport_AppendInittab("embedded_hello", &PyInit_embedded_hello) == -1)
		throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
			"builtin modules");

	if (PyImport_AppendInittab("extending", &PyInit_extending) == -1)
		throw std::runtime_error("Failed to add embedded_hello to the interpreter's "
			"builtin modules");


	// Initialize the interpreter
	Py_SetPythonHome(L"C:\\ProgramData\\Anaconda3");
	Py_Initialize();

	bool error_expected = false;

	if (
		python::handle_exception(exec_test1)
		|| python::handle_exception(exec_test2)
		)
	{
		if (PyErr_Occurred())
		{
			if (!error_expected)
				BOOST_ERROR("Python Error detected");
			PyErr_Print();
		}
		else
		{
			BOOST_ERROR("A C++ exception was thrown  for which "
				"there was no exception translator registered.");
		}
	}

	// Boost.Python doesn't support Py_Finalize yet, so don't call it!
	return boost::report_errors();
}

 

위의 코드를 실행하면 아래와 같은 결과가 나타난다.

출처: https://seanpaulskin.tistory.com/entry/부스트-라이브러리Boost-Library-VC과-MinGW-컴파일Compile-하기 [:: 스킨 테스트 블로그 :: HTML5 / XHTML / HTML / CSS]

 

 

1. 부스트 라이브러리 최신 버전이나 필요한 버전을 다운 받는다.

- 이 글 작성시 부스트 최신 버전 : 1_58_0 

부스트 공식 홈( http://www.boost.org ) 또는 

SourceForge 다운로드  http://sourceforge.net/projects/boost/files/

 

컴파일을 하지 않고 바로 사용 할려면 컴파일된 x32, 또는 x64 파일을 다운
http://sourceforge.net/projects/boost/files/boost-binaries/1.58.0/

Boost_1_58_0-bin-msvc-all-32-64.7z 1.3GB (x32, x64 비주얼 스튜디오 모든 버전 포함)

 

 

 

Visual Studio 2013 크로스 컴파일러를 설치하면  

여러 가지 커맨드 프롬프트 제공 합니다. ( Itanium 버전은 인텔의 개발 중지 발표로 생략)

 

x64 크로스 Tools 명령 프롬프트에서는 x86환경에서 x64 컴파일 가능.

x86, x64 네이티브에서는 각 각  x32 , x64 컴파일 가능.

 

*** 미리 컴파일된 버전을 사용하지 않고 직접 컴파일해서 사용 하고자 한다면 ***

Boost 라이브러리는 두가지 방법으로 컴파일 할 수 있는데,

1. 헤더 파일과 라이브러리를 생성하는 방법
2. 라이브 러리를 생성 하는 방법

처음 설치 한다면 헤더(Header)와 라이브러리(Library)가 필요 하므로 install 옵션을 쓰고 

추가적으로 라이브러리(Library)가 필요하다면 stage를 쓰면 됩니다.

 

 

부스트 라이브러리 루트 폴더에 boostrap.bat 실행해서 b2.exe와 bjam.exe를 생성 합니다.

2개다 같은 일을 하지만 여기서는 b2.exe으로 설명 합니다.

b2 [options] [properties] [install|stage]

VS 2013 기준(msvc-12.0), VS 2012 : msvc-11.0)

 

--stagedir (라이브러리 파일이 있는 stage 폴더내에 복사

--libdir을 지정 해주면 원하는 폴더로 복사( --libdir="C:\Boost\lib" )

--includedir 위 와 마찬가지( --includedir="C:\Boost\include" )

-j n - CPU 멀티 코어 사용 ( n : 코어 갯수 지정 )

 

x32 라이브러리

/MT, /MTd (멀티 스레드 릴리즈, 멀티 스레드 디버그) : 정적 라이브러리 생성( .LIB )

b2  --stagedir=stage32 --toolset=msvc-12.0 -j 4 runtime-link=static

 

/MD, /MDd (멀티 다이나믹, 멀티 다이나믹 디버그  ): 동적 라이브러리 생성( .DLL )

b2  --stagedir=stage32 --toolset=msvc-12.0 -j 4 runtime-link=shared

 

x64 라이브러리

/MT, /MTd (멀티 스레드 릴리즈, 멀티 스레드 디버그) : 정적 라이브러리 생성

b2  --stagedir=stage64 address-model=64 --toolset=msvc-12.0 -j 4 runtime-link=static

 

/MD, /MDd

b2  --stagedir=stage64 address-model=64 --toolset=msvc-12.0 -j 4 runtime-link=shared

 

컴파일 결과파일 이름(Naming) 규칙

- lib 접두사 - Win32에서 Static 라이브러리에 붙음.

- boost - 접두사 모든 파일 앞에 붙음.

- vc120 - 비주얼 스튜디오 버전별로( 2012 : vc110, 2010 : vc100 )

 

- mt - Shared 멀티 스레드용(multi-threading) 릴리즈 버전 

- mt-gd Shared 방식으로 멀티 스레드용(multi-threading) 디버그 버전

- mt-s Static 방식으로 릴리즈

- mt-sgd  Static multi-threading 디버그 버전

- 1_58 - 부스트 버전.

- .lib - 확장자

 


 

컴파일 예제 :

C:\Boost에 Visual Studio 2013버전 x32, x64 디버그, 릴리즈 

싱글, 멀티스레딩(정적, 동적 라이브러리)를 멀티코어 4개로 컴파일

 

b2  --libdir="C:\Boost\lib" --libinclude="C:\Boost\include"

 --toolset=msvc-12.0 variant=debug,release address-model=32,64 

threading=single,multi -j 4 runtime-link=static,shared

컴파일 결과 : C:\Boost\Lib, C:\Boost\Include 생성 lib 폴더내에 .lib, .dll 파일 생성




+ Recent posts