Page: 1 , 2 | Go to End | |
Author | Topic:RandomNumbersGenerator Plugin Development | 11917 Views |
12 February 2010 at 6:13pm
I want to develop a plugin in Visual C++ to use a NonRepeatable (stateless) random generator in Visual C++.
Does anybody have an example how this can be done ?
Thanks.
15 February 2010 at 9:42am
Hi!
There are two ways to define a plug-in in C++. The first way is to use the C++/CLI language extensions for c++ and define all the plug-in code and interface in the assembly. The second way is to define the plug-in interface in C# and the plug-in code in Ansi C++.
I post an example in C++/cli (maybe I will post the same example done in the other way, because it is portable to other platftoms)
So, if you are using Visual studio do the following:
1) creates a cli class library project
2) reference DVPLI.dll DVPLInterfaces.dll and Mono.Addins.dll from Fairmat directory
3) Basically, In order to create a random generator plugin, you have to create a class which implements DVPLI.IRandomNumbersGenerator interface and tell the engine to use it. See below an example of C++/CLI code
and in attachment the visual studio project.
4) Compile the code and copy the .dll file it in C:\Program Files\Fairmat Academic\Plugins
I you have other doubts does not esistate to ask,
Best regards,
Matteo
// RandomNumberGeneratorExample.h
#pragma once
using namespace System;
//Reference DVPLI.dll DVPLInterfaces.dll and Mono.Addins.dll
using namespace Mono::Addins;
using namespace DVPLI;
//the following lines tells the Plug-ins engine that the assemlby is a plug-in.
[assembly:Addin("Random Number generator example", "1.0", Category = "generator")];
[assembly: AddinDependency("Fairmat", "1.0")];
namespace RandomNumberGeneratorExample {
[Extension("/Fairmat/RandomNumbersGenerator")]
public ref class GeneratorTest: public IRandomNumbersGenerator
{
public:
virtual property IRandomVariablesGenerator^ Transformations
{
IRandomVariablesGenerator^ get()
{
return nullptr;
}
}
/// <summary>
/// initialize the random generator by using a integer
/// </summary>
/// <param name="p_Seed"></param>
virtual void InizializeRepeatable(int p_Seed)
{
}
/// <summary>
/// initialize the random generator by using
/// a time dependent
/// </summary>
virtual void InizializeNonRepeatable()
{
}
/// <summary>
/// Restore the state of the generator.
/// Note: first Save, must have been called.
/// </summary>
virtual void Restore()
{
}
/// <summary>
/// Save the state of the generator
/// </summary>
virtual void Save()
{
}
/// <summary>
/// restore with a given restore state
/// </summary>
/// <param name="restorePoint"></param>
virtual void RestoreState(IRandomGeneratorRestorePoint^restorePoint)
{
}
/// <summary>
/// Save and explicity return the restore point in order
/// to be saved externally to the class
/// </summary>
/// <returns></returns>
virtual IRandomGeneratorRestorePoint^ SaveState()
{
return nullptr;
}
/// <summary>
/// returns an uniform in (0,1) random draw
/// </summary>
/// <returns></returns>
virtual double Uniform()
{
//implement this;
return 0;
}
/// <summary>
/// returns an IID normal draw
/// </summary>
/// <returns></returns>
virtual double Normal()
{
//implement this;
return 0;
}
virtual void Normal(double* samples, int n)
{
//implement this;
}
//no need to implement this: will be removed from interface
virtual void Normal(array<double,2>^ samples)
{
}
//no need to implement this: will be removed from interface
virtual void Normal(array<double>^ samples)
{
}
virtual property ImplementationInfo ImplementationInfo
{
DVPLI::ImplementationInfo get()
{
DVPLI::ImplementationInfo info=DVPLI::ImplementationInfo(true,false);
return info;
}
}
};
/*
This class is needed to make the engine to use the new radnom generator...
*/
[Extension("/Fairmat/StartupOperation")]
public ref class Startup : public ICommand
{
public:
virtual void Execute()
{
//tell the engine to use this as random number generator
DVPLI::Engine::Generator =gcnew GeneratorTest();
}
};
}[/code:3nxv8bp7]
15 February 2010 at 11:57am
Thanks for the good example.
I have compiled the example and placed the .dll file in the directory C:\Program Files\Fairmat Academic\Plugins.
When I start Fairmat I don't get a message Fairmat has loaded this plugin.
When I look under Settings/Plugin Settings I don't see the .dll is loaded.
How can I verify that Faitmat is using this plugin ?
15 February 2010 at 12:59pm
Using the compiled .dll I have done an example Monte Carlo Simulation.
In the .dll I have not yet implemented the stateless random generator, but the stubvalues in your example. This should test if Fairmat is using the new random generator, although the results of the simulation are offcourse wrong. If I do this then I get the errors in the log which are included in this message at the end.
Can you correct the supplied code (possibly including different stubvalues for the randomgenerator) so these errors are corrected ?
Thanks.
Copy of the errors Fairmat gives when the .dll is included.
ERROR: There was an error while scanning assembly: C:\Program Files\Fairmat Academic\Plugins\RandomNumberGeneratorExample.dll (Could not load file or assembly 'RandomNumberGeneratorExample, Version=1.0.3698.21356, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
?ERROR: There was an error while scanning assembly: C:\Program Files\Fairmat Academic\Plugins\RandomNumberGeneratorExample.dll (Could not load file or assembly 'RandomNumberGeneratorExample, Version=1.0.3698.21356, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
Addin loadedFairmat Core library
Addin loadedFairmat Core Solver
Addin loadedMonte Carlo Simulation Solver
15 February 2010 at 3:42pm
At the moment, I'm manifesting a similar problem with the code I sent you. I never tried to use a C++/CLI plug-in, but we have developed some C# /ANSI C++ plug-ins. I can send you a stab using this technique.
I'm tried it in a 64 bit Windows, so maybe there is some problem linked to the architecture.
Which Windows and Fairmat version are you using?
15 February 2010 at 4:39pm
We are using Windows XP Professional 32bit and Fairmat version 0.9.10, revision 183.
We have compiled the .dll with Microsoft Visual C++ 2008.
15 February 2010 at 5:26pm
I tried it in a 32 bit machine. It works!, but I forgot to tell you that when you have to references DVPLI, DVPLInterfaces and Mono.Addins to set "Copy Local", "Copy Local Dependencies", "Copy Local S.A." and "Use dependencies in build process" to false.
Furthermore the only dll you have to copy in the Plugin directory is the Plugindllyoudeveloped.dll, because the DLL such as DVPLI.dll comes already with Fairmat.
Matteo
16 February 2010 at 7:13am
Thanks for the updates.
I have tried these updates and now Fairmat does not give errors anymore.
I have tried the program as you supplied it.
When I compare the results of the 4 examples which are given on your site using the standard and the plugin random generator I see no (significant) differences in the results of the monte carlo simulation.
This is strange because I suspected complete other results when random values are used which are completely not-random. Now I have doubts Fairmat is using the plugin random generator.
Is it posssible to update the supplied plugin with dummy values for the random generator and give a monte carlo simulation program to show the randomgenerator is used and the monte carlo simulation results are different from the results when the internal random generator is used ?
Thanks.
11917 Views | ||
Go to Top | Next > |
Currently Online: There is nobody online.
Welcome to our latest member: User4d51