Example #1
0
File: voterd.c Project: Chryel/PINT
void restart_prep(int restartee, int restarter) {
  int i;

  #ifdef TIME_RESTART_REPLICA
    timestamp_t start_restart = generate_timestamp();
  #endif // TIME_RESTART_REPLICA

  // Normally the pipes are stolen, then written back.
  // Here, we want to fill the pipes to test the impact of large messages.
  // So we will write to them, and then steal them (so that the fake data isn't processed).
  // But probably won't work if real data is there...
  // Problem: normally steal once, write twice. Need a second write... use restarted rep?
  #ifdef PIPE_SMASH
    char pipe_fill[PIPE_FILL_SIZE] = {1};
    for (i = 0; i < PIPE_LIMIT; i++) {
      if (replicas[restarter].vot_pipes[i].fd_out != 0) {
        writeBuffer(replicas[restarter].vot_pipes[i].fd_out, pipe_fill, sizeof(pipe_fill));
      }
    }  
  #endif // PIPE_SMASH

  char **restarter_buffer = (char **)malloc(sizeof(char *) * PIPE_LIMIT);
  if (restarter_buffer == NULL) {
    perror("Voter failed to malloc memory");
  }
  for (i = 0; i < PIPE_LIMIT; i++) {
    restarter_buffer[i] = (char *)malloc(sizeof(char) * MAX_VOTE_PIPE_BUFF);
    if (restarter_buffer[i] == NULL) {
      perror("Voter failed to allocat memory");
    }
  }
  int restarter_buff_count[PIPE_LIMIT] = {0};

  // Steal the pipes from healthy reps. This stops them from processing mid restart (also need to copy data to new rep)
  stealPipes(restarter, restarter_buffer, restarter_buff_count);

  // reset timer
  timer_started = false;
  restartReplica(replicas, rep_count, &sd, ext_pipes, restarter, restartee, replica_priority);

  for (i = 0; i < replicas[restarter].pipe_count; i++) {
    if (replicas[restarter].vot_pipes[i].fd_in != 0) {
      copyPipe(&(replicas[restartee].vot_pipes[i]), &(replicas[restarter].vot_pipes[i]));
      sendPipe(i, restarter); // TODO: Need to check if available?
    }
  }

  #ifndef PIPE_SMASH
    // Give the buffers back
    returnPipes(restartee, restarter_buffer, restarter_buff_count);
    returnPipes(restarter, restarter_buffer, restarter_buff_count);
    // free the buffers
    for (i = 0; i < PIPE_LIMIT; i++) {
      free(restarter_buffer[i]);
    }
    free(restarter_buffer);
  #endif // !PIPE_SMASH

  // The second write
  #ifdef PIPE_SMASH
    for (i = 0; i < PIPE_LIMIT; i++) {
      if (replicas[restarter].vot_pipes[i].fd_out != 0) {
        writeBuffer(replicas[restarter].vot_pipes[i].fd_out, pipe_fill, sizeof(pipe_fill));
      }
    }  
  #endif // PIPE_SMASH

  #ifdef TIME_RESTART_REPLICA
    timestamp_t end_restart = generate_timestamp();
    printf("Restart time elapsed usec (%lf)\n", diff_time(end_restart, start_restart, CPU_MHZ));
  #endif // TIME_RESTART_REPLICA

  // Clean up by stealing the extra write. Not timed.
  #ifdef PIPE_SMASH
    if (restarter_buffer == NULL) {
      perror("Voter failed to malloc memory");
    }
    for (i = 0; i < PIPE_LIMIT; i++) {
      restarter_buffer[i] = (char *)malloc(sizeof(char) * MAX_VOTE_PIPE_BUFF);
      if (restarter_buffer[i] == NULL) {
        perror("Voter failed to allocat memory");
      }
    }

    stealPipes(restarter, restarter_buffer, restarter_buff_count);
    // free the buffers
    for (i = 0; i < PIPE_LIMIT; i++) {
      free(restarter_buffer[i]);
    }
    free(restarter_buffer);
  #endif // PIPE_SMASH

  return;
}
Example #2
0
/**
 * \par Function
 *    readjoystick
 * \par Description
 *    This function is used to read the handle datas.
 * \param[in]
 *    None
 * \par Output
 *    None
 * \par Return
 *    Returns 0 if no data ready, 1 if data ready.
 * \par Others
 *    None
 */
boolean MePS2::readjoystick(void)
{
  boolean result = false;
  if(millis() - _lasttime > 200)
  {
    _isReady = false;
    _isStart=false;
    _prevc = 0x00;
    buffer[2] = buffer[4] = buffer[6] =buffer[8] =0x80;
    buffer[1] = buffer[3] = buffer[5] =buffer[7] =0x00;
  }
  readSerial();
  while(_isAvailable)
  {
    _lasttime = millis();
    unsigned char c = _serialRead & 0xff;
    if((c == 0x55) && (_isStart == false))
    {
      if(_prevc == 0xff)
      {
        _index=1;
        _isStart = true;
      }
    }
    else
    {
      _prevc = c;
      if(_isStart)
      {
        writeBuffer(_index,c);
      }
    }
    _index++;
    if((_isStart == false) && (_index > 12))
    {
      _index=0; 
      _isStart=false;
      buffer[2] = buffer[4] = buffer[6] =buffer[8] =0x80;
      buffer[1] = buffer[3] = buffer[5] =buffer[7] =0x00;
    }
    else if(_isStart && (_index > 9))
    {
      uint8_t checksum;
      checksum = buffer[2]+buffer[3]+buffer[4]+buffer[5]+buffer[6]+buffer[7]+buffer[8];
      if(checksum == buffer[9])
      {
        _isReady = true;
       	_isStart = false;
       	_index = 0;
       	result = true;
      }
      else
      {
        _isStart = false;
        _index = 0;
        _prevc = 0x00;
        _isStart=false;
        result = false;
      }
    }
    readSerial();
   }
   return result;
}
/* ==================================================================================
Function name:	writeBuffer
Arguments:		long long int fullAddress
				unsigned int action
				int trigger
Returns:		0 for valid return, -1 for no address matching
Description:	Write buffer that takes evicted lines and holds them until trigger is called.
				Allows the cache to have a small buffer in case evicted lines are used again in
				the short term.
================================================================================== */
int writeBuffer(long long int fullAddress, int action, int trigger)
{
	int counter = 0;
	int innerCounter = 0;
	int hit = FALSE;

	if (trigger >= MIN_TRIGGER_STATE)								//Trigger the removal of an item from the write buffer
	{
		fullAddress = writePtr.wBufferArray[0];		//Get the address of the item written to memory
		if (fullAddress != EMPTY)
		{
#if DEBUGBUFFER
			printf("\n------------------------------------------Writing to memory Address in Write Buffer---------------\n");
			printf(" Address in Write Buffer written to memory: %#x", fullAddress);
			printf("\n--------------------------------------------------------------------------------------------------\n\n");
#endif
			WriteMemory(fullAddress);					//Output that it is being written to memory
		}
#if DEBUGBUFFER
		else
		{
			printf("\n------------------------------------------Address in Write Buffer is invalid----------------------\n");
			printf(" Address in Write Buffer is empty, cycling to next address");
			printf("\n--------------------------------------------------------------------------------------------------\n\n");
		}
#endif
		for (innerCounter = 0; innerCounter < WRITE_BUFFER_SIZE; ++innerCounter)
		{
			if (innerCounter + 1 == WRITE_BUFFER_SIZE)
				writePtr.wBufferArray[innerCounter] = EMPTY;			//Walk through the array and shift all the contents to the left by one and replace the last line with a -1 for checking.
			else
				writePtr.wBufferArray[innerCounter] = writePtr.wBufferArray[innerCounter + 1];
#if DEBUGBUFFER
			printf("WriteBuffer After Cycle reflex - Index: %d  Address: %d\n", innerCounter, writePtr.wBufferArray[innerCounter]);
#endif
		}
		return 0;
	}
	if (action == CLEAR)							//Clear the buffer
	{
		for (counter = 0; counter < WRITE_BUFFER_SIZE; ++counter)
		{
			if (writePtr.wBufferArray[counter] != EMPTY)
			{
				WriteMemory(writePtr.wBufferArray[counter]);						//Write out the memory address that was written
				writePtr.wBufferArray[counter] = EMPTY;
			}
		}
	}
	if (action == INSERT)							//Insert an item into the write buffer
	{
#if DEBUGBUFFER
		printf("\n------------------------------------------Inserting Address in Write Buffer-----------------\n");
		printf(" Address inserted into Write Buffer: %#x", fullAddress);
		printf("\n--------------------------------------------------------------------------------------------\n\n");
#endif
		for (counter = 0; counter < WRITE_BUFFER_SIZE; ++counter)
		{
			if (writePtr.wBufferArray[counter] == fullAddress)
			{
				writePtr.wBufferArray[counter] = fullAddress;		//To mimic converging write buffer, "converge" the data bits from the found address with those passed in.
				return 0;
			}
		}
		if (writePtr.wBufferArray[WRITE_BUFFER_SIZE - 1] == EMPTY)
		{
			writePtr.wBufferArray[WRITE_BUFFER_SIZE - 1] = fullAddress;		//The first "NULL" found in the write buffer, insert the address.
		}
		else
		{
			writeBuffer(NOACTION, NOACTION, MIN_TRIGGER_STATE);
#if DEBUGBUFFER
			printf("\n------------------------------------------Done Cycling, Inserting Address-----------------\n");
#endif
			writePtr.wBufferArray[WRITE_BUFFER_SIZE - 1] = fullAddress;						//If completely full, trigger an eviction early, and add the full address.
		}
#if DEBUGBUFFER
		for (counter = 0; counter < WRITE_BUFFER_SIZE; ++counter)
		{
			printf("WriteBuffer - Index: %d  Address: %d\n", counter, writePtr.wBufferArray[counter]);
		}
#endif
		return 0;
	}
	else if (action == CHECK)
	{
		for (counter = 0; counter < WRITE_BUFFER_SIZE; ++counter)
		{
			if (writePtr.wBufferArray[counter] == fullAddress)		//Check if the address passed in is the same as in the buffer
			{
#if DEBUGBUFFER
				printf("\n------------------------------------------Address Found in Write Buffer-----------------\n");
				printf(" Address found in Write Buffer: %#x", writePtr.wBufferArray[counter]);
				printf("\n----------------------------------------------------------------------------------------\n\n");
#endif
				if (trigger = WRITE_TO_MEMORY)									//Check to see if we need to immediately write out, per the MESIF states and commands
				{
					for (innerCounter = counter; innerCounter < WRITE_BUFFER_SIZE; ++innerCounter)
					{
						if (innerCounter + 1 == WRITE_BUFFER_SIZE)
							writePtr.wBufferArray[innerCounter] = EMPTY;		//Redo the array to move off the first in the buffer and shift to the left.
						else
							writePtr.wBufferArray[innerCounter] = writePtr.wBufferArray[innerCounter + 1];
#if DEBUGBUFFER
						printf("WriteBuffer - Index: %d  Address: %d\n", counter, writePtr.wBufferArray[counter]);
#endif
					}
					WriteMemory(fullAddress);						//Write out the memory address that was written
				}
				return fullAddress;
			}
		}
		return -1;  //No address matching the checked for address is in the buffer.
	}
	return -3;  //Error, the wrong parameters were entered.
}
 void put(value_type const c)
 {
         *(pc++) = c;
         if ( pc == pe )
                 writeBuffer();
 }
Example #5
0
			/**
			 * flush the output buffer
			 **/
			void flush()
			{
				writeBuffer();
				W.flush();
			}
Example #6
0
File: Main.cpp Project: gigte/nlfix
bool processFile(const char *path)
{
	HANDLE hfile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ,
		NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

	if (hfile == INVALID_HANDLE_VALUE)
		return false;

	SetFilePointer(tempfile, 0, NULL, FILE_BEGIN);

	static char inbuf[NLF_BUFSIZE];
	static char outbuf[NLF_BUFSIZE];

	DWORD bytes;
	DWORD pos = 0;

	while (true)
	{
		if (!ReadFile(hfile, inbuf, NLF_BUFSIZE, &bytes, NULL))
			goto reading_failed;

		if (bytes < NLF_BUFSIZE)
			break;

		if (!writeBuffer(inbuf, outbuf, bytes, &pos, false))
			goto reading_failed;
	}

	if (bytes != 0 || pos != 0)
	{
		if (!writeBuffer(inbuf, outbuf, bytes, &pos, true))
			goto reading_failed;
	}

	/*if (SetFilePointer(hfile, 0, NULL, FILE_CURRENT)
		== SetFilePointer(tempfile, 0, NULL, FILE_CURRENT))
	{
		
	}*/

	SetEndOfFile(tempfile);

	CloseHandle(hfile);

	// TODO: check if replace isn't needed
	// (i.e. file already has the EOL format wanted)
	//
	// UPD: LF <-> CF check wasn't done

	if (Mode == MODE_DOS
		&& GetFileSize(hfile, NULL) == GetFileSize(tempfile, NULL))
	{
		//puts("(unchanged) ");
		return true;
	}

	if (!replaceFile(path, tempfile_name))
		return false;

	return true;

reading_failed:
	CloseHandle(hfile);

	return false;
}
Example #7
0
void Connection::enqueue( const EString &s )
{
    writeBuffer()->append( s );
}
Example #8
0
// clear the whole board
void LedBoard::clear()
{
	memset(buffer, 0, width * height);
	outputStart();
	writeBuffer();
}
Example #9
0
// processes the incoming packets
bool LedBoard::processPacket(const uint8_t* data, uint16_t packet_len)
{
	uint16_t packet_position = 0;
	// as long as there is data still...
	while(packet_position < packet_len)
	{
		// first byte is command
		uint8_t cmd = data[packet_position++];
		#ifdef DEBUG
		Serial.print("Packet: ");
		Serial.print(cmd, HEX);
		Serial.print(" " + String(packet_len));
		Serial.println();
		#endif
		switch(cmd)
		{
			// write buffer
			case 0x01:
				writeBuffer();
				break;

			// clear
			case 0x02:
				clear();
				break;

			// draw rows
			case 0x10:
			{
				// need 1 byte for y and 96 * 8 for pixel data
				if(packet_len - packet_position < 1 + (96 * 8))
					return false;

				uint8_t y = data[packet_position++];

				packet_position += drawImage(0, y * 8, 96, 8, (uint8_t*)data + packet_position);

				break;
			}
			// draw image rectangle
			case 0x11:
			{
				// 4 bytes for header
				if(packet_len - packet_position < 4)
					return false;
				uint8_t x = data[packet_position++];
				uint8_t y = data[packet_position++];
				uint8_t width = data[packet_position++];
				uint8_t height = data[packet_position++];

				// need enough bytes 
				if(packet_len - packet_position < width * height)
					return false;

				packet_position += drawImage(x, y, width, height, (uint8_t*)(data + packet_position));
				
				break;
			}

			// write text line based
			case 0x20:
			// write text absolute
			case 0x21:
			{
				bool absolute = cmd == 0x21;
				uint8_t x = data[packet_position++];
				uint8_t y = data[packet_position++];
				uint8_t brightness = data[packet_position++];
				int16_t str_size = strnlen((char*)(data + packet_position), packet_len - packet_position);
				// string error
				if(str_size < 0)
					return false;
				packet_position += drawString(
					(char*)(data + packet_position), 
					str_size, 
					x, y, 
					brightness,
					absolute
				);
				break;
			}
			// unknown command -> ignore this packet
			default:
				return false;
		}
	}
	return true;
}
void sample_los_extinction(std::string out_fname, TMCMCOptions &options, TLOSMCMCParams &params,
                           unsigned int N_regions, uint64_t healpix_index) {
	timespec t_start, t_write, t_end;
	clock_gettime(CLOCK_MONOTONIC, &t_start);
	
	std::cout << "guess of EBV max = " << params.EBV_guess_max << std::endl;
	
	guess_EBV_profile(options, params, N_regions);
	//monotonic_guess(img_stack, N_regions, params.EBV_prof_guess, options);
	
	TNullLogger logger;
	
	unsigned int max_attempts = 2;
	unsigned int N_steps = options.steps;
	unsigned int N_samplers = options.samplers;
	unsigned int N_threads = options.N_threads;
	unsigned int ndim = N_regions + 1;
	
	std::vector<double> GR_transf;
	TLOSTransform transf(ndim);
	double GR_threshold = 1.25;
	
	TAffineSampler<TLOSMCMCParams, TNullLogger>::pdf_t f_pdf = &lnp_los_extinction;
	TAffineSampler<TLOSMCMCParams, TNullLogger>::rand_state_t f_rand_state = &gen_rand_los_extinction_from_guess;
	
	std::cerr << std::endl;
	std::cout << "Line-of-Sight Extinction Profile" << std::endl;
	std::cout << "====================================" << std::endl;
	
	TParallelAffineSampler<TLOSMCMCParams, TNullLogger> sampler(f_pdf, f_rand_state, ndim, N_samplers*ndim, params, logger, N_threads);
	sampler.set_scale(1.2);
	sampler.set_replacement_bandwidth(0.50);
	
	// Burn-in
	std::cerr << "# Burn-in ..." << std::endl;
	sampler.step(int(N_steps*40./100.), false, 0., 0.4, 0.);
	sampler.step(int(N_steps*10./100), false, 0., 1.0, 0., false);
	sampler.step(int(N_steps*40./100.), false, 0., 0.4, 0.);
	sampler.step(int(N_steps*10./100), false, 0., 0.8, 0.);
	//sampler.step(N_steps, false, 0., options.p_replacement, 0.);
	//sampler.step(N_steps/2., false, 0., 1., 0.);
	sampler.print_stats();
	sampler.clear();
	
	std::cerr << "# Main run ..." << std::endl;
	bool converged = false;
	size_t attempt;
	for(attempt = 0; (attempt < max_attempts) && (!converged); attempt++) {
		sampler.step((1<<attempt)*N_steps, true, 0., options.p_replacement, 0.);
		
		std::cout << std::endl << "Transformed G-R Diagnostic:";
		sampler.calc_GR_transformed(GR_transf, &transf);
		for(unsigned int k=0; k<ndim; k++) {
			std::cout << "  " << std::setprecision(3) << GR_transf[k];
		}
		std::cout << std::endl << std::endl;
		
		converged = true;
		for(size_t i=0; i<ndim; i++) {
			if(GR_transf[i] > GR_threshold) {
				converged = false;
				if(attempt != max_attempts-1) {
					sampler.print_stats();
					std::cerr << "# Extending run ..." << std::endl;
					sampler.step(int(N_steps*1./5.), false, 0., 1., 0.);
					sampler.clear();
					//logger.clear();
				}
				break;
			}
		}
	}
	
	clock_gettime(CLOCK_MONOTONIC, &t_write);
	
	std::stringstream group_name;
	group_name << "/pixel " << healpix_index;
	TChain chain = sampler.get_chain();
	
	TChainWriteBuffer writeBuffer(ndim, 500, 1);
	writeBuffer.add(chain, converged);
	writeBuffer.write(out_fname, group_name.str(), "los");
	
	std::stringstream los_group_name;
	los_group_name << group_name.str() << "/los";
	H5Utils::add_watermark<double>(out_fname, los_group_name.str(), "DM_min", params.img_stack->rect->min[0]);
	H5Utils::add_watermark<double>(out_fname, los_group_name.str(), "DM_max", params.img_stack->rect->max[0]);
	
	clock_gettime(CLOCK_MONOTONIC, &t_end);
	
	sampler.print_stats();
	std::cout << std::endl;
	
	if(!converged) {
		std::cerr << "# Failed to converge." << std::endl;
	}
	std::cerr << "# Number of steps: " << (1<<(attempt-1))*N_steps << std::endl;
	std::cerr << "# Time elapsed: " << std::setprecision(2) << (t_end.tv_sec - t_start.tv_sec) + 1.e-9*(t_end.tv_nsec - t_start.tv_nsec) << " s" << std::endl;
	std::cerr << "# Sample time: " << std::setprecision(2) << (t_write.tv_sec - t_start.tv_sec) + 1.e-9*(t_write.tv_nsec - t_start.tv_nsec) << " s" << std::endl;
	std::cerr << "# Write time: " << std::setprecision(2) << (t_end.tv_sec - t_write.tv_sec) + 1.e-9*(t_end.tv_nsec - t_write.tv_nsec) << " s" << std::endl << std::endl;
}
void sample_los_extinction_clouds(std::string out_fname, TMCMCOptions &options, TLOSMCMCParams &params,
                                  unsigned int N_clouds, uint64_t healpix_index) {
	timespec t_start, t_write, t_end;
	clock_gettime(CLOCK_MONOTONIC, &t_start);
	
	/*double x[] = {8., 4., -0.693, -1.61};
	gsl_rng *r;
	seed_gsl_rng(&r);
	//gen_rand_los_extinction_clouds(&(x[0]), 4, r, params);
	double lnp_tmp = lnp_los_extinction_clouds(&(x[0]), 4, params);
	std::cout << lnp_tmp << std::endl;
	gsl_rng_free(r);*/
	
	std::cout << "subpixel: " << std::endl;
	for(size_t i=0; i<params.subpixel.size(); i++) {
		std::cout << " " << params.subpixel[i];
	}
	std::cout << std::endl;
	
	TNullLogger logger;
	
	unsigned int max_attempts = 2;
	unsigned int N_steps = options.steps;
	unsigned int N_samplers = options.samplers;
	unsigned int N_threads = options.N_threads;
	unsigned int ndim = 2 * N_clouds;
	
	std::vector<double> GR_transf;
	TLOSCloudTransform transf(ndim);
	double GR_threshold = 1.25;
	
	TAffineSampler<TLOSMCMCParams, TNullLogger>::pdf_t f_pdf = &lnp_los_extinction_clouds;
	TAffineSampler<TLOSMCMCParams, TNullLogger>::rand_state_t f_rand_state = &gen_rand_los_extinction_clouds;
	
	std::cerr << std::endl;
	std::cout << "Line-of-Sight Extinction Profile" << std::endl;
	std::cout << "====================================" << std::endl;
	
	//std::cerr << "# Setting up sampler" << std::endl;
	TParallelAffineSampler<TLOSMCMCParams, TNullLogger> sampler(f_pdf, f_rand_state, ndim, N_samplers*ndim, params, logger, N_threads);
	sampler.set_scale(2.);
	sampler.set_replacement_bandwidth(0.25);
	
	// Burn-in
	std::cerr << "# Burn-in ..." << std::endl;
	sampler.step(int(N_steps*25./100.), false, 0., 0., 0.);
	//sampler.step(int(N_steps*20./100.), false, 0., 0.5, 0.);
	//sampler.step(int(N_steps*5./100), false, 0., 1., 0.);
	sampler.step(int(N_steps*20./100.), false, 0., 0.5, 0.);
	sampler.step(int(N_steps*5./100.), false, 0., 1., 0.);
	sampler.step(int(N_steps*20./100.), false, 0., 0.5, 0.);
	sampler.step(int(N_steps*5./100.), false, 0., 1., 0.);
	sampler.step(int(N_steps*20./100.), false, 0., 0.5, 0.);
	sampler.step(int(N_steps*5./100), false, 0., 1., 0.);
	//sampler.step(N_steps, false, 0., options.p_replacement, 0.);
	//sampler.step(N_steps/2., false, 0., 1., 0.);
	sampler.print_stats();
	sampler.clear();
	
	std::cerr << "# Main run ..." << std::endl;
	bool converged = false;
	size_t attempt;
	for(attempt = 0; (attempt < max_attempts) && (!converged); attempt++) {
		sampler.step((1<<attempt)*N_steps, true, 0., options.p_replacement, 0.);
		
		std::cout << std::endl << "Transformed G-R Diagnostic:";
		sampler.calc_GR_transformed(GR_transf, &transf);
		for(unsigned int k=0; k<ndim; k++) {
			std::cout << "  " << std::setprecision(3) << GR_transf[k];
		}
		std::cout << std::endl << std::endl;
		
		converged = true;
		for(size_t i=0; i<ndim; i++) {
			if(GR_transf[i] > GR_threshold) {
				converged = false;
				if(attempt != max_attempts-1) {
					sampler.print_stats();
					std::cerr << "# Extending run ..." << std::endl;
					sampler.step(int(N_steps*1./5.), false, 0., 1., 0.);
					sampler.clear();
					//logger.clear();
				}
				break;
			}
		}
	}
	
	clock_gettime(CLOCK_MONOTONIC, &t_write);
	
	//std::stringstream group_name;
	//group_name << "/pixel " << healpix_index;
	//group_name << "/los clouds";
	//chain.save(out_fname, group_name.str(), 0, "Delta mu, Delta E(B-V)", 3, 100, converged);
	
	std::stringstream group_name;
	group_name << "/pixel " << healpix_index;
	TChain chain = sampler.get_chain();
	
	TChainWriteBuffer writeBuffer(ndim, 100, 1);
	writeBuffer.add(chain, converged);
	writeBuffer.write(out_fname, group_name.str(), "clouds");
	
	clock_gettime(CLOCK_MONOTONIC, &t_end);
	
	sampler.print_stats();
	std::cout << std::endl;
	
	
	
	if(!converged) {
		std::cerr << "# Failed to converge." << std::endl;
	}
	std::cerr << "# Number of steps: " << (1<<(attempt-1))*N_steps << std::endl;
	std::cerr << "# Time elapsed: " << std::setprecision(2) << (t_end.tv_sec - t_start.tv_sec) + 1.e-9*(t_end.tv_nsec - t_start.tv_nsec) << " s" << std::endl;
	std::cerr << "# Sample time: " << std::setprecision(2) << (t_write.tv_sec - t_start.tv_sec) + 1.e-9*(t_write.tv_nsec - t_start.tv_nsec) << " s" << std::endl;
	std::cerr << "# Write time: " << std::setprecision(2) << (t_end.tv_sec - t_write.tv_sec) + 1.e-9*(t_end.tv_nsec - t_write.tv_nsec) << " s" << std::endl << std::endl;
}
	bool PhysicsGeometry::load(FS::IFile& file)
	{
		Header header;
		file.read(&header, sizeof(header));
		if (header.m_magic != HEADER_MAGIC || header.m_version > (uint32_t)Versions::LAST)
		{
			return false;
		}

		auto* phy_manager = m_resource_manager.get(ResourceManager::PHYSICS);
		PhysicsSystem& system = static_cast<PhysicsGeometryManager*>(phy_manager)->getSystem();

		uint32_t num_verts;
		Array<Vec3> verts(getAllocator());
		file.read(&num_verts, sizeof(num_verts));
		verts.resize(num_verts);
		file.read(&verts[0], sizeof(verts[0]) * verts.size());

		m_is_convex = header.m_convex != 0;
		if (!m_is_convex)
		{
			physx::PxTriangleMeshGeometry* geom =
				getAllocator().newObject<physx::PxTriangleMeshGeometry>();
			m_geometry = geom;
			uint32_t num_indices;
			Array<uint32_t> tris(getAllocator());
			file.read(&num_indices, sizeof(num_indices));
			tris.resize(num_indices);
			file.read(&tris[0], sizeof(tris[0]) * tris.size());

			physx::PxTriangleMeshDesc meshDesc;
			meshDesc.points.count = num_verts;
			meshDesc.points.stride = sizeof(physx::PxVec3);
			meshDesc.points.data = &verts[0];

			meshDesc.triangles.count = num_indices / 3;
			meshDesc.triangles.stride = 3 * sizeof(physx::PxU32);
			meshDesc.triangles.data = &tris[0];

			OutputStream writeBuffer(getAllocator());
			system.getCooking()->cookTriangleMesh(meshDesc, writeBuffer);

			InputStream readBuffer(writeBuffer.data, writeBuffer.size);
			geom->triangleMesh = system.getPhysics()->createTriangleMesh(readBuffer);
		}
		else
		{
			physx::PxConvexMeshGeometry* geom =
				getAllocator().newObject<physx::PxConvexMeshGeometry>();
			m_geometry = geom;
			physx::PxConvexMeshDesc meshDesc;
			meshDesc.points.count = verts.size();
			meshDesc.points.stride = sizeof(Vec3);
			meshDesc.points.data = &verts[0];
			meshDesc.flags = physx::PxConvexFlag::eCOMPUTE_CONVEX;

			OutputStream writeBuffer(getAllocator());
			bool status = system.getCooking()->cookConvexMesh(meshDesc, writeBuffer);
			if (!status)
			{
				getAllocator().deleteObject(geom);
				m_geometry = nullptr;
				return false;
			}

			InputStream readBuffer(writeBuffer.data, writeBuffer.size);
			physx::PxConvexMesh* mesh = system.getPhysics()->createConvexMesh(readBuffer);
			geom->convexMesh = mesh;
		}

		m_size = file.size();
		return true;
	}
Example #13
0
void st7565Refresh(void)
{
  writeBuffer(buffer);
}
 /**
  * flush the internal buffer but not the file stream
  **/
 void shallowFlush()
 {
         writeBuffer();
 }
Example #15
0
/* If bidir, also copy data from printer (lp) to network (fd). */
int copy_stream(int fd, int lp)
{
	int result;
	Buffer_t networkToPrinterBuffer;
	initBuffer(&networkToPrinterBuffer, fd, lp, 1);

	if (bidir) {
		struct timeval now;
		struct timeval then;
		struct timeval timeout;
		int timer = 0;
		Buffer_t printerToNetworkBuffer;
		initBuffer(&printerToNetworkBuffer, lp, fd, 0);
		fd_set readfds;
		fd_set writefds;
		/* Initially read from both streams, don't write to either. */
		FD_ZERO(&readfds);
		FD_ZERO(&writefds);
		FD_SET(lp, &readfds);
		FD_SET(fd, &readfds);
		/* Finish when no longer reading fd, and no longer writing to lp. */
		/* Although the printer to network stream may not be finished, that does not matter. */
		while ((FD_ISSET(fd, &readfds)) || (FD_ISSET(lp, &writefds))) {
			int maxfd = lp > fd ? lp : fd;
			if (timer) {
				/* Delay after reading from the printer, so the */
				/* return stream cannot dominate. */
				/* Don't read from the printer until the timer expires. */
				gettimeofday(&now, 0);
				if ((now.tv_sec > then.tv_sec) || (now.tv_sec == then.tv_sec && now.tv_usec > then.tv_usec)) {
					timer = 0;
				} else {
					timeout.tv_sec = then.tv_sec - now.tv_sec;
					timeout.tv_usec = then.tv_usec - now.tv_usec;
					if (timeout.tv_usec < 0) {
						timeout.tv_usec += 1000000;
						timeout.tv_sec--;
					}
					FD_CLR(lp, &readfds);
				}
			}
			if (timer) {
				result = select(maxfd + 1, &readfds, &writefds, 0, &timeout);
			} else {
				result = select(maxfd + 1, &readfds, &writefds, 0, 0);
			}
			if (result < 0)
				return (result);
			if (FD_ISSET(fd, &readfds)) {
				/* Read network data. */
				result = readBuffer(&networkToPrinterBuffer);
			}
			if (FD_ISSET(lp, &readfds)) {
				/* Read printer data, but pace it more slowly. */
				result = readBuffer(&printerToNetworkBuffer);
				if (result >= 0) {
					gettimeofday(&then, 0);
					// wait 100 msec before reading again.
					then.tv_usec += 100000;
					if (then.tv_usec > 1000000) {
						then.tv_usec -= 1000000;
						then.tv_sec++;
					}
					timer = 1;
				}
			}
			if (FD_ISSET(lp, &writefds)) {
				/* Write data to printer. */
				result = writeBuffer(&networkToPrinterBuffer);
			}
			if (FD_ISSET(fd, &writefds)) {
				/* Write data to network. */
				result = writeBuffer(&printerToNetworkBuffer);
				/* If socket write error, stop reading from printer */
				if (result < 0)
					networkToPrinterBuffer.eof = 1;
			}
			/* Prepare for next iteration. */
			FD_ZERO(&readfds);
			FD_ZERO(&writefds);
			prepBuffer(&networkToPrinterBuffer, &readfds, &writefds);
			prepBuffer(&printerToNetworkBuffer, &readfds, &writefds);
		}
		syslog(LOG_NOTICE,
		       "Finished job: %d bytes received, %d bytes sent\n",
		       networkToPrinterBuffer.totalout, printerToNetworkBuffer.totalout);
		return (0);
	} else {
		/* Unidirectional: simply read from network, and write to printer. */
		while ((result = readBuffer(&networkToPrinterBuffer)) > 0) {
			(void)writeBuffer(&networkToPrinterBuffer);
		}
		syslog(LOG_NOTICE, "Finished job: %d bytes received\n", networkToPrinterBuffer.totalout);
		return (result);
	}
}
Example #16
0
			void put(data_type const c)
			{
				*(pc++) = c;
				if ( pc == pe )
					writeBuffer();
			}
Example #17
0
int main(int argc, char* argv[]) {
	if(argc != 2) {
		printf("Usage: %s [trace file]\n",argv[0]);
		return 1;
	}

	int hitTimeL1 = 1; 
	int accessTimeL2 = 20; 
	int accessTimeMem = 50; 

	int lineSizeL1 = 16;
	int assocL1 = 2; 
	int totalSizeL1 = 32; 

	int lineSizeL2 = 32; 
	int assocL2 = 8; 
	int totalSizeL2 = 256; 

	int numSetsL1, numSetsL2;


	u_int32_t addr, cycles;
	u_int32_t runtime = 0; // number of cycles in runtime
	u_int32_t nonmemrt = 0; // number of cycles in runtime spent with non-mem instructions

	FILE *fp; // to write out the final stats

	// calc number of sets (if assoc == 0 then it's fully assoc so there is only 1 set)
	if(assocL1 != 0) numSetsL1 = totalSizeL1 * 1024 / (assocL1 * lineSizeL1);
	else {
		numSetsL1 = 1;
		assocL1 = totalSizeL1 * 1024 / lineSizeL1;
	}

	if(assocL2 != 0) numSetsL2 = totalSizeL2 * 1024 / (assocL2 * lineSizeL2);
	else {
		numSetsL2 = 1;
		assocL2 = totalSizeL2 * 1024 / lineSizeL2;
	}

	// D-cache is write through with no-write-alloc, LRU replacement
	Cache DCache(numSetsL1,assocL1,lineSizeL1,false,false,true);

	// L2 cache is writeback with write-alloc, LRU replacement
	Cache L2Cache(numSetsL2,assocL2,lineSizeL2,false,true,false);

	CPU cpu(argv[1]);

	Prefetcher pf;

	memQueue writeBuffer(10,&DCache,accessTimeL2,true,true,'a');
	memQueue queueL2(20,&DCache,accessTimeL2,true,false,'b');
	memQueue queueMem(10,&L2Cache,accessTimeMem,false,false,'c');


	// statistical stuff
	u_int32_t nRequestsL2 = 0; // number of requests sent out to L2 (both CPU and prefetcher requests)
	u_int32_t memCycles = 0; // number of cycles that main memory is being accessed
	u_int32_t memQsize = 0; // used for calculating average queue length

	u_int32_t curr_cycle = 1;
	Request req;
	bool isHit;

	while(!cpu.isDone()) {

		isHit = false;

		cpuState cpu_status = cpu.getStatus(curr_cycle);

//		printf("%u: %u\n",curr_cycle,cpu_status);

		if(cpu_status == READY) { // request is ready
				
			req = cpu.issueRequest(curr_cycle);

			// check for L1 hit
			isHit = DCache.check(req.addr,req.load);
			cpu.hitL1(isHit);
			req.HitL1 = isHit;

			// notify the prefetcher of what just happened with this memory op
			pf.cpuRequest(req);

			if(isHit) {
				DCache.access(req.addr,req.load);
				cpu.completeRequest(curr_cycle);
			}
			else if(req.load) {
				nRequestsL2++;
				if(queueL2.add(req,curr_cycle)) cpu.setStatus(WAITING); // CPU is now "waiting" for response from L2/mem
				else cpu.setStatus(STALLED_L2); // no room in l2 queue so we are "stalled" on this request
			}
			else { 
				nRequestsL2++;

				if (writeBuffer.add(req,curr_cycle)) cpu.completeRequest(curr_cycle); 
				else { // need to stall for an entry in the write buffer to open up
					cpu.setStatus(STALLED_WB);
				}
			}
		}
		// PF can do some work if we are just waiting or idle OR if we had a hit in the D-cache so the D-to-L2 bus isn't needed
		else if(cpu_status == WAITING || cpu_status == IDLE || cpu_status == STALLED_WB || isHit) { // either waiting for lower mem levels or idle so PF can do something
			if(pf.hasRequest(curr_cycle)) { 
				nRequestsL2++;

				req = pf.getRequest(curr_cycle);
				req.fromCPU = false;
				req.load = true;
				if(queueL2.add(req,curr_cycle)) pf.completeRequest(curr_cycle); // if added to queue then the request is "complete"
			}

			if(cpu_status == STALLED_WB) { // attempt to put it in the write buffer
				req = cpu.getRequest(); // get the request we want

				if (writeBuffer.add(req,curr_cycle)) cpu.completeRequest(curr_cycle); // if added, we can move on
			}
		}
		else if(cpu_status == STALLED_L2) { // stalled b/c of L2 queue so let us just try this right away
			req = cpu.getRequest();
			if(queueL2.add(req,curr_cycle)) cpu.setStatus(WAITING); // l2 queue is free now so we can go into waiting state
		}

		// service the L2 queue
		if(queueL2.frontReady(curr_cycle)) { // check to see if the front element in the queue is ready
			//printf("servicing the l2 queue on cycle %u\n",curr_cycle);
			req = queueL2.getFront();

			isHit = L2Cache.check(req.addr,req.load);
			cpu.loadHitL2(isHit);

			if(isHit) {
				DCache.access(req.addr,req.load); // update D cache
				if(req.fromCPU) cpu.completeRequest(curr_cycle); // this request was from the CPU so update state to show we are done
				queueL2.remove(); // remove this request from the queue
			}
			else {
				if(queueMem.add(req,curr_cycle)) queueL2.remove(); // succesfully added to memory queue so we can remove it from L2 queue
			}
		}

		// service the memory queue
		if(queueMem.frontReady(curr_cycle)) {
			//printf("servicing the mem queue on cycle %u\n",curr_cycle);
			req = queueMem.getFront();
			queueMem.remove();

			// update both L2 and D cache
			L2Cache.access(req.addr,req.load);
			if(req.load) DCache.access(req.addr,req.load); // only update if this is a load

			if(req.fromCPU && req.load) cpu.completeRequest(curr_cycle);
		}

		// check to see if we are utilizing memory BW during this cycle
		if(queueMem.getSize() > 0) memCycles++;

		// used to find the average size of the memory queue
		memQsize += queueMem.getSize();

		// service the write buffer
		if(writeBuffer.frontReady(curr_cycle)) {
			req = writeBuffer.getFront();

			isHit = L2Cache.check(req.addr,req.load);
			cpu.storeHitL2(isHit);

			if(isHit) { // store hit in L2 so just save it and we are done
				L2Cache.access(req.addr,req.load);
				writeBuffer.remove();
			}
			else { // L2 is write-allocate so we need to load data from memory first
				if(queueMem.add(req,curr_cycle)) writeBuffer.remove(); // we can keep adding to the queue because we check for duplicates as part of add()
			}
		}

		curr_cycle++; // next cycle
	}

	curr_cycle--; // just for stats sake

	double avgMemQ = (double)memQsize / (double)curr_cycle;
	double L2BW = (double)nRequestsL2 / (double)curr_cycle;
	double memBW = (double)memCycles / (double)curr_cycle;

	/*
	fprintf("total run time: %u\n",curr_cycle);
	fprintf("D-cache total hit rate: %f\n",cpu.getHitRateL1());
	fprintf("L2 cache total hit rate: %f\n",cpu.getHitRateL2());
	fprintf("AMAT: %f\n",cpu.getAMAT());
	fprintf("Average Memory Queue Size: %f\n",avgMemQ);
	fprintf("L2 BW Utilization: %f\n",L2BW);
	fprintf("Memory BW Utilization: %f\n",memBW);
	*/


	// create output file name based on trace file name
	char* outfile = (char *)malloc(sizeof(char)*(strlen(argv[1])+5));
	strcpy(outfile,argv[1]);
	strcat(outfile,".out");

	fp = fopen(outfile,"w"); // open outfile for writing

	free(outfile);

	fprintf(fp,"%u\n",curr_cycle);
	fprintf(fp,"%.4f\n",cpu.getHitRateL1());
	fprintf(fp,"%.4f\n",cpu.getHitRateL2());
	fprintf(fp,"%.4f\n",cpu.getAMAT());
	fprintf(fp,"%.4f\n",avgMemQ);
	fprintf(fp,"%.4f\n",L2BW);
	fprintf(fp,"%.4f\n",memBW);

	fclose(fp);

	return 0;
}
void parse_note ( Creature *ch, const char *argument, int type )
{
	BUFFER *buffer;
	char buf[MAX_STRING_LENGTH];
	char arg[MAX_INPUT_LENGTH];
	NOTE_DATA *pnote;
	NOTE_DATA **list;
	const char *list_name;
	int vnum;
	int anum;

	if ( IS_NPC ( ch ) )
	{ return; }

	switch ( type ) {
		default:
			return;
		case NOTE_NOTE:
			list = &note_list;
			list_name = "notes";
			break;
		case NOTE_IDEA:
			list = &idea_list;
			list_name = "ideas";
			break;
		case NOTE_PENALTY:
			list = &penalty_list;
			list_name = "penalties";
			break;
		case NOTE_NEWS:
			list = &news_list;
			list_name = "news";
			break;
		case NOTE_CHANGES:
			list = &changes_list;
			list_name = "changes";
			break;
	}

	argument = ChopC ( argument, arg );
	smash_tilde ( argument );

	if ( arg[0] == '\0' || !str_prefix ( arg, "read" ) ) {
		bool fAll;

		if ( SameString ( argument, "all" ) ) {
			fAll = TRUE;
			anum = 0;
		}

		else if ( argument[0] == '\0' || !str_prefix ( argument, "next" ) )
			/* read next unread note */
		{
			vnum = 0;
			for ( pnote = *list; pnote != NULL; pnote = pnote->next ) {
				if ( !hide_note ( ch, pnote ) ) {
					sprintf ( buf, "[%3d] %s: %s\n\r%s\n\rTo: %s\n\r",
							  vnum,
							  pnote->sender,
							  pnote->subject,
							  pnote->date,
							  pnote->to_list );
					writeBuffer ( buf, ch );
					writePage ( pnote->text, ch );
					update_read ( ch, pnote );
					return;
				} else if ( is_note_to ( ch, pnote ) )
				{ vnum++; }
			}
			snprintf ( buf, sizeof ( buf ), "You have no unread %s.\n\r", list_name );
			writeBuffer ( buf, ch );
			return;
		}

		else if ( is_number ( argument ) ) {
			fAll = FALSE;
			anum = atoi ( argument );
		} else {
			writeBuffer ( "Read which number?\n\r", ch );
			return;
		}

		vnum = 0;
		for ( pnote = *list; pnote != NULL; pnote = pnote->next ) {
			if ( is_note_to ( ch, pnote ) && ( vnum++ == anum || fAll ) ) {
				sprintf ( buf, "[%3d] %s: %s\n\r%s\n\rTo: %s\n\r",
						  vnum - 1,
						  pnote->sender,
						  pnote->subject,
						  pnote->date,
						  pnote->to_list
						);
				writeBuffer ( buf, ch );
				writePage ( pnote->text, ch );
				update_read ( ch, pnote );
				return;
			}
		}

		snprintf ( buf, sizeof ( buf ), "There aren't that many %s.\n\r", list_name );
		writeBuffer ( buf, ch );
		return;
	}

	if ( !str_prefix ( arg, "list" ) ) {
		vnum = 0;
		for ( pnote = *list; pnote != NULL; pnote = pnote->next ) {
			if ( is_note_to ( ch, pnote ) ) {
				sprintf ( buf, "[%3d%s] %s: %s\n\r",
						  vnum, hide_note ( ch, pnote ) ? " " : "N",
						  pnote->sender, pnote->subject );
				writeBuffer ( buf, ch );
				vnum++;
			}
		}
		if ( !vnum ) {
			switch ( type ) {
				case NOTE_NOTE:
					writeBuffer ( "There are no notes for you.\n\r", ch );
					break;
				case NOTE_IDEA:
					writeBuffer ( "There are no ideas for you.\n\r", ch );
					break;
				case NOTE_PENALTY:
					writeBuffer ( "There are no penalties for you.\n\r", ch );
					break;
				case NOTE_NEWS:
					writeBuffer ( "There is no news for you.\n\r", ch );
					break;
				case NOTE_CHANGES:
					writeBuffer ( "There are no changes for you.\n\r", ch );
					break;
			}
		}
		return;
	}

	if ( !str_prefix ( arg, "remove" ) ) {
		if ( !is_number ( argument ) ) {
			writeBuffer ( "Note remove which number?\n\r", ch );
			return;
		}

		anum = atoi ( argument );
		vnum = 0;
		for ( pnote = *list; pnote != NULL; pnote = pnote->next ) {
			if ( is_note_to ( ch, pnote ) && vnum++ == anum ) {
				note_remove ( ch, pnote, FALSE );
				writeBuffer ( "Ok.\n\r", ch );
				return;
			}
		}

		snprintf ( buf, sizeof ( buf ), "There aren't that many %s.", list_name );
		writeBuffer ( buf, ch );
		return;
	}

	if ( !str_prefix ( arg, "delete" ) && get_trust ( ch ) >= MAX_LEVEL - 1 ) {
		if ( !is_number ( argument ) ) {
			writeBuffer ( "Note delete which number?\n\r", ch );
			return;
		}

		anum = atoi ( argument );
		vnum = 0;
		for ( pnote = *list; pnote != NULL; pnote = pnote->next ) {
			if ( is_note_to ( ch, pnote ) && vnum++ == anum ) {
				note_remove ( ch, pnote, TRUE );
				writeBuffer ( "Ok.\n\r", ch );
				return;
			}
		}

		snprintf ( buf, sizeof ( buf ), "There aren't that many %s.", list_name );
		writeBuffer ( buf, ch );
		return;
	}

	if ( !str_prefix ( arg, "catchup" ) ) {
		switch ( type ) {
			case NOTE_NOTE:
				ch->pcdata->last_note = current_time;
				break;
			case NOTE_IDEA:
				ch->pcdata->last_idea = current_time;
				break;
			case NOTE_PENALTY:
				ch->pcdata->last_penalty = current_time;
				break;
			case NOTE_NEWS:
				ch->pcdata->last_news = current_time;
				break;
			case NOTE_CHANGES:
				ch->pcdata->last_changes = current_time;
				break;
		}
		return;
	}

	/* below this point only certain people can edit notes */
	if ( ( type == NOTE_NEWS && !IS_TRUSTED ( ch, ANGEL ) )
			||  ( type == NOTE_CHANGES && !IS_TRUSTED ( ch, CREATOR ) ) ) {
		snprintf ( buf, sizeof ( buf ), "You aren't high enough level to write %s.", list_name );
		writeBuffer ( buf, ch );
		return;
	}

	if ( SameString ( arg, "+" ) ) {
		note_attach ( ch, type );
		if ( ch->pnote->type != type ) {
			writeBuffer (
				"You already have a different note in progress.\n\r", ch );
			return;
		}

		if ( strlen ( ch->pnote->text ) + strlen ( argument ) >= 4096 ) {
			writeBuffer ( "Note too long.\n\r", ch );
			return;
		}

		buffer = new_buf();

		add_buf ( buffer, ch->pnote->text );
		add_buf ( buffer, argument );
		add_buf ( buffer, "\n\r" );
		PURGE_DATA ( ch->pnote->text );
		ch->pnote->text = assign_string ( buf_string ( buffer ) );
		recycle_buf ( buffer );
		writeBuffer ( "Ok.\n\r", ch );
		return;
	}

	if ( SameString ( arg, "-" ) ) {
		int len;
		bool found = FALSE;

		note_attach ( ch, type );
		if ( ch->pnote->type != type ) {
			writeBuffer (
				"You already have a different note in progress.\n\r", ch );
			return;
		}

		if ( ch->pnote->text == NULL || ch->pnote->text[0] == '\0' ) {
			writeBuffer ( "No lines left to remove.\n\r", ch );
			return;
		}

		strcpy ( buf, ch->pnote->text );

		for ( len = strlen ( buf ); len > 0; len-- ) {
			if ( buf[len] == '\r' ) {
				if ( !found ) { /* back it up */
					if ( len > 0 )
					{ len--; }
					found = TRUE;
				} else { /* found the second one */
					buf[len + 1] = '\0';
					PURGE_DATA ( ch->pnote->text );
					ch->pnote->text = assign_string ( buf );
					return;
				}
			}
		}
		buf[0] = '\0';
		PURGE_DATA ( ch->pnote->text );
		ch->pnote->text = assign_string ( buf );
		return;
	}

	if ( !str_prefix ( arg, "subject" ) ) {
		note_attach ( ch, type );
		if ( ch->pnote->type != type ) {
			writeBuffer (
				"You already have a different note in progress.\n\r", ch );
			return;
		}

		PURGE_DATA ( ch->pnote->subject );
		ch->pnote->subject = assign_string ( argument );
		writeBuffer ( "Ok.\n\r", ch );
		return;
	}

	if ( !str_prefix ( arg, "to" ) ) {
		note_attach ( ch, type );
		if ( ch->pnote->type != type ) {
			writeBuffer (
				"You already have a different note in progress.\n\r", ch );
			return;
		}
		PURGE_DATA ( ch->pnote->to_list );
		ch->pnote->to_list = assign_string ( argument );
		writeBuffer ( "Ok.\n\r", ch );
		return;
	}

	if ( !str_prefix ( arg, "clear" ) ) {
		if ( ch->pnote != NULL ) {
			recycle_note ( ch->pnote );
			ch->pnote = NULL;
		}

		writeBuffer ( "Ok.\n\r", ch );
		return;
	}

	if ( !str_prefix ( arg, "show" ) ) {
		if ( ch->pnote == NULL ) {
			writeBuffer ( "You have no note in progress.\n\r", ch );
			return;
		}

		if ( ch->pnote->type != type ) {
			writeBuffer ( "You aren't working on that kind of note.\n\r", ch );
			return;
		}

		sprintf ( buf, "%s: %s\n\rTo: %s\n\r",
				  ch->pnote->sender,
				  ch->pnote->subject,
				  ch->pnote->to_list
				);
		writeBuffer ( buf, ch );
		writeBuffer ( ch->pnote->text, ch );
		return;
	}

	if ( !str_prefix ( arg, "post" ) || !str_prefix ( arg, "send" ) ) {
		char *strtime;

		if ( ch->pnote == NULL ) {
			writeBuffer ( "You have no note in progress.\n\r", ch );
			return;
		}

		if ( ch->pnote->type != type ) {
			writeBuffer ( "You aren't working on that kind of note.\n\r", ch );
			return;
		}

		if ( SameString ( ch->pnote->to_list, "" ) ) {
			writeBuffer (
				"You need to provide a recipient (name, all, or immortal).\n\r",
				ch );
			return;
		}

		if ( SameString ( ch->pnote->subject, "" ) ) {
			writeBuffer ( "You need to provide a subject.\n\r", ch );
			return;
		}

		ch->pnote->next			= NULL;
		strtime				= ctime ( &current_time );
		strtime[strlen ( strtime ) - 1]	= '\0';
		ch->pnote->date			= assign_string ( strtime );
		ch->pnote->date_stamp		= current_time;

		append_note ( ch->pnote );
		ch->pnote = NULL;
		return;
	}

	writeBuffer ( "You can't do that.\n\r", ch );
	return;
}
//
// 汇报线程
//
DWORD WINAPI CGameServer::ReportThread(LPVOID lpParam)
{
	if (CGameServer *pServer = (CGameServer *)lpParam) {
		SOCKET sock = INVALID_SOCKET;

		while (WAIT_OBJECT_0 != WaitForSingleObject(pServer->m_hShutdownEvent, 0)) {
			Sleep(10 * 1000);

			int rcode = NO_ERROR;

			BYTE buffer[PACK_BUFFER_SIZE];
			CCacheBuffer writeBuffer(sizeof(buffer), buffer);
			ProtoGameServer::ServerStatus requestServerStatus;

			//
			// 1. 链接网关服务器
			//
			if (sock == INVALID_SOCKET) {
				sockaddr_in sockAddr;
				sockAddr.sin_family = AF_INET;
				sockAddr.sin_addr.s_addr = inet_addr(pServer->m_szGateServerIP);
				sockAddr.sin_port = htons(pServer->m_nGateServerPort);

				sock = socket(AF_INET, SOCK_STREAM, 0);
				if (sock == INVALID_SOCKET) goto RETRY;

				rcode = connect(sock, (const struct sockaddr *)&sockAddr, sizeof(sockAddr));
				if (rcode != NO_ERROR) goto RETRY;
			}

			//
			// 2. 向网关服务器报告当前游戏列表
			//
			EnterCriticalSection(&pServer->m_sectionContext);
			{
				requestServerStatus.set_ip(pServer->m_ip);
				requestServerStatus.set_port(pServer->m_port);
				requestServerStatus.set_curgames(pServer->m_curGames);
				requestServerStatus.set_maxgames(pServer->m_maxGames);

				for (int index = 0; index < pServer->m_maxGames; index++) {
					if (const CGame *pGame = pServer->m_games[index]) {
//						if ((pGame->IsEmpty() == TRUE) || 
//							(pGame->IsFull() == FALSE && pGame->IsPrivate() == FALSE)) {
						if ((pGame->IsEmpty() == TRUE)) {
							ProtoGameServer::ServerStatus_Game *pGameStatus = requestServerStatus.add_games();
							pGameStatus->set_empty(pGame->IsEmpty() ? true : false);
							pGameStatus->set_gameid(pGame->id);
							pGameStatus->set_mode(pGame->GetMode());
							pGameStatus->set_mapid(pGame->GetMapID());
							pGameStatus->set_evaluation(pGame->GetEvaluation());
						}
					}
				}
			}
			LeaveCriticalSection(&pServer->m_sectionContext);

			Serializer(&writeBuffer, &requestServerStatus, ProtoGameServer::REQUEST_MSG::SERVER_STATUS);

			rcode = SendData((int)sock, (char *)buffer, (int)writeBuffer.GetActiveBufferSize());
			if (rcode < 0) goto RETRY;

			continue;

			//
			// 3. 断线重连
			//
		RETRY:
			if (sock != INVALID_SOCKET) {
				shutdown(sock, SD_BOTH);
				closesocket(sock);
				sock = INVALID_SOCKET;
			}
		}

		if (sock != INVALID_SOCKET) {
			shutdown(sock, SD_BOTH);
			closesocket(sock);
			sock = INVALID_SOCKET;
		}
	}

	return 0L;
}
Example #20
0
boost::uint64_t Writer::write(boost::uint64_t targetNumPointsToWrite,
                              boost::uint64_t startingPosition,
                              boost::uint64_t chunkSize)
{
    if (!isInitialized())
    {
        throw pdal_error("stage not initialized");
    }

    boost::uint64_t actualNumPointsWritten = 0;

    UserCallback* callback = getUserCallback();
    do_callback(0.0, callback);

    const Schema& schema = getPrevStage().getSchema();

    if (m_writer_buffer == 0)
    {
        boost::uint64_t capacity(targetNumPointsToWrite);
        if (capacity == 0)
        {
            if (chunkSize)
                capacity = chunkSize;
            else
                capacity = 131072;
        }
        else
        {
            if (chunkSize)
                capacity = (std::min)(static_cast<boost::uint64_t>(chunkSize), targetNumPointsToWrite) ;
            else
                capacity = targetNumPointsToWrite;
        }

        if (capacity > std::numeric_limits<boost::uint32_t>::max())
            throw pdal_error("Buffer capacity is larger than 2^32 points!");
        m_writer_buffer = new PointBuffer(schema, static_cast<boost::uint32_t>(capacity));

    }

    boost::scoped_ptr<StageSequentialIterator> iter(getPrevStage().createSequentialIterator(*m_writer_buffer));

    if (startingPosition)
        iter->skip(startingPosition);

    if (!iter) throw pdal_error("Unable to obtain iterator from previous stage!");

    // if we don't have an SRS, try to forward the one from the prev stage
    if (m_spatialReference.empty()) m_spatialReference = getPrevStage().getSpatialReference();

    writeBegin(targetNumPointsToWrite);

    iter->readBegin();



    //
    // The user has requested a specific number of points: proceed a
    // chunk at a time until we reach that number.  (If that number
    // is 0, we proceed until no more points can be read.)
    //
    // If the user requests an interrupt while we're running, we'll throw.
    //
    while (true)
    {
        // have we hit the end already?
        if (iter->atEnd()) break;

        // rebuild our PointBuffer, if it needs to hold less than the default max chunk size
        if (targetNumPointsToWrite != 0)
        {
            const boost::int64_t numRemainingPointsToRead = targetNumPointsToWrite - actualNumPointsWritten;

            const boost::int64_t numPointsToReadThisChunk64 = std::min<boost::int64_t>(numRemainingPointsToRead, chunkSize == 0 ? numRemainingPointsToRead : chunkSize);
            const boost::uint32_t numPointsToReadThisChunk = static_cast<boost::uint32_t>(numPointsToReadThisChunk64);

            // we are reusing the buffer, so we may need to adjust the capacity for the last (and likely undersized) chunk
            if (m_writer_buffer->getCapacity() != numPointsToReadThisChunk)
            {
                m_writer_buffer->resize(numPointsToReadThisChunk);
            }
        }

        // read...
        iter->readBufferBegin(*m_writer_buffer);
        const boost::uint32_t numPointsReadThisChunk = iter->readBuffer(*m_writer_buffer);
        iter->readBufferEnd(*m_writer_buffer);

        assert(numPointsReadThisChunk == m_writer_buffer->getNumPoints());
        assert(numPointsReadThisChunk <= m_writer_buffer->getCapacity());
        
        // Some drivers may do header setups and such, even for situations 
        // where there's no points, so we should a buffer begin. 
        writeBufferBegin(*m_writer_buffer);
        
        // were there no points left to write this chunk?
        if (numPointsReadThisChunk == 0) 
        {
            // Match the above writeBufferBegin now that 
            // we're breaking the loop
            writeBufferEnd(*m_writer_buffer);
            break;
        }

        // write...
        const boost::uint32_t numPointsWrittenThisChunk = writeBuffer(*m_writer_buffer);
        assert(numPointsWrittenThisChunk == numPointsReadThisChunk);
        writeBufferEnd(*m_writer_buffer);

        // update count
        actualNumPointsWritten += numPointsWrittenThisChunk;

        do_callback(actualNumPointsWritten, targetNumPointsToWrite, callback);

        if (targetNumPointsToWrite != 0)
        {
            // have we done enough yet?
            if (actualNumPointsWritten >= targetNumPointsToWrite) break;
        }

        // reset the buffer, so we can use it again
        m_writer_buffer->setNumPoints(0);
    }

    iter->readEnd();

    writeEnd(actualNumPointsWritten);

    assert((targetNumPointsToWrite == 0) || (actualNumPointsWritten <= targetNumPointsToWrite));

    do_callback(100.0, callback);


    return actualNumPointsWritten;
}
Example #21
0
void InvaderDetectOperation::operate( const std::vector<ImageBufferPtr>& inputList, const std::vector<double>&, ImageBufferPtr output ) {
	assert(inputList.size() == 2);
	min.clear();
	max.clear();

	CpuImage input = readBuffer(inputList[0]);
	Matrix<int> clusters( input.height(), input.width() );

	for(unsigned int y = 0; y < input.height(); y++)
	for(unsigned int x = 0; x < input.width(); x++) {
		clusters(y,x) = -1;
	}

	int lastCluster = 0;
	for(unsigned int y = 0; y < input.height(); y++ )
	for(unsigned int x = 0; x < input.width(); x++ ) {
		if( clusters(y,x).get() != -1 )
			continue;
		if( ! input(y,x).get().r ){
			clusters(y,x) = 0;
			continue;
		}
		clusters(y,x) = ++lastCluster;

		std::queue<Recursive> recurse;
		recurse.push(Recursive(y,x));

		while( !recurse.empty() ) {
			Recursive recursive = recurse.front();
			recurse.pop();
			int ry = recursive.ry;
			int rx = recursive.rx;

			for( int line = -1; line <= 1; line++){
			for( int column = -1; column <= 1; column++){
				if ( clusters(ry,rx).neighbour(line,column).get() == -1 && input(ry,rx).neighbour(line,column).get().r != 0 ){
					clusters(ry, rx).neighbour(line, column) = lastCluster;
					recurse.push( Recursive(ry+line, rx+column) );
				}
			}
			}	
		};
	}

	CpuImage backImage = readBuffer(inputList[1]);
	for( unsigned int y = 0; y < input.height(); y++)
	for( unsigned int x = 0; x < input.width(); x++) {
		maximize( backImage(y,x) , clusters(y,x).get() );
		minimize( backImage(y,x) , clusters(y,x).get() );
	}

	{
		auto minI = min.begin();
		auto maxI = max.begin();
		for( ; minI != min.end(); ++minI,++maxI) {
			if( minI->first == 0 )
				continue;

			int clusterWidth = maxI->second.first-minI->second.first;
			int clusterHeight = maxI->second.second-minI->second.second;
			int area = clusterHeight*clusterWidth;
			if( area < 500 )
				continue;
			double ratio = double(clusterHeight)/double(clusterWidth);
		//	std::cout << minI->first << ": (" << clusterWidth << "," << clusterHeight << "), ratio:" << ratio;
		//	std::cout << ", area:" << area << std::endl;

			int color = ratio > 1.2 ? 3 : 2;
			Point line = minI->second;
			for( ; line.second <= maxI->second.second; ++line.second) {
				colorize( backImage(line.second,line.first), color );
			}
			for( ; line.first <= maxI->second.first; ++line.first) {
				colorize( backImage(line.second,line.first), color );
			}
			for( ; line.second >= minI->second.second; --line.second) {
				colorize( backImage(line.second,line.first), color );
			}
			for( ; line.first >= minI->second.first; --line.first) {
				colorize( backImage(line.second,line.first), color );
			}
		}
	}

	writeBuffer(backImage,output);
}
 /*!  Transmit the GDB Tx buffer to GDB
  */
void GdbInOut::txGdbPkt(void) {
   writeBuffer(gdbTxBuffer, gdbTxCharCount);
   Logging::print( "txGdbPkt()=>:%3d%*s\n", gdbTxCharCount, gdbTxCharCount, gdbTxBuffer);
}
Example #23
0
boost::uint64_t Writer::write(  boost::uint64_t targetNumPointsToWrite,
                                boost::uint64_t startingPosition)
{
    if (!isInitialized())
    {
        throw pdal_error("stage not initialized");
    }

    boost::uint64_t actualNumPointsWritten = 0;

    UserCallback* callback = getUserCallback();
    do_callback(0.0, callback);

    const Schema& schema = getPrevStage().getSchema();
    PointBuffer buffer(schema, m_chunkSize);

    boost::scoped_ptr<StageSequentialIterator> iter(getPrevStage().createSequentialIterator(buffer));
    
    if (startingPosition)
        iter->skip(startingPosition);
        
    if (!iter) throw pdal_error("Unable to obtain iterator from previous stage!");

    // if we don't have an SRS, try to forward the one from the prev stage
    if (m_spatialReference.empty()) m_spatialReference = getPrevStage().getSpatialReference();

    writeBegin(targetNumPointsToWrite);

    iter->readBegin();



    //
    // The user has requested a specific number of points: proceed a
    // chunk at a time until we reach that number.  (If that number
    // is 0, we proceed until no more points can be read.)
    //
    // If the user requests an interrupt while we're running, we'll throw.
    //
    while (true)
    {
        // have we hit the end already?
        if (iter->atEnd()) break;

        // rebuild our PointBuffer, if it needs to hold less than the default max chunk size
        if (targetNumPointsToWrite != 0)
        {
            const boost::uint64_t numRemainingPointsToRead = targetNumPointsToWrite - actualNumPointsWritten;

            const boost::uint64_t numPointsToReadThisChunk64 = std::min<boost::uint64_t>(numRemainingPointsToRead, m_chunkSize);
            // this case is safe because m_chunkSize is a uint32
            const boost::uint32_t numPointsToReadThisChunk = static_cast<boost::uint32_t>(numPointsToReadThisChunk64);

            // we are reusing the buffer, so we may need to adjust the capacity for the last (and likely undersized) chunk
            if (buffer.getCapacity() != numPointsToReadThisChunk)
            {
                buffer.resize(numPointsToReadThisChunk);
            }
        }

        // read...
        iter->readBufferBegin(buffer);
        const boost::uint32_t numPointsReadThisChunk = iter->readBuffer(buffer);
        iter->readBufferEnd(buffer);

        assert(numPointsReadThisChunk == buffer.getNumPoints());
        assert(numPointsReadThisChunk <= buffer.getCapacity());

        // have we reached the end yet?
        if (numPointsReadThisChunk == 0) break;

        // write...
        writeBufferBegin(buffer);
        const boost::uint32_t numPointsWrittenThisChunk = writeBuffer(buffer);
        assert(numPointsWrittenThisChunk == numPointsReadThisChunk);
        writeBufferEnd(buffer);

        // update count
        actualNumPointsWritten += numPointsWrittenThisChunk;

        do_callback(actualNumPointsWritten, targetNumPointsToWrite, callback);

        if (targetNumPointsToWrite != 0)
        {
            // have we done enough yet?
            if (actualNumPointsWritten >= targetNumPointsToWrite) break;
        }

        // reset the buffer, so we can use it again
        buffer.setNumPoints(0);
    }

    iter->readEnd();

    writeEnd(actualNumPointsWritten);

    assert((targetNumPointsToWrite == 0) || (actualNumPointsWritten <= targetNumPointsToWrite));

    do_callback(100.0, callback);

    return actualNumPointsWritten;
}
 /*!  Send immediate ACK/NAK response
  *
  *   @param ackValue - Either '-' or '+' response to send
  */
void GdbInOut::sendAck(char ackValue) {
   Logging::print("sendAck()=>ack=%c\n", ackValue);
   writeBuffer((unsigned char *)&ackValue, 1);
}
Example #25
0
			/**
			 * put one element and flush buffer it is full afterwards
			 *
			 * @param c element to be put in buffer
			 **/
			void put(uint8_t const c)
			{
				*(pc++) = c;
				if ( pc == pe )
					writeBuffer();
			}
Example #26
0
void WSMSGridder::gridMeasurementSet(MSData &msData)
{
	const MultiBandData selectedBand(msData.SelectedBand());
	_gridder->PrepareBand(selectedBand);
	std::vector<std::complex<float>> modelBuffer(selectedBand.MaxChannels());
	std::vector<float> weightBuffer(selectedBand.MaxChannels());
	
	lane_write_buffer<InversionWorkItem> writeBuffer(&*_inversionWorkLane, 128);
	
	size_t rowsRead = 0;
	msData.msProvider->Reset();
	while(msData.msProvider->CurrentRowAvailable())
	{
		size_t dataDescId;
		double uInMeters, vInMeters, wInMeters;
		msData.msProvider->ReadMeta(uInMeters, vInMeters, wInMeters, dataDescId);
		const BandData& curBand(selectedBand[dataDescId]);
		const double
			w1 = wInMeters / curBand.LongestWavelength(),
			w2 = wInMeters / curBand.SmallestWavelength();
		if(_gridder->IsInLayerRange(w1, w2))
		{
			InversionWorkItem newItem;
			newItem.u = uInMeters;
			newItem.v = vInMeters;
			newItem.w = wInMeters;
			newItem.dataDescId = dataDescId;
			newItem.data = new std::complex<float>[curBand.ChannelCount()];
			
			if(DoImagePSF())
			{
				msData.msProvider->ReadWeights(newItem.data);
				if(_denormalPhaseCentre)
				{
					double lmsqrt = sqrt(1.0-_phaseCentreDL*_phaseCentreDL- _phaseCentreDM*_phaseCentreDM);
					double shiftFactor = 2.0*M_PI* (newItem.w * (lmsqrt-1.0));
					rotateVisibilities(curBand, shiftFactor, newItem.data);
				}
			}
			else {
				msData.msProvider->ReadData(newItem.data);
			}
			
			if(DoSubtractModel())
			{
				msData.msProvider->ReadModel(modelBuffer.data());
				std::complex<float>* modelIter = modelBuffer.data();
				for(std::complex<float>* iter = newItem.data; iter!=newItem.data+curBand.ChannelCount(); ++iter)
				{
					*iter -= *modelIter;
					modelIter++;
				}
			}
			msData.msProvider->ReadWeights(weightBuffer.data());
			switch(VisibilityWeightingMode())
			{
				case NormalVisibilityWeighting:
					// The MS provider has already preweighted the
					// visibilities for their weight, so we do not
					// have to do anything.
					break;
				case SquaredVisibilityWeighting:
					for(size_t ch=0; ch!=curBand.ChannelCount(); ++ch)
						newItem.data[ch] *= weightBuffer[ch];
					break;
				case UnitVisibilityWeighting:
					for(size_t ch=0; ch!=curBand.ChannelCount(); ++ch)
					{
						if(weightBuffer[ch] == 0.0)
							newItem.data[ch] = 0.0;
						else
							newItem.data[ch] /= weightBuffer[ch];
					}
					break;
			}
			switch(Weighting().Mode())
			{
				case WeightMode::UniformWeighted:
				case WeightMode::BriggsWeighted:
				case WeightMode::NaturalWeighted:
				{
					std::complex<float>* dataIter = newItem.data;
					float* weightIter = weightBuffer.data();
					for(size_t ch=0; ch!=curBand.ChannelCount(); ++ch)
					{
						double
							u = newItem.u / curBand.ChannelWavelength(ch),
							v = newItem.v / curBand.ChannelWavelength(ch),
							weight = PrecalculatedWeightInfo()->GetWeight(u, v);
						*dataIter *= weight;
						_totalWeight += weight * *weightIter;
						++dataIter;
						++weightIter;
					}
				} break;
				case WeightMode::DistanceWeighted:
				{
					float* weightIter = weightBuffer.data();
					double mwaWeight = sqrt(newItem.u*newItem.u + newItem.v*newItem.v + newItem.w*newItem.w);
					for(size_t ch=0; ch!=curBand.ChannelCount(); ++ch)
					{
						_totalWeight += *weightIter * mwaWeight;
						++weightIter;
					}
				} break;
			}
			
			writeBuffer.write(newItem);
			
			++rowsRead;
		}
		
		msData.msProvider->NextRow();
	}
	
	if(Verbose())
		std::cout << "Rows that were required: " << rowsRead << '/' << msData.matchingRows << '\n';
	msData.totalRowsProcessed += rowsRead;
}
Example #27
0
int main (int argc, char *argv[])
{
    /*data--------- */
    int *Geno;                    /*NUMINDSxLINES: genotypes */
    float *R;                    /*NUMINDS */
    float *Mapdistance;          /*NUMLOCI */
    float *Phase;                /*NUMLOCI*NUMINDS */
    int *Phasemodel=NULL;         /*NUMINDS */
    char *Markername;             /*GENELEN*NUMLOCI */

    struct IND *Individual;       /*NUMINDS: records for each individual */
    int *Translation;             /*NUMLOCIxMAXALLELES: value of each coded allele */
    int *NumAlleles;              /*NUMLOCI: number of alleles at each locus */

    /* only used for recessive or inbreeding models: */
    int *PreGeno=
        NULL;           /*NUMINDSxLINESxNUMLOCI; diploid genotype if recessive alleles */
    int *Recessive=
        NULL;         /*NUMLOCI recessive allele at each locus, or -1 if there is none */


    /*Basic parameters */
    int *Z;                       /*NUMINDSx2xNUMLOCI: Z=pop of origin for each allele */
    int *Z1;
    float *Q;                    /*NUMINDSxMAXPOPS:  Q=ancestry of individuals */
    float *P;                    /*NUMLOCIxMAXPOPSxMAXALLELES: P=population allele freqs */
    float *Epsilon;              /*NUMLOCIxMAXALLELES: Dirichlet parameter for allele
                                  frequencies. This is either LAMBDA (if uncorrelated), or
                                  ancestral allele freqs if they are correlated */
    float *Fst;          /*MAXPOPS: Factor multiplied by epsilon under the Fst model */
    float *Alpha;                /*MAXPOPS: Dirichlet parameter for degree of admixture.
                                  Start this at ALPHA, and possibly change
                                  (if INFERALPHA==1) */
    float *lambda;                /*Dirichlet prior parameter for allele frequencies;
                                   start this at LAMBDA, and update if INFERLAMBDA*/
    float *sumlambda;
    /*Summaries */
    int    *NumLociPop;           /*NUMINDSxMAXPOPS: Number of alleles from each pop (by ind) */
    float *PSum;                 /*NUMLOCIxMAXPOPSxMAXALLELES: sum of AlFreqs */
    float *QSum;                 /*NUMINDSxMAXPOPS:  sum of Ancestries */
    float *FstSum;               /*MAXPOPS:  Sum of Fst */
    float *SumEpsilon=
        NULL;      /*NUMLOCIxMAXALLELES: sum of ancestral allele freqs*/
    float *sumAlpha;              /*MAXPOPS*/
    float *sumR;                 /*NUMINDS */
    float *varR;                 /*NUMINDS */
    float recomblikelihood=0.0;
    float *like;                  /*current likelihood value */
    float *sumlikes;              /*sum of likelihood values */
    float *sumsqlikes;            /*sum of squared likelihoods */

    int *popflags; /*The populationflags of individuals*/
    unsigned int *randGens;


    /*Melissa added 7/12/07 for calculating DIC*/
    float *sumIndLikes, *indLikesNorm;

    int    *AncestDist=
        NULL;      /*NUMINDS*MAXPOPS*NUMBOXES histogram of Q values */
    float *UsePopProbs=
        NULL;     /*NUMINDS*MAXPOPS*(GENSBACK+1) This is used when the
                                  population info is used.  It stores the probability that an
                                  individual has each of a specified set of ancestry amounts */
    /*loop variables-------------- */
    int rep;                      /*MCMC iterations so far */
    int savefreq;                 /*frequency of saving to file */
    int ind;

    /*Melissa's new variables added 7/12/07 to use priors based on sampling location*/
    float *LocPrior=NULL, *sumLocPrior=NULL, LocPriorLen=0;

    /* ======================= GPU Structure ======================== */
    /*Dict to that keeps track of CL info */
    /*CLDict *clDict = NULL;*/
    float * randomArr; /* array of random numbers */
    int POPFLAGINDS = 0;
    float invsqrtnuminds;
    /* enum BUFFER buffers[5]; */
    /* char         *names[5]; */
    /* size_t        sizes[5]; */
    /* void         *dests[5]; */

    float  *reduceresult;
    int *Numafrompopscl;
    int *Numlocipopscl;

    if (signal(SIGINT, catch_function) == SIG_ERR) {
        fputs("An error occurred while setting a signal handler.\n", stderr);
        return EXIT_FAILURE;
    }

    clDict = malloc(sizeof (*clDict));
    sumlikes = calloc(1,sizeof(float));
    sumsqlikes = calloc(1,sizeof(float));
    like = calloc(1,sizeof(float));
    /*=====Code for getting started=============================*/

    Welcome (stdout);             /*welcome */
    GetParams (0,argc,argv);      /*read in parameter values */

    CheckParamCombinations();     /*check that some parameter combinations are valid*/

    Mapdistance = calloc (NUMLOCI, sizeof (float));
    Phase = calloc (NUMLOCI * NUMINDS, sizeof (float));


    if (LINES ==2 && PHASED ==0) {
        Phasemodel=calloc(NUMINDS,sizeof(int));
        for (ind=0; ind<NUMINDS; ind++) {
            if (MARKOVPHASE) {
                Phasemodel[ind]=0;
            } else {
                Phasemodel[ind]=1;
            }
        }
    }

    lambda=calloc(MAXPOPS, sizeof (float));
    sumlambda=calloc(MAXPOPS, sizeof (float));

    Markername = calloc (GENELEN*NUMLOCI, sizeof (char));
    Geno = calloc (LINES * NUMLOCI * NUMINDS, sizeof (int));
    if (RECESSIVEALLELES) {
        PreGeno = calloc (LINES * NUMLOCI * NUMINDS, sizeof (int));
        Recessive = calloc (NUMLOCI, sizeof (int));
        if (PreGeno == NULL || Recessive == NULL) {
            printf ("Error (3) in assigning memory\n");
            Kill ();
        }
    }

    Individual = calloc (NUMINDS, sizeof (struct IND));
    if (Geno == NULL || Individual == NULL || Mapdistance == NULL
            || Markername == NULL) {
        printf ("Error in assigning memory (not enough space?)\n");
        Kill ();
    }
    Randomize(RANDOMIZE, &SEED);

    /*read in data file */
    if (RECESSIVEALLELES) {
        ReadInputFile(PreGeno, Mapdistance, Markername, Individual, Phase, Recessive);
    } else {
        ReadInputFile (Geno, Mapdistance, Markername, Individual, Phase, Recessive);
    }

    if (RECESSIVEALLELES) {
        MAXALLELES = FindMaxAlleles (PreGeno, Recessive);
    } else {
        MAXALLELES = FindMaxAlleles (Geno, Recessive);
    }


    /*=============set aside memory space=====================*/
    Translation = calloc (NUMLOCI * MAXALLELES, sizeof (int));
    NumAlleles = calloc (NUMLOCI, sizeof (int));
    Z = calloc (NUMINDS * LINES * NUMLOCI, sizeof (int));
    Z1 = calloc (NUMINDS * LINES * NUMLOCI, sizeof (int));
    Q = calloc (NUMINDS * MAXPOPS, sizeof (float));
    P = calloc (NUMLOCI * MAXPOPS * MAXALLELES, sizeof (float));
    R = calloc (NUMINDS, sizeof (float));
    sumR = calloc (NUMINDS, sizeof (float));
    varR = calloc (NUMINDS, sizeof (float));
    Epsilon = calloc (NUMLOCI * MAXALLELES, sizeof (float));
    if (FREQSCORR) {
        SumEpsilon = calloc (NUMLOCI * MAXALLELES, sizeof (float));
    }
    Fst = calloc (MAXPOPS, sizeof (float));
    FstSum = calloc (MAXPOPS, sizeof (float));
    NumLociPop = calloc (NUMINDS * MAXPOPS, sizeof (int));
    PSum = calloc (NUMLOCI * MAXPOPS * MAXALLELES, sizeof (float));
    QSum = calloc (NUMINDS * MAXPOPS, sizeof (float));


    if (ANCESTDIST) {
        AncestDist = calloc (NUMINDS * MAXPOPS * NUMBOXES, sizeof (int));
    }
    if (USEPOPINFO) {
        UsePopProbs = calloc (NUMINDS * MAXPOPS * (GENSBACK + 1), sizeof (float));
    }

    /*Melissa added 7/12/07*/
    if (LOCDATA>0 || LOCISPOP) {
        GetNumLocations(Individual);
    }

    /*Allocate the LocPrior vector.
      For no-admixture, it contains r, and the vectors nu and gamma.
      For admixture, it contains gamma.  The alphas_locals are stored with alpha global*/
    if (LOCPRIOR) {
        if (NOADMIX) {
            LocPriorLen = 1+MAXPOPS*(NUMLOCATIONS+1);
        } else {
            LocPriorLen=1;
        }
        LocPrior = malloc(LocPriorLen*sizeof(float));
        sumLocPrior = malloc(LocPriorLen*sizeof(float));
    }

    if (LOCPRIOR && NOADMIX==0) {
        Alpha = malloc(MAXPOPS*(NUMLOCATIONS+1)*sizeof(float));
        sumAlpha = malloc(MAXPOPS*(NUMLOCATIONS+1)*sizeof(float));
    } else {
        Alpha = calloc(MAXPOPS, sizeof (float));
        sumAlpha = calloc(MAXPOPS, sizeof (float));
    }

    /* this is for DIC */
    sumIndLikes = malloc(NUMINDS*sizeof(float));
    indLikesNorm = malloc(NUMINDS*sizeof(float));

    if ((Translation == NULL) || (NumAlleles == NULL) || (Z == NULL)
            || (Z1 == NULL) || (Q == NULL) ||
            (P == NULL) || (R == NULL) || (sumR == NULL) || (varR == NULL)
            || (Epsilon == NULL) ||
            (Fst == NULL) || (NumLociPop == NULL) ||
            (PSum == NULL) || (QSum == NULL) ||  (FstSum == NULL) ||
            ((ANCESTDIST) && (AncestDist == NULL)) ||
            ((USEPOPINFO) && (UsePopProbs == NULL))||(Alpha == NULL)||(sumAlpha==NULL)||
            ((FREQSCORR) && (SumEpsilon == NULL)) ||
            (LocPriorLen>0 && (LocPrior==NULL || sumLocPrior==NULL)) ||
            sumIndLikes==NULL || indLikesNorm==NULL) {

        printf ("Error in assigning memory (not enough space?)\n");
        FreeAll(Mapdistance, Phase, Phasemodel, lambda, sumlambda, Markername, Geno,
                PreGeno, Recessive,
                Individual, Translation, NumAlleles, Z, Z1, Q, P,  R, sumR, varR, Epsilon,
                SumEpsilon,
                Fst, FstSum, NumLociPop, PSum, QSum,  AncestDist, UsePopProbs, LocPrior,
                sumLocPrior, Alpha, sumAlpha, sumIndLikes, indLikesNorm, clDict);
        Kill ();
    }
    /*=========done setting aside memory space=====================*/

    /*initialize variables and arrays */
    Initialization (Geno, PreGeno, Individual, Translation, NumAlleles, Z, Z1,
                    Epsilon, SumEpsilon,
                    Fst, PSum, Q, QSum, FstSum, AncestDist, UsePopProbs, Alpha,
                    sumAlpha, sumR, varR, sumlikes, sumsqlikes, &savefreq, R, lambda,
                    sumlambda,Phase,Recessive, LocPrior, sumLocPrior, LocPriorLen, sumIndLikes,
                    indLikesNorm, clDict);

    /* ==================== GPU Structure ==================== */

    /*Allocate an array of random numbers. Primarily used so that we can compare
     CL implementation to the original */
    randomArr = calloc(RANDSIZE,sizeof(float));
    randGens = calloc(NUMRANDGENS,sizeof(unsigned int));

    Numafrompopscl = calloc(NUMLOCI*MAXPOPS*MAXALLELES,sizeof(int));
    Numlocipopscl = calloc(NUMINDS*MAXPOPS,sizeof(int));

    /* ====== OpenCL initialized ====== */

    printf ("\n\n--------------------------------------\n\n");
    printf ("Finished initialization; starting MCMC \n");
    printf ("%d iterations + %d burnin\n\n", NUMREPS, BURNIN);

    /*=====Main MCMC loop=======================================*/

    writeBuffer(clDict,Numafrompopscl,sizeof(int) * NUMLOCI*MAXPOPS*MAXALLELES,NUMAFROMPOPSCL,"NumAFromPops");
    writeBuffer(clDict,Numlocipopscl,sizeof(int)*NUMINDS*MAXPOPS,NUMLOCIPOPSCL,"NUMLOCIPOPS");
    /* init buffers on GPU */
    writeBuffer(clDict,P,sizeof(float) * PSIZE,PCL,"P");
    writeBuffer(clDict,Z,sizeof(int)*ZSIZE,ZCL,"Z");
    writeBuffer(clDict,Q, sizeof(float) * QSIZE,QCL, "Q");
    writeBuffer(clDict,NumAlleles,sizeof(int) * NUMLOCI,NUMALLELESCL,"NumAlleles");
    writeBuffer(clDict,lambda,sizeof(float) * MAXPOPS,LAMBDACL,"LAMBDA");
    if(!RECESSIVEALLELES){
        writeBuffer(clDict,Geno,sizeof(int)*GENOSIZE,GENOCL,"Geno");
    }

    popflags = calloc(NUMINDS,sizeof(int));
    for(ind = 0; ind < NUMINDS;ind++){
        popflags[ind] = Individual[ind].PopFlag;
        if (!((USEPOPINFO) && (Individual[ind].PopFlag))) {
            POPFLAGINDS++;
        }
    }

    printf("Setting updatealpha arg\n");
    setKernelArgExplicit(clDict,UpdateAlphaKernel,sizeof(int),&POPFLAGINDS,7);

    printf("Setting invsqrtnuminds arg\n");
    invsqrtnuminds = 1.0/sqrt(NUMINDS);
    setKernelArgExplicit(clDict,NonIndUpdateEpsilonKernel,sizeof(float),&invsqrtnuminds,6);

    writeBuffer(clDict,popflags,sizeof(int)*NUMINDS,POPFLAGCL,"popflags");

    writeBuffer(clDict,Alpha,sizeof(float) *MAXPOPS,ALPHACL,"Alpha");
    reduceresult = calloc(NUMINDS*NUMLOCI*MAXGROUPS,sizeof(float));
    if(reduceresult == NULL){
        printf("Failed to allocate reduce result\n");
    }
    printf("Writing reduce results\n");
    writeBuffer(clDict,reduceresult,sizeof(float)*MAXGROUPS*NUMINDS*NUMLOCI,REDUCERESULTSCL,"result");
    printf("Done writing reduce results\n");

    writeBuffer(clDict,sumAlpha, sizeof(float) * MAXPOPS,ALPHASUMCL, "alphasum");
    writeBuffer(clDict,sumlambda, sizeof(float) * MAXPOPS,LAMBDASUMCL, "lambdasum");

    writeBuffer(clDict,like, sizeof(float),LIKECL, "like");
    writeBuffer(clDict,sumsqlikes, sizeof(float),SUMSQLIKESCL, "sumsqlikes");
    writeBuffer(clDict,sumlikes, sizeof(float),SUMLIKESCL, "sumlikes");

    writeBuffer(clDict,QSum, sizeof(float) * QSIZE,QSUMCL, "qsum");
    writeBuffer(clDict,PSum, sizeof(float) * PSIZE,PSUMCL, "psum");
    writeBuffer(clDict,SumEpsilon, sizeof(float) * NUMLOCI*MAXALLELES,EPSILONSUMCL, "epssum");
    if(ANCESTDIST){
        writeBuffer(clDict,AncestDist, sizeof(int)*NUMINDS*MAXPOPS*NUMBOXES,ANCESTDISTCL, "ancest dist");
    }

    if (FREQSCORR) {
        writeBuffer(clDict,Fst,sizeof(float) * MAXPOPS,FSTCL,"FST");
        writeBuffer(clDict,FstSum, sizeof(float) * MAXPOPS,FSTSUMCL, "fstsum");
        writeBuffer(clDict,Epsilon,sizeof(float) * NUMLOCI*MAXALLELES,EPSILONCL,
                    "EPSILON");
    }
    /*printf("%d, %d\n",INFERALPHA,INFERLAMBDA);*/
    /*printf("%d\n",USEPOPINFO);*/
    /*printf("%d\n",RECESSIVEALLELES);*/
    /*printf("%d\n",LINKAGE);*/
    /*printf("%d\n",COMPUTEPROB);*/
    /*printf("%d\n",POPALPHAS);*/
    /*printf("%d\n",NOADMIX);*/
    /*printf("%d\n",LOCPRIOR);*/
    /*handleCLErr(1,clDict,"heyhey");*/

    /*Initialize Q */
    initQ(Q);
    initRandGens(clDict,randGens);
    finishWaitList(clDict);
    printf("Waitlist finished!\n");
    for (rep = 0; rep < (NUMREPS + BURNIN); rep++) {
        breakP(clDict);

        /*FillArrayWithRandomCL(clDict,randomArr,NUMLOCI*MAXALLELES*MAXPOPS*MAXRANDOM);*/
        /*FillArrayWithRandom(randomArr,NUMLOCI*MAXALLELES*MAXPOPS*MAXRANDOM);*/
        /*FillArrayWithRandom(randomArr,RANDSIZE);*/

        /* if(DEBUGCOMPARE) { */
        /*     readBuffer(clDict,randomArr, */
        /*             sizeof(float) * NUMLOCI*MAXALLELES*MAXPOPS*MAXRANDOM,RANDCL, */
        /*             "randomArr"); */
        /*     comparePCLandP(clDict,P,Epsilon, Fst, NumAlleles, Geno, Z, */
        /*                    lambda, Individual, randomArr); */
        /* } */
        /* if (USEWORKINGCL) { */
            /* clear buffer */
            writeBuffer(clDict,Numafrompopscl,sizeof(int) * NUMLOCI*MAXPOPS*MAXALLELES,NUMAFROMPOPSCL,"NumAFromPops");
            UpdatePCL (clDict,P, Epsilon, Fst, NumAlleles, Geno, Z, lambda,
                       Individual,
                       randomArr);
        /* }  else { */
        /*     readBuffer(clDict,randomArr, */
        /*             sizeof(float) * NUMLOCI*MAXALLELES*MAXPOPS*MAXRANDOM,RANDCL, */
        /*             "randomArr"); */
        /*     UpdateP (P, Epsilon, Fst, NumAlleles, Geno, Z, lambda, Individual, */
        /*              randomArr); */
        /* } */

        /* Update Q */
        /*FillArrayWithRandomCL(clDict,randomArr,NUMINDS+NUMINDS*MAXRANDOM);*/
        if (LINKAGE && rep >= ADMBURNIN) {
            UpdateQMetroRecombine (Geno, Q, Z, P, Alpha, rep,
                                   Individual, Mapdistance, R, Phase,Phasemodel,randomArr);
        } else {
            writeBuffer(clDict,Numlocipopscl,sizeof(int)*NUMINDS*MAXPOPS,NUMLOCIPOPSCL,"NUMLOCIPOPS");
            UpdateQCL (clDict,Geno, PreGeno, Q, P, Z, Alpha, rep, Individual, UsePopProbs,
                     Recessive, LocPrior,randomArr);
        }

        if (LOCPRIOR && UPDATELOCPRIOR) {
            UpdateLocPrior(Q, LocPrior, Alpha, Individual);
        }

        if (RECESSIVEALLELES) {
            UpdateGeno (PreGeno, Geno, P, Z, Recessive, NumAlleles, Q);
            writeBuffer(clDict,Geno,sizeof(int) * GENOSIZE,GENOCL,"Geno");
            /*The Zs are not correct after UpdateGeno, until UpdateZ is run */
        }

        if (LINKAGE && rep > ADMBURNIN) {
            if (!INDIVIDUALR) {
                recomblikelihood = UpdateZandSingleR(Z,  Q, P, Geno,
                                                     R, Mapdistance, rep, Phase, Z1,Phasemodel, rep+1 > BURNIN? sumIndLikes : NULL,
                                                     indLikesNorm);
            } else {
                recomblikelihood = UpdateZandR(Z,  Q, P, Geno, R,
                                               Mapdistance, rep, Phase, Z1,Phasemodel, rep+1 > BURNIN ? sumIndLikes:NULL,
                                               indLikesNorm);
            }
        } else {
            /*FillArrayWithRandomCL(clDict,randomArr,NUMINDS*NUMLOCI*LINES);*/
            /* if (DEBUGCOMPARE) { */
            /*     readBuffer(clDict,randomArr, */
            /*             sizeof(float) * NUMINDS*NUMLOCI*LINES,RANDCL, */
            /*             "randomArr"); */
            /*     compareZCLandZ(clDict,Z,Q,P,Geno,randomArr); */
            /* } */
            /* if (USEWORKINGCL) { */
                UpdateZCL (clDict,Z,  Q, P, Geno,randomArr);
                /* Not needed */
                /*readBuffer(clDict,Z,sizeof(int)*ZSIZE,ZCL,"Z");*/
            /* } else { */
            /*     readBuffer(clDict,randomArr, */
            /*             sizeof(float) * NUMINDS*NUMLOCI*LINES,RANDCL, */
            /*             "randomArr"); */
            /*     UpdateZ (Z,  Q, P, Geno,randomArr); */
            /* } */
            /*      printf("done updatez alpha[2]=%e\n", Alpha[2]); */
        }


        if (LOCPRIOR && NOADMIX==0) {
            UpdateAlphaLocPrior(Q, Alpha, LocPrior, Individual);
        } else if (INFERALPHA) {

            UpdateAlphaCL (clDict,Q, Alpha, Individual, rep,POPFLAGINDS);

            /* readBuffer(clDict,Q, sizeof(float) * QSIZE,QCL, "Q"); */
            /* readBuffer(clDict,Alpha, sizeof(float) * MAXPOPS,ALPHACL, "alpha"); */

            /* UpdateAlpha(Q, Alpha, Individual, rep); */

            /* writeBuffer(clDict,Alpha, sizeof(float) * MAXPOPS,ALPHACL, "alpha"); */
        }

        if (INFERLAMBDA) {
            readBuffer(clDict,P,sizeof(float) * PSIZE,PCL,"P");
            if  (POPSPECIFICLAMBDA) {
                UpdatePopLambda(P,lambda,NumAlleles);
            } else {
                UpdateLambda (P,Epsilon,lambda, NumAlleles);
            }
            writeBuffer(clDict,lambda,sizeof(float) * MAXPOPS,LAMBDACL,"LAMBDA");
        }

        if (FREQSCORR) {
            UpdateEpsilonCL(clDict,P,Epsilon,Fst,NumAlleles,lambda[0]);

            UpdateFstCL (clDict,Epsilon, Fst, P, NumAlleles);

            /* readBuffer(clDict,P, sizeof(float) * PSIZE,PCL, "P"); */
            /* readBuffer(clDict,Fst,sizeof(float) * MAXPOPS,FSTCL,"FST"); */
            /* readBuffer(clDict,Epsilon,sizeof(float) * NUMLOCI*MAXALLELES,EPSILONCL,"eps"); */

            /* UpdateEpsilon(P,Epsilon,Fst,NumAlleles,lambda[0]); */

            /* UpdateFst (Epsilon, Fst, P, NumAlleles); */

            /* writeBuffer(clDict,Epsilon,sizeof(float) * NUMLOCI*MAXALLELES,EPSILONCL,"eps"); */
            /* writeBuffer(clDict,Fst,sizeof(float) * MAXPOPS,FSTCL,"FST"); */
        }

        /*====book-keeping stuff======================*/
        if (rep + 1 > BURNIN) {
            /*buffers[0] = PCL;
            names[0] = "P"; dests[0] = P; sizes[0] = sizeof(float) * PSIZE;
            buffers[1] = QCL;
            names[1] = "Q"; dests[1] = Q; sizes[1] = sizeof(float) * QSIZE;

            readBuffers(clDict,dests,sizes,buffers,names,2);*/
            /*readBuffer(clDict,Alpha, sizeof(float) * MAXPOPS,ALPHACL, "alpha");
            if(rep % 100 == 0){
            printf("%f",Alpha[0]);
            }*/
            DataCollectionCL (clDict,Geno, PreGeno, Q, QSum, Z, Z1,  P, PSum,
                            Fst, FstSum, NumAlleles,
                            AncestDist, Alpha, sumAlpha, sumR, varR, like,
                            sumlikes, sumsqlikes, R, Epsilon,SumEpsilon,recomblikelihood,
                            lambda, sumlambda, Recessive, LocPrior, sumLocPrior, LocPriorLen, sumIndLikes,
                            indLikesNorm, rep);
        }


        if ((savefreq) && ((rep + 1) > BURNIN)
                && (((rep + 1 - BURNIN) % savefreq) == 0)
                && ((rep + 1) != NUMREPS + BURNIN)) {
            /* readBuffer(clDict,Alpha, sizeof(float) * MAXPOPS,ALPHACL, "alpha"); */
            readBuffer(clDict,Fst, sizeof(float) * MAXPOPS,FSTCL, "fst");
            readBuffer(clDict,sumAlpha, sizeof(float) * MAXPOPS,ALPHASUMCL, "alphasum");
            readBuffer(clDict,sumlambda, sizeof(float) * MAXPOPS,LAMBDASUMCL, "lambdasum");
            readBuffer(clDict,FstSum, sizeof(float) * MAXPOPS,FSTSUMCL, "fstsum");
            readBuffer(clDict,QSum, sizeof(float) * QSIZE,QSUMCL, "Qsum");
            readBuffer(clDict,PSum, sizeof(float) * PSIZE,PSUMCL, "Psum");
            readBuffer(clDict,SumEpsilon, sizeof(float) * NUMLOCI*MAXALLELES,EPSILONSUMCL, "epssum");
            readBuffer(clDict,like, sizeof(float),LIKECL, "like");
            readBuffer(clDict,sumlikes, sizeof(float),SUMLIKESCL, "sumlike");
            readBuffer(clDict,sumsqlikes, sizeof(float),SUMSQLIKESCL, "sumsqlikes");
            OutPutResults (Geno, rep + 1, savefreq, Individual, PSum, QSum,
                           FstSum, AncestDist, UsePopProbs, *sumlikes,
                           *sumsqlikes, sumAlpha, sumR, varR,
                           NumAlleles, Translation, 0, Markername, R,
                           SumEpsilon,
                           lambda,sumlambda,sumLocPrior, LocPriorLen,
                           sumIndLikes, indLikesNorm, argc,argv);
        }


        if (PRINTLIKES) {
            readBuffer(clDict,like, sizeof(float),LIKECL, "like");
            PrintLike (*like, rep, Geno, PreGeno, Q, P,recomblikelihood);
        }

        if (((rep + 1) % UPDATEFREQ) == 0) {
            readBuffer(clDict,Alpha, sizeof(float) * MAXPOPS,ALPHACL, "alpha");
            readBuffer(clDict,Fst, sizeof(float) * MAXPOPS,FSTCL, "fst");
            readBuffer(clDict,like, sizeof(float),LIKECL, "like");
            readBuffer(clDict,sumlikes, sizeof(float),SUMLIKESCL, "sumlike");
            readBuffer(clDict,sumsqlikes, sizeof(float),SUMSQLIKESCL, "sumsqlikes");
            /*readBuffer(clDict,sumAlpha, sizeof(float) * MAXPOPS,ALPHASUMCL, "alphasum");
            readBuffer(clDict,sumlambda, sizeof(float) * MAXPOPS,LAMBDASUMCL, "lambdasum");
            readBuffer(clDict,FstSum, sizeof(float) * MAXPOPS,FSTSUMCL, "fstsum");
            readBuffer(clDict,QSum, sizeof(float) * QSIZE,QSUMCL, "Qsum");
            readBuffer(clDict,PSum, sizeof(float) * PSIZE,PSUMCL, "Psum");
            readBuffer(clDict,SumEpsilon, sizeof(float) * NUMLOCI*MAXALLELES,EPSILONSUMCL, "epssum");*/
            PrintUpdate (rep + 1, Geno, PreGeno, Alpha, Fst, P, Q, *like,
                         *sumlikes, *sumsqlikes, NumAlleles, R, lambda,Individual,
                         recomblikelihood, Recessive, LocPrior, LocPriorLen);
        }
        finishWaitList(clDict);
    }

    /*====final book-keeping====================================*/
    if ((rep % UPDATEFREQ) != 0) {
        readBuffer(clDict,Alpha, sizeof(float) * MAXPOPS,ALPHACL, "alpha");
        readBuffer(clDict,Fst, sizeof(float) * MAXPOPS,FSTCL, "fst");
        readBuffer(clDict,like, sizeof(float),LIKECL, "like");
        readBuffer(clDict,sumsqlikes, sizeof(float),SUMSQLIKESCL, "sumsqlikes");
            readBuffer(clDict,sumlikes, sizeof(float),SUMLIKESCL, "sumlikes");
        /*readBuffer(clDict,sumAlpha, sizeof(float) * MAXPOPS,ALPHASUMCL, "alphasum");
        readBuffer(clDict,sumlambda, sizeof(float) * MAXPOPS,LAMBDASUMCL, "lambdasum");
        readBuffer(clDict,FstSum, sizeof(float) * MAXPOPS,FSTSUMCL, "fstsum");
        readBuffer(clDict,QSum, sizeof(float) * QSIZE,QSUMCL, "Qsum");
        readBuffer(clDict,PSum, sizeof(float) * PSIZE,PSUMCL, "Psum");
        readBuffer(clDict,SumEpsilon, sizeof(float) * NUMLOCI*MAXALLELES,EPSILONSUMCL, "epssum");*/
        PrintUpdate (rep, Geno, PreGeno, Alpha, Fst, P, Q, *like, *sumlikes,
                     *sumsqlikes, NumAlleles,R, lambda, Individual,recomblikelihood,
                     Recessive, LocPrior, LocPriorLen);
    }


    readBuffer(clDict,Alpha, sizeof(float) * MAXPOPS,ALPHACL, "alpha");
    readBuffer(clDict,Fst, sizeof(float) * MAXPOPS,FSTCL, "fst");
    readBuffer(clDict,sumAlpha, sizeof(float) * MAXPOPS,ALPHASUMCL, "alphasum");
    readBuffer(clDict,sumlambda, sizeof(float) * MAXPOPS,LAMBDASUMCL, "lambdasum");
    readBuffer(clDict,FstSum, sizeof(float) * MAXPOPS,FSTSUMCL, "fstsum");
    readBuffer(clDict,QSum, sizeof(float) * QSIZE,QSUMCL, "Qsum");
    readBuffer(clDict,PSum, sizeof(float) * PSIZE,PSUMCL, "Psum");
    readBuffer(clDict,SumEpsilon, sizeof(float) * NUMLOCI*MAXALLELES,EPSILONSUMCL, "epssum");
    readBuffer(clDict,like, sizeof(float),LIKECL, "like");
    readBuffer(clDict,sumsqlikes, sizeof(float),SUMSQLIKESCL, "sumsqlikes");
    readBuffer(clDict,sumlikes, sizeof(float),SUMLIKESCL, "sumlikes");
    OutPutResults (Geno, rep, savefreq, Individual, PSum, QSum,
                   FstSum, AncestDist, UsePopProbs,
                   *sumlikes, *sumsqlikes,
                   sumAlpha, sumR, varR, NumAlleles, Translation, 1,
                   Markername, R, SumEpsilon,
                   lambda,sumlambda,sumLocPrior, LocPriorLen,
                   sumIndLikes, indLikesNorm,
                   argc,argv);

    /*=====Closing everything down==============================*/
    FreeAll(Mapdistance, Phase, Phasemodel, lambda, sumlambda, Markername, Geno,
            PreGeno, Recessive,
            Individual, Translation, NumAlleles, Z, Z1, Q, P, R, sumR, varR, Epsilon,
            SumEpsilon,
            Fst, FstSum, NumLociPop, PSum, QSum,  AncestDist, UsePopProbs, LocPrior,
            sumLocPrior, Alpha, sumAlpha, sumIndLikes, indLikesNorm, clDict);
    free(randomArr);
    free(randGens);
    free(like);
    free(sumsqlikes);
    free(sumlikes);
    free(reduceresult);
    printf("Structure seed: %d\n", SEED);
    return (0);
}
/**
   Write the content of the dictionary to the named file.  The existing
   content in the named file is overwritten.  The content of the dictionary
   file is laid out as follows.

   \li Signature "#IBIS Dictionary " and version number (currently
   0x020000). (20 bytes)

   \li N = Number of strings in the file. (4 bytes)

   \li uint64_t[N+1]: the starting positions of the strings in this file.

   \li uint32_t[N]: The integer code corresponding to each string value.

   \li the string values packed one after the other with their nil
   terminators.
*/
int ibis::dictionary::write(const char* name) const {
    std::string evt = "dictionary::write";
    if (name == 0 || *name == 0) {
        LOGGER(ibis::gVerbose > 1)
            << "Warning -- " << evt << " can not proceed with a "
            "null string as the file name";
        return -1;
    }
    if (ibis::gVerbose > 1) {
        evt += '(';
        evt += name;
        evt += ')';
    }
    if (key_.size() > raw_.size()) {
        LOGGER(ibis::gVerbose > 1)
            << "Warning -- " << evt
            << " can not write an inconsistent dictionary, key_.size("
            << key_.size() << "), raw_.size(" << raw_.size() << ")";
        return -2;
    }

    ibis::util::timer mytimer(evt.c_str(), 4);
    FILE* fptr = fopen(name, "wb");
    if (fptr == 0) {
        LOGGER(ibis::gVerbose > 1)
            << "Warning -- " << evt << " failed to open the file ... "
            << (errno ? strerror(errno) : "no free stdio stream");
        return -3;
    }

    IBIS_BLOCK_GUARD(fclose, fptr);
    int ierr = fwrite(_fastbit_dictionary_header, 1, 20, fptr);
    if (ierr != 20) {
        LOGGER(ibis::gVerbose > 1)
            << "Warning -- " << evt
            << " failed to write the header, fwrite returned " << ierr;
        return -4;
    }

    const uint32_t nkeys = key_.size();
    ierr = fwrite(&nkeys, sizeof(nkeys), 1, fptr);
    if (ierr != 1) {
        LOGGER(ibis::gVerbose > 1)
            << "Warning -- " << evt << " failed to write the size(" << nkeys
            << "), fwrite returned " << ierr;
        return -5;
    }
    if (nkeys == 0) // nothing else to write
        return 0;

    mergeBuffers();
    array_t<uint64_t> pos(nkeys+1);
    array_t<uint32_t> qos(nkeys);

    pos.clear();
    qos.clear();
    pos.push_back(0);
    if (buffer_.size() == 1) {
        for (uint32_t j = 0; j < raw_.size(); ++ j) {
            if (raw_[j] != 0) {
                pos.push_back(1U + strlen(raw_[j]));
                qos.push_back(j);
            }
        }
        ierr = writeBuffer(fptr, nkeys, pos, qos);
    }
    else {
        ierr = writeKeys(fptr, nkeys, pos, qos);
    }
    LOGGER(ibis::gVerbose > 1)
        << evt << " complete with ierr = " << ierr;
    return ierr;
} // ibis::dictionary::write
Example #29
0
void writeObject(refBuffer buffer, refObject object)
{ writingObject(buffer, object);
  writeBuffer(buffer); }
Example #30
0
int main(int argc, char** argv) {
    initialize(argc, argv);

    // open file
    FILE* output = fopen(fileName, "wb");
    if (output == NULL) {
        perror("fopen");
        exit(1);
    }

    // open connection
    int s = openConnection();

    ssize_t bytesRead = 0;
    while (1) {

      GoBackNMessageStruct* data = allocateGoBackNMessageStruct(DEFAULT_PAYLOAD_SIZE);

      if (recv(s, data, sizeof(*data), MSG_PEEK) < 0) {
          perror("recv(MSG_PEEK)");
		   close(s);
          exit(1);
      }

      bytesRead = data->size;
	bytesRead = recv(s, data, data->size, 0);
        if (bytesRead < 0) {
	    perror("recv");
		 close(s);
	    exit(1);
	}
        if (bytesRead < data->size) {
            fprintf(stderr,"WARNING: Truncated read\n");
        }
        DEBUGOUT("SOCKET: %d bytes received from %u.%u\n", bytesRead, data->srcNode, data->srcAppl);
        DEBUGOUT("%d.%d -> %d.%d, #%d/%d, size: %u, errors: %d\n",
                 data->srcNode, data->srcAppl, data->destNode, data->destAppl, data->seqNo,
                 data->seqNoExpected, data->size, (int)data->hasErrors);

        if (bytesRead == 0) {
            freeGoBackNMessageStruct(data);
            break;
        }
        
	data->size = bytesRead;
	totalBytes += bytesRead-sizeof(*data);

        /* YOUR TASK:*/
		
		// Check packet for errors
		if(data->hasErrors)
			 {
				DEBUGOUT("recieved data package has error\n");
			 } else
			 {
				
				if(data->seqNo<lastReceivedSeqNo+1) // # an older one
				{
					DEBUGOUT("recievd data package seqNo to low-> old package\n");
					sendAck(s, data, lastReceivedSeqNo+1);
				}
				else if(data->seqNo>lastReceivedSeqNo+1) // # too new (we missed at least one packet)
				{
					DEBUGOUT("recievd data package seqNo to high-> too new\n");
					sendAck(s, data, lastReceivedSeqNo+1);
				}
				else // # is the one we are expecting
				{
					// Acknowledge the packet if appropriate
					sendAck(s, data, data->seqNo+1);
					
					// Write packet to file if appropriate
					writeBuffer(output, data);
					
					// Update sequence number counter
					lastReceivedSeqNo=data->seqNo;
					// Update goodBytes
					goodBytes+=bytesRead;
					// If this was the last packet of the transmission
					if(sizeof(*data)==data->size)
					{
						//  close the file with fclose and
						fclose(output);

						//   output totalBytes and goodBytes
						DEBUGOUT("file recieved. total bytes: %d, good bytes: %d errounous Bytes\n", totalBytes, goodBytes, (totalBytes-goodBytes) );
						freeGoBackNMessageStruct(data);
            close(s);
            exit(0);
					}
				}
			 }
		



	freeGoBackNMessageStruct(data);
    }
	close(s);
}