mclInitializeApplication and parallel execution of mcc compiled functions - parfor - thread-pool

4 visualizzazioni (ultimi 30 giorni)
Hi,
I join some example code we are developping and we would like your opinion on this.
We have some mcc compiled matlab functions :
  1. matlab_singleFile : executes sequentially
  2. matlab_singleFile_parloop : executes a multi-threaded computation via parfor
We compile these functions with mcc to be able to call these functions in c++. The resulting dynamic library is called : libmatlabtest.dll
We have some questions :
  1. is there any risk when spawning the sequential function 1. to multiple threads in the context of the thread_pool ?
  2. is there any risk in calling the multi-threaded function 2. from C++ ?
By 'risk', we mean if matlab mcc-compiled code is entirely thread safe.
Please note that of course the functions we show here are nothing but test functions. Real functions we will be calling make wide use of Matlab toolboxes such as Parallel computing toolbox, Image processing toolbox, Signal toolbox, and others.
Please also refer to the previous Technical Support Case #04822880
Thanks,
Daniele
Code reported for clarity :
% Call this script as:
% >> matlab_singleFile("foobar")
function ret = matlab_oneFile(fileName)
save(fileName+".mat");
disp(['File Name Passed to matlab_oneFile: ', fileName])
ret= true;
end
% Call this script as:
% >> matlab_singleFile_parLoop("foobar")
function ret = matlab_singleFile_parLoop(fileName)
save("matlab_singleFile_parLoop.mat")
disp(['File Name Passed to matlab_oneFile: ', fileName])
disp("Beginning of the parallel computation")
parfor i=1:3, c(:,i) = eig(rand(1000)); end
disp("End of the parallel computation")
ret= true;
end
#include <iostream>
#include <stdexcept>
#include <vector>
#include <string>
#include <mclmcrrt.h>
#include <libmatlabtest.h>
// Use thread_pool from : https://github.com/bshoshany/thread-pool
#include "thread_pool.hpp"
void compute_parFor(const std::string& data) {
mwArray dataArray(data.c_str());
mwArray retType;
matlab_singleFile_parloop(1, retType, dataArray);
std::cout << "Got ret type : " << retType.Get(1, 1) << std::endl;
}
void compute(const std::string& data) {
mwArray FileNameArray(data.c_str());
mwArray retType;
matlab_singleFile(1, retType, FileNameArray);
std::cout << "Got ret type : " << retType.Get(1, 1) << std::endl;
}
int main(int argc, char** argv) {
std::vector<const char*> args{"-nojvm"};
bool initApp = mclInitializeApplication(args.data(), args.size());
if (initApp == false)
{
throw std::runtime_error("could not initialize MATLAB Runtime: " +
std::string(mclGetLastErrorMessage()));
}
std::cout << "mclInitializeApplication done" << std::endl;
bool initLib = libmatlabtestInitialize();
if (initLib == false)
throw std::runtime_error("could not initialize library: " +
std::string(mclGetLastErrorMessage()));
std::cout << "libMatlabTestInitialize done" << std::endl;
// 1_ Sequentially run a parallel Matlab Script (uses parfor)
compute_parFor("data1");
// 2_ run via thread pool : multiTreading a sequential Matlab Script
std::vector<std::string> data2matlab{"data2","data3","data4"};
{
thread_pool pool;
for (size_t i = 0; i < data2matlab.size(); i++)
{
pool.push_task( [&,i] { compute(data2matlab[i]); } );
}
}
libmatlabtestTerminate();
mclTerminateApplication();
return 0;
}

Risposte (0)

Categorie

Scopri di più su MATLAB Compiler SDK in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by