2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

conda を利用した Numpy 付 Boost の導入 Visual Studio 2017 対応

Last updated at Posted at 2018-03-24

本日は

Numpy付Boostの導入

Numpy 付 Boost 1.66.0 の導入 Visual Studio 2017 対応版

の別解としてBoostを conda install で入れてみようという話を紹介します.

Reference としては次があります.

install

https://anaconda.org/conda-forge/boost にしたがって次を実行します.

image.png

conda install -c conda-forge boost

こうすると Boost 1.66.0 のBoostがはいるっぽいです.

問題はどこにインストールされたか

Pybind11の場合は C:\Miniconda3\include にヘッダーが入ってたのですがぱっと見どこにあるかわかりませんでした.

そこでMiniconda以下のディレクトリで検索をかけたところ

ヘッダーは

  • C:\Miniconda3\Library\include

ライブラリは

  • C:\Miniconda3\Library\lib

に配置されることがわかりました.

早速試してみます.

Visual Studio 2017 起動

Visual Studioを起動して

condaboost という名前でプロジェクトを作ります.

VC++ ディレクトリ においてインクルードディレクトリとライブラリディレクトリに上で見つけたBoost関連のパス と 各自のもっているPythonのヘッダーとライブラリのパスを追加します.例えば下の図のように追加します.

image.png

あとはコードを書きます.

sample.cpp
/*
Call C++ From Python
Reference:
http://tadaoyamaoka.hatenablog.com/entry/2017/05/25/234934
https://qiita.com/takuya-ki/items/3555ab17f9cea534e13b
https://qiita.com/termoshtt/items/81eeb0467d9087958f7f
*/

//#define BOOST_PYTHON_STATIC_LIB <-不要になりました
//#define BOOST_NUMPY_STATIC_LIB <-不要になりました
# include <boost/python/numpy.hpp>
# include <stdexcept>
# include <algorithm>

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

/* 2倍にする */
void mult_two(np::ndarray a) {
	int nd = a.get_nd();
	if (nd != 1)
		throw std::runtime_error("a must be 1-dimensional");
	size_t N = a.shape(0);
	if (a.get_dtype() != np::dtype::get_builtin<float>())
		throw std::runtime_error("a must be float32 array");
	float *p = reinterpret_cast<float *>(a.get_data());
	std::transform(p, p + N, p, [](float x) { return 2 * x; });
}

BOOST_PYTHON_MODULE(condaboost) {
	Py_Initialize();
	np::initialize();
	p::def("mult_two", mult_two);
}

Numpy 付 Boost 1.66.0 の導入 Visual Studio 2017 対応版

のときは

# define BOOST_PYTHON_STATIC_LIB
# define BOOST_NUMPY_STATIC_LIB

C/C++->コード生成 における ランタイムライブラリの設定 が必要でしたが

今回の場合は不要です.

ビルドすると condaboost.pyd というものが生成されるので

>>> import condaboost

という形式で呼び出せます.

導入はちょっと楽になりました.

欠点

conda パッケージの都合上

https://github.com/peterjc123/pytorch-scripts
で導入した PyTorch パッケージが アンインストールされてしまう.

悲しい.

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?