<commit_before>// For conditions of distribution and use, see copyright notice in LICENSE

#include "StableHeaders.h"
#include "DebugOperatorNew.h"

#include "OgreRenderingModule.h"
#include "Renderer.h"
#include "EC_Placeable.h"
#include "EC_Mesh.h"
#include "EC_OgreCustomObject.h"
#include "EC_AnimationController.h"
#include "EC_Camera.h"
#include "EC_Light.h"
#include "EC_OgreCompositor.h"
#include "EC_RttTarget.h"
#include "EC_Material.h"
#include "OgreWorld.h"
#include "OgreMeshAsset.h"
#include "OgreParticleAsset.h"
#include "OgreSkeletonAsset.h"
#include "OgreMaterialAsset.h"
#include "OgreProfiler.h"
#ifdef OGRE_HAS_PROFILER_HOOKS
#include "OgreProfilerHook.h"
#endif
#include "TextureAsset.h"

#include "Application.h"
#include "Entity.h"
#include "Scene.h"
#include "AssetAPI.h"
#include "AssetCache.h"
#include "GenericAssetFactory.h"
#include "NullAssetFactory.h"
#include "Profiler.h"
#include "ConsoleAPI.h"
#include "SceneAPI.h"
#include "IComponentFactory.h"

#include "MemoryLeakCheck.h"

namespace OgreRenderer
{

std::string OgreRenderingModule::CACHE_RESOURCE_GROUP = "CACHED_ASSETS_GROUP";

#ifdef OGRE_HAS_PROFILER_HOOKS

#ifdef WIN32
DWORD mainThreadId;
#endif

void Profiler_BeginBlock(const char *name)
{
#if defined(PROFILING) && defined(WIN32)
    if (GetCurrentThreadId() != mainThreadId)
        return;
    Framework *fw = Framework::Instance();
    Profiler *p = fw ? fw->GetProfiler() : 0;
    if (p)
        p->StartBlock((std::string("OGRE_") + name).c_str());
#endif
}

void Profiler_EndBlock()
{
#if defined(PROFILING) && defined(WIN32)
    if (GetCurrentThreadId() != mainThreadId)
        return;
    Framework *fw = Framework::Instance();
    Profiler *p = fw ? fw->GetProfiler() : 0;
    if (p)
    {
        ProfilerNodeTree *treeNode = p->CurrentNode();
        if (!treeNode)
            return;
        p->EndBlock(treeNode->Name());
    }
#endif
}

#endif

OgreRenderingModule::OgreRenderingModule() :
    IModule("OgreRendering")
{
#ifdef OGRE_HAS_PROFILER_HOOKS
#ifdef WIN32
    mainThreadId = GetCurrentThreadId();
#endif
    OgreProfiler_BeginBlock = Profiler_BeginBlock;
    OgreProfiler_EndBlock = Profiler_EndBlock;
#endif
}

OgreRenderingModule::~OgreRenderingModule()
{
}

void OgreRenderingModule::Load()
{
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Placeable>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Mesh>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Light>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_OgreCustomObject>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_AnimationController>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Camera>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_OgreCompositor>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_RttTarget>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Material>));

    // Main ogre .mesh extension
    QStringList meshExtensions;
    meshExtensions << ".mesh";

    // These file types are supported by the Open Asset Import library:
    // http://assimp.sourceforge.net/main_features_formats.html
    /// @todo Detect during runtime if assimp module is loaded and only then add these types?
    meshExtensions << ".3d" << ".b3d" << ".dae" << ".bvh" << ".3ds" << ".ase" << ".obj" << ".ply" << ".dxf"
        << ".nff" << ".smd" << ".vta" << ".mdl" << ".md2" << ".md3" << ".mdc" << ".md5mesh" << ".x" << ".q3o"
        << ".q3s" << ".raw" << ".ac" << ".stl" << ".irrmesh" << ".irr" << ".off" << ".ter" << ".mdl" << ".hmp"
        << ".ms3d" << ".lwo" << ".lws" << ".lxo" << ".csm" << ".ply" << ".cob" << ".scn";

    // The following file types are from FreeImage's list of supported formats:
    // http://freeimage.sourceforge.net/features.html
    // The GIMP xcf is not in the list of known image formats, but detect it anyways.
    // Put the most common formats to the front as this list will be iterated to find asset types.
    QStringList textureExtensions;
    textureExtensions << ".dds" << ".png" << ".jpeg" << ".jpg" << ".gif" << ".bmp" << ".tga" << ".targa" << ".tiff" << ".tif"
        << ".cut" << ".exr" << ".g3"  << ".hdr" << ".ico" << ".iff" << ".j2k" << ".j2c" << ".jp2" << ".jif" << ".jpe" << ".jng"
        << ".koa" << ".lbm" << ".mng" << ".pbm" << ".pcd" << ".pcx" << ".pfm" << ".pict" << ".psd" << ".pgm" <<  ".ppm" << ".ras"
        << ".raw" << ".sgi" << ".wap" << ".wbmp" << ".wbm" << ".xbm" << ".xcf" << ".xpm";

    // Create asset type factories for each asset OgreRenderingModule provides to the system.
    framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreMeshAsset>("OgreMesh", meshExtensions)));

    // Loading materials crashes Ogre in headless mode because we don't have Ogre Renderer running, so only register the Ogre material asset type if not in headless mode.
    if (!framework_->IsHeadless())
    {
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreMaterialAsset>("OgreMaterial", ".material")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<TextureAsset>("Texture", textureExtensions)));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreParticleAsset>("OgreParticle", ".particle")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreSkeletonAsset>("OgreSkeleton", ".skeleton")));
    }
    else
    {
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("OgreMaterial", ".material")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("Texture", textureExtensions)));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("OgreParticle", ".particle")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("OgreSkeleton", ".skeleton")));
    }
}

void OgreRenderingModule::Initialize()
{
    std::string ogreConfigFilename = Application::InstallationDirectory().toStdString() + "ogre.cfg"; ///\todo Unicode support!
#if defined (_WINDOWS) && (_DEBUG)
    std::string pluginsFilename = "pluginsd.cfg";
#elif defined (_WINDOWS)
    std::string pluginsFilename = "plugins.cfg";
#elif defined(__APPLE__)
    std::string pluginsFilename = "plugins-mac.cfg";
#else
    std::string pluginsFilename = "plugins-unix.cfg";
#endif

    pluginsFilename = Application::InstallationDirectory().toStdString() + pluginsFilename; ///\todo Unicode support!

    std::string windowTitle = Application::FullIdentifier().toStdString();

    renderer = boost::make_shared<OgreRenderer::Renderer>(framework_, ogreConfigFilename, pluginsFilename, windowTitle);
    assert(renderer);
    assert(!renderer->IsInitialized());

    // Initializing the Renderer crashes inside Ogre if the current working directory is not the same as the directory where Ogre plugins reside in.
    // So, temporarily set the working dir to the installation directory, and restore it after succeeding to load the plugins.
    QString cwd = Application::CurrentWorkingDirectory();
    Application::SetCurrentWorkingDirectory(Application::InstallationDirectory());

    framework_->App()->SetSplashMessage("Initializing Ogre");
    renderer->Initialize();

    // Restore the original cwd to not disturb the environment we are running in.
    Application::SetCurrentWorkingDirectory(cwd);

    // Register renderer.
    framework_->RegisterRenderer(renderer.get());
    framework_->RegisterDynamicObject("renderer", renderer.get());

    // Connect to scene change signals.
    connect(framework_->Scene(), SIGNAL(SceneAdded(const QString&)), this, SLOT(OnSceneAdded(const QString&)));
    connect(framework_->Scene(), SIGNAL(SceneRemoved(const QString&)), this, SLOT(OnSceneRemoved(const QString&)));

    // Add asset cache directory as its own resource group to ogre to support threaded loading.
    if (GetFramework()->Asset()->GetAssetCache())
    {
        std::string cacheResourceDir = GetFramework()->Asset()->GetAssetCache()->CacheDirectory().toStdString();
        if (!Ogre::ResourceGroupManager::getSingleton().resourceLocationExists(cacheResourceDir, CACHE_RESOURCE_GROUP))
            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(cacheResourceDir, "FileSystem", CACHE_RESOURCE_GROUP);
    }

    framework_->Console()->RegisterCommand("renderStats", "Prints out render statistics.",
        this, SLOT(ConsoleStats()));
#if OGRE_PROFILING == 1
    framework_->Console()->RegisterCommand("ogreProf", "Toggles visibility of the Ogre profiler overlay.",
        this, SLOT(ToggleOgreProfilerOverlay()));
#endif
    framework_->Console()->RegisterCommand("setMaterialAttribute", "Sets an attribute on a material asset",
        this, SLOT(SetMaterialAttribute(const QStringList &)));
}

void OgreRenderingModule::Uninitialize()
{
    // We're shutting down. Force a release of all loaded asset objects from the Asset API so that
    // no refs to Ogre assets remain - below 'renderer.reset()' is going to delete Ogre::Root.
    framework_->Asset()->ForgetAllAssets();

    // Clear up the renderer object, so that it will not be left dangling.
    framework_->RegisterRenderer(0);
}

void OgreRenderingModule::ConsoleStats()
{
    if (framework_->IsHeadless())
        return;
    if (renderer)
    {
        const Ogre::RenderTarget::FrameStats& stats = renderer->GetCurrentRenderWindow()->getStatistics();
        ConsoleAPI *c = framework_->Console();
        c->Print("Average FPS: " + QString::number(stats.avgFPS));
        c->Print("Worst FPS: " + QString::number(stats.worstFPS));
        c->Print("Best FPS: " + QString::number(stats.bestFPS));
        c->Print("Triangles: " + QString::number(stats.triangleCount));
        c->Print("Batches: " + QString::number(stats.batchCount));
        return;
    }
    else
        LogError("No renderer found!");
}

void OgreRenderingModule::ToggleOgreProfilerOverlay()
{
#if OGRE_PROFILING == 1
    if (!framework_->IsHeadless())
    {
        bool enabled = Ogre::Profiler::getSingleton().getEnabled();
        Ogre::Profiler::getSingleton().setEnabled(!enabled);
    }
#else
    LogError("OgreRenderingModule::ToggleOgreProfilerOverlay: Ogre built without profiling support, cannot show profiler overlay.");
#endif
}

void OgreRenderingModule::OnSceneAdded(const QString& name)
{
    ScenePtr scene = GetFramework()->Scene()->GetScene(name);
    if (!scene)
    {
        LogError("OgreRenderingModule::OnSceneAdded: Could not find created scene");
        return;
    }

    // Add an OgreWorld to the scene
    OgreWorldPtr newWorld = boost::make_shared<OgreWorld>(renderer.get(), scene);
    renderer->ogreWorlds[scene.get()] = newWorld;
    scene->setProperty(OgreWorld::PropertyName(), QVariant::fromValue<QObject*>(newWorld.get()));
}

void OgreRenderingModule::OnSceneRemoved(const QString& name)
{
    // Remove the OgreWorld from the scene
    ScenePtr scene = GetFramework()->Scene()->GetScene(name);
    if (!scene)
    {
        LogError("OgreRenderingModule::OnSceneRemoved: Could not find scene about to be removed");
        return;
    }

    OgreWorld* worldPtr = scene->GetWorld<OgreWorld>().get();
    if (worldPtr)
    {
        scene->setProperty(OgreWorld::PropertyName(), QVariant());
        renderer->ogreWorlds.erase(scene.get());
    }
}

void OgreRenderingModule::SetMaterialAttribute(const QStringList &params)
{
    if (params.size() < 3)
    {
        LogError("OgreRenderingModule::SetMaterialAttribute: Usage: SetMaterialAttribute(asset,attribute,value)");
        return;
    }
    AssetPtr assetPtr = framework_->Asset()->GetAsset(framework_->Asset()->ResolveAssetRef("", params[0]));
    if (!assetPtr || !assetPtr->IsLoaded())
    {
        LogError("OgreRenderingModule::SetMaterialAttribute: No asset found or not loaded");
        return;
    }
    OgreMaterialAsset* matAsset = dynamic_cast<OgreMaterialAsset*>(assetPtr.get());
    if (!matAsset)
    {
        LogError("OgreRenderingModule::SetMaterialAttribute: Not a material asset");
        return;
    }
    matAsset->SetAttribute(params[1], params[2]);
}

} // ~namespace OgreRenderer

using namespace OgreRenderer;

extern "C"
{
DLLEXPORT void TundraPluginMain(Framework *fw)
{
    Framework::SetInstance(fw); // Inside this DLL, remember the pointer to the global framework object.
    fw->RegisterModule(new OgreRenderer::OgreRenderingModule());
}
}
<commit_msg>OgreRenderingModule: Add assimp ifdefs when adding supported mesh types, we dont want to add these if assimp is really not there. The current codebase tries to parse the files as an Ogre binary mesh and will fail/crash.<commit_after>// For conditions of distribution and use, see copyright notice in LICENSE

#include "StableHeaders.h"
#include "DebugOperatorNew.h"

#include "OgreRenderingModule.h"
#include "Renderer.h"
#include "EC_Placeable.h"
#include "EC_Mesh.h"
#include "EC_OgreCustomObject.h"
#include "EC_AnimationController.h"
#include "EC_Camera.h"
#include "EC_Light.h"
#include "EC_OgreCompositor.h"
#include "EC_RttTarget.h"
#include "EC_Material.h"
#include "OgreWorld.h"
#include "OgreMeshAsset.h"
#include "OgreParticleAsset.h"
#include "OgreSkeletonAsset.h"
#include "OgreMaterialAsset.h"
#include "OgreProfiler.h"
#ifdef OGRE_HAS_PROFILER_HOOKS
#include "OgreProfilerHook.h"
#endif
#include "TextureAsset.h"

#include "Application.h"
#include "Entity.h"
#include "Scene.h"
#include "AssetAPI.h"
#include "AssetCache.h"
#include "GenericAssetFactory.h"
#include "NullAssetFactory.h"
#include "Profiler.h"
#include "ConsoleAPI.h"
#include "SceneAPI.h"
#include "IComponentFactory.h"

#include "MemoryLeakCheck.h"

namespace OgreRenderer
{

std::string OgreRenderingModule::CACHE_RESOURCE_GROUP = "CACHED_ASSETS_GROUP";

#ifdef OGRE_HAS_PROFILER_HOOKS

#ifdef WIN32
DWORD mainThreadId;
#endif

void Profiler_BeginBlock(const char *name)
{
#if defined(PROFILING) && defined(WIN32)
    if (GetCurrentThreadId() != mainThreadId)
        return;
    Framework *fw = Framework::Instance();
    Profiler *p = fw ? fw->GetProfiler() : 0;
    if (p)
        p->StartBlock((std::string("OGRE_") + name).c_str());
#endif
}

void Profiler_EndBlock()
{
#if defined(PROFILING) && defined(WIN32)
    if (GetCurrentThreadId() != mainThreadId)
        return;
    Framework *fw = Framework::Instance();
    Profiler *p = fw ? fw->GetProfiler() : 0;
    if (p)
    {
        ProfilerNodeTree *treeNode = p->CurrentNode();
        if (!treeNode)
            return;
        p->EndBlock(treeNode->Name());
    }
#endif
}

#endif

OgreRenderingModule::OgreRenderingModule() :
    IModule("OgreRendering")
{
#ifdef OGRE_HAS_PROFILER_HOOKS
#ifdef WIN32
    mainThreadId = GetCurrentThreadId();
#endif
    OgreProfiler_BeginBlock = Profiler_BeginBlock;
    OgreProfiler_EndBlock = Profiler_EndBlock;
#endif
}

OgreRenderingModule::~OgreRenderingModule()
{
}

void OgreRenderingModule::Load()
{
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Placeable>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Mesh>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Light>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_OgreCustomObject>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_AnimationController>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Camera>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_OgreCompositor>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_RttTarget>));
    framework_->Scene()->RegisterComponentFactory(ComponentFactoryPtr(new GenericComponentFactory<EC_Material>));

    // Main ogre .mesh extension
    QStringList meshExtensions;
    meshExtensions << ".mesh";

    // These file types are supported by the Open Asset Import library:
    // http://assimp.sourceforge.net/main_features_formats.html
#ifdef ASSIMP_ENABLED
    meshExtensions << ".3d" << ".b3d" << ".dae" << ".bvh" << ".3ds" << ".ase" << ".obj" << ".ply" << ".dxf"
        << ".nff" << ".smd" << ".vta" << ".mdl" << ".md2" << ".md3" << ".mdc" << ".md5mesh" << ".x" << ".q3o"
        << ".q3s" << ".raw" << ".ac" << ".stl" << ".irrmesh" << ".irr" << ".off" << ".ter" << ".mdl" << ".hmp"
        << ".ms3d" << ".lwo" << ".lws" << ".lxo" << ".csm" << ".ply" << ".cob" << ".scn";
#endif

    // The following file types are from FreeImage's list of supported formats:
    // http://freeimage.sourceforge.net/features.html
    // The GIMP xcf is not in the list of known image formats, but detect it anyways.
    // Put the most common formats to the front as this list will be iterated to find asset types.
    QStringList textureExtensions;
    textureExtensions << ".dds" << ".png" << ".jpeg" << ".jpg" << ".gif" << ".bmp" << ".tga" << ".targa" << ".tiff" << ".tif"
        << ".cut" << ".exr" << ".g3"  << ".hdr" << ".ico" << ".iff" << ".j2k" << ".j2c" << ".jp2" << ".jif" << ".jpe" << ".jng"
        << ".koa" << ".lbm" << ".mng" << ".pbm" << ".pcd" << ".pcx" << ".pfm" << ".pict" << ".psd" << ".pgm" <<  ".ppm" << ".ras"
        << ".raw" << ".sgi" << ".wap" << ".wbmp" << ".wbm" << ".xbm" << ".xcf" << ".xpm";

    // Create asset type factories for each asset OgreRenderingModule provides to the system.
    framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreMeshAsset>("OgreMesh", meshExtensions)));

    // Loading materials crashes Ogre in headless mode because we don't have Ogre Renderer running, so only register the Ogre material asset type if not in headless mode.
    if (!framework_->IsHeadless())
    {
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreMaterialAsset>("OgreMaterial", ".material")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<TextureAsset>("Texture", textureExtensions)));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreParticleAsset>("OgreParticle", ".particle")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new GenericAssetFactory<OgreSkeletonAsset>("OgreSkeleton", ".skeleton")));
    }
    else
    {
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("OgreMaterial", ".material")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("Texture", textureExtensions)));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("OgreParticle", ".particle")));
        framework_->Asset()->RegisterAssetTypeFactory(AssetTypeFactoryPtr(new NullAssetFactory("OgreSkeleton", ".skeleton")));
    }
}

void OgreRenderingModule::Initialize()
{
    std::string ogreConfigFilename = Application::InstallationDirectory().toStdString() + "ogre.cfg"; ///\todo Unicode support!
#if defined (_WINDOWS) && (_DEBUG)
    std::string pluginsFilename = "pluginsd.cfg";
#elif defined (_WINDOWS)
    std::string pluginsFilename = "plugins.cfg";
#elif defined(__APPLE__)
    std::string pluginsFilename = "plugins-mac.cfg";
#else
    std::string pluginsFilename = "plugins-unix.cfg";
#endif

    pluginsFilename = Application::InstallationDirectory().toStdString() + pluginsFilename; ///\todo Unicode support!

    std::string windowTitle = Application::FullIdentifier().toStdString();

    renderer = boost::make_shared<OgreRenderer::Renderer>(framework_, ogreConfigFilename, pluginsFilename, windowTitle);
    assert(renderer);
    assert(!renderer->IsInitialized());

    // Initializing the Renderer crashes inside Ogre if the current working directory is not the same as the directory where Ogre plugins reside in.
    // So, temporarily set the working dir to the installation directory, and restore it after succeeding to load the plugins.
    QString cwd = Application::CurrentWorkingDirectory();
    Application::SetCurrentWorkingDirectory(Application::InstallationDirectory());

    framework_->App()->SetSplashMessage("Initializing Ogre");
    renderer->Initialize();

    // Restore the original cwd to not disturb the environment we are running in.
    Application::SetCurrentWorkingDirectory(cwd);

    // Register renderer.
    framework_->RegisterRenderer(renderer.get());
    framework_->RegisterDynamicObject("renderer", renderer.get());

    // Connect to scene change signals.
    connect(framework_->Scene(), SIGNAL(SceneAdded(const QString&)), this, SLOT(OnSceneAdded(const QString&)));
    connect(framework_->Scene(), SIGNAL(SceneRemoved(const QString&)), this, SLOT(OnSceneRemoved(const QString&)));

    // Add asset cache directory as its own resource group to ogre to support threaded loading.
    if (GetFramework()->Asset()->GetAssetCache())
    {
        std::string cacheResourceDir = GetFramework()->Asset()->GetAssetCache()->CacheDirectory().toStdString();
        if (!Ogre::ResourceGroupManager::getSingleton().resourceLocationExists(cacheResourceDir, CACHE_RESOURCE_GROUP))
            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(cacheResourceDir, "FileSystem", CACHE_RESOURCE_GROUP);
    }

    framework_->Console()->RegisterCommand("renderStats", "Prints out render statistics.",
        this, SLOT(ConsoleStats()));
#if OGRE_PROFILING == 1
    framework_->Console()->RegisterCommand("ogreProf", "Toggles visibility of the Ogre profiler overlay.",
        this, SLOT(ToggleOgreProfilerOverlay()));
#endif
    framework_->Console()->RegisterCommand("setMaterialAttribute", "Sets an attribute on a material asset",
        this, SLOT(SetMaterialAttribute(const QStringList &)));
}

void OgreRenderingModule::Uninitialize()
{
    // We're shutting down. Force a release of all loaded asset objects from the Asset API so that
    // no refs to Ogre assets remain - below 'renderer.reset()' is going to delete Ogre::Root.
    framework_->Asset()->ForgetAllAssets();

    // Clear up the renderer object, so that it will not be left dangling.
    framework_->RegisterRenderer(0);
}

void OgreRenderingModule::ConsoleStats()
{
    if (framework_->IsHeadless())
        return;
    if (renderer)
    {
        const Ogre::RenderTarget::FrameStats& stats = renderer->GetCurrentRenderWindow()->getStatistics();
        ConsoleAPI *c = framework_->Console();
        c->Print("Average FPS: " + QString::number(stats.avgFPS));
        c->Print("Worst FPS: " + QString::number(stats.worstFPS));
        c->Print("Best FPS: " + QString::number(stats.bestFPS));
        c->Print("Triangles: " + QString::number(stats.triangleCount));
        c->Print("Batches: " + QString::number(stats.batchCount));
        return;
    }
    else
        LogError("No renderer found!");
}

void OgreRenderingModule::ToggleOgreProfilerOverlay()
{
#if OGRE_PROFILING == 1
    if (!framework_->IsHeadless())
    {
        bool enabled = Ogre::Profiler::getSingleton().getEnabled();
        Ogre::Profiler::getSingleton().setEnabled(!enabled);
    }
#else
    LogError("OgreRenderingModule::ToggleOgreProfilerOverlay: Ogre built without profiling support, cannot show profiler overlay.");
#endif
}

void OgreRenderingModule::OnSceneAdded(const QString& name)
{
    ScenePtr scene = GetFramework()->Scene()->GetScene(name);
    if (!scene)
    {
        LogError("OgreRenderingModule::OnSceneAdded: Could not find created scene");
        return;
    }

    // Add an OgreWorld to the scene
    OgreWorldPtr newWorld = boost::make_shared<OgreWorld>(renderer.get(), scene);
    renderer->ogreWorlds[scene.get()] = newWorld;
    scene->setProperty(OgreWorld::PropertyName(), QVariant::fromValue<QObject*>(newWorld.get()));
}

void OgreRenderingModule::OnSceneRemoved(const QString& name)
{
    // Remove the OgreWorld from the scene
    ScenePtr scene = GetFramework()->Scene()->GetScene(name);
    if (!scene)
    {
        LogError("OgreRenderingModule::OnSceneRemoved: Could not find scene about to be removed");
        return;
    }

    OgreWorld* worldPtr = scene->GetWorld<OgreWorld>().get();
    if (worldPtr)
    {
        scene->setProperty(OgreWorld::PropertyName(), QVariant());
        renderer->ogreWorlds.erase(scene.get());
    }
}

void OgreRenderingModule::SetMaterialAttribute(const QStringList &params)
{
    if (params.size() < 3)
    {
        LogError("OgreRenderingModule::SetMaterialAttribute: Usage: SetMaterialAttribute(asset,attribute,value)");
        return;
    }
    AssetPtr assetPtr = framework_->Asset()->GetAsset(framework_->Asset()->ResolveAssetRef("", params[0]));
    if (!assetPtr || !assetPtr->IsLoaded())
    {
        LogError("OgreRenderingModule::SetMaterialAttribute: No asset found or not loaded");
        return;
    }
    OgreMaterialAsset* matAsset = dynamic_cast<OgreMaterialAsset*>(assetPtr.get());
    if (!matAsset)
    {
        LogError("OgreRenderingModule::SetMaterialAttribute: Not a material asset");
        return;
    }
    matAsset->SetAttribute(params[1], params[2]);
}

} // ~namespace OgreRenderer

using namespace OgreRenderer;

extern "C"
{
DLLEXPORT void TundraPluginMain(Framework *fw)
{
    Framework::SetInstance(fw); // Inside this DLL, remember the pointer to the global framework object.
    fw->RegisterModule(new OgreRenderer::OgreRenderingModule());
}
}
<|endoftext|>


<commit_before>/*
 * =====================================================================================
 *
 *       Filename:  run_levc.cpp
 *
 *    Description:  Compute levenshtein distance (main)
 *
 *        Version:  1.0
 *        Created:  09/07/2015 21:21:41
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Eugene Scherba (es), escherba@gmail.com
 *   Organization:  -
 *
 * =====================================================================================
 */

#include <iostream>
#include <fstream>
#include <sstream>
#include "metrohash64.h"

inline uint64_t bytes2int64(uint8_t * const array)
{
    // uint64_t is guaranteed to be 8 bytes long
    return (uint64_t)(
              static_cast<uint64_t>(array[0])
            | static_cast<uint64_t>(array[1]) << 8
            | static_cast<uint64_t>(array[2]) << 16
            | static_cast<uint64_t>(array[3]) << 24
            | static_cast<uint64_t>(array[4]) << 32
            | static_cast<uint64_t>(array[5]) << 40
            | static_cast<uint64_t>(array[6]) << 48
            | static_cast<uint64_t>(array[7]) << 56);
}

uint64_t metrohash64(const uint8_t * buffer, const uint64_t length, const uint64_t seed)
{
    uint8_t hash[8];
    MetroHash64::Hash(buffer, length, hash, seed);
    uint64_t result = bytes2int64(hash);
    return result;
}

int main(int argc, char** argv) {
    std::string line;
    if (argc <= 1) {
        return EXIT_FAILURE;
    }
    std::ifstream infile(argv[1]);
    while (std::getline(infile, line))
    {
        uint64_t result = metrohash64((uint8_t*)line.c_str(), line.length(), 0);
        std::cout << result << "\t" << line << std::endl;
    }
    return EXIT_SUCCESS;
}
<commit_msg>clarify multiples<commit_after>/*
 * =====================================================================================
 *
 *       Filename:  run_levc.cpp
 *
 *    Description:  Compute levenshtein distance (main)
 *
 *        Version:  1.0
 *        Created:  09/07/2015 21:21:41
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Eugene Scherba (es), escherba@gmail.com
 *   Organization:  -
 *
 * =====================================================================================
 */

#include <iostream>
#include <fstream>
#include <sstream>
#include "metrohash64.h"

inline uint64_t bytes2int64(uint8_t * const array)
{
    // uint64_t is guaranteed to be 8 bytes long
    return (uint64_t)(
              static_cast<uint64_t>(array[0])
            | static_cast<uint64_t>(array[1]) << 8 * 1
            | static_cast<uint64_t>(array[2]) << 8 * 2
            | static_cast<uint64_t>(array[3]) << 8 * 3
            | static_cast<uint64_t>(array[4]) << 8 * 4
            | static_cast<uint64_t>(array[5]) << 8 * 5
            | static_cast<uint64_t>(array[6]) << 8 * 6
            | static_cast<uint64_t>(array[7]) << 8 * 7);
}

uint64_t metrohash64(const uint8_t * buffer, const uint64_t length, const uint64_t seed)
{
    uint8_t hash[8];
    MetroHash64::Hash(buffer, length, hash, seed);
    uint64_t result = bytes2int64(hash);
    return result;
}

int main(int argc, char** argv) {
    std::string line;
    if (argc <= 1) {
        return EXIT_FAILURE;
    }
    std::ifstream infile(argv[1]);
    while (std::getline(infile, line))
    {
        uint64_t result = metrohash64((uint8_t*)line.c_str(), line.length(), 0);
        std::cout << result << "\t" << line << std::endl;
    }
    return EXIT_SUCCESS;
}
<|endoftext|>


<commit_before><commit_msg>planning: convert prediction data to relative corordinates in learning data<commit_after><|endoftext|>


<commit_before>/*
* Copyright (c)2015 - 2016 Oasis LMF Limited
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*
*   * Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in
*     the documentation and/or other materials provided with the
*     distribution.
*
*   * Neither the original author of this software nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
/*
Convert footprint csv to binary
Author: Ben Matharu  email: ben.matharu@oasislmf.org
*/

#include <stdio.h>
#include <stdlib.h>

#if defined(_MSC_VER)
#include "../wingetopt/wingetopt.h"
#else
#include <unistd.h>
#endif

namespace footprinttobin {
	void doit(int intensity_bins, int hasIntensityUncertainty, bool skipheader, const char * binFileName="footprint.bin", const char * idxFileName="footprint.idx");
	void doitz(int intensity_bins, int hasIntensityUncertainty, const char * binFileName="footprint.bin", const char * idxFileName="footprint.idx");
}

#include "../include/oasis.h"

#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <signal.h>
#include <string.h>
#endif

char *progname;
#if !defined(_MSC_VER) && !defined(__MINGW32__)
void segfault_sigaction(int signal, siginfo_t *si, void *arg) {
	fprintf(stderr, "%s: Segment fault at address: %p\n", progname,
		si->si_addr);
	exit(0);
}
#endif

void help() {
	fprintf(stderr, "-i max intensity bins\n"
		"-n No intensity uncertainty\n"
		"-s skip header\n"
		"-o output bin file name\n"
		"-v version\n"
		"-x output idx file name\n"
		"-z zip footprint data\n"
	);
}

int main(int argc, char *argv[]) {
	int opt;
	bool zip = false;
	int intensity_bins = -1;
	int hasIntensityUncertainty = true;
	bool skipheader = false;
	char *binFileName;
	bool binFileGiven = false;
	char *idxFileName;
	bool idxFileGiven = false;
	progname = argv[0];
	while ((opt = getopt(argc, argv, "zvshni:o:x:")) != -1) {
		switch (opt) {
		case 'v':
			fprintf(stderr, "%s : version: %s\n", argv[0], VERSION);
			exit(EXIT_FAILURE);
			break;
		case 'i':
			intensity_bins = atoi(optarg);
			break;
		case 'n':
			hasIntensityUncertainty = false;
			break;
		case 'o':
			binFileGiven = true;
			binFileName = optarg;
			break;
		case 's':
			skipheader = true;
			break;
		case 'x':
			idxFileGiven = true;
			idxFileName = optarg;
			break;
		case 'z':
			zip = true;
			break;
		case 'h':
			help();
			exit(EXIT_FAILURE);
		default:
			help();
			exit(EXIT_FAILURE);
		}
	}
	if (intensity_bins == -1) {
		fprintf(stderr, "%s: Intensity bin parameter not supplied\n", __func__);
		help();
		exit(EXIT_FAILURE);
	}

	initstreams();
	fprintf(stderr, "starting...\n");

	if (zip) {
#ifdef _MSC_VER
		fprintf(stderr, "Zip not supported in Microsoft build\n");
		exit(-1);
#else
		if(binFileGiven && idxFileGiven) {
			footprinttobin::doitz(intensity_bins, hasIntensityUncertainty, binFileName, idxFileName);
		} else if(binFileGiven || idxFileGiven) {
			fprintf(stderr, "Must specify both bin and idx file names\n");
			fprintf(stderr, "aborted\n");
			exit(EXIT_FAILURE);
		} else {
			footprinttobin::doitz(intensity_bins, hasIntensityUncertainty);
		}
#endif
	}
	else {
		if(binFileGiven && idxFileGiven) {
			footprinttobin::doit(intensity_bins, hasIntensityUncertainty, skipheader, binFileName, idxFileName);
		} else if(binFileGiven || idxFileGiven) {
			fprintf(stderr, "Must specify both bin and idx file names\n");
			fprintf(stderr, "aborted\n");
			exit(EXIT_FAILURE);
		} else {
			footprinttobin::doit(intensity_bins, hasIntensityUncertainty, skipheader);
		}
	}

	fprintf(stderr, "done...\n");
	return 0;
}
<commit_msg>Change flag for output binary file for uniformity + alter help text<commit_after>/*
* Copyright (c)2015 - 2016 Oasis LMF Limited
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*
*   * Redistributions in binary form must reproduce the above copyright
*     notice, this list of conditions and the following disclaimer in
*     the documentation and/or other materials provided with the
*     distribution.
*
*   * Neither the original author of this software nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
/*
Convert footprint csv to binary
Author: Ben Matharu  email: ben.matharu@oasislmf.org
*/

#include <stdio.h>
#include <stdlib.h>

#if defined(_MSC_VER)
#include "../wingetopt/wingetopt.h"
#else
#include <unistd.h>
#endif

namespace footprinttobin {
	void doit(int intensity_bins, int hasIntensityUncertainty, bool skipheader, const char * binFileName="footprint.bin", const char * idxFileName="footprint.idx");
	void doitz(int intensity_bins, int hasIntensityUncertainty, const char * binFileName="footprint.bin", const char * idxFileName="footprint.idx");
}

#include "../include/oasis.h"

#if !defined(_MSC_VER) && !defined(__MINGW32__)
#include <signal.h>
#include <string.h>
#endif

char *progname;
#if !defined(_MSC_VER) && !defined(__MINGW32__)
void segfault_sigaction(int signal, siginfo_t *si, void *arg) {
	fprintf(stderr, "%s: Segment fault at address: %p\n", progname,
		si->si_addr);
	exit(0);
}
#endif

void help() {
	fprintf(stderr, "-i max intensity bins\n"
		"-n No intensity uncertainty\n"
		"-s skip header\n"
		"-v version\n"
		"-b [FILE NAME] output bin file name\n"
		"-x [FILE NAME] output idx file name\n"
		"-z zip footprint data\n"
	);
}

int main(int argc, char *argv[]) {
	int opt;
	bool zip = false;
	int intensity_bins = -1;
	int hasIntensityUncertainty = true;
	bool skipheader = false;
	char *binFileName;
	bool binFileGiven = false;
	char *idxFileName;
	bool idxFileGiven = false;
	progname = argv[0];
	while ((opt = getopt(argc, argv, "zvshni:b:x:")) != -1) {
		switch (opt) {
		case 'v':
			fprintf(stderr, "%s : version: %s\n", argv[0], VERSION);
			exit(EXIT_FAILURE);
			break;
		case 'b':
			binFileGiven = true;
			binFileName = optarg;
			break;
		case 'i':
			intensity_bins = atoi(optarg);
			break;
		case 'n':
			hasIntensityUncertainty = false;
			break;
		case 's':
			skipheader = true;
			break;
		case 'x':
			idxFileGiven = true;
			idxFileName = optarg;
			break;
		case 'z':
			zip = true;
			break;
		case 'h':
			help();
			exit(EXIT_FAILURE);
		default:
			help();
			exit(EXIT_FAILURE);
		}
	}
	if (intensity_bins == -1) {
		fprintf(stderr, "%s: Intensity bin parameter not supplied\n", __func__);
		help();
		exit(EXIT_FAILURE);
	}

	initstreams();
	fprintf(stderr, "starting...\n");

	if (zip) {
#ifdef _MSC_VER
		fprintf(stderr, "Zip not supported in Microsoft build\n");
		exit(-1);
#else
		if(binFileGiven && idxFileGiven) {
			footprinttobin::doitz(intensity_bins, hasIntensityUncertainty, binFileName, idxFileName);
		} else if(binFileGiven || idxFileGiven) {
			fprintf(stderr, "Must specify both bin and idx file names\n");
			fprintf(stderr, "aborted\n");
			exit(EXIT_FAILURE);
		} else {
			footprinttobin::doitz(intensity_bins, hasIntensityUncertainty);
		}
#endif
	}
	else {
		if(binFileGiven && idxFileGiven) {
			footprinttobin::doit(intensity_bins, hasIntensityUncertainty, skipheader, binFileName, idxFileName);
		} else if(binFileGiven || idxFileGiven) {
			fprintf(stderr, "Must specify both bin and idx file names\n");
			fprintf(stderr, "aborted\n");
			exit(EXIT_FAILURE);
		} else {
			footprinttobin::doit(intensity_bins, hasIntensityUncertainty, skipheader);
		}
	}

	fprintf(stderr, "done...\n");
	return 0;
}
<|endoftext|>


<commit_before>/*
 *  Copyright 2008-2010 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file unique.inl
 *  \brief Inline file for unique.h.
 */

#pragma once

#include <thrust/iterator/iterator_traits.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/transform.h>
#include <thrust/scatter.h>
#include <thrust/scan.h>
#include <limits>

#include <thrust/detail/raw_buffer.h>
#include <thrust/detail/type_traits.h>
#include <thrust/detail/internal_functional.h>

#include <thrust/detail/device/scan.h>
#include <thrust/detail/device/copy.h>

namespace thrust
{
namespace detail
{
namespace device
{
namespace generic
{
namespace detail
{

template <typename ValueType, typename TailFlagType>
struct unique_by_key_functor
{
    typedef typename thrust::tuple<ValueType, TailFlagType> result_type;

    __host__ __device__
    result_type operator()(result_type a, result_type b)
    {
        return result_type(thrust::get<1>(b) ? thrust::get<0>(b) : thrust::get<0>(a),
                           thrust::get<1>(a) | thrust::get<1>(b));
    }
};

} // end namespace detail

template <typename ForwardIterator,
          typename BinaryPredicate>
ForwardIterator unique(ForwardIterator first,
                       ForwardIterator last,
                       BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_traits<ForwardIterator>::value_type InputType;
    typedef typename thrust::iterator_space<ForwardIterator>::type        Space;

    thrust::detail::raw_buffer<InputType,Space> input(first, last);

    return thrust::detail::device::generic::unique_copy(input.begin(), input.end(), first, binary_pred);
}

template <typename InputIterator,
          typename OutputIterator,
          typename BinaryPredicate>
OutputIterator unique_copy(InputIterator first,
                           InputIterator last,
                           OutputIterator output,
                           BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_space<OutputIterator>::type Space;

    // empty sequence
    if(first == last)
        return output;

    thrust::detail::raw_buffer<int,Space> stencil(thrust::distance(first, last));

    // mark first element in each group
    stencil[0] = 1;
    thrust::transform(first, last - 1, first + 1, stencil.begin() + 1, thrust::detail::not2(binary_pred));

    return thrust::detail::device::copy_if(first, last, stencil.begin(), output, thrust::identity<int>());
}

template <typename ForwardIterator1,
          typename ForwardIterator2,
          typename BinaryPredicate>
  thrust::pair<ForwardIterator1,ForwardIterator2>
  unique_by_key(ForwardIterator1 keys_first,
                ForwardIterator1 keys_last,
                ForwardIterator2 values_first,
                BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_traits<ForwardIterator1>::value_type InputType1;
    typedef typename thrust::iterator_traits<ForwardIterator2>::value_type InputType2;
    typedef typename thrust::iterator_space<ForwardIterator1>::type        Space;

    ForwardIterator2 values_last = values_first + (keys_last - keys_first);

    thrust::detail::raw_buffer<InputType1,Space> keys(keys_first, keys_last);
    thrust::detail::raw_buffer<InputType2,Space> vals(values_first, values_last);

    return thrust::detail::device::generic::unique_copy_by_key
        (keys.begin(), keys.end(), vals.begin(), keys_first, values_first, binary_pred);
}

template <typename InputIterator1,
          typename InputIterator2,
          typename OutputIterator1,
          typename OutputIterator2,
          typename BinaryPredicate>
  thrust::pair<OutputIterator1,OutputIterator2>
  unique_copy_by_key(InputIterator1 keys_first,
                     InputIterator1 keys_last,
                     InputIterator2 values_first,
                     OutputIterator1 keys_output,
                     OutputIterator2 values_output,
                     BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_traits<InputIterator1>::difference_type difference_type;
    typedef typename thrust::iterator_traits<InputIterator1>::value_type  KeyType;
    typedef typename thrust::iterator_traits<OutputIterator2>::value_type ValueType;
    typedef typename thrust::iterator_space<OutputIterator1>::type Space;
    typedef unsigned int FlagType;

    if (keys_first == keys_last)
        return thrust::make_pair(keys_output, values_output);

    // input size
    difference_type n = keys_last - keys_first;

    InputIterator2 values_last = values_first + n;

    // compute head flags
    thrust::detail::raw_buffer<FlagType,Space> head_flags(n);
    thrust::transform(keys_first, keys_last - 1, keys_first + 1, head_flags.begin() + 1, thrust::detail::not2(binary_pred));
    head_flags[0] = 1;

    // compute tail flags
    thrust::detail::raw_buffer<FlagType,Space> tail_flags(n); //COPY INSTEAD OF TRANSFORM
    thrust::transform(keys_first, keys_last - 1, keys_first + 1, tail_flags.begin(), thrust::detail::not2(binary_pred));
    tail_flags[n-1] = 1;

    // scan the values by flag
    thrust::detail::raw_buffer<ValueType,Space> scanned_values(n);
    thrust::detail::raw_buffer<FlagType,Space>  scanned_tail_flags(n);

    thrust::detail::device::inclusive_scan
        (thrust::make_zip_iterator(thrust::make_tuple(values_first,           head_flags.begin())),
         thrust::make_zip_iterator(thrust::make_tuple(values_last,            head_flags.end())),
         thrust::make_zip_iterator(thrust::make_tuple(scanned_values.begin(), scanned_tail_flags.begin())),
         detail::unique_by_key_functor<ValueType, FlagType>());

    thrust::exclusive_scan(tail_flags.begin(), tail_flags.end(), scanned_tail_flags.begin());

    // number of unique keys
    FlagType N = scanned_tail_flags[n - 1] + 1;

//    std::cout << "head_flags" << std::endl;
//    thrust::copy(head_flags.begin(), head_flags.end(), std::ostream_iterator<FlagType>(std::cout, " "));
//    std::cout << std::endl;
//
//    std::cout << "tail_flags" << std::endl;
//    thrust::copy(tail_flags.begin(), tail_flags.end(), std::ostream_iterator<FlagType>(std::cout, " "));
//    std::cout << std::endl;
//
//    std::cout << "scanned_tail_flags" << std::endl;
//    thrust::copy(scanned_tail_flags.begin(), scanned_tail_flags.end(), std::ostream_iterator<FlagType>(std::cout, " "));
//    std::cout << std::endl;
//
//    std::cout << "scanned_values" << std::endl;
//    thrust::copy(scanned_values.begin(), scanned_values.end(), std::ostream_iterator<ValueType>(std::cout, " "));
//    std::cout << std::endl;

    // scatter the keys and accumulated values
    thrust::scatter_if(keys_first,            keys_last,             scanned_tail_flags.begin(), head_flags.begin(), keys_output);
    thrust::scatter_if(scanned_values.begin(), scanned_values.end(), scanned_tail_flags.begin(), tail_flags.begin(), values_output);

    return thrust::make_pair(keys_output + N, values_output + N);
}

} // end namespace generic
} // end namespace device
} // end namespace detail
} // end namespace thrust

<commit_msg>implemented generic::unique_copy_by_key() with device::copy_if() resolves issue #108<commit_after>/*
 *  Copyright 2008-2010 NVIDIA Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/*! \file unique.inl
 *  \brief Inline file for unique.h.
 */

#pragma once

#include <thrust/iterator/iterator_traits.h>
#include <thrust/iterator/zip_iterator.h>
#include <thrust/transform.h>

#include <thrust/detail/raw_buffer.h>
#include <thrust/detail/type_traits.h>
#include <thrust/detail/internal_functional.h>

#include <thrust/detail/device/scan.h>
#include <thrust/detail/device/copy.h>

namespace thrust
{
namespace detail
{
namespace device
{
namespace generic
{

template <typename ForwardIterator,
          typename BinaryPredicate>
ForwardIterator unique(ForwardIterator first,
                       ForwardIterator last,
                       BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_traits<ForwardIterator>::value_type InputType;
    typedef typename thrust::iterator_space<ForwardIterator>::type        Space;

    thrust::detail::raw_buffer<InputType,Space> input(first, last);

    return thrust::detail::device::generic::unique_copy(input.begin(), input.end(), first, binary_pred);
}

template <typename InputIterator,
          typename OutputIterator,
          typename BinaryPredicate>
OutputIterator unique_copy(InputIterator first,
                           InputIterator last,
                           OutputIterator output,
                           BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_space<OutputIterator>::type Space;

    // empty sequence
    if(first == last)
        return output;

    thrust::detail::raw_buffer<int,Space> stencil(thrust::distance(first, last));

    // mark first element in each group
    stencil[0] = 1;
    thrust::transform(first, last - 1, first + 1, stencil.begin() + 1, thrust::detail::not2(binary_pred));

    return thrust::detail::device::copy_if(first, last, stencil.begin(), output, thrust::identity<int>());
}

template <typename ForwardIterator1,
          typename ForwardIterator2,
          typename BinaryPredicate>
  thrust::pair<ForwardIterator1,ForwardIterator2>
  unique_by_key(ForwardIterator1 keys_first,
                ForwardIterator1 keys_last,
                ForwardIterator2 values_first,
                BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_traits<ForwardIterator1>::value_type InputType1;
    typedef typename thrust::iterator_traits<ForwardIterator2>::value_type InputType2;
    typedef typename thrust::iterator_space<ForwardIterator1>::type        Space;

    ForwardIterator2 values_last = values_first + (keys_last - keys_first);

    thrust::detail::raw_buffer<InputType1,Space> keys(keys_first, keys_last);
    thrust::detail::raw_buffer<InputType2,Space> vals(values_first, values_last);

    return thrust::detail::device::generic::unique_copy_by_key
        (keys.begin(), keys.end(), vals.begin(), keys_first, values_first, binary_pred);
}

template <typename InputIterator1,
          typename InputIterator2,
          typename OutputIterator1,
          typename OutputIterator2,
          typename BinaryPredicate>
  thrust::pair<OutputIterator1,OutputIterator2>
  unique_copy_by_key(InputIterator1 keys_first,
                     InputIterator1 keys_last,
                     InputIterator2 values_first,
                     OutputIterator1 keys_output,
                     OutputIterator2 values_output,
                     BinaryPredicate binary_pred)
{
    typedef typename thrust::iterator_traits<InputIterator1>::difference_type difference_type;
    typedef typename thrust::iterator_space<OutputIterator1>::type            Space;

    // empty sequence
    if(keys_first == keys_last)
        return thrust::make_pair(keys_output, values_output);

    difference_type n = thrust::distance(keys_first, keys_last);

    thrust::detail::raw_buffer<int,Space> stencil(n);

    // mark first element in each group
    stencil[0] = 1;
    thrust::transform(keys_first, keys_last - 1, keys_first + 1, stencil.begin() + 1, thrust::detail::not2(binary_pred));

    thrust::zip_iterator< thrust::tuple<OutputIterator1, OutputIterator2> > result =
        thrust::detail::device::copy_if(thrust::make_zip_iterator(thrust::make_tuple(keys_first, values_first)),
                                        thrust::make_zip_iterator(thrust::make_tuple(keys_first, values_first)) + n,
                                        stencil.begin(),
                                        thrust::make_zip_iterator(thrust::make_tuple(keys_output, values_output)),
                                        thrust::identity<int>());

    difference_type output_size = result - thrust::make_zip_iterator(thrust::make_tuple(keys_output, values_output));

    return thrust::make_pair(keys_output + output_size, values_output + output_size);
}

} // end namespace generic
} // end namespace device
} // end namespace detail
} // end namespace thrust

<|endoftext|>


<commit_before>#include "QuorumDatabase.h"
#include "Framework/Storage/StorageShardProxy.h"
#include "Framework/Storage/StorageEnvironment.h"

void QuorumDatabase::Init(StorageShardProxy* paxosShard_, StorageShardProxy* logShard_)
{
    assert(paxosShard_ != NULL);
    assert(logShard_ != NULL);
    paxosShard = paxosShard_;
    logShard = logShard_;
}

uint64_t QuorumDatabase::GetPaxosID()
{
    return GetUint64("paxosID");
}

void QuorumDatabase::SetPaxosID(uint64_t paxosID)
{
    SetUint64("paxosID", paxosID);
}

bool QuorumDatabase::GetAccepted()
{
    Buffer      key;
    ReadBuffer  rbKey;
    ReadBuffer  rbValue;
    bool        ret;

    key.Write("accepted");
    rbKey.Wrap(key);

    ret = paxosShard->Get(rbKey, rbValue);
    if (!ret)
      return false;   // not found, return default

    if (rbValue.GetLength() != 1)
      return false;   // incorrect value, return default

    if (rbValue.GetCharAt(0) == '1')
      return true;

    return false;
}

void QuorumDatabase::SetAccepted(bool accepted)
{
    Buffer      key;
    Buffer      value;
    ReadBuffer  rbKey;
    ReadBuffer  rbValue;
    bool        ret;

    key.Write("accepted");
    rbKey.Wrap(key);

    value.Writef("%d", accepted);
    rbValue.Wrap(value);

    ret = paxosShard->Set(rbKey, rbValue);
}

uint64_t QuorumDatabase::GetPromisedProposalID()
{
    return GetUint64("promisedProposalID");
}

void QuorumDatabase::SetPromisedProposalID(uint64_t promisedProposalID)
{
    SetUint64("promisedProposalID", promisedProposalID);
}

uint64_t QuorumDatabase::GetAcceptedProposalID()
{
    return GetUint64("acceptedProposalID");
}

void QuorumDatabase::SetAcceptedProposalID(uint64_t acceptedProposalID)
{
    SetUint64("acceptedProposalID", acceptedProposalID);
}

uint64_t QuorumDatabase::GetAcceptedRunID()
{
    return GetUint64("acceptedRunID");
}

void QuorumDatabase::SetAcceptedRunID(uint64_t acceptedRunID)
{
    SetUint64("acceptedRunID", acceptedRunID);
}

void QuorumDatabase::GetAcceptedValue(uint64_t paxosID, Buffer& value)
{
    Buffer      key;
    ReadBuffer  rbKey;
    ReadBuffer  rbValue;
    bool        ret;

    key.Writef("accepted:%021U", paxosID);
    rbKey.Wrap(key);

    ret = logShard->Get(rbKey, rbValue);
    if (!ret)
        return;

    ASSERT(rbValue.GetLength());

    value.Write(rbValue.GetBuffer(), rbValue.GetLength());
}

void QuorumDatabase::SetAcceptedValue(uint64_t paxosID, ReadBuffer value)
{
    Buffer      key;
    ReadBuffer  rbKey;

    ASSERT(value.GetLength());

    key.Writef("accepted:%021U", paxosID);
    rbKey.Wrap(key);

    logShard->Set(rbKey, value);
}

bool QuorumDatabase::IsCommiting()
{
    return paxosShard->GetEnvironment()->IsCommiting();
}

void QuorumDatabase::Commit()
{
    paxosShard->GetEnvironment()->Commit();
}

void QuorumDatabase::Commit(Callable& onCommit)
{
    paxosShard->GetEnvironment()->Commit(onCommit);
}

uint64_t QuorumDatabase::GetUint64(const char* name)
{
    ReadBuffer  key(name);
    ReadBuffer  value;
    uint64_t    u64;
    unsigned    nread;
    int         ret;

    ret = paxosShard->Get(key, value);
    if (!ret)
        return false;

    nread = 0;
    u64 = BufferToUInt64(value.GetBuffer(), value.GetLength(), &nread);
    if (nread != value.GetLength())
    {
        Log_Trace();
        u64 = 0;
    }

    return u64;
}

void QuorumDatabase::SetUint64(const char* name, uint64_t u64)
{
    ReadBuffer  key(name);
    Buffer      tmp;
    ReadBuffer  value;
    int         ret;

    tmp.Writef("%U", u64);
    value.Wrap(tmp);

    ret = paxosShard->Set(key, value);
}
<commit_msg>Eq. transf.<commit_after>#include "QuorumDatabase.h"
#include "Framework/Storage/StorageShardProxy.h"
#include "Framework/Storage/StorageEnvironment.h"

void QuorumDatabase::Init(StorageShardProxy* paxosShard_, StorageShardProxy* logShard_)
{
    assert(paxosShard_ != NULL);
    assert(logShard_ != NULL);
    paxosShard = paxosShard_;
    logShard = logShard_;
}

uint64_t QuorumDatabase::GetPaxosID()
{
    return GetUint64("paxosID");
}

void QuorumDatabase::SetPaxosID(uint64_t paxosID)
{
    SetUint64("paxosID", paxosID);
}

bool QuorumDatabase::GetAccepted()
{
    Buffer      key;
    ReadBuffer  rbKey;
    ReadBuffer  rbValue;
    bool        ret;

    key.Write("accepted");
    rbKey.Wrap(key);

    ret = paxosShard->Get(rbKey, rbValue);
    if (!ret)
      return false;   // not found, return default

    if (rbValue.GetLength() != 1)
      return false;   // incorrect value, return default

    if (rbValue.GetCharAt(0) == '1')
      return true;

    return false;
}

void QuorumDatabase::SetAccepted(bool accepted)
{
    Buffer      key;
    Buffer      value;
    ReadBuffer  rbKey;
    ReadBuffer  rbValue;
    bool        ret;

    key.Write("accepted");
    rbKey.Wrap(key);

    value.Writef("%d", accepted);
    rbValue.Wrap(value);

    ret = paxosShard->Set(rbKey, rbValue);
}

uint64_t QuorumDatabase::GetPromisedProposalID()
{
    return GetUint64("promisedProposalID");
}

void QuorumDatabase::SetPromisedProposalID(uint64_t promisedProposalID)
{
    SetUint64("promisedProposalID", promisedProposalID);
}

uint64_t QuorumDatabase::GetAcceptedProposalID()
{
    return GetUint64("acceptedProposalID");
}

void QuorumDatabase::SetAcceptedProposalID(uint64_t acceptedProposalID)
{
    SetUint64("acceptedProposalID", acceptedProposalID);
}

uint64_t QuorumDatabase::GetAcceptedRunID()
{
    return GetUint64("acceptedRunID");
}

void QuorumDatabase::SetAcceptedRunID(uint64_t acceptedRunID)
{
    SetUint64("acceptedRunID", acceptedRunID);
}

void QuorumDatabase::GetAcceptedValue(uint64_t paxosID, Buffer& value)
{
    Buffer      key;
    ReadBuffer  rbKey;
    ReadBuffer  rbValue;
    bool        ret;

    key.Writef("accepted:%021U", paxosID);
    rbKey.Wrap(key);

    ret = logShard->Get(rbKey, rbValue);
    if (!ret)
        return;

    ASSERT(rbValue.GetLength() > 0);

    value.Write(rbValue);
}

void QuorumDatabase::SetAcceptedValue(uint64_t paxosID, ReadBuffer value)
{
    Buffer      key;
    ReadBuffer  rbKey;

    ASSERT(value.GetLength() > 0);

    key.Writef("accepted:%021U", paxosID);
    rbKey.Wrap(key);

    logShard->Set(rbKey, value);
}

bool QuorumDatabase::IsCommiting()
{
    return paxosShard->GetEnvironment()->IsCommiting();
}

void QuorumDatabase::Commit()
{
    paxosShard->GetEnvironment()->Commit();
}

void QuorumDatabase::Commit(Callable& onCommit)
{
    paxosShard->GetEnvironment()->Commit(onCommit);
}

uint64_t QuorumDatabase::GetUint64(const char* name)
{
    ReadBuffer  key(name);
    ReadBuffer  value;
    uint64_t    u64;
    unsigned    nread;
    int         ret;

    ret = paxosShard->Get(key, value);
    if (!ret)
        return false;

    nread = 0;
    u64 = BufferToUInt64(value.GetBuffer(), value.GetLength(), &nread);
    if (nread != value.GetLength())
    {
        Log_Trace();
        u64 = 0;
    }

    return u64;
}

void QuorumDatabase::SetUint64(const char* name, uint64_t u64)
{
    ReadBuffer  key(name);
    Buffer      tmp;
    ReadBuffer  value;
    int         ret;

    tmp.Writef("%U", u64);
    value.Wrap(tmp);

    ret = paxosShard->Set(key, value);
}
<|endoftext|>


<commit_before>//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.

//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef UUID_8D22C4CA9CC811DCAA9133D256D89593
#define UUID_8D22C4CA9CC811DCAA9133D256D89593

#include <boost/type.hpp>
#include <boost/exception/exception.hpp>
#include <boost/exception/error_info.hpp>
#include <boost/exception/to_string_stub.hpp>
#include <boost/current_function.hpp>
#include <boost/shared_ptr.hpp>
#include <map>

#define BOOST_ERROR_INFO\
	::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
	::boost::throw_file(__FILE__) <<\
	::boost::throw_line((int)__LINE__)

namespace
boost
	{
	typedef error_info<struct tag_throw_function,char const *> throw_function;
	typedef error_info<struct tag_throw_file,char const *> throw_file;
	typedef error_info<struct tag_throw_line,int> throw_line;

	namespace
	exception_detail
		{
		class
		error_info_base
			{
			public:

			virtual std::type_info const & tag_typeid() const = 0;
			virtual std::string value_as_string() const = 0;
			};
		}

	template <class Tag,class T>
	class
	error_info:
		public exception_detail::error_info_base
		{
		public:

		typedef T value_type;

		error_info( value_type const & value ):
			value_(value)
			{
			}

		value_type const &
		value() const
			{
			return value_;
			}

		private:

		std::type_info const &
		tag_typeid() const
			{
			return typeid(type<Tag>);
			}

		std::string
		value_as_string() const
			{
			return to_string_stub(value_);
			}

		value_type const value_;
		};

	template <class ErrorInfo>
	struct
	error_info_type
		{
		typedef typename ErrorInfo::value_type value_type;
		};

	template <class E,class Tag,class T>
	E const &
	operator<<( E const & x, error_info<Tag,T> const & v )
		{
		shared_ptr< error_info<Tag,T> > p( new error_info<Tag,T>(v) );
		x.set(p);
		return x;
		}

	template <class ErrorInfo,class E>
	shared_ptr<typename ErrorInfo::value_type const>
	get_error_info( E const & some_exception )
		{
		if( exception const * x = dynamic_cast<exception const *>(&some_exception) )
			if( shared_ptr<exception_detail::error_info_base const> eib = x->get(typeid(ErrorInfo)) )
				{
				BOOST_ASSERT( 0!=dynamic_cast<ErrorInfo const *>(eib.get()) );
				ErrorInfo const * w = static_cast<ErrorInfo const *>(eib.get());
				return shared_ptr<typename ErrorInfo::value_type const>(eib,&w->value());
				}
		return shared_ptr<typename ErrorInfo::value_type const>();
		}

	namespace
	exception_detail
		{
		class
		error_info_container_impl:
			public error_info_container
			{
			public:

			error_info_container_impl():
				count_(0)
				{
				}

			~error_info_container_impl() throw()
				{
				}

			shared_ptr<error_info_base const>
			get( std::type_info const & ti ) const
				{
				error_info_map::const_iterator i=info_.find(typeinfo(ti));
				if( info_.end()!=i )
					{
					shared_ptr<error_info_base const> const & p = i->second;
					BOOST_ASSERT( typeid(*p)==ti );
					return p;
					}
				return shared_ptr<error_info_base const>();
				}

			void
			set( shared_ptr<error_info_base const> const & x )
				{
				BOOST_ASSERT(x);
				info_[typeinfo(typeid(*x))] = x;
				what_.clear();
				}

			char const *
			what( std::type_info const & exception_type ) const
				{
				if( what_.empty() )
					{
					std::string tmp(exception_type.name());
					tmp += '\n';
					for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
						{
						shared_ptr<error_info_base const> const & x = i->second;
						tmp += '[';
						tmp += x->tag_typeid().name();
						tmp += "] = ";
						tmp += x->value_as_string();
						tmp += '\n';
						}
					what_.swap(tmp);
					}
				return what_.c_str();
				}

			private:

			friend class exception;

			struct
			typeinfo
				{
				std::type_info const * type;

				explicit
				typeinfo( std::type_info const & t ):
					type(&t)
					{
					}

				bool
				operator<( typeinfo const & b ) const
					{
					return 0!=(type->before(*b.type));
					}
				};

			typedef std::map< typeinfo, shared_ptr<error_info_base const> > error_info_map;
			error_info_map info_;
			std::string mutable what_;
			int mutable count_;

			void
			add_ref() const
				{
				++count_;
				}

			void
			release() const
				{
				if( !--count_ )
					delete this;
				}
			};
		}

	inline
	void
	exception::
	set( shared_ptr<exception_detail::error_info_base const> const & x ) const
		{
		if( !data_ )
			data_ = intrusive_ptr<exception_detail::error_info_container>(new exception_detail::error_info_container_impl);
		data_->set(x);
		}

	inline
	shared_ptr<exception_detail::error_info_base const>
	exception::
	get( std::type_info const & ti ) const
		{
		if( data_ )
			return data_->get(ti);
		else
			return shared_ptr<exception_detail::error_info_base const>();
		}
	}

#endif
<commit_msg>Make boost::exception compatible with Boost 1.34.1<commit_after>//Copyright (c) 2006-2008 Emil Dotchevski and Reverge Studios, Inc.

//Distributed under the Boost Software License, Version 1.0. (See accompanying
//file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef UUID_8D22C4CA9CC811DCAA9133D256D89593
#define UUID_8D22C4CA9CC811DCAA9133D256D89593

#include <boost/type.hpp>
#include <boost/exception/exception.hpp>
#include <boost/exception/error_info.hpp>
#include <boost/exception/to_string_stub.hpp>
#include <boost/current_function.hpp>
#include <boost/shared_ptr.hpp>
#include <map>

#define BOOST_ERROR_INFO\
	::boost::throw_function(BOOST_CURRENT_FUNCTION) <<\
	::boost::throw_file(__FILE__) <<\
	::boost::throw_line((int)__LINE__)

namespace
boost
	{
	typedef error_info<struct tag_throw_function,char const *> throw_function;
	typedef error_info<struct tag_throw_file,char const *> throw_file;
	typedef error_info<struct tag_throw_line,int> throw_line;

	namespace
	exception_detail
		{
		class
		error_info_base
			{
			public:

			virtual std::type_info const & tag_typeid() const = 0;
			virtual std::string value_as_string() const = 0;
			};
		}

	template <class Tag,class T>
	class
	error_info:
		public exception_detail::error_info_base
		{
		public:

		typedef T value_type;

		error_info( value_type const & value ):
			value_(value)
			{
			}

		value_type const &
		value() const
			{
			return value_;
			}

		private:

		std::type_info const &
		tag_typeid() const
			{
			return typeid(type<Tag>);
			}

		std::string
		value_as_string() const
			{
			return to_string_stub(value_);
			}

		value_type const value_;
		};

	template <class ErrorInfo>
	struct
	error_info_type
		{
		typedef typename ErrorInfo::value_type value_type;
		};

	template <class E,class Tag,class T>
	E const &
	operator<<( E const & x, error_info<Tag,T> const & v )
		{
		shared_ptr< error_info<Tag,T> > p( new error_info<Tag,T>(v) );
		x.set(p);
		return x;
		}

	template <class ErrorInfo,class E>
	shared_ptr<typename ErrorInfo::value_type const>
	get_error_info( E const & some_exception )
		{
		if( exception const * x = dynamic_cast<exception const *>(&some_exception) )
			if( shared_ptr<exception_detail::error_info_base const> eib = x->get(typeid(ErrorInfo)) )
				{
				BOOST_ASSERT( 0!=dynamic_cast<ErrorInfo const *>(eib.get()) );
				ErrorInfo const * w = static_cast<ErrorInfo const *>(eib.get());
				return shared_ptr<typename ErrorInfo::value_type const>(new typename ErrorInfo::value_type (w->value()));
				}
		return shared_ptr<typename ErrorInfo::value_type const>();
		}

	namespace
	exception_detail
		{
		class
		error_info_container_impl:
			public error_info_container
			{
			public:

			error_info_container_impl():
				count_(0)
				{
				}

			~error_info_container_impl() throw()
				{
				}

			shared_ptr<error_info_base const>
			get( std::type_info const & ti ) const
				{
				error_info_map::const_iterator i=info_.find(typeinfo(ti));
				if( info_.end()!=i )
					{
					shared_ptr<error_info_base const> const & p = i->second;
					BOOST_ASSERT( typeid(*p)==ti );
					return p;
					}
				return shared_ptr<error_info_base const>();
				}

			void
			set( shared_ptr<error_info_base const> const & x )
				{
				BOOST_ASSERT(x);
				info_[typeinfo(typeid(*x))] = x;
				what_.clear();
				}

			char const *
			what( std::type_info const & exception_type ) const
				{
				if( what_.empty() )
					{
					std::string tmp(exception_type.name());
					tmp += '\n';
					for( error_info_map::const_iterator i=info_.begin(),end=info_.end(); i!=end; ++i )
						{
						shared_ptr<error_info_base const> const & x = i->second;
						tmp += '[';
						tmp += x->tag_typeid().name();
						tmp += "] = ";
						tmp += x->value_as_string();
						tmp += '\n';
						}
					what_.swap(tmp);
					}
				return what_.c_str();
				}

			private:

			friend class exception;

			struct
			typeinfo
				{
				std::type_info const * type;

				explicit
				typeinfo( std::type_info const & t ):
					type(&t)
					{
					}

				bool
				operator<( typeinfo const & b ) const
					{
					return 0!=(type->before(*b.type));
					}
				};

			typedef std::map< typeinfo, shared_ptr<error_info_base const> > error_info_map;
			error_info_map info_;
			std::string mutable what_;
			int mutable count_;

			void
			add_ref() const
				{
				++count_;
				}

			void
			release() const
				{
				if( !--count_ )
					delete this;
				}
			};
		}

	inline
	void
	exception::
	set( shared_ptr<exception_detail::error_info_base const> const & x ) const
		{
		if( !data_ )
			data_ = intrusive_ptr<exception_detail::error_info_container>(new exception_detail::error_info_container_impl);
		data_->set(x);
		}

	inline
	shared_ptr<exception_detail::error_info_base const>
	exception::
	get( std::type_info const & ti ) const
		{
		if( data_ )
			return data_->get(ti);
		else
			return shared_ptr<exception_detail::error_info_base const>();
		}
	}

#endif
<|endoftext|>


<commit_before>#ifndef LOC_LOSS_LAYERS_HPP_
#define LOC_LOSS_LAYERS_HPP_

#include <string>
#include <utility>
#include <vector>

#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/layer.hpp"
#include "caffe/neuron_layers.hpp"
#include "caffe/proto/caffe.pb.h"

namespace caffe {

/* Input: theta
 * Output: loss, one single value
 *
 * This loss layer tends to force the crops to be in the range
 * of image space when performing spatial transformation
 */

template <typename Dtype>
class LocLossLayer : public LossLayer<Dtype> {
public:
  explicit LocLossLayer(const LayerParameter& param)
      : LossLayer<Dtype>(param) {}
  virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
        const vector<Blob<Dtype>*>& top);
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual inline const char* type() const { return "LocLoss"; }

  virtual inline int ExactNumBottomBlobs() const { return 1; }

  virtual inline bool AllowForceBackward(const int bottom_index) const {
    return true;
  }

 protected:

  virtual void Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);
  virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top);

  virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);

 private:
  int N;
  Dtype threshold;

  Blob<Dtype> loss_;
};

}  // namespace caffe

#endif  // LOC_LOSS_LAYERS_HPP_
<commit_msg>Delete loc_loss_layer.hpp<commit_after><|endoftext|>


<commit_before>/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <string>

#include "llvm/ADT/StringRef.h"
#include "mlir/Dialect/Quant/QuantTypes.h"  // from @llvm-project
#include "mlir/IR/Block.h"  // from @llvm-project
#include "mlir/IR/Builders.h"  // from @llvm-project
#include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
#include "mlir/IR/Operation.h"  // from @llvm-project
#include "mlir/IR/Value.h"  // from @llvm-project
#include "mlir/Pass/Pass.h"  // from @llvm-project
#include "mlir/Pass/PassRegistry.h"  // from @llvm-project
#include "tensorflow/compiler/mlir/tfr/ir/tfr_ops.h"
#include "tensorflow/compiler/mlir/tfr/passes/passes.h"

namespace mlir {
namespace TFR {

class RewriteQuantizedIOPass
    : public PassWrapper<RewriteQuantizedIOPass, OperationPass<ModuleOp>> {
 public:
  StringRef getArgument() const final { return "tfr-rewrite-quantized-io"; }

  StringRef getDescription() const final {
    return "Replaces operands and results that has quantized type with their "
           "storage types.";
  }
  void runOnOperation() override;
};

void RewriteQuantizedIOPass::runOnOperation() {
  void runOnOperation();

  ModuleOp module = getOperation();
  OpBuilder builder(module);
  module.walk([&](FuncOp func) {
    Block& block = func.front();
    Operation* terminator = block.getTerminator();

    // Replace input_arg(tensor<quant_type>) -> tfr.cast
    // with input_arg(tensor<storage_type>) -> tfr.cast
    for (BlockArgument arg : block.getArguments()) {
      Type arg_type = arg.getType();
      if (auto quant_type = arg_type.cast<TensorType>()
                                .getElementType()
                                .dyn_cast<quant::QuantizedType>()) {
        if (arg.hasOneUse() && llvm::isa<TFR::CastOp>(*arg.user_begin())) {
          arg.setType(
              arg_type.cast<TensorType>().clone(quant_type.getStorageType()));
        } else {
          std::string error_message;
          llvm::raw_string_ostream os{error_message};
          os << "The argument with type ";
          arg.getType().print(os);
          os << " should have one user, which should be tfr.cast.";
          func->emitError(error_message);
          return;
        }
      }
    }

    builder.setInsertionPoint(terminator);
    // Replace tfr.cast(tensor<quant_type>) -> output
    // with tfr.cast(tensor<storage_type>) -> output
    for (OpOperand& returned_value : terminator->getOpOperands()) {
      auto returned_type =
          returned_value.get().getType().dyn_cast<TensorType>();
      if (!returned_type ||
          !returned_type.getElementType().isa<quant::QuantizedType>()) {
        continue;
      }

      if (auto returned_op =
              returned_value.get().getDefiningOp<TFR::CastOp>()) {
        auto new_type = returned_type.clone(returned_type.getElementType()
                                                .cast<quant::QuantizedType>()
                                                .getStorageType());
        auto new_op = builder.create<TFR::CastOp>(returned_op->getLoc(),
                                                  new_type, returned_op.arg());
        returned_value.set(new_op.getResult());
        if (returned_op.use_empty()) {
          returned_op.erase();
        }
      } else {
        returned_value.get().getDefiningOp()->emitError(
            "The producer of quantized type result should be a tfr.cast op.");
        return;
      }
    }

    auto new_func_type = builder.getFunctionType(block.getArgumentTypes(),
                                                 terminator->getOperandTypes());
    func.setType(new_func_type);
  });
}

// Creates an instance of the pass to decompose the TF ops.
std::unique_ptr<OperationPass<ModuleOp>> CreateRewriteQuantizedIOPass() {
  return std::make_unique<RewriteQuantizedIOPass>();
}

static PassRegistration<RewriteQuantizedIOPass> pass([] {
  return CreateRewriteQuantizedIOPass();
});

}  // namespace TFR
}  // namespace mlir
<commit_msg>Cleanup unnecessary code.<commit_after>/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <string>

#include "llvm/ADT/StringRef.h"
#include "mlir/Dialect/Quant/QuantTypes.h"  // from @llvm-project
#include "mlir/IR/Block.h"  // from @llvm-project
#include "mlir/IR/Builders.h"  // from @llvm-project
#include "mlir/IR/BuiltinTypes.h"  // from @llvm-project
#include "mlir/IR/Operation.h"  // from @llvm-project
#include "mlir/IR/Value.h"  // from @llvm-project
#include "mlir/Pass/Pass.h"  // from @llvm-project
#include "mlir/Pass/PassRegistry.h"  // from @llvm-project
#include "tensorflow/compiler/mlir/tfr/ir/tfr_ops.h"
#include "tensorflow/compiler/mlir/tfr/passes/passes.h"

namespace mlir {
namespace TFR {

class RewriteQuantizedIOPass
    : public PassWrapper<RewriteQuantizedIOPass, OperationPass<ModuleOp>> {
 public:
  StringRef getArgument() const final { return "tfr-rewrite-quantized-io"; }

  StringRef getDescription() const final {
    return "Replaces operands and results that has quantized type with their "
           "storage types.";
  }
  void runOnOperation() override;
};

void RewriteQuantizedIOPass::runOnOperation() {
  ModuleOp module = getOperation();
  OpBuilder builder(module);
  module.walk([&](FuncOp func) {
    Block& block = func.front();
    Operation* terminator = block.getTerminator();

    // Replace input_arg(tensor<quant_type>) -> tfr.cast
    // with input_arg(tensor<storage_type>) -> tfr.cast
    for (BlockArgument arg : block.getArguments()) {
      Type arg_type = arg.getType();
      if (auto quant_type = arg_type.cast<TensorType>()
                                .getElementType()
                                .dyn_cast<quant::QuantizedType>()) {
        if (arg.hasOneUse() && llvm::isa<TFR::CastOp>(*arg.user_begin())) {
          arg.setType(
              arg_type.cast<TensorType>().clone(quant_type.getStorageType()));
        } else {
          std::string error_message;
          llvm::raw_string_ostream os{error_message};
          os << "The argument with type ";
          arg.getType().print(os);
          os << " should have one user, which should be tfr.cast.";
          func->emitError(error_message);
          return;
        }
      }
    }

    builder.setInsertionPoint(terminator);
    // Replace tfr.cast(tensor<quant_type>) -> output
    // with tfr.cast(tensor<storage_type>) -> output
    for (OpOperand& returned_value : terminator->getOpOperands()) {
      auto returned_type =
          returned_value.get().getType().dyn_cast<TensorType>();
      if (!returned_type ||
          !returned_type.getElementType().isa<quant::QuantizedType>()) {
        continue;
      }

      if (auto returned_op =
              returned_value.get().getDefiningOp<TFR::CastOp>()) {
        auto new_type = returned_type.clone(returned_type.getElementType()
                                                .cast<quant::QuantizedType>()
                                                .getStorageType());
        auto new_op = builder.create<TFR::CastOp>(returned_op->getLoc(),
                                                  new_type, returned_op.arg());
        returned_value.set(new_op.getResult());
        if (returned_op.use_empty()) {
          returned_op.erase();
        }
      } else {
        returned_value.get().getDefiningOp()->emitError(
            "The producer of quantized type result should be a tfr.cast op.");
        return;
      }
    }

    auto new_func_type = builder.getFunctionType(block.getArgumentTypes(),
                                                 terminator->getOperandTypes());
    func.setType(new_func_type);
  });
}

// Creates an instance of the pass to decompose the TF ops.
std::unique_ptr<OperationPass<ModuleOp>> CreateRewriteQuantizedIOPass() {
  return std::make_unique<RewriteQuantizedIOPass>();
}

static PassRegistration<RewriteQuantizedIOPass> pass([] {
  return CreateRewriteQuantizedIOPass();
});

}  // namespace TFR
}  // namespace mlir
<|endoftext|>


<commit_before>//
// MateSTPath.hpp
//
// Copyright (c) 2012 Jun Kawahara
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef MATESTPATH_HPP
#define MATESTPATH_HPP

#include "StateFrontier.hpp"
#include "PseudoZDD.hpp"

namespace frontier_dd {

class MateSTPath;

//*************************************************************************************************
// StateSTPath: s-t パスのための State
class StateSTPath : public StateFrontier<mate_t> {
protected:
    int start_vertex_; // 始点 s
    int end_vertex_; // 終点 t
    bool is_hamilton_; // ハミルトンパスに限定するか（true の場合ハミルトンパス）
    bool is_cycle_; // s-t パスではなくてサイクルにするか（true の場合サイクル）
                    // true の場合、start_vertex_ と end_vertex_ は無視される

    //MateSTPath* global_mate_; // 処理速度効率化のため、MateSTPath オブジェクトを使いまわしする。
                                // アルゴリズムを通して、MateSTPath オブジェクトは一度だけ
                                // 作成される。そのオブジェクトを指すポインタ。

public:
    StateSTPath(Graph* graph) : StateFrontier<mate_t>(graph)
    {
        start_vertex = 1;
        end_vertex = graph->GetNumberOfVertices();
        is_hamilton_ = false;
        is_cycle_ = false;
    }

    virtual ~StateSTPath() { }

    int GetStartVertex() const
    {
        return start_vertex_;
    }

    int GetEndVertex() const
    {
        return end_vertex_;
    }

    void SetStartAndEndVertex(int start_vertex, int end_vertex)
    {
        start_vertex_ = start_vertex;
        end_vertex_ = end_vertex;
    }

    bool IsHamilton() const
    {
        return is_hamilton_;
    }

    void SetHamilton(bool is_hamilton)
    {
        is_hamilton_ = is_hamilton;
    }

    bool IsCycle() const
    {
        return is_cycle_;
    }

    void SetCycle(bool is_cycle)
    {
        is_cycle_ = is_cycle;
    }

    virtual Mate* MakeEmptyMate();
    //virtual void PackMate(ZDDNode* node, Mate* mate);
    virtual void UnpackMate(ZDDNode* node, Mate* mate, int child_num);
};

//*************************************************************************************************
// MateSTPath
class MateSTPath : public MateFrontier<mate_t> {
public:
    MateSTPath(State* state) : MateFrontier<mate_t>(state) { }

    virtual void UpdateMate(State* state, int child_num);
    virtual int CheckTerminalPre(State* state, int child_num);
    virtual int CheckTerminalPost(State* state);
};

} // the end of the namespace

#endif // MATESTPATH_HPP
<commit_msg>Fixed typo<commit_after>//
// MateSTPath.hpp
//
// Copyright (c) 2012 Jun Kawahara
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#ifndef MATESTPATH_HPP
#define MATESTPATH_HPP

#include "StateFrontier.hpp"
#include "PseudoZDD.hpp"

namespace frontier_dd {

class MateSTPath;

//*************************************************************************************************
// StateSTPath: s-t パスのための State
class StateSTPath : public StateFrontier<mate_t> {
protected:
    int start_vertex_; // 始点 s
    int end_vertex_; // 終点 t
    bool is_hamilton_; // ハミルトンパスに限定するか（true の場合ハミルトンパス）
    bool is_cycle_; // s-t パスではなくてサイクルにするか（true の場合サイクル）
                    // true の場合、start_vertex_ と end_vertex_ は無視される

    //MateSTPath* global_mate_; // 処理速度効率化のため、MateSTPath オブジェクトを使いまわしする。
                                // アルゴリズムを通して、MateSTPath オブジェクトは一度だけ
                                // 作成される。そのオブジェクトを指すポインタ。

public:
    StateSTPath(Graph* graph) : StateFrontier<mate_t>(graph)
    {
        start_vertex_ = 1;
        end_vertex_ = graph->GetNumberOfVertices();
        is_hamilton_ = false;
        is_cycle_ = false;
    }

    virtual ~StateSTPath() { }

    int GetStartVertex() const
    {
        return start_vertex_;
    }

    int GetEndVertex() const
    {
        return end_vertex_;
    }

    void SetStartAndEndVertex(int start_vertex, int end_vertex)
    {
        start_vertex_ = start_vertex;
        end_vertex_ = end_vertex;
    }

    bool IsHamilton() const
    {
        return is_hamilton_;
    }

    void SetHamilton(bool is_hamilton)
    {
        is_hamilton_ = is_hamilton;
    }

    bool IsCycle() const
    {
        return is_cycle_;
    }

    void SetCycle(bool is_cycle)
    {
        is_cycle_ = is_cycle;
    }

    virtual Mate* MakeEmptyMate();
    //virtual void PackMate(ZDDNode* node, Mate* mate);
    virtual void UnpackMate(ZDDNode* node, Mate* mate, int child_num);
};

//*************************************************************************************************
// MateSTPath
class MateSTPath : public MateFrontier<mate_t> {
public:
    MateSTPath(State* state) : MateFrontier<mate_t>(state) { }

    virtual void UpdateMate(State* state, int child_num);
    virtual int CheckTerminalPre(State* state, int child_num);
    virtual int CheckTerminalPost(State* state);
};

} // the end of the namespace

#endif // MATESTPATH_HPP
<|endoftext|>