<commit_before>/*
 * Copyright 2006 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkEmbossMaskFilter.h"
#include "SkBlurMaskFilter.h"
#include "SkBlurMask.h"
#include "SkColorPriv.h"
#include "SkEmbossMask.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkString.h"

static void normalize3(SkScalar dst[3], const SkScalar src[3]) {
    SkScalar mag = SkScalarSquare(src[0]) + SkScalarSquare(src[1]) + SkScalarSquare(src[2]);
    SkScalar scale = SkScalarInvert(SkScalarSqrt(mag));

    for (int i = 0; i < 3; i++) {
        dst[i] = src[i] * scale;
    }
}

sk_sp<SkMaskFilter> SkEmbossMaskFilter::Make(SkScalar blurSigma, const Light& light) {
    if (!SkScalarIsFinite(blurSigma) || blurSigma <= 0) {
        return nullptr;
    }

    Light newLight = light;
    normalize3(newLight.fDirection, light.fDirection);
    if (SkScalarsAreFinite(newLight.fDirection, 3)) {
        return nullptr;
    }

    return sk_sp<SkMaskFilter>(new SkEmbossMaskFilter(blurSigma, newLight));
}

#ifdef SK_SUPPORT_LEGACY_EMBOSSMASKFILTER
sk_sp<SkMaskFilter> SkBlurMaskFilter::MakeEmboss(SkScalar blurSigma, const SkScalar direction[3],
                                                 SkScalar ambient, SkScalar specular) {
    if (direction == nullptr) {
        return nullptr;
    }

    SkEmbossMaskFilter::Light   light;

    memcpy(light.fDirection, direction, sizeof(light.fDirection));
    // ambient should be 0...1 as a scalar
    light.fAmbient = SkUnitScalarClampToByte(ambient);
    // specular should be 0..15.99 as a scalar
    static const SkScalar kSpecularMultiplier = SkIntToScalar(255) / 16;
    light.fSpecular = static_cast<U8CPU>(SkScalarPin(specular, 0, 16) * kSpecularMultiplier + 0.5);

    return SkEmbossMaskFilter::Make(blurSigma, light);
}
#endif

///////////////////////////////////////////////////////////////////////////////

SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
    : fLight(light), fBlurSigma(blurSigma)
{
    SkASSERT(fBlurSigma > 0);
    SkASSERT(SkScalarsAreFinite(fLight.fDirection, 3));
}

SkMask::Format SkEmbossMaskFilter::getFormat() const {
    return SkMask::k3D_Format;
}

bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
                                    const SkMatrix& matrix, SkIPoint* margin) const {
    SkScalar sigma = matrix.mapRadius(fBlurSigma);

    if (!SkBlurMask::BoxBlur(dst, src, sigma, kInner_SkBlurStyle, kLow_SkBlurQuality)) {
        return false;
    }

    dst->fFormat = SkMask::k3D_Format;
    if (margin) {
        margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
    }

    if (src.fImage == nullptr) {
        return true;
    }

    // create a larger buffer for the other two channels (should force fBlur to do this for us)

    {
        uint8_t* alphaPlane = dst->fImage;
        size_t   planeSize = dst->computeImageSize();
        if (0 == planeSize) {
            return false;   // too big to allocate, abort
        }
        dst->fImage = SkMask::AllocImage(planeSize * 3);
        memcpy(dst->fImage, alphaPlane, planeSize);
        SkMask::FreeImage(alphaPlane);
    }

    // run the light direction through the matrix...
    Light   light = fLight;
    matrix.mapVectors((SkVector*)(void*)light.fDirection,
                      (SkVector*)(void*)fLight.fDirection, 1);

    // now restore the length of the XY component
    // cast to SkVector so we can call setLength (this double cast silences alias warnings)
    SkVector* vec = (SkVector*)(void*)light.fDirection;
    vec->setLength(light.fDirection[0],
                   light.fDirection[1],
                   SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));

    SkEmbossMask::Emboss(dst, light);

    // restore original alpha
    memcpy(dst->fImage, src.fImage, src.computeImageSize());

    return true;
}

sk_sp<SkFlattenable> SkEmbossMaskFilter::CreateProc(SkReadBuffer& buffer) {
    Light light;
    if (buffer.readByteArray(&light, sizeof(Light))) {
        light.fPad = 0; // for the font-cache lookup to be clean
        const SkScalar sigma = buffer.readScalar();
        return Make(sigma, light);
    }
    return nullptr;
}

void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
    Light tmpLight = fLight;
    tmpLight.fPad = 0;    // for the font-cache lookup to be clean
    buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
    buffer.writeScalar(fBlurSigma);
}

#ifndef SK_IGNORE_TO_STRING
void SkEmbossMaskFilter::toString(SkString* str) const {
    str->append("SkEmbossMaskFilter: (");

    str->append("direction: (");
    str->appendScalar(fLight.fDirection[0]);
    str->append(", ");
    str->appendScalar(fLight.fDirection[1]);
    str->append(", ");
    str->appendScalar(fLight.fDirection[2]);
    str->append(") ");

    str->appendf("ambient: %d specular: %d ",
        fLight.fAmbient, fLight.fSpecular);

    str->append("blurSigma: ");
    str->appendScalar(fBlurSigma);
    str->append(")");
}
#endif
<commit_msg>doh -- reverse check for finite<commit_after>/*
 * Copyright 2006 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkEmbossMaskFilter.h"
#include "SkBlurMaskFilter.h"
#include "SkBlurMask.h"
#include "SkColorPriv.h"
#include "SkEmbossMask.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkString.h"

static void normalize3(SkScalar dst[3], const SkScalar src[3]) {
    SkScalar mag = SkScalarSquare(src[0]) + SkScalarSquare(src[1]) + SkScalarSquare(src[2]);
    SkScalar scale = SkScalarInvert(SkScalarSqrt(mag));

    for (int i = 0; i < 3; i++) {
        dst[i] = src[i] * scale;
    }
}

sk_sp<SkMaskFilter> SkEmbossMaskFilter::Make(SkScalar blurSigma, const Light& light) {
    if (!SkScalarIsFinite(blurSigma) || blurSigma <= 0) {
        return nullptr;
    }

    Light newLight = light;
    normalize3(newLight.fDirection, light.fDirection);
    if (!SkScalarsAreFinite(newLight.fDirection, 3)) {
        return nullptr;
    }

    return sk_sp<SkMaskFilter>(new SkEmbossMaskFilter(blurSigma, newLight));
}

#ifdef SK_SUPPORT_LEGACY_EMBOSSMASKFILTER
sk_sp<SkMaskFilter> SkBlurMaskFilter::MakeEmboss(SkScalar blurSigma, const SkScalar direction[3],
                                                 SkScalar ambient, SkScalar specular) {
    if (direction == nullptr) {
        return nullptr;
    }

    SkEmbossMaskFilter::Light   light;

    memcpy(light.fDirection, direction, sizeof(light.fDirection));
    // ambient should be 0...1 as a scalar
    light.fAmbient = SkUnitScalarClampToByte(ambient);
    // specular should be 0..15.99 as a scalar
    static const SkScalar kSpecularMultiplier = SkIntToScalar(255) / 16;
    light.fSpecular = static_cast<U8CPU>(SkScalarPin(specular, 0, 16) * kSpecularMultiplier + 0.5);

    return SkEmbossMaskFilter::Make(blurSigma, light);
}
#endif

///////////////////////////////////////////////////////////////////////////////

SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
    : fLight(light), fBlurSigma(blurSigma)
{
    SkASSERT(fBlurSigma > 0);
    SkASSERT(SkScalarsAreFinite(fLight.fDirection, 3));
}

SkMask::Format SkEmbossMaskFilter::getFormat() const {
    return SkMask::k3D_Format;
}

bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
                                    const SkMatrix& matrix, SkIPoint* margin) const {
    SkScalar sigma = matrix.mapRadius(fBlurSigma);

    if (!SkBlurMask::BoxBlur(dst, src, sigma, kInner_SkBlurStyle, kLow_SkBlurQuality)) {
        return false;
    }

    dst->fFormat = SkMask::k3D_Format;
    if (margin) {
        margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
    }

    if (src.fImage == nullptr) {
        return true;
    }

    // create a larger buffer for the other two channels (should force fBlur to do this for us)

    {
        uint8_t* alphaPlane = dst->fImage;
        size_t   planeSize = dst->computeImageSize();
        if (0 == planeSize) {
            return false;   // too big to allocate, abort
        }
        dst->fImage = SkMask::AllocImage(planeSize * 3);
        memcpy(dst->fImage, alphaPlane, planeSize);
        SkMask::FreeImage(alphaPlane);
    }

    // run the light direction through the matrix...
    Light   light = fLight;
    matrix.mapVectors((SkVector*)(void*)light.fDirection,
                      (SkVector*)(void*)fLight.fDirection, 1);

    // now restore the length of the XY component
    // cast to SkVector so we can call setLength (this double cast silences alias warnings)
    SkVector* vec = (SkVector*)(void*)light.fDirection;
    vec->setLength(light.fDirection[0],
                   light.fDirection[1],
                   SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));

    SkEmbossMask::Emboss(dst, light);

    // restore original alpha
    memcpy(dst->fImage, src.fImage, src.computeImageSize());

    return true;
}

sk_sp<SkFlattenable> SkEmbossMaskFilter::CreateProc(SkReadBuffer& buffer) {
    Light light;
    if (buffer.readByteArray(&light, sizeof(Light))) {
        light.fPad = 0; // for the font-cache lookup to be clean
        const SkScalar sigma = buffer.readScalar();
        return Make(sigma, light);
    }
    return nullptr;
}

void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
    Light tmpLight = fLight;
    tmpLight.fPad = 0;    // for the font-cache lookup to be clean
    buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
    buffer.writeScalar(fBlurSigma);
}

#ifndef SK_IGNORE_TO_STRING
void SkEmbossMaskFilter::toString(SkString* str) const {
    str->append("SkEmbossMaskFilter: (");

    str->append("direction: (");
    str->appendScalar(fLight.fDirection[0]);
    str->append(", ");
    str->appendScalar(fLight.fDirection[1]);
    str->append(", ");
    str->appendScalar(fLight.fDirection[2]);
    str->append(") ");

    str->appendf("ambient: %d specular: %d ",
        fLight.fAmbient, fLight.fSpecular);

    str->append("blurSigma: ");
    str->appendScalar(fBlurSigma);
    str->append(")");
}
#endif
<|endoftext|>


<commit_before>// main window and event handling for X11

#include <stdio.h>
#include <stdarg.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <GL/glx.h>

#include "drawstuff/drawstuff.h"
#include "drawstuff/version.h"
#include "internal.h"

//***************************************************************************
// error handling for unix

static void printMessage (char *msg1, char *msg2, va_list ap)
{
  fflush (stderr);
  fflush (stdout);
  fprintf (stderr,"\n%s: ",msg1);
  vfprintf (stderr,msg2,ap);
  fprintf (stderr,"\n");
  fflush (stderr);
}

extern "C" void dsError (char *msg, ...)
{
  va_list ap;
  va_start (ap,msg);
  printMessage ("Error",msg,ap);
  exit (1);
}

extern "C" void dsDebug (char *msg, ...)
{
  va_list ap;
  va_start (ap,msg);
  printMessage ("INTERNAL ERROR",msg,ap);
  // *((char *)0) = 0;	 ... commit SEGVicide ?
  abort();
}

extern "C" void dsPrint (char *msg, ...)
{
  va_list ap;
  va_start (ap,msg);
  vprintf (msg,ap);
}

//***************************************************************************
// openGL window

// X11 display info
static Display *display=0;
static int screen=0;
static XVisualInfo *visual=0;		// best visual for openGL
static Colormap colormap=0;		// window's colormap
static Atom wm_protocols_atom = 0;
static Atom wm_delete_window_atom = 0;

// window and openGL
static Window win=0;			// X11 window, 0 if not initialized
static int width=0,height=0;		// window size
static GLXContext glx_context=0;	// openGL rendering context
static int last_key_pressed=0;		// last key pressed in the window
static int run=1;			// 1 if simulation running
static int pause=0;			// 1 if in `pause' mode
static int singlestep=0;		// 1 if single step key pressed
static int writeframes=0;		// 1 if frame files to be written

static void createMainWindow (int _width, int _height)
{
  // create X11 display connection
  display = XOpenDisplay (0);
  if (!display) dsError (0,"can not open X11 display");
  screen = DefaultScreen(display);

  // get GL visual
  static int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,16,
			     GLX_RED_SIZE,4, GLX_GREEN_SIZE,4,
			     GLX_BLUE_SIZE,4, None};
  visual = glXChooseVisual (display,screen,attribList);
  if (!visual) dsError ("no good X11 visual found for OpenGL");

  // create colormap
  colormap = XCreateColormap (display,RootWindow(display,screen),
			      visual->visual,AllocNone);

  // initialize variables
  win = 0;
  width = _width;
  height = _height;
  glx_context = 0;
  last_key_pressed = 0;

  if (width < 1 || height < 1) dsDebug (0,"bad window width or height");

  // create the window
  XSetWindowAttributes attributes;
  attributes.background_pixel = BlackPixel(display,screen);
  attributes.colormap = colormap;
  attributes.event_mask = ButtonPressMask | ButtonReleaseMask |
    KeyPressMask | KeyReleaseMask | ButtonMotionMask | PointerMotionHintMask |
    StructureNotifyMask;
  win = XCreateWindow (display,RootWindow(display,screen),50,50,width,height,
		       0,visual->depth, InputOutput,visual->visual,
		       CWBackPixel | CWColormap | CWEventMask,&attributes);

  // associate a GLX context with the window
  glx_context = glXCreateContext (display,visual,0,GL_TRUE);
  if (!glx_context) dsError ("can't make an OpenGL context");

  // set the window title
  XTextProperty window_name;
  window_name.value = (unsigned char *) "Simulation";
  window_name.encoding = XA_STRING;
  window_name.format = 8;
  window_name.nitems = strlen((char *) window_name.value);
  XSetWMName (display,win,&window_name);

  // participate in the window manager 'delete yourself' protocol
  wm_protocols_atom = XInternAtom (display,"WM_PROTOCOLS",False);
  wm_delete_window_atom = XInternAtom (display,"WM_DELETE_WINDOW",False);
  if (XSetWMProtocols (display,win,&wm_delete_window_atom,1)==0)
    dsError ("XSetWMProtocols() call failed");

  // pop up the window
  XMapWindow (display,win);
  XSync (display,win);
}

static void destroyMainWindow()
{
  glXDestroyContext (display,glx_context);
  XDestroyWindow (display,win);
  XSync (display,0);
  display = 0;
  win = 0;
  glx_context = 0;
}

static void handleEvent (XEvent &event, dsFunctions *fn)
{
  static int mx=0,my=0; 	// mouse position
  static int mode = 0;		// mouse button bits

  switch (event.type) {

  case ButtonPress: {
    if (event.xbutton.button == Button1) mode |= 1;
    if (event.xbutton.button == Button2) mode |= 2;
    if (event.xbutton.button == Button3) mode |= 4;
    mx = event.xbutton.x;
    my = event.xbutton.y;
  }
  return;

  case ButtonRelease: {
    if (event.xbutton.button == Button1) mode &= (~1);
    if (event.xbutton.button == Button2) mode &= (~2);
    if (event.xbutton.button == Button3) mode &= (~4);
    mx = event.xbutton.x;
    my = event.xbutton.x;
  }
  return;

  case MotionNotify: {
    if (event.xmotion.is_hint) {
      Window root,child;
      unsigned int mask;
      XQueryPointer (display,win,&root,&child,&event.xbutton.x_root,
		     &event.xbutton.y_root,&event.xbutton.x,&event.xbutton.y,
		     &mask);
    }
    dsMotion (mode, event.xmotion.x - mx, event.xmotion.y - my);
    mx = event.xmotion.x;
    my = event.xmotion.y;
  }
  return;

  case KeyPress: {
    KeySym key;
    XLookupString (&event.xkey,NULL,0,&key,0);
    if ( (event.xkey.state & (ShiftMask | ControlMask)) == 0) {
      if (key >= ' ' && key <= 126) fn->command (key);
    }
    else if (event.xkey.state & ControlMask) {
      switch (key) {
      case 't': case 'T':
	dsSetTextures (dsGetTextures() ^ 1);
	break;
      case 's': case 'S':
	dsSetShadows (dsGetShadows() ^ 1);
	break;
      case 'x': case 'X':
	run = 0;
	break;
      case 'p': case 'P':
	pause ^= 1;
	singlestep = 0;
	break;
      case 'o': case 'O':
	if (pause) singlestep = 1;
	break;
      case 'v': case 'V': {
	float xyz[3],hpr[3];
	dsGetViewpoint (xyz,hpr);
	printf ("Viewpoint = (%.4f,%.4f,%.4f,%.4f,%.4f,%.4f)\n",
		xyz[0],xyz[1],xyz[2],hpr[0],hpr[1],hpr[2]);
	break;
      }
      case 'w': case 'W':
	writeframes ^= 1;
	if (writeframes) printf ("Now writing frames to PPM files\n");
	break;
      }
    }
    last_key_pressed = key;		// a kludgy place to put this...
  }
  return;

  case KeyRelease: {
    // hmmmm...
  }
  return;

  case ClientMessage:
    if (event.xclient.message_type == wm_protocols_atom &&
	event.xclient.format == 32 &&
	Atom(event.xclient.data.l[0]) == wm_delete_window_atom) {
      run = 0;
      return;
    }
    return;

  case ConfigureNotify:
    width = event.xconfigure.width;
    height = event.xconfigure.height;
    return;
  }
}

// return the index of the highest bit
static int getHighBitIndex (unsigned int x)
{
  int i = 0;
  while (x) {
    i++;
    x >>= 1;
  }
  return i-1;
}

// shift x left by i, where i can be positive or negative
#define SHIFTL(x,i) (((i) >= 0) ? ((x) << (i)) : ((x) >> (-i)))

static void captureFrame (int num)
{
  fprintf (stderr,"capturing frame %04d\n",num);

  char s[100];
  sprintf (s,"frame/frame%04d.ppm",num);
  FILE *f = fopen (s,"wb");
  if (!f) dsError ("can't open \"%s\" for writing",s);
  fprintf (f,"P6\n%d %d\n255\n",width,height);
  XImage *image = XGetImage (display,win,0,0,width,height,~0,ZPixmap);

  int rshift = 7 - getHighBitIndex (image->red_mask);
  int gshift = 7 - getHighBitIndex (image->green_mask);
  int bshift = 7 - getHighBitIndex (image->blue_mask);

  for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
      unsigned long pixel = XGetPixel (image,x,y);
      unsigned char b[3];
      b[0] = SHIFTL(pixel & image->red_mask,rshift);
      b[1] = SHIFTL(pixel & image->green_mask,gshift);
      b[2] = SHIFTL(pixel & image->blue_mask,bshift);
      fwrite (b,3,1,f);
    }
  }
  fclose (f);
  XDestroyImage (image);
}

void dsPlatformSimLoop (int window_width, int window_height, dsFunctions *fn,
			int initial_pause)
{
  pause = initial_pause;
  createMainWindow (window_width, window_height);
  glXMakeCurrent (display,win,glx_context);

  dsStartGraphics (window_width,window_height,fn);
  if (fn->start) fn->start();

  fprintf (stderr,
	   "\n"
	   "Simulation test environment v%d.%02d\n"
	   "   Ctrl-P : pause / unpause.\n"
	   "   Ctrl-O : single step when paused.\n"
	   "   Ctrl-T : toggle textures (or say `-notex' on command line).\n"
	   "   Ctrl-S : toggle shadows (or say `-noshadow' on command line).\n"
	   "   Ctrl-V : print current viewpoint coordinates (x,y,z,h,p,r).\n"
	   "   Ctrl-W : write frames to ppm files: frame/frameNNN.ppm\n"
	   "   Ctrl-X : exit.\n"
	   "\n",DS_VERSION >> 8,DS_VERSION & 0xff);

  int frame = 1;
  run = 1;
  while (run) {
    // read in and process all pending events for the main window
    XEvent event;
    while (run && XPending (display)) {
      XNextEvent (display,&event);
      handleEvent (event,fn);
    }

    dsDrawFrame (width,height,fn,pause && !singlestep);
    singlestep = 0;

    glFlush();
    glXSwapBuffers (display,win);
    XSync (display,0);

    // capture frames if necessary
    printf ("%d %d\n",pause,writeframes);
    if (pause==0 && writeframes) {
      captureFrame (frame);
      frame++;
    }
  };

  if (fn->stop) fn->stop();
  dsStopGraphics();

  destroyMainWindow();
}
<commit_msg>remove printf.<commit_after>// main window and event handling for X11

#include <stdio.h>
#include <stdarg.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <GL/glx.h>

#include "drawstuff/drawstuff.h"
#include "drawstuff/version.h"
#include "internal.h"

//***************************************************************************
// error handling for unix

static void printMessage (char *msg1, char *msg2, va_list ap)
{
  fflush (stderr);
  fflush (stdout);
  fprintf (stderr,"\n%s: ",msg1);
  vfprintf (stderr,msg2,ap);
  fprintf (stderr,"\n");
  fflush (stderr);
}

extern "C" void dsError (char *msg, ...)
{
  va_list ap;
  va_start (ap,msg);
  printMessage ("Error",msg,ap);
  exit (1);
}

extern "C" void dsDebug (char *msg, ...)
{
  va_list ap;
  va_start (ap,msg);
  printMessage ("INTERNAL ERROR",msg,ap);
  // *((char *)0) = 0;	 ... commit SEGVicide ?
  abort();
}

extern "C" void dsPrint (char *msg, ...)
{
  va_list ap;
  va_start (ap,msg);
  vprintf (msg,ap);
}

//***************************************************************************
// openGL window

// X11 display info
static Display *display=0;
static int screen=0;
static XVisualInfo *visual=0;		// best visual for openGL
static Colormap colormap=0;		// window's colormap
static Atom wm_protocols_atom = 0;
static Atom wm_delete_window_atom = 0;

// window and openGL
static Window win=0;			// X11 window, 0 if not initialized
static int width=0,height=0;		// window size
static GLXContext glx_context=0;	// openGL rendering context
static int last_key_pressed=0;		// last key pressed in the window
static int run=1;			// 1 if simulation running
static int pause=0;			// 1 if in `pause' mode
static int singlestep=0;		// 1 if single step key pressed
static int writeframes=0;		// 1 if frame files to be written

static void createMainWindow (int _width, int _height)
{
  // create X11 display connection
  display = XOpenDisplay (0);
  if (!display) dsError (0,"can not open X11 display");
  screen = DefaultScreen(display);

  // get GL visual
  static int attribList[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE,16,
			     GLX_RED_SIZE,4, GLX_GREEN_SIZE,4,
			     GLX_BLUE_SIZE,4, None};
  visual = glXChooseVisual (display,screen,attribList);
  if (!visual) dsError ("no good X11 visual found for OpenGL");

  // create colormap
  colormap = XCreateColormap (display,RootWindow(display,screen),
			      visual->visual,AllocNone);

  // initialize variables
  win = 0;
  width = _width;
  height = _height;
  glx_context = 0;
  last_key_pressed = 0;

  if (width < 1 || height < 1) dsDebug (0,"bad window width or height");

  // create the window
  XSetWindowAttributes attributes;
  attributes.background_pixel = BlackPixel(display,screen);
  attributes.colormap = colormap;
  attributes.event_mask = ButtonPressMask | ButtonReleaseMask |
    KeyPressMask | KeyReleaseMask | ButtonMotionMask | PointerMotionHintMask |
    StructureNotifyMask;
  win = XCreateWindow (display,RootWindow(display,screen),50,50,width,height,
		       0,visual->depth, InputOutput,visual->visual,
		       CWBackPixel | CWColormap | CWEventMask,&attributes);

  // associate a GLX context with the window
  glx_context = glXCreateContext (display,visual,0,GL_TRUE);
  if (!glx_context) dsError ("can't make an OpenGL context");

  // set the window title
  XTextProperty window_name;
  window_name.value = (unsigned char *) "Simulation";
  window_name.encoding = XA_STRING;
  window_name.format = 8;
  window_name.nitems = strlen((char *) window_name.value);
  XSetWMName (display,win,&window_name);

  // participate in the window manager 'delete yourself' protocol
  wm_protocols_atom = XInternAtom (display,"WM_PROTOCOLS",False);
  wm_delete_window_atom = XInternAtom (display,"WM_DELETE_WINDOW",False);
  if (XSetWMProtocols (display,win,&wm_delete_window_atom,1)==0)
    dsError ("XSetWMProtocols() call failed");

  // pop up the window
  XMapWindow (display,win);
  XSync (display,win);
}

static void destroyMainWindow()
{
  glXDestroyContext (display,glx_context);
  XDestroyWindow (display,win);
  XSync (display,0);
  display = 0;
  win = 0;
  glx_context = 0;
}

static void handleEvent (XEvent &event, dsFunctions *fn)
{
  static int mx=0,my=0; 	// mouse position
  static int mode = 0;		// mouse button bits

  switch (event.type) {

  case ButtonPress: {
    if (event.xbutton.button == Button1) mode |= 1;
    if (event.xbutton.button == Button2) mode |= 2;
    if (event.xbutton.button == Button3) mode |= 4;
    mx = event.xbutton.x;
    my = event.xbutton.y;
  }
  return;

  case ButtonRelease: {
    if (event.xbutton.button == Button1) mode &= (~1);
    if (event.xbutton.button == Button2) mode &= (~2);
    if (event.xbutton.button == Button3) mode &= (~4);
    mx = event.xbutton.x;
    my = event.xbutton.x;
  }
  return;

  case MotionNotify: {
    if (event.xmotion.is_hint) {
      Window root,child;
      unsigned int mask;
      XQueryPointer (display,win,&root,&child,&event.xbutton.x_root,
		     &event.xbutton.y_root,&event.xbutton.x,&event.xbutton.y,
		     &mask);
    }
    dsMotion (mode, event.xmotion.x - mx, event.xmotion.y - my);
    mx = event.xmotion.x;
    my = event.xmotion.y;
  }
  return;

  case KeyPress: {
    KeySym key;
    XLookupString (&event.xkey,NULL,0,&key,0);
    if ( (event.xkey.state & (ShiftMask | ControlMask)) == 0) {
      if (key >= ' ' && key <= 126) fn->command (key);
    }
    else if (event.xkey.state & ControlMask) {
      switch (key) {
      case 't': case 'T':
	dsSetTextures (dsGetTextures() ^ 1);
	break;
      case 's': case 'S':
	dsSetShadows (dsGetShadows() ^ 1);
	break;
      case 'x': case 'X':
	run = 0;
	break;
      case 'p': case 'P':
	pause ^= 1;
	singlestep = 0;
	break;
      case 'o': case 'O':
	if (pause) singlestep = 1;
	break;
      case 'v': case 'V': {
	float xyz[3],hpr[3];
	dsGetViewpoint (xyz,hpr);
	printf ("Viewpoint = (%.4f,%.4f,%.4f,%.4f,%.4f,%.4f)\n",
		xyz[0],xyz[1],xyz[2],hpr[0],hpr[1],hpr[2]);
	break;
      }
      case 'w': case 'W':
	writeframes ^= 1;
	if (writeframes) printf ("Now writing frames to PPM files\n");
	break;
      }
    }
    last_key_pressed = key;		// a kludgy place to put this...
  }
  return;

  case KeyRelease: {
    // hmmmm...
  }
  return;

  case ClientMessage:
    if (event.xclient.message_type == wm_protocols_atom &&
	event.xclient.format == 32 &&
	Atom(event.xclient.data.l[0]) == wm_delete_window_atom) {
      run = 0;
      return;
    }
    return;

  case ConfigureNotify:
    width = event.xconfigure.width;
    height = event.xconfigure.height;
    return;
  }
}

// return the index of the highest bit
static int getHighBitIndex (unsigned int x)
{
  int i = 0;
  while (x) {
    i++;
    x >>= 1;
  }
  return i-1;
}

// shift x left by i, where i can be positive or negative
#define SHIFTL(x,i) (((i) >= 0) ? ((x) << (i)) : ((x) >> (-i)))

static void captureFrame (int num)
{
  fprintf (stderr,"capturing frame %04d\n",num);

  char s[100];
  sprintf (s,"frame/frame%04d.ppm",num);
  FILE *f = fopen (s,"wb");
  if (!f) dsError ("can't open \"%s\" for writing",s);
  fprintf (f,"P6\n%d %d\n255\n",width,height);
  XImage *image = XGetImage (display,win,0,0,width,height,~0,ZPixmap);

  int rshift = 7 - getHighBitIndex (image->red_mask);
  int gshift = 7 - getHighBitIndex (image->green_mask);
  int bshift = 7 - getHighBitIndex (image->blue_mask);

  for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
      unsigned long pixel = XGetPixel (image,x,y);
      unsigned char b[3];
      b[0] = SHIFTL(pixel & image->red_mask,rshift);
      b[1] = SHIFTL(pixel & image->green_mask,gshift);
      b[2] = SHIFTL(pixel & image->blue_mask,bshift);
      fwrite (b,3,1,f);
    }
  }
  fclose (f);
  XDestroyImage (image);
}

void dsPlatformSimLoop (int window_width, int window_height, dsFunctions *fn,
			int initial_pause)
{
  pause = initial_pause;
  createMainWindow (window_width, window_height);
  glXMakeCurrent (display,win,glx_context);

  dsStartGraphics (window_width,window_height,fn);
  if (fn->start) fn->start();

  fprintf (stderr,
	   "\n"
	   "Simulation test environment v%d.%02d\n"
	   "   Ctrl-P : pause / unpause.\n"
	   "   Ctrl-O : single step when paused.\n"
	   "   Ctrl-T : toggle textures (or say `-notex' on command line).\n"
	   "   Ctrl-S : toggle shadows (or say `-noshadow' on command line).\n"
	   "   Ctrl-V : print current viewpoint coordinates (x,y,z,h,p,r).\n"
	   "   Ctrl-W : write frames to ppm files: frame/frameNNN.ppm\n"
	   "   Ctrl-X : exit.\n"
	   "\n",DS_VERSION >> 8,DS_VERSION & 0xff);

  int frame = 1;
  run = 1;
  while (run) {
    // read in and process all pending events for the main window
    XEvent event;
    while (run && XPending (display)) {
      XNextEvent (display,&event);
      handleEvent (event,fn);
    }

    dsDrawFrame (width,height,fn,pause && !singlestep);
    singlestep = 0;

    glFlush();
    glXSwapBuffers (display,win);
    XSync (display,0);

    // capture frames if necessary
    if (pause==0 && writeframes) {
      captureFrame (frame);
      frame++;
    }
  };

  if (fn->stop) fn->stop();
  dsStopGraphics();

  destroyMainWindow();
}
<|endoftext|>


<commit_before>/*
 *  Copyright 2019-2020 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *
 *  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.
 *
 *  In no event and under no legal theory, whether in tort (including negligence),
 *  contract, or otherwise, unless required by applicable law (such as deliberate
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental,
 *  or consequential damages of any character arising as a result of this License or
 *  out of the use or inability to use the software (including but not limited to damages
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and
 *  all other commercial damages or losses), even if such Contributor has been advised
 *  of the possibility of such damages.
 */

#include <vector>
#include <cstring>

#if PLATFORM_ANDROID
// Android provides Vulkan loader starting with API level 24. To be
// compatible with earlier versions, we will use manual loading.
#    define VOLK_IMPLEMENTATION
#    include "volk/volk.h"
#endif

#include "VulkanErrors.hpp"
#include "VulkanUtilities/VulkanInstance.hpp"
#include "VulkanUtilities/VulkanDebug.hpp"

#if !NO_GLSLANG
#    include "SPIRVUtils.hpp"
#endif

#include "PlatformDefinitions.h"

namespace VulkanUtilities
{

bool VulkanInstance::IsLayerAvailable(const char* LayerName) const
{
    for (const auto& Layer : m_Layers)
        if (strcmp(Layer.layerName, LayerName) == 0)
            return true;

    return false;
}

bool VulkanInstance::IsExtensionAvailable(const char* ExtensionName) const
{
    for (const auto& Extension : m_Extensions)
        if (strcmp(Extension.extensionName, ExtensionName) == 0)
            return true;

    return false;
}

std::shared_ptr<VulkanInstance> VulkanInstance::Create(bool                   EnableValidation,
                                                       uint32_t               GlobalExtensionCount,
                                                       const char* const*     ppGlobalExtensionNames,
                                                       VkAllocationCallbacks* pVkAllocator)
{
    auto Instance = new VulkanInstance{EnableValidation, GlobalExtensionCount, ppGlobalExtensionNames, pVkAllocator};
    return std::shared_ptr<VulkanInstance>{Instance};
}

VulkanInstance::VulkanInstance(bool                   EnableValidation,
                               uint32_t               GlobalExtensionCount,
                               const char* const*     ppGlobalExtensionNames,
                               VkAllocationCallbacks* pVkAllocator) :
    m_pVkAllocator{pVkAllocator}
{
#if PLATFORM_ANDROID
    if (volkInitialize() != VK_SUCCESS)
    {
        LOG_ERROR_AND_THROW("Failed to load Vulkan.");
    }
#endif

    {
        // Enumerate available layers
        uint32_t LayerCount = 0;

        auto res = vkEnumerateInstanceLayerProperties(&LayerCount, nullptr);
        CHECK_VK_ERROR_AND_THROW(res, "Failed to query layer count");
        m_Layers.resize(LayerCount);
        vkEnumerateInstanceLayerProperties(&LayerCount, m_Layers.data());
        CHECK_VK_ERROR_AND_THROW(res, "Failed to enumerate extensions");
        VERIFY_EXPR(LayerCount == m_Layers.size());
    }

    {
        // Enumerate available extensions
        uint32_t ExtensionCount = 0;

        auto res = vkEnumerateInstanceExtensionProperties(nullptr, &ExtensionCount, nullptr);
        CHECK_VK_ERROR_AND_THROW(res, "Failed to query extension count");
        m_Extensions.resize(ExtensionCount);
        vkEnumerateInstanceExtensionProperties(nullptr, &ExtensionCount, m_Extensions.data());
        CHECK_VK_ERROR_AND_THROW(res, "Failed to enumerate extensions");
        VERIFY_EXPR(ExtensionCount == m_Extensions.size());
    }

    std::vector<const char*> GlobalExtensions =
    {
        VK_KHR_SURFACE_EXTENSION_NAME,

    // Enable surface extensions depending on OS
#if defined(VK_USE_PLATFORM_WIN32_KHR)
        VK_KHR_WIN32_SURFACE_EXTENSION_NAME
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
        VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
        VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
#elif defined(VK_USE_PLATFORM_XCB_KHR)
        VK_KHR_XCB_SURFACE_EXTENSION_NAME
#elif defined(VK_USE_PLATFORM_IOS_MVK)
        VK_MVK_IOS_SURFACE_EXTENSION_NAME
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
        VK_MVK_MACOS_SURFACE_EXTENSION_NAME
#endif
    };

    if (ppGlobalExtensionNames != nullptr)
    {
        for (uint32_t ext = 0; ext < GlobalExtensionCount; ++ext)
            GlobalExtensions.push_back(ppGlobalExtensionNames[ext]);
    }
    else
    {
        if (GlobalExtensionCount != 0)
            LOG_ERROR_MESSAGE("Global extensions pointer is null while extensions count is ", GlobalExtensionCount,
                              ". Please initialize 'ppGlobalExtensionNames' member of EngineVkCreateInfo struct.");
    }

    for (const auto* ExtName : GlobalExtensions)
    {
        if (!IsExtensionAvailable(ExtName))
            LOG_ERROR_AND_THROW("Required extension ", ExtName, " is not available");
    }

    if (EnableValidation)
    {
        m_DebugUtilsEnabled = IsExtensionAvailable(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
        if (m_DebugUtilsEnabled)
        {
            GlobalExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
        }
        else
        {
            LOG_WARNING_MESSAGE("Extension ", VK_EXT_DEBUG_UTILS_EXTENSION_NAME, " is not available.");
        }
    }

    VkApplicationInfo appInfo = {};

    appInfo.sType              = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pNext              = nullptr; // Pointer to an extension-specific structure.
    appInfo.pApplicationName   = nullptr;
    appInfo.applicationVersion = 0; // Developer-supplied version number of the application
    appInfo.pEngineName        = "Diligent Engine";
    appInfo.engineVersion      = 0; // Developer-supplied version number of the engine used to create the application.
    appInfo.apiVersion         = VK_API_VERSION_1_0;

    VkInstanceCreateInfo InstanceCreateInfo = {};

    InstanceCreateInfo.sType                   = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    InstanceCreateInfo.pNext                   = nullptr; // Pointer to an extension-specific structure.
    InstanceCreateInfo.flags                   = 0;       // Reserved for future use.
    InstanceCreateInfo.pApplicationInfo        = &appInfo;
    InstanceCreateInfo.enabledExtensionCount   = static_cast<uint32_t>(GlobalExtensions.size());
    InstanceCreateInfo.ppEnabledExtensionNames = GlobalExtensions.empty() ? nullptr : GlobalExtensions.data();

    if (EnableValidation)
    {
        bool ValidationLayersPresent = true;
        for (size_t l = 0; l < _countof(VulkanUtilities::ValidationLayerNames); ++l)
        {
            auto* pLayerName = VulkanUtilities::ValidationLayerNames[l];
            if (!IsLayerAvailable(pLayerName))
            {
                ValidationLayersPresent = false;
                LOG_WARNING_MESSAGE("Failed to find ", pLayerName, " layer. Validation will be disabled");
            }
        }
        if (ValidationLayersPresent)
        {
            InstanceCreateInfo.enabledLayerCount   = static_cast<uint32_t>(_countof(VulkanUtilities::ValidationLayerNames));
            InstanceCreateInfo.ppEnabledLayerNames = VulkanUtilities::ValidationLayerNames;
        }
    }
    auto res = vkCreateInstance(&InstanceCreateInfo, m_pVkAllocator, &m_VkInstance);
    CHECK_VK_ERROR_AND_THROW(res, "Failed to create Vulkan instance");

#if PLATFORM_ANDROID
    volkLoadInstance(m_VkInstance);
#endif

    // If requested, we enable the default validation layers for debugging
    if (m_DebugUtilsEnabled)
    {
        VkDebugUtilsMessageSeverityFlagsEXT messageSeverity =
            VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
            VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
        VkDebugUtilsMessageTypeFlagsEXT messageType =
            VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
            VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
            VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
        VulkanUtilities::SetupDebugging(m_VkInstance, messageSeverity, messageType, nullptr);
    }

    // Enumerate physical devices
    {
        // Physical device
        uint32_t PhysicalDeviceCount = 0;
        // Get the number of available physical devices
        auto err = vkEnumeratePhysicalDevices(m_VkInstance, &PhysicalDeviceCount, nullptr);
        CHECK_VK_ERROR(err, "Failed to get physical device count");
        if (PhysicalDeviceCount == 0)
            LOG_ERROR_AND_THROW("No physical devices found on the system");

        // Enumerate devices
        m_PhysicalDevices.resize(PhysicalDeviceCount);
        err = vkEnumeratePhysicalDevices(m_VkInstance, &PhysicalDeviceCount, m_PhysicalDevices.data());
        CHECK_VK_ERROR(err, "Failed to enumerate physical devices");
        VERIFY_EXPR(m_PhysicalDevices.size() == PhysicalDeviceCount);
    }
#if !NO_GLSLANG
    Diligent::InitializeGlslang();
#endif
}

VulkanInstance::~VulkanInstance()
{
    if (m_DebugUtilsEnabled)
    {
        VulkanUtilities::FreeDebugging(m_VkInstance);
    }
    vkDestroyInstance(m_VkInstance, m_pVkAllocator);

#if !NO_GLSLANG
    Diligent::FinalizeGlslang();
#endif
}

VkPhysicalDevice VulkanInstance::SelectPhysicalDevice() const
{
    VkPhysicalDevice SelectedPhysicalDevice = VK_NULL_HANDLE;

    // Select a device that exposes a queue family that suppors both compute and graphics operations
    // Prefer discrete GPU
    for (auto Device : m_PhysicalDevices)
    {
        VkPhysicalDeviceProperties DeviceProps;
        vkGetPhysicalDeviceProperties(Device, &DeviceProps);

        uint32_t QueueFamilyCount = 0;
        vkGetPhysicalDeviceQueueFamilyProperties(Device, &QueueFamilyCount, nullptr);
        VERIFY_EXPR(QueueFamilyCount > 0);
        std::vector<VkQueueFamilyProperties> QueueFamilyProperties(QueueFamilyCount);
        vkGetPhysicalDeviceQueueFamilyProperties(Device, &QueueFamilyCount, QueueFamilyProperties.data());
        VERIFY_EXPR(QueueFamilyCount == QueueFamilyProperties.size());

        // If an implementation exposes any queue family that supports graphics operations,
        // at least one queue family of at least one physical device exposed by the implementation
        // must support both graphics and compute operations.
        bool GraphicsAndComputeQueueSupported = false;
        for (const auto& QueueFamilyProps : QueueFamilyProperties)
        {
            if ((QueueFamilyProps.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0 &&
                (QueueFamilyProps.queueFlags & VK_QUEUE_COMPUTE_BIT) != 0)
            {
                GraphicsAndComputeQueueSupported = true;
                break;
            }
        }
        if (GraphicsAndComputeQueueSupported)
        {
            SelectedPhysicalDevice = Device;
            if (DeviceProps.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
                break;
        }
    }

    if (SelectedPhysicalDevice != VK_NULL_HANDLE)
    {
        VkPhysicalDeviceProperties SelectedDeviceProps;
        vkGetPhysicalDeviceProperties(SelectedPhysicalDevice, &SelectedDeviceProps);
        LOG_INFO_MESSAGE("Using physical device '", SelectedDeviceProps.deviceName, '\'');
    }
    else
    {
        LOG_ERROR_MESSAGE("Failed to find suitable physical device");
    }

    return SelectedPhysicalDevice;
}

} // namespace VulkanUtilities
<commit_msg>Fix for XLib support for Linux<commit_after>/*
 *  Copyright 2019-2020 Diligent Graphics LLC
 *  Copyright 2015-2019 Egor Yusov
 *
 *  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.
 *
 *  In no event and under no legal theory, whether in tort (including negligence),
 *  contract, or otherwise, unless required by applicable law (such as deliberate
 *  and grossly negligent acts) or agreed to in writing, shall any Contributor be
 *  liable for any damages, including any direct, indirect, special, incidental,
 *  or consequential damages of any character arising as a result of this License or
 *  out of the use or inability to use the software (including but not limited to damages
 *  for loss of goodwill, work stoppage, computer failure or malfunction, or any and
 *  all other commercial damages or losses), even if such Contributor has been advised
 *  of the possibility of such damages.
 */

#include <vector>
#include <cstring>

#if PLATFORM_ANDROID
// Android provides Vulkan loader starting with API level 24. To be
// compatible with earlier versions, we will use manual loading.
#    define VOLK_IMPLEMENTATION
#    include "volk/volk.h"
#endif

#include "VulkanErrors.hpp"
#include "VulkanUtilities/VulkanInstance.hpp"
#include "VulkanUtilities/VulkanDebug.hpp"

#if !NO_GLSLANG
#    include "SPIRVUtils.hpp"
#endif

#include "PlatformDefinitions.h"

namespace VulkanUtilities
{

bool VulkanInstance::IsLayerAvailable(const char* LayerName) const
{
    for (const auto& Layer : m_Layers)
        if (strcmp(Layer.layerName, LayerName) == 0)
            return true;

    return false;
}

bool VulkanInstance::IsExtensionAvailable(const char* ExtensionName) const
{
    for (const auto& Extension : m_Extensions)
        if (strcmp(Extension.extensionName, ExtensionName) == 0)
            return true;

    return false;
}

std::shared_ptr<VulkanInstance> VulkanInstance::Create(bool                   EnableValidation,
                                                       uint32_t               GlobalExtensionCount,
                                                       const char* const*     ppGlobalExtensionNames,
                                                       VkAllocationCallbacks* pVkAllocator)
{
    auto Instance = new VulkanInstance{EnableValidation, GlobalExtensionCount, ppGlobalExtensionNames, pVkAllocator};
    return std::shared_ptr<VulkanInstance>{Instance};
}

VulkanInstance::VulkanInstance(bool                   EnableValidation,
                               uint32_t               GlobalExtensionCount,
                               const char* const*     ppGlobalExtensionNames,
                               VkAllocationCallbacks* pVkAllocator) :
    m_pVkAllocator{pVkAllocator}
{
#if PLATFORM_ANDROID
    if (volkInitialize() != VK_SUCCESS)
    {
        LOG_ERROR_AND_THROW("Failed to load Vulkan.");
    }
#endif

    {
        // Enumerate available layers
        uint32_t LayerCount = 0;

        auto res = vkEnumerateInstanceLayerProperties(&LayerCount, nullptr);
        CHECK_VK_ERROR_AND_THROW(res, "Failed to query layer count");
        m_Layers.resize(LayerCount);
        vkEnumerateInstanceLayerProperties(&LayerCount, m_Layers.data());
        CHECK_VK_ERROR_AND_THROW(res, "Failed to enumerate extensions");
        VERIFY_EXPR(LayerCount == m_Layers.size());
    }

    {
        // Enumerate available extensions
        uint32_t ExtensionCount = 0;

        auto res = vkEnumerateInstanceExtensionProperties(nullptr, &ExtensionCount, nullptr);
        CHECK_VK_ERROR_AND_THROW(res, "Failed to query extension count");
        m_Extensions.resize(ExtensionCount);
        vkEnumerateInstanceExtensionProperties(nullptr, &ExtensionCount, m_Extensions.data());
        CHECK_VK_ERROR_AND_THROW(res, "Failed to enumerate extensions");
        VERIFY_EXPR(ExtensionCount == m_Extensions.size());
    }

    std::vector<const char*> GlobalExtensions =
    {
        VK_KHR_SURFACE_EXTENSION_NAME,

    // Enable surface extensions depending on OS
#if defined(VK_USE_PLATFORM_WIN32_KHR)
        VK_KHR_WIN32_SURFACE_EXTENSION_NAME,
#endif
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
        VK_KHR_ANDROID_SURFACE_EXTENSION_NAME,
#endif
#if defined(VK_USE_PLATFORM_WAYLAND_KHR)
        VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME,
#endif
#if defined(VK_USE_PLATFORM_XLIB_KHR)
        VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
#endif
#if defined(VK_USE_PLATFORM_XCB_KHR)
        VK_KHR_XCB_SURFACE_EXTENSION_NAME,
#endif
#if defined(VK_USE_PLATFORM_IOS_MVK)
        VK_MVK_IOS_SURFACE_EXTENSION_NAME,
#endif
#if defined(VK_USE_PLATFORM_MACOS_MVK)
        VK_MVK_MACOS_SURFACE_EXTENSION_NAME,
#endif
    };

    if (ppGlobalExtensionNames != nullptr)
    {
        for (uint32_t ext = 0; ext < GlobalExtensionCount; ++ext)
            GlobalExtensions.push_back(ppGlobalExtensionNames[ext]);
    }
    else
    {
        if (GlobalExtensionCount != 0)
            LOG_ERROR_MESSAGE("Global extensions pointer is null while extensions count is ", GlobalExtensionCount,
                              ". Please initialize 'ppGlobalExtensionNames' member of EngineVkCreateInfo struct.");
    }

    for (const auto* ExtName : GlobalExtensions)
    {
        if (!IsExtensionAvailable(ExtName))
            LOG_ERROR_AND_THROW("Required extension ", ExtName, " is not available");
    }

    if (EnableValidation)
    {
        m_DebugUtilsEnabled = IsExtensionAvailable(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
        if (m_DebugUtilsEnabled)
        {
            GlobalExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
        }
        else
        {
            LOG_WARNING_MESSAGE("Extension ", VK_EXT_DEBUG_UTILS_EXTENSION_NAME, " is not available.");
        }
    }

    VkApplicationInfo appInfo = {};

    appInfo.sType              = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pNext              = nullptr; // Pointer to an extension-specific structure.
    appInfo.pApplicationName   = nullptr;
    appInfo.applicationVersion = 0; // Developer-supplied version number of the application
    appInfo.pEngineName        = "Diligent Engine";
    appInfo.engineVersion      = 0; // Developer-supplied version number of the engine used to create the application.
    appInfo.apiVersion         = VK_API_VERSION_1_0;

    VkInstanceCreateInfo InstanceCreateInfo = {};

    InstanceCreateInfo.sType                   = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    InstanceCreateInfo.pNext                   = nullptr; // Pointer to an extension-specific structure.
    InstanceCreateInfo.flags                   = 0;       // Reserved for future use.
    InstanceCreateInfo.pApplicationInfo        = &appInfo;
    InstanceCreateInfo.enabledExtensionCount   = static_cast<uint32_t>(GlobalExtensions.size());
    InstanceCreateInfo.ppEnabledExtensionNames = GlobalExtensions.empty() ? nullptr : GlobalExtensions.data();

    if (EnableValidation)
    {
        bool ValidationLayersPresent = true;
        for (size_t l = 0; l < _countof(VulkanUtilities::ValidationLayerNames); ++l)
        {
            auto* pLayerName = VulkanUtilities::ValidationLayerNames[l];
            if (!IsLayerAvailable(pLayerName))
            {
                ValidationLayersPresent = false;
                LOG_WARNING_MESSAGE("Failed to find ", pLayerName, " layer. Validation will be disabled");
            }
        }
        if (ValidationLayersPresent)
        {
            InstanceCreateInfo.enabledLayerCount   = static_cast<uint32_t>(_countof(VulkanUtilities::ValidationLayerNames));
            InstanceCreateInfo.ppEnabledLayerNames = VulkanUtilities::ValidationLayerNames;
        }
    }
    auto res = vkCreateInstance(&InstanceCreateInfo, m_pVkAllocator, &m_VkInstance);
    CHECK_VK_ERROR_AND_THROW(res, "Failed to create Vulkan instance");

#if PLATFORM_ANDROID
    volkLoadInstance(m_VkInstance);
#endif

    // If requested, we enable the default validation layers for debugging
    if (m_DebugUtilsEnabled)
    {
        VkDebugUtilsMessageSeverityFlagsEXT messageSeverity =
            VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
            VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
        VkDebugUtilsMessageTypeFlagsEXT messageType =
            VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
            VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
            VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
        VulkanUtilities::SetupDebugging(m_VkInstance, messageSeverity, messageType, nullptr);
    }

    // Enumerate physical devices
    {
        // Physical device
        uint32_t PhysicalDeviceCount = 0;
        // Get the number of available physical devices
        auto err = vkEnumeratePhysicalDevices(m_VkInstance, &PhysicalDeviceCount, nullptr);
        CHECK_VK_ERROR(err, "Failed to get physical device count");
        if (PhysicalDeviceCount == 0)
            LOG_ERROR_AND_THROW("No physical devices found on the system");

        // Enumerate devices
        m_PhysicalDevices.resize(PhysicalDeviceCount);
        err = vkEnumeratePhysicalDevices(m_VkInstance, &PhysicalDeviceCount, m_PhysicalDevices.data());
        CHECK_VK_ERROR(err, "Failed to enumerate physical devices");
        VERIFY_EXPR(m_PhysicalDevices.size() == PhysicalDeviceCount);
    }
#if !NO_GLSLANG
    Diligent::InitializeGlslang();
#endif
}

VulkanInstance::~VulkanInstance()
{
    if (m_DebugUtilsEnabled)
    {
        VulkanUtilities::FreeDebugging(m_VkInstance);
    }
    vkDestroyInstance(m_VkInstance, m_pVkAllocator);

#if !NO_GLSLANG
    Diligent::FinalizeGlslang();
#endif
}

VkPhysicalDevice VulkanInstance::SelectPhysicalDevice() const
{
    VkPhysicalDevice SelectedPhysicalDevice = VK_NULL_HANDLE;

    // Select a device that exposes a queue family that suppors both compute and graphics operations
    // Prefer discrete GPU
    for (auto Device : m_PhysicalDevices)
    {
        VkPhysicalDeviceProperties DeviceProps;
        vkGetPhysicalDeviceProperties(Device, &DeviceProps);

        uint32_t QueueFamilyCount = 0;
        vkGetPhysicalDeviceQueueFamilyProperties(Device, &QueueFamilyCount, nullptr);
        VERIFY_EXPR(QueueFamilyCount > 0);
        std::vector<VkQueueFamilyProperties> QueueFamilyProperties(QueueFamilyCount);
        vkGetPhysicalDeviceQueueFamilyProperties(Device, &QueueFamilyCount, QueueFamilyProperties.data());
        VERIFY_EXPR(QueueFamilyCount == QueueFamilyProperties.size());

        // If an implementation exposes any queue family that supports graphics operations,
        // at least one queue family of at least one physical device exposed by the implementation
        // must support both graphics and compute operations.
        bool GraphicsAndComputeQueueSupported = false;
        for (const auto& QueueFamilyProps : QueueFamilyProperties)
        {
            if ((QueueFamilyProps.queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0 &&
                (QueueFamilyProps.queueFlags & VK_QUEUE_COMPUTE_BIT) != 0)
            {
                GraphicsAndComputeQueueSupported = true;
                break;
            }
        }
        if (GraphicsAndComputeQueueSupported)
        {
            SelectedPhysicalDevice = Device;
            if (DeviceProps.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
                break;
        }
    }

    if (SelectedPhysicalDevice != VK_NULL_HANDLE)
    {
        VkPhysicalDeviceProperties SelectedDeviceProps;
        vkGetPhysicalDeviceProperties(SelectedPhysicalDevice, &SelectedDeviceProps);
        LOG_INFO_MESSAGE("Using physical device '", SelectedDeviceProps.deviceName, '\'');
    }
    else
    {
        LOG_ERROR_MESSAGE("Failed to find suitable physical device");
    }

    return SelectedPhysicalDevice;
}

} // namespace VulkanUtilities
<|endoftext|>


<commit_before>#ifndef __MATCHER_TRAITS__
#define __MATCHER_TRAITS__

#include <type_traits>

#include <boost/mpl/vector.hpp>

#include <boost/function_types/function_type.hpp>
#include <boost/function_types/result_type.hpp>

#include "clang/ASTMatchers/ASTMatchers.h"

// TODO: handle templates: possible solution: replace templated parameters with
// a type such that any type can be converted to that type.

#define MATCHER(x)                                                             \
  std::tuple<typename matcher_trait<decltype(x)>::type,                        \
             typename matcher_trait<decltype(x)>::object_type,                 \
             MetaString<_S(#x)> >

template <typename T, typename U> struct matcher_trait_helper;

template <typename T> struct matcher_trait_helper<T, std::true_type> {
  typedef T type;
  typedef typename boost::function_types::result_type<T>::type object_type;
};

template <typename T, typename U>
struct matcher_trait_helper<
    const clang::ast_matchers::internal::VariadicDynCastAllOfMatcher<T, U>,
    std::false_type> {
  typedef typename boost::function_types::function_type<boost::mpl::vector<
      clang::ast_matchers::internal::Matcher<T>,
      clang::ast_matchers::internal::Matcher<U> > >::type type;
  typedef const clang::ast_matchers::internal::VariadicDynCastAllOfMatcher<T, U>
  object_type;
};

template <typename T>
struct matcher_trait_helper<
    const clang::ast_matchers::internal::VariadicAllOfMatcher<T>,
    std::false_type> {
  typedef typename boost::function_types::function_type<boost::mpl::vector<
      clang::ast_matchers::internal::Matcher<T>,
      clang::ast_matchers::internal::Matcher<T> > >::type type;
  typedef const clang::ast_matchers::internal::VariadicAllOfMatcher<T>
  object_type;
};

template <typename T>
struct matcher_trait
    : matcher_trait_helper<T, typename std::is_function<T>::type> {};

#endif
<commit_msg>Added further traits to decompose matcher types.<commit_after>#ifndef __MATCHER_TRAITS__
#define __MATCHER_TRAITS__

#include <type_traits>
#include <utility>

#include <boost/mpl/vector.hpp>

#include <boost/function_types/function_type.hpp>
#include <boost/function_types/result_type.hpp>
#include <boost/function_types/parameter_types.hpp>

#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Basic/LLVM.h"

// TODO: handle templates: possible solution: replace templated parameters with
// a type such that any type can be converted to that type.

#define MATCHER(x)                                                             \
  std::tuple<typename matcher_trait<decltype(x)>::type,                        \
             typename matcher_trait<decltype(x)>::object_type,                 \
             MetaString<_S(#x)> >

template <typename T, typename U> struct matcher_trait_helper;

template <typename T> struct matcher_trait_helper<T, std::true_type> {
  typedef T type;
  typedef typename boost::function_types::result_type<T>::type object_type;
};

template <typename T, typename U>
struct matcher_trait_helper<
    const clang::ast_matchers::internal::VariadicDynCastAllOfMatcher<T, U>,
    std::false_type> {
  typedef typename boost::function_types::function_type<boost::mpl::vector<
      clang::ast_matchers::internal::Matcher<T>,
      clang::ast_matchers::internal::Matcher<U> > >::type type;
  typedef const clang::ast_matchers::internal::VariadicDynCastAllOfMatcher<T, U>
  object_type;
};

template <typename T>
struct matcher_trait_helper<
    const clang::ast_matchers::internal::VariadicAllOfMatcher<T>,
    std::false_type> {
  typedef typename boost::function_types::function_type<boost::mpl::vector<
      clang::ast_matchers::internal::Matcher<T>,
      clang::ast_matchers::internal::Matcher<T> > >::type type;
  typedef const clang::ast_matchers::internal::VariadicAllOfMatcher<T>
  object_type;
};

template <template <typename ToArg, typename FromArg> class ArgumentAdapter,
          typename T, typename U>
struct matcher_trait_helper<
    const clang::ast_matchers::internal::ArgumentAdaptingMatcherFunc<
        ArgumentAdapter, T, U>,
    std::false_type> {
  typedef typename boost::function_types::function_type<boost::mpl::vector<
      clang::ast_matchers::internal::Matcher<T>,
      clang::ast_matchers::internal::Matcher<U> > >::type type;
  typedef const clang::ast_matchers::internal::ArgumentAdaptingMatcherFunc<
      ArgumentAdapter, T, U> object_type;
};

template<typename Result, typename Arg, Result (*Func)(llvm::ArrayRef<const Arg *>)>
struct var_func_container {
  typedef Result result;
  typedef Arg arg;
  Result (*f)(llvm::ArrayRef<const Arg *>) = Func;
};

template <typename Result, typename Arg,
          Result (*Func)(llvm::ArrayRef<const Arg *>)>
var_func_container<Result, Arg, Func>
getLLVMVarFunc(llvm::VariadicFunction<Result, Arg, Func> *) {}

template <typename T> struct matcher_trait_helper<const T, std::false_type> {

  typedef typename decltype(getLLVMVarFunc((T*)0))::result result;
  typedef typename decltype(getLLVMVarFunc((T*)0))::arg arg;

  typedef typename boost::function_types::function_type<
      boost::mpl::vector<result, arg> >::type type;

  typedef T object_type;
};

template <typename T>
struct matcher_trait
    : matcher_trait_helper<T, typename std::is_function<T>::type> {};

#endif
<|endoftext|>


<commit_before>#pragma once

#include "depthai/pipeline/Node.hpp"

// shared
#include "depthai-shared/pb/properties/StereoDepthProperties.hpp"

namespace dai {
namespace node {

class StereoDepth : public Node {
    dai::StereoDepthProperties properties;

    std::string getName() override;
    std::vector<Output> getOutputs() override;
    std::vector<Input> getInputs() override;
    nlohmann::json getProperties() override;
    std::shared_ptr<Node> clone() override;

  public:
    StereoDepth(const std::shared_ptr<PipelineImpl>& par, int64_t nodeId);

    Input left{*this, "left", Input::Type::SReceiver, {{DatatypeEnum::ImgFrame, true}}};
    Input right{*this, "right", Input::Type::SReceiver, {{DatatypeEnum::ImgFrame, true}}};
    Output depth{*this, "depth", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output disparity{*this, "disparity", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output syncedLeft{*this, "syncedLeft", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output syncedRight{*this, "syncedRight", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output rectifiedLeft{*this, "rectifiedLeft", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output rectifiedRight{*this, "rectifiedRight", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};

    // Specify local filesystem path to the calibration file. Empty string to use EEPROM
    void loadCalibrationFile(const std::string& path);
    // Specify calibration data as a vector of bytes. Empty vector to use EEPROM
    void loadCalibrationData(const std::vector<std::uint8_t>& data);
    // Optional (taken from MonoCamera nodes if they exist)
    void setInputResolution(int width, int height);

    void setMedianFilter(StereoDepthProperties::MedianFilter median);
    void setConfidenceThreshold(int confThr);
    void setLeftRightCheck(bool enable);
    void setSubpixel(bool enable);
    void setExtendedDisparity(bool enable);
    void setRectifyEdgeFillColor(int color);
    void setRectifyMirrorFrame(bool enable);
    void setOutputRectified(bool enable);
    void setOutputDepth(bool enable);
};

}  // namespace node
}  // namespace dai
<commit_msg>Change StereoDepth inputs from ImgFrame to Buffer, to allow XLinkIn nodes to link to<commit_after>#pragma once

#include "depthai/pipeline/Node.hpp"

// shared
#include "depthai-shared/pb/properties/StereoDepthProperties.hpp"

namespace dai {
namespace node {

class StereoDepth : public Node {
    dai::StereoDepthProperties properties;

    std::string getName() override;
    std::vector<Output> getOutputs() override;
    std::vector<Input> getInputs() override;
    nlohmann::json getProperties() override;
    std::shared_ptr<Node> clone() override;

  public:
    StereoDepth(const std::shared_ptr<PipelineImpl>& par, int64_t nodeId);

    Input left{*this, "left", Input::Type::SReceiver, {{DatatypeEnum::Buffer, true}}};
    Input right{*this, "right", Input::Type::SReceiver, {{DatatypeEnum::Buffer, true}}};
    Output depth{*this, "depth", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output disparity{*this, "disparity", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output syncedLeft{*this, "syncedLeft", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output syncedRight{*this, "syncedRight", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output rectifiedLeft{*this, "rectifiedLeft", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};
    Output rectifiedRight{*this, "rectifiedRight", Output::Type::MSender, {{DatatypeEnum::ImgFrame, false}}};

    // Specify local filesystem path to the calibration file. Empty string to use EEPROM
    void loadCalibrationFile(const std::string& path);
    // Specify calibration data as a vector of bytes. Empty vector to use EEPROM
    void loadCalibrationData(const std::vector<std::uint8_t>& data);
    // Optional (taken from MonoCamera nodes if they exist)
    void setInputResolution(int width, int height);

    void setMedianFilter(StereoDepthProperties::MedianFilter median);
    void setConfidenceThreshold(int confThr);
    void setLeftRightCheck(bool enable);
    void setSubpixel(bool enable);
    void setExtendedDisparity(bool enable);
    void setRectifyEdgeFillColor(int color);
    void setRectifyMirrorFrame(bool enable);
    void setOutputRectified(bool enable);
    void setOutputDepth(bool enable);
};

}  // namespace node
}  // namespace dai
<|endoftext|>


<commit_before>/*

Copyright (c) 2006, Arvid Norberg
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 name of the author 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.

*/

#ifndef ROUTING_TABLE_HPP
#define ROUTING_TABLE_HPP

#include <vector>
#include <deque>
#include <boost/cstdint.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_categories.hpp>
#include <boost/utility.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/array.hpp>

#include <libtorrent/kademlia/logging.hpp>

#include <libtorrent/kademlia/node_id.hpp>
#include <libtorrent/kademlia/node_entry.hpp>
#include <libtorrent/session_settings.hpp>

namespace pt = boost::posix_time;

namespace libtorrent { namespace dht
{

using asio::ip::udp;

//TORRENT_DECLARE_LOG(table);

typedef std::deque<node_entry> bucket_t;

// differences in the implementation from the description in
// the paper:
//
// * The routing table tree is not allocated dynamically, there
// 	are always 160 buckets.
// * Nodes are not marked as being stale, they keep a counter
// 	that tells how many times in a row they have failed. When
// 	a new node is to be inserted, the node that has failed
// 	the most times is replaced. If none of the nodes in the
// 	bucket has failed, then it is put in the replacement
// 	cache (just like in the paper).

class routing_table;

namespace aux
{

	// Iterates over a flattened routing_table structure.
	class routing_table_iterator
	: public boost::iterator_facade<
		routing_table_iterator
		, node_entry const
		, boost::forward_traversal_tag
		>
	{
	public:
		routing_table_iterator()
		{
		}

	private:
		friend class libtorrent::dht::routing_table;
		friend class boost::iterator_core_access;

		typedef boost::array<std::pair<bucket_t, bucket_t>, 160>::const_iterator
			bucket_iterator_t;

		routing_table_iterator(
			bucket_iterator_t begin
			, bucket_iterator_t end)
			: m_bucket_iterator(begin)
			, m_bucket_end(end)
			, m_iterator(begin != end ? begin->first.begin() : bucket_t::iterator())
		{
			if (m_bucket_iterator == m_bucket_end) return;
			while (m_iterator == m_bucket_iterator->first.end())
			{
				if (++m_bucket_iterator == m_bucket_end)
					break;
				m_iterator = m_bucket_iterator->first.begin();
			}
		}

		bool equal(routing_table_iterator const& other) const
		{
			return m_bucket_iterator == other.m_bucket_iterator
				&& (m_bucket_iterator == m_bucket_end
					|| m_iterator == other.m_iterator);
		}

		void increment()
		{
			assert(m_bucket_iterator != m_bucket_end);
			++m_iterator;
			while (m_iterator == m_bucket_iterator->first.end())
			{
				if (++m_bucket_iterator == m_bucket_end)
					break;
				m_iterator = m_bucket_iterator->first.begin();
			}
		}

		node_entry const& dereference() const
		{
			assert(m_bucket_iterator != m_bucket_end);
			return *m_iterator;
		}

		bucket_iterator_t m_bucket_iterator;
		bucket_iterator_t m_bucket_end;
		bucket_t::const_iterator m_iterator;
	};

} // namespace aux

class routing_table
{
public:
	typedef aux::routing_table_iterator iterator;
	typedef iterator const_iterator;

	routing_table(node_id const& id, int bucket_size
		, dht_settings const& settings);

	void node_failed(node_id const& id);

	// adds an endpoint that will never be added to
	// the routing table
	void add_router_node(udp::endpoint router);

	// iterates over the router nodes added
	typedef std::set<udp::endpoint>::const_iterator router_iterator;
	router_iterator router_begin() const { return m_router_nodes.begin(); }
	router_iterator router_end() const { return m_router_nodes.end(); }

	// this function is called every time the node sees
	// a sign of a node being alive. This node will either
	// be inserted in the k-buckets or be moved to the top
	// of its bucket.
	bool node_seen(node_id const& id, udp::endpoint addr);

	// returns time when the given bucket needs another refresh.
	// if the given bucket is empty but there are nodes
	// in a bucket closer to us, or if the bucket is non-empty and
	// the time from the last activity is more than 15 minutes
	boost::posix_time::ptime next_refresh(int bucket);

	// fills the vector with the count nodes from our buckets that
	// are nearest to the given id.
	void find_node(node_id const& id, std::vector<node_entry>& l
		, bool include_self, int count = 0);

	// returns true if the given node would be placed in a bucket
	// that is not full. If the node already exists in the table
	// this function returns false
	bool need_node(node_id const& id);

	// this will set the given bucket's latest activity
	// to the current time
	void touch_bucket(int bucket);

	int bucket_size(int bucket)
	{
		assert(bucket >= 0 && bucket < 160);
		return (int)m_buckets[bucket].first.size();
	}
	int bucket_size() const { return m_bucket_size; }

	iterator begin() const;
	iterator end() const;

	boost::tuple<int, int> size() const;

	// returns true if there are no working nodes
	// in the routing table
	bool need_bootstrap() const;

	void replacement_cache(bucket_t& nodes) const;

	// used for debug and monitoring purposes. This will print out
	// the state of the routing table to the given stream
	void print_state(std::ostream& os) const;

private:

	// constant called k in paper
	int m_bucket_size;

	dht_settings const& m_settings;

	// 160 (k-bucket, replacement cache) pairs
	typedef boost::array<std::pair<bucket_t, bucket_t>, 160> table_t;
	table_t m_buckets;
	// timestamps of the last activity in each bucket
	typedef boost::array<boost::posix_time::ptime, 160> table_activity_t;
	table_activity_t m_bucket_activity;
	node_id m_id; // our own node id

	// this is a set of all the endpoints that have
	// been identified as router nodes. They will
	// be used in searches, but they will never
	// be added to the routing table.
	std::set<udp::endpoint> m_router_nodes;

	// this is the lowest bucket index with nodes in it
	int m_lowest_active_bucket;
};

} } // namespace libtorrent::dht

#endif // ROUTING_TABLE_HPP

<commit_msg>added missing include of set.hpp<commit_after>/*

Copyright (c) 2006, Arvid Norberg
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 name of the author 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.

*/

#ifndef ROUTING_TABLE_HPP
#define ROUTING_TABLE_HPP

#include <vector>
#include <deque>
#include <boost/cstdint.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <boost/iterator/iterator_facade.hpp>
#include <boost/iterator/iterator_categories.hpp>
#include <boost/utility.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/array.hpp>
#include <set.hpp>

#include <libtorrent/kademlia/logging.hpp>

#include <libtorrent/kademlia/node_id.hpp>
#include <libtorrent/kademlia/node_entry.hpp>
#include <libtorrent/session_settings.hpp>

namespace pt = boost::posix_time;

namespace libtorrent { namespace dht
{

using asio::ip::udp;

//TORRENT_DECLARE_LOG(table);

typedef std::deque<node_entry> bucket_t;

// differences in the implementation from the description in
// the paper:
//
// * The routing table tree is not allocated dynamically, there
// 	are always 160 buckets.
// * Nodes are not marked as being stale, they keep a counter
// 	that tells how many times in a row they have failed. When
// 	a new node is to be inserted, the node that has failed
// 	the most times is replaced. If none of the nodes in the
// 	bucket has failed, then it is put in the replacement
// 	cache (just like in the paper).

class routing_table;

namespace aux
{

	// Iterates over a flattened routing_table structure.
	class routing_table_iterator
	: public boost::iterator_facade<
		routing_table_iterator
		, node_entry const
		, boost::forward_traversal_tag
		>
	{
	public:
		routing_table_iterator()
		{
		}

	private:
		friend class libtorrent::dht::routing_table;
		friend class boost::iterator_core_access;

		typedef boost::array<std::pair<bucket_t, bucket_t>, 160>::const_iterator
			bucket_iterator_t;

		routing_table_iterator(
			bucket_iterator_t begin
			, bucket_iterator_t end)
			: m_bucket_iterator(begin)
			, m_bucket_end(end)
			, m_iterator(begin != end ? begin->first.begin() : bucket_t::iterator())
		{
			if (m_bucket_iterator == m_bucket_end) return;
			while (m_iterator == m_bucket_iterator->first.end())
			{
				if (++m_bucket_iterator == m_bucket_end)
					break;
				m_iterator = m_bucket_iterator->first.begin();
			}
		}

		bool equal(routing_table_iterator const& other) const
		{
			return m_bucket_iterator == other.m_bucket_iterator
				&& (m_bucket_iterator == m_bucket_end
					|| m_iterator == other.m_iterator);
		}

		void increment()
		{
			assert(m_bucket_iterator != m_bucket_end);
			++m_iterator;
			while (m_iterator == m_bucket_iterator->first.end())
			{
				if (++m_bucket_iterator == m_bucket_end)
					break;
				m_iterator = m_bucket_iterator->first.begin();
			}
		}

		node_entry const& dereference() const
		{
			assert(m_bucket_iterator != m_bucket_end);
			return *m_iterator;
		}

		bucket_iterator_t m_bucket_iterator;
		bucket_iterator_t m_bucket_end;
		bucket_t::const_iterator m_iterator;
	};

} // namespace aux

class routing_table
{
public:
	typedef aux::routing_table_iterator iterator;
	typedef iterator const_iterator;

	routing_table(node_id const& id, int bucket_size
		, dht_settings const& settings);

	void node_failed(node_id const& id);

	// adds an endpoint that will never be added to
	// the routing table
	void add_router_node(udp::endpoint router);

	// iterates over the router nodes added
	typedef std::set<udp::endpoint>::const_iterator router_iterator;
	router_iterator router_begin() const { return m_router_nodes.begin(); }
	router_iterator router_end() const { return m_router_nodes.end(); }

	// this function is called every time the node sees
	// a sign of a node being alive. This node will either
	// be inserted in the k-buckets or be moved to the top
	// of its bucket.
	bool node_seen(node_id const& id, udp::endpoint addr);

	// returns time when the given bucket needs another refresh.
	// if the given bucket is empty but there are nodes
	// in a bucket closer to us, or if the bucket is non-empty and
	// the time from the last activity is more than 15 minutes
	boost::posix_time::ptime next_refresh(int bucket);

	// fills the vector with the count nodes from our buckets that
	// are nearest to the given id.
	void find_node(node_id const& id, std::vector<node_entry>& l
		, bool include_self, int count = 0);

	// returns true if the given node would be placed in a bucket
	// that is not full. If the node already exists in the table
	// this function returns false
	bool need_node(node_id const& id);

	// this will set the given bucket's latest activity
	// to the current time
	void touch_bucket(int bucket);

	int bucket_size(int bucket)
	{
		assert(bucket >= 0 && bucket < 160);
		return (int)m_buckets[bucket].first.size();
	}
	int bucket_size() const { return m_bucket_size; }

	iterator begin() const;
	iterator end() const;

	boost::tuple<int, int> size() const;

	// returns true if there are no working nodes
	// in the routing table
	bool need_bootstrap() const;

	void replacement_cache(bucket_t& nodes) const;

	// used for debug and monitoring purposes. This will print out
	// the state of the routing table to the given stream
	void print_state(std::ostream& os) const;

private:

	// constant called k in paper
	int m_bucket_size;

	dht_settings const& m_settings;

	// 160 (k-bucket, replacement cache) pairs
	typedef boost::array<std::pair<bucket_t, bucket_t>, 160> table_t;
	table_t m_buckets;
	// timestamps of the last activity in each bucket
	typedef boost::array<boost::posix_time::ptime, 160> table_activity_t;
	table_activity_t m_bucket_activity;
	node_id m_id; // our own node id

	// this is a set of all the endpoints that have
	// been identified as router nodes. They will
	// be used in searches, but they will never
	// be added to the routing table.
	std::set<udp::endpoint> m_router_nodes;

	// this is the lowest bucket index with nodes in it
	int m_lowest_active_bucket;
};

} } // namespace libtorrent::dht

#endif // ROUTING_TABLE_HPP

<|endoftext|>


<commit_before>#include "dynet/cfsm-builder.h"

#include <fstream>
#include <iostream>

#include <boost/serialization/vector.hpp>

#include "dynet/io-macros.h"

using namespace std;

namespace dynet {

using namespace expr;

inline bool is_ws(char x) { return (x == ' ' || x == '\t'); }
inline bool not_ws(char x) { return (x != ' ' && x != '\t'); }

SoftmaxBuilder::~SoftmaxBuilder() {}

StandardSoftmaxBuilder::StandardSoftmaxBuilder() {}

StandardSoftmaxBuilder::StandardSoftmaxBuilder(unsigned rep_dim, unsigned vocab_size, Model* model) {
  p_w = model->add_parameters({vocab_size, rep_dim});
  p_b = model->add_parameters({vocab_size});
}

void StandardSoftmaxBuilder::new_graph(ComputationGraph& cg) {
  pcg = &cg;
  w = parameter(cg, p_w);
  b = parameter(cg, p_b);
}

Expression StandardSoftmaxBuilder::neg_log_softmax(const Expression& rep, unsigned wordidx) {
  return pickneglogsoftmax(affine_transform({b, w, rep}), wordidx);
}

unsigned StandardSoftmaxBuilder::sample(const Expression& rep) {
  Expression dist_expr = softmax(affine_transform({b, w, rep}));
  vector<float> dist = as_vector(pcg->incremental_forward(dist_expr));
  unsigned c = 0;
  double p = rand01();
  for (; c < dist.size(); ++c) {
    p -= dist[c];
    if (p < 0.0) { break; }
  }
  if (c == dist.size()) {
    --c;
  }
  return c;
}

Expression StandardSoftmaxBuilder::full_log_distribution(const Expression& rep) {
  return log(softmax(affine_transform({b, w, rep})));
}

template<class Archive>
void StandardSoftmaxBuilder::serialize(Archive& ar, const unsigned int) {
  ar & boost::serialization::base_object<SoftmaxBuilder>(*this);
  ar & p_w;
  ar & p_b;
}
DYNET_SERIALIZE_IMPL(StandardSoftmaxBuilder)

ClassFactoredSoftmaxBuilder::ClassFactoredSoftmaxBuilder() {}

ClassFactoredSoftmaxBuilder::ClassFactoredSoftmaxBuilder(unsigned rep_dim,
                             const std::string& cluster_file,
                             Dict* word_dict,
                             Model* model) {
  read_cluster_file(cluster_file, word_dict);
  const unsigned num_clusters = cdict.size();
  p_r2c = model->add_parameters({num_clusters, rep_dim});
  p_cbias = model->add_parameters({num_clusters});
  p_rc2ws.resize(num_clusters);
  p_rcwbiases.resize(num_clusters);
  for (unsigned i = 0; i < num_clusters; ++i) {
    auto& words = cidx2words[i];  // vector of word ids
    const unsigned num_words_in_cluster = words.size();
    if (num_words_in_cluster > 1) {
      // for singleton clusters, we don't need these parameters, so
      // we don't create them
      p_rc2ws[i] = model->add_parameters({num_words_in_cluster, rep_dim});
      p_rcwbiases[i] = model->add_parameters({num_words_in_cluster});
    }
  }
}

void ClassFactoredSoftmaxBuilder::new_graph(ComputationGraph& cg) {
  pcg = &cg;
  const unsigned num_clusters = cdict.size();
  r2c = parameter(cg, p_r2c);
  cbias = parameter(cg, p_cbias);
  rc2ws.clear();
  rc2biases.clear();
  rc2ws.resize(num_clusters);
  rc2biases.resize(num_clusters);
}

Expression ClassFactoredSoftmaxBuilder::neg_log_softmax(const Expression& rep, unsigned wordidx) {
  // TODO assert that new_graph has been called
  int clusteridx = widx2cidx[wordidx];
  assert(clusteridx >= 0);  // if this fails, wordid is missing from clusters
  Expression cscores = affine_transform({cbias, r2c, rep});
  Expression cnlp = pickneglogsoftmax(cscores, clusteridx);
  if (singleton_cluster[clusteridx]) return cnlp;
  // if there is only one word in the cluster, just return -log p(class | rep)
  // otherwise predict word too
  unsigned wordrow = widx2cwidx[wordidx];
  Expression& cwbias = get_rc2wbias(clusteridx);
  Expression& r2cw = get_rc2w(clusteridx);
  Expression wscores = affine_transform({cwbias, r2cw, rep});
  Expression wnlp = pickneglogsoftmax(wscores, wordrow);
  return cnlp + wnlp;
}

unsigned ClassFactoredSoftmaxBuilder::sample(const Expression& rep) {
  // TODO assert that new_graph has been called
  Expression cscores = affine_transform({cbias, r2c, rep});
  Expression cdist_expr = softmax(cscores);
  auto cdist = as_vector(pcg->incremental_forward(cdist_expr));
  unsigned c = 0;
  double p = rand01();
  for (; c < cdist.size(); ++c) {
    p -= cdist[c];
    if (p < 0.0) { break; }
  }
  if (c == cdist.size()) --c;
  unsigned w = 0;
  if (!singleton_cluster[c]) {
    Expression& cwbias = get_rc2wbias(c);
    Expression& r2cw = get_rc2w(c);
    Expression wscores = affine_transform({cwbias, r2cw, rep});
    Expression wdist_expr = softmax(wscores);
    auto wdist = as_vector(pcg->incremental_forward(wdist_expr));
    p = rand01();
    for (; w < wdist.size(); ++w) {
      p -= wdist[w];
      if (p < 0.0) { break; }
    }
    if (w == wdist.size()) --w;
  }
  return cidx2words[c][w];
}

Expression ClassFactoredSoftmaxBuilder::full_log_distribution(const Expression& rep) {
  vector<Expression> full_dist(widx2cidx.size());
  Expression cscores = log(softmax(affine_transform({cbias, r2c, rep})));

  for (unsigned i = 0; i < widx2cidx.size(); ++i) {
    if (widx2cidx[i] == -1) {
      // XXX: Should be -inf
      full_dist[i] = input(*pcg, -10000);
    }
  }

  for (unsigned c = 0; c < p_rc2ws.size(); ++c) {
    Expression cscore = pick(cscores, c);
    if (singleton_cluster[c]) {
      for (unsigned i = 0; i < cidx2words[c].size(); ++i) {
        unsigned w = cidx2words[c][i];
        full_dist[w] = cscore;
      }
    }
    else {
      Expression& cwbias = get_rc2wbias(c);
      Expression& r2cw = get_rc2w(c);
      Expression wscores = affine_transform({cwbias, r2cw, rep});
      Expression wdist = softmax(wscores);

      for (unsigned i = 0; i < cidx2words[c].size(); ++i) {
        unsigned w = cidx2words[c][i];
        full_dist[w] = pick(wdist, i) + cscore;
      }
    }
  }

  return log(softmax(concatenate(full_dist)));
}

void ClassFactoredSoftmaxBuilder::read_cluster_file(const std::string& cluster_file, Dict* word_dict) {
  cerr << "Reading clusters from " << cluster_file << " ...\n";
  ifstream in(cluster_file);
  assert(in);
  int wc = 0;
  string line;
  while(getline(in, line)) {
    ++wc;
    const unsigned len = line.size();
    unsigned startc = 0;
    while (is_ws(line[startc]) && startc < len) { ++startc; }
    unsigned endc = startc;
    while (not_ws(line[endc]) && endc < len) { ++endc; }
    unsigned startw = endc;
    while (is_ws(line[startw]) && startw < len) { ++startw; }
    unsigned endw = startw;
    while (not_ws(line[endw]) && endw < len) { ++endw; }
    assert(endc > startc);
    assert(startw > endc);
    assert(endw > startw);
    unsigned c = cdict.convert(line.substr(startc, endc - startc));
    unsigned word = word_dict->convert(line.substr(startw, endw - startw));
    if (word >= widx2cidx.size()) {
      widx2cidx.resize(word + 1, -1);
      widx2cwidx.resize(word + 1);
    }
    widx2cidx[word] = c;
    if (c >= cidx2words.size()) cidx2words.resize(c + 1);
    auto& clusterwords = cidx2words[c];
    widx2cwidx[word] = clusterwords.size();
    clusterwords.push_back(word);
  }
  singleton_cluster.resize(cidx2words.size());
  int scs = 0;
  for (unsigned i = 0; i < cidx2words.size(); ++i) {
    bool sc = cidx2words[i].size() <= 1;
    if (sc) scs++;
    singleton_cluster[i] = sc;
  }
  cerr << "Read " << wc << " words in " << cdict.size() << " clusters (" << scs << " singleton clusters)\n";
}

template<class Archive>
void ClassFactoredSoftmaxBuilder::serialize(Archive& ar, const unsigned int) {
  ar & boost::serialization::base_object<SoftmaxBuilder>(*this);
  ar & cdict;
  ar & widx2cidx;
  ar & widx2cwidx;
  ar & cidx2words;
  ar & singleton_cluster;
  ar & p_r2c;
  ar & p_cbias;
  ar & p_rc2ws;
  ar & p_rcwbiases;
}

void ClassFactoredSoftmaxBuilder::initialize_expressions() {
  for (unsigned c = 0; c < p_rc2ws.size(); ++c) {
    Expression temp = get_rc2w(c);
    Expression temp2 = get_rc2wbias(c);
  }
}

DYNET_SERIALIZE_IMPL(ClassFactoredSoftmaxBuilder)

} // namespace dynet

BOOST_CLASS_EXPORT_IMPLEMENT(dynet::StandardSoftmaxBuilder)
BOOST_CLASS_EXPORT_IMPLEMENT(dynet::ClassFactoredSoftmaxBuilder)
<commit_msg>tweaked initialize_expressions to match grahams recommendations<commit_after>#include "dynet/cfsm-builder.h"

#include <fstream>
#include <iostream>

#include <boost/serialization/vector.hpp>

#include "dynet/io-macros.h"

using namespace std;

namespace dynet {

using namespace expr;

inline bool is_ws(char x) { return (x == ' ' || x == '\t'); }
inline bool not_ws(char x) { return (x != ' ' && x != '\t'); }

SoftmaxBuilder::~SoftmaxBuilder() {}

StandardSoftmaxBuilder::StandardSoftmaxBuilder() {}

StandardSoftmaxBuilder::StandardSoftmaxBuilder(unsigned rep_dim, unsigned vocab_size, Model* model) {
  p_w = model->add_parameters({vocab_size, rep_dim});
  p_b = model->add_parameters({vocab_size});
}

void StandardSoftmaxBuilder::new_graph(ComputationGraph& cg) {
  pcg = &cg;
  w = parameter(cg, p_w);
  b = parameter(cg, p_b);
}

Expression StandardSoftmaxBuilder::neg_log_softmax(const Expression& rep, unsigned wordidx) {
  return pickneglogsoftmax(affine_transform({b, w, rep}), wordidx);
}

unsigned StandardSoftmaxBuilder::sample(const Expression& rep) {
  Expression dist_expr = softmax(affine_transform({b, w, rep}));
  vector<float> dist = as_vector(pcg->incremental_forward(dist_expr));
  unsigned c = 0;
  double p = rand01();
  for (; c < dist.size(); ++c) {
    p -= dist[c];
    if (p < 0.0) { break; }
  }
  if (c == dist.size()) {
    --c;
  }
  return c;
}

Expression StandardSoftmaxBuilder::full_log_distribution(const Expression& rep) {
  return log(softmax(affine_transform({b, w, rep})));
}

template<class Archive>
void StandardSoftmaxBuilder::serialize(Archive& ar, const unsigned int) {
  ar & boost::serialization::base_object<SoftmaxBuilder>(*this);
  ar & p_w;
  ar & p_b;
}
DYNET_SERIALIZE_IMPL(StandardSoftmaxBuilder)

ClassFactoredSoftmaxBuilder::ClassFactoredSoftmaxBuilder() {}

ClassFactoredSoftmaxBuilder::ClassFactoredSoftmaxBuilder(unsigned rep_dim,
                             const std::string& cluster_file,
                             Dict* word_dict,
                             Model* model) {
  read_cluster_file(cluster_file, word_dict);
  const unsigned num_clusters = cdict.size();
  p_r2c = model->add_parameters({num_clusters, rep_dim});
  p_cbias = model->add_parameters({num_clusters});
  p_rc2ws.resize(num_clusters);
  p_rcwbiases.resize(num_clusters);
  for (unsigned i = 0; i < num_clusters; ++i) {
    auto& words = cidx2words[i];  // vector of word ids
    const unsigned num_words_in_cluster = words.size();
    if (num_words_in_cluster > 1) {
      // for singleton clusters, we don't need these parameters, so
      // we don't create them
      p_rc2ws[i] = model->add_parameters({num_words_in_cluster, rep_dim});
      p_rcwbiases[i] = model->add_parameters({num_words_in_cluster});
    }
  }
}

void ClassFactoredSoftmaxBuilder::new_graph(ComputationGraph& cg) {
  pcg = &cg;
  const unsigned num_clusters = cdict.size();
  r2c = parameter(cg, p_r2c);
  cbias = parameter(cg, p_cbias);
  rc2ws.clear();
  rc2biases.clear();
  rc2ws.resize(num_clusters);
  rc2biases.resize(num_clusters);
}

Expression ClassFactoredSoftmaxBuilder::neg_log_softmax(const Expression& rep, unsigned wordidx) {
  // TODO assert that new_graph has been called
  int clusteridx = widx2cidx[wordidx];
  assert(clusteridx >= 0);  // if this fails, wordid is missing from clusters
  Expression cscores = affine_transform({cbias, r2c, rep});
  Expression cnlp = pickneglogsoftmax(cscores, clusteridx);
  if (singleton_cluster[clusteridx]) return cnlp;
  // if there is only one word in the cluster, just return -log p(class | rep)
  // otherwise predict word too
  unsigned wordrow = widx2cwidx[wordidx];
  Expression& cwbias = get_rc2wbias(clusteridx);
  Expression& r2cw = get_rc2w(clusteridx);
  Expression wscores = affine_transform({cwbias, r2cw, rep});
  Expression wnlp = pickneglogsoftmax(wscores, wordrow);
  return cnlp + wnlp;
}

unsigned ClassFactoredSoftmaxBuilder::sample(const Expression& rep) {
  // TODO assert that new_graph has been called
  Expression cscores = affine_transform({cbias, r2c, rep});
  Expression cdist_expr = softmax(cscores);
  auto cdist = as_vector(pcg->incremental_forward(cdist_expr));
  unsigned c = 0;
  double p = rand01();
  for (; c < cdist.size(); ++c) {
    p -= cdist[c];
    if (p < 0.0) { break; }
  }
  if (c == cdist.size()) --c;
  unsigned w = 0;
  if (!singleton_cluster[c]) {
    Expression& cwbias = get_rc2wbias(c);
    Expression& r2cw = get_rc2w(c);
    Expression wscores = affine_transform({cwbias, r2cw, rep});
    Expression wdist_expr = softmax(wscores);
    auto wdist = as_vector(pcg->incremental_forward(wdist_expr));
    p = rand01();
    for (; w < wdist.size(); ++w) {
      p -= wdist[w];
      if (p < 0.0) { break; }
    }
    if (w == wdist.size()) --w;
  }
  return cidx2words[c][w];
}

Expression ClassFactoredSoftmaxBuilder::full_log_distribution(const Expression& rep) {
  vector<Expression> full_dist(widx2cidx.size());
  Expression cscores = log(softmax(affine_transform({cbias, r2c, rep})));

  for (unsigned i = 0; i < widx2cidx.size(); ++i) {
    if (widx2cidx[i] == -1) {
      // XXX: Should be -inf
      full_dist[i] = input(*pcg, -10000);
    }
  }

  for (unsigned c = 0; c < p_rc2ws.size(); ++c) {
    Expression cscore = pick(cscores, c);
    if (singleton_cluster[c]) {
      for (unsigned i = 0; i < cidx2words[c].size(); ++i) {
        unsigned w = cidx2words[c][i];
        full_dist[w] = cscore;
      }
    }
    else {
      Expression& cwbias = get_rc2wbias(c);
      Expression& r2cw = get_rc2w(c);
      Expression wscores = affine_transform({cwbias, r2cw, rep});
      Expression wdist = softmax(wscores);

      for (unsigned i = 0; i < cidx2words[c].size(); ++i) {
        unsigned w = cidx2words[c][i];
        full_dist[w] = pick(wdist, i) + cscore;
      }
    }
  }

  return log(softmax(concatenate(full_dist)));
}

void ClassFactoredSoftmaxBuilder::read_cluster_file(const std::string& cluster_file, Dict* word_dict) {
  cerr << "Reading clusters from " << cluster_file << " ...\n";
  ifstream in(cluster_file);
  assert(in);
  int wc = 0;
  string line;
  while(getline(in, line)) {
    ++wc;
    const unsigned len = line.size();
    unsigned startc = 0;
    while (is_ws(line[startc]) && startc < len) { ++startc; }
    unsigned endc = startc;
    while (not_ws(line[endc]) && endc < len) { ++endc; }
    unsigned startw = endc;
    while (is_ws(line[startw]) && startw < len) { ++startw; }
    unsigned endw = startw;
    while (not_ws(line[endw]) && endw < len) { ++endw; }
    assert(endc > startc);
    assert(startw > endc);
    assert(endw > startw);
    unsigned c = cdict.convert(line.substr(startc, endc - startc));
    unsigned word = word_dict->convert(line.substr(startw, endw - startw));
    if (word >= widx2cidx.size()) {
      widx2cidx.resize(word + 1, -1);
      widx2cwidx.resize(word + 1);
    }
    widx2cidx[word] = c;
    if (c >= cidx2words.size()) cidx2words.resize(c + 1);
    auto& clusterwords = cidx2words[c];
    widx2cwidx[word] = clusterwords.size();
    clusterwords.push_back(word);
  }
  singleton_cluster.resize(cidx2words.size());
  int scs = 0;
  for (unsigned i = 0; i < cidx2words.size(); ++i) {
    bool sc = cidx2words[i].size() <= 1;
    if (sc) scs++;
    singleton_cluster[i] = sc;
  }
  cerr << "Read " << wc << " words in " << cdict.size() << " clusters (" << scs << " singleton clusters)\n";
}

template<class Archive>
void ClassFactoredSoftmaxBuilder::serialize(Archive& ar, const unsigned int) {
  ar & boost::serialization::base_object<SoftmaxBuilder>(*this);
  ar & cdict;
  ar & widx2cidx;
  ar & widx2cwidx;
  ar & cidx2words;
  ar & singleton_cluster;
  ar & p_r2c;
  ar & p_cbias;
  ar & p_rc2ws;
  ar & p_rcwbiases;
}

void ClassFactoredSoftmaxBuilder::initialize_expressions() {
  for (unsigned c = 0; c < p_rc2ws.size(); ++c) {
    //get_rc2w(_bias) creates the expression at c if the expression does not already exist.
    get_rc2w(c);
    get_rc2wbias(c);
  }
}

DYNET_SERIALIZE_IMPL(ClassFactoredSoftmaxBuilder)

} // namespace dynet

BOOST_CLASS_EXPORT_IMPLEMENT(dynet::StandardSoftmaxBuilder)
BOOST_CLASS_EXPORT_IMPLEMENT(dynet::ClassFactoredSoftmaxBuilder)
<|endoftext|>


<commit_before>/*
* Unix EntropySource
* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/internal/es_unix.h>
#include <botan/internal/unix_cmd.h>
#include <botan/parsing.h>
#include <algorithm>

#include <sys/time.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>

namespace Botan {

namespace {

/**
* Sort ordering by priority
*/
bool Unix_Program_Cmp(const Unix_Program& a, const Unix_Program& b)
   {
   if(a.priority < b.priority)
      return true;

   return (a.name_and_args < b.name_and_args);
   }

}

/**
* Unix_EntropySource Constructor
*/
Unix_EntropySource::Unix_EntropySource(const std::vector<std::string>& path) :
   PATH(path)
   {
   std::vector<Unix_Program> default_sources = get_default_sources();
   add_sources(&default_sources[0], default_sources.size());
   }

/**
* Add sources to the list
*/
void Unix_EntropySource::add_sources(const Unix_Program srcs[], u32bit count)
   {
   sources.insert(sources.end(), srcs, srcs + count);
   std::sort(sources.begin(), sources.end(), Unix_Program_Cmp);
   }

/**
* Poll for entropy on a generic Unix system, first by grabbing various
* statistics (stat on common files, getrusage, etc), and then, if more
* is required, by exec'ing various programs like uname and rpcinfo and
* reading the output.
*/
void Unix_EntropySource::poll(Entropy_Accumulator& accum)
   {
   const char* stat_targets[] = {
      "/",
      "/tmp",
      "/var/tmp",
      "/usr",
      "/home",
      "/etc/passwd",
      ".",
      "..",
      0 };

   for(u32bit j = 0; stat_targets[j]; j++)
      {
      struct stat statbuf;
      clear_mem(&statbuf, 1);
      ::stat(stat_targets[j], &statbuf);
      accum.add(&statbuf, sizeof(statbuf), .005);
      }

   accum.add(::getpid(),  0);
   accum.add(::getppid(), 0);
   accum.add(::getuid(),  0);
   accum.add(::getgid(), 0);
   accum.add(::getpgrp(), 0);

   struct ::rusage usage;
   ::getrusage(RUSAGE_SELF, &usage);
   accum.add(usage, .005);

   ::getrusage(RUSAGE_CHILDREN, &usage);
   accum.add(usage, .005);

   const u32bit MINIMAL_WORKING = 16;

   MemoryRegion<byte>& io_buffer = accum.get_io_buffer(DEFAULT_BUFFERSIZE);

   for(u32bit j = 0; j != sources.size(); j++)
      {
      DataSource_Command pipe(sources[j].name_and_args, PATH);

      u32bit got_from_src = 0;

      while(!pipe.end_of_data())
         {
         u32bit got_this_loop = pipe.read(io_buffer, io_buffer.size());
         got_from_src += got_this_loop;

         accum.add(io_buffer.begin(), got_this_loop, .005);
         }

      sources[j].working = (got_from_src >= MINIMAL_WORKING) ? true : false;

      if(accum.polling_goal_achieved())
         break;
      }
   }

}
<commit_msg>Fix comparison functor<commit_after>/*
* Unix EntropySource
* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/internal/es_unix.h>
#include <botan/internal/unix_cmd.h>
#include <botan/parsing.h>
#include <algorithm>

#include <sys/time.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <unistd.h>

namespace Botan {

namespace {

/**
* Sort ordering by priority
*/
bool Unix_Program_Cmp(const Unix_Program& a, const Unix_Program& b)
   {
   if(a.priority == b.priority)
      return (a.name_and_args < b.name_and_args);

   return (a.priority < b.priority);
   }

}

/**
* Unix_EntropySource Constructor
*/
Unix_EntropySource::Unix_EntropySource(const std::vector<std::string>& path) :
   PATH(path)
   {
   std::vector<Unix_Program> default_sources = get_default_sources();
   add_sources(&default_sources[0], default_sources.size());
   }

/**
* Add sources to the list
*/
void Unix_EntropySource::add_sources(const Unix_Program srcs[], u32bit count)
   {
   sources.insert(sources.end(), srcs, srcs + count);
   std::sort(sources.begin(), sources.end(), Unix_Program_Cmp);
   }

/**
* Poll for entropy on a generic Unix system, first by grabbing various
* statistics (stat on common files, getrusage, etc), and then, if more
* is required, by exec'ing various programs like uname and rpcinfo and
* reading the output.
*/
void Unix_EntropySource::poll(Entropy_Accumulator& accum)
   {
   const char* stat_targets[] = {
      "/",
      "/tmp",
      "/var/tmp",
      "/usr",
      "/home",
      "/etc/passwd",
      ".",
      "..",
      0 };

   for(u32bit j = 0; stat_targets[j]; j++)
      {
      struct stat statbuf;
      clear_mem(&statbuf, 1);
      ::stat(stat_targets[j], &statbuf);
      accum.add(&statbuf, sizeof(statbuf), .005);
      }

   accum.add(::getpid(),  0);
   accum.add(::getppid(), 0);
   accum.add(::getuid(),  0);
   accum.add(::getgid(), 0);
   accum.add(::getpgrp(), 0);

   struct ::rusage usage;
   ::getrusage(RUSAGE_SELF, &usage);
   accum.add(usage, .005);

   ::getrusage(RUSAGE_CHILDREN, &usage);
   accum.add(usage, .005);

   const u32bit MINIMAL_WORKING = 16;

   MemoryRegion<byte>& io_buffer = accum.get_io_buffer(DEFAULT_BUFFERSIZE);

   for(u32bit j = 0; j != sources.size(); j++)
      {
      DataSource_Command pipe(sources[j].name_and_args, PATH);

      u32bit got_from_src = 0;

      while(!pipe.end_of_data())
         {
         u32bit got_this_loop = pipe.read(io_buffer, io_buffer.size());
         got_from_src += got_this_loop;

         accum.add(io_buffer.begin(), got_this_loop, .005);
         }

      sources[j].working = (got_from_src >= MINIMAL_WORKING) ? true : false;

      if(accum.polling_goal_achieved())
         break;
      }
   }

}
<|endoftext|>