Example #1
0
static t_tree	*balance(t_tree *node)
{
	int		diff;
	t_tree	*tmp;

	diff = get_diff(node);
	if (diff > 1)
	{
		if (get_diff(node->left) > 0)
			node = left_rot(node);
		else
		{
			tmp = node->left;
			node->left = right_rot(tmp);
			node = left_rot(node);
		}
	}
	else if (diff < -1)
	{
		if (get_diff(node->right) > 0)
		{
			tmp = node->right;
			node->right = left_rot(tmp);
		}
		node = right_rot(node);
	}
	return (node);
}
static struct dma_service *service_lookup(lpaddr_t mem_low,
                                          size_t size,
                                          uint8_t numa_node)
{

    struct dma_service *current = dma_services;
    struct dma_service *best = NULL;
    while (current) {
        if (current->info.mem_low <= mem_low) {
            if (current->info.mem_high >= (mem_low + size)) {
                /* we have a match */
                if (best == NULL) {
                    best = current;
                } else {
                    uint8_t best_numa_diff = get_diff(numa_node,
                                                      best->info.numa_node);
                    uint8_t curr_numa_diff = get_diff(numa_node,
                                                      current->info.numa_node);
                    if (curr_numa_diff < best_numa_diff) {
                        best = current;
                    }
                }
            }
        }
        current = current->next;
    }
    return best;
}
Example #3
0
void 
JB2Dict::JB2Codec::code_relative_location(JB2Blit *jblt, int rows, int columns)
{
  // Check start record
  if (!gotstartrecordp)
    G_THROW( ERR_MSG("JB2Image.no_start") );
  // Find location
  int bottom=0, left=0, top=0, right=0;
  int x_diff, y_diff;
  if (encoding)
    {
      left = jblt->left + 1;
      bottom = jblt->bottom + 1;
      right = left + columns - 1;
      top = bottom + rows - 1;
    }
  // Code offset type
  int new_row=CodeBit((left<last_left), offset_type_dist);
  if (new_row)
    {
      // Begin a new row
      x_diff=get_diff(left-last_row_left,rel_loc_x_last);
      y_diff=get_diff(top-last_row_bottom,rel_loc_y_last);
      if (!encoding)
        {
          left = last_row_left + x_diff;
          top = last_row_bottom + y_diff;
          right = left + columns - 1;
          bottom = top - rows + 1;
        }
      last_left = last_row_left = left;
      last_right = right;
      last_bottom = last_row_bottom = bottom;
      fill_short_list(bottom);
    }
  else
    {
      // Same row
      x_diff=get_diff(left-last_right,rel_loc_x_current);
      y_diff=get_diff(bottom-last_bottom,rel_loc_y_current);
      if (!encoding)
        {
          left = last_right + x_diff;
          bottom = last_bottom + y_diff;
          right = left + columns - 1;
          top = bottom + rows - 1;
        }
      last_left = left;
      last_right = right;
      last_bottom = update_short_list(bottom);
    }
  // Store in blit record
  if (!encoding)
    {
      jblt->bottom = bottom - 1;
      jblt->left = left - 1;
    }
}
Example #4
0
void unified_difft::output_diff(
  const irep_idt &identifier,
  const goto_programt &old_goto_program,
  const goto_programt &new_goto_program,
  const differencest &differences,
  std::ostream &os) const
{
  goto_program_difft diff;
  get_diff(
    identifier,
    old_goto_program,
    new_goto_program,
    differences,
    diff);

  bool has_diff=false;
  for(const auto &d : diff)
  {
    if(d.second!=differencet::SAME)
    {
      has_diff=true;
      break;
    }
  }
  if(!has_diff)
    return;

  os << "/** " << identifier << " **/\n";

  for(const auto &d : diff)
  {
    switch(d.second)
    {
      case differencet::SAME:
        os << ' ';
        new_goto_program.output_instruction(
          ns_new,
          identifier,
          os,
          d.first);
        break;
      case differencet::DELETED:
        os << '-';
        old_goto_program.output_instruction(
          ns_old,
          identifier,
          os,
          d.first);
        break;
      case differencet::NEW:
        os << '+';
        new_goto_program.output_instruction(
          ns_new,
          identifier,
          os,
          d.first);
        break;
    }
  }
}
Example #5
0
int main(int argc, char* argv[])
{
	string src, dst;
    
    cin >> src;
    cin >> dst;

    string match(dst.size() * 2 + src.size(), ' ');
    std::copy(src.begin(), src.end(), match.begin() + dst.size());

    int min_diff = dst.size();
    //string::const_iterator mid_diff_it = match.begin();
    for (string::const_iterator it = match.begin(); it != match.end() - dst.size(); ++it)
    {
        const int diff = get_diff(dst.begin(), dst.end(), it);
        if (diff < min_diff)
        {
            min_diff = diff;
            //mid_diff_it = it;
        }
    }
    
    cout << min_diff << endl;

    return 0;
}
int main(int argc, char *argv[])
{
   //number of threads
   THREAD_NUM = atoi(argv[1]);
   //precision to work to
   PRECISION = atof(argv[2]);

   pthread_mutex_init(&lock, NULL); 

   count = 0; 
   printf("Original Array\n");
   print_array(n, arr);

   memcpy(temparr, arr, sizeof(arr));
   matrix_work(n, arr);

   while(get_diff()!=0){
   	//printf("change\n");
   	//print_array(n, arr);
    memcpy(arr, temparr, sizeof(arr));
   	matrix_work(n, arr);
   }

   printf("Array after relaxation technique applied\n");
   print_array(n, arr);

   pthread_mutex_destroy(&lock);

   return 0;
}
Example #7
0
void unified_difft::get_diff(
  const irep_idt &function,
  goto_program_difft &dest) const
{
  dest.clear();

  differences_mapt::const_iterator entry=
    differences_map.find(function);
  if(entry==differences_map.end())
    return;

  goto_functionst::function_mapt::const_iterator old_fit=
    old_goto_functions.function_map.find(function);
  goto_functionst::function_mapt::const_iterator new_fit=
    new_goto_functions.function_map.find(function);

  goto_programt empty;

  const goto_programt &old_goto_program=
    old_fit==old_goto_functions.function_map.end() ?
    empty :
    old_fit->second.body;
  const goto_programt &new_goto_program=
    new_fit==new_goto_functions.function_map.end() ?
    empty :
    new_fit->second.body;

  get_diff(
    function,
    old_goto_program,
    new_goto_program,
    entry->second,
    dest);
}
Example #8
0
config config::get_diff(const config& c) const
{
	check_valid(c);

	config res;
	get_diff(c, res);
	return res;
}
Example #9
0
bool const configs_verifyer::verify(u8* data, u32 data_size, string256 & diff)
{
	IReader		tmp_reader(data, data_size);
	CInifile	tmp_ini(&tmp_reader);
	CInifile	tmp_active_params(NULL, FALSE, FALSE, FALSE);
	
	string16	tmp_digit;
	u32			ap_index = 1;
	sprintf_s	(tmp_digit, "%d", ap_index);
	while		(tmp_ini.line_exist(active_params_section, tmp_digit))
	{
		LPCSTR	tmp_ap_section		= tmp_ini.r_string(active_params_section, tmp_digit);
		tmp_active_params.w_string	(active_params_section, tmp_digit, tmp_ap_section);
		m_original_ap.load_to		(tmp_ap_section, tmp_active_params);
		++ap_index;
		sprintf_s					(tmp_digit, "%d", ap_index);
	}
	
	m_orig_config_body.seek			(m_orig_config_end_pos);
	tmp_active_params.save_as		(m_orig_config_body);

	if (!tmp_ini.line_exist(cd_info_secion, cd_player_name_key) ||
		!tmp_ini.line_exist(cd_info_secion, cd_player_digest_key) ||
		!tmp_ini.line_exist(cd_info_secion, cd_creation_date) ||
		!tmp_ini.line_exist(cd_info_secion, cd_digital_sign_key))
	{
		strcpy_s(diff, "invalid dump");
		return false;
	}

	LPCSTR		add_str = NULL;
	STRCONCAT	(add_str,
		tmp_ini.r_string(cd_info_secion, cd_player_name_key),
		tmp_ini.r_string(cd_info_secion, cd_player_digest_key),
		tmp_ini.r_string(cd_info_secion, cd_creation_date));

	m_orig_config_body.w_stringZ(add_str);
	
	crypto::xr_sha256	tmp_sha_checksum;
	tmp_sha_checksum.start_calculate(
		m_orig_config_body.pointer(),
		m_orig_config_body.tell());
	while (!tmp_sha_checksum.continue_calculate()) {};
	
	u8	tmp_checksum[crypto::xr_sha256::digest_length];
	if (!verify_dsign(data, data_size, tmp_checksum))
	{
		strcpy_s(diff, "invalid digital sign");
		return false;
	}

	if (memcmp(tmp_checksum, tmp_sha_checksum.pointer(), sizeof(tmp_checksum)))
	{
		get_diff(tmp_ini, tmp_active_params, diff);
		return false;
	}
	return true;
}
Example #10
0
avl_node* balancing(avl_node *node) {
    int b_factor = get_diff(node);
    
    if (b_factor > 1) {
        if (get_diff(node->left) > 0) {
            node = left_left_rotation(node);
        } else {
            node = left_right_rotation(node);
        }
    } else if (b_factor < -1) {
        if (get_diff(node->right) > 0) {
            node = right_left_rotation(node);
        } else {
            node = right_right_rotation(node);
        }
    }
    
    return node;
}
Example #11
0
int manhattan(t_env *env, t_state *s1, t_state *s2)
{
	int total = 0;

	for (int y = 0; y < env->size; ++y)
	{
		for (int x = 0; x < env->size; ++x)
		{
			total += get_diff(env, s2, x, y, s1->puzzle[y][x]);
		}
	}
	return (total);
}
Example #12
0
int main(int argc, char *argv[])
{
        int fd_in, fd_out;
        struct timeval t0, t1;

        setup(&fd_in, &fd_out, NULL);

        CHECK_ERROR(-1, gettimeofday(&t0, NULL));

        for (int i = 0; i < BENCHNUM; i++) {
                run_sendfile(fd_in, fd_out);
        }

        CHECK_ERROR(-1, gettimeofday(&t1, NULL));

        int diff = get_diff(&t0, &t1);

        printf("====== BENCH SENDFILE ======\n");
        print_result(diff);

        return 0;
}
// Handler for interrupt
void handle_gpio_interrupt(void) {
	struct timeval now;
	unsigned long diff;

	if (!lock) {
		gettimeofday(&now, NULL);

		// Time difference in usec
		diff = get_diff(now, last_change);

		// Filter jitter
		if (diff > IGNORE_CHANGE_BELOW_USEC) {
			// Should switch to post gap? It's a gap > gap len but less than train boundary. Only when we're doing numbers, though.
			if (pre_gap && diff > MIN_GAP_LEN_USEC && diff < MIN_TRAIN_BOUNDARY_USEC) {
				pre_gap = 0;
			}

			// Increment the right counter
			if (pre_gap) {
				pre_gap_pulses++;
			}
			else {
				post_gap_pulses++;
			}

			if (debug)
				printf("Pulse! Pre: %d Post: %d Diff: %lu\n", pre_gap_pulses, post_gap_pulses, diff);
		}

		// Record when the last change was
		last_change = now;
	}
	else {
		printf("Locked. Ignoring interrupt\n");
	}
}
Example #14
0
int print_gpios(struct pci_dev *sb, int show_all, int show_diffs)
{
	int i, j, size, defaults_size = 0;
	const io_register_t *gpio_registers;
	const gpio_default_t *gpio_defaults = NULL;
	uint32_t gpio_diff;

	if (show_diffs && !show_all)
		printf("\n========== GPIO DIFFS ===========\n\n");
	else
		printf("\n============= GPIOS =============\n\n");

	switch (sb->device_id) {
	case PCI_DEVICE_ID_INTEL_Z68:
	case PCI_DEVICE_ID_INTEL_P67:
	case PCI_DEVICE_ID_INTEL_H67:
	case PCI_DEVICE_ID_INTEL_Q65:
	case PCI_DEVICE_ID_INTEL_QS67:
	case PCI_DEVICE_ID_INTEL_Q67:
	case PCI_DEVICE_ID_INTEL_B65:
	case PCI_DEVICE_ID_INTEL_C202:
	case PCI_DEVICE_ID_INTEL_C204:
	case PCI_DEVICE_ID_INTEL_C206:
	case PCI_DEVICE_ID_INTEL_H61:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = pch_gpio_registers;
		size = ARRAY_SIZE(pch_gpio_registers);
		gpio_defaults = cp_pch_desktop_defaults;
		defaults_size = ARRAY_SIZE(cp_pch_desktop_defaults);
		break;
	case PCI_DEVICE_ID_INTEL_UM67:
	case PCI_DEVICE_ID_INTEL_HM65:
	case PCI_DEVICE_ID_INTEL_HM67:
	case PCI_DEVICE_ID_INTEL_QM67:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = pch_gpio_registers;
		size = ARRAY_SIZE(pch_gpio_registers);
		gpio_defaults = cp_pch_mobile_defaults;
		defaults_size = ARRAY_SIZE(cp_pch_mobile_defaults);
		break;
	case PCI_DEVICE_ID_INTEL_Z77:
	case PCI_DEVICE_ID_INTEL_Z75:
	case PCI_DEVICE_ID_INTEL_Q77:
	case PCI_DEVICE_ID_INTEL_Q75:
	case PCI_DEVICE_ID_INTEL_B75:
	case PCI_DEVICE_ID_INTEL_H77:
	case PCI_DEVICE_ID_INTEL_C216:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = pch_gpio_registers;
		size = ARRAY_SIZE(pch_gpio_registers);
		gpio_defaults = pp_pch_desktop_defaults;
		defaults_size = ARRAY_SIZE(pp_pch_desktop_defaults);
		break;
	case PCI_DEVICE_ID_INTEL_QM77:
	case PCI_DEVICE_ID_INTEL_QS77:
	case PCI_DEVICE_ID_INTEL_HM77:
	case PCI_DEVICE_ID_INTEL_UM77:
	case PCI_DEVICE_ID_INTEL_HM76:
	case PCI_DEVICE_ID_INTEL_HM75:
	case PCI_DEVICE_ID_INTEL_HM70:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = pch_gpio_registers;
		size = ARRAY_SIZE(pch_gpio_registers);
		gpio_defaults = pp_pch_mobile_defaults;
		defaults_size = ARRAY_SIZE(pp_pch_mobile_defaults);
		break;
	case PCI_DEVICE_ID_INTEL_ICH10R:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = ich10_gpio_registers;
		size = ARRAY_SIZE(ich10_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH9DH:
	case PCI_DEVICE_ID_INTEL_ICH9DO:
	case PCI_DEVICE_ID_INTEL_ICH9R:
	case PCI_DEVICE_ID_INTEL_ICH9:
	case PCI_DEVICE_ID_INTEL_ICH9M:
	case PCI_DEVICE_ID_INTEL_ICH9ME:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = ich9_gpio_registers;
		size = ARRAY_SIZE(ich9_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH8:
	case PCI_DEVICE_ID_INTEL_ICH8M:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = ich8_gpio_registers;
		size = ARRAY_SIZE(ich8_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH7:
	case PCI_DEVICE_ID_INTEL_ICH7M:
	case PCI_DEVICE_ID_INTEL_ICH7DH:
	case PCI_DEVICE_ID_INTEL_ICH7MDH:
	case PCI_DEVICE_ID_INTEL_NM10:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = ich7_gpio_registers;
		size = ARRAY_SIZE(ich7_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH6:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = ich6_gpio_registers;
		size = ARRAY_SIZE(ich6_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH5:
		gpiobase = pci_read_word(sb, 0x58) & 0xfffc;
		gpio_registers = ich5_gpio_registers;
		size = ARRAY_SIZE(ich5_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH4:
	case PCI_DEVICE_ID_INTEL_ICH4M:
		gpiobase = pci_read_word(sb, 0x58) & 0xfffc;
		gpio_registers = ich4_gpio_registers;
		size = ARRAY_SIZE(ich4_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH2:
		gpiobase = pci_read_word(sb, 0x58) & 0xfffc;
		gpio_registers = ich2_gpio_registers;
		size = ARRAY_SIZE(ich2_gpio_registers);
		break;
	case PCI_DEVICE_ID_INTEL_ICH:
	case PCI_DEVICE_ID_INTEL_ICH0:
		gpiobase = pci_read_word(sb, 0x58) & 0xfffc;
		gpio_registers = ich0_gpio_registers;
		size = ARRAY_SIZE(ich0_gpio_registers);
		break;

	case PCI_DEVICE_ID_INTEL_I63XX:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = i631x_gpio_registers;
		size = ARRAY_SIZE(i631x_gpio_registers);
		break;

	case PCI_DEVICE_ID_INTEL_3400_DESKTOP:
	case PCI_DEVICE_ID_INTEL_3400_MOBILE:
	case PCI_DEVICE_ID_INTEL_P55:
	case PCI_DEVICE_ID_INTEL_PM55:
	case PCI_DEVICE_ID_INTEL_H55:
	case PCI_DEVICE_ID_INTEL_QM57:
	case PCI_DEVICE_ID_INTEL_H57:
	case PCI_DEVICE_ID_INTEL_HM55:
	case PCI_DEVICE_ID_INTEL_Q57:
	case PCI_DEVICE_ID_INTEL_HM57:
	case PCI_DEVICE_ID_INTEL_3400_MOBILE_SFF:
	case PCI_DEVICE_ID_INTEL_B55_A:
	case PCI_DEVICE_ID_INTEL_QS57:
	case PCI_DEVICE_ID_INTEL_3400:
	case PCI_DEVICE_ID_INTEL_3420:
	case PCI_DEVICE_ID_INTEL_3450:
	case PCI_DEVICE_ID_INTEL_B55_B:
		gpiobase = pci_read_word(sb, 0x48) & 0xfffc;
		gpio_registers = i631x_gpio_registers;
		size = ARRAY_SIZE(i631x_gpio_registers);
		break;

	case PCI_DEVICE_ID_INTEL_82371XX:
		printf("This southbridge has GPIOs in the PM unit.\n");
		return 1;
	case 0x1234: // Dummy for non-existent functionality
		printf("This southbridge does not have GPIOBASE.\n");
		return 1;
	default:
		printf("Error: Dumping GPIOs on this southbridge is not (yet) supported.\n");
		return 1;
	}

	printf("GPIOBASE = 0x%04x (IO)\n\n", gpiobase);

	j = 0;
	for (i = 0; i < size; i++) {
		if (show_all)
			print_reg(&gpio_registers[i]);

		if (show_diffs &&
		    (j < defaults_size) &&
		    (gpio_defaults[j].addr == gpio_registers[i].addr)) {
			gpio_diff = get_diff(&gpio_registers[i],
					     gpio_defaults[j].def);
			if (gpio_diff) {
				if (!show_all)
					print_reg(&gpio_registers[i]);
				print_diff(&gpio_registers[i],
					   gpio_defaults[j].def, gpio_diff);
				if (!show_all)
					printf("\n");
			}
			j++;
		}
	}

	return 0;
}
Example #15
0
int
yylex (void)
{
  unichar *start_token;
  unichar ch;

  if (! input_stream_pos)
    {
      fatal ("Input stream not setuped.\n");
      return -1;
    }
  if (mclex_want_line)
    {
      start_token = input_stream_pos;
      if (input_stream_pos[0] == '.'
	  && (input_stream_pos[1] == '\n'
	      || (input_stream_pos[1] == '\r' && input_stream_pos[2] == '\n')))
      {
	mclex_want_line = FALSE;
	while (input_stream_pos[0] != 0 && input_stream_pos[0] != '\n')
	  ++input_stream_pos;
	if (input_stream_pos[0] == '\n')
	  ++input_stream_pos;
	return MCENDLINE;
      }
      while (input_stream_pos[0] != 0 && input_stream_pos[0] != '\n')
	++input_stream_pos;
      if (input_stream_pos[0] == '\n')
	++input_stream_pos;
      yylval.ustr = get_diff (input_stream_pos, start_token);
      return MCLINE;
    }
  while ((ch = input_stream_pos[0]) <= 0x20)
    {
      if (ch == 0)
	return -1;
      ++input_stream_pos;
      if (ch == '\n')
	input_line += 1;
      if (mclex_want_nl && ch == '\n')
	{
	  mclex_want_nl = FALSE;
	  return NL;
	}
    }
  start_token = input_stream_pos;
  ++input_stream_pos;
  if (mclex_want_filename)
    {
      mclex_want_filename = FALSE;
      if (ch == '"')
	{
	  start_token++;
	  while ((ch = input_stream_pos[0]) != 0)
	    {
	      if (ch == '"')
		break;
	      ++input_stream_pos;
	    }
	  yylval.ustr = get_diff (input_stream_pos, start_token);
	  if (ch == '"')
	    ++input_stream_pos;
	}
      else
	{
	  while ((ch = input_stream_pos[0]) != 0)
	    {
	      if (ch <= 0x20 || ch == ')')
		break;
	      ++input_stream_pos;
	    }
	  yylval.ustr = get_diff (input_stream_pos, start_token);
	}
      return MCFILENAME;
    }
  switch (ch)
  {
  case ';':
    ++start_token;
    while (input_stream_pos[0] != '\n' && input_stream_pos[0] != 0)
      ++input_stream_pos;
    if (input_stream_pos[0] == '\n')
      input_stream_pos++;
    yylval.ustr = get_diff (input_stream_pos, start_token);
    return MCCOMMENT;
  case '=':
    return '=';
  case '(':
    return '(';
  case ')':
    return ')';
  case '+':
    return '+';
  case ':':
    return ':';
  case '0': case '1': case '2': case '3': case '4':
  case '5': case '6': case '7': case '8': case '9':
    yylval.ival = parse_digit (ch);
    return MCNUMBER;
  default:
    if (ch >= 0x40)
      {
	int ret;
	while (input_stream_pos[0] >= 0x40 || (input_stream_pos[0] >= '0' && input_stream_pos[0] <= '9'))
	  ++input_stream_pos;
	ret = mc_token (start_token, (size_t) (input_stream_pos - start_token));
	if (ret != -1)
	  return ret;
	yylval.ustr = get_diff (input_stream_pos, start_token);
	return MCIDENT;
      }
    yyerror ("illegal character 0x%x.", ch);
  }
  return -1;
}
Example #16
0
static inline float get_pitot(uint32_t raw)
{
  return sqrtf((2.0f * get_diff(raw)) / 1.293f);
}
int main(int argc, char **argv) {
	int c;
	struct timeval now;
	unsigned long diff;
	char letter;
	int number;
	int pre, post;

	// CLI Params
	while ((c = getopt(argc, argv, "dp:")) != -1) {
		switch (c) {
			case 'd':
				debug = 1;
				break;
			// Programme to pass the generated key combo to for handling
			case 'p':
				pass_to = strdup(optarg);
				break;
		}
	}

	// Init
	wiringPiSetup();

	// Set pin to output in case it's not
	pinMode(PIN, OUTPUT);

	// Init last change to be now
	gettimeofday(&last_change, NULL);

	// Bind to interrupt
	wiringPiISR(PIN, INT_EDGE_BOTH, &handle_gpio_interrupt);

	// The loop...
	for (;;) {
		// Time now
		gettimeofday(&now, NULL);

		// Time difference in usec
		diff = get_diff(now, last_change);

		// Should reset counters?
		if (diff > MIN_TRAIN_BOUNDARY_USEC) {
			// Have we registered a full pulse train (i.e. seen a gap)?
			if (!pre_gap && pre_gap_pulses && post_gap_pulses) {
				// 0 base the counts without changing the originals
				pre = pre_gap_pulses - 1;
				post = post_gap_pulses - 1;

				if (debug)
					printf("Locking\n");

				lock = 1;

				if (debug) {
					printf("Locked\n");
					printf("Before calc. Pre: %d Post: %d\n", pre, post);
				}

				// Calc the key combination...
				letter = 'A' + (2 * post) + (pre > 10); // A plus the offset plus 1 more if pre gap pulses > 10
				letter += (letter > 'H'); // Hax for missing I
				number = pre % 10;

				// Hand off to the handler
				handle_key_combo(letter, number);
			}

			// Reset counters
			if (pre_gap_pulses || post_gap_pulses) {
				if (debug)
					printf("Reset! %lu\n", diff);

				pre_gap_pulses = 0;
				post_gap_pulses = 0;
				pre_gap = 1;
			}

			if (lock) {
				if (debug)
					printf("Unlocking\n");

				lock = 0;

				if (debug)
					printf("Unlocked\n");
			}
		}

		// Should update time to stop diff overflowing?
		if (diff > OVERFLOW_PROTECTION_INTERVAL_USEC) {
			if (debug)
				printf("Overflow protection\n");

			gettimeofday(&last_change, NULL);
		}

		// Waste time but not CPU whilst still allowing us to detect finished pulses
		usleep(10000);
	}

	if (pass_to) {
		free(pass_to);
	}

	return 0;
}
Example #18
0
int main(int argc, char *argv[]) 
{
   //precision to work to
   sscanf(argv[1], "%lf", &PRECISION);

   fill_array();

   long int sum, partial_sum;
   MPI_Status status;
   int ID, root_process, ierr, i, num_procs, an_id, sender, length, flag;

   ierr = MPI_Init(&argc, &argv);
   
   root_process = 0;
   
   /* find out MY process ID, and how many processes were started. */
   
   ierr = MPI_Comm_rank(MPI_COMM_WORLD, &ID);
   ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);

   THREAD_NUM = num_procs-1;

   int INNER_ARRAY_SIZE = n-2;
   int TOTAL_NUMS = (INNER_ARRAY_SIZE)*(INNER_ARRAY_SIZE);
      
   //splits total amount of numbers in matrix into number of threads beng used, and finds the remainder
   int NUMS_PER_THREAD = (int)floor(TOTAL_NUMS / THREAD_NUM);
   int NUMS_REMAINDER = (TOTAL_NUMS % THREAD_NUM);

   if(ID == root_process) {

      // ROOT PROCESS

      //copies original array into a temporary array
      memcpy(temparr, arr, sizeof(arr));

      matrix_work(num_procs, INNER_ARRAY_SIZE, NUMS_PER_THREAD, NUMS_REMAINDER, TOTAL_NUMS);

      while(get_diff()!=0){
         memcpy(arr, temparr, sizeof(arr));
         matrix_work(num_procs, INNER_ARRAY_SIZE, NUMS_PER_THREAD, NUMS_REMAINDER, TOTAL_NUMS);
      }


      flag=1;
      for(an_id = 1; an_id < num_procs; an_id++) {
         ierr = MPI_Send(&flag, 1, MPI_INT, an_id, send_data_tag, MPI_COMM_WORLD);
      }

   }
   else {

      // SLAVE PROCESS

      //while program not done, calculate averages from range in array and send back to root process
      while(get_flag()==0){
         ierr = MPI_Recv(&(arr[0][0]), n*n, MPI_DOUBLE, root_process, send_data_tag, MPI_COMM_WORLD, &status);
      

         //Calculates the start and end numbers in the matrix for the thread to work on
         int START_NUM = 0 + ((ID-1)*NUMS_PER_THREAD);
         int END_NUM = (ID*NUMS_PER_THREAD)-1;

         if (ID == THREAD_NUM){
            END_NUM = END_NUM+ NUMS_REMAINDER;
         }
         
         int range = END_NUM - START_NUM;
         double localnums[range];

         //loops through numbers from start to end that need to be worked on and overwrites the temp array
         int i;
         int count = 0;
         for(i = START_NUM; i <= END_NUM; i++) {
            //get the x & y values for corresponding number in the matrix
            int x = (i%INNER_ARRAY_SIZE)+1;
            int y = ((i-(i%INNER_ARRAY_SIZE))/INNER_ARRAY_SIZE)+1;
            //get four surrounding numbers and average them
            //above
            double a = arr[y-1][x];
            //right
            double b = arr[y][x+1];
            //below
            double c = arr[y+1][x];
            //left
            double d = arr[y][x-1];

            double average = (a+b+c+d)/4;

            localnums[count] = average;
            count++;
         }

         //and finally, send array of new numbers back to the root process
         ierr = MPI_Send(&count, 1, MPI_INT, root_process, return_data_tag, MPI_COMM_WORLD);
         ierr = MPI_Send(&localnums, count, MPI_DOUBLE, root_process, return_data_tag, MPI_COMM_WORLD);
      }
   }

   ierr = MPI_Finalize();
}