int 
tp_init (struct thread_pool_t* tp, size_t nthread, size_t max_queue_size,
    thread_func_t tf, real_task_destroy rt_destroy)
{
  int j;

  if (tf == NULL || rt_destroy == NULL || tp == NULL || nthread == 0)
      return -1;

  if (tp_queue_init (&tp->tpq, max_queue_size) == -1)
      return -1;

  tp->nthread = nthread;
  tp->thread_func = tf;
  tp->rt_destroy = rt_destroy;

  tp->threads = (pthread_t*) malloc (nthread * sizeof(pthread_t));

  if (tp->threads == NULL)
    {
      tp_destroy (tp);
      return -1;
    }

  for (j = 0; j < nthread; ++j) {
      if (pthread_create (&tp->threads[j], NULL, tp_thread_func, (void*)tp) != 0)
        {
          tp_destroy (tp);
          return -1;
        }
  }

  return 0;
}
Exemple #2
0
Tour * tour_mutate(Tour *old_tour) {
    town_index_t i, num_vertexes;
    TownPool *tp;
    Tour * new_tour;

    num_vertexes = old_tour->town_list->config.num_vertexes;
    tp           = tp_new(num_vertexes);
    new_tour     = new_empty_tour(old_tour->town_list, num_vertexes);

    #pragma omp parallel for
    for (i=0; i<num_vertexes; i++){
        new_tour->tour[i] = old_tour->tour[i];
    }

    {
        town_index_t i1, i2;
        i1 = (town_index_t)tp_random_town(tp);
        i2 = (town_index_t)tp_random_town(tp);
        new_tour->tour[i1] = old_tour->tour[i2];
        new_tour->tour[i2] = old_tour->tour[i1];
    }

    new_tour->length = tour_length(new_tour);

    tp_destroy(tp);
    return new_tour;
}
Exemple #3
0
Tour * tour_new(TownList *towns) {
    town_index_t i, num_vertexes = towns->config.num_vertexes;
    TownPool *tp = tp_new(num_vertexes);
    if (!tp)
        exit(1);
    Tour * newtour = new_empty_tour(towns, num_vertexes);
    
    for (i=0; i<num_vertexes; i++){
        newtour->tour[i] = tp_random_town(tp);
    }
    

    newtour->length = tour_length(newtour);
    tp_destroy(tp);
    return newtour;
}
Exemple #4
0
int tsload_destroy_threadpool(const char* tp_name) {
	thread_pool_t* tp = tp_search(tp_name);

	if(tp == NULL) {
		tsload_error_msg(TSE_INVALID_DATA, "Thread pool '%s' not exists", tp_name);
		return TSLOAD_ERROR;
	}

	if(tp->tp_wl_count > 0) {
		tsload_error_msg(TSE_INVALID_STATE, "Thread pool '%s' has workloads running on it", tp_name);
		return TSLOAD_ERROR;
	}

	tp_destroy(tp);
	return TSLOAD_OK;
}
Exemple #5
0
int
main(void) {
    int stdin_size;
    char *stdin_mem = read_stdin(&stdin_size);
    int cpu_count = get_cpu_count();

    char output_buffer[7][MAX_OUTPUT];

#   define DECLARE_PARAM(o, n) {\
    .start = stdin_mem, \
    .length = stdin_size, \
    .frame = n,\
    .output = output_buffer[o],\
    .output_size = MAX_OUTPUT }

    struct print_freqs_param freq_params[2] = {
        DECLARE_PARAM(0, 1),
        DECLARE_PARAM(1, 2)
    }; 

#   undef DECLARE_PARAM

#   define DECLARE_PARAM(o, s) {\
    .start = stdin_mem, \
    .length = stdin_size, \
    .nuc_seq = s,\
    .output = output_buffer[o],\
    .output_size = MAX_OUTPUT }

    struct print_occurences_param occurences_params[5] = {
        DECLARE_PARAM(2, "GGT"),
        DECLARE_PARAM(3, "GGTA"),
        DECLARE_PARAM(4, "GGTATT"),
        DECLARE_PARAM(5, "GGTATTTTAATT"),
        DECLARE_PARAM(6, "GGTATTTTAATTTATAGT")
    };

#   undef DECLARE_PARAM

    struct tp *tp = tp_create(7);

    for (int i = 0 ; i < 2; i++) {
        tp_add_job(tp, &print_freqs, &freq_params[i]);
    }
    for (int i = 0 ;i <  5; i++) {
        tp_add_job(tp, &print_occurences, &occurences_params[i]);
    }

    tp_run(tp, cpu_count + 1);

    tp_destroy(tp);

    for (int i = 0; i < 2; i++) {
        printf("%s\n", output_buffer[i]);
    }
    for (int i = 2; i < 7; i++) {
        printf("%s", output_buffer[i]);
    }

    free(stdin_mem);

    return 0;
}