20
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 $
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"
30
31
#include "ResourceMap.hxx"
31
32
#include "WrapperCommon.h"
33
#if GCC_VERSION <= 30406 /* GCC 3.4.6 and below */
34
#define REINTERPRET_CAST(T,V) ( (T) (V) )
36
#else /* GCC after 3.4.6 */
37
#define REINTERPRET_CAST(T,V) ( reinterpret_cast< T > (V) )
33
#include "WrapperCommonFunctions.hxx"
41
35
namespace OpenTURNS
86
80
static enum WrapperErrorCode optionalWrapperFunction(...)
82
const char FUNCTIONNAME[] = "optionalWrapperFunction";
83
printEntrance(FUNCTIONNAME);
84
printExit(FUNCTIONNAME);
89
static enum WrapperErrorCode defaultWrapperGetInfoFunction(void * p_state,
90
struct WrapperInformation * p_info)
92
const char FUNCTIONNAME[] = "defaultWrapperGetInfoFunction";
93
printEntrance(FUNCTIONNAME);
94
printState(FUNCTIONNAME, p_state);
96
if (!p_state) return WRAPPER_CANNOT_PROVIDE_INFORMATION;
98
struct WrapperExchangedData * p_exchangedData = REINTERPRET_CAST( struct WrapperExchangedData *, p_state );
99
if (!p_exchangedData) return WRAPPER_CANNOT_PROVIDE_INFORMATION;
101
p_info->inSize_ = getNumberOfVariables(p_exchangedData, WRAPPER_IN);
102
p_info->outSize_ = getNumberOfVariables(p_exchangedData, WRAPPER_OUT);
104
printWrapperInformation(FUNCTIONNAME, p_info);
105
printExit(FUNCTIONNAME);
110
static enum WrapperErrorCode defaultWrapperCreateStateFunction(void ** p_p_state,
111
const struct WrapperExchangedData * p_exchangedData)
113
const char FUNCTIONNAME[] = "defaultWrapperCreateStateFunction";
114
printEntrance(FUNCTIONNAME);
115
if (!p_p_state) return WRAPPER_CANNOT_CREATE_STATE;
116
printState(FUNCTIONNAME, *p_p_state);
118
if (!p_exchangedData) return WRAPPER_INTERNAL_ERROR;
119
printWrapperExchangedData(FUNCTIONNAME, p_exchangedData);
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;
125
printExit(FUNCTIONNAME);
130
static enum WrapperErrorCode defaultWrapperDeleteStateFunction(void * p_state)
132
const char FUNCTIONNAME[] = "defaultWrapperDeleteStateFunction";
133
printEntrance(FUNCTIONNAME);
135
freeWrapperExchangedData( static_cast<struct WrapperExchangedData *>( p_state ) );
137
printState(FUNCTIONNAME, p_state);
138
printExit(FUNCTIONNAME);
88
140
return WRAPPER_OK;
91
143
static enum WrapperErrorCode notDefinedFunction(...)
145
const char FUNCTIONNAME[] = "notDefinedFunction";
146
printEntrance(FUNCTIONNAME);
147
printExit(FUNCTIONNAME);
93
148
return WRAPPER_NOT_IMPLEMENTED;
133
189
const Bool optional = true;
134
const LibrarySymbol optWrpFunc = REINTERPRET_CAST(LibrarySymbol, &optionalWrapperFunction);
135
const LibrarySymbol notDefFunc = REINTERPRET_CAST(LibrarySymbol, ¬DefinedFunction);
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, ¬DefinedFunction );
193
const LibrarySymbol createFunc = REINTERPRET_CAST( LibrarySymbol, &defaultWrapperCreateStateFunction );
194
const LibrarySymbol deleteFunc = REINTERPRET_CAST( LibrarySymbol, &defaultWrapperDeleteStateFunction );
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) );
147
217
/* Virtual constructor */
192
262
UnsignedLong WrapperObject::getInNumericalPointDimension(void * p_state) const
193
263
throw (WrapperInternalException)
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);
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);
273
return wrapperInfo_->inSize_;
205
276
/* Method getDescription returns the description of the input and output variables of the functions */
217
288
UnsignedLong WrapperObject::getOutNumericalPointDimension(void * p_state) const
218
289
throw (WrapperInternalException)
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);
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);
299
return wrapperInfo_->outSize_;
267
339
NumericalPoint out(outPoint.size_);
268
340
outPoint.data_ = const_cast<NumericalScalar *>(&out[0]);
272
std::cerr << "DBG - In struct point {size= " << inPoint.size_ << ", data=[";
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];
284
358
<< "' returned error message: " << getErrorAsString(returnCode);
289
std::cerr << "DBG - Out struct point {size= " << outPoint.size_ << ", data=[";
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];
355
431
// If it fails, try to do it by calling the wrapper execution function for a point
356
432
catch(WrapperInternalException & ex)
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;
361
437
catch(DynamicLibraryException & ex)
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;
366
442
if(switchToPointEvaluation)
368
WrapperErrorCode returnCode;
444
WrapperErrorCode returnCode = WRAPPER_OK;
369
445
for(UnsignedLong i = 0; i < size; i++)
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_];
412
std::cerr << "DBG - In struct point {size= " << inPoint.size_ << ", data=[";
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];
431
std::cerr << "DBG - Out struct matrix {rows= " << outMatrix.nbrows_
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];
473
553
outTensor.nbsheets_ = getOutNumericalPointDimension(p_state);
474
554
outTensor.data_ = new double[outTensor.nbrows_ * outTensor.nbcols_ * outTensor.nbsheets_];
478
std::cerr << "DBG - In struct point {size= " << inPoint.size_ << ", data=[";
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];
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];
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);
653
String nbCPUs = Common::ResourceMap::GetInstance().get("parallel-threads");
654
std::istringstream iss( nbCPUs );
655
iss >> exchangedData.platform_->nbCpus_;
569
657
WrapperErrorCode returnCode = (*stateCreationSymbol_)( & p_state, & exchangedData );
570
658
if (returnCode != WRAPPER_OK)
571
659
throw WrapperInternalException(HERE)