~ubuntu-branches/ubuntu/maverick/openturns/maverick

« back to all changes in this revision

Viewing changes to lib/src/Base/Func/WrapperObject.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2008-11-18 06:32:22 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20081118063222-pa0qncclrerrqkg2
Tags: 0.12.2-1
* New upstream release
* Bug fix: "New upstream release available (0.12.2)", thanks to Jerome
  Robert (Closes: #506005).
* Applied patch by J. Robert.
* debian/control: build-depends on libxml2

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21
21
 *
22
22
 *  @author: $LastChangedBy: dutka $
23
 
 *  @date:   $LastChangedDate: 2008-06-26 13:50:17 +0200 (jeu, 26 jun 2008) $
24
 
 *  Id:      $Id: WrapperObject.cxx 862 2008-06-26 11:50:17Z dutka $
 
23
 *  @date:   $LastChangedDate: 2008-10-29 18:54:45 +0100 (mer 29 oct 2008) $
 
24
 *  Id:      $Id: WrapperObject.cxx 990 2008-10-29 17:54:45Z dutka $
25
25
 */
26
 
#include <dlfcn.h>               // for dlopen, dlsym, dlclose, dlerror
 
26
#include <sstream>               // for std::istringstream
27
27
#include "WrapperObject.hxx"
28
28
#include "LibraryLoader.hxx"
 
29
#include "OSS.hxx"
29
30
#include "Log.hxx"
30
31
#include "ResourceMap.hxx"
31
32
#include "WrapperCommon.h"
32
 
 
33
 
#if GCC_VERSION <= 30406 /* GCC 3.4.6 and below */
34
 
#define REINTERPRET_CAST(T,V) ( (T) (V) )
35
 
 
36
 
#else /* GCC after 3.4.6 */
37
 
#define REINTERPRET_CAST(T,V) ( reinterpret_cast< T > (V) )
38
 
 
39
 
#endif
 
33
#include "WrapperCommonFunctions.hxx"
40
34
 
41
35
namespace OpenTURNS
42
36
{
46
40
 
47
41
    namespace Func
48
42
    {
49
 
      typedef Common::DynamicLibraryException DynamicLibraryException;
50
 
      typedef Common::Log                     Log;
 
43
      using Common::DynamicLibraryException;
 
44
      using Common::Log;
51
45
      
52
46
      CLASSNAMEINIT(WrapperObject);
53
47
 
85
79
 
86
80
      static enum WrapperErrorCode optionalWrapperFunction(...)
87
81
      {
 
82
        const char FUNCTIONNAME[] = "optionalWrapperFunction";
 
83
        printEntrance(FUNCTIONNAME);
 
84
        printExit(FUNCTIONNAME);
 
85
 
 
86
        return WRAPPER_OK;
 
87
      }
 
88
 
 
89
      static enum WrapperErrorCode defaultWrapperGetInfoFunction(void * p_state,
 
90
                                                                 struct WrapperInformation * p_info)
 
91
      {
 
92
        const char FUNCTIONNAME[] = "defaultWrapperGetInfoFunction";
 
93
        printEntrance(FUNCTIONNAME);
 
94
        printState(FUNCTIONNAME, p_state);
 
95
 
 
96
        if (!p_state) return WRAPPER_CANNOT_PROVIDE_INFORMATION;
 
97
 
 
98
        struct WrapperExchangedData * p_exchangedData = REINTERPRET_CAST( struct WrapperExchangedData *, p_state );
 
99
        if (!p_exchangedData) return WRAPPER_CANNOT_PROVIDE_INFORMATION;
 
100
 
 
101
        p_info->inSize_  = getNumberOfVariables(p_exchangedData, WRAPPER_IN);
 
102
        p_info->outSize_ = getNumberOfVariables(p_exchangedData, WRAPPER_OUT);
 
103
 
 
104
        printWrapperInformation(FUNCTIONNAME, p_info);
 
105
        printExit(FUNCTIONNAME);
 
106
 
 
107
        return WRAPPER_OK;
 
108
      }
 
109
 
 
110
      static enum WrapperErrorCode defaultWrapperCreateStateFunction(void ** p_p_state,
 
111
                                                                     const struct WrapperExchangedData * p_exchangedData)
 
112
      {
 
113
        const char FUNCTIONNAME[] = "defaultWrapperCreateStateFunction";
 
114
        printEntrance(FUNCTIONNAME);
 
115
        if (!p_p_state) return WRAPPER_CANNOT_CREATE_STATE;
 
116
        printState(FUNCTIONNAME, *p_p_state);
 
117
 
 
118
        if (!p_exchangedData) return WRAPPER_INTERNAL_ERROR;
 
119
        printWrapperExchangedData(FUNCTIONNAME, p_exchangedData);
 
120
 
 
121
        struct WrapperExchangedData ** p_p_new_exchangedData = REINTERPRET_CAST( struct WrapperExchangedData **, p_p_state );
 
122
        if ( copyWrapperExchangedData( p_p_new_exchangedData, p_exchangedData) )
 
123
          return WRAPPER_CANNOT_CREATE_STATE;
 
124
 
 
125
        printExit(FUNCTIONNAME);
 
126
 
 
127
        return WRAPPER_OK;
 
128
      }
 
129
 
 
130
      static enum WrapperErrorCode defaultWrapperDeleteStateFunction(void * p_state)
 
131
      {
 
132
        const char FUNCTIONNAME[] = "defaultWrapperDeleteStateFunction";
 
133
        printEntrance(FUNCTIONNAME);
 
134
 
 
135
        freeWrapperExchangedData( static_cast<struct WrapperExchangedData *>( p_state ) );
 
136
 
 
137
        printState(FUNCTIONNAME, p_state);
 
138
        printExit(FUNCTIONNAME);
 
139
 
88
140
        return WRAPPER_OK;
89
141
      }
90
142
 
91
143
      static enum WrapperErrorCode notDefinedFunction(...)
92
144
      {
 
145
        const char FUNCTIONNAME[] = "notDefinedFunction";
 
146
        printEntrance(FUNCTIONNAME);
 
147
        printExit(FUNCTIONNAME);
93
148
        return WRAPPER_NOT_IMPLEMENTED;
94
149
      }
95
150
 
117
172
          hessSymbol_(0),
118
173
          finalizeSymbol_(0),
119
174
          stateCreationSymbol_(0),
120
 
          stateDeletionSymbol_(0)
 
175
          stateDeletionSymbol_(0),
 
176
          wrapperInfo_(0)
121
177
      {
122
178
        if (symbolName.empty()) {
123
179
          String name;
131
187
        }
132
188
 
133
189
        const Bool optional = true;
134
 
        const LibrarySymbol optWrpFunc = REINTERPRET_CAST(LibrarySymbol, &optionalWrapperFunction);
135
 
        const LibrarySymbol notDefFunc = REINTERPRET_CAST(LibrarySymbol, &notDefinedFunction);
136
 
        getInfoSymbol_       = REINTERPRET_CAST(GetWrapperInformationFunctionPointer, getSymbol(getFunctionName(INFORMATION)));
137
 
        stateCreationSymbol_ = REINTERPRET_CAST(StateCreationFunctionPointer, getSymbol(getFunctionName(STATE_CREATION), optional, optWrpFunc));
138
 
        stateDeletionSymbol_ = REINTERPRET_CAST(StateDeletionFunctionPointer, getSymbol(getFunctionName(STATE_DELETION), optional, optWrpFunc));
139
 
        initSymbol_          = REINTERPRET_CAST(InitializationFunctionPointer, getSymbol(getFunctionName(INITIALIZATION), optional, optWrpFunc));
140
 
        finalizeSymbol_      = REINTERPRET_CAST(FinalizationFunctionPointer, getSymbol(getFunctionName(FINALIZATION), optional, optWrpFunc));
141
 
        execSymbol_          = REINTERPRET_CAST(ExecutionFunctionPointer, getSymbol(getFunctionName(EXECUTION)));
142
 
        gradSymbol_          = REINTERPRET_CAST(GradientFunctionPointer, getSymbol(getFunctionName(EXECUTION)));
143
 
        hessSymbol_          = REINTERPRET_CAST(HessianFunctionPointer, getSymbol(getFunctionName(EXECUTION)));
144
 
        execSampleSymbol_    = REINTERPRET_CAST(ExecutionSampleFunctionPointer, getSymbol(getFunctionName(EXECUTION_SAMPLE), optional, notDefFunc));
 
190
        const LibrarySymbol optWrpFunc = REINTERPRET_CAST( LibrarySymbol, &optionalWrapperFunction           );
 
191
        const LibrarySymbol defWrpFunc = REINTERPRET_CAST( LibrarySymbol, &defaultWrapperGetInfoFunction     );
 
192
        const LibrarySymbol notDefFunc = REINTERPRET_CAST( LibrarySymbol, &notDefinedFunction                );
 
193
        const LibrarySymbol createFunc = REINTERPRET_CAST( LibrarySymbol, &defaultWrapperCreateStateFunction );
 
194
        const LibrarySymbol deleteFunc = REINTERPRET_CAST( LibrarySymbol, &defaultWrapperDeleteStateFunction );
 
195
 
 
196
 
 
197
        getInfoSymbol_       = REINTERPRET_CAST(GetWrapperInformationFunctionPointer,
 
198
                                                getSymbol(getFunctionName(INFORMATION), optional, defWrpFunc) );
 
199
        stateCreationSymbol_ = REINTERPRET_CAST(StateCreationFunctionPointer,
 
200
                                                getSymbol(getFunctionName(STATE_CREATION), optional, createFunc) );
 
201
        stateDeletionSymbol_ = REINTERPRET_CAST(StateDeletionFunctionPointer,
 
202
                                                getSymbol(getFunctionName(STATE_DELETION), optional, deleteFunc) );
 
203
        initSymbol_          = REINTERPRET_CAST(InitializationFunctionPointer,
 
204
                                                getSymbol(getFunctionName(INITIALIZATION), optional, optWrpFunc) );
 
205
        finalizeSymbol_      = REINTERPRET_CAST(FinalizationFunctionPointer,
 
206
                                                getSymbol(getFunctionName(FINALIZATION), optional, optWrpFunc) );
 
207
        execSymbol_          = REINTERPRET_CAST(ExecutionFunctionPointer,
 
208
                                                getSymbol(getFunctionName(EXECUTION))) ;
 
209
        gradSymbol_          = REINTERPRET_CAST(GradientFunctionPointer,
 
210
                                                getSymbol(getFunctionName(EXECUTION)) );
 
211
        hessSymbol_          = REINTERPRET_CAST(HessianFunctionPointer,
 
212
                                                getSymbol(getFunctionName(EXECUTION)) );
 
213
        execSampleSymbol_    = REINTERPRET_CAST(ExecutionSampleFunctionPointer,
 
214
                                                getSymbol(getFunctionName(EXECUTION_SAMPLE), optional, notDefFunc) );
145
215
      }
146
216
 
147
217
      /* Virtual constructor */
181
251
      }
182
252
 
183
253
      /* Data accessor */
184
 
      const Common::WrapperData & WrapperObject::getWrapperData() const
 
254
      const WrapperData & WrapperObject::getWrapperData() const
185
255
      {
186
256
        return data_;
187
257
      }
192
262
      UnsignedLong WrapperObject::getInNumericalPointDimension(void * p_state) const
193
263
        throw (WrapperInternalException)
194
264
      {
195
 
        struct WrapperInformation functionInformation;
196
 
        WrapperErrorCode returnCode = (*getInfoSymbol_)( p_state, & functionInformation );
197
 
        if (returnCode != WRAPPER_OK) 
198
 
          throw WrapperInternalException(HERE)
199
 
            << "Wrapper function '" << getFunctionName(INFORMATION)
200
 
            << "' returned error message: " << getErrorAsString(returnCode);
201
 
        
202
 
        return functionInformation.inSize_;
 
265
        if (wrapperInfo_.isNull() ) {
 
266
          wrapperInfo_.reset(new WrapperInformation);
 
267
          WrapperErrorCode returnCode = (*getInfoSymbol_)( p_state, wrapperInfo_.get() );
 
268
          if (returnCode != WRAPPER_OK) 
 
269
            throw WrapperInternalException(HERE)
 
270
              << "Wrapper function '" << getFunctionName(INFORMATION)
 
271
              << "' returned error message: " << getErrorAsString(returnCode);
 
272
        }
 
273
        return wrapperInfo_->inSize_;
203
274
      }
204
275
 
205
276
      /* Method getDescription returns the description of the input and output variables of the functions */
208
279
      {
209
280
        Description description;
210
281
        WrapperData::VariableListType variableList(data_.getVariableList());
211
 
        for (UnsignedLong i = 0; i < variableList.size(); i++) description.add(variableList[i].id_);
 
282
        for (UnsignedLong i = 0; i < variableList.getSize(); ++i) description.add(variableList[i].id_);
212
283
        return description;
213
284
      }
214
285
 
217
288
      UnsignedLong WrapperObject::getOutNumericalPointDimension(void * p_state) const
218
289
        throw (WrapperInternalException)
219
290
      {
220
 
        struct WrapperInformation functionInformation;
221
 
        WrapperErrorCode returnCode = (*getInfoSymbol_)( p_state, & functionInformation );
222
 
        if (returnCode != WRAPPER_OK) 
223
 
          throw WrapperInternalException(HERE)
224
 
            << "Wrapper function '" << getFunctionName(INFORMATION)
225
 
            << "' returned error message: " << getErrorAsString(returnCode);
226
 
        
227
 
        return functionInformation.outSize_;
 
291
        if (wrapperInfo_.isNull() ) {
 
292
          wrapperInfo_.reset(new WrapperInformation);
 
293
          WrapperErrorCode returnCode = (*getInfoSymbol_)( p_state, wrapperInfo_.get() );
 
294
          if (returnCode != WRAPPER_OK) 
 
295
            throw WrapperInternalException(HERE)
 
296
              << "Wrapper function '" << getFunctionName(INFORMATION)
 
297
              << "' returned error message: " << getErrorAsString(returnCode);
 
298
        }
 
299
        return wrapperInfo_->outSize_;
228
300
      }
229
301
 
230
302
 
267
339
        NumericalPoint out(outPoint.size_);
268
340
        outPoint.data_ = const_cast<NumericalScalar *>(&out[0]);
269
341
 
270
 
#ifdef DEBUG_5
 
342
#ifdef DEBUG
271
343
        {
272
 
          std::cerr << "DBG - In struct point {size= " << inPoint.size_ << ", data=[";
 
344
          OSS oss;
 
345
          oss << "In struct point {size= " << inPoint.size_ << ", data=[";
273
346
          const char * separator = "";
274
 
          for(unsigned int i=0; i < inPoint.size_; i++, separator=",") std::cerr << separator << inPoint.data_[i];
275
 
          std::cerr << "]}" << std::endl;       
 
347
          for(unsigned int i=0; i < inPoint.size_; i++, separator=",") oss << separator << inPoint.data_[i];
 
348
          oss << "]}";
 
349
          Log::Debug( oss );
276
350
        }
277
351
#endif
278
352
 
284
358
            << "' returned error message: " << getErrorAsString(returnCode);
285
359
        
286
360
 
287
 
#ifdef DEBUG_5
 
361
#ifdef DEBUG
288
362
        {
289
 
          std::cerr << "DBG - Out struct point {size= " << outPoint.size_ << ", data=[";
 
363
          OSS oss;
 
364
          oss << "Out struct point {size= " << outPoint.size_ << ", data=[";
290
365
          const char * separator = "";
291
 
          for(unsigned int i=0; i < outPoint.size_; i++, separator=",") std::cerr << separator << outPoint.data_[i];
292
 
          std::cerr << "]}" << std::endl;       
 
366
          for(unsigned int i=0; i < outPoint.size_; i++, separator=",") oss << separator << outPoint.data_[i];
 
367
          oss << "]}";
 
368
          Log::Debug( oss );
293
369
        }
294
370
#endif
295
371
        return out;
344
420
          {
345
421
            WrapperErrorCode returnCode = (* execSampleSymbol_)( p_state, & inSample, & outSample);
346
422
            if (returnCode == WRAPPER_NOT_IMPLEMENTED) {
347
 
              Log::Debug("DBG - Sample evaluation function NOT provided. Switching to point evaluation");
 
423
              Log::Info("Sample evaluation function NOT provided. Switching to point evaluation");
348
424
              switchToPointEvaluation = true;
349
425
 
350
426
            } else if (returnCode != WRAPPER_OK)
355
431
        // If it fails, try to do it by calling the wrapper execution function for a point
356
432
        catch(WrapperInternalException & ex)
357
433
          {
358
 
            Log::Debug("DBG - WrapperInternalException catched. Switching to point evaluation");
 
434
            Log::Info("Sample evaluation function returned an error. Switching to point evaluation");
359
435
            switchToPointEvaluation = true;
360
436
          }
361
437
        catch(DynamicLibraryException & ex)
362
438
          {
363
 
            Log::Debug("DBG - WrapperInternalException catched. Switching to point evaluation");
 
439
            Log::Info("Sample evaluation function failure. Switching to point evaluation");
364
440
            switchToPointEvaluation = true;
365
441
          }
366
442
        if(switchToPointEvaluation)
367
443
          {
368
 
            WrapperErrorCode returnCode;
 
444
            WrapperErrorCode returnCode = WRAPPER_OK;
369
445
            for(UnsignedLong i = 0; i < size; i++)
370
446
              {
371
447
                returnCode = (* execSymbol_)( p_state, & inSample.data_[i], & outSample.data_[i]);
407
483
        outMatrix.nbcols_ = getOutNumericalPointDimension(p_state);
408
484
        outMatrix.data_   = new double[outMatrix.nbrows_*outMatrix.nbcols_];
409
485
 
410
 
#ifdef DEBUG_5
 
486
#ifdef DEBUG
411
487
        {
412
 
          std::cerr << "DBG - In struct point {size= " << inPoint.size_ << ", data=[";
 
488
          OSS oss;
 
489
          oss << "In struct point {size= " << inPoint.size_ << ", data=[";
413
490
          const char * separator = "";
414
 
          for(unsigned int i=0; i < inPoint.size_; i++, separator=",") std::cerr << separator << inPoint.data_[i];
415
 
          std::cerr << "]}" << std::endl;       
 
491
          for(unsigned int i=0; i < inPoint.size_; i++, separator=",") oss << separator << inPoint.data_[i];
 
492
          oss << "]}";  
 
493
          Log::Debug( oss );
416
494
        }
417
495
#endif
418
496
 
426
504
 
427
505
 
428
506
 
429
 
#ifdef DEBUG_5
 
507
#ifdef DEBUG
430
508
        {
431
 
          std::cerr << "DBG - Out struct matrix {rows= " << outMatrix.nbrows_ 
 
509
          OSS oss;
 
510
          oss << "Out struct matrix {rows= " << outMatrix.nbrows_ 
432
511
                    << ", cols=" << outMatrix.nbcols_ << ", data=[[";
433
512
          const char * separator = "";
434
513
          for(unsigned int i=0; i < outMatrix.nbrows_; i++, separator="],[")
435
514
            for(unsigned int j=0; j < outMatrix.nbcols_; j++, separator=",")
436
 
              std::cerr << separator << outMatrix.data_[i*outMatrix.nbcols_ + j];
437
 
          std::cerr << "]]}" << std::endl;      
 
515
              oss << separator << outMatrix.data_[i*outMatrix.nbcols_ + j];
 
516
          oss << "]]}"; 
 
517
          Log::Debug( oss );
438
518
        }
439
519
#endif
440
520
 
473
553
        outTensor.nbsheets_ = getOutNumericalPointDimension(p_state);
474
554
        outTensor.data_     = new double[outTensor.nbrows_ * outTensor.nbcols_ * outTensor.nbsheets_];
475
555
 
476
 
#ifdef DEBUG_5
 
556
#ifdef DEBUG
477
557
        {
478
 
          std::cerr << "DBG - In struct point {size= " << inPoint.size_ << ", data=[";
 
558
          OSS oss;
 
559
          oss << "In struct point {size= " << inPoint.size_ << ", data=[";
479
560
          const char * separator = "";
480
 
          for(unsigned int i=0; i < inPoint.size_; i++, separator=",") std::cerr << separator << inPoint.data_[i];
481
 
          std::cerr << "]}" << std::endl;       
 
561
          for(unsigned int i=0; i < inPoint.size_; i++, separator=",") oss << separator << inPoint.data_[i];
 
562
          oss << "]}";  
 
563
          Log::Debug( oss );
482
564
        }
483
565
#endif
484
566
 
490
572
            << "' returned error message: " << getErrorAsString(returnCode);
491
573
 
492
574
 
493
 
#ifdef DEBUG_5
 
575
#ifdef DEBUG
494
576
        {
495
 
          std::cerr << "DBG - Out struct tensor {rows= " << outTensor.nbrows_ 
 
577
          OSS oss;
 
578
          oss << "Out struct tensor {rows= " << outTensor.nbrows_ 
496
579
                    << ", cols=" << outTensor.nbcols_ 
497
580
                    << ", sheets=" << outTensor.nbsheets_ 
498
581
                    << ", data=[[[";
500
583
          for(unsigned int i=0; i < outTensor.nbrows_; i++, separator="],[")
501
584
            for(unsigned int j=0; j < outTensor.nbcols_; j++, separator="],[")
502
585
              for(unsigned int k=0; k < outTensor.nbsheets_; k++, separator=",")
503
 
                std::cerr << separator << outTensor.data_[(i*outTensor.nbcols_ + j)*outTensor.nbsheets_ + k];
504
 
          std::cerr << "]]]}" << std::endl;
 
586
                oss << separator << outTensor.data_[(i*outTensor.nbcols_ + j)*outTensor.nbsheets_ + k];
 
587
          oss << "]]]}";
 
588
          Log::Debug( oss );
505
589
        }
506
590
#endif
507
591
        SymmetricTensor out(outTensor.nbrows_,
566
650
        exchangedData.platform_->separatorRegexpShortcut_ = new char[separatorRegexpShortcut.size()+1];
567
651
        strncpy(exchangedData.platform_->separatorRegexpShortcut_, separatorRegexpShortcut.c_str(), separatorRegexpShortcut.size()+1);
568
652
 
 
653
        String nbCPUs = Common::ResourceMap::GetInstance().get("parallel-threads");
 
654
        std::istringstream iss( nbCPUs );
 
655
        iss >> exchangedData.platform_->nbCpus_;
 
656
 
569
657
        WrapperErrorCode returnCode = (*stateCreationSymbol_)( & p_state, & exchangedData );
570
658
        if (returnCode != WRAPPER_OK) 
571
659
          throw WrapperInternalException(HERE)
615
703
 
616
704
        } catch (DynamicLibraryException & ex) {
617
705
          if (optional) {
618
 
            Log::Debug("DBG - DynamicLibraryException catched. Library symbol set as optionalWrapperFunction");
 
706
            Log::Info(OSS() << "Library symbol '" << name << "' linked to an internal function");
619
707
            return alternateFunction;
620
708
          } else throw WrapperInternalException(HERE) << ex.str();
621
709
        }