I have this simple MATLAB code which calls imregionalmax in a parallel loop over 256 pathces of 16X16 pixels.
function y = Parimregionalmax(x)
assert(isa(x,'uint8'));
assert( all(size(x)==[16 16 256]) );
y = zeros(size(x));
parfor k = 1:size(x,3)
y(:,:,k) = imregionalmax(x(:,:,k));
end
end
Giving this code to MATLAB's Coder gives the following C code:
/*
* Parimregionalmax.c
*
* Code generation for function 'Parimregionalmax'
*
*/
/* Include files */
#include "rt_nonfinite.h"
#include "Parimregionalmax.h"
#include "libmwimregionalmax.h"
/* Function Definitions */
void Parimregionalmax(const unsigned char x[65536], double y[65536])
{
int k;
int i0;
int i1;
double imSize[2];
unsigned char varargin_1[256];
boolean_T conn[9];//unsigned char
double connSize[2];
boolean_T BW[256];
#pragma omp parallel for \
num_threads(omp_get_max_threads()) \
private(i0,i1) \
firstprivate(varargin_1,imSize,conn,connSize,BW)
for (k = 0; k < 256; k++) {
for (i0 = 0; i0 < 16; i0++) {
for (i1 = 0; i1 < 16; i1++) {
varargin_1[i1 + (i0 << 4)] = x[(i1 + (i0 << 4)) + (k << 8)];
}
}
for (i0 = 0; i0 < 2; i0++) {
imSize[i0] = 16.0;
}
for (i0 = 0; i0 < 9; i0++) {
conn[i0] = true;
}
for (i0 = 0; i0 < 2; i0++) {
connSize[i0] = 3.0;
}
imregionalmax_uint8(varargin_1, BW, 2.0, imSize, conn, 2.0, connSize);
for (i0 = 0; i0 < 16; i0++) {
for (i1 = 0; i1 < 16; i1++) {
y[(i1 + (i0 << 4)) + (k << 8)] = BW[i1 + (i0 << 4)];
}
}
}
}
/* End of code generation (Parimregionalmax.c) */
As you can see, the generated code (which works perfectly) uses openmp parallelization framework and calls the function imregionalmax_uint8 which is implemented in a separate "black-box" dll (with corresponding header and lib files).
Can I assume that this fuction is always thread safe no matter which parallelization framework I use?
Examples:
1.Is it OK to define two threads using C++ <thread> library in a console application (a.k.a exe file in VS) and call from each thread to imregionalmax_uint8?
- Do two separate dlls (loaded by exe file) can call this function simultaneously?
Aucun commentaire:
Enregistrer un commentaire