Exemple #1
0
int main(int argc, char *argcv[]) {

  float /*combs, deleted_combs,*/ bf_time /*,time_difference*/;
  int error;
  int *combination, *solution;
  Satellite *sats;
  struct timeval time;
  struct timezone tz;
  suseconds_t start_time_u, final_time_u;
  time_t start_time_s, final_time_s;

  if (argc != 4) {
    fprintf(stderr, "Usage: %s tasks solutions satellites\n",
            basename(argcv[0]));
    return 1;
  } else {
    ntasks = atoi(argcv[1]);
    golden_index_max = atoi(argcv[2]);
    nsats = atoi(argcv[3]);
  }
  if (gettimeofday(&time, &tz))
    return 1;
  start_time_u = time.tv_usec;
  srand(start_time_u); // The seed of the random number

  error = generate_array(nsats, &combination);
  if (error == -1)
    return error;
  error = generate_array(nsats, &solution);
  if (error == -1)
    return error;
  error = init_satellites(&sats);
  if (error == -1)
    return error;
  if (gettimeofday(&time, &tz))
    return 1;
  start_time_u = time.tv_usec;
  start_time_s = time.tv_sec;

  //#pragma omp parallel num_threads(4)
  // print_F_matrix(sats);
  // print_t_matrix(sats);
  solve(sats, combination, solution);
  if (gettimeofday(&time, &tz))
    return 1;
  final_time_u = time.tv_usec;
  final_time_s = time.tv_sec;
  bf_time =
      (final_time_s - start_time_s) * 1000000 + final_time_u - start_time_u;
  // printf("%d %d %d %.0f\n\n", ntasks, golden_index_max, nsats, bf_time);
  // print_array("Solution", solution, nsats);
  // printf("%.2f\n", max);
  // printf("Duplicates: %d\n", duplicates);
  free_satellites(sats);
  free(solution);
  free(combination);
  return 0;
}
Exemple #2
0
int main(int argc, char **argv) {
    if (argc < 3) {
        printf("usage: %s <N> <M> [k]\n", argv[0]);
        return 0;
    }

    srand(time(NULL));
    int N = atoi(argv[1]);
    int M = atoi(argv[2]);
    int k = 2;

    if (argc > 3) {
        k = atoi(argv[3]);
        if (!k) 
            k = 2;
    }

    int * a = generate_array(N, M);
    shuffle_array(a, M);
    print_array(a, M);
    int small = kth_smallest(a, M, k);
    printf("%d smallest: %d\n", k, small);

    return 0;
}
Exemple #3
0
void
generate_vardata(Symbol* vsym, Generator* generator, Writer writer, Bytebuffer* code)
{
    Dimset* dimset = &vsym->typ.dimset;
    int rank = dimset->ndims;
    Symbol* basetype = vsym->typ.basetype;
    Datalist* filler = getfiller(vsym);
    const size_t* start;
    const size_t* count;
    Odometer* odom;

    if(vsym->data == NULL) return;

    /* give the buffer a running start to be large enough*/
    if(!bbSetalloc(code, nciterbuffersize))
      return;

    if(rank == 0) {/*scalar case*/
        NCConstant* c0 = datalistith(vsym->data,0);
        generate_basetype(basetype,c0,code,filler,generator);
        writer(generator,vsym,code,0,NULL,NULL);
    } else {/*rank > 0*/
        /* First, create an odometer using all of the dimensions */
        odom = newodometer(dimset,NULL,NULL);
	start = odometerstartvector(odom);
	count = odometercountvector(odom);
	generate_array(vsym,code,filler,generator,writer);
    }
}
Exemple #4
0
void
generate_random(void)
{
    unsigned int count = RANDOM_PREGEN;

    generate_array(&__random_data, &count, "/dev/urandom");
}
Exemple #5
0
__task void base_task( void ) {
	array_t array;
	// Set the priority of the base task
	//  - lowest priority:     1
	//  - highest priority:  254

	os_tsk_prio_self( 10 );

	while ( 1 ) {
		array = generate_array();

		time = os_time_get();

		// Sort array
		#ifdef TEST_SEMAPHORE_IMPLEMENTATION
			quicksort_sem( array );
		#else
			quicksort( array );
		#endif

		time = os_time_get() - time;

		if( is_sorted_array( &array ) )
			printf("The array is sucessfully sorted in %.1f us.\n", time);
		//else
		//	printf("The array is not sorted!\n");

		#ifdef PRINT_ARRAY
			print_array( &array );
		#endif
	}
}
Exemple #6
0
int main(int argc, char *argv[]) {
  generate_array(int_array, ARRAY_SIZE);
  printf("Before sorting:\nArray is:");
  print_array(int_array, ARRAY_SIZE);
  qsort(int_array, ARRAY_SIZE, sizeof(int), int_cmp_desc);
  printf("After sorting:\nArray is:");
  print_array(int_array, ARRAY_SIZE);
}
int main(){
    start();
    int array[ARRAY_LEN];
    generate_array(array, ARRAY_LEN);
    bubble_sort(array, ARRAY_LEN);
    stop();
    return 0;
}
int main(int argc, char** argv)
{
	int array_size;
	int *array;
	int threads_num;
	long array_sum, *local_sums;
	int i;
	pthread_t *tids;
	worker_param *params;
	
	printf("Array size: ");
	scanf("%d", &array_size);
	printf("Threads num: ");
	scanf("%d", &threads_num);
	
	array = (int *) malloc(array_size * sizeof(int));
	generate_array(array, array_size, 100);
	
	tids = (pthread_t *) malloc(threads_num * sizeof(pthread_t));
	params = (worker_param *) malloc(threads_num * sizeof(worker_param));
	local_sums = (long *) malloc(threads_num * sizeof(long));
	

	for(i = 0; i < threads_num; i++){
		/* init param of thread */
		params[i].threads_num = threads_num;
		params[i].thread_id = i;
		params[i].array_size = array_size;
		params[i].array = array;
		params[i].local_sums = local_sums;

		pthread_create(&(tids[i]), NULL, worker, &(params[i]));
	}
	
	array_sum = 0;
	for(i = 0; i < threads_num; i++){
		pthread_join(tids[i], NULL);
		array_sum += local_sums[i];
	}

	printf("Parallel sum = %ld\n", array_sum);

	/* cheking the result */
	array_sum = 0;
	for(i = 0; i < array_size; i++){
		array_sum += array[i];
	}

	printf("Serial sum = %ld\n", array_sum);
	
	free(local_sums);
	free(params);
	free(tids);
	free(array);
	
	return 0;
}
Exemple #9
0
void main()
{
	int a[MAXLENGTH] = {0};
	generate_array(a, MAXLENGTH);
	print_array(a,MAXLENGTH);
	printf("sorting...\n");
	selection_sort(a, MAXLENGTH);
	print_array(a,MAXLENGTH);
	return;
}
Exemple #10
0
int main(){
  int *a;
  int i,n;
  scanf("%d",&n);
  a=(int*)(malloc(n*sizeof(int)));
 for(i=0;i<n;i++)
	 scanf("%d",a+i);
  	generate_array(a,n);


return '0';

}
Exemple #11
0
float *generate_mesh(MPI_Comm *comm_new, int local_rank, int num_procs, 
                 char *proc_name, int *elem_per_node) {
  float *local_array;
  float *tmp_array;
  double start, end, dt;
  int i, j;
  int nProc = n / sqrt(num_procs);
  MPI_Status status;

  local_array = (float *) malloc(sizeof(float) * nProc * nProc);
  if( local_rank == 0 ) {
    tmp_array = generate_array(num_procs, proc_name, local_rank);
   
    int i, j, k, l;
    for( i = 0; i < sqrt(num_procs); i++ ) {
      for( j = 0; j < sqrt(num_procs); j++ ) {
        if( i == 0 && j == 0 ) {
          int index = 0;
          for( k = 0; k < nProc; k++ ) {
            for( l = 0; l < nProc; l++ ) {
              local_array[index++] = tmp_array[k * n + l];
            }
          }
        }
        else {
          float *buff_to_send = (float*)malloc(sizeof(float) * nProc * nProc);
          int startingRow = i * nProc;
          int startingColumn = j * nProc;
          int index = 0;
          for( k = startingRow; k < startingRow + nProc; k++ ) {
            for( l = startingColumn; l < startingColumn + nProc; l++ ) {
              buff_to_send[index++] = tmp_array[k * n + l];
            }
          }
          MPI_Send(buff_to_send, nProc * nProc, MPI_FLOAT, j * sqrt(num_procs) + i, 0, *comm_new);
          free (buff_to_send);
        }
      }
    }
    free(tmp_array);
  } else {
    MPI_Recv(local_array, nProc * nProc, MPI_FLOAT, 0, 0, *comm_new, &status);
  }

  if( !computerStats )
    printf("(%s(%d/%d)%s: It took %1.8fs to receive the sub-array\n", proc_name, local_rank, num_procs, s_local_coords, dt);

  return local_array;
}
Exemple #12
0
int main(void)
{
	char arr[100];
	int n = 100;

	memset(arr,0,n);
	_printf(arr,n);
	generate_array(arr, n);

	quick_sort(arr,0,99);

	_printf(arr,n);

	printf("hello\n");
}
Exemple #13
0
float *generate_2d(MPI_Comm *comm_new, int local_rank, int num_procs, 
                 char *proc_name, int *elem_per_node) {
  float *local_array;
  double start, end, dt;
  int i, j;

  if( local_rank == 0 ) {
    local_array = generate_array(num_procs, proc_name, local_rank);
  } else local_array = (float *) malloc(sizeof(float) * n * n);
 
  MPI_Bcast(local_array, n * n, MPI_FLOAT, 0, *comm_new);

  if( !computerStats )
    printf("(%s(%d/%d)%s: It took %1.8fs to receive the sub-array\n", proc_name, local_rank, num_procs, s_local_coords, dt);

  return local_array;
}
Exemple #14
0
/* driver function, to activate a sorting algorithm uncomment it */
int main()
{
   size_t s = 20;
//   print_array(ar, 3);
   value_type *a = generate_array(s);
//   std::cout << a[0].first << a[0].second << a[1].first << a[1].second << a[2].first << a[2].second;
   std::cout<< "prior to sort:" << std::endl;
   print_array(a, s);
   std::cout << std::endl;
   quick_sort(a, s,compare ());
 //  heap_sort(a, s, compare());
 //  merge_sort(a, 0, s -1, compare_h());
  // bubble_sort(a, s, compare());
   std::cout << "post sort (ordered by values of first components in elements):" << std::endl; 
   print_array(a, s);
   std::cout << std::endl;
   delete [] a; 
   return 0;
};
Exemple #15
0
int allocate_satellites(Satellite *sats) {
  int j, k;
  int error;
  for (k = 0; k < nsats; k++) {
    sats[k].id = k + 1;
    sats[k].golden_index = golden_index_max;
    error =
        generate_array_struct(sats[k].golden_index, &(sats[k].local_solutions));
    if (error == -1)
      return error;
    for (j = 0; j < sats[k].golden_index; j++) {
      error = generate_array(ntasks, &(sats[k].local_solutions[j].tasks));
      if (error == -1) {
        return error;
      }
    }
  }
  return 0;
}
Exemple #16
0
void
generate_vardata(Symbol* vsym, Generator* generator, Writer writer, Bytebuffer* code)
{
    Dimset* dimset = &vsym->typ.dimset;
    int rank = dimset->ndims;
    Symbol* basetype = vsym->typ.basetype;
    Datalist* filler = getfiller(vsym);

    if(vsym->data == NULL) return;

    /* give the buffer a running start to be large enough*/
    bbSetalloc(code, nciterbuffersize);

    if(rank == 0) {/*scalar case*/
	Constant* c0 = datalistith(vsym->data,0);
        generate_basetype(basetype,c0,code,filler,generator);
        writer(generator,vsym,code,0,NULL,NULL);
    } else {/*rank > 0*/
	generate_array(vsym,code,filler,generator,writer);
    }
}
Exemple #17
0
/* It computes the total amount of occurrences of all tasks. */
int total_occurrences(Satellite *sats, int *combination) {
  int i, k;
  int *sum; // number of repetitions of each task
  int error;
  int n = 0;
  int nzeros = 0; // tasks not present in combination

  error = generate_array(ntasks, &sum);
  if (error == -1)
    return error;

  for (i = 0; i < ntasks; i++) {
    sum[i] = 0;
  }
  for (k = 0; k < nsats; k++) {
    if (combination[k] == 0)
      continue;
    for (i = 0; i < ntasks; i++) {
      if (sats[k].local_solutions[combination[k] - 1].tasks[i] == 1)
        sum[i]++;
    }
  }
  for (i = 0; i < ntasks; i++) {
    if (sum[i] == 0) {
      nzeros++;
      continue;
    }
    if (sum[i] == 1)
      continue;
    n += sum[i];
  }
  tic = (ntasks - nzeros) * 10 / ntasks;
  // printf("tic: %.2f\n", tic);
  free(sum);
  if (n == 0)
    return 1;
  else
    return n;
}
Exemple #18
0
int main(int argc, char **argv) {
    //int v[ARR_SIZE+1]; //+1 because i guess VS adds something at the end of the array
    int *v = new int[ARR_SIZE+1];

    //test Bubble sort
    omp_set_num_threads(NUM_THREADS);

    generate_array(v,ARR_SIZE,1000);
    for(int i = 0; i < 10 ; ++i) {
#if PRINT
        for(int i = 0; i < ARR_SIZE; ++i) {
            printf("%d, ", v[i]);
        }
        printf("\n");
#endif



        start_time = omp_get_wtime();
        // bubbleSort(v,7, ARR_SIZE);
        quicksort(v,0,ARR_SIZE);

        end_time = omp_get_wtime();

#if PRINT
        for(int i = 0; i < ARR_SIZE; ++i) {
            if( i < ARR_SIZE-1 && v[i] > v[i+1]) {
                printf("*");
            }
            printf("%d, ", v[i]);
        }
#endif
        // quicksort_test();
        printf("\ndone\nExectution took: %g\n", end_time- start_time);
    }
    //delete v;
    return 0;
}
int main(int argc, char * argv[]) {
    // Deal with args
    if (argc < 2) {
        printf("\nUSAGE: %s ARRAY_SIZE\n\n", argv[0]);
        return 1;
    }
    int length = atoi(argv[1]);

    // Header stdout
    printf("\n -- %s --\n", argv[0]);
    printf("\nSorting an array of length %d\n", length);

    // Generate the array
    srand(time(NULL));
    int * array = generate_array(length);
    print_array(array, length);

    // Sort the array

    // All done
    terminate(array);

    return 0;
}
Exemple #20
0
void process(void (*function)(int*, const int), int *t, int n, struct to_print* toPrint, FILE *fd){

    char buf[128];
    int pipefd[2];
    int f, i;
    clock_t start, end;
    double temp;

    temp = 0.0;
    success = 1;
    if(pipe(pipefd) < 0){
        perror("pipe");
        exit(1);
    }
    f = fork();
    if (f < 0){
        perror("fork");
        exit(EXIT_FAILURE);
    }
    else if (f == 0) {
        for(i = 0; i < 20; i++) {
            generate_array(t, n);
            sig_handler(SIGALRM, sigalarm_func, NULL);
            close(pipefd[0]);

            alarm(max_time);
            start = clock();
            (*function)(t, n);
            end = clock();
            alarm(0);

            for (i = 0; i < n-1; i++) {
                if (t[i] > t[i + 1]) {
                    kill(getppid(), SIGUSR1);
                    close(pipefd[1]);
                    exit(1);
                }

            }
            sprintf(buf, "%lf \n", ((double) end - start) / CLOCKS_PER_SEC);

            write(pipefd[1], buf, 5);

            close(pipefd[1]);
        }
        exit(EXIT_SUCCESS);
    }
    else{
        close(pipefd[1]);
        sig_handler(SIGUSR1, sigusr_func, NULL);
        int nb;
        while((nb = read(pipefd[0], buf, 5)) > 0){
            temp += atof(buf);
        }
        close(pipefd[0]);
        wait(NULL);
        toPrint->is_success = success;
        toPrint->computing_time = temp;
        if(toPrint->is_success){
            nb = sprintf(buf, "%s : [SUCCESS] in %lf \n", toPrint->name,atof(buf));
            write(1, buf, (size_t) nb);
        }
        else{
            nb = sprintf(buf, "%s : [FAIL] %s \n", toPrint->name, nb > 0 ? "Not sorted" : "Timeout" );
            write(2, buf, (size_t) nb);
        }
        print_results(*toPrint, fd);
        fflush(fd);
    }


}
Exemple #21
0
int main(int argc, char **argv)
{
    int ret = 1;
    const char *input_data = "/dev/random", *output_data = NULL;
    int count = 0;

    struct kalman_filter_state k_state;
    unsigned int *array_in, *array_out;

    int opt;
    const struct option options[] = {
        {"control-input",     required_argument, NULL, 'B'},
        {"state-transition",  required_argument, NULL, 'F'},
        {"observation",       required_argument, NULL, 'H'},
        {"process-noise-cov", required_argument, NULL, 'Q'},
        {"meas-noise-cov",    required_argument, NULL, 'R'},
        {"count",             required_argument, NULL, 'c'},
        {"input-data",        required_argument, NULL, 'i'},
        {"output-data",       required_argument, NULL, 'o'},
        {0,                   0,                 0,    0}
    };

    memset(&k_state, 0x0, sizeof(k_state));
    k_state.I = 1;

    while ((opt = getopt_long(argc, argv, "B:F:H:Q:R:c:i:o:", options, NULL)) != -1) {
        switch (opt) {
            case 'B':
                k_state.B = atof(optarg);
                break;
            case 'F':
                k_state.F = atof(optarg);
                break;
            case 'H':
                k_state.H = atof(optarg);
                break;
            case 'Q':
                k_state.Q = atof(optarg);
                break;
            case 'R':
                k_state.R = atof(optarg);
                break;
            case 'c':
                count = atoi(optarg);
                break;
            case 'i':
                input_data = optarg;
                break;
            case 'o':
                output_data = optarg;
                break;
            case 0:
                break;
            default:
                return usage(argv[0]);
        }
    }

    if (generate_array(&array_in, &count, input_data) == -1)
        return 1;

    printf("Elements: %d\n", count);
    {
        int i;
        for (i=0;i<count;++i)
            array_in[i] %= 100;
    }

    printf("Kalman parameters: Q:%f R:%f I:%f F:%f H:%f B:%f\n",
            k_state.Q, k_state.R, k_state.I, k_state.F, k_state.H, k_state.B);

    array_out = malloc(sizeof(unsigned int)*count);
    if (!array_out) {
        fprintf(stderr, "Unable to allocate %d bytes", count);
        goto out;
    }
    memset(array_out, 0x0, sizeof(unsigned int)*count);

    {
        int i;
        kalman_filter_init(&k_state, 0.1, array_in[0]);
        for (i=1;i<count;++i)
            array_out[i] = kalman_filter(&k_state, array_in[i]);
    }

    if (output_data) {
        int i;
        int d = open(output_data, O_WRONLY|O_CREAT, 0666);
        if (d == -1) {
            fprintf(stderr, "Error opening %s: %s", output_data, strerror(errno));
            goto out;
        }
        ftruncate(d, 0);
        for (i=0;i<count;++i) {
            char b[1024] = {0x0, };
            int n;
            n = snprintf(b, sizeof(b), "%d %d %d\n", i, array_in[i], array_out[i]);
            if (write(d, b, n) == -1) {
                fprintf(stderr, "Error writing to %s: %s", output_data, strerror(errno));
                break;
            }
        }
        close(d);
    }

    ret = 0;
  out:
    if (array_in)
        free(array_in);
    if (array_out)
        free(array_out);

    return ret;
}
Exemple #22
0
int main(int argc, char **argv)
{
    int ret = 1;

    const char *input_data = "/dev/random", *output_data = NULL;
    int count = 0;

    struct moving_average_filter_state ma_state;
    unsigned int *array_in, *array_out;

    int opt;
    const struct option options[] = {
        {"order",             required_argument, NULL, 'P'},
        {"count",             required_argument, NULL, 'c'},
        {"input-data",        required_argument, NULL, 'i'},
        {"output-data",       required_argument, NULL, 'o'},
        {0,                   0,                 0,    0}
    };

    memset(&ma_state, 0x0, sizeof(ma_state));

    while ((opt = getopt_long(argc, argv, "P:c:i:o:", options, NULL)) != -1) {
        switch (opt) {
            case 'P':
                ma_state.P = atof(optarg);
                break;
            case 'c':
                count = atoi(optarg);
                break;
            case 'i':
                input_data = optarg;
                break;
            case 'o':
                output_data = optarg;
                break;
            case 0:
                break;
            default:
                return usage(argv[0]);
        }
    }

    if (generate_array(&array_in, &count, input_data) == -1)
        return 1;

    printf("Elements: %d\n", count);
    {
        int i;
        for (i=0;i<count;++i)
            array_in[i] %= 100;
    }

    if (moving_average_filter_init(&ma_state))
        goto out;

    printf("Moving Average parameters: P:%d\n",
            ma_state.P);
    {
        int i;

        printf("b:\n");
        for (i=0;i<ma_state.P;++i)
            printf("%f ", ma_state.b[i]);
        printf("\n");
    }

    array_out = malloc(sizeof(unsigned int)*count);
    if (!array_out) {
        fprintf(stderr, "Unable to allocate %d bytes", count);
        goto out;
    }
    memset(array_out, 0x0, sizeof(unsigned int)*count);

    {
        int i;

        for (i=0;i<count;++i)
            array_out[i] = moving_average_filter(&ma_state, array_in[i]);
    }

    if (output_data) {
        int i;
        int d = open(output_data, O_WRONLY|O_CREAT, 0666);
        if (d == -1) {
            fprintf(stderr, "Error opening %s: %s", output_data, strerror(errno));
            goto out;
        }
        ftruncate(d, 0);
        for (i=0;i<count;++i) {
            char b[1024] = {0x0, };
            int n;
            n = snprintf(b, sizeof(b), "%d %d %d\n", i, array_in[i], array_out[i]);
            if (write(d, b, n) == -1) {
                fprintf(stderr, "Error writing to %s: %s", output_data, strerror(errno));
                break;
            }
        }
        close(d);
    }

    ret = 0;
  out:
    moving_average_filter_destroy(&ma_state);
    if (array_in)
        free(array_in);
    if (array_out)
        free(array_out);

    return ret;
}
Exemple #23
0
int main() {
	int k, N;
	int* A;
	int* B;
	int i;

	printf("Testing Sift_Down Heap Sort\n");
	for(k = 2; k <= 5; k++) {
		comparison_counter = 0;
		N = (int)pow((double)10, k);

		begin = clock();
		A = generate_array(N);
		end = clock();
		time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
		printf("Time Spent Generating Array: %f\n", time_spent);

		// print the first unsorted array
		//printf("Unsorted Array:\n");
		//print_array(A, N);

		begin = clock();
		// call heap_sort on the first unsorted array
		heap_sort(A, N);
		end = clock();
		time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

		// show that the array is now sorted
		//printf("Sorted array: \n");
		//print_array(A, N);
		printf("Done with k = %d\n", k);
		printf("Comparisons for Heap Sort: %lu\n", comparison_counter);
		printf("Time Spent on Heap Sort: %f\n", time_spent);
		printf("\n");
	}

	printf("----------------------------------\n");
	printf("Testing Sift_Up Heap Sort\n");
        for(k = 2; k <= 5; k++) {
		comparison_counter = 0;
                N = (int)pow((double)10, k);

		begin = clock();
                B = generate_array(N);
		end = clock();
		time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
		printf("Time Spent Generating Array: %f\n", time_spent);

                // print the unsorted array
                //printf("Unsorted Array:\n");
                //print_array(B, N);

		begin = clock();
                // call heap_sort on the unsorted array
                bottom_up_heap_sort(B, N);
		end = clock();
		time_spent = (double)(end - begin) / CLOCKS_PER_SEC;

                // show that the array is now sorted
                //printf("Sorted array: \n");
                //print_array(B, N);
                printf("Done with k = %d\n", k);
		printf("Comparisons for Heap Sort: %lu\n", comparison_counter);
		printf("Time Spent on Heap Sort: %f\n", time_spent);
		printf("\n");
        }

	printf("----------------------------------\n");

	return 0;
}