Beispiel #1
0
/** mapreduce_sort()
 *  
 */
void mapreduce_sort(void *base, size_t num_elems, size_t width,
       int (*compar)(const void *, const void *), int threads)
{
   final_data_t sort_vals;

   // Global variable
   unit_size = width;
   compare_g = compar;

   // Setup scheduler args
   scheduler_args_t sched_args;
   memset(&sched_args, 0, sizeof(scheduler_args_t));
   sched_args.task_data = base; // Array to sort
   sched_args.map = mr_sort_map;
   sched_args.reduce = NULL; // Identity Reduce
   sched_args.splitter = NULL; // Array splitter //mr_sort_splitter;
   sched_args.key_cmp = compar;
   sched_args.unit_size = width;
   sched_args.partition = mr_mypartition; 
   sched_args.result = &sort_vals;
   sched_args.data_size = num_elems * width;
   //sched_args.L1_cache_size = atoi(MR_GETENV("MR_L1CACHESIZE")); //1024 * 64;
   //sched_args.num_map_threads = atoi(MR_GETENV("MR_NUMTHREADS"));//8;
   //sched_args.num_reduce_threads = atoi(MR_GETENV("MR_NUMTHREADS"));//16;
   //sched_args.num_merge_threads = atoi(MR_GETENV("MR_NUMTHREADS")) / 2;//8;
   sched_args.num_map_threads = threads;
   sched_args.num_reduce_threads = threads;
   sched_args.num_merge_threads = threads;
   //sched_args.num_procs = 8;
   //sched_args.key_match_factor = (float)atof(MR_GETENV("MR_KEYMATCHFACTOR"));//2;
   //sched_args.use_one_queue_per_task = atoi(MR_GETENV("MR_1QPERTASK"));

   CHECK_ERROR(map_reduce_scheduler(&sched_args) < 0);
   
   char * tmp = (char *)MR_MALLOC(width * num_elems); 
   int i;
   
   // Need to copy to temp array first since
   // we could be sorting array of pointers
   for(i = 0; i < sort_vals.length; i++)
   {
      memcpy(tmp + (i*width), sort_vals.data[i].key, width);
   }
   
   memcpy(base, tmp, width * num_elems);
   
   //free(tmp);
   //free(sort_vals.data);
}
Beispiel #2
0
/**
 * MR_BITMASK type saving handler. Look up type descriptor and save as
 * stringified bitmask value or as integer otherwise.
 * @param idx an index of node in ptrs
 * @param ptrs resizeable array with pointers descriptors
 * @return stringified enum value
 */
static char *
scm_save_bitmask (int idx, mr_ra_mr_ptrdes_t * ptrs)
{
  char * str = mr_stringify_bitmask (&ptrs->ra.data[idx], MR_SCM_BITMASK_OR_DELIMITER);
  if (str)
    {
      char * str_ = MR_MALLOC ((sizeof (MR_SCM_BITMASK_TEMPLATE) - 1) + (strlen (str) - 2) + 1);
      if (NULL == str_)
	{
	  MR_MESSAGE (MR_LL_FATAL, MR_MESSAGE_OUT_OF_MEMORY);
	  return (NULL);
	}
      sprintf (str_, MR_SCM_BITMASK_TEMPLATE, str);
      MR_FREE (str);
      str = str_;
    }
  return (str);
}
Beispiel #3
0
/**
 * XML quote function. Escapes XML special characters.
 * @param str input string
 * @return XML quoted string
 */
static char *
xml_quote_string (char * str)
{
  int length = 0;
  char * str_;

  if (NULL == str)
    return (NULL);

  for (str_ = str; *str_; ++str_)
    if (map[(unsigned char)*str_])
      length += strlen (map[(unsigned char)*str_]);
    else if (isprint (*str_))
      ++length;
    else
      length += sizeof (XML_NONPRINT_ESC) - 1;

  str_ = MR_MALLOC (length + 1);
  if (NULL == str)
    {
      MR_MESSAGE (MR_LL_FATAL, MR_MESSAGE_OUT_OF_MEMORY);
      return (NULL);
    }

  length = 0;
  for (; *str; ++str)
    if (map[(unsigned char)*str])
      {
	strcpy (&str_[length], map[(unsigned char)*str]);
	length += strlen (&str_[length]);
      }
    else if (isprint (*str))
      str_[length++] = *str;
    else
      {
	sprintf (&str_[length], XML_NONPRINT_ESC, (int)(unsigned char)*str);
	length += strlen (&str_[length]);
      }
  str_[length] = 0;
  return (str_);
}
Beispiel #4
0
/**
 * Quote string.
 * @param str string pointer
 * @param quote quote character
 * @return quoted string
 */
static char *
scm_quote_string (char * str, char quote)
{
  int length = 0;
  char * str_;
  char * ptr;

  for (ptr = str; *ptr; ++ptr)
    {
      if ((quote == *ptr) || ('\\' == *ptr))
	++length;
      if (!isprint (*ptr))
	length += 4;
      ++length;
    }
  str_ = MR_MALLOC (length + 3);
  if (NULL == str_)
    {
      MR_MESSAGE (MR_LL_FATAL, MR_MESSAGE_OUT_OF_MEMORY);
      return (NULL);
    }
  length = 0;
  str_[length++] = quote;
  for (ptr = str; *ptr; ++ptr)
    {
      if ((quote == *ptr) || ('\\' == *ptr))
	str_[length++] = '\\';
      if (isprint (*ptr))
	str_[length++] = *ptr;
      else
	{
	  str_[length++] = '\\';
	  sprintf (&str_[length], "x%02x;", (int)(unsigned char)*ptr);
	  length += 4;
	}
    }
  str_[length++] = quote;
  str_[length++] = 0;
  return (str_);
}
Beispiel #5
0
mr_object_t 
mr_object_new_by_size(mr_heap_t heap, mr_uintptr_t size)
{
    if (heap && heap->object_count >= heap->gc_threshold)
    {
        if (do_gc(heap) != 0) return NULL;
    }

    mr_gc_header_t gc = (mr_gc_header_t)MR_MALLOC(sizeof(mr_gc_header_s) + size);
    if (gc == NULL) return NULL;

    gc->type          = &dummy_type;
    gc->gc_status     = heap ? GC_STATUS_UNTOUCH : GC_STATUS_HOST;
    gc->extern_ref    = 1;

    if (heap)
    {
        heap->tracker[heap->object_count] = MR_TO_OBJECT(gc);
        ++ heap->object_count;
    }
    
    return MR_TO_OBJECT(gc);
}
Beispiel #6
0
/**
 * XML unquote function. Replace XML special characters aliases on a source characters.
 * @param str input string
 * @param length length of the input string
 * @return XML unquoted string
 */
char *
xml_unquote_string (mr_substr_t * substr)
{
  char * str_ = MR_MALLOC (substr->length + 1);
  int length_ = 0;
  int i, j;

  static int inited = 0;
  static char map_c[ESC_CHAR_MAP_SIZE];
  static char * map_cp[ESC_CHAR_MAP_SIZE];
  static int map_s[ESC_CHAR_MAP_SIZE];
  static int map_size = 0;

  if (0 == inited)
    {
      for (i = 0; i < ESC_CHAR_MAP_SIZE; ++i)
	if (map[i])
	  {
	    int size = strlen (map[i]);
	    if (size > 1)
	      {
		map_c[map_size] = i;
		map_cp[map_size] = map[i];
		map_s[map_size] = size;
		++map_size;
	      }
	  }
      inited = !0;
    }

  if (NULL == str_)
    {
      MR_MESSAGE (MR_LL_FATAL, MR_MESSAGE_OUT_OF_MEMORY);
      return (NULL);
    }

  for (j = 0; j < substr->length; ++j)
    if (substr->str[j] != '&')
      str_[length_++] = substr->str[j];
    else
      {
	char esc[ESC_SIZE];
	strncpy (esc, &substr->str[j], sizeof (esc) - 1);
	if ('#' == substr->str[j + 1])
	  {
	    int32_t code = 0;
	    int size = 0;
	    if (1 != sscanf (&substr->str[j], XML_NONPRINT_ESC "%n", &code, &size))
	      MR_MESSAGE (MR_LL_WARN, MR_MESSAGE_WRONG_XML_ESC, esc);
	    else
	      {
		j += size - 1; /* one more +1 in the loop */
		str_[length_++] = code;
	      }
	  }
	else
	  {
	    for (i = 0; i < map_size; ++i)
	      if (0 == strncasecmp (&substr->str[j], map_cp[i], map_s[i]))
		{
		  str_[length_++] = map_c[i];
		  j += map_s[i] - 1; /* one more increase in the loop */
		  break;
		}
	    if (i >= map_size)
	      {
		MR_MESSAGE (MR_LL_WARN, MR_MESSAGE_UNKNOWN_XML_ESC, esc);
		str_[length_++] = substr->str[j];
	      }
	  }
      }
  str_[length_] = 0;
  return (str_);
}