V_initMT
FunctionInitialize multi-processor optimized library
Syntax C/C++#include <VecLib.h>
int V_initMT( unsigned nProcCores );
Pascal/Delphiuses VecLib;
function V_initMT( nProcCores:UInt ): IntBool;
DescriptionThis function initializes the multi-processor libraries. It takes the number of processor cores as argument and creates the appropriate number of additional worker threads, so that subsequent OptiVec functions can distribute their work-load over the available processor cores. nProcCores can presently take on values of 2 up to 128. When Intel, AMD or others shall announce the development of systems with even more processor cores, future versions of OptiVec shall make provisions for as many cores as may become available.

On computers with many processor cores (from about 8 or 16 on), one might wish to limit the number of threads which each called OptiVec function may claim for itself. This is especially true, if an application generates several worker threads, and if each (or several) of them calls OptiVec functions. In the interest of a well-balanced use of the processor capacities, call V_limitThreadsPerFunc to set the number of threads available to each call of an OptiVec function. Be, however, rather generous: For example, with 16 cores and 4 application threads, give the OptiVec functions not only 16/4=4, but rather 8 threads each. The OS will take care of optimum scheduling of all these threads.

V_initMT must be called once (and only once), before any of the OptiVec routines present in the multi-processor libraries can be executed. On the other hand, calling V_initMT, when you are using a general-purpose (non-threaded) OptiVec library, does not harm. V_initMT is present as an empty function also in these libraries. Calling it just does not have any effect. Thereby, switching back and forth between the various versions of OptiVec libraries for testing purposes is facilitated.

In case you use V_setFPAccuracy in order to modify the floating-point accuracy of the FPU, be sure that the call to V_initMT stands after the call to V_setFPAccuracy. The reason is that V_initMT creates the additional threads with the same FPU settings as found at the moment when the function is invoked.

nProcCores need not be your actual number of processor cores. For testing purposes, you can enter any of the legal values. Of course, optimum performance will be attained only for the correct value. For applications distributed to others, where you do not have control over the configuration of the systems they will ultimately run on, it is recommended to determine nProcCores either through user input or more elegantly through a system detection routine. Consult the processor documentations by Intel and AMD for details.
Although it is not absolutely necessary, it is recommended that you delete the extra threads by calling V_closeMT at the end of your programme.
V_initMT stores the number of processor cores in the public variable V_nProcCores which is defined as (extern "C") unsigned in C/C++, and as UInt in Delphi. Reading this variable, one can determine if V_initMT has already been called.

MT in DLL'sUsing the multi-processor libraries with DLL's requires some additional thoughts:
  • DLL for distribution to third parties:
    If you do not have control over the main program (.exe) which shall invoke your DLL(s), each DLL you bind with the "M" library must have its own call to V_initMT in its initialization.
  • One or a few DLL's are invoked by your own main program (.exe):
    For simplicity and re-usability of the DLL's, you may proceed as above and call V_initMT in the initialization of each DLL. Then, each DLL will work with its own threads, and the Operating System usually will be able to optimize their scheduling.
  • Simultaneous use of several or many DLL's:
    If you have your own main program (.exe) which calls several or even many DLL's, all of which use OptiVec routines, you should think about keeping the number of threads under control. In this case, two strategies may be employed:
    Either one links the less time-critical DLL's with a "general-purpose" library of OptiVec, and only those DLL's, for which auto-threading really leads to a gain in performance, get linked with an "M" library.
    Or one installs a callback in each of the DLL's, so that the thread handler of the main program is also employed in the DLL's. That is the most efficient, however somewhat cumbersome solution. For that, one has to write and to export a function in each participating DLL:
    • C/C++:
      #include <VecLib.h>

      int V_DLLxxx_setThreadHandler( V_THREADHANDLERFUNC ThreadHandler )
      { return V_setThreadHandler( ThreadHandler ); }

      Here, "DLLxxx" must be a unique marker for each single DLL.

      In the main program then, one calls these functions one after the other for all DLL's:

      int main( ..... )
      {   ....
          V_initMT( nProcCores ); // Initialize threads in main program
          V_DLL001_setThreadHandler( V_threadHandler ); // Install callback in DLL 1
          V_DLL002_setThreadHandler( V_threadHandler ); // Install callback in DLL 2
          // etc. for all DLL's which use OptiVec functions
          ....
      }

    • Pascal/Delphi:
      uses VecLib.h;

      function V_DLLxxx_setThreadHandler( ThreadHandler:V_THREADHANDLERFUNC ): IntBool;
      begin V_DLLxxx_setThreadHandler := V_setThreadHandler( ThreadHandler ); end;

      Here, "DLLxxx" must be a unique marker for each single DLL.

      In the main program then, one calls these functions one after the other for all DLL's:

      begin   ....
          V_initMT( nProcCores ); // Initialize threads in main program
          V_DLL001_setThreadHandler( V_threadHandler ); // Install callback in DLL 1
          V_DLL002_setThreadHandler( V_threadHandler ); // Install callback in DLL 2
          // etc. for all DLL's which use OptiVec functions
          ....
      end.

Return valueFALSE (0), if the threads could be initialized error-free, otherwise TRUE (non-zero).
See alsoV_closeMT,  V_limitThreadsPerFunc,  V_setThreadHandler,  chap. 1.1.2.

VectorLib Table of Contents  OptiVec home