Benny K
Benny K

Reputation: 2087

Are "thread-safe" functions parallelization framework dependent?

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?

  1. Do two separate dlls (loaded by exe file) can call this function simultaneously?

Upvotes: 0

Views: 79

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275760

Yes and no.

No, because next to nobody writes out good guarantees for this kind of thing. This is a wonderful podcast about NVidia using C++'s threading model to finally formalize a threading system, with the complaint that prior to C++'s attempts most abstract threading/memory models out there where crap. At best you'd get very arcane and specific information about a specific set of hardware, or claims that you worked like some other (usually more ancient) hardware.

Except yes, because in practice, the kinds of mutexes and synchronization and the like they are using are going to be compatible on pretty much every desktop system. Maybe less so on mobile computing CPUs and other more obscure systems.

Upvotes: 1

Related Questions