Exemple #1
0
void stl_compute(double *x, int n, int period, int s_window, double *trend, double *season){
	int s_degree = 0;
	int t_degree = 1;
	int l_window = nextodd(period);
	int l_degree = t_degree;
	
	int inner =  2;
	int outer = 0;
	int  t_window = nextodd(ceiling( 1.5 * period / (1- 1.5/s_window)));
	int s_jump = ceiling(s_window/10.0);
	int t_jump = ceiling(t_window/10.0);
	int l_jump = ceiling(l_window/10.0);

	double *weights=(double*)malloc((n+1)*sizeof(double));
	
	double *buf=(double*)malloc(  ((n+2*period)*5)* sizeof(double) );
	stl_( x,  &n, &period,
		  &s_window,
	   	&t_window,
		  &l_window,
		  &s_degree, &t_degree, &l_degree,
		  &s_jump,
		  &t_jump,
		  &l_jump,
		  &inner,
		  &outer,
		  weights ,
		  season,
		  trend ,
		  buf);
	
	free(buf);
	free(weights);

}
static statefile_hdr_t* read_statefile_item(ir_uint32 **buffer, ir_uint32 *buffer_len)
{
  statefile_hdr_t *all;
  
  if (*buffer_len < sizeof(statefile_hdr_t))
    {
      return NULL;
    }
  
  all = (statefile_hdr_t*)*buffer;
  
  all->tag = ntohl(all->tag);
  all->length = ntohl(all->length);
  
  if (*buffer_len < all->length)
    {
      outerror(OUTERROR_TYPE_WARN_LOUD, "Bad Header From State File (%u < %u)",
               *buffer_len, all->length);
      return NULL;
    }
  
  *buffer += ceiling(all->length, 4) / sizeof(ir_uint32);
  *buffer_len -= ceiling(all->length, 4);
  
  return all;
}
Exemple #3
0
//calculates total number of workgroups
unsigned int Trace::getTotalWorkgroups()const{

  unsigned int num_wk = ceiling(global_threads[0],local_threads[0]);

  if(dims > 1)
    num_wk *= ceiling(global_threads[1],local_threads[1]);

  if(dims > 2) 
   num_wk *= ceiling(global_threads[2],local_threads[2]);

  return num_wk;

}
Exemple #4
0
Integer EnsureVirtualAddressRange (Integer vma, int count)
{
  int pages = ceiling(count, MemoryPageSize);
  caddr_t data, tag;
  Integer aligned_vma = vma - MemoryPageOffset(vma);
  int n;

  while (pages) {
    n = 0;
    while (!Created (vma) && pages) {
      n++;
      pages--;
      SetCreated(vma);
      vma += MemoryPageSize;
    }
    if (n) {
      data = (caddr_t)&DataSpace[aligned_vma];
      tag = (caddr_t)&TagSpace[aligned_vma];
      if (data != mmap(data, n * sizeof(Integer[MemoryPageSize]), PROT_READ|PROT_WRITE,
		       MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,-1,0))
	punt ("Couldn't map %d data pages at %x for VMA %x", n, data, aligned_vma);
      if (tag != mmap(tag, n * sizeof(Tag[MemoryPageSize]), PROT_READ|PROT_WRITE,
		      MAP_ANONYMOUS|MAP_PRIVATE|MAP_FIXED,-1,0))
	punt ("Couldn't map %d tag pages at %x for VMA %x", n, tag, aligned_vma);
      aligned_vma += n * MemoryPageSize;
    }
    while (Created (vma) && pages) {
      pages--;
      vma += MemoryPageSize;
      aligned_vma += MemoryPageSize;
    }
  }

  return(vma);
}
void reverse (List<Object>& l) 
{
  iterator front = l.begin();
  iterator back = l.end();
  iterator deleter = l.begin();
  iterator ender = l.end();

  //get to the middle
  for (int i = 0; i < (ceiling(mSize/2)); i++) 
  {
    ++front;
    --back;
  }
  
  //push all the new reversed stuff
  while (front != l.begin()) && (back != l.end())
  {
    l.push_back(front.current());
    l.push_front(back.current());
    --front;
    ++back ;
  }

  //delete all the old stuff
  while (deleter != ender) 
  {
    erase(deleter.current);
    ++deleter;
  }
}
Exemple #6
0
Integer DestroyVirtualAddressRange (Integer vma, int count)
{
  int pages = ceiling(count, MemoryPageSize);

  for (; pages--; vma += MemoryPageSize)
    DestroyVirtualAddress(vma);

  return(vma);
}
Exemple #7
0
void computeNumCTAs(KernelPointer kernel, int smemDynamicBytes, bool bManualCoalesce)
{
    cudaDeviceProp devprop;
    int deviceID = -1;
    cudaError_t err = cudaGetDevice(&deviceID);
    assert(err == cudaSuccess);

    cudaGetDeviceProperties(&devprop, deviceID);

    // Determine the maximum number of CTAs that can be run simultaneously for each kernel
    // This is equivalent to the calculation done in the CUDA Occupancy Calculator spreadsheet
    const unsigned int regAllocationUnit = (devprop.major < 2 && devprop.minor < 2) ? 256 : 512; // in registers
    const unsigned int warpAllocationMultiple = 2;
    const unsigned int smemAllocationUnit = 512;                                                 // in bytes
    const unsigned int maxThreadsPerSM = bManualCoalesce ? 768 : 1024; // sm_12 GPUs increase threads/SM to 1024
    const unsigned int maxBlocksPerSM = 8;

    cudaFuncAttributes attr;
    err = cudaFuncGetAttributes(&attr, (const char*)kernel);
    assert(err == cudaSuccess);


    // Number of warps (round up to nearest whole multiple of warp size)
    size_t numWarps = multiple(RadixSort::CTA_SIZE, devprop.warpSize);
    // Round up to warp allocation multiple
    numWarps = ceiling(numWarps, warpAllocationMultiple);

    // Number of regs is regs per thread times number of warps times warp size
    size_t regsPerCTA = attr.numRegs * devprop.warpSize * numWarps;
    // Round up to multiple of register allocation unit size
    regsPerCTA = ceiling(regsPerCTA, regAllocationUnit);

    size_t smemBytes = attr.sharedSizeBytes + smemDynamicBytes;
    size_t smemPerCTA = ceiling(smemBytes, smemAllocationUnit);

    size_t ctaLimitRegs    = regsPerCTA > 0 ? devprop.regsPerBlock / regsPerCTA : maxBlocksPerSM;
    size_t ctaLimitSMem    = smemPerCTA > 0 ? devprop.sharedMemPerBlock      / smemPerCTA : maxBlocksPerSM;
    size_t ctaLimitThreads =                  maxThreadsPerSM                / RadixSort::CTA_SIZE;

    unsigned int numSMs = devprop.multiProcessorCount;
    int maxCTAs = numSMs * std::min<size_t>(ctaLimitRegs, std::min<size_t>(ctaLimitSMem, std::min<size_t>(ctaLimitThreads, maxBlocksPerSM)));
    setNumCTAs(kernel, maxCTAs);
}
Exemple #8
0
int isPrime(int value) {
    int i;
    
    for(i=3;i<=ceiling((float)value/i);i++) {
        if(value%i==0) {
            return 0;
        }
    }
    
    return 1;
}
Exemple #9
0
 int DocumentContainer::text_width ( const litehtml::tchar_t * text, litehtml::uint_ptr hFont )
 {
     FT_Face face = reinterpret_cast<FT_Face> ( hFont );
     size_t textlen = strlen ( text );
     FT_Pos width = 0;
     for ( size_t i = 0; i < textlen; ++i )
     {
         FT_Load_Char ( face, text[i], FT_LOAD_NO_BITMAP );
         width += face->glyph->metrics.horiAdvance;
     }
     return ceiling ( width );
 }
void MinefieldModel::newGame(GameData gameData)
{
    _boardSize = gameData.getBoard();
    _gameBoard = createGameBoard(_boardSize);

    _player = new Coordinate(1, ceiling(_boardSize, 2));
    setPlayer(_player->x(), _player->y());

    int numberOfChasers = gameData.getChasers();
    int numberOfMines = gameData.getMines();

    int lastRow = _boardSize + 1;
    int lastColumn = _boardSize;

    for (int i = 1; i <= ceiling(numberOfChasers, 2); i++) {
        _gameBoard[lastRow - i][1] = Chaser;
        _gameBoard[lastRow - i][lastColumn] = Chaser;
    }

    for (int i = 1; i <= numberOfMines; i++) {
        Coordinate* mine = generateValidRandom(_boardSize);
        _gameBoard[mine->x()][mine->y()] = Mine;
    }
}
Exemple #11
0
unsigned int Trace::warpsPerWorkgroup() const {
 
  unsigned int warps_per_workgroup;
 
  //number of warps in the first dimension of a workgroup
  warps_per_workgroup = local_threads.at(0);

  if(dims > 1){
    warps_per_workgroup *= local_threads.at(1);
  }

  if(dims > 2){
    warps_per_workgroup *= local_threads.at(2);
  }

  warps_per_workgroup = ceiling(warps_per_workgroup,warp_size);

  return warps_per_workgroup;
  }
Exemple #12
0
void PrintFileReadable(char *file, double kb) {
   double mb = kb / BYTE_CONV, gb = mb / BYTE_CONV, tb = gb / BYTE_CONV;
   char size[8];

   if (tb >= 1) {
      if (tb / 10 > 1)
         sprintf(size, "%dT", ceiling(tb));
      else
         sprintf(size, "%.1lfT", ceiling(tb * 10) / 10.0);
      printf(RD_FORMAT, size, file);
      return;
   }
   if (gb >= 1) {
      if (gb / 10 > 1)
         sprintf(size, "%dG", ceiling(gb));
      else
         sprintf(size, "%.1lfG", ceiling(gb * 10) / 10.0);
      printf(RD_FORMAT, size, file);
      return;
   }
   if (mb >= 1) {
      if (mb / 10 > 1)
         sprintf(size, "%dM", ceiling(mb));
      else
         sprintf(size, "%.1lfM", ceiling(mb * 10) / 10.0);
      printf(RD_FORMAT, size, file);
      return;
   }
   if (kb / 10 > 1)
      sprintf(size, "%dK", ceiling(kb));
   else if (!kb)
      sprintf(size, "0");
   else
      sprintf(size, "%.1lfK", ceiling(kb * 10) / 10.0);
   printf(RD_FORMAT, size, file);
}
// -----------------------------------------------------------------------------
// Apply values to opened objects
// -----------------------------------------------------------------------------
void SectorPropsPanel::applyChanges()
{
	for (auto& object : objects_)
	{
		auto sector = dynamic_cast<MapSector*>(object);

		// Special
		if (cb_override_special_->GetValue())
			sector->setIntProperty("special", panel_special_->selectedSpecial());

		// Floor texture
		if (!fcb_floor_->GetValue().IsEmpty())
			sector->setFloorTexture(fcb_floor_->GetValue().ToStdString());

		// Ceiling texture
		if (!fcb_ceiling_->GetValue().IsEmpty())
			sector->setCeilingTexture(fcb_ceiling_->GetValue().ToStdString());

		// Floor height
		if (!text_height_floor_->GetValue().IsEmpty())
			sector->setFloorHeight(text_height_floor_->number(sector->floor().height));

		// Ceiling height
		if (!text_height_ceiling_->GetValue().IsEmpty())
			sector->setCeilingHeight(text_height_ceiling_->number(sector->ceiling().height));

		// Light level
		if (!text_light_->GetValue().IsEmpty())
			sector->setLightLevel(text_light_->number(sector->lightLevel()));

		// Tag
		if (!text_tag_->GetValue().IsEmpty())
			sector->setTag(text_tag_->number(sector->tag()));
	}

	if (mopp_all_props_)
		mopp_all_props_->applyChanges();
}
 int levelCount() const {
     return ceiling(log(m_photons.size() + 1));
 }
Exemple #15
0
void MemInit(void)
{
  zz_lengths[ WORD         ] = 0;
  zz_lengths[ QWORD        ] = 0;
  zz_lengths[ LINK         ] = ceiling(sizeof(struct link_type), sizeof(ALIGN));

  /* object types, except closure NB have actual() field in token phase! */
  zz_lengths[ CLOSURE      ] =
  zz_lengths[ NULL_CLOS    ] =
  zz_lengths[ PAGE_LABEL   ] =
  zz_lengths[ UNDER_REC    ] =
  zz_lengths[ CROSS        ] =
  zz_lengths[ FORCE_CROSS  ] =
  zz_lengths[ SPLIT        ] =
  zz_lengths[ PAR          ] =
  zz_lengths[ ROW_THR      ] =
  zz_lengths[ COL_THR      ] =
  zz_lengths[ HSPANNER     ] =
  zz_lengths[ VSPANNER     ] =
  zz_lengths[ ACAT         ] =
  zz_lengths[ HCAT         ] =
  zz_lengths[ VCAT         ] =
  zz_lengths[ BEGIN_HEADER ] =
  zz_lengths[ END_HEADER   ] =
  zz_lengths[ SET_HEADER   ] =
  zz_lengths[ CLEAR_HEADER ] =
  zz_lengths[ ONE_COL      ] =
  zz_lengths[ ONE_ROW      ] =
  zz_lengths[ WIDE         ] =
  zz_lengths[ HIGH         ] =
  zz_lengths[ HSHIFT       ] =
  zz_lengths[ VSHIFT       ] =
  zz_lengths[ HMIRROR      ] =
  zz_lengths[ VMIRROR      ] =
  zz_lengths[ HSCALE       ] =
  zz_lengths[ VSCALE       ] =
  zz_lengths[ HCOVER       ] =
  zz_lengths[ VCOVER       ] =
  zz_lengths[ SCALE        ] =
  zz_lengths[ KERN_SHRINK  ] =
  zz_lengths[ HCONTRACT    ] =
  zz_lengths[ VCONTRACT    ] =
  zz_lengths[ HLIMITED     ] =
  zz_lengths[ VLIMITED     ] =
  zz_lengths[ HEXPAND      ] =
  zz_lengths[ VEXPAND      ] =
  zz_lengths[ START_HVSPAN ] =
  zz_lengths[ START_HSPAN  ] =
  zz_lengths[ START_VSPAN  ] =
  zz_lengths[ HSPAN        ] =
  zz_lengths[ VSPAN        ] =
  zz_lengths[ PADJUST      ] =
  zz_lengths[ HADJUST      ] =
  zz_lengths[ VADJUST      ] =
  zz_lengths[ ROTATE       ] =
  zz_lengths[ BACKGROUND   ] =
  zz_lengths[ VERBATIM     ] =
  zz_lengths[ RAW_VERBATIM ] =
  zz_lengths[ CASE         ] =
  zz_lengths[ YIELD        ] =
  zz_lengths[ BACKEND      ] =
  zz_lengths[ FILTERED     ] =
  zz_lengths[ XCHAR        ] =
  zz_lengths[ FONT         ] =
  zz_lengths[ SPACE        ] =
  zz_lengths[ YUNIT        ] =
  zz_lengths[ ZUNIT        ] =
  zz_lengths[ BREAK        ] =
  zz_lengths[ UNDERLINE    ] =
  zz_lengths[ UNDERLINE_COLOUR ] =
  zz_lengths[ COLOUR       ] =
  zz_lengths[ TEXTURE      ] =
  zz_lengths[ OUTLINE      ] =
  zz_lengths[ LANGUAGE     ] =
  zz_lengths[ CURR_LANG    ] =
  zz_lengths[ CURR_FAMILY  ] =
  zz_lengths[ CURR_FACE    ] =
  zz_lengths[ CURR_YUNIT   ] =
  zz_lengths[ CURR_ZUNIT   ] =
  zz_lengths[ GET_CONTEXT  ] =
  zz_lengths[ SET_CONTEXT  ] =
  zz_lengths[ COMMON       ] =
  zz_lengths[ RUMP         ] =
  zz_lengths[ MELD         ] =
  zz_lengths[ INSERT       ] =
  zz_lengths[ ONE_OF       ] =
  zz_lengths[ NEXT         ] =
  zz_lengths[ PLUS         ] =
  zz_lengths[ MINUS        ] =
  zz_lengths[ ENV_OBJ      ] =
  zz_lengths[ ENV          ] =
  zz_lengths[ ENVA         ] =
  zz_lengths[ ENVB         ] =
  zz_lengths[ ENVC         ] =
  zz_lengths[ ENVD         ] =
  zz_lengths[ CENV         ] =
  zz_lengths[ CLOS         ] =
  zz_lengths[ LVIS         ] =
  zz_lengths[ LUSE         ] =
  zz_lengths[ LEO          ] =
  zz_lengths[ OPEN         ] =
  zz_lengths[ TAGGED       ] =
  zz_lengths[ INCGRAPHIC   ] =
  zz_lengths[ SINCGRAPHIC  ] =
  zz_lengths[ PLAIN_GRAPHIC] =
  zz_lengths[ GRAPHIC      ] =
  zz_lengths[ LINK_SOURCE  ] =
  zz_lengths[ LINK_DEST    ] =
  zz_lengths[ LINK_DEST_NULL] =
  zz_lengths[ LINK_URL     ] =
	ceiling(sizeof(struct closure_type), sizeof(ALIGN));

  zz_lengths[ HEAD         ] =
	ceiling(sizeof(struct head_type), sizeof(ALIGN));

  zz_lengths[ LBR          ] =
  zz_lengths[ RBR          ] =
  zz_lengths[ BEGIN        ] =
  zz_lengths[ END          ] =
  zz_lengths[ USE          ] =
  zz_lengths[ NOT_REVEALED ] =
  zz_lengths[ GSTUB_NONE   ] =
  zz_lengths[ GSTUB_INT    ] =
  zz_lengths[ GSTUB_EXT    ] =
  zz_lengths[ UNEXPECTED_EOF] =
  zz_lengths[ INCG_REPEATED ] =
  zz_lengths[ SINCG_REPEATED] =
  zz_lengths[ PREPEND      ] =
  zz_lengths[ SYS_PREPEND  ] =
  zz_lengths[ DATABASE     ] =
  zz_lengths[ SYS_DATABASE ] =
  zz_lengths[ DEAD         ] =
  zz_lengths[ UNATTACHED   ] =
  zz_lengths[ RECEPTIVE    ] =
  zz_lengths[ RECEIVING    ] =
  zz_lengths[ RECURSIVE    ] =
  zz_lengths[ PRECEDES     ] =
  zz_lengths[ FOLLOWS      ] =
  zz_lengths[ CROSS_FOLL   ] =
  zz_lengths[ CROSS_FOLL_OR_PREC] =
  zz_lengths[ GALL_FOLL    ] =
  zz_lengths[ GALL_FOLL_OR_PREC ] =
  zz_lengths[ CROSS_TARG   ] =
  zz_lengths[ GALL_TARG    ] =
  zz_lengths[ GALL_PREC    ] =
  zz_lengths[ CROSS_PREC   ] =
  zz_lengths[ PAGE_LABEL_IND] =
  zz_lengths[ SCALE_IND    ] =
  zz_lengths[ COVER_IND    ] =
  zz_lengths[ EXPAND_IND   ] =
  zz_lengths[ THREAD       ] =
  zz_lengths[ CR_LIST      ] =
  zz_lengths[ SCOPE_SNAPSHOT] =
	ceiling(sizeof(struct closure_type), sizeof(ALIGN));

  /* symbol types */
  zz_lengths[ MACRO        ] =
  zz_lengths[ LOCAL        ] =
  zz_lengths[ LPAR         ] =
  zz_lengths[ RPAR         ] =
  zz_lengths[ NPAR         ] =
	ceiling(sizeof(struct symbol_type), sizeof(ALIGN));

  /* gap objects */
  zz_lengths[ TSPACE       ] =
  zz_lengths[ TJUXTA       ] =
  zz_lengths[ GAP_OBJ      ] =
	ceiling(sizeof(struct gapobj_type), sizeof(ALIGN));

  /* cross-reference and data base types */
  zz_lengths[ CROSS_SYM    ] =
  zz_lengths[ CR_ROOT      ] = ceiling(sizeof(struct cr_type) , sizeof(ALIGN));

  /* external galley record */
  zz_lengths[ EXT_GALL  ] = ceiling(sizeof(struct ext_gall_type),sizeof(ALIGN));

} /* end MemInit() */
Exemple #16
0
 litehtml::uint_ptr DocumentContainer::create_font ( const litehtml::tchar_t * faceName, int size, int weight, litehtml::font_style italic, unsigned int decoration, litehtml::font_metrics * fm )
 {
     /* Hardcoding any font request to OpenSans for now. */
     Font font = {size, weight, italic, nullptr};
     std::vector<Font>::iterator it = std::lower_bound ( mFonts.begin(), mFonts.end(), font );
     if ( ( it != mFonts.end() ) && ( *it == font ) )
     {
         return reinterpret_cast<litehtml::uint_ptr> ( it->face );
     }
     unsigned char* buffer;
     unsigned int  buffer_size;
     switch ( italic )
     {
     case litehtml::fontStyleNormal:
         if ( size < 400 )
         {
             buffer = OpenSans_Light_ttf;
             buffer_size = OpenSans_Light_ttf_len;
         }
         else if ( size < 600 )
         {
             buffer = OpenSans_Regular_ttf;
             buffer_size = OpenSans_Regular_ttf_len;
         }
         else if ( size < 700 )
         {
             buffer = OpenSans_Semibold_ttf;
             buffer_size = OpenSans_Semibold_ttf_len;
         }
         else if ( size < 800 )
         {
             buffer = OpenSans_Bold_ttf;
             buffer_size = OpenSans_Bold_ttf_len;
         }
         else
         {
             buffer = OpenSans_ExtraBold_ttf;
             buffer_size = OpenSans_ExtraBold_ttf_len;
         }
         break;
     case litehtml::fontStyleItalic:
         if ( size < 400 )
         {
             buffer = OpenSans_LightItalic_ttf;
             buffer_size = OpenSans_LightItalic_ttf_len;
         }
         else if ( size < 600 )
         {
             buffer = OpenSans_Italic_ttf;
             buffer_size = OpenSans_Italic_ttf_len;
         }
         else if ( size < 700 )
         {
             buffer = OpenSans_SemiboldItalic_ttf;
             buffer_size = OpenSans_SemiboldItalic_ttf_len;
         }
         else if ( size < 800 )
         {
             buffer = OpenSans_BoldItalic_ttf;
             buffer_size = OpenSans_BoldItalic_ttf_len;
         }
         else
         {
             buffer = OpenSans_ExtraBoldItalic_ttf;
             buffer_size = OpenSans_ExtraBoldItalic_ttf_len;
         }
         break;
     }
     FT_Error ft_error;
     if ( ( ft_error = FT_New_Memory_Face ( mFreeType, buffer, buffer_size, 0, &font.face ) ) != 0 )
     {
         AEONGUI_LOG_ERROR ( "FT_New_Memory_Face returned error code 0x%02x", ft_error );
         return nullptr;
     }
     if ( ft_error = FT_Set_Pixel_Sizes ( font.face, 0, font.size ) )
     {
         AEONGUI_LOG_ERROR ( "FT_Set_Pixel_Sizes returned error code 0x%02x", ft_error );
         FT_Done_Face ( font.face );
         return nullptr;
     }
     if ( fm != nullptr )
     {
         fm->height = ceiling ( font.face->bbox.yMax - font.face->bbox.yMin );
         fm->ascent = ceiling ( font.face->bbox.yMax );
         fm->descent = ceiling ( font.face->bbox.yMin );
         FT_Load_Char ( font.face, 'x', FT_LOAD_NO_BITMAP );
         fm->x_height = ceiling ( font.face->glyph->metrics.height );
     }
     return mFonts.insert ( it, font )->face;
 }
void wfm_sublatt_pointers(int lx, int ly, int lz, int lt, int slatt,
		       Wilson *wilson_p)
{
  char *cname = " ";
  char *fname = "wfm_sublatt_pointers(...)";
  VRB.Func(cname,fname);

   static int evenlx, evenp;
   static int sx;
   static int chkbd, bpad, shift;
   static int schkbd, SFT, ixx;
   static int acc0, acc1, acc2, acc3, xmax;
   static int it, iz, iy, ix;
   static int ip;
   static int *ptr;

   /* Local copy of the pointer ptr in the Wilson structure  */
   ptr = wilson_p->ptr;

   /* find if lx, ly, lz, lt are even (EVEN=1)*/
   evenlx = EVEN(lx);
   
   sx = (lx + (1 - evenlx))/2;

   /* In the structure PTRTAB initialize the block size, stride, 
      and number of blocks (used for communications). Also in the
      same structure initialize the word that results from packaging 
      them in one word ready to be stored in the SCU DMA registers. 
      This is reduntant information but it is available in convinient
      forms. 
      blklen below indicates the block length as the number of 
      half spinor words = BLOCK = 12 */
   /* X-direction */
   wilson_p->comm_offset[0] = sx * BLOCK;
   wilson_p->comm_stride[0] = sx * BLOCK + 1;
   wilson_p->comm_numblk[0] = ly * lz * lt;
   wilson_p->comm_blklen[0] = 1;
   /* package in one 32 bit word: blklen[10bits]-numblk[10bits]-stride[12bits] */
   wilson_p->comm[0].stride = sx * BLOCK + 1;
   wilson_p->comm[0].numblk = ly * lz * lt;
   wilson_p->comm[0].blklen = 1;

   /* Y-direction */
   wilson_p->comm_offset[1] = sx * ly * BLOCK;
   wilson_p->comm_stride[1] = sx * ly * BLOCK + 1;
   wilson_p->comm_numblk[1] = lz * lt;
   wilson_p->comm_blklen[1] = sx;
   /* package in one 32 bit word: blklen[10bits]-numblk[10bits]-stride[12bits] */
   wilson_p->comm[1].stride = sx * ly * BLOCK + 1;
   wilson_p->comm[1].numblk = lz * lt;
   wilson_p->comm[1].blklen = sx;

   /* Z-direction */
   wilson_p->comm_offset[2] = sx * ly * lz * BLOCK;
   wilson_p->comm_stride[2] = sx * ly * lz * BLOCK + 1;
   wilson_p->comm_numblk[2] = lt;
   wilson_p->comm_blklen[2] = sx * ly;
   /* package in one 32 bit word: blklen[10bits]-numblk[10bits]-stride[12bits] */
   wilson_p->comm[2].stride =  sx * ly * lz * BLOCK + 1;
   wilson_p->comm[2].numblk = lt;
   wilson_p->comm[2].blklen = sx * ly;

   /* T-direction */
   wilson_p->comm_offset[3] = sx * ly * lz * lt * BLOCK;
   wilson_p->comm_stride[3] = sx * ly * lz * lt * BLOCK + 1;
   wilson_p->comm_numblk[3] = 1;
   wilson_p->comm_blklen[3] = sx * ly * lz;
   /* package in one 32 bit word: blklen[10bits]-numblk[10bits]-stride[12bits] */
   wilson_p->comm[3].stride = sx * ly * lz * lt * BLOCK + 1;
   wilson_p->comm[3].numblk = 1;
   wilson_p->comm[3].blklen = sx * ly * lz;


   /************************* INCORRECT HACK ********************************/
   /* compute the subgrd volume of each chkbd */
   wilson_p->vol[0] = sx * ly * lz * lt;
   wilson_p->vol[1] = sx * ly * lz * lt;
   /************************* INCORRECT HACK ********************************/

   /* compute the subgrid volume of each padded temporary in dslash */
   wilson_p->padded_subgrid_vol[0] = (sx+1) * ly * lz * lt;
   wilson_p->padded_subgrid_vol[1] = sx * (ly+1) * lz * lt;
   wilson_p->padded_subgrid_vol[2] = sx * ly * (lz+1) * lt;
   wilson_p->padded_subgrid_vol[3] = sx * ly * lz * (lt+1);

   /* calculate the external variables YZTMAX, OFFSET needed
      for the sublattice loop */
   wilson_p->yztmax = ly * lz * lt - 1;
   wilson_p->offset = ly * lz * lt * 5;

   /* calculate the pointers for the various cases: 
      chkbd=0, 1, bpad=0, 1, shift=0, 1 */ 

   ip = 0;
   for( chkbd=0 ; chkbd <=1; ++chkbd) 
      for(bpad=0 ; bpad <=1; ++bpad) 	 
	 for( shift=0 ; shift <=1; ++shift){
/*
	    VRB.Flow(cname,fname,"\n %d %d %d \n", chkbd, bpad, shift );
	    VRB.Flow(cname,fname,"---------------------------------- \n");
*/

	    /* If we use the coordinate system of the whole lattice then
	       let the coordinates of a point be (X, Y, Z, T). If we use the
	       coordinate system of the given sublattice then let the
	       coordinates of a point on that sublattice be (x, y, z, t). The 
	       formulas use (x, y, z, t) and the parity is defined as 
	       p=(-1)^(x+y+z+t). The true parity is P=(-1)^(X+Y+Z+T).
	       If for (x, y, z, t)=(0, 0, 0, 0) P is not equal to 1 then the 
	       meaning of (even/odd) checkerboard is reversed. 
	       If checkerboard is even chkbd=0.
	       If checkerboard is odd  chkbd=1.
	       If sublattice is even slatt=0 (P=1).
	       If sublattice is odd  slatt=1 (P=-1).
	       Then the "sublattice checkerboard" is:
	       schkbd = slatt .XOR. chkbd */

	    schkbd = (slatt | chkbd) & (1 - (slatt & chkbd));
	    	    
	    /* loop over y, z, t */
	    for( it=0 ; it < lt; ++it){
 	       for( iz=0 ; iz < lz; ++iz){
		  for( iy=0 ; iy < ly; ++iy){

		     /* set the relevant coordinate shifts */
		     if( (bpad == 0) && (shift == 0) ) SFT = 0;
		     if( (bpad == 0) && (shift == 1) ) SFT = 1;
		     if( (bpad == 1) && (shift == 0) ) SFT = 0;
		     if( (bpad == 1) && (shift == 1) ) SFT = -1;

		     /* find the parity of the x-coordinate of the first
			point of the x-line (after any shifting)*/
		     evenp = EVEN(SFT + 0 + iy + iz + it);

		     /* if this point is a point of the given "sublattice
			checkerboard", then ix=0, else ix=1 */
		     if( schkbd == (1 - evenp)) ix = 0, xmax = sx-1;
		     if( schkbd == evenp) ix = 1, xmax = sx-1-(1 - evenlx);

		     /* for shifts along the x (0) direction, 
			the value of the x-coordinate of the first
			point of the x-line of the given "sublattice
			checkerboard" has x-coordinate ix = ix + SFT */
		     if( evenlx == 0 && bpad == 0 ) ixx = ceiling(ix+SFT);
		     if( evenlx == 0 && bpad == 1 ) ixx = floor(ix+SFT);
		     if( evenlx == 1 ) ixx = floor(ix+SFT);
		     acc0 = bpad + ixx + (sx+1) * (iy + ly * ( iz + lz * it));

		     /* for shifts along the y, z, t (1, 2, 3) directions this
			point has x-coordinate ix */
		     if( evenlx == 0 && bpad == 0 ) ixx = ceiling(ix);
		     if( evenlx == 0 && bpad == 1 ) ixx = floor(ix);
		     if( evenlx == 1 ) ixx = floor(ix);
		     acc1=ixx+(sx)*((iy+bpad+SFT) + (ly+1) * ( iz + lz * it));
		     acc2=ixx+(sx)*(iy + ly * ( (iz+bpad+SFT) + (lz+1) * it));
		     acc3=ixx+(sx)*(iy + ly * ( iz + lz * (it+bpad+SFT)));

		     /* set the pointers */
		     ptr[ip++]=BLOCK * acc0;
		     ptr[ip++]=BLOCK * acc1;
		     ptr[ip++]=BLOCK * acc2;
		     ptr[ip++]=BLOCK * acc3;
		     ptr[ip++]=xmax;
/*
		     VRB.Flow(cname,fname, "%4d %4d %4d %4d %4d \n", 
			      ptr[ip-5], ptr[ip-4], ptr[ip-3], 
			      ptr[ip-2], ptr[ip-1] );
*/
		  }
	       }
	    }
	 }

   return;
}
unsigned int read_statefile(void)
{
  int fd;
  ir_uint32 *buffer, *buffer_begin;
  ir_uint32 buffer_len;
  struct MD5Context md5sum;
  MD5Digest digest;
  struct stat st;
  statefile_hdr_t *hdr;
  ir_uint32 calluval;
  unsigned int save = 0;
  time_t timestamp = 0;
  
  updatecontext();
  
  if (gdata.statefile == NULL)
    {
      return save;
    }
  
  ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "Loading State File... ");
  
  fd = open(gdata.statefile,
            O_RDONLY | O_CREAT | ADDED_OPEN_FLAGS,
            CREAT_PERMISSIONS );
  
  if (fd < 0)
    {
      outerror(OUTERROR_TYPE_WARN_LOUD, "Cant Access State File '%s': %s",
               gdata.statefile, strerror(errno));
      return ++save;
    }
  
  if ((fstat(fd, &st) < 0) || (st.st_size < ((off_t)(sizeof(ir_uint32) * 2) + (off_t)(sizeof(MD5Digest)))))
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "State File: Too small, Skipping");
      close(fd);
      return ++save;
    }
  
  buffer_len = st.st_size;
  buffer_begin = buffer = mycalloc(buffer_len);
  
  calluval = read(fd, buffer, buffer_len);
  close(fd);
  if (calluval != buffer_len)
    {
      outerror(OUTERROR_TYPE_WARN_LOUD, "Cant Read State File (%u != %u) %s",
               calluval, buffer_len, strerror(errno));
      goto error_out;
    }
  
  /* verify md5sum */
  buffer_len -= sizeof(MD5Digest);
  
  MD5Init(&md5sum);
  MD5Update(&md5sum, (md5byte*)buffer, buffer_len);
  MD5Final(digest, &md5sum);
  
  if (memcmp(digest, buffer+(buffer_len/sizeof(ir_uint32)), sizeof(MD5Digest)))
    {
      outerror(OUTERROR_TYPE_CRASH,
               "\"%s\" Appears corrupt or is not an iroffer state file",
               gdata.statefile);
      goto error_out;
    }
  
  /* read */
  
  if (ntohl(*buffer) != STATEFILE_MAGIC)
    {
      outerror(OUTERROR_TYPE_CRASH,
               "\"%s\" Does not appear to be an iroffer state file",
               gdata.statefile);
      goto error_out;
    }
  buffer++;
  buffer_len -= sizeof(ir_uint32);
  
  if (gdata.debug > 0)
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
              "  [Version %u State File]", ntohl(*buffer));
    }
  buffer++;
  buffer_len -= sizeof(ir_uint32);
  
  while ((hdr = read_statefile_item(&buffer, &buffer_len)))
    {
      switch (hdr->tag)
        {
        case STATEFILE_TAG_TIMESTAMP:
          read_statefile_time(hdr, "Timestamp", &timestamp, "Written on");
          break;
          
        case STATEFILE_TAG_XFR_RECORD:
          read_statefile_float(hdr, "xfr Record", &(gdata.record), "Record");
          break;
          
        case STATEFILE_TAG_SENT_RECORD:
          read_statefile_float(hdr, "sent Record", &(gdata.sentrecord), "Bandwidth Record");
          break;
          
        case STATEFILE_TAG_TOTAL_SENT:
          read_statefile_llint(hdr, "Total Transferred", &(gdata.totalsent), "Total Transferred");
          break;
          
        case STATEFILE_TAG_TOTAL_UPTIME:
          read_statefile_long(hdr, "Total Runtime", &(gdata.totaluptime));
              if (gdata.debug > 0)
                {
                  char *tempstr;
                  tempstr = mymalloc(maxtextlength);
                  getuptime(tempstr, 0, gdata.curtime-gdata.totaluptime, maxtextlength);
                  ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                          "  [Total Runtime %s]", tempstr);
                  mydelete(tempstr);
                }
          break;
          
        case STATEFILE_TAG_LAST_LOGROTATE:
          read_statefile_time(hdr, "Last Log Rotate", &(gdata.last_logrotate), "Last Log Rotate");
          break;
          
        case STATEFILE_TAG_IROFFER_VERSION:
          if (hdr->length > sizeof(statefile_hdr_t))
            {
              char *iroffer_version = (char*)(&hdr[1]);
              char *iroffer_now;
              iroffer_version[hdr->length-sizeof(statefile_hdr_t)-1] = '\0';
              
              if (gdata.debug > 0)
                {
                  ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                          "  [Written by %s]", iroffer_version);
                }
              iroffer_now = mycalloc(maxtextlength);
              snprintf(iroffer_now, maxtextlength, "iroffer-dinoex " VERSIONLONG ", %s", gdata.osstring);
              if (strcmp(iroffer_version, iroffer_now) != 0)
                {
                  ++save;
                  backup_statefile();
                }
              mydelete(iroffer_now);
            }
          else
            {
              read_statefile_bad_tag(hdr, "Iroffer Version");
            }
          break;
          
        case STATEFILE_TAG_IGNORE:
          {
            igninfo *ignore;
            statefile_hdr_t *ihdr;

            ignore = irlist_add(&gdata.ignorelist, sizeof(igninfo));
            
            hdr->length -= sizeof(*hdr);
            ihdr = &hdr[1];
            
            while (hdr->length >= sizeof(*hdr))
              {
                ihdr->tag = ntohl(ihdr->tag);
                ihdr->length = ntohl(ihdr->length);
                switch (ihdr->tag)
                  {
                  case STATEFILE_TAG_IGNORE_FLAGS:
                    read_statefile_int(ihdr, "Ignore Flags", &(ignore->flags));
                    break;
                    
                  case STATEFILE_TAG_IGNORE_BUCKET:
                    read_statefile_long(ihdr, "Ignore Bucket", &(ignore->bucket));
                    break;
                    
                  case STATEFILE_TAG_IGNORE_LASTCONTACT:
                    read_statefile_time(ihdr, "Ignore Lastcontact", &(ignore->lastcontact), NULL);
                    break;
                    
                  case STATEFILE_TAG_IGNORE_HOSTMASK:
                    read_statefile_string(ihdr, "Ignore Hostmask", &(ignore->hostmask));
                    break;
                    
                  default:
                    read_statefile_unknown_tag(ihdr, "Ignore" );
                  }
                hdr->length -= ceiling(ihdr->length, 4);
                ihdr = (statefile_hdr_t*)(((char*)ihdr) + ceiling(ihdr->length, 4));
              }
            
            if ((!ignore->lastcontact) || (!ignore->hostmask))
              {
                read_statefile_incomplete_tag("Ignore" );
                mydelete(ignore->hostmask);
                irlist_delete(&gdata.ignorelist, ignore);
              }
            else
              {
                ignore->bucket -= (gdata.curtime - timestamp)/gdata.autoignore_threshold;
              }
          }
          
          break;
          
        case STATEFILE_TAG_MSGLOG:
          {
            msglog_t *msglog;
            statefile_hdr_t *ihdr;

            msglog = irlist_add(&gdata.msglog, sizeof(msglog_t));
            
            hdr->length -= sizeof(*hdr);
            ihdr = &hdr[1];
            
            while (hdr->length >= sizeof(*hdr))
              {
                ihdr->tag = ntohl(ihdr->tag);
                ihdr->length = ntohl(ihdr->length);
                switch (ihdr->tag)
                  {
                  case STATEFILE_TAG_MSGLOG_WHEN:
                    read_statefile_time(ihdr, "Msglog When", &(msglog->when), NULL);
                    break;
                    
                  case STATEFILE_TAG_MSGLOG_HOSTMASK:
                    read_statefile_string(ihdr, "Msglog Hostmask", &(msglog->hostmask));
                    break;
                    
                  case STATEFILE_TAG_MSGLOG_MESSAGE:
                    read_statefile_string(ihdr, "Msglog Message", &(msglog->message));
                    break;
                    
                  default:
                    read_statefile_unknown_tag(ihdr, "Msglog" );
                  }
                hdr->length -= ceiling(ihdr->length, 4);
                ihdr = (statefile_hdr_t*)(((char*)ihdr) + ceiling(ihdr->length, 4));
              }
            
            if ((!msglog->when) || (!msglog->hostmask) || (!msglog->message))
              {
                read_statefile_incomplete_tag("Msglog" );
                mydelete(msglog->hostmask);
                mydelete(msglog->message);
                irlist_delete(&gdata.msglog, msglog);
              }
          }
          
          break;
          
        case STATEFILE_TAG_XDCCS:
          {
            xdcc *xd;
            statefile_hdr_t *ihdr;
            
            xd = irlist_add(&gdata.xdccs, sizeof(xdcc));
            xd->file_fd = FD_UNUSED;
            
            xd->minspeed = gdata.transferminspeed;
            xd->maxspeed = gdata.transfermaxspeed;
            
            hdr->length -= sizeof(*hdr);
            ihdr = &hdr[1];
            
            while (hdr->length >= sizeof(*hdr))
              {
                ihdr->tag = ntohl(ihdr->tag);
                ihdr->length = ntohl(ihdr->length);
                switch (ihdr->tag)
                  {
                  case STATEFILE_TAG_XDCCS_FILE:
                    read_statefile_string(ihdr, "XDCC File", &(xd->file));
                    xd->desc = mystrdup(getfilename(xd->file));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_DESC:
                    mydelete(xd->desc);
                    read_statefile_string(ihdr, "XDCC Desc", &(xd->desc));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_NOTE:
                    read_statefile_string(ihdr, "XDCC Note", &(xd->note));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_GETS:
                    read_statefile_int(ihdr, "XDCC Gets", &(xd->gets));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_MINSPEED:
                    read_statefile_float_set(ihdr, "XDCC Minspeed", &(xd->minspeed));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_MAXSPEED:
                    read_statefile_float_set(ihdr, "XDCC Maxspeed", &(xd->maxspeed));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_MD5SUM_INFO:
                    read_statefile_md5info(ihdr, "XDCC md5sum", xd);
                    break;
                    
                  case STATEFILE_TAG_XDCCS_CRC32:
                    read_statefile_int(ihdr, "XDCC CRC32", &(xd->crc32));
                    xd->has_crc32 = 1;
                    break;
                    
                  case STATEFILE_TAG_XDCCS_GROUP:
                    read_statefile_string(ihdr, "XDCC Group", &(xd->group));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_GROUP_DESC:
                    read_statefile_string(ihdr, "XDCC Group Desc", &(xd->group_desc));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_LOCK:
                    read_statefile_string(ihdr, "XDCC Lock", &(xd->lock));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_DLIMIT_MAX:
                    read_statefile_int(ihdr, "XDCC Limit Max", &(xd->dlimit_max));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_DLIMIT_USED:
                    read_statefile_int(ihdr, "XDCC Limit Used", &(xd->dlimit_used));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_DLIMIT_DESC:
                    read_statefile_string(ihdr, "XDCC Limit Desc", &(xd->dlimit_desc));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_TRIGGER:
                    read_statefile_string(ihdr, "XDCC Trigger", &(xd->trigger));
                    break;
                    
                  case STATEFILE_TAG_XDCCS_XTIME:
                    read_statefile_time(ihdr, "XDCC Time", &(xd->xtime), NULL);
                    break;

                  case STATEFILE_TAG_XDCCS_COLOR:
                    read_statefile_int(ihdr, "XDCC Color", &(xd->color));
                    break;
                    
                  default:
                    read_statefile_unknown_tag(ihdr, "XDCC" );
                  }
                hdr->length -= ceiling(ihdr->length, 4);
                ihdr = (statefile_hdr_t*)(((char*)ihdr) + ceiling(ihdr->length, 4));
              }
            
            if ((!xd->file) || (!xd->desc))
              {
                read_statefile_incomplete_tag("XDCC" );
                mydelete(xd->file);
                mydelete(xd->desc);
                mydelete(xd->note);
                mydelete(xd->group);
                mydelete(xd->group_desc);
                mydelete(xd->lock);
                mydelete(xd->trigger);
                irlist_delete(&gdata.xdccs, xd);
              }
            else
              {
                if (stat(xd->file, &st) < 0)
                  {
                    outerror(OUTERROR_TYPE_WARN, "Pack %u: Cant Access Offered File '%s': %s",
                             number_of_pack(xd),
                             xd->file, strerror(errno));
                    memset(&st, 0, sizeof(st));
                    break;
                  }
                if (!xd->has_md5sum ||
                    (xd->st_ino   != st.st_ino) ||
                    (xd->mtime    != st.st_mtime) ||
                    (xd->st_size  != st.st_size))
                  {
                    xd->st_size     = st.st_size;
                    xd->st_dev      = st.st_dev;
                    xd->st_ino      = st.st_ino;
                    xd->mtime       = st.st_mtime;
                    xd->has_md5sum  = 0;
                    memset(xd->md5sum, 0, sizeof(MD5Digest));
                  }
                if (xd->st_dev != st.st_dev)
                  {
                    /* only mountpoint has changed */
                    xd->st_dev = st.st_dev;
                  }
                
                if (xd->st_size == 0)
                  {
                    outerror(OUTERROR_TYPE_WARN, "Pack %u: The file \"%s\" has size of 0 byte!",
                             number_of_pack(xd),
                             xd->file);
                  }
                
                if (xd->st_size > gdata.max_file_size)
                  {
                    outerror(OUTERROR_TYPE_CRASH, "Pack %u: The file \"%s\" is too large!",
                             number_of_pack(xd),
                             xd->file);
                  }
              }
          }
          
          break;
          
        case STATEFILE_TAG_TLIMIT_DAILY_USED:
          read_statefile_llint(hdr, "Daily Transfer Limit Used", &(gdata.transferlimits[TRANSFERLIMIT_DAILY].used), "Daily Transfer Limit Used");
          break;
          
        case STATEFILE_TAG_TLIMIT_DAILY_ENDS:
          read_statefile_time(hdr, "Daily Transfer Limit Ends", &(gdata.transferlimits[TRANSFERLIMIT_DAILY].ends), "Daily Transfer Limit Ends");
          break;
          
        case STATEFILE_TAG_TLIMIT_WEEKLY_USED:
          read_statefile_llint(hdr, "Weekly Transfer Limit Used", &(gdata.transferlimits[TRANSFERLIMIT_WEEKLY].used), "Weekly Transfer Limit Used");
          break;
          
        case STATEFILE_TAG_TLIMIT_WEEKLY_ENDS:
          read_statefile_time(hdr, "Weekly Transfer Limit Ends", &(gdata.transferlimits[TRANSFERLIMIT_WEEKLY].ends), "Weekly Transfer Limit Ends");
          break;
          
        case STATEFILE_TAG_TLIMIT_MONTHLY_USED:
          read_statefile_llint(hdr, "Monthly Transfer Limit Used", &(gdata.transferlimits[TRANSFERLIMIT_MONTHLY].used), "Monthly Transfer Limit Used");
          break;
          
        case STATEFILE_TAG_TLIMIT_MONTHLY_ENDS:
          read_statefile_time(hdr, "Monthly Transfer Limit Ends", &(gdata.transferlimits[TRANSFERLIMIT_MONTHLY].ends), "Monthly Transfer Limit Ends");
          break;
          
        case STATEFILE_TAG_QUEUE:
          read_statefile_queue(hdr);
          break;
          
        default:
          read_statefile_unknown_tag(hdr, "Main" );
          break;
        }
    }
  
  if (buffer_len)
    {
      outerror(OUTERROR_TYPE_WARN, "Extra data at end of state file!? %u left",
               buffer_len);
    }
  
  /* end read */
  
  if ((gdata.debug > 0) || irlist_size(&gdata.ignorelist))
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "  [Found %u %s]",
              irlist_size(&gdata.ignorelist),
              (irlist_size(&gdata.ignorelist) == 1) ? "ignore" : "ignores");
    }
  
  if ((gdata.debug > 0) || irlist_size(&gdata.msglog))
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "  [Found %u %s]",
              irlist_size(&gdata.msglog),
              (irlist_size(&gdata.msglog) == 1) ? "message" : "messages");
    }
  
  if ((gdata.debug > 0) || irlist_size(&gdata.xdccs))
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "  [Found %u %s]",
              irlist_size(&gdata.xdccs),
              (irlist_size(&gdata.xdccs) == 1) ? "pack" : "packs");
    }
  
  if ((gdata.debug > 0) || irlist_size(&gdata.mainqueue))
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "  [Found %u %s]",
              irlist_size(&gdata.mainqueue),
              (irlist_size(&gdata.mainqueue) == 1) ? "queue" : "queues");
    }
  if ((gdata.debug > 0) || irlist_size(&gdata.idlequeue))
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "  [Found %u %s]",
              irlist_size(&gdata.idlequeue),
              (irlist_size(&gdata.idlequeue) == 1) ? "queue" : "queues");
    }
  
  ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "  [Done]");
  
 error_out:
  
  mydelete(buffer_begin);
  
  return save;
}
void computeNumCTAs(KernelPointer kernel, int smemDynamicBytes, bool bManualCoalesce)
{
    cudaDeviceProp devprop;
    int deviceID = -1;
    cudaError_t err = cudaGetDevice(&deviceID);
    assert(err == cudaSuccess);

    cudaGetDeviceProperties(&devprop, deviceID);

    int smVersion = devprop.major * 10 + devprop.minor;
    // Determine the maximum number of CTAs that can be run simultaneously for each kernel
    // This is equivalent to the calculation done in the CUDA Occupancy Calculator spreadsheet
    
    const unsigned int warpAllocationMultiple = 2;
    const unsigned int maxBlocksPerSM = 8;
    unsigned int maxThreadsPerSM = 768;
    unsigned int regAllocationUnit = 256;  // in registers
    unsigned int smemAllocationUnit = 512; // in bytes
    bool blockRegisterAllocation = true;   // otherwise warp granularity (sm_20)

    if (smVersion >= 20)
    {
        maxThreadsPerSM = 1536;
        regAllocationUnit = 64;
        blockRegisterAllocation = false;
        smemAllocationUnit = 128;
    }
    else if (smVersion >= 12)
    {
        maxThreadsPerSM = 1024;
        regAllocationUnit = 512;
    }
   
    cudaFuncAttributes attr;
    err = cudaFuncGetAttributes(&attr, (const char*)kernel);
    assert(err == cudaSuccess);

    // Number of warps (round up to nearest whole multiple of warp size)
    size_t numWarps = multiple(RadixSort::CTA_SIZE, devprop.warpSize);
    // Round up to warp allocation multiple
    numWarps = ceiling(numWarps, warpAllocationMultiple);

    size_t regsPerCTA = 0;

    if (blockRegisterAllocation)
    {
        // Number of regs is regs per thread times number of warps times warp size
        // rounded up to multiple of register allocation unit size
        regsPerCTA = ceiling(attr.numRegs * devprop.warpSize * numWarps, regAllocationUnit);
    }
    else
    {
        // warp register allocation
        // Number of regs is regs per thread times warp size, rounded up to multiple of 
        // register allocation unit size, times number of warps.
        regsPerCTA = ceiling(attr.numRegs * devprop.warpSize, regAllocationUnit) * numWarps;
    }

    size_t smemBytes = attr.sharedSizeBytes + smemDynamicBytes;
    size_t smemPerCTA = ceiling(smemBytes, smemAllocationUnit);

    size_t ctaLimitRegs    = regsPerCTA > 0 ? devprop.regsPerBlock           / regsPerCTA : maxBlocksPerSM;
    size_t ctaLimitSMem    = smemPerCTA > 0 ? devprop.sharedMemPerBlock      / smemPerCTA : maxBlocksPerSM;
    size_t ctaLimitThreads =                  maxThreadsPerSM                / RadixSort::CTA_SIZE;

    unsigned int numSMs = devprop.multiProcessorCount;
    int maxCTAs = numSMs * std::min<size_t>(ctaLimitRegs, std::min<size_t>(ctaLimitSMem, std::min<size_t>(ctaLimitThreads, maxBlocksPerSM)));
    setNumCTAs(kernel, maxCTAs);
}
Exemple #20
0
/* Generate earthquake :-) of desired force. That is: create random chasms
   (pits). Currently assumes that the player created it (you'll need to change
   at least messages, angering, and kill credit if you generalize it). */
static void
do_earthquake(int force)
{
    int x, y;
    struct monst *mtmp;
    struct obj *otmp;
    struct trap *chasm, *oldtrap;
    int start_x, start_y, end_x, end_y;

    start_x = youmonst.mx - (force * 2);
    start_y = youmonst.my - (force * 2);
    end_x = youmonst.mx + (force * 2);
    end_y = youmonst.my + (force * 2);
    if (start_x < 0)
        start_x = 0;
    if (start_y < 0)
        start_y = 0;
    if (end_x >= COLNO)
        end_x = COLNO - 1;
    if (end_y >= ROWNO)
        end_y = ROWNO - 1;
    for (x = start_x; x <= end_x; x++)
        for (y = start_y; y <= end_y; y++) {
            if ((mtmp = m_at(level, x, y)) != 0) {
                wakeup(mtmp, FALSE);  /* peaceful monster will become hostile */
                if (mtmp->mundetected && is_hider(mtmp->data)) {
                    mtmp->mundetected = 0;
                    if (cansee(x, y))
                        pline(msgc_youdiscover, "%s is shaken loose from %s!",
                              Amonnam(mtmp), mtmp->data == &mons[PM_TRAPPER] ?
                              "its hiding place" : the(ceiling(youmonst.mx, youmonst.my)));
                    else
                        You_hear(msgc_levelsound, "a thumping sound.");
                    if (x == youmonst.mx && y == youmonst.my &&
                        mtmp->data != &mons[PM_TRAPPER])
                        pline(msgc_moncombatgood,
                              "You easily dodge the falling %s.",
                              mon_nam(mtmp));
                    newsym(x, y);
                }
            }
            if (!rn2(14 - force))
                switch (level->locations[x][y].typ) {
                case FOUNTAIN: /* Make the fountain disappear */
                    if (cansee(x, y))
                        pline(msgc_consequence,
                              "The fountain falls into a chasm.");
                    goto do_pit;
                case SINK:
                    if (cansee(x, y))
                        pline(msgc_consequence,
                              "The kitchen sink falls into a chasm.");
                    goto do_pit;
                case ALTAR:
                    if (level->locations[x][y].altarmask & AM_SANCTUM)
                        break;

                    if (cansee(x, y))
                        pline(msgc_consequence,
                              "The altar falls into a chasm.");
                    goto do_pit;
                case GRAVE:
                    if (cansee(x, y))
                        pline(msgc_consequence,
                              "The headstone topples into a chasm.");
                    goto do_pit;
                case THRONE:
                    if (cansee(x, y))
                        pline(msgc_consequence,
                              "The throne falls into a chasm.");
                    /* Falls into next case */
                case ROOM:
                case CORR:     /* Try to make a pit */
                    /* Pits, spiked pits, holes, trapdoors, vibrating squares,
                       magic portals are immune.  A bear trap will leave the
                       trap in the pit.  It would be kind of cool to make
                       landmines detonate, but that's more trouble than it's
                       worth. */
                    if ((oldtrap = t_at(level, x, y))) {
                        if (oldtrap->ttyp == PIT || oldtrap->ttyp == SPIKED_PIT
                            || oldtrap->ttyp == HOLE ||
                            oldtrap->ttyp == TRAPDOOR ||
                            oldtrap->ttyp == VIBRATING_SQUARE ||
                            oldtrap->ttyp == MAGIC_PORTAL)
                            break;

                        if (oldtrap->ttyp == BEAR_TRAP) {
                            if (mtmp)
                                mtmp->mtrapped = 0;
                            cnv_trap_obj(level, BEARTRAP, 1, oldtrap);
                        }
                    }

                do_pit:
                    chasm = maketrap(level, x, y, PIT, rng_main);
                    if (!chasm)
                        break;  /* no pit if portal at that location */
                    chasm->tseen = 1;

                    level->locations[x][y].doormask = 0;

                    mtmp = m_at(level, x, y);

                    if ((otmp = sobj_at(BOULDER, level, x, y)) != 0) {
                        if (cansee(x, y))
                            pline(msgc_consequence,
                                  "KADOOM! The boulder falls into a chasm%s!",
                                  ((x == youmonst.mx) &&
                                   (y == youmonst.my)) ? " below you" : "");
                        if (mtmp)
                            mtmp->mtrapped = 0;
                        obj_extract_self(otmp);
                        flooreffects(otmp, x, y, "");
                        break;
                    }

                    /* We have to check whether monsters or player falls in a
                       chasm... */

                    if (mtmp) {
                        if (!flying(mtmp) && !levitates(mtmp) &&
                            !is_clinger(mtmp->data)) {
                            mtmp->mtrapped = 1;
                            if (cansee(x, y))
                                pline(combat_msgc(&youmonst, mtmp, cr_hit),
                                      "%s falls into a chasm!", Monnam(mtmp));
                            else if (humanoid(mtmp->data))
                                You_hear(msgc_levelsound, "a scream!");
                            mselftouch(mtmp, "Falling, ", &youmonst);
                            if (!DEADMONSTER(mtmp))
                                if ((mtmp->mhp -= rnd(6)) <= 0) {
                                    if (!cansee(x, y))
                                        pline(msgc_kill, "It is destroyed!");
                                    else {
                                        pline(msgc_petfatal, "You destroy %s!",
                                              mtmp->mtame ?
                                              x_monnam(mtmp, ARTICLE_THE,
                                                       "poor", mx_name(mtmp) ?
                                                       SUPPRESS_SADDLE : 0,
                                                       FALSE) : mon_nam(mtmp));
                                    }
                                    xkilled(mtmp, 0);
                                }
                        }
                    } else if (!u.utrap && x == youmonst.mx && y == youmonst.my) {
                        if (Levitation || Flying || is_clinger(youmonst.data)) {
                            pline(msgc_noconsequence,
                                  "A chasm opens up under you!");
                            pline(msgc_noconsequence, "You don't fall in!");
                        } else {
                            pline(msgc_badidea, "You fall into a chasm!");
                            u.utrap = rn1(6, 2);
                            u.utraptype = TT_PIT;
                            turnstate.vision_full_recalc = TRUE;
                            losehp(rnd(6), "fell into a chasm");
                            selftouch("Falling, you",
                                      "falling into a chasm while wielding");
                        }
                    } else
                        newsym(x, y);
                    break;
                case DOOR:     /* Make the door collapse */
                    if (level->locations[x][y].doormask == D_NODOOR)
                        goto do_pit;
                    if (cansee(x, y))
                        pline(msgc_consequence, "The door collapses.");
                    if (*in_rooms(level, x, y, SHOPBASE))
                        add_damage(x, y, 0L);
                    level->locations[x][y].doormask = D_NODOOR;
                    unblock_point(x, y);
                    newsym(x, y);
                    break;
                }
        }
}
Exemple #21
0
unsigned int Trace::workgroupsByDim(int dim)const{
    return ceiling(global_threads.at(dim), local_threads.at(dim));

}
void write_statefile(void)
{
#ifdef DEBUG
  ir_uint64 ms1;
  ir_uint64 ms2;
#endif /* DEBUG */
  char *statefile_tmp, *statefile_bkup;
  int fd;
  int callval;
  statefile_hdr_t *hdr;
  size_t length;
  ir_moutput_t bout;
  
  updatecontext();
  
  if (gdata.statefile == NULL)
    {
      return;
    }
  
#ifdef DEBUG
  ms1 = get_time_in_ms();
#endif /* DEBUG */
  statefile_tmp = mystrsuffix(gdata.statefile, ".tmp");
  statefile_bkup = mystrsuffix(gdata.statefile, "~");
  
  if (gdata.debug > 0)
    {
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "Saving State File... ");
    }
  
  fd = open(statefile_tmp,
            O_WRONLY | O_CREAT | O_TRUNC | ADDED_OPEN_FLAGS,
            CREAT_PERMISSIONS);
  
  if (fd < 0)
    {
      outerror(OUTERROR_TYPE_WARN_LOUD, "Cant Create State File '%s': %s",
               statefile_tmp, strerror(errno));
      goto error_out;
    }
  
  ir_moutput_init(&bout, fd);
  
  /*** write ***/
  write_statefile_direct(&bout, STATEFILE_MAGIC);
  write_statefile_direct(&bout, gdata.old_statefile ? STATEFILE_OLD_VERSION : STATEFILE_VERSION);
  
  updatecontext();
  {
    unsigned char *data;
    unsigned char *next;

    length = sizeof(statefile_hdr_t) + maxtextlength;
    
    data = mycalloc(length);
    
    /* header */
    hdr = (statefile_hdr_t*)data;
    next = (unsigned char*)(&hdr[1]);
    
    length = add_snprintf((char *)next, maxtextlength,
                          "iroffer-dinoex " VERSIONLONG ", %s", gdata.osstring);
    
        create_statefile_hdr(hdr, STATEFILE_TAG_IROFFER_VERSION, sizeof(statefile_hdr_t) + ceiling(length+1, 4));
        write_statefile_item(&bout, data);
    
    mydelete(data);
  }
   
  write_statefile_dinoex(&bout);
   
  updatecontext();
  {
    MD5Digest digest;
    
    bzero(digest, sizeof(digest));
    ir_moutput_get_md5sum(&bout, digest);
    write_statefile_raw(&bout, &digest, sizeof(digest));
  }
  
  /*** end write ***/
  updatecontext();
  
  close(fd);
  /* abort if statefile was not written in full */
  if (bout.error > 0)
    {
      goto error_out;
    }
  
  /* remove old bkup */
  callval = unlink(statefile_bkup);
  if ((callval < 0) && (errno != ENOENT))
    {
      outerror(OUTERROR_TYPE_WARN_LOUD, "Cant Remove Old State File '%s': %s",
               statefile_bkup, strerror(errno));
      /* ignore, continue */
    }
  
  /* backup old -> bkup */
  callval = link(gdata.statefile, statefile_bkup);
  if ((callval < 0) && (errno != ENOENT))
    {
      outerror(OUTERROR_TYPE_WARN_LOUD, "Cant Backup Old State File '%s' -> '%s': %s",
               gdata.statefile, statefile_bkup, strerror(errno));
      /* ignore, continue */
    }
  
  /* rename new -> current */
  callval = rename(statefile_tmp, gdata.statefile);
  if (callval < 0)
    {
      outerror(OUTERROR_TYPE_WARN_LOUD, "Cant Save New State File '%s': %s",
               gdata.statefile, strerror(errno));
      /* ignore, continue */
    }
  
  if (gdata.debug > 0)
    {
#ifdef DEBUG
      ms2 = get_time_in_ms();
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "Done"
              " running: %ld ms", (long)(ms2 - ms1));
#else
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "Done");
#endif /* DEBUG */
    }
  
 error_out:
  
  mydelete(statefile_tmp);
  mydelete(statefile_bkup);
  return;
}