[eigen] Re: Compiling Eigen to the Web

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

Oh yes, at the moment Emscripten doesn't like Eigen's prefetch() intrinsics, this should probably be fixed in Emscripten itself, meanwhile you can simply locally patch Eigen to remove prefetch.

Emscripten also doesn't like Eigen's asm comments, but that's easy to work around by passing -DEIGEN_ASM_COMMENT on the compiler invocation.


2013/11/7 Benoit Jacob <jacob.benoit.1@xxxxxxxxx>

This is to share a simple example of using Eigen in a Web page:


Here, a linear solver from Eigen is used to build a small _javascript_ power series library, which is used to do some calculus to find a constant-speed parametrization of a Bezier curve.

The compiler used to compile Eigen to JS is Emscripten:


The Eigen-using C++ code being compiled is:


The compilation command line is:

emcc solve.cpp -O2 -o solve.js -I /hack/eigen -std=c++11 -DEIGEN_ASM_COMMENT -s EXPORTED_FUNCTIONS="['_solve']"

This yields a JS file which you can see here:


(If you want it to be more readable, so you can see what Eigen is doing in JS, pass -g to the compiler)

To use that JS code, user code uses Emscripten's pseudo malloc() function and writes/reads directly on the Typed Array that serves as Emscripten's heap, see e.g. the code implementing power series division using Eigen's solver:

function PowerSeriesDiv(a, b) {
  var size = a.length;

  var matrix_data = Module._malloc(4 * size * size);
  var rhs_data = Module._malloc(4 * size);
  var result_data = Module._malloc(4 * size);

  for (var i = 0; i < size; i++) {
    HEAPF32[(rhs_data>>2) + i] = a[i];

  for (var col = 0; col < size; col++) {
    for (var row = 0; row < size; row++) {
      HEAPF32[(matrix_data>>2) + col * size + row] = row >= col ? b[row - col] : 0;

  _solve(size, matrix_data, rhs_data, result_data);

  var result = new Float32Array(size);
  for (var i = 0; i < size; i++) {
    result[i] = HEAPF32[(result_data>>2) + i];

  return result;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/