Ejemplo n.º 1
0
ivec bitalloc(const vec &variances, int nobits)
{
  ivec bitvec(variances.length());
  bitvec.clear();
  int  i, bits = nobits;
  vec  var = variances;

  while (bits > 0) {
    i = max_index(var);
    var(i) /= 4;
    bitvec(i)++;
    bits--;
  }
  return bitvec;
}
Ejemplo n.º 2
0
Archivo: main.cpp Proyecto: he3210/wd
int main(void)
{
	std::string str("1000000010000100101110");
	std::bitset<32> bitvec(str);
	std::cout << bitvec << std::endl;
	std::cout << bitvec[2] << std::endl;

	std::cout << "-----------------------------" << std::endl;

	//数列符合fibonacci数列规律
	std::bitset<32> bitvec2;
	int a = 0, b = 1, sum = a + b;
	while(sum <= 21)
	{
		bitvec2.set(sum);
		a = b;
		b = sum;
		sum = a + b;
	}
	std::cout << bitvec2 << std::endl;

	return 0;
}
Ejemplo n.º 3
0
Worker::Worker(char *parameters[], NoVoHT *novoht) {
	/* set thread detachstate attribute to DETACHED */
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
	/* filename definitions */
	set_dir(parameters[9], parameters[10]);
	file_worker_start.append(shared);
	file_worker_start.append("startinfo");
	file_task_fp.append(prefix);
	file_task_fp.append("pkgs");
	file_migrate_fp.append(prefix);
	file_migrate_fp.append("log_migrate");
	file_fin_fp.append(prefix);
	file_fin_fp.append("finish");
	file_log_fp.append(prefix);
	file_log_fp.append("log_worker");

	pmap = novoht;
	Env_var::set_env_var(parameters);
	svrclient.initialize(Env_var::cfgFile, Env_var::membershipFile,
			Env_var::TCP);
	//svrzht.initialize(Env_var::cfgFile, Env_var::membershipFile, Env_var::TCP);
	//svrmig.initialize(Env_var::cfgFile, Env_var::membershipFile, Env_var::TCP);

	if (set_ip(ip)) {
		printf("Could not get the IP address of this machine!\n");
		exit(1);
	}

	for (int i = 0; i < 10; i++) {
		msg_count[i] = 0;
	}

	poll_interval = start_poll;
	poll_threshold = start_thresh;
	num_nodes = svrclient.memberList.size();
	num_cores = atoi(parameters[11]);
	ws_sleep = atoi(parameters[12]);
	num_idle_cores = num_cores;
	neigh_mode = 'd';
	//worker.num_neigh = (int)(sqrt(worker.num_nodes));
	num_neigh = (int) (sqrt(num_nodes));
	neigh_index = new int[num_neigh];
	selfIndex = getSelfIndex(ip, atoi(parameters[1]), svrclient.memberList);// replace "localhost" with proper hostname, host is the IP in C++ string
	ostringstream oss;
	oss << selfIndex;

	printf("<ip:selfIndex>: <%s:%d>\n", ip.c_str(), selfIndex);

	//string f1 = file_fin_fp;
	//f1 = f1 + oss.str();
	//fin_fp.open(f1.c_str(), ios_base::app);

	if (LOGGING) {

		string f2 = file_task_fp;
		f2 = f2 + oss.str();
		task_fp.open(f2.c_str(), ios_base::app);

		string f3 = file_log_fp;
		f3 = f3 + oss.str();
		log_fp.open(f3.c_str(), ios_base::app);

		string f4 = file_migrate_fp;
		f4 = f4 + oss.str();
		migrate_fp.open(f4.c_str(), ios_base::app);
	}

	migratev = bitvec(num_nodes);

	Package loadPackage, tasksPackage;
	string loadmessage("Load Information!");
	loadPackage.set_virtualpath(loadmessage);
	loadPackage.set_operation(13);
	loadstr = loadPackage.SerializeAsString();

	stringstream selfIndexstream;
	selfIndexstream << selfIndex;
	string taskmessage(selfIndexstream.str());
	tasksPackage.set_virtualpath(taskmessage);
	tasksPackage.set_operation(14);
	taskstr = tasksPackage.SerializeAsString();

	srand((selfIndex + 1) * (selfIndex + 5));
	int rand_wait = rand() % 1000000;
	cout << "Worker ip = " << ip << " selfIndex = " << selfIndex << endl;
	//cout << "Worker ip = " << ip << " selfIndex = " << selfIndex << " going to wait for " << rand_wait << " seconds" << endl;
	usleep(rand_wait);

	file_worker_start.append(oss.str());
	string cmd("touch ");
	cmd.append(file_worker_start);
	//executeShell(cmd);
	system(cmd.c_str());

	FILE *fp = fopen(file_worker_start.c_str(), "w+");
	if (fp != NULL) {
		//fputs("fopen example", fp);
		char fbuf[100];
		memset(fbuf, 0, sizeof(fbuf));
		sprintf(fbuf, "%s:%d ", ip.c_str(), selfIndex);
		fwrite(fbuf, sizeof(char), strlen(fbuf), fp);
		fflush(fp);
		fclose(fp);
	}

	/*worker_start.open(file_worker_start.c_str(),
	 std::ofstream::out | ios_base::app);
	 if (worker_start.is_open()) {

	 worker_start << ip << ":" << selfIndex << " ";
	 worker_start.flush();
	 worker_start.close();
	 worker_start.open(file_worker_start.c_str(), ios_base::app);
	 }
	 worker_start.open(file_worker_start.c_str(),
	 std::ofstream::out | ios_base::app);
	 */

	clock_gettime(CLOCK_REALTIME, &poll_start);

	int err;
	/*pthread_t *ready_queue_thread = new pthread_t();//(pthread_t*)malloc(sizeof(pthread_t));
	 pthread_create(ready_queue_thread, &attr, check_ready_queue, NULL);*/
	try {
		pthread_t *ready_queue_thread = new pthread_t[num_cores];
		for (int i = 0; i < num_cores; i++) {
			err = pthread_create(&ready_queue_thread[i], &attr,
					check_ready_queue, (void*) this);
			if (err) {
				printf(
						"work_steal_init: pthread_create: ready_queue_thread: %s\n",
						strerror(errno));
				exit(1);
			}
		}

		pthread_t *wait_queue_thread = new pthread_t();
		err = pthread_create(wait_queue_thread, &attr, check_wait_queue,
				(void*) this);
		if (err) {
			printf("work_steal_init: pthread_create: wait_queue_thread: %s\n",
					strerror(errno));
			exit(1);
		}

		pthread_t *complete_queue_thread = new pthread_t();
		err = pthread_create(complete_queue_thread, &attr, check_complete_queue,
				(void*) this);
		if (err) {
			printf(
					"work_steal_init: pthread_create: complete_queue_thread: %s\n",
					strerror(errno));
			exit(1);
		}

		package_thread_args rq_args, wq_args;
		rq_args.source = &insertq_new;
		wq_args.source = &waitq;
		rq_args.dest = &rqueue;
		wq_args.dest = &wqueue;
		rq_args.slock = &iq_new_lock;
		wq_args.slock = &waitq_lock;
		rq_args.dlock = &lock;
		wq_args.dlock = &w_lock;
		rq_args.worker = this;
		wq_args.worker = this;
		pthread_t *waitq_thread = new pthread_t();
		err = pthread_create(waitq_thread, &attr, HB_insertQ_new,
				(void*) &wq_args);
		if (err) {
			printf("work_steal_init: pthread_create: waitq_thread: %s\n",
					strerror(errno));
			exit(1);
		}

		pthread_t *readyq_thread = new pthread_t();
		err = pthread_create(readyq_thread, &attr, HB_insertQ_new,
				(void*) &rq_args);
		if (err) {
			printf("work_steal_init: pthread_create: ready_thread: %s\n",
					strerror(errno));
			exit(1);
		}

		pthread_t *migrateq_thread = new pthread_t();
		err = pthread_create(migrateq_thread, &attr, migrateTasks,
				(void*) this);
		if (err) {
			printf("work_steal_init: pthread_create: migrateq_thread: %s\n",
					strerror(errno));
			exit(1);
		}

		pthread_t *notq_thread = new pthread_t();
		err = pthread_create(notq_thread, &attr, notQueue, (void*) this);
		if (err) {
			printf("work_steal_init: pthread_create: notq_thread: %s\n",
					strerror(errno));
			exit(1);
		}

		int min_lines = svrclient.memberList.size();
		//min_lines++;
		string filename(file_worker_start);
		//string cmd("wc -l ");
		//cmd = cmd + filename + " | awk {\'print $1\'}";

		string cmd2("ls -l ");
		cmd2.append(shared);
		cmd2.append("startinfo*");
		cmd2.append(" | wc -l");
		string result = executeShell(cmd2);
		//cout << "server: minlines = " << min_lines << " cmd = " << cmd << " result = " << result << endl;
		while (atoi(result.c_str()) < min_lines) {
			sleep(2);
			//cout << "server: " << worker.selfIndex << " minlines = " << min_lines << " cmd = " << cmd << " result = " << result << endl;
			result = executeShell(cmd2);
		}
		//cout << "server: " << selfIndex << " minlines = " << min_lines << " cmd = " << cmd2 << " result = " << result << endl;

		/*int num = min_lines - 1;
		 stringstream num_ss;
		 num_ss << num;
		 //min_lines++;
		 string cmd1("cat ");    cmd1.append(shared);    cmd1.append("startinfo"); 	cmd1.append(num_ss.str());     cmd1.append(" | wc -l");
		 string result1 = executeShell(cmd1);
		 //cout << "server: minlines = " << min_lines << " cmd = " << cmd << " result = " << result << endl;
		 while(atoi(result1.c_str()) < 1) {
		 sleep(2);
		 result1 = executeShell(cmd1);
		 }
		 cout << "worksteal started: server: " << selfIndex << " minlines = " << 1 << " cmd = " << cmd1 << " result = " << result1 << endl;*/

		pthread_t *work_steal_thread = new pthread_t(); //(pthread_t*)malloc(sizeof(pthread_t));
		err = pthread_create(work_steal_thread, &attr, worksteal, (void*) this);
		if (err) {
			printf("work_steal_init: pthread_create: work_steal_thread: %s\n",
					strerror(errno));
			exit(1);
		}

		delete ready_queue_thread;
		delete wait_queue_thread;
		delete complete_queue_thread;
		delete work_steal_thread;
		delete readyq_thread;
		delete waitq_thread;
		delete migrateq_thread;
		delete notq_thread;
	} catch (std::bad_alloc& exc) {
		cout
				<< "work_steal_init: failed to allocate memory while creating threads"
				<< endl;
		exit(1);
	}
}