示例#1
0
文件: facebull.c 项目: nikmikov/misc
int 
main(int argc, char** argv)
{

     int ncpu = (int)sysconf(_SC_NPROCESSORS_ONLN);
#ifdef DEBUG
     printf ("Configured for %d CPU\n",ncpu);
#endif     


     Graph *graph = graph_new (1024);
     if (argc > 0){
	  read_case_file (graph, argv [1]);
     }

     int num_threads = ncpu + 1;

     nodes = graph_get_nodes (graph);
     num_nodes = graph_get_num_nodes (graph);

     int i;
     for (i = 0;i<num_nodes;i++){
	  qsort (&nodes[i]->edges[0], nodes[i]->num_edges, sizeof (GraphEdge*), cmp_edges_by_weight);
     }


     int elem_per_thread = (num_nodes / num_threads) + 1;

     int **arr = malloc (sizeof (int*) * num_threads); 
     for (i = 0; i < num_threads; i++){
	  arr[i] = malloc (sizeof (int) * (elem_per_thread + 2));
	  arr[i][0] = i;
	  arr[i][1] = 0;
     }

     for (i = 0; i < num_nodes; i++){
	  int y = i % num_threads;
	  int x = arr[y][1] + 2;
	  arr[y][x] = i;
	  arr[y][1] = arr[y][1] + 1;
     }


     pthread_t tids[num_threads];
     for (i = 0; i < num_threads;i++){
	  pthread_create (&tids[i], NULL, &pthread_main, arr[i]);
     }
     for (i = 0; i < num_threads; i++){
	  pthread_join (tids[i], NULL);
     }

     if (best_found_path)
	  print_solution (best_found_path);
     return 0;
}
示例#2
0
文件: main.c 项目: k21/make
int main(int argc, char **argv) {
	int c;
	const char *makefile = NULL;
	long jobs = 1;
	char *endptr;

	while ((c = getopt(argc, argv, "C:f:j:")) != -1) {
		switch (c) {
			case 'C':
				if (chdir(optarg)) {
					fprintf(stderr,
							"Could not change "
							"directory to %s\n",
							optarg);
					return (ERROR_EXIT_CODE);
				}
				break;

			case 'f':
				if (makefile != NULL) {
					fprintf(stderr, "Option -f specified "
							"multiple times\n");
					return (ERROR_EXIT_CODE);
				}
				makefile = optarg;
				break;

			case 'j':
				errno = 0;
				jobs = strtol(optarg, &endptr, 10);
				if (errno != 0 || jobs < 1 || *endptr != '\0') {
					fprintf(stderr, "Invalid numeric value "
							"for option -j\n");
					return (ERROR_EXIT_CODE);
				}
				break;

			default:
				print_usage();
				return (ERROR_EXIT_CODE);
		}
	}

	if (makefile == NULL) {
		makefile = "Makefile";
	}

	{
		struct graph *graph;
		struct dict *macros;
		int fd;

		fd = open(makefile, O_RDONLY);
		if (fd < 0) {
			fprintf(stderr, "Could not open the makefile\n");
			return (ERROR_EXIT_CODE);
		}

		graph = graph_init();
		macros = dict_init();

		populate_builtin_macros(macros);
		populate_environment_variables(macros);

		if (parse_file(fd, graph, macros)) {
			close(fd);
			graph_destroy(graph);
			dict_destroy(macros);
			return (ERROR_EXIT_CODE);
		}

		close(fd);


		if (optind == argc) {
			/* No targets specified, use the first target */
			struct list *nodes;
			struct list_item *item;
			struct graph_node *node;

			nodes = graph_get_nodes(graph);
			item = list_head(nodes);
			if (item == NULL) {
				fprintf(stderr, "No target specified\n");
				graph_destroy(graph);
				dict_destroy(macros);
				return (ERROR_EXIT_CODE);
			}
			node = list_get_data(item);
			graph_node_mark_target(node);
		} else {
			/* Read the list of targets from the command line */
			int i;

			for (i = optind; i < argc; ++i) {
				struct string *node_name;
				struct graph_node *node;

				node_name = string_init(argv[i]);
				node = graph_get_node_by_name(graph, node_name);
				string_destroy(node_name);

				if (node == NULL) {
					fprintf(stderr, "Unknown target "
							"\"%s\"\n", argv[i]);
					graph_destroy(graph);
					dict_destroy(macros);
					return (ERROR_EXIT_CODE);
				}

				graph_node_mark_target(node);
			}
		}

		if (update_all_files_info(graph)) {
			dict_destroy(macros);
			graph_destroy(graph);
			return (ERROR_EXIT_CODE);
		}

		if (graph_process(graph)) {
			dict_destroy(macros);
			graph_destroy(graph);
			return (ERROR_EXIT_CODE);
		}

		{
			int ret = run_jobs(graph, macros, (size_t)jobs);

			dict_destroy(macros);
			graph_destroy(graph);

			return (ret);
		}
	}
}