File: build_windows.md

package info (click to toggle)
mlpack 4.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 32,064 kB
  • sloc: cpp: 233,202; python: 1,940; sh: 1,201; lisp: 414; makefile: 85
file content (265 lines) | stat: -rw-r--r-- 10,589 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Building mlpack from source on Windows

*by German Lancioni, Miguel Canteras, Shikhar Jaiswal, Ziyang Jiang*

This tutorial will show you how to build mlpack for Windows from source, so
you can later create your own C++ applications, using two different ways:

  - Using CMake to generate an intermeditate Visual Studio solution (`.sln`).
  - Use Visual Studio's CMake integration to directly build from the
    `CMakeLists.txt`.

Before you try building mlpack, you may
want to install mlpack using `vcpkg` for Windows. If you don't want to install
using `vcpkg`, skip this section and continue with the build tutorial.

- Install Git (https://git-scm.com/downloads and execute setup)

- Install CMake (https://cmake.org/ and execute setup)

- Install vcpkg (https://github.com/Microsoft/vcpkg and execute setup)

- To install the mlpack library only:

```
PS> .\vcpkg install mlpack:x64-windows
```

- To install mlpack and its console programs:

```
PS> .\vcpkg install mlpack[tools]:x64-windows
```

After installing, in Visual Studio, you can create a new project (or open
an existing one). The library is immediately ready to be included
(via preprocessor directives) and used in your project without additional
configuration.

Note that when building mlpack, the `/std:c++17` option is required for Visual
Studio.

## Build Environment

This tutorial has been designed and tested using:

- Windows 10
- Visual Studio 2019 (toolset v142)
- mlpack
- OpenBLAS.0.2.14.1
- armadillo (newest version)
- and x64 configuration

The directories and paths used in this tutorial are just for reference purposes.

## Pre-requisites

- Install CMake for Windows (win64-x64 version from https://cmake.org/download/)
  and make sure you can use it from the Command Prompt (may need to add the
  `PATH` to system environment variables or manually set the `PATH` before
  running CMake)

- Download the latest mlpack release from the
  [mlpack website](https://www.mlpack.org)

## Windows build instructions

- Unzip mlpack to `C:\mlpack\mlpack`
- Open Visual Studio and select: File > New > Project from Existing Code
 - Type of project: Visual C++
 - Project location: `C:\mlpack\mlpack`
 - Project name: mlpack
 - Finish
- Make sure the solution configuration is `Debug` and the solution platform is
  `x64` for this Visual Studio project
- We will use this Visual Studio project to get the OpenBLAS dependency in the
  next section

## Dependencies

### OpenBLAS Dependency

- Open the NuGet packages manager (Tools > NuGet Package Manager > Manage NuGet
  Packages for Solution...)
- Click on the "Browse" tab and search for "openblas"
- Click on OpenBlas and check the mlpack project, then click Install
- Once it has finished installing, close Visual Studio

### Building OpenBLAS from Source

Unfortunately, the support for building `LAPACK` and `BLAS` on Windows is quite
poor, due to the need for Fortran compiler and libraries. The easiest method to
get the necessary `BLAS/LAPACK` libraries built on Windows is to compile
OpenBLAS with LLVM's `clang-cl` and `flang` to produce the required static
library (`.lib`) files 
compatible with the MSVC compiler. A comprehensive guide on the
compilation of OpenBLAS for Windows can be found
[here](https://github.com/OpenMathLib/OpenBLAS/wiki/How-to-use-OpenBLAS-in-Microsoft-Visual-Studio).

One could always download prebuilt `LAPACK` and `BLAS` libraries for Windows.
However, there are few official sources, and some of those libraries may require
further `dll`s at runtime which may not be available in your system.

It you choose to build `OpenBLAS` from source, make sure that `LAPACK` functions
are also built. Finally, make sure that the `openblas.lib` library is linked in
your `Armadillo` build (see below), as well as the library path used for the
CMake options `BLAS_LIBRARIES` and `LAPACK_LIBRARIES` in the mlpack CMake
project.

### Armadillo Dependency

- Download the newest version of Armadillo from
  [Sourceforge](https://arma.sourceforge.net/download.html)
- Unzip to `C:\mlpack\armadillo`
- Create a `build` directory into `C:\mlpack\armadillo\`
- Open the Command Prompt and navigate to `C:\mlpack\armadillo\build`
- Run CMake:

```
cmake -G "Visual Studio 16 2019" -A x64 -DBLAS_LIBRARY:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" -DLAPACK_LIBRARY:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" ..
```

*Note*: If you are using different directory paths, a different configuration
(e.g. Release) or a different VS version, update the cmake command accordingly.
If CMake cannot identify the compiler version, check if the Visual Studio
compiler and Windows SDK are installed correctly.

- Once it has successfully finished, open
  `C:\mlpack\armadillo\build\armadillo.sln`
- Build > Build Solution
- Once it has successfully finished, close Visual Studio

## Building mlpack with CMake-generated Solution

- Create a `build` directory into `C:\mlpack\mlpack\`
- You can generate the project using either cmake via command line or GUI. If
  you prefer to use GUI, refer to the appendix
- To use the CMake command line prompt, open the Command Prompt and navigate to
  `C:\mlpack\mlpack\build`
- Run cmake:

```
cmake -G "Visual Studio 16 2019" -A x64 -DBLAS_LIBRARIES:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" -DLAPACK_LIBRARIES:FILEPATH="C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a" -DARMADILLO_INCLUDE_DIR="C:/mlpack/armadillo/include" -DARMADILLO_LIBRARY:FILEPATH="C:/mlpack/armadillo/build/Debug/armadillo.lib" ..
```

*Note*: CMake will attempt to automatically download the ensmallen dependency.
If for some reason cmake can't download the dependency, you will need to
manually download ensmallen from http://ensmallen.org/ and extract it to
`C:\mlpack\mlpack\deps\`. Then, specify the path to ensmallen using the flag:
`-DENSMALLEN_INCLUDE_DIR=C:/mlpack/mlpack/deps/ensmallen/include`.

- Once CMake configuration has successfully finished, open
  `C:\mlpack\mlpack\build\mlpack.sln`
- Build > Build Solution (this may be by default in Debug mode)
- Once it has sucessfully finished, you will find the library files you need in:
  `C:\mlpack\mlpack\build\Debug` (or `C:\mlpack\mlpack\build\Release` if you
  changed to Release mode)

You are ready to create your first application; take a look at
[Deploying mlpack on Windows](deploy_windows.md).

## Building mlpack with Visual Studio's CMake integration

This project can be directly built from the `CMakeLists.txt` with the latest
version of MS Visual Studio, given you have CMake integration via the
[C++ CMake tools for Windows](https://learn.microsoft.com/en-us/cpp/build/cmake-projects-in-visual-studio?view=msvc-160).
To open the CMake project with Visual Studio, select File -> Open -> CMake in
the top menu, followed by selecting the root `CMakeLists.txt` located in
mlpack's root directory.

In order to allow Visual Studio to configure the CMake project, the CMake
configuration json will have to be edited to provide the [relevant options
shown in the `README`](../../README.md#2-dependencies) needed to find all the
dependencies. The options that you must provide to Visual Studio's CMake are:

  - `ARMADILLO_INCLUDE_DIR`
  - `ARMADILLO_LIBRARY`
  - `CEREAL_INCLUDE_DIR`
  - `BLAS_LIBRARIES`
  - `LAPACK_LIBRARIES`

The CMake configuration json can be edited in Visual Studio by right clicking
the root `CMakeLists.txt` in the project view, selecting *CMake settings for
mlpack* and finally clicking on *edit JSON*.  Adding a new CMake option can be
done by adding object fields with the following format to the variables array in
the `CMakeSettings.json`:

```
{
    "name": "options_name_string",
    "value": "options_value_string",
    "type" : "{BOOL|FILEPATH|PATH|STRING}"
}
```

Here is a full example of the `CMakeSettings.json`file:

```
{
  "configurations": [
    {
      "name": "x64-Debug (default)",
      "generator": "Ninja",
      "configurationType": "Debug",
      "inheritEnvironments": [ "msvc_x64_x64" ],
      "buildRoot": "${projectDir}\\out\\build\\${name}",
      "installRoot": "${projectDir}\\out\\install\\${name}",
      "cmakeCommandArgs": "",
      "buildCommandArgs": "",
      "ctestCommandArgs": "",
      "variables": [
        {
          "name": "ARMADILLO_INCLUDE_DIR",
          "value": "PATH/TO/CPP/DEPENDENCY/armadillo-10.1.2/include",
          "type": "PATH"
        },
        {
          "name": "ARMADILLO_LIBBRARY",
          "value": "PATH/TO/CPP/DEPENDENCY/armadillo-10.1.2/lib/armadillo.lib",
          "type": "PATH"
        },
        {
          "name": "CEREAL_INCLUDE_DIR",
          "value": "PATH/TO/CPP/DEPENDENCY/cereal-1.3.0/include",
          "type": "PATH"
        },
        {
          "name": "BLAS_LIBRARIES",
          "value": "PATH/TO/CPP/DEPENDENCY/OpenBLAS/lib/openblas.lib",
          "type": "PATH"
        },
        {
          "name": "LAPACK_LIBRARIES",
          "value": "PATH/TO/CPP/DEPENDENCY/OpenBLAS/lib/openblas.lib",
          "type": "PATH"
        }
      ]
    }
  ]
}
```

## Appendix

If you prefer to use the CMake GUI, follow these instructions:

  - To use the CMake GUI, open "CMake".
    - For "Where is the source code:" set `C:\mlpack\mlpack\`
    - For "Where to build the binaries:" set `C:\mlpack\mlpack\build`
    - Click `Configure`
    - If there is an error and Armadillo is not found, try "Add Entry" with the
      following variables and reconfigure:
      - Name: `ARMADILLO_INCLUDE_DIR`; type `PATH`; value `C:/mlpack/armadillo/include/`
      - Name: `ARMADILLO_LIBRARY`; type `FILEPATH`; value `C:/mlpack/armadillo/build/Debug/armadillo.lib`
      - Name: `BLAS_LIBRARY`; type `FILEPATH`; value `C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a`
      - Name: `LAPACK_LIBRARY`; type `FILEPATH`; value `C:/mlpack/mlpack/packages/OpenBLAS.0.2.14.1/lib/native/lib/x64/libopenblas.dll.a`
    - Once CMake has configured successfully, hit "Generate" to create the `.sln` file.

## Additional information

If you are facing issues during the build process of mlpack, you may take a look
at other third-party tutorials for Windows, but they may be out of date:

 * [Github wiki Windows Build page](https://github.com/mlpack/mlpack/wiki/WindowsBuild)
 * [Keon's tutorial for mlpack 2.0.3](https://keon.github.io/mlpack-on-windows/)
 * [Kirizaki's tutorial for mlpack 2](https://overdosedblog.wordpress.com/2016/08/15/once_again/)