Example #1
0
/* Print a statement for copying an array to or from the device.
 * The statement identifier is called "to_device_<array name>" or
 * "from_device_<array name>" and its user pointer points
 * to the gpu_array_info of the array that needs to be copied.
 *
 * Extract the array from the identifier and call
 * copy_array_to_device or copy_array_from_device.
 */
static __isl_give isl_printer *print_to_from_device(__isl_take isl_printer *p,
	__isl_keep isl_ast_node *node, struct gpu_prog *prog)
{
	isl_ast_expr *expr, *arg;
	isl_id *id;
	const char *name;
	struct gpu_array_info *array;

	expr = isl_ast_node_user_get_expr(node);
	arg = isl_ast_expr_get_op_arg(expr, 0);
	id = isl_ast_expr_get_id(arg);
	name = isl_id_get_name(id);
	array = isl_id_get_user(id);
	isl_id_free(id);
	isl_ast_expr_free(arg);
	isl_ast_expr_free(expr);

	if (!name)
		array = NULL;
	if (!array)
		return isl_printer_free(p);

	if (!prefixcmp(name, "to_device"))
		return copy_array(p, array, 0);
	else
		return copy_array(p, array, 1);
}
int main(int argc,char *argv[])
{
	int N=atoi(argv[1]);
	item_t *array=malloc(N*sizeof(*array));
	item_t *array_2=malloc(N*sizeof(*array_2));
	int i;
	FILE *input;
	clock_t c1,c2;

	input=fopen("merged_sequence.dat","r");
	
	srand((unsigned)time(NULL));

	for(i=0;i<N;i++){
		array[i]=rand()%N;
	}

	copy_array(array_2,array,N);

	while(fgets(buffer,BUFFER_SIZE,input) != NULL){
		copy_array(array,array_2,N);
	
		c1=clock();
		shell_sort(array,0,N-1);
		c2=clock();
	
		printf("sequence: %s",buffer);
		printf("sort time: %fs\n\n",(double)(c2-c1)/CLOCKS_PER_SEC);
	}

	return(0);
}
Example #3
0
File: sort.c Project: bortoq/zerovm
/**@param global_array_index is used to save result to correct place*/
BigArrayPtr
alloc_merge(
		const BigArrayPtr left_array, int left_array_len,
		const BigArrayPtr right_array, int right_array_len ){
	BigArrayPtr larray = left_array;
	BigArrayPtr rarray = right_array;
	BigArrayPtr result = malloc( sizeof(BigArrayItem) *(left_array_len+right_array_len));
	if ( !result ){
		WRITE_FMT_LOG(LOG_DEBUG, "NULL pointer, for len=%d\n", left_array_len+right_array_len );
		return NULL;
	}
	int current_result_index = 0;
	while ( left_array_len > 0 && right_array_len > 0 ){
		if ( larray[0] <= rarray[0]  ){
			result[current_result_index++] = larray[0];
			++larray;
			--left_array_len;
		}
		else{
			result[current_result_index++] = rarray[0];
			++rarray;
			--right_array_len;
		}
	}

	//copy last item
	if ( left_array_len > 0 ){
		copy_array( result+current_result_index, larray, left_array_len );
	}
	if ( right_array_len > 0 ){
		copy_array( result+current_result_index, rarray, right_array_len );
	}

	return result;
}
Example #4
0
File: sort.c Project: bortoq/zerovm
BigArrayPtr merge( BigArrayPtr dest_array, BigArrayPtr left_array, int left_array_len,
		BigArrayPtr right_array, int right_array_len ){
	if (!dest_array) return NULL;
	BigArrayPtr larray = left_array;
	BigArrayPtr rarray = right_array;
	int current_result_index = 0;
	while ( left_array_len > 0 && right_array_len > 0 ){
		if ( larray[0] <= rarray[0]  ){
			dest_array[current_result_index++] = larray[0];
			++larray;
			--left_array_len;
		}
		else{
			dest_array[current_result_index++] = rarray[0];
			++rarray;
			--right_array_len;
		}
	}

	//copy last item
	if ( left_array_len > 0 ){
		copy_array( dest_array+current_result_index, larray, left_array_len );
	}
	if ( right_array_len > 0 ){
		copy_array( dest_array+current_result_index, rarray, right_array_len );
	}

	return dest_array;
}
Example #5
0
File: motif.c Project: CPFL/gmeme
/***********************************************************************
 * Turn a given motif into its own reverse complement.
 ***********************************************************************/
void reverse_complement_motif
  (MOTIF_T* a_motif)
{
  ALPH_SIZE_T size;
  int i, temp_trim;
  ARRAY_T* left_freqs;
  ARRAY_T* right_freqs;
  ARRAY_T* temp_freqs;   // Temporary space during swap.

  assert(a_motif->alph == DNA_ALPH);

  // Allocate space.
  size = (a_motif->flags & MOTIF_HAS_AMBIGS ? ALL_SIZE : ALPH_SIZE);
  temp_freqs = allocate_array(alph_size(a_motif->alph, size));

  // Consider each row (position) in the motif.
  for (i = 0; i < (int)((a_motif->length + 1) / 2); i++) {
    left_freqs = get_matrix_row(i, a_motif->freqs);
    right_freqs = get_matrix_row(a_motif->length - (i + 1), a_motif->freqs);

    // Make a temporary copy of one row.
    copy_array(left_freqs, temp_freqs);

    // Compute reverse complements in both directions.
    complement_dna_freqs(right_freqs, left_freqs);
    complement_dna_freqs(temp_freqs, right_freqs);
  }
  free_array(temp_freqs);
  if (a_motif->scores) {
    // Allocate space.
    temp_freqs = allocate_array(alph_size(a_motif->alph, ALPH_SIZE));

    // Consider each row (position) in the motif.
    for (i = 0; i < (int)((a_motif->length + 1) / 2); i++) {
      left_freqs = get_matrix_row(i, a_motif->scores);
      right_freqs = get_matrix_row(a_motif->length - (i + 1), a_motif->scores);

      // Make a temporary copy of one row.
      copy_array(left_freqs, temp_freqs);

      // Compute reverse complements in both directions.
      complement_dna_freqs(right_freqs, left_freqs);
      complement_dna_freqs(temp_freqs, right_freqs);
    }
    free_array(temp_freqs);
  }
  //swap the trimming variables
  temp_trim = a_motif->trim_left;
  a_motif->trim_left = a_motif->trim_right;
  a_motif->trim_right = temp_trim;
  //swap the strand indicator
  //this assumes a ? is equalivant to +
  if (get_motif_strand(a_motif) == '-') {
    set_motif_strand('+', a_motif);
  } else {
    set_motif_strand('-', a_motif);
  }
}
Example #6
0
void receive_msg()
{
   unsigned int16 ip, lbody, index, pl;
   unsigned int8 msg[USB_EP1_RX_SIZE - 1], code, *body;
   unsigned int1 hasBody;
   
   usb_get_packet(1, msg, USB_EP1_RX_SIZE - 1);
   
   ip = (msg[0] + (msg[1] * 256));
   hasBody = bit_test(msg[2], 7);
   code = msg[2];
   bit_clear(code, 7);
  
#ifdef DEBUG_STREAM_A
   fprintf(A, "receive_msg IP: %Lu - Code: %u - HasBody: %u\n\r", ip, code, hasBody);
#endif

   if (hasBody)
   {
      lbody = (msg[3] + (msg[4] * 256));
      
      body = calloc(lbody - 1, sizeof(unsigned int8));
      
      pl = lbody;
      if (pl > USB_EP1_RX_SIZE - 6)
         pl = USB_EP1_RX_SIZE - 6;
      
      copy_array(msg, 5, body, 0, pl);
      index = pl;
      
      while(index < lbody - 1)
      {
         pl = lbody - index;
         if (pl > USB_EP1_RX_SIZE - 1)
            pl = USB_EP1_RX_SIZE - 1;
      
         while (!usb_kbhit(1));
         usb_get_packet(1, msg, USB_EP1_RX_SIZE - 1);
         copy_array(msg, 0, body, index, pl);

         index += pl;
      }
   
      process_msg(ip, code, lbody, body);
   
      if (body)
      {
         free(body);
      }
   }
   else
   {
      process_msg(ip, code, 0, body);
   }
}
Example #7
0
void join_paths(char *path1,char *path2,char *new_path ){

/*	printf("strlen path1:%d\n",strlen(path1));
	printf("strlen path2:%d\n",strlen(path2));
	printf("path1; %s\n",path1);
*/
	add_to_buffer(&message_buffer, "joining paths:",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
	append_to_buffer(&message_buffer,path1,MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
	append_to_buffer(&message_buffer, " and ",MESSAGE_LEVEL_BASIC_TASKS,NPRINT);
	append_to_buffer(&message_buffer, path2,MESSAGE_LEVEL_BASIC_TASKS,PRINT);
	if ( (path1[(sizeof(path1)/sizeof(char))-2]=='/') && path2[0]!='/') {
		/*paths are compatiable. concatanate them. note -2 is because of \0*/  
		strcat(new_path,path1);
		strcat(new_path,path2);		
		//char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
		//strcpy(new_path,strcat(path1,path2)); 
		//return new_path;
	}	
	else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]=='/') {
		/*paths are compatiable. concatanate them*/  
		strcat(new_path,path1);
		strcat(new_path,path2);		
		//char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))];
		//strcpy(new_path,strcat(path1,path2)); 
		//return new_path;
	}
	else if ((path1[(sizeof(path1)/sizeof(char))-2]!='/') && path2[0]!='/') {
			/*need to add a "/". */  
		strcat(new_path,path1);
		strcat(new_path,"/");
		strcat(new_path,path2);
		//strcpy(new_path,strcat(path1,strcat("/\0",path2)));

#if 0
		copy_array(path1,new_path,0,0);
		copy_array('\0',new_path,0,(sizeof(path1)/sizeof(char)));
		copy_array(path2,new_path,0,(sizeof(path1)/sizeof(char))+1);
 old method now trying to use copy_array
		//char new_path[(sizeof(path1)/sizeof(char))+(sizeof(path2)/sizeof(char))+1];
		for (x=0;x<=(sizeof(path1)/sizeof(char))-1;x++){ 
			new_path[x]=path1[x];
		}
		new_path[x+1]='/';
		for (x=(sizeof(path1)/sizeof(char)) ,i=0 ;i<=(sizeof(path2)/sizeof(char));x++,i++){ 
			new_path[x]=path2[i]; 
		}
#endif

		//return new_path;
	}
Example #8
0
void run_all_except_insertion(int random_arr[], int sorted_arr[], int length)
{
    //copy of the input arrays to keep them intact
    int random_arr_copy[length];
    int sorted_arr_copy[length];
    
    //double arrays with running times
    double sorted_times[4];
    double random_times[4];
    
    //clock_t struct returned by the clock function for start and end times
    clock_t start, end;
    int ndx = 0;
    
    copy_array(random_arr, random_arr_copy, length);    
    copy_array(sorted_arr, sorted_arr_copy, length);
    
    //run quicksort getting elapsed time for each
    start = clock();
    quick_sort(random_arr_copy, 0, length - 1);
    end = clock();
    copy_array(random_arr, random_arr_copy, length);
    random_times[ndx] = ((double)(end - start)) / CLOCKS_PER_SEC;
    
    start = clock();
    quick_sort(sorted_arr_copy, 0, length - 1);
    end = clock();
    copy_array(sorted_arr, sorted_arr_copy, length);
    sorted_times[ndx++] = ((double)(end - start)) / CLOCKS_PER_SEC;
    
    //run heapsort
    start = clock();
    heap_sort(random_arr_copy, length, length);
    end = clock();
    copy_array(random_arr, random_arr_copy, length);
    random_times[ndx] = ((double)(end - start)) / CLOCKS_PER_SEC;
    
    start = clock();
    heap_sort(sorted_arr_copy, length, length);
    end = clock();
    copy_array(sorted_arr, sorted_arr_copy, length);
    sorted_times[ndx++] = ((double)(end - start)) / CLOCKS_PER_SEC;

    
    //run merge sort
    start = clock();
    merge_sort(random_arr_copy, 0, length - 1);
    end = clock();
    copy_array(random_arr, random_arr_copy, length);
    random_times[ndx] = ((double)(end - start)) / CLOCKS_PER_SEC;
    
    start = clock();
    merge_sort(sorted_arr_copy, 0, length - 1);
    end = clock();
    copy_array(sorted_arr, sorted_arr_copy, length);
    sorted_times[ndx++] = ((double)(end - start)) / CLOCKS_PER_SEC;
    
    output_table(random_times, sorted_times);
}
Example #9
0
void __init init_hypervisor_platform(void)
{
	const struct hypervisor_x86 *h;

	h = detect_hypervisor_vendor();

	if (!h)
		return;

	copy_array(&h->init, &x86_init.hyper, sizeof(h->init));
	copy_array(&h->runtime, &x86_platform.hyper, sizeof(h->runtime));

	x86_hyper_type = h->type;
	x86_init.hyper.init_platform();
}
Example #10
0
static void whittle2 (Array acf, Array Aold, Array Bold, int lag,
		      char *direction, Array A, Array K, Array E)
{

    int d, i, nser=DIM(acf)[1];
    const void *vmax;
    Array beta, tmp, id;

    d = strcmp(direction, "forward") == 0;

    vmax = vmaxget();

    beta = make_zero_matrix(nser,nser);
    tmp = make_zero_matrix(nser, nser);
    id = make_identity_matrix(nser);

    set_array_to_zero(E);
    copy_array(id, subarray(A,0));

    for(i = 0; i < lag; i++) {
       matrix_prod(subarray(acf,lag - i), subarray(Aold,i), d, 1, tmp);
       array_op(beta, tmp, '+', beta);
       matrix_prod(subarray(acf,i), subarray(Bold,i), d, 1, tmp);
       array_op(E, tmp, '+', E);
    }
    qr_solve(E, beta, K);
    transpose_matrix(K,K);
    for (i = 1; i <= lag; i++) {
	matrix_prod(K, subarray(Bold,lag - i), 0, 0, tmp);
	array_op(subarray(Aold,i), tmp, '-', subarray(A,i));
    }

    vmaxset(vmax);
}
Example #11
0
/**************************************************************************
*
*	reverse_complement_pssm_matrix
*
*	Turn a pssm matrix into its own reverse complement.
*
 *************************************************************************/
static void reverse_complement_pssm (
  ALPH_T alph,
  MATRIX_T* pssm_matrix
)
{
  int i;
  ARRAY_T* left_scores;
  ARRAY_T* right_scores;
  ARRAY_T* temp_scores;   // Temporary space during swap.
  int length = get_num_rows(pssm_matrix);

  // Allocate space.
  temp_scores = allocate_array(alph_size(alph, ALL_SIZE));

  // Consider each row (position) in the motif.
  for (i = 0; i < (int)((length+1) / 2); i++) {
    left_scores = get_matrix_row(i, pssm_matrix);
    right_scores = get_matrix_row(length - (i + 1), pssm_matrix);

    // Make a temporary copy of one row.
    copy_array(left_scores, temp_scores);

    // Compute reverse complements in both directions.
    complement_dna_freqs(right_scores, left_scores);
    complement_dna_freqs(temp_scores, right_scores);
  }
  free_array(temp_scores);
} // reverse_complement_pssm_matrix
Example #12
0
/* for a sub-part of main array S given by starting and
	ending indices, split in half, move pointers to a
	tmp array, and then walk the tmp arrays, restoring
	pointers back in S in sorted order */
void merge(int *S, int *tmp_S, int begin, int middle, int end)
{
	// some indices
	int i, j;

	copy_array(S + begin, tmp_S + begin, end);
	i = begin, j = middle;
	
	// printf("\told S:\t");
	// print_array(S, 0, 8);
	
	S += begin;

	// printf("\tmid S:\t");
	// print_array(S, 0, 8);
	
	while((i<middle) && (j<end))
		if(tmp_S[i] <= tmp_S[j]) 
			*S++ = tmp_S[i++];
		else
			*S++ = tmp_S[j++];
	
	while(i < middle)
		*S++ = tmp_S[i++];
	while(j < end)
		*S++ = tmp_S[j++];

	// printf("\tnew S:\t");
	// print_array(S, 0, 8);

}
Example #13
0
/***********************************************************************
 * Turn a given motif into its own reverse complement.
 * TODO this does not handle the scores matrix, and it should.
 ***********************************************************************/
void reverse_complement_motif
  (MOTIF_T* a_motif)
{
  int i, temp_trim;
  ARRAY_T* left_freqs;
  ARRAY_T* right_freqs;
  ARRAY_T* temp_freqs;   // Temporary space during swap.

  // Allocate space.
  //temp_freqs = allocate_array(get_alph_size(ALL_SIZE)); //this relys on a global which assumes DNA has ambigs which meme doesn't seem to use 
  temp_freqs = allocate_array(a_motif->alph_size + a_motif->ambigs);

  // Consider each row (position) in the motif.
  for (i = 0; i < (int)((a_motif->length + 1) / 2); i++) {
    left_freqs = get_matrix_row(i, a_motif->freqs);
    right_freqs = get_matrix_row(a_motif->length - (i + 1), a_motif->freqs);

    // Make a temporary copy of one row.
    copy_array(left_freqs, temp_freqs);

    // Compute reverse complements in both directions.
    complement_dna_freqs(right_freqs, left_freqs);
    complement_dna_freqs(temp_freqs, right_freqs);
  }
  free_array(temp_freqs);
  //swap the trimming variables
  temp_trim = a_motif->trim_left;
  a_motif->trim_left = a_motif->trim_right;
  a_motif->trim_right = temp_trim;
}
Example #14
0
/* Checks state safety. Returns true if safe, else false. */
bool is_safe() {
    /* Work vector, length m. */
    int work[RESOURCES];
    /* Finish vector, length n. */
    bool finish[CUSTOMERS];

    /* Initialize work vector = available vector. */
    copy_array(available, work);

    /* Set all elements in finish vector to false. */
    set_all_false(finish);

    /* Find index i such that finish[i] == false && need[i] <= work. */
    int i = find_i(work, finish);

    /* If no such i exists, check if all finish elements are true. */
    if (i == -1) { //such I does not exist
        if (all_true(finish) == true) {
            /* The system is in a safe state! */
            return true;
        }
    }
    else { 
        /* work = work + allocation */
        add_vectors(work, allocation[i]);
        finish[i] = true;
        /* Go to step 2. */
        i = find_i(work, finish);
    }
}
Example #15
0
void Array2D<T>::setColumn(int columns)
{
	if (columns < 0)
	{
		throw Exception("Number of columns must be greater than or equal to 0");
	}

	if (m_array.getLength() == 0)
	{
		m_col = columns;
		m_array.setLength(m_row * m_col);
	}
	else
	{
		Array<T> copy_array(m_row * columns);

		for (int i = 0; i < m_row; i++)
		{
			for (int j = 0; j < columns; j++)
			{
				if ((((i * columns) + j) - (i * (columns - m_col))) < ((i * m_col) + m_col))
				{
					copy_array[(i * columns) + j] = m_array[(i * m_col) + j];
				}
			}
		}
		m_col = columns;
		m_array = copy_array;
	}
}
Example #16
0
File: ctrls.c Project: OPSF/uClinux
int pr_ctrls_copy_args(pr_ctrls_t *src_ctrl, pr_ctrls_t *dst_ctrl) {

  /* Sanity checks */
  if (!src_ctrl || !dst_ctrl) {
    errno = EINVAL;
    return -1;
  }

  /* If source ctrl has no ctrls_cb_args member, there's nothing to be
   * done.
   */
  if (!src_ctrl->ctrls_cb_args)
    return 0;

  /* Make sure the pr_ctrls_t has a temporary pool, from which the args will
   * be allocated.
   */
  if (!dst_ctrl->ctrls_tmp_pool) {
    dst_ctrl->ctrls_tmp_pool = make_sub_pool(ctrls_pool);
    pr_pool_tag(dst_ctrl->ctrls_tmp_pool, "ctrls tmp pool");
  }

  /* Overwrite any existing dst_ctrl->ctrls_cb_args.  This is OK, as
   * the ctrl will be reset (cleared) once it has been processed.
   */
  dst_ctrl->ctrls_cb_args = copy_array(dst_ctrl->ctrls_tmp_pool,
    src_ctrl->ctrls_cb_args);

  return 0;
}
Example #17
0
int main(int argc, char* argv[]) {
    if (argc != NB_OF_ARGS + 1) {
        printf("Run the program with 10 command line arguments please.\n");
        return EXIT_FAILURE;
    }
    
    /* REPLACE WITH COMMENT WITH YOUR CODE */
    char* copy[NB_OF_ARGS + 1];
    copy_array(argv, copy, NB_OF_ARGS);
    // Reagrange words based on first digit, if any found in them.
    for (int w = 1; w <= NB_OF_ARGS; ++w) {
        int j = 0;
        char c;
        while((c = copy[w][j++])) {
            if(isdigit(c)) {
                int word_index = c - '0';
                if(word_index == 0)
                    word_index = 10;
                copy[w] = argv[word_index];
                break;
            }
        }
    }
    for (int w = 1; w <= NB_OF_ARGS; ++w)
        clean(copy[w]);
    
    for (int i = 1; i <= NB_OF_ARGS; ++i)
        printf("  %s", copy[i]);
    putchar('\n');
    return EXIT_SUCCESS;
}
Example #18
0
/*
 * Juho Gävert & Santeri Hiltunen
 Starting point of calculation. Searches for temperature balance in Array for
 maximum iterations of max_iters.
 */
double calculate_heatconduct(Array* arr, unsigned int max_iters)
{

  if (max_iters == 0 || arr->width < 3 || arr->height < 3)
    return -1;

  Array* temp_arr = new_array(arr->width, arr->height);
  copy_array(arr, temp_arr);

  double prev_mean = -1;
  for (unsigned int i = 0; i < max_iters; ++i) {
    double new_mean = calculate_iteration(arr, temp_arr);

    swap_ptrs((void**) &arr, (void**) &temp_arr);

    if (conf.verbose) {
      printf("Iter: %d Mean: %.15f\n", i + 1, new_mean);
      if (conf.verbose > 1) {
        print_arr(arr);
      }
    }

    if (fabs(new_mean - prev_mean) < 0.0000000000001) {
      printf("Found balance after %d iterations.\n", i);
      del_array(temp_arr);
      return new_mean;
    }

    prev_mean = new_mean;
  }
  del_array(temp_arr);
  printf("Didn't find balance after %d iterations.\n", max_iters);
  return prev_mean;
}
Example #19
0
void
f_assemble_class() {
   array_t *arr = copy_array( sp->u.arr );
   pop_stack();
   push_refed_array(arr);
   sp->type = T_CLASS;
}
Example #20
0
int main() {
	char **list = malloc(sizeof(char*) * 3);
	list[0] = "Hello, world";
	list[1] = "cat is very cute";
	list[2] = NULL;

	char **list2 = malloc(sizeof(char*) * 3);

	copy_array(list2, list);

	printf("%s\n%s\n%s\n", list[0], list[1], list[2]);
	printf("%s\n%s\n%s\n", list2[0], list2[1], list2[2]);

	char **list3 = concat_array(list, list2);
	char **iter = list3;
	while (*iter != NULL) {
		printf("%s\n", *iter);
		iter++;
	}

	printf("%d\n", array_length(list));
	printf("%d\n", array_length(list2));
	printf("%d\n", array_length(list3));

	/*
	free_array(list);
	free_array(list2);
	free_array(list3);
	*/
	
	return 0;
}
/************************************************************************
 * See .h file for description.
 ************************************************************************/
void copy_mhmm
  (MHMM_T*        an_mhmm,
   MHMM_T*        new_mhmm)
{
  int i_state;

  /* Copy the top-level data. */
  new_mhmm->type = an_mhmm->type;
  new_mhmm->log_odds = an_mhmm->log_odds;
  new_mhmm->num_motifs = an_mhmm->num_motifs;
  new_mhmm->num_states = an_mhmm->num_states;
  new_mhmm->num_spacers = an_mhmm->num_spacers;
  new_mhmm->spacer_states = an_mhmm->spacer_states;
  new_mhmm->alph = alph_hold(an_mhmm->alph);
  new_mhmm->background = allocate_array(alph_size_full(an_mhmm->alph));
  copy_array(an_mhmm->background, new_mhmm->background);
  copy_string(&(new_mhmm->description), an_mhmm->description);
  copy_string(&(new_mhmm->motif_file), an_mhmm->motif_file);
  copy_string(&(new_mhmm->sequence_file), an_mhmm->sequence_file);
  // FIXME: Copy hot states array.
  new_mhmm->num_hot_states = an_mhmm->num_hot_states;

  /* Copy each state. */
  for (i_state = 0; i_state < an_mhmm->num_states; i_state++) {
    copy_state(&(an_mhmm->states[i_state]),
               &(new_mhmm->states[i_state]));
  }

  /* Copy the transition matrix. */
  copy_matrix(an_mhmm->trans, new_mhmm->trans);
}
Example #22
0
static double multigrid_recurse(struct element **sel, double ****f,
		double ****x, int var, int level, double eps,
		void (*opeval) ())
{
	if (level == (*sel)->precon->nlevels - 1)
		return conjugate_gradient(sel, f, x, var, opeval,
				jacobi_preconditioner, eps, 500);

	const int m = 15;
	const int n1 = (*sel)->basis->n;
	const int n2 = (*sel)->precon->nmg[level + 1];
	double ****r = new_4d_array(n1, n1, n1, (*sel)->nel);
	double ****fn = new_4d_array(n2, n2, n2, (*sel)->nel);
	double ****e = new_4d_array(n2, n2, n2, (*sel)->nel);

	if (level == 0)
		compute_residual(sel, x, f, r, var, opeval);
	else
		copy_array(***f, ***r, n1 * n1 * n1 * (*sel)->nel);

	switch_multigrid_level(sel, level + 1);
	multigrid_restriction(sel, r, fn, level + 1);
	multigrid_recurse(sel, fn, e, var, level + 1, eps, opeval);
	switch_multigrid_level(sel, level);
	multigrid_prolongation(sel, e, r, level);
	add_array(***r, ***x, n1 * n1 * n1 * (*sel)->nel, 1.0);

	double err = conjugate_gradient(sel, f, x, var, opeval,
			jacobi_preconditioner, eps, m);
	free_4d_array(r);
	free_4d_array(fn);
	free_4d_array(e);
	return err;
}
Example #23
0
extern jobject _javabind_asObjectArray(int dim, jclass clazz, 
				      jobject (*asJava)(OBJ),
				      OBJ arr){

  jsize leng,i, excl; jarray jarr; 
  excl = excl_array(arr, 1); 
  leng = leng_array(arr);
  jarr = (*javabind_env)->NewObjectArray(javabind_env, leng,
					 array_type(dim-1, clazz),
					 NULL);
  javabind_catch_abort();
  if (dim > 1){
    for (i = 0; i < leng; i++){
      OBJ subArr = data_array(arr)[i];
      jobject jo;
      if (!excl) copy_array(subArr, 1);
      jo = _javabind_asObjectArray(dim-1, clazz, asJava, subArr);
      (*javabind_env)->SetObjectArrayElement(javabind_env, jarr, i, jo);
      javabind_catch_abort();
      (*javabind_env)->DeleteLocalRef(javabind_env, jo);
    }
  } else {
    /* passing asJava alone doesnt works anymore since 1.1b (the free method
       is required as well). Yet, the only unregular treatment of free
       currently is for _javabind_asObject; so a quick hack
       checking for the function address helps out. */
    if (asJava == _javabind_asObject){
      for (i = 0; i < leng; i++){
	OBJ elem = data_array(arr)[i];
	(*javabind_env)->SetObjectArrayElement(javabind_env, jarr, i,
					       get_jobject(elem));
	javabind_catch_abort();
	if (excl){
	  if (excl_jobject(elem, 1)){
	    _javabind_dispose(elem);
	  } else {
	    decr_jobject(elem, 1);
	  }
	}
      }
    } else {
      for (i = 0; i < leng; i++){
	OBJ elem = data_array(arr)[i];
	jobject jo;
	if (!excl) copy_some(elem, 1);
	jo = (*asJava)(elem);
	(*javabind_env)->SetObjectArrayElement(javabind_env, jarr, i, jo);
	javabind_catch_abort();
	(*javabind_env)->DeleteLocalRef(javabind_env, jo);
      }
    }
  }
  if (excl){
    dispose_array_flat(arr);
  } else {
    decr_array(arr,1);
  }
  return jarr;
}       
Example #24
0
BOOLEAN_T dxml_get_bg(void *data, ARRAY_T **bg) {
    DXML_T *parser;
    parser = (DXML_T*)data;
    if (parser->data->fscope.background == NULL) return FALSE;
    *bg = resize_array(*bg, get_array_length(parser->data->fscope.background));
    copy_array(parser->data->fscope.background, *bg);
    return TRUE;
}
Example #25
0
void split_and_merge(char a[][MAX_CHARS], int i_start, int i_end, char b[][MAX_CHARS]) {
    if(i_end - i_start < 2) return;
    int i_middle = (i_start + i_end) * 0.5;
    split_and_merge(a, i_start, i_middle, b);
    split_and_merge(a, i_middle, i_end, b);
    merge(a, i_start, i_middle, i_end, b);
    copy_array(a, i_start, i_end, b);
}
Example #26
0
/* interface calling into the fortran routine */
static int lbfgs(index_t *x0, at *f, at *g, double gtol, htable_t *p, at *vargs)
{
   /* argument checking and setup */

   extern void lbfgs_(int *n, int *m, double *x, double *fval, double *gval, \
                      int *diagco, double *diag, int iprint[2], double *gtol, \
                      double *xtol, double *w, int *iflag);

   ifn (IND_STTYPE(x0) == ST_DOUBLE)
      error(NIL, "not an array of doubles", x0->backptr);
   ifn (Class(f)->listeval)
      error(NIL, "not a function", f);
   ifn (Class(f)->listeval)
      error(NIL, "not a function", g);
   ifn (gtol > 0)
      error(NIL, "threshold value not positive", NEW_NUMBER(gtol));
   
   at *gx = copy_array(x0)->backptr;
   at *(*listeval_f)(at *, at *) = Class(f)->listeval;
   at *(*listeval_g)(at *, at *) = Class(g)->listeval;
   at *callf = new_cons(f, new_cons(x0->backptr, vargs));
   at *callg = new_cons(g, new_cons(gx, new_cons(x0->backptr, vargs)));

   htable_t *params = lbfgs_params();
   if (p) htable_update(params, p);
   int iprint[2];
   iprint[0] = (int)Number(htable_get(params, NEW_SYMBOL("iprint-1")));
   iprint[1] = (int)Number(htable_get(params, NEW_SYMBOL("iprint-2")));
   lb3_.gtol = Number(htable_get(params, NEW_SYMBOL("ls-gtol")));
   lb3_.stpmin = Number(htable_get(params, NEW_SYMBOL("ls-stpmin")));
   lb3_.stpmax = Number(htable_get(params, NEW_SYMBOL("ls-stpmax")));
   int m = (int)Number(htable_get(params, NEW_SYMBOL("lbfgs-m")));
   int n = index_nelems(x0);
   double *x = IND_ST(x0)->data;
   double  fval;
   double *gval = IND_ST(Mptr(gx))->data;
   int diagco = false;
   double *diag = mm_blob(n*sizeof(double));
   double *w = mm_blob((n*(m+m+1)+m+m)*sizeof(double));
   double xtol = eps(1); /* machine precision */
   int iflag = 0;

   ifn (n>0)
      error(NIL, "empty array", x0->backptr);
   ifn (m>0)
      error(NIL, "m-parameter must be positive", NEW_NUMBER(m));

   /* reverse communication loop */
   do {
      fval = Number(listeval_f(Car(callf), callf));
      listeval_g(Car(callg), callg);
      lbfgs_(&n, &m, x, &fval, gval, &diagco, diag, iprint, &gtol, &xtol, w, &iflag);
      assert(iflag<2);
   } while (iflag > 0);
   
   return iflag;
}
Example #27
0
static void matrix_prod(Array mat1, Array mat2, int trans1, int trans2, Array ans)
/*
    General matrix product between mat1 and mat2. Put answer in ans.
    trans1 and trans2 are logical flags which indicate if the matrix is
    to be transposed. Normal matrix multiplication has trans1 = trans2 = 0.
*/
{
    int i,j,k,K1,K2;
    const void *vmax;
    double m1, m2;
    Array tmp;

    /* Test whether everything is a matrix */
    assert(DIM_LENGTH(mat1) == 2 &&
	   DIM_LENGTH(mat2) == 2 && DIM_LENGTH(ans) == 2);

    /* Test whether matrices conform. K is the dimension that is
       lost by multiplication */
    if (trans1) {
	assert ( NCOL(mat1) == NROW(ans) );
	K1 = NROW(mat1);
    }
    else {
	assert ( NROW(mat1) == NROW(ans) );
	K1 = NCOL(mat1);
    }
    if (trans2) {
	assert ( NROW(mat2) == NCOL(ans) );
	K2 = NCOL(mat2);
    }
    else {
	assert ( NCOL(mat2) == NCOL(ans) );
	K2 = NROW(mat2);
    }
    assert (K1 == K2);

    tmp = init_array();

    /* In case ans is the same as mat1 or mat2, we create a temporary
       matrix to hold the answer, then copy it to ans
    */
    vmax = vmaxget();

    tmp = make_zero_matrix(NROW(ans), NCOL(ans));
    for (i = 0; i < NROW(tmp); i++) {
	for (j = 0; j < NCOL(tmp); j++) {
	    for(k = 0; k < K1; k++) {
		    m1 = (trans1) ? MATRIX(mat1)[k][i] : MATRIX(mat1)[i][k];
		    m2 = (trans2) ? MATRIX(mat2)[j][k] : MATRIX(mat2)[k][j];
		    MATRIX(tmp)[i][j] += m1 * m2;
	    }
	}
    }
    copy_array(tmp, ans);

    vmaxset(vmax);
}
Example #28
0
static void test_copy_array(){
  int array[3] = {1,2,3};
  int array_length = 3;
  int* array_copy = copy_array(array, array_length);

  assert(array_copy != array);
  assert(compare_arrays(array, array_copy, array_length));
  free(array_copy);
}
Example #29
0
void
f_disassemble_class() {
   array_t *arr;
   if( sp->type != T_CLASS )
     error( "Argument to disassemble_class() not a class.\n" );
   arr = copy_array( sp->u.arr );
   pop_stack();
   push_refed_array(arr);
}
Example #30
0
void lu(double *A, int matrix_size) 
{
    double *a=NULL, *a_verify=NULL;
    int info;
    
    a        = (double*)malloc(matrix_size*matrix_size*sizeof(double));
    a_verify = (double*)malloc(matrix_size*matrix_size*sizeof(double));
    if(a == NULL || a_verify == NULL) 
    {
       GA_Error("lu(): malloc failed", matrix_size*matrix_size*sizeof(double));
    }
    
    copy_array(A, a, matrix_size);
    copy_array(A, a_verify, matrix_size);
    
#if 0
    printf("\nDoing LU Factorization\n\n");
    lu_basic(a, matrix_size);
    printf("LU = \n");
    print_array(a, matrix_size);
    
    printf("\nDoing LAPACK's LU Factorization\n\n");
#endif
    info = lu_lapack(a_verify, matrix_size);

#if DEBUG
    printf("LU = ");
    print_array(a_verify, matrix_size);
#endif
    
    if(info!=0) 
    {
       printf("\nError: dgetrf() of lapack is NOT successful (INFO=%d)\n\n",
              info);
       printf("NOTE:\n INFO=0:  successful exit\n INFO<0:  if INFO = -i, the i-th argument had an illegal value\n INFO>0:  if INFO = i, U(i,i) is exactly zero. The factorization\n has been completed, but the factor U is exactly singular,  and\n division by zero will occur if it is used to solve a system of\n equations.\n");
       exit(0);
    }
    

    free(a_verify);
    free(a);

    
}