Keep MATLAB engine open in C++ dll - c++

I want to create a dll which keeps an instance of the MATLAB engine open for use by other scripts. That is, the goal is to not have to keep initialising and closing the MATLAB instance, which takes time.
For some background, I have a working C++ program which initialises a MATLAB engine and contains a load of functions to do various things in MATLAB. As some kind of minimum example, I have the three scripts below.
header.h
#pragma once
#include "MatlabEngine.hpp"
#include "MatlabDataArray.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <tuple>
#pragma comment (lib,"libmat.lib")
#pragma comment (lib,"libmx.lib")
#pragma comment (lib,"libmex.lib")
#pragma comment (lib,"libeng.lib")
// Example function definition
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir);
MatlabFunctions.cpp
#include "header.h"
// Example function
void setWorkingDir(std::unique_ptr<matlab::engine::MATLABEngine>& matlabPtr, std::string dir){
std::u16string matlabCommand = matlab::engine::convertUTF8StringToUTF16String("cd '"+dir+"'");
matlabPtr->eval(matlabCommand);
}
Main.cpp
#include "header.h"
int main(){
using namespace matlab::engine;
matlab::data::ArrayFactory factory; // Create MATLAB data array factory
std::unique_ptr<MATLABEngine> matlabPtr = startMATLAB(); // Start MATLAB engine
// Do various functions
}
My goal is to get rid of Main.cpp and create a dll which keeps keeps the MATLAB engine open, allowing the functions in MatlabFunctions.cpp to be run without having to start the MATLAB engine each time.
Is this possible? And if so, how can it be done?

Some people suggested to use IPC here for the task to be done. Using something like COM is not cross-platform and, well, pretty outdated at the moment. As a modern and cross-platform solution, I'd use an actual server application (which will start MATLAB engine and wait for requests) with, for example, gRPC running. Here's tutorial, pretty good one.
Other than that, you can specify your own networking protocol to run your tasks on the server application without the need of IPC, but the gRPC solution works simply out of the box, no bloat coding included.

Related

Compiling multiple source files in Rcpp

I have the following directory structure
my_func
- my_func_r.cpp
- my_func.c
- my_func.h
- my_func_test.c
- matrix/
- matrix.h
- matrix.c
The matrix directory contains some matrix structures in matrix.h and some initialisation, free, print etc. functions in matrix.c. The my_func.h file is something like
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "matrix/matrix.h"
... some structures and templates ...
The my_func.c file is then
#include "my_func.h"
... helper functions ...
int my_func(...) {
... my_func stuff ...
return 0;
}
The my_func_test.c is something like
#include "my_func.h"
int main() {
... some test ...
return 0;
}
With gcc/g++ I can run this fine with
gcc my_func_test.c my_func.c matrix/matrix.c -o test -lm
The final file my_func_r.cpp is an interface between the Rcpp structures and the structures used in my_func.c. It is currently something like
#include "my_func.h"
#include <Rcpp.h>
// [[Rcpp::export]]
int my_func_r(Rcpp::List x, ...) {
... convert inputs to structure recognised by my_func.h ...
... run my_func.c ...
... put returned objects back into one of the R structure ...
return 0;
}
The problem I have is if I now run
sourceCpp('my_func_r.cpp', verbose=TRUE, rebuild=TRUE)
It complains about missing symbols for functions located in matrix/matrix.c. A workaround is to simply paste all my header and source code from both the my_func and matrix files at the top of my_func_r.cpp.
This however feels a very unsatisfactory solution especially for code maintenance. What is the easiest way to accomplish what I am trying to do?
Quick ones:
This is not really particular to Rcpp per se
You are simply struggling with a more advanced / complicated src/ directory in an R build.
There is official documentation about this in Writing R Extensions, and the questions has come up here on SO before.
You could compile a libmatrix.a first in the subdirectory and link to that. This is doable via a simple src/Makevars but still discouraged. So read on.
But this is a self-inflicted wound. Just copy matrix.h and matrix.c into src/, adjust the include path, and you are done.
As always: Create a package. Don't use sourceCpp() on larger setup. It is not made for that,

How do I time a process in c++?

I'm working on a project where I'm to time the creation of fork() and pthread_create(). We're supposed to time how long it takes to do each of these tasks by using system calls to create a personalized timer class. For assistance, the professor has told us to check man -k time.
I'm new to both using system calls and using man pages for documentation, so I'm entirely lost. So far the code I'm trying to get working is this:
#include "Timer.h"
#include <iostream>
#include <signal.h>
#include <ctime>
#include <time.h>
using namespace std;
Timer::Timer() {
timer_t * tid;
timer_create(CLOCK_REALTIME, NULL, tid);
int time = timer_gettime(tid, 0);
cout<<time;
}
When compiling, eclipse tosses me these errors:
undefined reference to 'timer_create', line 20
undefined reference to 'timer_gettime', line 21
The internet has pointed me in the direction of including the -lrt library when compiling, but I can't find anything that says how to do that, suggesting that perhaps it's entirely wrong.
So, am I on the right path? If I am, how am I supposed to get this code working?
Edit:
I got the clock() based timer working. The downside is that we're timing fork(), which isn't registering as taking any time at all when timing it. Any ideas?
You can use this
time_t start,end;
start=clock();//predefined function in c
//after the user defined function does its work
end=clock();
t=(end-start)/CLOCKS_PER_SEC;

'sqlite3_api' was not declared in this scope

I've been learning sqlite3 programming in C++ for the first time and this error confounds me and my internet searching abilities.
Here is my code, as far as it gets before throwing an error.
#include <iostream>
#include <sqlite3ext.h>
using namespace std;
int main()
{
sqlite3 *database;
int check;
check = sqlite3_open("introdb3.db", &database); //error is here
}
I'm pretty sure that it has something to do with the libraries that are (or aren't) being linked, but I can't figure out how to make it go properly.
I'm on Ubuntu using code::blocks.
Thanks!!
Instead of
#include <sqlite3ext.h>
write
#include <sqlite3.h>
The sqlite3ext.h file is only needed if you are going to write an SQLite extension - a custom function, for example. For regular database access, use sqlite3.h.

Communicating with XBoard (chess engine) (C++/C)

I was just toying around with making a basic chess engine. I was able to get a lot of advice from http://web.archive.org/web/20070704121716/http://www.brucemo.com/compchess/programming/alphabeta.htm, but the real site is down and not all the pages are archived. (Anyone know where to find a full version of Bruce's site?)
But now to the real question: how do I communicate with XBoard? I understand it is via stdin and stdout, but I've been having problems in code. Basically, to get started, I just want to
receive input from XBoard and print it to the console/screen
Give a move of hard-coded input to XBoard and have it make the move
program utility functions and have a random chess ai which chooses random moves.
After that, I can start implementing real things like alpha-beta searching.
I am stuck on the first two things right now. Here is some code I have tried to write/borrowed.
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STR_BUFF 256
using namespace std;
int main (int argc, const char * argv[])
{
char input[STR_BUFF];
char output[STR_BUFF];
while(true){
fflush(stdout);
// read input
if (!fgets(input, STR_BUFF, stdin)){
printf("terminated");
return 0;;
}
printf("%s", input);
}
return 0;
}
I am just getting back into C after about 6 months break and this is the first project that I have used stdin/stdout pipelines to communicate with another program (minus a few basic programs) so I would appreciate any help and any explanations. I know programming a chess engine is a herculean task, but I have already programmed the rules of chess before and what I can find of Bruce's site is absolutely amazing.
You are doing it almost right: get a command from XBoard with fgets, then report a move with printf and fflush. (One thing is wrong, though: you don't need to 'print the command to the console/screen'; you are not communicating with the console/screen; you only read commands from XBoard and send moves back to XBoard).
Probably, it would be easier to start with some existing code. Try to read sources for GNU Chess. Or download sources for any other chess engine, supporting XBoard protocol.
And here is other question with lots of information on chess engine programming: "What are some good resources for writing a chess engine?".
I think that you're looking for pipe(), included in unistd.h. Take a look at Can popen() make bidirectional pipes like pipe() + fork()? for notes on implementation.

Selecting a device for CUDA debugging

I've got a couple of Parallel Nsight compatible CUDA GPU's. The second (lower) one is connected to my monitor, and the first (upper) one is is set up as a PhysX accelerator. You can clearly see my configuration below.
Now in order to perform CUDA debugging with Nsight, I need to only run kernel code on the GPU not connected to the monitor. My computer is already setup for "headless debugging". Also below is a basic example of code I wish to execute on the other GPU:
// KernelCall.cu
#include <iostream>
#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__global__ void kernel(void){}
int main()
{
kernel<<<1,1>>>();
system("pause");
return 0;
}
My question is basically what do I need to put in this code so that it only executes on the other device and allow me to perform CUDA debugging?
cudaSetDevice
https://visualization.hpc.mil/wiki/Choosing_the_GPU_in_a_Multi-GPU_system