Пример #1
0
static void parse_args(int argc, char *argv[])
{
    set_default_opts();

    static struct option long_opts[] = {
        {"file", required_argument, 0, 'f'},
        {"nodata", no_argument, 0, 'x'},
        {"ipv4", no_argument, 0, '4'},
        {"default_locale", no_argument, 0, 'd'},
        {"config", required_argument, 0, 'c'},
        {"nodes", required_argument, 0, 'n'},
        {"help", no_argument, 0, 'h'},
    };

    const char *opts_str = "4xdf:c:n:h";
    int opt, indexptr;

    while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
        switch (opt) {
            case 'f':
                DATA_FILE = strdup(optarg);
                BLOCK_FILE = strdup(optarg);

                if (DATA_FILE == NULL || BLOCK_FILE == NULL)
                    exit_toxic_err("failed in parse_args", FATALERR_MEMORY);

                strcat(BLOCK_FILE, "-blocklist");
                break;

            case 'x':
                arg_opts.ignore_data_file = 1;
                break;

            case '4':
                arg_opts.use_ipv4 = 1;
                break;

            case 'c':
                snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
                break;

            case 'n':
                snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
                break;

            case 'd':
                arg_opts.default_locale = 1;
                break;

            case 'h':
            default:
                print_usage();
                exit(EXIT_SUCCESS);
        }
    }
}
Пример #2
0
static void parse_args(int argc, char *argv[])
{
    set_default_opts();

    static struct option long_opts[] = {
        {"file", required_argument, 0, 'f'},
        {"nodata", no_argument, 0, 'x'},
        {"ipv4", no_argument, 0, '4'},
        {"config", required_argument, 0, 'c'},
        {"nodes", required_argument, 0, 'n'},
        {"help", no_argument, 0, 'h'},
    };

    const char *opts_str = "4xf:c:n:h";
    int opt, indexptr;

    while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
        switch (opt) {
            case 'f':
                DATA_FILE = strdup(optarg);
                break;

            case 'x':
                arg_opts.ignore_data_file = 1;
                break;

            case '4':
                arg_opts.use_ipv4 = 1;
                break;

            case 'c':
                snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
                break;

            case 'n':
                snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
                break;

            case 'h':
            default:
                print_usage();
                exit(EXIT_FAILURE);
        }
    }
}
Пример #3
0
static void parse_args(int argc, char *argv[])
{
    set_default_opts();

    static struct option long_opts[] = {
        {"file", required_argument, 0, 'f'},
        {"nodata", no_argument, 0, 'x'},
        {"ipv4", no_argument, 0, '4'},
        {"debug", no_argument, 0, 'b'},
        {"default-locale", no_argument, 0, 'd'},
        {"config", required_argument, 0, 'c'},
        {"nodes", required_argument, 0, 'n'},
        {"help", no_argument, 0, 'h'},
        {"noconnect", no_argument, 0, 'o'},
        {"dnslist", required_argument, 0, 'r'},
        {"force-tcp", no_argument, 0, 't'},
        {"proxy", required_argument, 0, 'p'},
    };

    const char *opts_str = "4bdhotxc:f:n:r:p:";
    int opt, indexptr;

    while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
        switch (opt) {
            case '4':
                arg_opts.use_ipv4 = 1;
                break;

            case 'b':
                arg_opts.debug = 1;
                break;

            case 'c':
                snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);
                break;

            case 'd':
                arg_opts.default_locale = 1;
                break;

            case 'f':
                arg_opts.use_custom_data = 1;
                DATA_FILE = strdup(optarg);
                BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1);

                if (DATA_FILE == NULL || BLOCK_FILE == NULL)
                    exit_toxic_err("failed in parse_args", FATALERR_MEMORY);

                strcpy(BLOCK_FILE, optarg);
                strcat(BLOCK_FILE, "-blocklist");
                break;

            case 'n':
                snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
                break;

            case 'o':
                arg_opts.no_connect = 1;
                break;

            case 'p':
                arg_opts.use_proxy = 1;
                snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);

                if (++optind > argc || argv[optind-1][0] == '-')
                    exit_toxic_err("Proxy error", FATALERR_PROXY);

                arg_opts.proxy_port = (uint16_t) atoi(argv[optind-1]);
                break;

            case 'r':
                snprintf(arg_opts.dns_path, sizeof(arg_opts.dns_path), "%s", optarg);
                break;

            case 't':
                arg_opts.force_tcp = 1;
                break;

            case 'x':
                arg_opts.ignore_data_file = 1;
                break;

            case 'h':
            default:
                print_usage();
                exit(EXIT_SUCCESS);
        }
    }
}
Пример #4
0
void 
_parse_options( struct switch_arguments *args, int argc, char **argv ) {
  static struct option long_options[] = {
    { "logging_level", required_argument, 0, 'l' },
    { "daemonize", no_argument, 0, 'd' },
    { "datapath_id", required_argument, 0, 'i' },
    { "max_flow_entries", required_argument, 0, 'm' },
    { "server_ip", required_argument, 0, 'c' },
    { "server_port", required_argument, 0, 'p' },
    { "switch_ports", optional_argument, 0, 'e' },
    { "administer", optional_argument, 0, 'a' },
    { "help", no_argument, 0, 'h' },
    { 0, 0, 0, 0 },
  };
  static const char *short_options = "l:di:c:p:e:h";
  set_default_opts( args, long_options );
  
  int c, index = 0;
  optind = 0;
  while ( 1 ) {
    c = getopt_long( argc, argv, short_options, args->options, &index );
    if ( c == -1 ) {
      break;
    }
    switch ( c ) {
      case 'h':
        print_usage( args, 0 );
      break;
      case 'a':
        args->administer = true;
      break;
      case 'l':
        if ( optarg ) {
          args->log_level = optarg;
          set_logging_level( args->log_level );
        }
      break;
      case 'd':
        args->run_as_daemon = true;
      break;
      case 'i':
        if ( optarg ) {
          string_to_datapath_id( optarg, &args->datapath_id );
        }
      break;
      case 'm':
        if ( optarg ) {
          args->max_flow_entries = ( uint16_t ) atoi( optarg );
        }
      break;
      case 'c':
        if ( optarg ) {
            char *save_ptr = NULL;
            uint32_t temp_addr = 0;
            char *p = strtok_r( optarg, ".", &save_ptr );
            while ( p ) {
              temp_addr = ( temp_addr << 8 ) | ( uint32_t ) atoi( p );
              p = strtok_r( NULL, ".", &save_ptr ); 
            }
            if ( temp_addr ) {
              args->server_ip = temp_addr;
            }
        }
      break;
      case 'p':
        if ( optarg ) {
          args->server_port = ( uint16_t ) atoi( optarg );
        }
      break;
      case 'e':
        if ( optarg ) {
          args->datapath_ports = optarg;
        }
      break;
      default:
      break;
    }
  }
}
Пример #5
0
static void parse_args(int argc, char *argv[])
{
    set_default_opts();

    static struct option long_opts[] = {
        {"file", required_argument, 0, 'f'},
        {"ipv4", no_argument, 0, '4'},
        {"debug", no_argument, 0, 'b'},
        {"default-locale", no_argument, 0, 'd'},
        {"config", required_argument, 0, 'c'},
        {"encrypt-data", no_argument, 0, 'e'},
        {"nodes", required_argument, 0, 'n'},
        {"help", no_argument, 0, 'h'},
        {"noconnect", no_argument, 0, 'o'},
        {"namelist", required_argument, 0, 'r'},
        {"force-tcp", no_argument, 0, 't'},
        {"tcp-server", required_argument, 0, 'T'},
        {"SOCKS5-proxy", required_argument, 0, 'p'},
        {"HTTP-proxy", required_argument, 0, 'P'},
        {"unencrypt-data", no_argument, 0, 'u'},
        {NULL, no_argument, NULL, 0},
    };

    const char *opts_str = "4bdehotuxc:f:n:r:p:P:T:";
    int opt, indexptr;
    long int port = 0;

    while ((opt = getopt_long(argc, argv, opts_str, long_opts, &indexptr)) != -1) {
        switch (opt) {
            case '4':
                arg_opts.use_ipv4 = 1;
                break;

            case 'b':
                arg_opts.debug = 1;
                queue_init_message("stderr enabled");
                break;

            case 'c':
                snprintf(arg_opts.config_path, sizeof(arg_opts.config_path), "%s", optarg);

                if (!file_exists(arg_opts.config_path))
                    queue_init_message("Config file not found");

                break;

            case 'd':
                arg_opts.default_locale = 1;
                queue_init_message("Using default POSIX locale");
                break;

            case 'e':
                arg_opts.encrypt_data = 1;
                break;

            case 'f':
                arg_opts.use_custom_data = 1;

                if (DATA_FILE)
                    free(DATA_FILE);

                if (BLOCK_FILE)
                    free(BLOCK_FILE);

                DATA_FILE = malloc(strlen(optarg) + 1);
                strcpy(DATA_FILE, optarg);

                if (DATA_FILE == NULL)
                    exit_toxic_err("failed in parse_args", FATALERR_MEMORY);

                BLOCK_FILE = malloc(strlen(optarg) + strlen("-blocklist") + 1);

                if (BLOCK_FILE == NULL)
                    exit_toxic_err("failed in parse_args", FATALERR_MEMORY);

                strcpy(BLOCK_FILE, optarg);
                strcat(BLOCK_FILE, "-blocklist");

                queue_init_message("Using '%s' data file", DATA_FILE);

                break;

            case 'n':
                snprintf(arg_opts.nodes_path, sizeof(arg_opts.nodes_path), "%s", optarg);
                break;

            case 'o':
                arg_opts.no_connect = 1;
                queue_init_message("DHT disabled");
                break;

            case 'p':
                arg_opts.proxy_type = TOX_PROXY_TYPE_SOCKS5;
                snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);

                if (++optind > argc || argv[optind - 1][0] == '-')
                    exit_toxic_err("Proxy error", FATALERR_PROXY);

                port = strtol(argv[optind - 1], NULL, 10);

                if (port <= 0 || port > MAX_PORT_RANGE)
                    exit_toxic_err("Proxy error", FATALERR_PROXY);

                arg_opts.proxy_port = port;
                break;

            case 'P':
                arg_opts.proxy_type = TOX_PROXY_TYPE_HTTP;
                snprintf(arg_opts.proxy_address, sizeof(arg_opts.proxy_address), "%s", optarg);

                if (++optind > argc || argv[optind - 1][0] == '-')
                    exit_toxic_err("Proxy error", FATALERR_PROXY);

                port = strtol(argv[optind - 1], NULL, 10);

                if (port <= 0 || port > MAX_PORT_RANGE)
                    exit_toxic_err("Proxy error", FATALERR_PROXY);

                arg_opts.proxy_port = port;
                break;

            case 'r':
                snprintf(arg_opts.nameserver_path, sizeof(arg_opts.nameserver_path), "%s", optarg);

                if (!file_exists(arg_opts.nameserver_path))
                    queue_init_message("nameserver list not found");

                break;

            case 't':
                arg_opts.force_tcp = 1;
                break;

            case 'T':
                port = strtol(optarg, NULL, 10);

                if (port <= 0 || port > MAX_PORT_RANGE)
                    port = 14191;

                arg_opts.tcp_port = port;
                break;

            case 'u':
                arg_opts.unencrypt_data = 1;
                break;

            case 'h':
            default:
                print_usage();
                exit(EXIT_SUCCESS);
        }
    }
}
Пример #6
0
int main(int argc, char **argv)
{
   term_init();
   set_default_opts();

   if (getenv("NVC_GDB") != NULL)
      register_gdb_signal_handlers();
   else
      register_trace_signal_handlers();

   atexit(fbuf_cleanup);

   static struct option long_options[] = {
      { "help",    no_argument,       0, 'h' },
      { "version", no_argument,       0, 'v' },
      { "work",    required_argument, 0, 'w' },
      { "dump",    no_argument,       0, 'd' },
      { "codegen", no_argument,       0, 'c' },
      { "make",    no_argument,       0, 'm' },
      { "std",     required_argument, 0, 's' },
      { 0, 0, 0, 0 }
   };

   int c, index = 0;
   const char *spec = "aehrvL:";
   while ((c = getopt_long(argc, argv, spec, long_options, &index)) != -1) {
      switch (c) {
      case 0:
         // Set a flag
         break;
      case 'h':
         usage();
         exit(EXIT_SUCCESS);
      case 'v':
         printf("%s\n%s\n", version_string, copy_string);
         exit(EXIT_SUCCESS);
      case 'w':
         opt_set_str("work-name", optarg);
         break;
      case 'L':
         lib_add_search_path(optarg);
         break;
      case 's':
         set_standard(parse_standard(optarg));
         break;
      case 'a':
      case 'e':
      case 'd':
      case 'r':
      case 'c':
      case 'm':
         // Subcommand options are parsed later
         argc -= (optind - 1);
         argv += (optind - 1);
         goto getopt_out;
      case '?':
         // getopt_long already printed an error message
         exit(EXIT_FAILURE);
      default:
         abort();
      }
   }
 getopt_out:

   switch (c) {
   case 'a':
      return analyse(argc, argv);
   case 'e':
      return elaborate(argc, argv);
   case 'r':
      return run(argc, argv);
   case 'd':
      return dump_cmd(argc, argv);
   case 'c':
      return codegen(argc, argv);
   case 'm':
      return make_cmd(argc, argv);
   default:
      fprintf(stderr, "%s: missing command\n", PACKAGE);
      return EXIT_FAILURE;
   }
}
void nmfDriver(const char* a, const int k, int iter, const char* w0, const char* h0, alg_t alg, options_t * opts) {


#ifdef PROFILE_NMF_DRIVER
	struct timeval start, end;
	gettimeofday(&start, 0);
#endif
#if DEBUG_LEVEL >= 2
	printf("Entering nmfDriver\n");
#endif

#ifdef ERROR_CHECKING
  errno = 0;		//no error occured so far
#endif

  options_t default_opts;

  //Explicit options or Defaultoptions (if opts = NULL)
  if (!opts) {
    set_default_opts(&default_opts);
    opts = &default_opts;
  }

  // checking arguments
  if (checkArguments(a, k, iter, w0, h0, opts)) {
    perror("Arguments invalid in nmfDriver");
    return;
  }
  

  // declaring basic variables to load matrices
  int m, n, m_w0, n_w0, m_h0, n_h0;
  double * A, *W0, *H0;
  W0 = NULL;
  H0 = NULL;

  loadMatrix(a, &m, &n, &A);
  if (errno) {
    perror("Loading matrix A failed in nmfDriver");
    free(A);
    return;
  }
  if (k > m || k > n) {
    errno = EDOM;
    perror("Approximation factor k bigger than original matrix dimensions.");
    free(A);
    return;
  }
  if (w0 && h0) {
    loadMatrix(w0, &m_w0, &n_w0, &W0);
    loadMatrix(h0, &m_h0, &n_h0, &H0);
  }  
  else {
    generateMatrix(m, n, k, opts->init, opts->min_init, opts->max_init, &W0, &H0, A, opts);
  }  

#ifdef ERROR_CHECKING
  if(errno) {
    perror("Loading matrix W0 failed in nmfDriver");
    perror("Loading matrix H0 failed in nmfDriver");
    free(H0);
    free(A);
    free(W0);
    return;
  }
#endif


#ifdef PROFILE_MATLAB_COMPARISON
	struct timeval start_matlab, end_matlab;
	gettimeofday(&start_matlab, 0);
#endif


  if (checkMatrices(A, W0, H0, m, n, k)) {
    errno = EDOM;
    perror("Matrices not compatible in nmfDriver");
    free(A);
    free(W0);
    free(H0);
    return;
  }
  
  
  //memory for saving matrices w and h with the best norm
  double * wbest = (double*) malloc(sizeof(double)*m*k);
  double * hbest = (double*) malloc(sizeof(double)*k*n);
  //memory for normalizing final matrices wbest and hbest
  double * hlen = (double*) malloc(sizeof(double)*k);
  idx_double * wlen = (idx_double*) malloc(sizeof(idx_double)*k);	//stores the indices of column-sums as well for resorting wbest/hbest
  

#ifdef ERROR_CHECKING
  if(errno) {
    perror("Failed allocating memory in nmfDriver");
    free(A);
    free(W0);
    free(H0);
    free(wbest);
    free(hbest);
    free(hlen);
    free(wlen);
    return;
  }
#endif

  double norm = 0.0;
  norm = HUGE_VAL;
  double normbest = 0.0;
  normbest = HUGE_VAL;

  int repetitions;

  for (repetitions = 1; repetitions <= opts->rep; ++repetitions) {
    switch (alg) {
      case mu:	norm = nmf_mu(A, &W0, &H0, m, n, k, &iter, opts->TolX, opts->TolFun);
		break;
      case als:	norm = nmf_als(&A, &W0, &H0, m, n, k, &iter, opts->TolX, opts->TolFun);
		break;
      case neals: norm = nmf_neals(&A, &W0, &H0, m, n, k, &iter, opts->TolX, opts->TolFun);
		break;
      case alspg:	norm = nmf_alspg(&A, &W0, &H0, m, n, k, &iter, opts->TolX);
		break;
      case pg:	norm = nmf_pg(&A, &W0, &H0, m, n, k, &iter, opts->TolX);
		break;
    }
    //storing best matrices w and h so far, if the norm is the best so far
    if (norm < normbest) {
      normbest = norm;
      swap(&wbest, &W0);
      swap(&hbest, &H0);
    }
    //re-initialise the starting matrices w0 and h0, if there are more repetitions to be run
    if (repetitions < opts->rep) {
      generateMatrix(m, n, k, ran, opts->min_init, opts->max_init, &W0, &H0, A, opts);
    }
  } //end of loop from 1 to rep


#if DEBUG_LEVEL >= 0
  //Output final norm
  printf("Final Norm: %.16f\n", normbest);
#endif
 

  //normalizing results
  //-------------------
  double temp;
  temp = 0.;
  int i, j;

  //calculating hlen
  for (i = 0; i<k; ++i) {
    temp = 0.;
    for (j = 0; j<n; ++j) {
      temp += pow(hbest[i + j*k], 2);
    }
    temp = sqrt(temp);
    
    if (temp == 0.) {
      hlen[i] = 1.;
      fprintf(stderr, "Warning: Matrix H doesn't have full rank\n");
    }
    else
      hlen[i] = temp;
    
  }
  
  //wbest = wbest .* hlen'
  for (i=0; i<m; ++i) {
    for (j=0; j<k; ++j) {
      wbest[i + j*m] *= hlen[j];
    }
  }
  //hbest = hbest ./ hlen

  for (j=0; j<n; ++j)
    for (i=0; i<k; ++i)
      hbest[i + j*k] /= hlen[i];

  //Calculating wlen for sorting columns of w and rows of h
  for(j = 0; j<k; ++j) {
    temp = 0;
    for(i=0; i<m; ++i) {
      temp += wbest[i+j*m] * wbest[i+j*m];
    }
    wlen[j].val = temp;
    wlen[j].idx = j;
  }
  
  //sort wlen in descending order
  int elementsize = 0;
  elementsize = sizeof(idx_double);
  qsort(wlen, k, elementsize, cmpidx_double);
  
  
  //resorting columns of wbest according to order in wlen
  for(j=0; j<k; ++j) {
    for(i=0; i<m; ++i) {
      W0[i + j*m] = wbest[i + wlen[j].idx*m];
    }
  }
  
  //resorting rows of hbest according to order in wlen
  for(j=0; j<n; ++j) {
    for(i=0; i<k; ++i) {
      H0[i + j*k] = hbest[wlen[i].idx + j*k]; 
    }
  }



#ifdef PROFILE_MATLAB_COMPARISON
	gettimeofday(&end_matlab, 0);
	outputTiming("Timing of MATLAB_COMPARISON:", start_matlab, end_matlab);
#endif  
  

  //storing final results in files
  if (opts->w_out)
    storeMatrix(opts->w_out, m, k, W0);
  if (opts->h_out)
    storeMatrix(opts->h_out, k, n, H0);


  //freeing memory of dynamic variables
  free(A);
  free(W0);
  free(H0);
  free(wbest);
  free(hbest);
  free(hlen);
  free(wlen);

#if DEBUG_LEVEL >= 2
	printf("Exiting nmfDriver\n");
#endif
#ifdef PROFILE_NMF_DRIVER
	gettimeofday(&end, 0);
	outputTiming("Timing:", start, end);
#endif

}
Пример #8
0
Файл: nvc.c Проект: jkone27/nvc
int main(int argc, char **argv)
{
   term_init();
   set_default_opts();
   intern_strings();

   if (getenv("NVC_GDB") != NULL)
      register_gdb_signal_handlers();
   else
      register_trace_signal_handlers();

   if (is_debugger_running())
      atexit(tree_gc);

   atexit(fbuf_cleanup);

   static struct option long_options[] = {
      { "help",        no_argument,       0, 'h' },
      { "version",     no_argument,       0, 'v' },
      { "work",        required_argument, 0, 'w' },
      { "std",         required_argument, 0, 's' },
      { "messages",    required_argument, 0, 'M' },
      { "map",         required_argument, 0, 'p' },
      { "ignore-time", no_argument,       0, 'i' },
      { "force-init",  no_argument,       0, 'f' },
      { 0, 0, 0, 0 }
   };

   opterr = 0;

   const char *work_name = "work";
   const char *work_path = work_name;
   lib_t work = NULL;

   const int next_cmd = scan_cmd(1, argc, argv);
   int c, index = 0;
   const char *spec = "aehrvL:";
   while ((c = getopt_long(next_cmd, argv, spec, long_options, &index)) != -1) {
      switch (c) {
      case 0:
         // Set a flag
         break;
      case 'h':
         usage();
         exit(EXIT_SUCCESS);
      case 'v':
         printf("%s\n%s\n", version_string, copy_string);
         exit(EXIT_SUCCESS);
      case 'w':
         parse_work_name(optarg, &work_name, &work_path);
         break;
      case 'L':
         lib_add_search_path(optarg);
         break;
      case 's':
         set_standard(parse_standard(optarg));
         break;
      case 'M':
         set_message_style(parse_message_style(optarg));
         break;
      case 'p':
         parse_library_map(optarg);
         break;
      case 'i':
         opt_set_int("ignore-time", 1);
         break;
      case 'f':
         opt_set_int("force-init", 1);
         break;
      case '?':
         fatal("unrecognised global option %s", argv[optind - 1]);
      default:
         abort();
      }
   }

   work = lib_new(work_name, work_path);
   lib_set_work(work);

   argc -= next_cmd - 1;
   argv += next_cmd - 1;

   return process_command(argc, argv);
}