I had a similar issue. Mainly would like to paralellize facets of my code but that was not possible when using shared memory.
A solution to this could be using MPI message passage interface where the memory is distributed.
In a simplistic manner, Threads exploit multiple cpu's but they share the same shunk of memory; while mpi exploits multiple cpu's
the memory is also multiple (e.g. one shunk of memory per cpu).
In this case it occurs seg fault, because std::Function or something in casadi libraries is not thread safe and even thought it is two different function f and g,
probably they share some members (I guess).
Anyway I played a bit with OpenMPI and here you can find a workarround to this issue:
#include <casadi/casadi.hpp>
#include <mpi.h>
using namespace casadi;
void callFuncLoop(Function & func, const DM &x_0, int sleep_time){
std::cout << "Calling func: " << func.name() << std::endl;
int call_count = 0;
while(true) {
auto result = func(std::vector<casadi::DM>{x_0})[0];
std::this_thread::sleep_for(std::chrono::milliseconds(sleep_time));
call_count++;
if(call_count%5) std::cout << func.name() << " - " << call_count << " calls" << std::endl;
}
}
int main(int argc, char** argv){
MPI_Init( &argc, &argv );
// Reading size and rank
int process_id, size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &process_id);
// Create casadi function objects
auto x = SX::sym("x", 2,1);
auto f = Function("f", {x}, {x(0) + x(1)});
auto g = Function("g", {x}, {x(0)});
if(process_id == 0) {
auto x_0 = DM({0,1});
callFuncLoop(f, x_0, 5);
}
if(process_id == 1) {
auto x_0 = DM({0,1});
callFuncLoop(g, x_0, 5);
}
// Finalisation
MPI_Finalize();
}
Note: you need to install mpi and run the executable
mpirun -np 2 ./executable
run 2 processes over the executable
If you are interested in the avenue , this tutorial was very helpful: