void Graph<T>::read_input(std::ifstream& pFile) { if (READ_FROM_FILE) { while (!pFile.eof()) { std::string strLine; std::getline(pFile,strLine); //Now split the string using space as delimiter std::vector<std::string> vecToken; tokenize(strLine,vecToken); record_task(vecToken); int gh = 0; } } populate_graph(); }
int main() { std::vector<std::list<char>> graph(26); std::vector<bool> node_done(26, 0); char starting_node = 'a'; populate_graph('a', 'b', graph); populate_graph('b', 'c', graph); populate_graph('c', 'd', graph); populate_graph('c', 'f', graph); populate_graph('g', 'f', graph); populate_graph('d', 'f', graph); print_graph(graph); //populate_graph('a', 'b', graph); do_dfs(starting_node, node_done, graph); return 0; }
int main(int argc, char *argv[]) { unsigned long long num_nodes, ram_size; unsigned long num_forks = 1; struct sysinfo info; void *shm; int *cond; struct sigaction zig; int c, add_wait = -1, is_parent = 1; #ifdef __cpu_set_t_defined int affinity = 0; cpu_set_t my_cpu_mask; #endif /* By default we'll use 1/16th of total RAM, rounded * down to the nearest page. */ if (sysinfo(&info) != 0) { perror("sysinfo"); return 1; } ram_size = info.totalram / 16; ram_size = ram_size & ~(getpagesize() - 1); num_nodes = ram_size / sizeof(void *); /* Parse command line args */ while ((c = getopt(argc, argv, "a:p:n:d:s:t")) != -1) { switch (c) { case 'p': num_forks = atoi(optarg); break; case 'd': ram_size = info.totalram / atoi(optarg); ram_size = ram_size & ~(getpagesize() - 1); num_nodes = ram_size / sizeof(void *); break; case 'n': num_nodes = atoi(optarg); ram_size = num_nodes * sizeof(void *); break; case 's': report_interval = atoi(optarg); break; case 'a': add_wait = atoi(optarg); break; #ifdef __cpu_set_t_defined case 't': affinity = 1; break; #endif default: print_help(argv[0]); return 0; } } /* Will we exceed half the address space size? Use 1/4 of it at most. */ if (ram_size > ((unsigned long long)1 << ((sizeof(void *) * 8) - 1))) { printf("Was going to use %lluKB (%llu nodes) but that's too big.\n", ram_size / 1024, num_nodes); ram_size = ((unsigned long long)1 << (sizeof(void *) * 8)); ram_size /= 4; num_nodes = ram_size / sizeof(void *); printf("Clamping to %lluKB (%llu nodes) instead.\n", ram_size / 1024, num_nodes); } /* Talk about what we're going to do. */ printf("Going to use %lluKB (%llu nodes).\n", ram_size / 1024, num_nodes); /* Make a shared anonymous map of the RAM */ shm = mmap(NULL, ram_size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); if (shm == MAP_FAILED) { perror("mmap"); return 2; } printf("mmap region: %p (%llu nodes)\n", shm, num_nodes); /* Create an SHM condition variable. Bogus, I know... */ cond = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0); if (cond == MAP_FAILED) { perror("mmap"); return 4; } *cond = 1; /* Create a "graph" by populating it with random pointers. */ printf("Populating nodes..."); fflush(stdout); populate_graph(shm, num_nodes); printf("done.\n"); printf("Creating %lu processes with reports every %lu seconds \ and %d seconds between adding children.\n", num_forks, report_interval, add_wait); /* Fork off separate processes. The shared region is shared * across all children. If we only wanted one thread, we shouldn't * fork anything. Note that the "cond" mmap is a really crappy * condition variable kludge that works well enough for HERE ONLY. */ for (c = (add_wait >= 0 ? 0 : 1); c < num_forks; c++) { /* Child should wait for the condition and then break. */ if (!fork()) { #ifdef __cpu_set_t_defined if (affinity) { CPU_ZERO(&my_cpu_mask); CPU_SET(c, &my_cpu_mask); if (0 != sched_setaffinity(0,sizeof(cpu_set_t), &my_cpu_mask)) { perror("sched_setaffinity"); } } #endif is_parent = 0; while (*cond) { usleep(10000); } break; } } if (is_parent) { #ifdef __cpu_set_t_defined if (affinity) { CPU_ZERO(&my_cpu_mask); CPU_SET(0, &my_cpu_mask); if (0 != sched_setaffinity(0,sizeof(cpu_set_t), &my_cpu_mask)) { perror("sched_setaffinity"); } } #endif printf("All threads created. Launching!\n"); *cond = 0; } /* now start the work */ if (!is_parent) { start_thread: /* Set up the alarm handler to print speed info. */ memset(&zig, 0x00, sizeof(zig)); zig.sa_handler = alarm_func; sigaction(SIGALRM, &zig, NULL); gettimeofday(&last, NULL); alarm(report_interval); /* Walk the graph. */ walk_graph(shm); /* This function never returns */ } else { /* Start the ramp-up. The children will never die, * so we don't need to wait() for 'em. */ while (add_wait != -1) { sleep(add_wait); if (fork() == 0) { /* goto is cheesy, but works. */ goto start_thread; } else { printf("Added thread.\n"); } } goto start_thread; } return 0; }