<commit_before>#ifndef DUNE_STUFF_RANGES_RANGES_HH
#define DUNE_STUFF_RANGES_RANGES_HH

#ifdef HAVE_CMAKE_CONFIG
 #include "cmake_config.h"
#else
 #include "config.h"
#endif // ifdef HAVE_CMAKE_CONFIG

#if HAVE_DUNE_GRID
#include <dune/stuff/common/disable_warnings.hh>
  #include <dune/grid/common/gridview.hh>
#include <dune/stuff/common/reenable_warnings.hh>
#include <boost/serialization/static_warning.hpp>
#endif

#if HAVE_DUNE_FEM
#include <dune/fem/version.hh>
#include <dune/stuff/common/disable_warnings.hh>
  #include <dune/fem/function/common/discretefunction.hh>
#include <dune/stuff/common/reenable_warnings.hh>
#include <dune/fem/gridpart/common/gridpart.hh>
#endif
#include <dune/stuff/common/math.hh>
#include <dune/stuff/fem/namespace.hh>

namespace Dune {

#if HAVE_DUNE_FEM
namespace Fem {

template < class DiscreteFunctionTraits >
auto begin( const Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dbegin())
{
  return func.dbegin();
}

template < class DiscreteFunctionTraits >
auto end( const Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dend())
{
  return func.dend();
}

template < class DiscreteFunctionTraits >
auto begin( Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dbegin())
{
  return func.dbegin();
}

template < class DiscreteFunctionTraits >
auto end( Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dend())
{
  return func.dend();
}

} // namespace Fem
#endif

} //namespace Dune

namespace Dune {
namespace Stuff {
namespace Common {

#if HAVE_DUNE_GRID
//! adapter enabling view usage in range-based for
template < class GridViewType, int codim = 0>
class ViewRange {
  const GridViewType& view_;
public:
  ViewRange(const GridViewType& view)
    :view_(view)
  {
    BOOST_STATIC_WARNING(codim == 0 && "unnecessary ViewRange usage with codim 0");
  }

  typename GridViewType::template Codim< codim >::Iterator
  begin() const {
    return view_.template begin< codim >();
  }
  typename GridViewType::template Codim< codim >::Iterator
  end() const {
    return view_.template end< codim >();
  }
};

template < class GridViewTraits, int codim = 0>
ViewRange<Dune::GridView<GridViewTraits>, codim> viewRange(const Dune::GridView<GridViewTraits>& view)
{
    return ViewRange<Dune::GridView<GridViewTraits>, codim>(view);
}

/** adapter enabling intersectionniterator usage in range-based for
 * works for GridParts and GridViews
 */
template < class GridAbstractionType, class EntityType >
class IntersectionRange {
  const GridAbstractionType& view_;
  const EntityType& entity_;
public:
  IntersectionRange(const GridAbstractionType& view, const EntityType& entity)
    : view_(view)
    , entity_(entity)
  {}

  auto begin() const -> decltype(view_.ibegin(entity_)) {
    return view_.ibegin(entity_);
  }

  auto end() const -> decltype(view_.iend(entity_)) {
    return view_.iend(entity_);
  }
};

template < class GridViewTraits>
IntersectionRange<Dune::GridView<GridViewTraits>,
                  typename Dune::GridView<GridViewTraits>::template Codim< 0 >::Entity>
intersectionRange(const Dune::GridView<GridViewTraits>& gridview,
                  const typename Dune::GridView<GridViewTraits>::template Codim< 0 >::Entity& entity)
{
  return IntersectionRange<Dune::GridView<GridViewTraits>,
          typename Dune::GridView<GridViewTraits>::template Codim< 0 >::Entity>(gridview, entity);
}

#endif //#if HAVE_DUNE_GRID

#if HAVE_DUNE_FEM

//! Range adapter for lagrange points from lagrange spaces
template < class DiscreteFunctionspaceType, int faceCodim >
class LagrangePointSetRange {
  typedef typename DiscreteFunctionspaceType::LagrangePointSetType
    LagrangePointSetType;
  typedef typename LagrangePointSetType::template Codim< faceCodim >::SubEntityIteratorType
    SubEntityIteratorType;
  const LagrangePointSetType& lp_set_;
  const unsigned int subEntity_;

public:
  /** the template isn't lazyness here, the underlying set is templated on it too
   */
  template < class EntityType >
  LagrangePointSetRange(const DiscreteFunctionspaceType& space, const EntityType& entity, const unsigned int subEntity)
    : lp_set_(space.lagrangePointSet(entity))
    , subEntity_(subEntity)
  {}

  SubEntityIteratorType begin() const {
    return lp_set_.template beginSubEntity< faceCodim >(subEntity_);
  }
  SubEntityIteratorType end() const {
    return lp_set_.template endSubEntity< faceCodim >(subEntity_);
  }
};

template < int codim, class DiscreteFunctionspaceType, class EntityType >
LagrangePointSetRange<DiscreteFunctionspaceType,codim>
lagrangePointSetRange(const DiscreteFunctionspaceType& space, const EntityType& entity, const int subEntity) {
  return LagrangePointSetRange<DiscreteFunctionspaceType,codim>(space, entity, subEntity);
}

template < class GridPartTraits >
IntersectionRange<Dune::Fem::GridPartInterface<GridPartTraits>,
                  typename Dune::Fem::GridPartInterface<GridPartTraits>::template Codim< 0 >::EntityType>
intersectionRange(const Dune::Fem::GridPartInterface<GridPartTraits>& gridpart,
                  const typename Dune::Fem::GridPartInterface<GridPartTraits>::template Codim< 0 >::EntityType& entity)
{
  return IntersectionRange<Dune::Fem::GridPartInterface<GridPartTraits>,
          typename Dune::Fem::GridPartInterface<GridPartTraits>::template Codim< 0 >::EntityType>(gridpart, entity);
}
#endif //HAVE_DUNE_FEM

//! get a vector with values in [start : increment : end)
template < class T, class sequence = std::vector<T> >
sequence valueRange(const T start, const T end, const T increment = Epsilon<T>::value) {
  sequence ret(typename sequence::size_type(((end>start) ? end-start : start-end)/increment), start);
  typename sequence::size_type i = 0;
  std::generate(std::begin(ret), std::end(ret), [&](){ return T(start + (increment * i++)); });
  return ret;
}

//! get a vector with values in [0 : Epsilon<T> : end)
template < class T, class sequence = std::vector<T> >
sequence valueRange(const T end) {
  return valueRange(T(0),end);
}

} // namespace Common
} // namespace Stuff
} // namespace Dune

#endif // DUNE_STUFF_RANGES_RANGES_HH
/** Copyright (c) 2012, Felix Albrecht, Rene Milk
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of source code must retain the above copyright notice, this
   *    list of conditions and the following disclaimer.
   * 2. 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.
   *
   * 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.
   *
   * The views and conclusions contained in the software and documentation are those
   * of the authors and should not be interpreted as representing official policies,
   * either expressed or implied, of the FreeBSD Project.
   **/
<commit_msg>[common] fix value ranges with neg. stepsize<commit_after>#ifndef DUNE_STUFF_RANGES_RANGES_HH
#define DUNE_STUFF_RANGES_RANGES_HH

#ifdef HAVE_CMAKE_CONFIG
 #include "cmake_config.h"
#else
 #include "config.h"
#endif // ifdef HAVE_CMAKE_CONFIG

#if HAVE_DUNE_GRID
#include <dune/stuff/common/disable_warnings.hh>
  #include <dune/grid/common/gridview.hh>
#include <dune/stuff/common/reenable_warnings.hh>
#include <boost/serialization/static_warning.hpp>
#endif

#if HAVE_DUNE_FEM
#include <dune/fem/version.hh>
#include <dune/stuff/common/disable_warnings.hh>
  #include <dune/fem/function/common/discretefunction.hh>
#include <dune/stuff/common/reenable_warnings.hh>
#include <dune/fem/gridpart/common/gridpart.hh>
#endif
#include <dune/stuff/common/math.hh>
#include <dune/stuff/fem/namespace.hh>

namespace Dune {

#if HAVE_DUNE_FEM
namespace Fem {

template < class DiscreteFunctionTraits >
auto begin( const Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dbegin())
{
  return func.dbegin();
}

template < class DiscreteFunctionTraits >
auto end( const Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dend())
{
  return func.dend();
}

template < class DiscreteFunctionTraits >
auto begin( Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dbegin())
{
  return func.dbegin();
}

template < class DiscreteFunctionTraits >
auto end( Dune::Fem::DiscreteFunctionInterface< DiscreteFunctionTraits >& func )
  -> decltype(func.dend())
{
  return func.dend();
}

} // namespace Fem
#endif

} //namespace Dune

namespace Dune {
namespace Stuff {
namespace Common {

#if HAVE_DUNE_GRID
//! adapter enabling view usage in range-based for
template < class GridViewType, int codim = 0>
class ViewRange {
  const GridViewType& view_;
public:
  ViewRange(const GridViewType& view)
    :view_(view)
  {
    BOOST_STATIC_WARNING(codim == 0 && "unnecessary ViewRange usage with codim 0");
  }

  typename GridViewType::template Codim< codim >::Iterator
  begin() const {
    return view_.template begin< codim >();
  }
  typename GridViewType::template Codim< codim >::Iterator
  end() const {
    return view_.template end< codim >();
  }
};

template < class GridViewTraits, int codim = 0>
ViewRange<Dune::GridView<GridViewTraits>, codim> viewRange(const Dune::GridView<GridViewTraits>& view)
{
    return ViewRange<Dune::GridView<GridViewTraits>, codim>(view);
}

/** adapter enabling intersectionniterator usage in range-based for
 * works for GridParts and GridViews
 */
template < class GridAbstractionType, class EntityType >
class IntersectionRange {
  const GridAbstractionType& view_;
  const EntityType& entity_;
public:
  IntersectionRange(const GridAbstractionType& view, const EntityType& entity)
    : view_(view)
    , entity_(entity)
  {}

  auto begin() const -> decltype(view_.ibegin(entity_)) {
    return view_.ibegin(entity_);
  }

  auto end() const -> decltype(view_.iend(entity_)) {
    return view_.iend(entity_);
  }
};

template < class GridViewTraits>
IntersectionRange<Dune::GridView<GridViewTraits>,
                  typename Dune::GridView<GridViewTraits>::template Codim< 0 >::Entity>
intersectionRange(const Dune::GridView<GridViewTraits>& gridview,
                  const typename Dune::GridView<GridViewTraits>::template Codim< 0 >::Entity& entity)
{
  return IntersectionRange<Dune::GridView<GridViewTraits>,
          typename Dune::GridView<GridViewTraits>::template Codim< 0 >::Entity>(gridview, entity);
}

#endif //#if HAVE_DUNE_GRID

#if HAVE_DUNE_FEM

//! Range adapter for lagrange points from lagrange spaces
template < class DiscreteFunctionspaceType, int faceCodim >
class LagrangePointSetRange {
  typedef typename DiscreteFunctionspaceType::LagrangePointSetType
    LagrangePointSetType;
  typedef typename LagrangePointSetType::template Codim< faceCodim >::SubEntityIteratorType
    SubEntityIteratorType;
  const LagrangePointSetType& lp_set_;
  const unsigned int subEntity_;

public:
  /** the template isn't lazyness here, the underlying set is templated on it too
   */
  template < class EntityType >
  LagrangePointSetRange(const DiscreteFunctionspaceType& space, const EntityType& entity, const unsigned int subEntity)
    : lp_set_(space.lagrangePointSet(entity))
    , subEntity_(subEntity)
  {}

  SubEntityIteratorType begin() const {
    return lp_set_.template beginSubEntity< faceCodim >(subEntity_);
  }
  SubEntityIteratorType end() const {
    return lp_set_.template endSubEntity< faceCodim >(subEntity_);
  }
};

template < int codim, class DiscreteFunctionspaceType, class EntityType >
LagrangePointSetRange<DiscreteFunctionspaceType,codim>
lagrangePointSetRange(const DiscreteFunctionspaceType& space, const EntityType& entity, const int subEntity) {
  return LagrangePointSetRange<DiscreteFunctionspaceType,codim>(space, entity, subEntity);
}

template < class GridPartTraits >
IntersectionRange<Dune::Fem::GridPartInterface<GridPartTraits>,
                  typename Dune::Fem::GridPartInterface<GridPartTraits>::template Codim< 0 >::EntityType>
intersectionRange(const Dune::Fem::GridPartInterface<GridPartTraits>& gridpart,
                  const typename Dune::Fem::GridPartInterface<GridPartTraits>::template Codim< 0 >::EntityType& entity)
{
  return IntersectionRange<Dune::Fem::GridPartInterface<GridPartTraits>,
          typename Dune::Fem::GridPartInterface<GridPartTraits>::template Codim< 0 >::EntityType>(gridpart, entity);
}
#endif //HAVE_DUNE_FEM

//! get a vector with values in [start : increment : end)
template < class T, class sequence = std::vector<T> >
sequence valueRange(const T start, const T end, const T increment = Epsilon<T>::value) {
  sequence ret(typename sequence::size_type(((end>start) ? end-start : start-end)/std::abs(increment)), start);
  typename sequence::size_type i = 0;
  std::generate(std::begin(ret), std::end(ret), [&](){ return T(start + (increment * i++)); });
  return ret;
}

//! get a vector with values in [0 : Epsilon<T> : end)
template < class T, class sequence = std::vector<T> >
sequence valueRange(const T end) {
  return valueRange(T(0),end);
}

} // namespace Common
} // namespace Stuff
} // namespace Dune

#endif // DUNE_STUFF_RANGES_RANGES_HH
/** Copyright (c) 2012, Felix Albrecht, Rene Milk
   * All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of source code must retain the above copyright notice, this
   *    list of conditions and the following disclaimer.
   * 2. 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.
   *
   * 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.
   *
   * The views and conclusions contained in the software and documentation are those
   * of the authors and should not be interpreted as representing official policies,
   * either expressed or implied, of the FreeBSD Project.
   **/
<|endoftext|>


<commit_before>/*
Copyright (c) 2012, William Magato
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. 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.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) 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 HOLDER(S) 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.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the copyright holder(s) or contributors.
*/

#include <cstdint>
#include <cstring>

#include <ios>
#include <stdexcept>

#include <xen/xen.h>
#include <xen/features.h>
#include <xen/version.h>

#include <llamaos/memory/memory.h>
#include <llamaos/xen/Hypercall.h>
#include <llamaos/xen/Hypervisor.h>
#include <llamaos/config.h>
#include <llamaos/trace.h>

using namespace std;
using namespace llamaos;
using namespace llamaos::xen;

namespace llamaos {
namespace memory {

// needs initialized by startup logic
extern uint64_t *machine_table;
extern uint64_t  machine_table_size;
extern uint64_t *pseudo_table;
extern uint64_t  pseudo_table_size;

} }

// runtime stack memory
char RUNTIME_STACK [2 * llamaos::STACK_SIZE];

uint8_t xen_features [XENFEAT_NR_SUBMAPS * 32];

static bool verify_magic (const start_info_t *start_info)
{
   if (   (nullptr != start_info)
       && (0 == strncmp (start_info->magic, "xen-", 4)))
   {
      return true;
   }

   return false;
}

static void trace_startup (const start_info_t *start_info)
{
   trace ("\n\n\n*********************************\n");
   trace (      "****  starting llamaOS (Xen)  ***\n");
   trace (      "*********************************\n\n\n");

   trace ("%s\n\n", VERSION_TEXT);

   trace ("=== start_info ===\n");
   trace ("  magic: %s\n", start_info->magic);
   trace ("  nr_pages: %x\n", start_info->nr_pages);
   trace ("  shared_info: %x\n", start_info->shared_info);
   trace ("  flags: %x\n", start_info->flags);
   trace ("  store_mfn: %x\n", start_info->store_mfn);
   trace ("  store_evtchn: %x\n", start_info->store_evtchn);
   trace ("  console.domU.mfn: %x\n", start_info->console.domU.mfn);
   trace ("  console.domU.evtchn: %x\n", start_info->console.domU.evtchn);
   trace ("  pt_base: %x\n", start_info->pt_base);
   trace ("  nr_pt_frames: %x\n", start_info->nr_pt_frames);
   trace ("  mfn_list: %x\n", start_info->mfn_list);
   trace ("  mod_start: %x\n", start_info->mod_start);
   trace ("  mod_len: %x\n", start_info->mod_len);
   trace ("  cmd_line: %s\n", start_info->cmd_line);
   trace ("  first_p2m_pfn: %x\n", start_info->first_p2m_pfn);
   trace ("  nr_p2m_frames: %x\n", start_info->nr_p2m_frames);
   trace ("\n");
}

extern int main (int, char*[]);

void register_glibc_exports ();

typedef void (*func_ptr) (void);
extern func_ptr __CTOR_LIST__[];
extern func_ptr __DTOR_LIST__[];

static const char *program_name = "llamaOS";

extern "C"
void start (start_info_t *start_info)
{
   if (verify_magic (start_info))
   {
      trace_startup (start_info);

      // initialize memory management
      memory::machine_table = memory::address_to_pointer<uint64_t>(MACH2PHYS_VIRT_START);
      memory::machine_table_size = MACH2PHYS_NR_ENTRIES;
      memory::pseudo_table = memory::address_to_pointer<uint64_t> (start_info->mfn_list);
      memory::pseudo_table_size = start_info->nr_pages;
      memory::initialize (start_info->pt_base, start_info->nr_pages);

      // register callbacks to the glibc library
      register_glibc_exports ();

      uint64_t ctor_size = reinterpret_cast<uint64_t>(__CTOR_LIST__[0]);
      trace ("__CTOR_LIST__[0]: %lx\n", ctor_size);
      for (uint64_t i = ctor_size; i >= 1; i--)
      {
         __CTOR_LIST__[i] ();
      }

      // initialize libstdc++
      ios_base::Init ios_base_init;

      try
      {
         // create the one and only hypervisor object
         trace ("Creating Hypervisor...\n");
         Hypervisor hypervisor (start_info);

         // start the application
         char *argv [2];
         argv [0] = const_cast<char *>(program_name);
         argv [1] = 0;
         main (1, argv);

         trace ("ending llamaOS...\n");

         for (;;);
      }
      catch (const std::runtime_error &e)
      {
         trace ("*** runtime_error: %s ***\n", e.what ());
      }
      catch (...)
      {
         trace ("*** unknown exception ***\n");
      }

      Hypercall::sched_op_shutdown ();

      uint64_t dtor_size = reinterpret_cast<uint64_t>(__DTOR_LIST__[0]);
      trace ("__DTOR_LIST__[0]: %lx\n", dtor_size);
      for (uint64_t i = dtor_size; i >= 1; i--)
      {
         __DTOR_LIST__[i] ();
      }
   }
   // else
   // something terrible is wrong and nothing can be done about it!
}
<commit_msg>yield at close of start to stop 100 pct cpu<commit_after>/*
Copyright (c) 2012, William Magato
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. 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.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) 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 HOLDER(S) 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.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the copyright holder(s) or contributors.
*/

#include <cstdint>
#include <cstring>

#include <ios>
#include <stdexcept>

#include <xen/xen.h>
#include <xen/features.h>
#include <xen/version.h>

#include <llamaos/memory/memory.h>
#include <llamaos/xen/Hypercall.h>
#include <llamaos/xen/Hypervisor.h>
#include <llamaos/config.h>
#include <llamaos/trace.h>

using namespace std;
using namespace llamaos;
using namespace llamaos::xen;

namespace llamaos {
namespace memory {

// needs initialized by startup logic
extern uint64_t *machine_table;
extern uint64_t  machine_table_size;
extern uint64_t *pseudo_table;
extern uint64_t  pseudo_table_size;

} }

// runtime stack memory
char RUNTIME_STACK [2 * llamaos::STACK_SIZE];

uint8_t xen_features [XENFEAT_NR_SUBMAPS * 32];

static bool verify_magic (const start_info_t *start_info)
{
   if (   (nullptr != start_info)
       && (0 == strncmp (start_info->magic, "xen-", 4)))
   {
      return true;
   }

   return false;
}

static void trace_startup (const start_info_t *start_info)
{
   trace ("\n\n\n*********************************\n");
   trace (      "****  starting llamaOS (Xen)  ***\n");
   trace (      "*********************************\n\n\n");

   trace ("%s\n\n", VERSION_TEXT);

   trace ("=== start_info ===\n");
   trace ("  magic: %s\n", start_info->magic);
   trace ("  nr_pages: %x\n", start_info->nr_pages);
   trace ("  shared_info: %x\n", start_info->shared_info);
   trace ("  flags: %x\n", start_info->flags);
   trace ("  store_mfn: %x\n", start_info->store_mfn);
   trace ("  store_evtchn: %x\n", start_info->store_evtchn);
   trace ("  console.domU.mfn: %x\n", start_info->console.domU.mfn);
   trace ("  console.domU.evtchn: %x\n", start_info->console.domU.evtchn);
   trace ("  pt_base: %x\n", start_info->pt_base);
   trace ("  nr_pt_frames: %x\n", start_info->nr_pt_frames);
   trace ("  mfn_list: %x\n", start_info->mfn_list);
   trace ("  mod_start: %x\n", start_info->mod_start);
   trace ("  mod_len: %x\n", start_info->mod_len);
   trace ("  cmd_line: %s\n", start_info->cmd_line);
   trace ("  first_p2m_pfn: %x\n", start_info->first_p2m_pfn);
   trace ("  nr_p2m_frames: %x\n", start_info->nr_p2m_frames);
   trace ("\n");
}

extern int main (int, char*[]);

void register_glibc_exports ();

typedef void (*func_ptr) (void);
extern func_ptr __CTOR_LIST__[];
extern func_ptr __DTOR_LIST__[];

static const char *program_name = "llamaOS";

extern "C"
void start (start_info_t *start_info)
{
   if (verify_magic (start_info))
   {
      trace_startup (start_info);

      // initialize memory management
      memory::machine_table = memory::address_to_pointer<uint64_t>(MACH2PHYS_VIRT_START);
      memory::machine_table_size = MACH2PHYS_NR_ENTRIES;
      memory::pseudo_table = memory::address_to_pointer<uint64_t> (start_info->mfn_list);
      memory::pseudo_table_size = start_info->nr_pages;
      memory::initialize (start_info->pt_base, start_info->nr_pages);

      // register callbacks to the glibc library
      register_glibc_exports ();

      uint64_t ctor_size = reinterpret_cast<uint64_t>(__CTOR_LIST__[0]);
      trace ("__CTOR_LIST__[0]: %lx\n", ctor_size);
      for (uint64_t i = ctor_size; i >= 1; i--)
      {
         __CTOR_LIST__[i] ();
      }

      // initialize libstdc++
      ios_base::Init ios_base_init;

      try
      {
         // create the one and only hypervisor object
         trace ("Creating Hypervisor...\n");
         Hypervisor hypervisor (start_info);

         // start the application
         char *argv [2];
         argv [0] = const_cast<char *>(program_name);
         argv [1] = 0;
         main (1, argv);

         trace ("ending llamaOS...\n");
      }
      catch (const std::runtime_error &e)
      {
         trace ("*** runtime_error: %s ***\n", e.what ());
      }
      catch (...)
      {
         trace ("*** unknown exception ***\n");
      }

      Hypercall::sched_op_shutdown ();

      uint64_t dtor_size = reinterpret_cast<uint64_t>(__DTOR_LIST__[0]);
      trace ("__DTOR_LIST__[0]: %lx\n", dtor_size);
      for (uint64_t i = dtor_size; i >= 1; i--)
      {
         __DTOR_LIST__[i] ();
      }

      for (;;)
      {
         Hypercall::sched_op_yield ();
      }
   }
   // else
   // something terrible is wrong and nothing can be done about it!
}
<|endoftext|>


<commit_before>/*
 * Copyright 2016 Facebook, Inc.
 *
 * 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 <folly/io/async/ScopedEventBaseThread.h>

#include <chrono>
#include <folly/Baton.h>
#include <folly/portability/GTest.h>

using namespace std;
using namespace std::chrono;
using namespace folly;

class ScopedEventBaseThreadTest : public testing::Test {};

TEST_F(ScopedEventBaseThreadTest, example) {
  ScopedEventBaseThread sebt;

  Baton<> done;
  sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
  done.timed_wait(steady_clock::now() + milliseconds(100));
}

TEST_F(ScopedEventBaseThreadTest, start_stop) {
  ScopedEventBaseThread sebt(false);

  for (size_t i = 0; i < 4; ++i) {
    EXPECT_EQ(nullptr, sebt.getEventBase());
    sebt.start();
    EXPECT_NE(nullptr, sebt.getEventBase());

    Baton<> done;
    sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
    done.timed_wait(steady_clock::now() + milliseconds(100));

    EXPECT_NE(nullptr, sebt.getEventBase());
    sebt.stop();
    EXPECT_EQ(nullptr, sebt.getEventBase());
  }
}

TEST_F(ScopedEventBaseThreadTest, move) {
  auto sebt0 = ScopedEventBaseThread();
  auto sebt1 = std::move(sebt0);
  auto sebt2 = std::move(sebt1);

  EXPECT_EQ(nullptr, sebt0.getEventBase());
  EXPECT_EQ(nullptr, sebt1.getEventBase());
  EXPECT_NE(nullptr, sebt2.getEventBase());

  Baton<> done;
  sebt2.getEventBase()->runInEventBaseThread([&] { done.post(); });
  done.timed_wait(steady_clock::now() + milliseconds(100));
}

TEST_F(ScopedEventBaseThreadTest, self_move) {
  ScopedEventBaseThread sebt0;
  auto sebt = std::move(sebt0);

  EXPECT_NE(nullptr, sebt.getEventBase());

  Baton<> done;
  sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
  done.timed_wait(steady_clock::now() + milliseconds(100));
}

TEST_F(ScopedEventBaseThreadTest, manager) {
  EventBaseManager ebm;
  ScopedEventBaseThread sebt(&ebm);
  auto sebt_eb = sebt.getEventBase();
  auto ebm_eb = (EventBase*)nullptr;
  sebt_eb->runInEventBaseThreadAndWait([&] {
      ebm_eb = ebm.getEventBase();
  });
  EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
}
<commit_msg>Check the baton waits in the ScopedEventBaseThread tests<commit_after>/*
 * Copyright 2016 Facebook, Inc.
 *
 * 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 <folly/io/async/ScopedEventBaseThread.h>

#include <chrono>
#include <folly/Baton.h>
#include <folly/portability/GTest.h>

using namespace std;
using namespace std::chrono;
using namespace folly;

class ScopedEventBaseThreadTest : public testing::Test {};

TEST_F(ScopedEventBaseThreadTest, example) {
  ScopedEventBaseThread sebt;

  Baton<> done;
  sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
  ASSERT_TRUE(done.timed_wait(seconds(1)));
}

TEST_F(ScopedEventBaseThreadTest, start_stop) {
  ScopedEventBaseThread sebt(false);

  for (size_t i = 0; i < 4; ++i) {
    EXPECT_EQ(nullptr, sebt.getEventBase());
    sebt.start();
    EXPECT_NE(nullptr, sebt.getEventBase());

    Baton<> done;
    sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
    ASSERT_TRUE(done.timed_wait(seconds(1)));

    EXPECT_NE(nullptr, sebt.getEventBase());
    sebt.stop();
    EXPECT_EQ(nullptr, sebt.getEventBase());
  }
}

TEST_F(ScopedEventBaseThreadTest, move) {
  auto sebt0 = ScopedEventBaseThread();
  auto sebt1 = std::move(sebt0);
  auto sebt2 = std::move(sebt1);

  EXPECT_EQ(nullptr, sebt0.getEventBase());
  EXPECT_EQ(nullptr, sebt1.getEventBase());
  EXPECT_NE(nullptr, sebt2.getEventBase());

  Baton<> done;
  sebt2.getEventBase()->runInEventBaseThread([&] { done.post(); });
  ASSERT_TRUE(done.timed_wait(seconds(1)));
}

TEST_F(ScopedEventBaseThreadTest, self_move) {
  ScopedEventBaseThread sebt0;
  auto sebt = std::move(sebt0);

  EXPECT_NE(nullptr, sebt.getEventBase());

  Baton<> done;
  sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
  ASSERT_TRUE(done.timed_wait(seconds(1)));
}

TEST_F(ScopedEventBaseThreadTest, manager) {
  EventBaseManager ebm;
  ScopedEventBaseThread sebt(&ebm);
  auto sebt_eb = sebt.getEventBase();
  auto ebm_eb = (EventBase*)nullptr;
  sebt_eb->runInEventBaseThreadAndWait([&] {
      ebm_eb = ebm.getEventBase();
  });
  EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
}
<|endoftext|>


<commit_before>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <blas.hpp>

#include <Array.hpp>
#include <arith.hpp>
#include <common/half.hpp>
#include <common/traits.hpp>
#include <complex.hpp>
#include <err_opencl.hpp>
#include <math.hpp>
#include <reduce.hpp>
#include <transpose.hpp>

#include <complex>
#include <vector>

// Includes one of the supported OpenCL BLAS back-ends (e.g. clBLAS, CLBlast)
#include <magma/magma_blas.h>

#if defined(WITH_LINEAR_ALGEBRA)
#include <cpu/cpu_blas.hpp>
#endif

using common::half;

namespace opencl {

void initBlas() { gpu_blas_init(); }

void deInitBlas() { gpu_blas_deinit(); }

// Converts an af_mat_prop options to a transpose type for one of the OpenCL
// BLAS back-ends
OPENCL_BLAS_TRANS_T
toBlasTranspose(af_mat_prop opt) {
    switch (opt) {
        case AF_MAT_NONE: return OPENCL_BLAS_NO_TRANS;
        case AF_MAT_TRANS: return OPENCL_BLAS_TRANS;
        case AF_MAT_CTRANS: return OPENCL_BLAS_CONJ_TRANS;
        default: AF_ERROR("INVALID af_mat_prop", AF_ERR_ARG);
    }
}

template<typename T>
void gemm_fallback(Array<T> &out, af_mat_prop optLhs, af_mat_prop optRhs,
                   const T *alpha, const Array<T> &lhs, const Array<T> &rhs,
                   const T *beta) {
    cpu::gemm(out, optLhs, optRhs, alpha, lhs, rhs, beta);
}

template<>
void gemm_fallback<half>(Array<half> &out, af_mat_prop optLhs, af_mat_prop optRhs,
                          const half *alpha,
                          const Array<half> &lhs, const Array<half> &rhs,
                          const half *beta) {
    assert(false && "CPU fallback not implemented for f16");
}

template<typename T>
void gemm(Array<T> &out, af_mat_prop optLhs, af_mat_prop optRhs,
          const T *alpha,
          const Array<T> &lhs, const Array<T> &rhs,
          const T *beta) {
#if defined(WITH_LINEAR_ALGEBRA)
    // Do not force offload gemm on OSX Intel devices
    if (OpenCLCPUOffload(false) && (af_dtype)dtype_traits<T>::af_type != f16) {
        gemm_fallback(out, optLhs, optRhs, alpha, lhs, rhs, beta);
        return;
    }
#endif
    const auto lOpts = toBlasTranspose(optLhs);
    const auto rOpts = toBlasTranspose(optRhs);

    const auto aRowDim = (lOpts == OPENCL_BLAS_NO_TRANS) ? 0 : 1;
    const auto aColDim = (lOpts == OPENCL_BLAS_NO_TRANS) ? 1 : 0;
    const auto bColDim = (rOpts == OPENCL_BLAS_NO_TRANS) ? 1 : 0;

    const dim4 lDims = lhs.dims();
    const dim4 rDims = rhs.dims();
    const int M      = lDims[aRowDim];
    const int N      = rDims[bColDim];
    const int K      = lDims[aColDim];
    const dim4 oDims = out.dims();

    const dim4 lStrides = lhs.strides();
    const dim4 rStrides = rhs.strides();
    const dim4 oStrides = out.strides();

    int batchSize = oDims[2] * oDims[3];

    bool is_l_d2_batched = oDims[2] == lDims[2];
    bool is_l_d3_batched = oDims[3] == lDims[3];
    bool is_r_d2_batched = oDims[2] == rDims[2];
    bool is_r_d3_batched = oDims[3] == rDims[3];

    for (int n = 0; n < batchSize; n++) {
        int w = n / oDims[2];
        int z = n - w * oDims[2];

        int loff = z * (is_l_d2_batched * lStrides[2]) +
                   w * (is_l_d3_batched * lStrides[3]);
        int roff = z * (is_r_d2_batched * rStrides[2]) +
                   w * (is_r_d3_batched * rStrides[3]);

        dim_t lOffset = lhs.getOffset() + loff;
        dim_t rOffset = rhs.getOffset() + roff;
        dim_t oOffset = out.getOffset() + z * oStrides[2] + w * oStrides[3];

        cl::Event event;
        if (rDims[bColDim] == 1) {
            dim_t incr = (optRhs == AF_MAT_NONE) ? rStrides[0] : rStrides[1];
            gpu_blas_gemv_func<T> gemv;
            OPENCL_BLAS_CHECK(gemv(lOpts, lDims[0], lDims[1], *alpha,
                                   (*lhs.get())(), lOffset, lStrides[1],
                                   (*rhs.get())(), rOffset, incr, *beta,
                                   (*out.get())(), oOffset, oStrides[0], 1, &getQueue()(),
                                   0, nullptr, &event()));
        } else {
            gpu_blas_gemm_func<T> gemm;
            OPENCL_BLAS_CHECK(gemm(lOpts, rOpts, M, N, K, *alpha, (*lhs.get())(),
                                   lOffset, lStrides[1], (*rhs.get())(),
                                   rOffset, rStrides[1], *beta, (*out.get())(),
                                   oOffset, oStrides[1], 1, &getQueue()(), 0,
                                   nullptr, &event()));
        }
    }
}

template<typename T>
Array<T> dot(const Array<T> &lhs, const Array<T> &rhs, af_mat_prop optLhs,
             af_mat_prop optRhs) {
    const Array<T> lhs_ = (optLhs == AF_MAT_NONE ? lhs : conj<T>(lhs));
    const Array<T> rhs_ = (optRhs == AF_MAT_NONE ? rhs : conj<T>(rhs));

    const Array<T> temp = arithOp<T, af_mul_t>(lhs_, rhs_, lhs_.dims());
    return reduce<af_add_t, T, T>(temp, 0, false, 0);
}

#define INSTANTIATE_GEMM(TYPE)                                                         \
    template void gemm<TYPE>(Array<TYPE> &out, af_mat_prop optLhs, af_mat_prop optRhs, \
                             const TYPE *alpha,                    \
                             const Array<TYPE> &lhs, const Array<TYPE> &rhs,           \
                             const TYPE *beta);

INSTANTIATE_GEMM(float)
INSTANTIATE_GEMM(cfloat)
INSTANTIATE_GEMM(double)
INSTANTIATE_GEMM(cdouble)
INSTANTIATE_GEMM(half)

#define INSTANTIATE_DOT(TYPE)                                                  \
    template Array<TYPE> dot<TYPE>(const Array<TYPE> &lhs,                     \
                                   const Array<TYPE> &rhs, af_mat_prop optLhs, \
                                   af_mat_prop optRhs);

INSTANTIATE_DOT(float)
INSTANTIATE_DOT(double)
INSTANTIATE_DOT(cfloat)
INSTANTIATE_DOT(cdouble)
INSTANTIATE_DOT(half)

}  // namespace opencl
<commit_msg>Remove guards around cpu blas functions with the OpenCL backend<commit_after>/*******************************************************
 * Copyright (c) 2014, ArrayFire
 * All rights reserved.
 *
 * This file is distributed under 3-clause BSD license.
 * The complete license agreement can be obtained at:
 * http://arrayfire.com/licenses/BSD-3-Clause
 ********************************************************/

#include <blas.hpp>

#include <Array.hpp>
#include <arith.hpp>
#include <common/half.hpp>
#include <common/traits.hpp>
#include <complex.hpp>
#include <err_opencl.hpp>
#include <math.hpp>
#include <reduce.hpp>
#include <transpose.hpp>

#include <complex>
#include <vector>

// Includes one of the supported OpenCL BLAS back-ends (e.g. clBLAS, CLBlast)
#include <magma/magma_blas.h>
#include <cpu/cpu_blas.hpp>

using common::half;

namespace opencl {

void initBlas() { gpu_blas_init(); }

void deInitBlas() { gpu_blas_deinit(); }

// Converts an af_mat_prop options to a transpose type for one of the OpenCL
// BLAS back-ends
OPENCL_BLAS_TRANS_T
toBlasTranspose(af_mat_prop opt) {
    switch (opt) {
        case AF_MAT_NONE: return OPENCL_BLAS_NO_TRANS;
        case AF_MAT_TRANS: return OPENCL_BLAS_TRANS;
        case AF_MAT_CTRANS: return OPENCL_BLAS_CONJ_TRANS;
        default: AF_ERROR("INVALID af_mat_prop", AF_ERR_ARG);
    }
}

template<typename T>
void gemm_fallback(Array<T> &out, af_mat_prop optLhs, af_mat_prop optRhs,
                   const T *alpha, const Array<T> &lhs, const Array<T> &rhs,
                   const T *beta) {
    cpu::gemm(out, optLhs, optRhs, alpha, lhs, rhs, beta);
}

template<>
void gemm_fallback<half>(Array<half> &out, af_mat_prop optLhs, af_mat_prop optRhs,
                          const half *alpha,
                          const Array<half> &lhs, const Array<half> &rhs,
                          const half *beta) {
    assert(false && "CPU fallback not implemented for f16");
}

template<typename T>
void gemm(Array<T> &out, af_mat_prop optLhs, af_mat_prop optRhs,
          const T *alpha,
          const Array<T> &lhs, const Array<T> &rhs,
          const T *beta) {
#if defined(WITH_LINEAR_ALGEBRA)
    // Do not force offload gemm on OSX Intel devices
    if (OpenCLCPUOffload(false) && (af_dtype)dtype_traits<T>::af_type != f16) {
        gemm_fallback(out, optLhs, optRhs, alpha, lhs, rhs, beta);
        return;
    }
#endif
    const auto lOpts = toBlasTranspose(optLhs);
    const auto rOpts = toBlasTranspose(optRhs);

    const auto aRowDim = (lOpts == OPENCL_BLAS_NO_TRANS) ? 0 : 1;
    const auto aColDim = (lOpts == OPENCL_BLAS_NO_TRANS) ? 1 : 0;
    const auto bColDim = (rOpts == OPENCL_BLAS_NO_TRANS) ? 1 : 0;

    const dim4 lDims = lhs.dims();
    const dim4 rDims = rhs.dims();
    const int M      = lDims[aRowDim];
    const int N      = rDims[bColDim];
    const int K      = lDims[aColDim];
    const dim4 oDims = out.dims();

    const dim4 lStrides = lhs.strides();
    const dim4 rStrides = rhs.strides();
    const dim4 oStrides = out.strides();

    int batchSize = oDims[2] * oDims[3];

    bool is_l_d2_batched = oDims[2] == lDims[2];
    bool is_l_d3_batched = oDims[3] == lDims[3];
    bool is_r_d2_batched = oDims[2] == rDims[2];
    bool is_r_d3_batched = oDims[3] == rDims[3];

    for (int n = 0; n < batchSize; n++) {
        int w = n / oDims[2];
        int z = n - w * oDims[2];

        int loff = z * (is_l_d2_batched * lStrides[2]) +
                   w * (is_l_d3_batched * lStrides[3]);
        int roff = z * (is_r_d2_batched * rStrides[2]) +
                   w * (is_r_d3_batched * rStrides[3]);

        dim_t lOffset = lhs.getOffset() + loff;
        dim_t rOffset = rhs.getOffset() + roff;
        dim_t oOffset = out.getOffset() + z * oStrides[2] + w * oStrides[3];

        cl::Event event;
        if (rDims[bColDim] == 1) {
            dim_t incr = (optRhs == AF_MAT_NONE) ? rStrides[0] : rStrides[1];
            gpu_blas_gemv_func<T> gemv;
            OPENCL_BLAS_CHECK(gemv(lOpts, lDims[0], lDims[1], *alpha,
                                   (*lhs.get())(), lOffset, lStrides[1],
                                   (*rhs.get())(), rOffset, incr, *beta,
                                   (*out.get())(), oOffset, oStrides[0], 1, &getQueue()(),
                                   0, nullptr, &event()));
        } else {
            gpu_blas_gemm_func<T> gemm;
            OPENCL_BLAS_CHECK(gemm(lOpts, rOpts, M, N, K, *alpha, (*lhs.get())(),
                                   lOffset, lStrides[1], (*rhs.get())(),
                                   rOffset, rStrides[1], *beta, (*out.get())(),
                                   oOffset, oStrides[1], 1, &getQueue()(), 0,
                                   nullptr, &event()));
        }
    }
}

template<typename T>
Array<T> dot(const Array<T> &lhs, const Array<T> &rhs, af_mat_prop optLhs,
             af_mat_prop optRhs) {
    const Array<T> lhs_ = (optLhs == AF_MAT_NONE ? lhs : conj<T>(lhs));
    const Array<T> rhs_ = (optRhs == AF_MAT_NONE ? rhs : conj<T>(rhs));

    const Array<T> temp = arithOp<T, af_mul_t>(lhs_, rhs_, lhs_.dims());
    return reduce<af_add_t, T, T>(temp, 0, false, 0);
}

#define INSTANTIATE_GEMM(TYPE)                                                         \
    template void gemm<TYPE>(Array<TYPE> &out, af_mat_prop optLhs, af_mat_prop optRhs, \
                             const TYPE *alpha,                    \
                             const Array<TYPE> &lhs, const Array<TYPE> &rhs,           \
                             const TYPE *beta);

INSTANTIATE_GEMM(float)
INSTANTIATE_GEMM(cfloat)
INSTANTIATE_GEMM(double)
INSTANTIATE_GEMM(cdouble)
INSTANTIATE_GEMM(half)

#define INSTANTIATE_DOT(TYPE)                                                  \
    template Array<TYPE> dot<TYPE>(const Array<TYPE> &lhs,                     \
                                   const Array<TYPE> &rhs, af_mat_prop optLhs, \
                                   af_mat_prop optRhs);

INSTANTIATE_DOT(float)
INSTANTIATE_DOT(double)
INSTANTIATE_DOT(cfloat)
INSTANTIATE_DOT(cdouble)
INSTANTIATE_DOT(half)

}  // namespace opencl
<|endoftext|>


<commit_before>/**
 *  Member selection
 *  ================
 *
 *  Member variables declared like a bitset.  F**k you SFINAE.
 *
 *  Note: Compile with -std=c++17.
 */

#include <iostream>

enum class cmd_t : uint32_t {
    A,
    B,
};

// NOTE: 4 options => 2^4 (=16) specialized templates.

enum class opt_t : uint32_t {
    None,
    A = 1,
    B = 1 << 1,
};

constexpr uint32_t opt_v(opt_t opt) {
    return std::underlying_type_t<opt_t>(opt);
}

constexpr opt_t operator|(opt_t lhs, opt_t rhs) {
    return static_cast<opt_t>(opt_v(lhs) | opt_v(rhs));
}

constexpr opt_t operator&(opt_t lhs, opt_t rhs) {
    return static_cast<opt_t>(opt_v(rhs) & opt_v(lhs));
}

template <cmd_t Cmd>
struct cmd_trait {
    static constexpr auto opts = opt_t::None;
};

template <cmd_t Cmd> constexpr bool is_opt(opt_t opt) {
    return opt == cmd_trait<Cmd>::opts;
}

template <>
struct cmd_trait<cmd_t::A> {
    static constexpr auto opts = opt_t::A;
};

template <>
struct cmd_trait<cmd_t::B> {
    static constexpr auto opts = opt_t::A | opt_t::B;
};

template <cmd_t Cmd, typename = void>
struct val_t {
    int A;
    int B;
};

template <cmd_t Cmd>
struct val_t<Cmd, std::enable_if_t<is_opt<Cmd>(opt_t::A)>> {
    int A;
};

template <cmd_t Cmd>
struct val_t<Cmd, std::enable_if_t<is_opt<Cmd>(opt_t::B)>> {
    int B;
};

template <cmd_t Cmd>
struct val_t<Cmd, std::enable_if_t<is_opt<Cmd>(opt_t::A | opt_t::B)>> {
    int A;
    int B;
};

int main()
{
    val_t<cmd_t::A> val;
    val.A = 42;
    // val.B = 69; // member not found
    std::cout << val.A << std::endl;
    return 0;
}
<commit_msg>Updated member_select: default templ<commit_after>/**
 *  Member selection
 *  ================
 *
 *  Member variables declared like a bitset.  F**k you SFINAE.
 *
 *  Note: Compile with -std=c++17.
 */

#include <iostream>

enum class cmd_t : uint32_t {
    A,
    B,
};

// NOTE: 4 options => 2^4 (=16) specialized templates.

enum class opt_t : uint32_t {
    None,
    A = 1,
    B = 1 << 1,
};

constexpr uint32_t opt_v(opt_t opt) {
    return std::underlying_type_t<opt_t>(opt);
}

constexpr opt_t operator|(opt_t lhs, opt_t rhs) {
    return static_cast<opt_t>(opt_v(lhs) | opt_v(rhs));
}

constexpr opt_t operator&(opt_t lhs, opt_t rhs) {
    return static_cast<opt_t>(opt_v(rhs) & opt_v(lhs));
}

template <cmd_t Cmd>
struct cmd_trait {
    static constexpr auto opts = opt_t::None;
};

template <cmd_t Cmd> constexpr bool is_opt(opt_t opt) {
    return opt == cmd_trait<Cmd>::opts;
}

template <>
struct cmd_trait<cmd_t::A> {
    static constexpr auto opts = opt_t::A;
};

template <>
struct cmd_trait<cmd_t::B> {
    static constexpr auto opts = opt_t::A | opt_t::B;
};

template <cmd_t Cmd, typename = void>
struct val_t {};

template <cmd_t Cmd>
struct val_t<Cmd, std::enable_if_t<is_opt<Cmd>(opt_t::A)>> {
    int A;
};

template <cmd_t Cmd>
struct val_t<Cmd, std::enable_if_t<is_opt<Cmd>(opt_t::B)>> {
    int B;
};

template <cmd_t Cmd>
struct val_t<Cmd, std::enable_if_t<is_opt<Cmd>(opt_t::A | opt_t::B)>> {
    int A;
    int B;
};

int main()
{
    val_t<cmd_t::A> val;
    val.A = 42;
    // val.B = 69; // member not found
    std::cout << val.A << std::endl;
    return 0;
}
<|endoftext|>


<commit_before>/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  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.
 */

/*
 * Copyright (C) 2021 ScyllaDB
 */

#include <seastar/core/thread.hh>
#include <seastar/testing/test_case.hh>
#include <seastar/testing/thread_test_case.hh>
#include <seastar/testing/test_runner.hh>
#include <seastar/core/reactor.hh>
#include <seastar/core/smp.hh>
#include <seastar/core/when_all.hh>
#include <seastar/core/file.hh>
#include <seastar/core/io_queue.hh>
#include <seastar/core/io_intent.hh>
#include <seastar/core/internal/io_request.hh>
#include <seastar/core/internal/io_sink.hh>

using namespace seastar;

template <size_t Len>
struct fake_file {
    int data[Len] = {};

    static internal::io_request make_write_req(size_t idx, int val) {
        int* buf = new int(val);
        return internal::io_request::make_write(0, idx, buf, 1, false);
    }

    void execute_write_req(internal::io_request& rq, io_completion* desc) {
        data[rq.pos()] = *(reinterpret_cast<int*>(rq.address()));
        desc->complete_with(rq.size());
    }
};

struct io_queue_for_tests {
    io_group_ptr group;
    internal::io_sink sink;
    io_queue queue;

    io_queue_for_tests()
        : group(std::make_shared<io_group>(io_group::config{}))
        , sink()
        , queue(group, sink, io_queue::config{0})
    {}
};

SEASTAR_THREAD_TEST_CASE(test_basic_flow) {
    io_queue_for_tests tio;
    fake_file<1> file;

    auto f = tio.queue.queue_request(default_priority_class(), 0, file.make_write_req(0, 42), nullptr)
    .then([&file] (size_t len) {
        BOOST_REQUIRE(file.data[0] == 42);
    });

    tio.queue.poll_io_queue();
    tio.sink.drain([&file] (internal::io_request& rq, io_completion* desc) -> bool {
        file.execute_write_req(rq, desc);
        return true;
    });

    f.get();
}

SEASTAR_THREAD_TEST_CASE(test_intent_safe_ref) {
    auto get_cancelled = [] (internal::intent_reference& iref) -> bool {
        try {
            iref.retrieve();
            return false;
        } catch(seastar::cancelled_error& err) {
            return true;
        }
    };

    io_intent intent, intent_x;

    internal::intent_reference ref_orig(&intent);
    BOOST_REQUIRE(ref_orig.retrieve() == &intent);

    // Test move armed
    internal::intent_reference ref_armed(std::move(ref_orig));
    BOOST_REQUIRE(ref_orig.retrieve() == nullptr);
    BOOST_REQUIRE(ref_armed.retrieve() == &intent);

    internal::intent_reference ref_armed_2(&intent_x);
    ref_armed_2 = std::move(ref_armed);
    BOOST_REQUIRE(ref_armed.retrieve() == nullptr);
    BOOST_REQUIRE(ref_armed_2.retrieve() == &intent);

    intent.cancel();
    BOOST_REQUIRE(get_cancelled(ref_armed_2));

    // Test move cancelled
    internal::intent_reference ref_cancelled(std::move(ref_armed_2));
    BOOST_REQUIRE(ref_armed_2.retrieve() == nullptr);
    BOOST_REQUIRE(get_cancelled(ref_cancelled));

    internal::intent_reference ref_cancelled_2(&intent_x);
    ref_cancelled_2 = std::move(ref_cancelled);
    BOOST_REQUIRE(ref_cancelled.retrieve() == nullptr);
    BOOST_REQUIRE(get_cancelled(ref_cancelled_2));

    // Test move empty
    internal::intent_reference ref_empty(std::move(ref_orig));
    BOOST_REQUIRE(ref_empty.retrieve() == nullptr);

    internal::intent_reference ref_empty_2(&intent_x);
    ref_empty_2 = std::move(ref_empty);
    BOOST_REQUIRE(ref_empty_2.retrieve() == nullptr);
}

static constexpr int nr_requests = 24;

SEASTAR_THREAD_TEST_CASE(test_io_cancellation) {
    fake_file<nr_requests> file;

    io_queue_for_tests tio;
    io_priority_class pc0 = tio.queue.register_one_priority_class("a", 100);
    io_priority_class pc1 = tio.queue.register_one_priority_class("b", 100);

    size_t idx = 0;
    int val = 100;

    io_intent live, dead;

    std::vector<future<>> finished;
    std::vector<future<>> cancelled;

    auto queue_legacy_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
        auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, val), nullptr)
            .then([&file, idx, val] (size_t len) {
                BOOST_REQUIRE(file.data[idx] == val);
                return make_ready_future<>();
            });
        finished.push_back(std::move(f));
        idx++;
        val++;
    };

    auto queue_live_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
        auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, val), &live)
            .then([&file, idx, val] (size_t len) {
                BOOST_REQUIRE(file.data[idx] == val);
                return make_ready_future<>();
            });
        finished.push_back(std::move(f));
        idx++;
        val++;
    };

    auto queue_dead_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
        auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, val), &dead)
            .then_wrapped([] (auto&& f) {
                try {
                    f.get();
                    BOOST_REQUIRE(false);
                } catch(...) {}
                return make_ready_future<>();
            })
            .then([&file, idx] () {
                BOOST_REQUIRE(file.data[idx] == 0);
            });
        cancelled.push_back(std::move(f));
        idx++;
        val++;
    };

    auto seed = std::random_device{}();
    std::default_random_engine reng(seed);
    std::uniform_int_distribution<> dice(0, 5);

    for (int i = 0; i < nr_requests; i++) {
        int pc = dice(reng) % 2;
        if (dice(reng) < 3) {
            fmt::print("queue live req to pc {}\n", pc);
            queue_live_request(tio, pc == 0 ? pc0 : pc1);
        } else if (dice(reng) < 5) {
            fmt::print("queue dead req to pc {}\n", pc);
            queue_dead_request(tio, pc == 0 ? pc0 : pc1);
        } else {
            fmt::print("queue legacy req to pc {}\n", pc);
            queue_legacy_request(tio, pc == 0 ? pc0 : pc1);
        }
    }

    dead.cancel();

    // cancelled requests must resolve right at once

    when_all_succeed(cancelled.begin(), cancelled.end()).get();

    tio.queue.poll_io_queue();
    tio.sink.drain([&file] (internal::io_request& rq, io_completion* desc) -> bool {
        file.execute_write_req(rq, desc);
        return true;
    });

    when_all_succeed(finished.begin(), finished.end()).get();
}
<commit_msg>test: Fix leak in io_queue_test<commit_after>/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  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.
 */

/*
 * Copyright (C) 2021 ScyllaDB
 */

#include <seastar/core/thread.hh>
#include <seastar/testing/test_case.hh>
#include <seastar/testing/thread_test_case.hh>
#include <seastar/testing/test_runner.hh>
#include <seastar/core/reactor.hh>
#include <seastar/core/smp.hh>
#include <seastar/core/when_all.hh>
#include <seastar/core/file.hh>
#include <seastar/core/io_queue.hh>
#include <seastar/core/io_intent.hh>
#include <seastar/core/internal/io_request.hh>
#include <seastar/core/internal/io_sink.hh>

using namespace seastar;

template <size_t Len>
struct fake_file {
    int data[Len] = {};

    static internal::io_request make_write_req(size_t idx, int* buf) {
        return internal::io_request::make_write(0, idx, buf, 1, false);
    }

    void execute_write_req(internal::io_request& rq, io_completion* desc) {
        data[rq.pos()] = *(reinterpret_cast<int*>(rq.address()));
        desc->complete_with(rq.size());
    }
};

struct io_queue_for_tests {
    io_group_ptr group;
    internal::io_sink sink;
    io_queue queue;

    io_queue_for_tests()
        : group(std::make_shared<io_group>(io_group::config{}))
        , sink()
        , queue(group, sink, io_queue::config{0})
    {}
};

SEASTAR_THREAD_TEST_CASE(test_basic_flow) {
    io_queue_for_tests tio;
    fake_file<1> file;

    auto val = std::make_unique<int>(42);
    auto f = tio.queue.queue_request(default_priority_class(), 0, file.make_write_req(0, val.get()), nullptr)
    .then([&file] (size_t len) {
        BOOST_REQUIRE(file.data[0] == 42);
    });

    tio.queue.poll_io_queue();
    tio.sink.drain([&file] (internal::io_request& rq, io_completion* desc) -> bool {
        file.execute_write_req(rq, desc);
        return true;
    });

    f.get();
}

SEASTAR_THREAD_TEST_CASE(test_intent_safe_ref) {
    auto get_cancelled = [] (internal::intent_reference& iref) -> bool {
        try {
            iref.retrieve();
            return false;
        } catch(seastar::cancelled_error& err) {
            return true;
        }
    };

    io_intent intent, intent_x;

    internal::intent_reference ref_orig(&intent);
    BOOST_REQUIRE(ref_orig.retrieve() == &intent);

    // Test move armed
    internal::intent_reference ref_armed(std::move(ref_orig));
    BOOST_REQUIRE(ref_orig.retrieve() == nullptr);
    BOOST_REQUIRE(ref_armed.retrieve() == &intent);

    internal::intent_reference ref_armed_2(&intent_x);
    ref_armed_2 = std::move(ref_armed);
    BOOST_REQUIRE(ref_armed.retrieve() == nullptr);
    BOOST_REQUIRE(ref_armed_2.retrieve() == &intent);

    intent.cancel();
    BOOST_REQUIRE(get_cancelled(ref_armed_2));

    // Test move cancelled
    internal::intent_reference ref_cancelled(std::move(ref_armed_2));
    BOOST_REQUIRE(ref_armed_2.retrieve() == nullptr);
    BOOST_REQUIRE(get_cancelled(ref_cancelled));

    internal::intent_reference ref_cancelled_2(&intent_x);
    ref_cancelled_2 = std::move(ref_cancelled);
    BOOST_REQUIRE(ref_cancelled.retrieve() == nullptr);
    BOOST_REQUIRE(get_cancelled(ref_cancelled_2));

    // Test move empty
    internal::intent_reference ref_empty(std::move(ref_orig));
    BOOST_REQUIRE(ref_empty.retrieve() == nullptr);

    internal::intent_reference ref_empty_2(&intent_x);
    ref_empty_2 = std::move(ref_empty);
    BOOST_REQUIRE(ref_empty_2.retrieve() == nullptr);
}

static constexpr int nr_requests = 24;

SEASTAR_THREAD_TEST_CASE(test_io_cancellation) {
    fake_file<nr_requests> file;

    io_queue_for_tests tio;
    io_priority_class pc0 = tio.queue.register_one_priority_class("a", 100);
    io_priority_class pc1 = tio.queue.register_one_priority_class("b", 100);

    size_t idx = 0;
    int val = 100;

    io_intent live, dead;

    std::vector<future<>> finished;
    std::vector<future<>> cancelled;

    auto queue_legacy_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
        auto buf = std::make_unique<int>(val);
        auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, buf.get()), nullptr)
            .then([&file, idx, val, buf = std::move(buf)] (size_t len) {
                BOOST_REQUIRE(file.data[idx] == val);
                return make_ready_future<>();
            });
        finished.push_back(std::move(f));
        idx++;
        val++;
    };

    auto queue_live_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
        auto buf = std::make_unique<int>(val);
        auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, buf.get()), &live)
            .then([&file, idx, val, buf = std::move(buf)] (size_t len) {
                BOOST_REQUIRE(file.data[idx] == val);
                return make_ready_future<>();
            });
        finished.push_back(std::move(f));
        idx++;
        val++;
    };

    auto queue_dead_request = [&] (io_queue_for_tests& q, io_priority_class& pc) {
        auto buf = std::make_unique<int>(val);
        auto f = q.queue.queue_request(pc, 0, file.make_write_req(idx, buf.get()), &dead)
            .then_wrapped([buf = std::move(buf)] (auto&& f) {
                try {
                    f.get();
                    BOOST_REQUIRE(false);
                } catch(...) {}
                return make_ready_future<>();
            })
            .then([&file, idx] () {
                BOOST_REQUIRE(file.data[idx] == 0);
            });
        cancelled.push_back(std::move(f));
        idx++;
        val++;
    };

    auto seed = std::random_device{}();
    std::default_random_engine reng(seed);
    std::uniform_int_distribution<> dice(0, 5);

    for (int i = 0; i < nr_requests; i++) {
        int pc = dice(reng) % 2;
        if (dice(reng) < 3) {
            fmt::print("queue live req to pc {}\n", pc);
            queue_live_request(tio, pc == 0 ? pc0 : pc1);
        } else if (dice(reng) < 5) {
            fmt::print("queue dead req to pc {}\n", pc);
            queue_dead_request(tio, pc == 0 ? pc0 : pc1);
        } else {
            fmt::print("queue legacy req to pc {}\n", pc);
            queue_legacy_request(tio, pc == 0 ? pc0 : pc1);
        }
    }

    dead.cancel();

    // cancelled requests must resolve right at once

    when_all_succeed(cancelled.begin(), cancelled.end()).get();

    tio.queue.poll_io_queue();
    tio.sink.drain([&file] (internal::io_request& rq, io_completion* desc) -> bool {
        file.execute_write_req(rq, desc);
        return true;
    });

    when_all_succeed(finished.begin(), finished.end()).get();
}
<|endoftext|>


<commit_before>#include <algorithm>
#include <map>
#include <math.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

#include <fj_tool/fapp.h>
#include <fjcoll.h>

using namespace std;

#include "pearson-3d.h"

int T_MAX;
int T_MONITOR;
int mpi_my_rank;
const double PI=3.14159265358979323846;

float frand() {
  return rand() / float(RAND_MAX);
}

double wctime() {
  struct timeval tv;
  gettimeofday(&tv,NULL);
  return (double)tv.tv_sec + (double)tv.tv_usec*1e-6;
}
/*
void init() {
  for(int ix = navi.lower_x; ix < navi.upper_x; ++ix) {
    for(int iy = navi.lower_y; iy < navi.upper_y; ++iy) {
      for(int iz = navi.lower_z; iz < navi.upper_z; ++iz) {
        double x = double(navi.offset_x + ix)/NX;
        double y = double(navi.offset_y + iy)/NY;
        double z = double(navi.offset_z + iz)/NZ;
        U[ix][iy][iz] = 1.0;
        V[ix][iy][iz] = 0.0;
        if (z > 0.49 && z < 0.51 && x > 0.4 && x < 0.6) {
          U[ix][iy][iz] = 0.5;
          V[ix][iy][iz] = 0.25;
        }
      }
    }
  }
}*/

typedef pair<int,pair<int,int> > Key;
void init() {
  map<Key ,double> seeds;
  for(int ix = navi.lower_x; ix < navi.upper_x; ++ix) {
    for(int iy = navi.lower_y; iy < navi.upper_y; ++iy) {
      for(int iz = navi.lower_z; iz < navi.upper_z; ++iz) {
        Key k (ix/16, pair<int,int>(iy/16, iz/16));
        U[ix][iy][iz] = 1.0;
        V[ix][iy][iz] = 0.0;
        double s = seeds[k];
        if (s==0) {
          s = frand();
          seeds[k]=s;
        }
        if (s < 0.1 ) {
          U[ix][iy][iz] = 0.5;
          V[ix][iy][iz] = 0.25;
        }
      }
    }
  }
  }

void write_monitor() {
  printf("#%d: t = %d\n", mpi_my_rank, navi.time_step);
  char fn[256];
  sprintf(fn, "out/monitor-%06d-%d.txt", navi.time_step, mpi_my_rank);
  FILE *fp = fopen(fn,"wb");
  int global_position[6];
  global_position[0] = navi.offset_x + navi.lower_x;
  global_position[1] = navi.offset_y + navi.lower_y;
  global_position[2] = navi.offset_z + navi.lower_z;
  global_position[3] = navi.upper_x - navi.lower_x;
  global_position[4] = navi.upper_y - navi.lower_y;
  global_position[5] = navi.upper_z - navi.lower_z;
  fwrite(global_position, sizeof(int), 6, fp);

  int x_size = navi.upper_x - navi.lower_x;
  int y_size = navi.upper_y - navi.lower_y;
  int z_size = navi.upper_z - navi.lower_z;
  {
    const int y=navi.lower_y + y_size/2;
    for(int x = navi.lower_x; x < navi.upper_x; ++x) fwrite(U[x][y]+navi.lower_z, sizeof(double),z_size, fp);
    for(int x = navi.lower_x; x < navi.upper_x; ++x) fwrite(V[x][y]+navi.lower_z, sizeof(double),z_size, fp);
  }
  {
    const int x=navi.lower_x + x_size/2;
    for(int y = navi.lower_y; y < navi.upper_y; ++y) fwrite(U[x][y]+navi.lower_z, sizeof(double),z_size, fp);
    for(int y = navi.lower_y; y < navi.upper_y; ++y) fwrite(V[x][y]+navi.lower_z, sizeof(double),z_size, fp);
  }
  fclose(fp);
}

int main (int argc, char **argv) {
  MPI_Init(&argc, &argv);
  Formura_Init(&navi, MPI_COMM_WORLD);
  MPI_Comm_rank(MPI_COMM_WORLD, &mpi_my_rank);
  srand(time(NULL)+mpi_my_rank*65537);

  if (argc <= 1) {
    T_MAX=8192;
  }else{
    sscanf(argv[1], "%d",  &T_MAX);
  }
  if (argc <= 2) {
    T_MONITOR=8192;
  }else{
    sscanf(argv[2], "%d",  &T_MONITOR);
  }

  init();

  double t_begin = wctime(), t_end;

  int last_monitor_t = -T_MONITOR;
  for(;;){
    double t = wctime();
    bool monitor_flag = navi.time_step >= last_monitor_t + T_MONITOR;
    if(monitor_flag || navi.time_step <= 3 * T_MONITOR ) {
      printf("%d step @ %lf sec\n", navi.time_step, t-t_begin);
    }
    if(monitor_flag) {
      write_monitor();
    }
    if(monitor_flag) {
      last_monitor_t += T_MONITOR;
    }

    if (navi.time_step >= T_MAX) break;
    if (navi.time_step == 0) {
      t_begin = wctime();
      start_collection("main");
    }

    Formura_Forward(&navi); // navi.time_step increases
    MPI_Barrier(MPI_COMM_WORLD); // TODO: test the effect of synchronization

    if (navi.time_step >= T_MAX) {
      t_end = wctime();
      stop_collection("main");
    }
  }
  printf("wct = %lf sec\n",t_end - t_begin);

  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Finalize();
}
<commit_msg>Limit the number of output files<commit_after>#include <algorithm>
#include <map>
#include <math.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

#include <fj_tool/fapp.h>
#include <fjcoll.h>

using namespace std;

#include "pearson-3d.h"

bool EXTEND_MISSION=false;
int T_MAX;
int T_MONITOR;
int mpi_my_rank;
const double PI=3.14159265358979323846;

float frand() {
  return rand() / float(RAND_MAX);
}

double wctime() {
  struct timeval tv;
  gettimeofday(&tv,NULL);
  return (double)tv.tv_sec + (double)tv.tv_usec*1e-6;
}
/*
void init() {
  for(int ix = navi.lower_x; ix < navi.upper_x; ++ix) {
    for(int iy = navi.lower_y; iy < navi.upper_y; ++iy) {
      for(int iz = navi.lower_z; iz < navi.upper_z; ++iz) {
        double x = double(navi.offset_x + ix)/NX;
        double y = double(navi.offset_y + iy)/NY;
        double z = double(navi.offset_z + iz)/NZ;
        U[ix][iy][iz] = 1.0;
        V[ix][iy][iz] = 0.0;
        if (z > 0.49 && z < 0.51 && x > 0.4 && x < 0.6) {
          U[ix][iy][iz] = 0.5;
          V[ix][iy][iz] = 0.25;
        }
      }
    }
  }
}*/

typedef pair<int,pair<int,int> > Key;
void init() {
  map<Key ,double> seeds;
  for(int ix = navi.lower_x; ix < navi.upper_x; ++ix) {
    for(int iy = navi.lower_y; iy < navi.upper_y; ++iy) {
      for(int iz = navi.lower_z; iz < navi.upper_z; ++iz) {
        Key k (ix/16, pair<int,int>(iy/16, iz/16));
        U[ix][iy][iz] = 1.0;
        V[ix][iy][iz] = 0.0;
        double s = seeds[k];
        if (s==0) {
          s = frand();
          seeds[k]=s;
        }
        if (s < 0.1 ) {
          U[ix][iy][iz] = 0.5;
          V[ix][iy][iz] = 0.25;
        }
      }
    }
  }
  }

void write_monitor() {
  printf("#%d: t = %d\n", mpi_my_rank, navi.time_step);

  int global_position[6];
  global_position[0] = navi.offset_x + navi.lower_x;
  global_position[1] = navi.offset_y + navi.lower_y;
  global_position[2] = navi.offset_z + navi.lower_z;
  global_position[3] = navi.upper_x - navi.lower_x;
  global_position[4] = navi.upper_y - navi.lower_y;
  global_position[5] = navi.upper_z - navi.lower_z;
  int x_size = navi.upper_x - navi.lower_x;
  int y_size = navi.upper_y - navi.lower_y;
  int z_size = navi.upper_z - navi.lower_z;

  if (navi.offset_x + navi.lower_x < navi.upper_x) {
    char fn[256];
    sprintf(fn, "out/monitorX-%06d-%d.txt", navi.time_step, mpi_my_rank);

    FILE *fp = fopen(fn,"wb");
    fwrite(global_position, sizeof(int), 6, fp);
    {
      const int x=navi.lower_x + x_size/2;
      for(int y = navi.lower_y; y < navi.upper_y; ++y) fwrite(U[x][y]+navi.lower_z, sizeof(double),z_size, fp);
      for(int y = navi.lower_y; y < navi.upper_y; ++y) fwrite(V[x][y]+navi.lower_z, sizeof(double),z_size, fp);
    }
    fclose(fp);
  }

  if (navi.offset_y + navi.lower_y < navi.upper_y) {
    char fn[256];
    sprintf(fn, "out/monitorY-%06d-%d.txt", navi.time_step, mpi_my_rank);

    FILE *fp = fopen(fn,"wb");
    fwrite(global_position, sizeof(int), 6, fp);
    {
      const int y=navi.lower_y + y_size/2;
      for(int x = navi.lower_x; x < navi.upper_x; ++x) fwrite(U[x][y]+navi.lower_z, sizeof(double),z_size, fp);
      for(int x = navi.lower_x; x < navi.upper_x; ++x) fwrite(V[x][y]+navi.lower_z, sizeof(double),z_size, fp);
    }
    fclose(fp);
  }

}

int main (int argc, char **argv) {
  MPI_Init(&argc, &argv);
  Formura_Init(&navi, MPI_COMM_WORLD);
  MPI_Comm_rank(MPI_COMM_WORLD, &mpi_my_rank);
  srand(time(NULL)+mpi_my_rank*65537);

  if (argc <= 1) {
    T_MAX=8192;
  }else{
    sscanf(argv[1], "%d",  &T_MAX);
  }
  if (argc <= 2) {
    T_MONITOR=8192;
  }else{
    sscanf(argv[2], "%d",  &T_MONITOR);
  }
  if (argc >= 4) {
    EXTEND_MISSION = true;
  }

  init();

  double t_begin = wctime(), t_end;

  int last_monitor_t = -T_MONITOR;
  for(;;){
    double t = wctime();
    bool monitor_flag = navi.time_step >= last_monitor_t + T_MONITOR;
    if(monitor_flag || navi.time_step <= 3 * T_MONITOR ) {
      printf("%d step @ %lf sec\n", navi.time_step, t-t_begin);
    }
    if(monitor_flag) {
      write_monitor();
    }
    if(monitor_flag) {
      last_monitor_t += T_MONITOR;
    }

    if (navi.time_step >= T_MAX) {
      if (EXTEND_MISSION){
        T_MAX*=2;
        T_MONITOR*=2;
      }else{
        break;
      }

    }
    if (navi.time_step == 0) {
      t_begin = wctime();
      start_collection("main");
    }

    Formura_Forward(&navi); // navi.time_step increases
    MPI_Barrier(MPI_COMM_WORLD); // TODO: test the effect of synchronization

    if (navi.time_step >= T_MAX) {
      t_end = wctime();
      stop_collection("main");
    }
  }
  printf("wct = %lf sec\n",t_end - t_begin);

  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Finalize();
}
<|endoftext|>


<commit_before>#include "neural_network_trainer.hpp"
#include "InputData/sentence.hpp"
#include "Learning/learning_method.hpp"
#include "InputData/image.hpp"
#include "Layer/fully_connected_neural_network.hpp"
#include "Layer/filters.hpp"

using namespace Convolutional;

enum class Categories {
	Cat,
	NotCat,

	CategoryCount
};

int main(int argc, char* argv[]){
	TrainingData<Categories> trainingData;
	InputData::Image someCat("../../image.png");
	trainingData.AddData(std::move(someCat), Categories::Cat);

	Layer::Layers layers {
		Layer::Filters{3},
		Layer::ReLU{},
		Layer::Pooler::MaxPooler{},
		Layer::FullyConnectedNeuralNetwork{static_cast<std::size_t>(Categories::CategoryCount)}
	};

	NeuralNetworktrainer<Categories> networktrainer {
		50,
		std::move(trainingData),
		std::move(layers)
	};

	auto trainedNetwork = networktrainer.Train();
	return 0;
}
<commit_msg>Remove superflous arguments from main()<commit_after>#include "neural_network_trainer.hpp"
#include "InputData/sentence.hpp"
#include "Learning/learning_method.hpp"
#include "InputData/image.hpp"
#include "Layer/fully_connected_neural_network.hpp"
#include "Layer/filters.hpp"

using namespace Convolutional;

enum class Categories {
	Cat,
	NotCat,

	CategoryCount
};

int main() {
	TrainingData<Categories> trainingData;
	InputData::Image someCat("../../image.png");
	trainingData.AddData(std::move(someCat), Categories::Cat);

	Layer::Layers layers {
		Layer::Filters{3},
		Layer::ReLU{},
		Layer::Pooler::MaxPooler{},
		Layer::FullyConnectedNeuralNetwork{static_cast<std::size_t>(Categories::CategoryCount)}
	};

	NeuralNetworktrainer<Categories> networktrainer {
		50,
		std::move(trainingData),
		std::move(layers)
	};

	auto trainedNetwork = networktrainer.Train();
	return 0;
}
<|endoftext|>