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

Add differentiable support #10

Closed
mikeheddes opened this issue Mar 17, 2019 · 3 comments
Closed

Add differentiable support #10

mikeheddes opened this issue Mar 17, 2019 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@mikeheddes
Copy link
Contributor

mikeheddes commented Mar 17, 2019

Add differentiable support by compiling with AuDi.

Dependencies

Requirements

  • To compile gmp a 32 bit system is required, I use i386/ubuntu:latest.
  • The m4, build-essentials, gcc-multilib, git, cmake, curl, node.js and python. I use the following command to install the dependencies:
apt-get -y update
apt-get -y install build-essential m4 gcc-multilib git cmake nodejs default-jre python3.6 python3-distutils vim curl
ln /usr/bin/python3.6 /usr/bin/python

Note: the command ln /usr/bin/python3.6 /usr/bin/python is needed to access python on the comand line instead of using Python with python3.6.

Compilation steps

Boost

curl -L -o boost_1_68_0.tar.bz2 https://dl.bintray.com/boostorg/release/1.68.0/source/boost_1_68_0.tar.bz2
tar --bzip2 -xf ./boost_1_68_0.tar.bz2
cd ./boost_1_68_0
./bootstrap.sh
# see Using boost with emscripten
sed -i 's/using gcc ;/using gcc : : "\/root\/emsdk\/emscripten\/tag-1.38.27\/em++" ;/g' project-config.jam 
./b2 variant=release threading=single --with-timer --with-chrono --with-serialization --with-system --with-test --with-iostreams --with-regex install

Eigen

# curl -L -o eigen_3_3_7.tar.bz2 http://bitbucket.org/eigen/eigen/get/3.3.7.tar.bz2
# tar --bzip2 -xf ./eigen_3_3_7.tar.bz2
# mkdir eigen-eigen-323c052e1731/build
# cd eigen-eigen-323c052e1731/build
# emconfigure cmake ..
# emmake make install
apt-get install libeigen3-dev

gmp

curl -L -o gmp_6_1_2.tar.bz2 https://gmplib.org/download/gmp/gmp-6.1.2.tar.bz2
tar --bzip2 -xf ./gmp_6_1_2.tar.bz2
cd gmp-6.1.2
CC_FOR_BUILD=/usr/bin/gcc emconfigure ./configure --build i686-pc-linux-gnu --host none --disable-assembly --enable-cxx
# apply patches found in: https://github.com/marcosscriven/cgaljs/tree/master/components/gmp/patches
emmake make -j4
emmake make install

mpfr

curl -L -o mpfr-4.0.1.tar.bz2 https://www.mpfr.org/mpfr-4.0.1/mpfr-4.0.1.tar.bz2
tar --bzip2 -xf ./mpfr-4.0.1.tar.bz2
curl -L -o mpfr-4.0.1-allpatches https://www.mpfr.org/mpfr-4.0.1/allpatches
cd mpfr-4.0.1
patch -N -Z -p1 < ../mpfr-4.0.1-allpatches
emconfigure ./configure --build i686-pc-linux-gnu --host none --with-gmp-include=/usr/local/include/gmp --with-gmp-lib=/usr/local/lib
emmake make -j 4
emmake make install

mp++

git clone https://github.com/bluescarni/mppp.git
cd mppp
git checkout tags/v0.9
mkdir build && cd build
emconfigure cmake .. -DMPPP_WITH_MPFR=ON -DGMP_INCLUDE_DIR=/usr/local/include/gmp -DGMP_LIBRARY=/usr/local/lib -DMPFR_INCLUDE_DIR=/usr/local/include/mpfr -DMPFR_LIBRARY=/usr/local/lib
emmake make install

piranha

git clone https://github.com/bluescarni/piranha.git
cd piranha
git checkout tags/v0.11
mkdir build && cd build
# disable all try_compile statements in ../CMakeLists.txt and in ../cmake_modules/PiranhaCompilerLinkerSettings.cmake
emconfigure cmake .. -DGMP_INCLUDE_DIR=/usr/local/include -DMPFR_INCLUDE_DIR=/usr/local/include -DMPFR_LIBRARIES=/usr/local/lib -DGMP_LIBRARIES=/usr/local/lib -DBoost_INCLUDE_DIR=/usr/local/include -DBoost_LIBRARY_DIR_RELEASE=/usr/local/lib
emmake make install

audi

git clone https://github.com/darioizzo/audi.git
cd audi
git checkout tags/1.6.1
mkdir build && cd build
# disable try_compile statements in ../CMakeLists.txt
emconfigure cmake .. -DAUDI_WITH_MPPP=ON -DBoost_INCLUDE_DIR=/usr/local/include -DBoost_LIBRARY_DIR=/usr/local/lib -DEIGEN3_INCLUDE_DIR=/usr/local/include/eigen3 -DPiranha_INCLUDE_DIR=/usr/local/include -DGMP_INCLUDE_DIR=/usr/local/include -DGMP_LIBRARY=/usr/local/lib -DMPFR_INCLUDE_DIR=/usr/local/include -DMPFR_LIBRARY=/usr/local/lib -DAUDI_BUILD_TESTS=OFF
emmake make install

dcgp

git clone https://github.com/darioizzo/dcgp.git
mkdir dcgp/build && cd dcgp/build
# disable try_compile statements and statements with TBB in ../CMakeLists.txt 
emconfigure cmake .. -DBoost_INCLUDE_DIR=/usr/local/include -DBoost_LIBRARY_DIR_RELEASE=/usr/local/lib -DEIGEN3_INCLUDE_DIR=/usr/include/eigen3/ -DPiranha_INCLUDE_DIR=/usr/local/include/ -DGMP_INCLUDE_DIR=/usr/local/include/ -DGMP_LIBRARY=/usr/local/lib/ -DMPFR_INCLUDE_DIR=/usr/local/include/ -DMPFR_LIBRARY=/usr/local/lib/ -DAUDI_INCLUDE_DIRS=/usr/local/include/ -DDCGP_BUILD_TESTS=OFF
emmake make install

Resources

The following links might be helpful for figuring out how to compile all the necessary libraries correctly.

  1. dCGP Github
  2. Boost Getting Started on Unix Variants
  3. Using boost with emscripten
  4. Building Boost with emscripten
  5. Boost Building the Unit Test Framework
  6. Status on using webidl binder or embind for C++ bindings
  7. Compiling GMP to WASM instructions
  8. Compiling GMP/MPFR with Emscripten
  9. Error when configuring gmp, missing m4
  10. GMP and MPFR comilation script
  11. Setting Up a 32-bit chroot Environment in Ubuntu
  12. 32-bit Ubuntu docker image
  13. Compiling GMP/MPFR practical example
  14. GMP/MPFR/BOOST python build script with emscripten
@mikeheddes mikeheddes added enhancement New feature or request help wanted Extra attention is needed labels Mar 17, 2019
@mikeheddes mikeheddes added this to the Working differentiable CGP milestone Mar 17, 2019
@mikeheddes
Copy link
Contributor Author

This repo contains a build script to compile gmp@5.1.1, mpfr@3.1.1 and boost@1.49.0 emscripten.

@mikeheddes
Copy link
Contributor Author

mikeheddes commented Mar 21, 2019

After following the instructions above I compiled the following test:

#include <emscripten.h>
#include <vector>
#include <iostream>
#include <audi/gdual.hpp>
#include <dcgp/wrapped_functions.hpp>

extern "C"
{
	double EMSCRIPTEN_KEEPALIVE my_calc()
	{
		std::vector<audi::gdual<double>> my_vect;
		my_vect.emplace_back(1.1, "x", 1);
		my_vect.emplace_back(1.2, "y", 1);

		auto result = dcgp::my_sum<audi::gdual<double>>(my_vect);
		return result.constant_cf();
	}

	void EMSCRIPTEN_KEEPALIVE gdual_evaluation()
	{
		gdual<double> x1(1., "x1", 2);
		gdual<double> x2(1., "x2", 2);
		
		auto f = x1 * x1 + x2;
		
		std::cout << f.evaluate({{"dx1", 1.}, {"dx2", 1.}}) << "\n";
	}

}

Which for gdual_evaluation gives threading errors from piranha. my_calc works as expected.

Compiling was done with the following steps:

emcc wrapped_functions_test.cpp -o wrapped_functions_test.bc -I/usr/local/include -I/usr/include/eigen3 -std=c++11
emcc wrapped_functions_test.bc /usr/local/lib/libmpfr.a /usr/local/lib/libgmpxx.a /usr/local/lib/libgmp.a -o wrapped_functions_test.html

Note: when running the commands above it gave an error saying that asm2wasm was not found. This was because it was searching in the 64 bit binaryen folder instead of in the installed 32 bit binaryen folder. Simply copying the 32 bit folder and naming it the 64 bit folder resolved this error.

cp -a ~/emsdk/binaryen/tag-1.38.27_32bit_binaryen/. ~/emsdk/binaryen/tag-1.38.27_64bit_binaryen/

@mikeheddes
Copy link
Contributor Author

Enabling the #enable-webassembly-threadsflag in chrome://flags/ and then compiling again with the -s USE_PTHREADS=1 flag removes the errors.

The compiling steps became:

emcc wrapped_functions_test.cpp -o wrapped_functions_test.bc -I/usr/local/include -I/usr/include/eigen3 -std=c++11 -s USE_PTHREADS=1
emcc wrapped_functions_test.bc /usr/local/lib/libmpfr.a /usr/local/lib/libgmpxx.a /usr/local/lib/libgmp.a -o wrapped_functions_test.html -s USE_PTHREADS=1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant