<commit_before>// ======================================================================== //
// Copyright 2009-2015 Intel 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.                                           //
// ======================================================================== //

#include "ospray/include/ospray/ospray.h"
#include "ospray/render/Renderer.h"
#include "ospray/camera/Camera.h"
#include "ospray/common/Material.h"
#include "ospray/volume/Volume.h"
#include "ospray/transferFunction/TransferFunction.h"
#include "LocalDevice.h"
#include "ospray/common/OSPCommon.h"

#if 1
# define LOG(a) if (ospray::logLevel > 2) std::cout << "#ospray: " << a << std::endl;
#else
# define LOG(a) /*ignore*/
#endif

/*! \file api.cpp implements the public ospray api functions by
  routing them to a respective \ref device */
namespace ospray {

  using std::endl;
  using std::cout;

#if OSPRAY_MPI
  namespace mpi {
    ospray::api::Device *createMPI_ListenForWorkers(int *ac, const char **av, 
                                                    const char *fileNameToStorePortIn);
    ospray::api::Device *createMPI_LaunchWorkerGroup(int *ac, const char **av, 
                                                     const char *launchCommand);
    ospray::api::Device *createMPI_RanksBecomeWorkers(int *ac, const char **av);
  }
#endif
#if OSPRAY_MIC_COI
  namespace coi {
    ospray::api::Device *createCoiDevice(int *ac, const char **av);
  }
#endif


#define ASSERT_DEVICE() if (ospray::api::Device::current == NULL)     \
    throw std::runtime_error("OSPRay not yet initialized "            \
                             "(most likely this means you tried to "  \
                             "call an ospray API function before "    \
                             "first calling ospInit())");

  
  extern "C" void ospInit(int *_ac, const char **_av) 
  {
    if (ospray::api::Device::current) 
      throw std::runtime_error("OSPRay error: device already exists "
                               "(did you call ospInit twice?)");

    /* call ospray::init to properly parse common args like
       --osp:verbose, --osp:debug etc */
    ospray::init(_ac,&_av);

    const char *OSP_MPI_LAUNCH_FROM_ENV = getenv("OSPRAY_MPI_LAUNCH");

    if (OSP_MPI_LAUNCH_FROM_ENV) {
#if OSPRAY_MPI
      std::cout << "#osp: launching ospray mpi ring - make sure that mpd is running" << std::endl;
      ospray::api::Device::current
        = mpi::createMPI_LaunchWorkerGroup(_ac,_av,OSP_MPI_LAUNCH_FROM_ENV);
#else
      throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
    }

    if (_ac && _av) {
      // we're only supporting local rendering for now - network device
      // etc to come.
      for (int i=1;i<*_ac;i++) {

        if (std::string(_av[i]) == "--osp:mpi") {
#if OSPRAY_MPI
          removeArgs(*_ac,(char **&)_av,i,1);
          ospray::api::Device::current
            = mpi::createMPI_RanksBecomeWorkers(_ac,_av);
#else
          throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
          --i; continue;
        }
        if (std::string(_av[i]) == "--osp:coi") {
#if OSPRAY_TARGET_MIC
          throw std::runtime_error("The COI device can only be created on the host");
#elif OSPRAY_MIC_COI
          removeArgs(*_ac,(char **&)_av,i,1);
          ospray::api::Device::current
            = ospray::coi::createCoiDevice(_ac,_av);
#else
          throw std::runtime_error("OSPRay's COI support not compiled in");
#endif
          --i; continue;
        }

        if (std::string(_av[i]) == "--osp:mpi-launch") {
#if OSPRAY_MPI
          if (i+2 > *_ac)
            throw std::runtime_error("--osp:mpi-launch expects an argument");
          const char *launchCommand = strdup(_av[i+1]);
          removeArgs(*_ac,(char **&)_av,i,2);
          ospray::api::Device::current
            = mpi::createMPI_LaunchWorkerGroup(_ac,_av,launchCommand);
#else
          throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
          --i; continue;
        }
        const char *listenArgName = "--osp:mpi-listen";
        if (!strncmp(_av[i],listenArgName,strlen(listenArgName))) {
#if OSPRAY_MPI
          const char *fileNameToStorePortIn = NULL;
          if (strlen(_av[i]) > strlen(listenArgName)) {
            fileNameToStorePortIn = strdup(_av[i]+strlen(listenArgName)+1);
          }
          removeArgs(*_ac,(char **&)_av,i,1);
          ospray::api::Device::current
            = mpi::createMPI_ListenForWorkers(_ac,_av,fileNameToStorePortIn);
#else
          throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
          --i; continue;
        }
      }
    }
    
    // no device created on cmd line, yet, so default to localdevice
    if (ospray::api::Device::current == NULL)
      ospray::api::Device::current = new ospray::api::LocalDevice(_ac,_av);
  } 


  /*! destroy a given frame buffer. 

    due to internal reference counting the framebuffer may or may not be deleted immeidately
  */
  extern "C" void ospFreeFrameBuffer(OSPFrameBuffer fb)
  {
    ASSERT_DEVICE();
    Assert(fb != NULL);
    std::cout << "warning: ospFreeFrameBuffer not yet implemented - ignoring (this means there is a memory hole!)" << std::endl;
  }

  extern "C" OSPFrameBuffer ospNewFrameBuffer(const osp::vec2i &size, 
                                              const OSPFrameBufferFormat mode,
                                              const int channels)
  {
    ASSERT_DEVICE();
    return ospray::api::Device::current->frameBufferCreate(size,mode,channels);
  }

  //! load module \<name\> from shard lib libospray_module_\<name\>.so, or 
  extern "C" error_t ospLoadModule(const char *moduleName)
  {
    ASSERT_DEVICE();
    return ospray::api::Device::current->loadModule(moduleName);
  }

  extern "C" const void *ospMapFrameBuffer(OSPFrameBuffer fb, 
                                           OSPFrameBufferChannel channel)
  {
    ASSERT_DEVICE();
    return ospray::api::Device::current->frameBufferMap(fb,channel);
  }
  
  extern "C" void ospUnmapFrameBuffer(const void *mapped,
                                      OSPFrameBuffer fb)
  {<commit_msg>ospFreeFrameBuffer() now releases FB object.<commit_after>// ======================================================================== //
// Copyright 2009-2015 Intel 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.                                           //
// ======================================================================== //

#include "ospray/include/ospray/ospray.h"
#include "ospray/render/Renderer.h"
#include "ospray/camera/Camera.h"
#include "ospray/common/Material.h"
#include "ospray/volume/Volume.h"
#include "ospray/transferFunction/TransferFunction.h"
#include "LocalDevice.h"
#include "ospray/common/OSPCommon.h"

#if 1
# define LOG(a) if (ospray::logLevel > 2) std::cout << "#ospray: " << a << std::endl;
#else
# define LOG(a) /*ignore*/
#endif

/*! \file api.cpp implements the public ospray api functions by
  routing them to a respective \ref device */
namespace ospray {

  using std::endl;
  using std::cout;

#if OSPRAY_MPI
  namespace mpi {
    ospray::api::Device *createMPI_ListenForWorkers(int *ac, const char **av, 
                                                    const char *fileNameToStorePortIn);
    ospray::api::Device *createMPI_LaunchWorkerGroup(int *ac, const char **av, 
                                                     const char *launchCommand);
    ospray::api::Device *createMPI_RanksBecomeWorkers(int *ac, const char **av);
  }
#endif
#if OSPRAY_MIC_COI
  namespace coi {
    ospray::api::Device *createCoiDevice(int *ac, const char **av);
  }
#endif


#define ASSERT_DEVICE() if (ospray::api::Device::current == NULL)     \
    throw std::runtime_error("OSPRay not yet initialized "            \
                             "(most likely this means you tried to "  \
                             "call an ospray API function before "    \
                             "first calling ospInit())");

  
  extern "C" void ospInit(int *_ac, const char **_av) 
  {
    if (ospray::api::Device::current) 
      throw std::runtime_error("OSPRay error: device already exists "
                               "(did you call ospInit twice?)");

    /* call ospray::init to properly parse common args like
       --osp:verbose, --osp:debug etc */
    ospray::init(_ac,&_av);

    const char *OSP_MPI_LAUNCH_FROM_ENV = getenv("OSPRAY_MPI_LAUNCH");

    if (OSP_MPI_LAUNCH_FROM_ENV) {
#if OSPRAY_MPI
      std::cout << "#osp: launching ospray mpi ring - make sure that mpd is running" << std::endl;
      ospray::api::Device::current
        = mpi::createMPI_LaunchWorkerGroup(_ac,_av,OSP_MPI_LAUNCH_FROM_ENV);
#else
      throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
    }

    if (_ac && _av) {
      // we're only supporting local rendering for now - network device
      // etc to come.
      for (int i=1;i<*_ac;i++) {

        if (std::string(_av[i]) == "--osp:mpi") {
#if OSPRAY_MPI
          removeArgs(*_ac,(char **&)_av,i,1);
          ospray::api::Device::current
            = mpi::createMPI_RanksBecomeWorkers(_ac,_av);
#else
          throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
          --i; continue;
        }
        if (std::string(_av[i]) == "--osp:coi") {
#if OSPRAY_TARGET_MIC
          throw std::runtime_error("The COI device can only be created on the host");
#elif OSPRAY_MIC_COI
          removeArgs(*_ac,(char **&)_av,i,1);
          ospray::api::Device::current
            = ospray::coi::createCoiDevice(_ac,_av);
#else
          throw std::runtime_error("OSPRay's COI support not compiled in");
#endif
          --i; continue;
        }

        if (std::string(_av[i]) == "--osp:mpi-launch") {
#if OSPRAY_MPI
          if (i+2 > *_ac)
            throw std::runtime_error("--osp:mpi-launch expects an argument");
          const char *launchCommand = strdup(_av[i+1]);
          removeArgs(*_ac,(char **&)_av,i,2);
          ospray::api::Device::current
            = mpi::createMPI_LaunchWorkerGroup(_ac,_av,launchCommand);
#else
          throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
          --i; continue;
        }
        const char *listenArgName = "--osp:mpi-listen";
        if (!strncmp(_av[i],listenArgName,strlen(listenArgName))) {
#if OSPRAY_MPI
          const char *fileNameToStorePortIn = NULL;
          if (strlen(_av[i]) > strlen(listenArgName)) {
            fileNameToStorePortIn = strdup(_av[i]+strlen(listenArgName)+1);
          }
          removeArgs(*_ac,(char **&)_av,i,1);
          ospray::api::Device::current
            = mpi::createMPI_ListenForWorkers(_ac,_av,fileNameToStorePortIn);
#else
          throw std::runtime_error("OSPRay MPI support not compiled in");
#endif
          --i; continue;
        }
      }
    }
    
    // no device created on cmd line, yet, so default to localdevice
    if (ospray::api::Device::current == NULL)
      ospray::api::Device::current = new ospray::api::LocalDevice(_ac,_av);
  } 


  /*! destroy a given frame buffer. 

    due to internal reference counting the framebuffer may or may not be deleted immeidately
  */
  extern "C" void ospFreeFrameBuffer(OSPFrameBuffer fb)
  {
    ASSERT_DEVICE();
    Assert(fb != NULL);
    ospray::api::Device::current->release(fb);
  }

  extern "C" OSPFrameBuffer ospNewFrameBuffer(const osp::vec2i &size, 
                                              const OSPFrameBufferFormat mode,
                                              const int channels)
  {
    ASSERT_DEVICE();
    return ospray::api::Device::current->frameBufferCreate(size,mode,channels);
  }

  //! load module \<name\> from shard lib libospray_module_\<name\>.so, or 
  extern "C" error_t ospLoadModule(const char *moduleName)
  {
    ASSERT_DEVICE();
    return ospray::api::Device::current->loadModule(moduleName);
  }

  extern "C" const void *ospMapFrameBuffer(OSPFrameBuffer fb, 
                                           OSPFrameBufferChannel channel)
  {
    ASSERT_DEVICE();
    return ospray::api::Device::current->frameBufferMap(fb,channel);
  }
  
  extern "C" void ospUnmapFrameBuffer(const void *mapped,
                                      OSPFrameBuffer fb)
  {
{'fraction_non_alphanumeric': 0.14051754696915988, 'fraction_numerical': 0.0032612548741581, 'mean_word_length': 2.1863564490625707, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4182219', 'n_tokens_mistral': 4164, 'n_tokens_neox': 4012, 'n_words': 1016}