void launch_get_min_recur(void *args) {
    struct get_min_arg_struct *arg_struct = (struct get_min_arg_struct *)args;
    int *cur_path = MY_MALLOC(arg_struct->tot_depth * sizeof(int));
    CHECK_PTR(cur_path, "cur_path could not be allocated");
    
    for (int i = 0; i < arg_struct->tot_depth; i++)
        cur_path[i] = -1;
    
    arg_struct->single_exprs = MY_MALLOC(arg_struct->ncol * arg_struct->nrow);
    CHECK_PTR(arg_struct->single_exprs, "args.single_exprs could not be allocated");
    
    memcpy(arg_struct->single_exprs, arg_struct->exprs, arg_struct->ncol*arg_struct->nrow);

    bool *skip_single = MY_MALLOC(arg_struct->ncol * sizeof(bool));
    memset(skip_single, 0, arg_struct->ncol * sizeof(bool));
    
    if(g_trace >= 2)
        printf("Launching search for (%lf,%lf)\n", arg_struct->num_const, arg_struct->denom_const);
    
    get_min_recur(0, arg_struct->tot_depth, cur_path, arg_struct->exprs, arg_struct->single_exprs, arg_struct->nrow, arg_struct->ncol, arg_struct->w1p, arg_struct->w1m, arg_struct->w2p, arg_struct->w2m, arg_struct->num_const, arg_struct->denom_const, arg_struct->curmin, arg_struct->combi_min_idx, skip_single, arg_struct->active, arg_struct->active_len, arg_struct->mutex);
    
    if(g_trace >= 2)
        printf("Finished search for (%lf,%lf)\n", arg_struct->num_const, arg_struct->denom_const);
    
    MY_FREE(arg_struct->single_exprs);
    MY_FREE(skip_single);
    MY_FREE(cur_path);
}
MyMapObjObj *my_mapObjObj_new(my_func_compareObj func_compare_keys) {
	MyMapObjObj *map = MY_MALLOC(1, MyMapObjObj);
	map->func_compare_keys = func_compare_keys;
	if (func_compare_keys == NULL)
		my_log_error("func_compare_keys is null\n");
	return map;
}
Example #3
0
void doProcDispatch(char *buf,long length,struct sockaddr_in from_addr)
{
    struct MyMessage *msg = (struct MyMessage *)MY_MALLOC(length,"doProcDispatch");

    memcpy(msg,buf,length);
    msgwangwang(msg,&from_addr);
}
Example #4
0
void doTcpProcDispatch(char *buf,unsigned long length,struct sockaddr_in from_addr)
{
    struct MyMessage *msg = (struct MyMessage *)MY_MALLOC(length,"doTcpProcDispatch");
    memcpy(msg,buf,length);
    LOGD(TAG,"doTcpProcDispatch msg id is %d,length is %d \n",msg->msgId,length);  
    msgwangwang(msg,&from_addr);
}
bool is_idx_in_set(int *cur_path, int tot_depth, int min_idx, int *active, int active_len) {
    int *idxs = MY_MALLOC(tot_depth * sizeof(int));
    CHECK_PTR(idxs, "idxs could not be allocated");
    
    int i, j;
    
    
    for (i = 0; i < tot_depth && cur_path[i] >= 0; i++)
        idxs[i] = cur_path[i];
    idxs[i] = min_idx;
    for (j = i+1; j < tot_depth; j++)
        idxs[j] = -1;
    
    qsort(idxs, i+1, sizeof(int), intcmp);
    
    for(i = 0; i < active_len; i++) {
        for (j = 0; j < tot_depth; j++) {
            if(active[i*tot_depth + j] != idxs[j])
                break;
        }
        if(j >= tot_depth) {
            MY_FREE(idxs);
            return true;
        }
    }
    
    MY_FREE(idxs);
    return false;
}
static void validate_frequencies(int64_t *array, int64_t minValueIncluded,
		int64_t maxValueNotIncluded, int64_t numSamples) {
	int64_t interval_size = maxValueNotIncluded - minValueIncluded;
	int64_t *frequencies = MY_MALLOC(interval_size, int64_t);
	for (int64_t i = 0; i < numSamples; ++i) {
		frequencies[array[i] - minValueIncluded]++;
	}
	int64_t expectedFreq = my_math_round_int(
			numSamples / (double) interval_size);
	int64_t expectedFreqDelta = my_math_round_int(expectedFreq * 0.01);
	my_log_info("expected frequency=%"PRIi64" +/- %"PRIi64"\n", expectedFreq,
			expectedFreqDelta);
	int64_t cont_errors = 0;
	for (int64_t i = 0; i < interval_size; ++i) {
		int64_t freq = frequencies[i];
		bool is_ok = (MY_ABS_INT(expectedFreq - freq) <= expectedFreqDelta);
		if (interval_size <= 100) {
			double freq_pct = 100.0 * freq / (double) numSamples;
			my_log_info("%3"PRIi64" =%7"PRIi64" (%1.2lf%%)%s\n",
					i + minValueIncluded, freq, freq_pct, is_ok ? "" : " *");
		}
		if (!is_ok)
			cont_errors++;
	}
	if (cont_errors == 0)
		my_log_info("OK. All frequencies inside range [%"PRIi64",%"PRIi64"]\n",
				expectedFreq - expectedFreqDelta,
				expectedFreq + expectedFreqDelta);
	else
		my_log_info(
				"ERROR. %"PRIi64"/%"PRIi64" frequencies out of range [%"PRIi64",%"PRIi64"]\n",
				cont_errors, interval_size, expectedFreq - expectedFreqDelta,
				expectedFreq + expectedFreqDelta);
	MY_FREE(frequencies);
}
MyLocalDescriptors *my_localDescriptors_new(int64_t num_descriptors,
		MyDatatype vector_datatype, int64_t vector_dimensions) {
	MyLocalDescriptors *ldes = MY_MALLOC(1, MyLocalDescriptors);
	my_localDescriptors_redefineVectorDatatype(ldes, vector_datatype,
			vector_dimensions, num_descriptors);
	return ldes;
}
Example #8
0
int startListenPartData(int sock,onAcceptPartData callback,int id)
{
    fd_set readfd;
    char *buf = (char *)MY_MALLOC(sizeof(struct PartFrameData),"startListenPartbuf");
    struct sockaddr_in from_addr;
    int ret = -1;
    int len = sizeof(struct sockaddr_in);;
    LOGE(TAG,"startListenPartData,receive sock is %d \n",sock);
    //we should wait for the udp broadcast
    while(1)
    {
        //clean fd set
        FD_ZERO(&readfd);

        //add sock fd to readfd
        FD_SET(sock, &readfd);

        //start listen
        LOGE(TAG,"startListenPartData,receive trace1,sock is %d \n",sock);
        ret = select(sock+1, &readfd, NULL, NULL, NULL);
        LOGE(TAG,"startListenPartData,receive trace2\n");
        switch(ret)
        {
            case -1:
            case 0:
                continue;
                break;

            default:
            {
                //confirm whether there are some datas in the sock
                
                if(FD_ISSET(sock, &readfd))
                {
                    ret = recvfrom(sock, buf, sizeof(struct PartFrameData), 0, (struct sockaddr *)&from_addr, &len);
                    if(0 > ret)
                    {
                        LOGE(TAG,"startListenPartData,receive sock failed!,error = %s \n",strerror(errno));
                        continue;
                    }
                    struct PartFrameData* partFrame = (struct PartFrameData*)buf;
#ifdef DUMP_UDP_DATA                    
                    char filepath[32];
                    memset(filepath,0,32);
                    sprintf(filepath,".//acce//%d",wwww);
                    dumpBuf2File(partFrame,sizeof(struct PartFrameData),filepath);
                    wwww++;
#endif
                    LOGD(TAG,"listen partFrame index is %d,size is %d \n",partFrame->index,partFrame->size);

                    callback(partFrame,id);
                }
            }     
        }
        
     }

}
MknnDataset *mknn_datasetLoader_reorderNearestNeighbor(
		MknnDataset *superdataset, MknnDistance *distance,
		int64_t start_position,
		bool free_superdataset_on_release) {
	int64_t max_size = mknn_dataset_getNumObjects(superdataset);
	int64_t *sorted_array = MY_MALLOC(max_size, int64_t);
	for (int64_t i = 0; i < max_size; ++i)
		sorted_array[i] = i;
	if (start_position < 0) {
		start_position = getPositionCloserToZero(superdataset, distance);
	}
	my_assert_indexRangeInt("start_position", start_position, max_size);
	if (start_position != 0) {
		int64_t tmp = sorted_array[0];
		sorted_array[0] = sorted_array[start_position];
		sorted_array[start_position] = tmp;
	}
	int64_t sorted_size = 1;
	while (sorted_size < max_size) {
		if (false) {
			char *st1 = my_newString_arrayInt(sorted_array, sorted_size, ' ');
			char *st2 = my_newString_arrayInt(sorted_array + sorted_size,
					max_size - sorted_size, ' ');
			my_log_info("sorted=%s\n    left=%s\n", st1, st2);
			free(st1);
			free(st2);
		}
		MknnDataset *query_subset = mknn_datasetLoader_SubsetSegment(
				superdataset, sorted_array[sorted_size - 1], 1, false);
		MknnDataset *ref_subset = mknn_datasetLoader_SubsetPositions(
				superdataset, sorted_array + sorted_size,
				max_size - sorted_size, false);
		MknnIndex *index = mknn_index_newPredefined(
				mknn_predefIndex_LinearScan_indexParams(), true, ref_subset,
				true, distance, false);
		MknnResolver *resolver = mknn_index_newResolver(index,
				mknn_predefIndex_LinearScan_resolverExactNearestNeighbors(1, 0,
						1), true);
		MknnResult *result = mknn_resolver_search(resolver, true, query_subset,
		true);
		MknnResultQuery *res = mknn_result_getResultQuery(result, 0);
		my_assert_equalInt("num_nns", res->num_nns, 1);
		int64_t nn_position = sorted_size + res->nn_position[0];
		mknn_result_release(result);
		mknn_index_release(index);
		if (nn_position != sorted_size) {
			int64_t tmp = sorted_array[sorted_size];
			sorted_array[sorted_size] = sorted_array[nn_position];
			sorted_array[nn_position] = tmp;
		}
		sorted_size++;
	}
	MknnDataset *sortedset = mknn_datasetLoader_SubsetPositions(superdataset,
			sorted_array, max_size, free_superdataset_on_release);
	free(sorted_array);
	return sortedset;
}
Example #10
0
Transform_Def *newTransformDef(const char *code, const char *helpText) {
	Transform_Def *def = MY_MALLOC(1, Transform_Def);
	def->trCode = code;
	def->helpText = helpText;
	if (defs_tr == NULL)
		defs_tr = my_vectorObj_new();
	my_vectorObj_add(defs_tr, def);
	return def;
}
Example #11
0
void *tcpServerStart(void *data) 
{
    struct sockaddr_in server_addr;
    int opt = 1;
    //bzreo(&(server_addr.sin_zero),8);
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htons(INADDR_ANY);
    struct globalConfig *config = getConfig();
    
    server_addr.sin_port = htons(config->tcp_server_port);

    int server_socket = socket(AF_INET, SOCK_STREAM, 0);

    char *buf = MY_MALLOC(LOACL_BUFFER_LENGTH,"tcpserverstartbuf");

    if (server_socket < 0) {
        LOGE(TAG,"create socket faild \n");
    }

    setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));

    if (bind(server_socket, (struct sockaddr *) &server_addr,
            sizeof(server_addr)) != 0) {
        LOGE(TAG,"bind server faild , error = %s \n", strerror(errno));
    }

    if (listen(server_socket, LENGTH_OF_LISTEN_QUEUE) != 0) {
        LOGE(TAG,"listen faild , error = %s \n", strerror(errno));
    }


    while (1) {
        struct sockaddr_in client_addr;
        socklen_t length = sizeof(client_addr);
        LOGD(TAG,"tcpServerStart start \n");        
        int client_fd = accept(server_socket,( struct sockaddr *)&client_addr,&length);
        LOGD(TAG,"tcpServerStart accept \n");   
        if (client_fd < 0) {
            LOGD(TAG,"accept faild \n");
            continue;
        }

        unsigned long recvbytes = 0;
        LOGD(TAG,"tcpServerStart trace \n");  
        if((recvbytes = recv(client_fd,buf,LOACL_BUFFER_LENGTH,0))==-1)
        {
            LOGE(TAG,"receive error \n");
        }
        LOGD(TAG,"tcpServerStart trace2 \n");  

        doTcpProcDispatch(buf,recvbytes,client_addr);
        LOGD(TAG,"tcpserver receive a message \n");

    }
}
Example #12
0
Transform *findTransform2(const char *code, const char *parameters) {
	Transform_Def *def = findTransformDef(code);
	void *state = NULL;
	def->func_new(code, parameters, &state);
	Transform *tr = MY_MALLOC(1, Transform);
	tr->def = def;
	tr->state = state;
	if (def->func_preprocess_compute != NULL)
		tr->mustPreprocessCompute = true;
	if (def->func_preprocess_load != NULL)
		tr->mustPreprocessLoad = true;
	return tr;
}
void my_random_testInt(int64_t minValueIncluded, int64_t maxValueNotIncluded,
		int64_t numSamples) {
	my_log_info("\nrandomTestInt\n");
	int64_t *array = MY_MALLOC(numSamples, int64_t);
	my_random_intList(minValueIncluded, maxValueNotIncluded, array, numSamples);
	double actualAvg = my_math_averageIntArray(numSamples, array);
	double expectedAvg = (maxValueNotIncluded - 1 + minValueIncluded) / 2.0;
	char *st = my_newString_double(actualAvg);
	my_log_info("%"PRIi64" random int numbers in [%"PRIi64",%"PRIi64"): %s\n",
			numSamples, minValueIncluded, maxValueNotIncluded, st);
	MY_FREE(st);
	validate_stats(actualAvg, expectedAvg);
	validate_frequencies(array, minValueIncluded, maxValueNotIncluded,
			numSamples);
	MY_FREE(array);
}
Example #14
0
void createNewUdpServerListener(struct NewUdpServerRet *result)
{
    //we should init udp listener.
    long ret = -1;
    int sock = -1;
    unsigned int len = -1;
    char *buf = MY_MALLOC(LOACL_BUFFER_LENGTH,"udplistenbuf");
    fd_set readfd;
    struct timeval timeout;
    struct sockaddr_in local_addr;
    struct sockaddr_in from_addr;

    LOGD(TAG,"createNewUdpServerListener start \n");

    //socket
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(0 > sock)
    {
        return;
    }

    struct globalConfig *config = getConfig();
    int tryCount = 0;
    int tryPort = config->broadcast_server_port;

    while(ret < 0)
    {
        tryPort += tryCount + 5;
        len = sizeof(struct sockaddr_in);
        memset(&local_addr, 0, sizeof(struct sockaddr_in));
        local_addr.sin_family = AF_INET;                    
        local_addr.sin_addr.s_addr = htonl(INADDR_ANY);   //local address
        local_addr.sin_port = htons(tryPort);                //listen port
        ret = bind(sock, (struct sockaddr *)&local_addr, sizeof(struct sockaddr_in));
        LOGD(TAG,"bind result is %d \n",ret);
        if(tryCount > MAX_TRY_BIND_COUNT) 
        {
            return;
        }
    }

    result->socket = sock;
    result->port = tryPort;
    LOGD(TAG,"bind tryPort is %d \n",tryPort);
    LOGD(TAG,"bind socket is %d \n",sock);
}
int64_t *my_random_newPermutation(int64_t minValueIncluded,
		int64_t maxValueNotIncluded) {
	int64_t interval_size = maxValueNotIncluded - minValueIncluded;
	my_assert_greaterInt("random interval", interval_size, 0);
	int64_t *array = MY_MALLOC(interval_size, int64_t);
	for (int64_t i = 0; i < interval_size; ++i)
		array[i] = minValueIncluded + i;
	MY_MUTEX_LOCK(rnd_mutex);
	internal_ensure_seed();
	for (int64_t i = 0; i < interval_size - 1; i++) {
		int64_t rnd = internal_random_int(interval_size - i);
		int64_t pos = i + rnd;
		int64_t swp = array[i];
		array[i] = array[pos];
		array[pos] = swp;
	}
	MY_MUTEX_UNLOCK(rnd_mutex);
	return array;
}
MyVectorInt *my_random_sampleNoRepetitionsSorted(int64_t minValueIncluded,
		int64_t maxValueNotIncluded, double sampleSizeOrFraction) {
	int64_t max_size = maxValueNotIncluded - minValueIncluded;
	int64_t sample_size;
	if (sampleSizeOrFraction >= 1)
		sample_size = my_math_round_int(sampleSizeOrFraction);
	else if (sampleSizeOrFraction > 0)
		sample_size = my_math_round_int(sampleSizeOrFraction * max_size);
	else
		sample_size = max_size;
	sample_size = MIN(max_size, MAX(0, sample_size));
	int64_t *positions = MY_MALLOC(sample_size, int64_t);
	if (sample_size == max_size) {
		for (int64_t i = 0; i < max_size; ++i)
			positions[i] = minValueIncluded + i;
	} else {
		my_random_intList_noRepetitions(minValueIncluded, maxValueNotIncluded,
				positions, sample_size);
		my_qsort_int_array(positions, sample_size);
	}
	return my_vectorInt_new_wrapper(positions, sample_size, true);
}
void get_min_itemset(int *_tot_depth, int *exprs_col_major, int *_nrow, int *_ncol, double *w1, double *w2, int *active, int *_active_len, double *_lambda, double *curmin, int *min_idxs, int *trace) {
    
    g_trace = *trace;
    
    int tot_depth = *_tot_depth,
    nrow = *_nrow,
    ncol = *_ncol,
    active_len = *_active_len;
    double lambda = *_lambda;
    
    // printf("testing input:\ntot_depth: %d\nnrow: %d\nncol: %d\nw1: %.10f\nw2: %.10f\nactive: ", tot_depth, nrow, ncol, w1[0], w2[0]);
    //     printf("##DEBUG: active set: ");
    //     for(int i=(active_len-1) * tot_depth; i < active_len * tot_depth; i++)
    //        printf("%d ", active[i]);
    //    printf("\n\n");
    // printf("\n");
    // printf("\nlambda: %.4f\ncurmin: %.4f\nexprs:\n", lambda, *curmin);
    // for(int i=0; i < 100; i++)
    //    printf("%d ", (int) exprs_row_major[i]);
    // printf("\n");
    
    double *w1p = MY_MALLOC(nrow * sizeof(double));
    CHECK_PTR(w1p, "w1p could not be allocated");
    double *w1m = MY_MALLOC(nrow * sizeof(double));
    CHECK_PTR(w1m, "w1m could not be allocated");
    double *w2p = MY_MALLOC(nrow * sizeof(double));
    CHECK_PTR(w2p, "w2p could not be allocated");
    double *w2m = MY_MALLOC(nrow * sizeof(double));
    CHECK_PTR(w2m, "w2m could not be allocated");
    char *exprs = MY_MALLOC(ncol * nrow); //column-major
    CHECK_PTR(exprs, "exprs could not be allocated");
    weight_ratios = MY_MALLOC(nrow * sizeof(double));
    
    for (int i = 0; i < nrow; i++) {
        w1p[i] = max(0., w1[i]);
        w1m[i] = max(0., -w1[i]);
        w2p[i] = max(0., w2[i]);
        w2m[i] = max(0., -w2[i]);
    }
    
    for (int i = 0; i < active_len*tot_depth; i++)
        active[i]--;
    
    for (int i = 0; i < tot_depth; i++)
        min_idxs[i] = -1;
    
    
    for (int j = 0; j < nrow; j++)
        for (int i = 0; i < ncol; i++)
            exprs[i*nrow+j] = exprs_col_major[i*nrow+j];
    // exprs[i*nrow+j] = exprs_row_major[j*ncol+i];
    
    if(*curmin == 0 || *curmin > lambda)
        *curmin = lambda;
    
    tot_node_explored = 0;
    tot_non_leaf_explored = 0;
    
    
    struct get_min_arg_struct args;
    args.tot_depth = tot_depth;
    args.exprs = exprs;
//    args.single_exprs = MY_MALLOC(ncol * nrow);
//    CHECK_PTR(args.single_exprs, "args.single_exprs could not be allocated");
//    
//    memcpy(args.single_exprs, exprs, ncol*nrow);
    args.nrow = nrow;
    args.ncol = ncol;
    args.w1p = w1p;
    args.w1m = w1m;
    args.w2p = w2p;
    args.w2m = w2m;
    args.num_const = lambda;
    args.denom_const = 1;
    args.curmin = curmin;
    args.combi_min_idx = min_idxs;
    args.active = active;
    args.active_len = active_len;
    
    pthread_mutex_t mutex;
    args.mutex = &mutex;
    
    pthread_mutex_init(args.mutex, NULL);
    
    pthread_t sub1, sub2;
    
    pthread_create(&sub1, NULL, (void *) launch_get_min_recur, &args);
    
    struct get_min_arg_struct args2 = args;
    args2.num_const = -lambda;
    args2.denom_const = -1;
    
    pthread_create(&sub2, NULL, (void *) launch_get_min_recur, &args2);
    
    pthread_join(sub1, NULL);
    pthread_join(sub2, NULL);
    
    pthread_mutex_destroy(args.mutex);
    
    for (int i = 0; i < args.tot_depth; i++) {
        min_idxs[i] = min_idxs[i]+1;  //IMPORTANT: must increment!
        // printf("[%d]", min_idxs[i]);
    }
    
    if(g_trace >= 2)
        printf("Total explored: %.0f non-terminal (%.0f total)\n Pruned: %ld + %ld(/%ld)\n\n", tot_non_leaf_explored, tot_node_explored, easy_pruned, pruned, pruned+not_pruned);
    
    
//    MY_FREE(args.single_exprs);
//    MY_FREE(args2.single_exprs);
    
    MY_FREE(w1p);
    MY_FREE(w1m);
    MY_FREE(w2p);
    MY_FREE(w2m);
    MY_FREE(exprs);
    MY_FREE(weight_ratios);
    // printf(" (lambda: %.4f)\n", lambda);
    
}
void get_min_recur(int cur_depth, int tot_depth, int *cur_path,
                   char *cur_exprs, char *single_exprs, int nrow, int ncol,
                   double *w1p, double *w1m, double *w2p, double *w2m,
                   double num_const, double denom_const,
                   double *curmin, int *combi_min_idx, bool *skip_single,
                   int *active, int active_len, pthread_mutex_t *mutex) {
        
    
    
    double *a = MY_MALLOC(ncol * sizeof(double));
    CHECK_PTR(a, "a could not be allocated");
    double *b = MY_MALLOC(ncol * sizeof(double));
    CHECK_PTR(b, "b could not be allocated");
    double *c = MY_MALLOC(ncol * sizeof(double));
    CHECK_PTR(c, "c could not be allocated");
    double *d = MY_MALLOC(ncol * sizeof(double));
    CHECK_PTR(d, "d could not be allocated");
    double *gamma = MY_MALLOC(ncol * sizeof(double));
    CHECK_PTR(gamma, "gamma could not be allocated");

    double minval = DBL_MAX;
    int min_idx = -1;

    for (int j = 0; j < ncol; j++) {
        if(skip_single[j])
            continue;

        tot_node_explored++;

        a[j] = 0;
        b[j] = 0;
        c[j] = 0;
        d[j] = 0;
        for (int i = 0; i < nrow; i++) {
            if(cur_exprs[i + j*nrow]) {
                a[j] += w1p[i];
                b[j] += w1m[i];
                c[j] += w2p[i];
                d[j] += w2m[i];
            }
        }
                    
        gamma[j] = (num_const + a[j] - b[j]) / (denom_const + c[j] - d[j]);
        
        if(gamma[j] > EPSILON && gamma[j] < minval && !is_idx_in_set(cur_path, tot_depth, j, active, active_len)) {
            minval = gamma[j];
            min_idx = j;
        }
    }
//    printf("#%.0f | Single skipped: %ld\nskip_single[17375] = %d\n", tot_explored, single_skipped, skip_single[17375]);
    
//    int min_idx = get_pos_min_idx(gamma, ncol, cur_path, tot_depth, active, active_len);
    
    if (min_idx >= 0 && gamma[min_idx] < *curmin) {
        bool is_duplicate = false;
        for(int i = 0; i < cur_depth; i++)
            if(cur_path[i] == min_idx) {
                is_duplicate = true;
                break;
            }
        
        pthread_mutex_lock(mutex);
        if(! is_duplicate && gamma[min_idx] < *curmin) { // Checking again here to avoid race conditions
            
            *curmin = gamma[min_idx];
            for (int i = 0; i < tot_depth; i++)
                combi_min_idx[i] = cur_path[i];
            combi_min_idx[cur_depth] = min_idx;
            
            if(g_trace >= 3) {
                printf("New min: %.3f ", *curmin);
                for (int i = 0; i < tot_depth; i++)
                    printf("[%d]", combi_min_idx[i]);
                printf("\n");
            }
            
        }
        pthread_mutex_unlock(mutex);
    }
    
    
    if(cur_depth+1 < tot_depth) {
        
        
        char *combi_exprs = MY_MALLOC(ncol*nrow);
        CHECK_PTR(combi_exprs, "combi_exprs could not be allocated");
        char *exprs_mask = MY_MALLOC(ncol*nrow);
        if(exprs_mask == NULL) {
            printf("exprs_mask could not be allocated: %d * %d = %d\ncurdepth: %d\n", ncol, nrow, ncol*nrow, cur_depth);
            exit(-1);
        }
        
        clock_t begin=clock();
        
        char null_block [nrow];
        bzero(null_block, nrow);
        
        char *x = MY_MALLOC(nrow);
        int *x_subset = MY_MALLOC(nrow * sizeof(int));
        int *ordered = MY_MALLOC(nrow * sizeof(int));
        int *ranking = MY_MALLOC(nrow * sizeof(int));

        
        for(int j = 0; j < ncol; j++) {
            if(skip_single[j]) {
                easy_pruned++;
                goto skip;
            }

#ifdef USE_HEURISTIC_1
            if((denom_const - d[j] >= 0
                && (((num_const - b[j]) / (denom_const + c[j]) >= *curmin) ||
                    ((num_const + a[j]) / (denom_const - d[j]) <= EPSILON)))
               || (denom_const + c[j] <= 0
                   && (((num_const + a[j]) / (denom_const - d[j]) >= *curmin) ||
                       ((num_const - b[j]) / (denom_const + c[j]) <= EPSILON)))) {
                       
                easy_pruned++;
                goto skip;
            }
#endif
            
            for(int i = 0; i < cur_depth; i++)
                if(cur_path[i] == j)
                    goto skip;

#ifdef USE_HEURISTIC_2

            if(denom_const - d[j] > 0 || denom_const + c[j] < 0) {
                
                bool pos_denom = (denom_const - d[j] > 0);
                double num, denom;
                
                int tot_subset = 0;
                                
                for (int i = 0; i < nrow; i++) {
                    
                    if(cur_exprs[i + j*nrow] > 0) {
                        
                        if(pos_denom) {
                            num = w1p[i] - w1m[i];
                            denom = w2p[i] - w2m[i];
                        }
                        else {
                            num = w1m[i] - w1p[i];
                            denom = w2m[i] - w2p[i];
                        }
                        
                        if((num < 0 && denom >= 0) || (num == 0 && denom > 0))
                            x[i] = 1;
                        else if((num > 0 && denom <= 0) || (num == 0 && denom < 0))
                            x[i] = 0;
                        else if(num == 0 && denom == 0)
                            x[i] = 0;
                        else {
                            x_subset[tot_subset++] = i;
                            
                            if(num < 0 && denom < 0) // both strictly negative -> equiv. to both positives, taking !x afterward
                                x[i] = -2;
                            else // both strictly positive
                                x[i] = -1;
                        }
                    }
                    else
                        x[i] = 0;
                }
                
                double optim_min = MAXFLOAT;
                
                if(tot_subset > 0)  {
                    
                    pthread_mutex_lock(mutex);
                    for (int i = 0; i < tot_subset; i++) {
                        int sub_i = x_subset[i];
                        if(pos_denom)
                            ordered[i] = tot_subset-1-i;
                        else
                            ordered[i] = i;
                        weight_ratios[i] = (w1m[sub_i] - w1p[sub_i])/(w2m[sub_i] - w2p[sub_i]);
                    }
                    qsort(ordered, tot_subset, sizeof(int), order_of_double_cmp);
                    pthread_mutex_unlock(mutex);

                    for (int i = 0; i < tot_subset; i++)
                        ranking[ordered[i]] = i;
                    
                    
                    
                    
                    double optim_min_n_found, optim_min_d_found;
//                    int save_k = 0;
                    
                    for (int i = -1; i <= tot_subset; i++) {
                        int sub_k = 0;
                        optim_min_n_found = num_const;
                        optim_min_d_found = denom_const;
                        
                        for (int k = 0; k < nrow; k++) {
                            if((x[k] > 0) || (x[k] == -1 && ranking[sub_k] <= i) || (x[k] == -2 && ranking[sub_k] > i)) {
                                optim_min_n_found += w1p[k] - w1m[k];
                                optim_min_d_found += w2p[k] - w2m[k];
                            }
                            
                            if(x[k] < 0)
                                sub_k++;
                        }
                        if(optim_min_n_found / optim_min_d_found < optim_min) {
                            optim_min = optim_min_n_found / optim_min_d_found;
//                            save_k = i;
                            if(optim_min < *curmin)
                                break;
                        }
                    }
                    
                }
                else {
                    double optim_min_n = num_const, optim_min_d = denom_const;
                    for (int i = 0; i < nrow; i++) {
                        if(x[i] > 0) {
                            optim_min_n += w1p[i] - w1m[i];
                            optim_min_d += w2p[i] - w2m[i];
                        }
                    }
                    optim_min = optim_min_n/optim_min_d;
                }

                
                if(optim_min >= *curmin) {
                    pruned++;
                    goto skip;
                }
                else
                    not_pruned++;
            }
#endif
            

            if(cur_depth > 0) { //doing breadth first for first level saves time
                for(int k = 0; k < ncol; k++)
                    memcpy(&exprs_mask[k*nrow], &cur_exprs[j*nrow], nrow);
                
                bool non_null = false;
                for (int i = 0; i < nrow*ncol / sizeof(uint64_t); i++) {
                    ((uint64_t *) combi_exprs)[i] = ((uint64_t *) single_exprs)[i] & ((uint64_t *) exprs_mask)[i];
                    non_null |= (((uint64_t *) combi_exprs)[i] != 0);
                }
                for (int i = nrow*ncol - (nrow*ncol % sizeof(uint64_t)); i < nrow*ncol; i++) {
                    combi_exprs[i] = single_exprs[i] & exprs_mask[i];
                    non_null |= (combi_exprs[i] != 0);
                }
                
                if (! non_null) {
                    easy_pruned++;
                    goto skip;
                }

                cur_path[cur_depth] = j;
                get_min_recur(cur_depth+1, tot_depth, cur_path, combi_exprs, single_exprs, nrow, ncol, w1p, w1m, w2p, w2m, num_const, denom_const, curmin, combi_min_idx, skip_single, active, active_len, mutex);
            }
            
            tot_non_leaf_explored++;

            continue;
            
            skip:
            if(cur_depth == 0)
                skip_single[j] = TRUE;
        }
        
        if (cur_depth == 0) {
            for (int j = 0; j < ncol; j++) {
                
                if(skip_single[j])
                    continue;
                
                for(int k = 0; k < ncol; k++)
                    memcpy(&exprs_mask[k*nrow], &cur_exprs[j*nrow], nrow);
                
                bool non_null = false;
                for (int i = 0; i < nrow*ncol / sizeof(uint64_t); i++) {
                    ((uint64_t *) combi_exprs)[i] = ((uint64_t *) single_exprs)[i] & ((uint64_t *) exprs_mask)[i];
                    non_null |= (((uint64_t *) combi_exprs)[i] != 0);
                }
                for (int i = nrow*ncol - (nrow*ncol % sizeof(uint64_t)); i < nrow*ncol; i++) {
                    combi_exprs[i] = single_exprs[i] & exprs_mask[i];
                    non_null |= (combi_exprs[i] != 0);
                }
                
                if (! non_null) {
                    easy_pruned++;
                    continue;
                }

                cur_path[cur_depth] = j;
                get_min_recur(cur_depth+1, tot_depth, cur_path, combi_exprs, single_exprs, nrow, ncol, w1p, w1m, w2p, w2m, num_const, denom_const, curmin, combi_min_idx, skip_single, active, active_len, mutex);

            }
        }
        
        
        cur_path[cur_depth] = -1;
        
        MY_FREE(ordered);
        MY_FREE(ranking);

        MY_FREE(x);
        MY_FREE(x_subset);

        MY_FREE(combi_exprs);
        MY_FREE(exprs_mask);
        
        clock_t end=clock();
        double diff = (end - begin)*1000/CLOCKS_PER_SEC;
        if(tot_non_leaf_explored > 0 && cur_depth < 1) {
            if(g_trace >= 2)
                printf("Explored %.0f non-terminal so far (total: %.2f s.).\n", tot_non_leaf_explored, diff/1000);
        }
    }
    
    MY_FREE(a);
    MY_FREE(b);
    MY_FREE(c);
    MY_FREE(d);
    MY_FREE(gamma);
    
    return;
}
char *solve_it(char *inputData)
{
   pynum_t *values;
   pynum_t *weights;


   /* Parse the first line */
   char *line = strtok(inputData, "\r\n");

   if (line == nullptr) {
      puts("ERROR: input file malformed");
      return nullptr;
   }

   char *pSpace;

   const int numItems = strtoul(line, &pSpace, 10);
   const pynum_t capacity = strtoul(pSpace, nullptr, 10);
   line = strtok(nullptr, "\r\n");

   values = (pynum_t *)MY_MALLOC(sizeof(pynum_t)*numItems);
   weights = (pynum_t *)MY_MALLOC(sizeof(pynum_t)*numItems);
   
   /* Parse the rest of the data */
   int itemNum = 0;
   while(line != nullptr) {
      values[itemNum] = strtoul(line, &pSpace, 10);
      weights[itemNum] = strtoul(pSpace, nullptr, 10);

      ++itemNum;
      line = strtok(nullptr, "\r\n");
   }
  
   pynum_t rows = capacity + 1;
   pynum_t cols = numItems + 1;

   puts("Initializing table...");

   pynum_t *table = (pynum_t *)MY_MALLOC(sizeof(pynum_t) * rows * cols);
      
   /* Zero out the first row and colum */
   for (pynum_t col = 0; col < cols; ++col) {
      TBL_VAL(0, col) = 0;
   }

   for (pynum_t row = 0; row < rows; ++row) {
      TBL_VAL(row, 0) = 0;
   }

   puts("Done initializing table...\n");

   /* Build the table */
   puts("Building Table...");
   for (pynum_t col = 1; col < cols; ++col) {
      for (pynum_t row = 1; row < rows; ++row) {
         TBL_VAL(row, col) = 
            weights[col-1] <= row ?
               max(TBL_VAL(row, col-1), values[col-1] + TBL_VAL(row - weights[col-1], col-1)) :
               TBL_VAL(row, col-1);
      }
   }
   puts("Done building table.\n");
   

   //puts("Table:");
   //for (pynum_t row = 0; row < rows; ++row) {
   //   fputs("|", stdout);
   //   for (pynum_t col = 0; col < cols-1; ++col) {
   //      printf("%llu, ", TBL_VAL(row, col));
   //   }
   //   printf("%llu", TBL_VAL(row, cols-1));
   //   fputs("|\n", stdout);
   //}
   
   puts("\nTracking backward...");
   pynum_t weightRem = capacity;
   itemNum = (int)(numItems-1);
   while (itemNum >= 0) {
      if (TBL_VAL(weightRem, itemNum) != TBL_VAL(weightRem, itemNum+1)) {
         values[itemNum] = 1;
         weightRem -= weights[itemNum];
      } else {
         values[itemNum] = 0;
      }

      // printf("\tAssigning item %llu = %llu\n", itemNum, values[itemNum]);
      --itemNum;
   }
   puts("Done tracking backward.\n");

   /* Build the solution string.  
      Remember that the 'taken' value for each item is stored in values.
      Estimate the string length based on a generous guess
   */

   char *retString = (char *)MY_MALLOC(sizeof(char) * (100 + 100*numItems));

   if (retString == nullptr) {
      puts("ERROR: Couldn't allocate return string.");
      return nullptr; 
   }

   puts("Writing solution string...");
   int written = sprintf(retString, "%llu 1\n", TBL_VAL(rows-1, cols-1));
   
   for (itemNum = 0; itemNum < numItems; ++itemNum) {
      written += sprintf((retString+written), "%llu ", values[itemNum]);
   }
   retString[written] = '\0';
   puts("Done writing solution string...\n");

   MY_FREE(weights);
   MY_FREE(values);
   MY_FREE(table);
   return retString;
}
Example #20
0
int startListenIFrame(int sock,onAcceptIFrame callback,int id) 
{
    char buf[TCP_IFRAME_LENGTH];
    char *localbuf = buf;

    while(1)
    {
        struct sockaddr_in client_addr;
        socklen_t length = sizeof(client_addr); 
        if (listen(sock, LENGTH_OF_LISTEN_QUEUE) != 0) {
            LOGE(TAG,"startListenIFrame , error = %s \n", strerror(errno));
        }

        LOGE(TAG,"startListenIFrame trace1 \n");
        int client_fd = accept(sock,( struct sockaddr *)&client_addr,&length);

        LOGE(TAG,"startListenIFrame trace2 \n");
        if (client_fd < 0) {
            LOGE(TAG,"startListenIFrame accept faild error = %s,sock is %d,error is %d \n", strerror(errno),sock,errno);
            continue;
        }

        long recvbytes = 0;
        unsigned long totalLength = 0;
        char *savebuf = NULL;
        char *savebufCursor = NULL;
        int totalrecv = 0;
        while(1)
        {
            recvbytes = recv(client_fd,localbuf,TCP_IFRAME_LENGTH,0);
            totalrecv += recvbytes;
            LOGD(TAG,"startListenIFrame trace1 recvbytes is %d \n",recvbytes);  
            if(recvbytes <= 0)
            {
                LOGE(TAG,"startListenIFrame,receive sock failed! error = %s \n",strerror(errno));
                break;
            }


            if(totalLength == 0)
            {
                localbuf = prase_token_sint(localbuf,&totalLength);
                savebuf = MY_MALLOC(totalLength - sizeof(int),"startListensave");
                memset(savebuf,0,totalLength - sizeof(int));

                memcpy(savebuf,
                       localbuf,
                       recvbytes - sizeof(int));

                savebufCursor = savebuf + recvbytes - sizeof(int);
                LOGE(TAG,"startListenIFrame,receive totallength is %d \n",totalLength);
            }
            else
            {
                memcpy(savebufCursor,localbuf,recvbytes);
                savebufCursor += recvbytes;
            }

            localbuf = buf;
            memset(localbuf,0,TCP_FILE_BUFFER_LENGTH);
        }
        LOGE(TAG,"startListenIFrame,total receive is  %d \n",totalrecv);
        callback(savebuf,totalLength,id);
#ifdef DEBUG_IFRAME_BUFF

#endif
        close(client_fd);
     }

     return ACCEPT_IFRAME_FAIL;

}
Example #21
0
void *broadcastserverStart(void *data) 
{
    //we should init udp listener.
    long ret = -1;
    int sock = -1;
    unsigned int len = -1;
    char *buf = MY_MALLOC(LOACL_BUFFER_LENGTH,"broadcaststartbuf");
    fd_set readfd;
    struct timeval timeout;
    struct sockaddr_in local_addr;
    struct sockaddr_in from_addr;

    LOGD(TAG,"broadcastserverStart start \n");

    //socket
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if(0 > sock)
    {
        return NULL;
    }

    len = sizeof(struct sockaddr_in);
    memset(&local_addr, 0, sizeof(struct sockaddr_in));
    local_addr.sin_family = AF_INET;                    
    local_addr.sin_addr.s_addr = htonl(INADDR_ANY);   //local address
    struct globalConfig *config = getConfig();
    
    local_addr.sin_port = htons(config->broadcast_server_port);                //listen port

    ret = bind(sock, (struct sockaddr *)&local_addr, sizeof(struct sockaddr_in));
    if(0 > ret)
    {
        return NULL;
    }
    LOGD(TAG,"broadcastserverStart port is %d \n",config->broadcast_server_port);
    //set the time out 
    timeout.tv_sec = 120;        //seconds
    timeout.tv_usec = 0;        //mseconds
    
    //we should wait for the udp broadcast
    while(1)
    {
        //clean fd set
        FD_ZERO(&readfd);

        //add sock fd to readfd
        FD_SET(sock, &readfd);

        //start listen
        LOGE(TAG,"broadcastserverstart,receive trace1\n");
        ret = select(sock+1, &readfd, NULL, NULL, NULL);
        LOGE(TAG,"broadcastserverstart,receive trace2\n");
        switch(ret)
        {
            case -1:
            case 0:
                continue;
                break;

            default:
            {
                //confirm whether there are some datas in the sock
                
                if(FD_ISSET(sock, &readfd))
                {
                    ret = recvfrom(sock, buf, LOACL_BUFFER_LENGTH, 0, (struct sockaddr *)&from_addr, &len);
                    doProcDispatch(buf,ret,from_addr);
                    if(0 > ret)
                    {
                        LOGE(TAG,"broadcastserverstart,receive sock failed! \n");
                        continue;
                    }
                    memset(buf,0,LOACL_BUFFER_LENGTH);
                }
            }     
        }
        
     }

     return NULL;
}