Example #1
0
int main(int argc, char *argv[]) {
  int myid;
  int numWorkers, gridSize;  /* assume gridSize is multiple of numWorkers */
  int stripSize;             /* gridSize/numWorkers             */
  int numIters;              /* number of iterations to execute */

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);  /* what is my id (rank)? */
  MPI_Comm_size(MPI_COMM_WORLD, &numWorkers);  /* how many processes? */
  numWorkers--;   /* one coordinator, the other processes are workers */

  /* get command-line arguments and do a simple error check */
  gridSize = atoi(argv[1]);
  numIters = atoi(argv[2]);
  stripSize = gridSize/numWorkers;
  if (gridSize%numWorkers != 0) {
    printf("grid size must be a multiple of number of workers\n");
  }

  /* become one of the actual processes, depending on my id */
  if (myid == 0) {
    printf("1 Coordinator and %d Workers\n", numWorkers);
    printf("  gridSize:  %d\n  stripSize:  %d\n  numIters:  %d\n",
       gridSize, stripSize, numIters);
    Coordinator(numWorkers, stripSize, gridSize);
  } else {
    Worker(myid, numWorkers, stripSize, gridSize, numIters);
  }

  MPI_Finalize();  /* clean up MPI */
}
Example #2
0
    FileRead( FILE* f )
        : m_stream( LZ4_createStreamDecode() )
        , m_file( f )
        , m_buf( m_bufData[1] )
        , m_second( m_bufData[0] )
        , m_offset( 0 )
        , m_lastBlock( 0 )
        , m_signalSwitch( false )
        , m_signalAvailable( false )
        , m_exit( false )
    {
        char hdr[4];
        if( fread( hdr, 1, sizeof( hdr ), m_file ) != sizeof( hdr ) ) throw NotTracyDump();
        if( memcmp( hdr, Lz4Header, sizeof( hdr ) ) != 0 )
        {
            fseek( m_file, 0, SEEK_SET );
            uint32_t sz;
            static_assert( sizeof( sz ) == sizeof( hdr ), "Size mismatch" );
            memcpy( &sz, hdr, sizeof( sz ) );
            if( sz > LZ4Size ) throw NotTracyDump();
        }

        ReadBlock();
        std::swap( m_buf, m_second );
        m_decThread = std::thread( [this] { Worker(); } );
    }
Example #3
0
double Solution857::mincostToHireWorkers(vector<int> &quality, vector<int> &wage, int K)
{
    vector<Worker> workers;
    for (int i = 0; i < quality.size(); ++i)
    {
        workers.push_back(Worker(wage[i], quality[i]));
    }
    std::sort(workers.begin(), workers.end(), this->compare);
    double res = std::numeric_limits<double>::max();
    int quality_sum = 0;
    std::priority_queue<int> priority_q;
    for (auto work : workers)
    {
        priority_q.push(work.quality);
        quality_sum += work.quality;

        if (priority_q.size() > K)
        {
            quality_sum -= priority_q.top();
            priority_q.pop();
        }
        if (priority_q.size() == K)
            res = std::min(res, quality_sum * work.radio);
    }
    return res;
}
int main(int argc, char *argv[])
{
  shmem_init();
  input_file = argv[1];
  mype = shmem_my_pe();
  NumProcs = shmem_n_pes();
  shmemx_am_attach(hid_BESTPATH, &handler_master_bestpath);
  shmemx_am_attach(hid_SUBSCRIBE, &handler_master_subscribe);
  shmemx_am_attach(hid_PUTPATH, &handler_master_putpath);
  shmemx_am_mutex_init(&lock_shortestlen);
  shmemx_am_mutex_init(&lock_queue);
  shmemx_am_mutex_init(&lock_workers_stack);

  if (NumProcs<2) {
    printf("At least 2 processes are required\n");
    exit(-1);
  }  

  
  // Initialize distance matrix. Ususally done by one process 
  // and bcast, or initialized from a file in a shared file system.
  Fill_Dist();  // process 0 read the data and broadcast it to the others

  if (mype==0) 
    Master();
  else
    Worker();
  
  //TODO
//  shmemx_am_detach(hid_BESTPATH);
//  shmemx_am_detach(hid_SUBSCRIBE);
//  shmemx_am_detach(hid_PUTPATH);
  shmem_finalize();
  return 0;
}
Example #5
0
File: xns.c Project: mingpen/OpenNT
BOOL _CRTAPI1 main( void) {
    BOOL        success;
    pCcb = &Ccb;
    hCompletionEvent = CreateEvent(
                NULL,       //  no security attributes
                TRUE,       //  use manual reset
                TRUE,       //  initial state is signalled
                NULL        //  no name
                );
    if ( hCompletionEvent == NULL) {
        PRINTF(( "CreateEvent() error\n", GetLastError()));
        return( FALSE);
    }
    cbBufferSize = 0xA000;
    pBuffer = GlobalAlloc( GMEM_FIXED, cbBufferSize);
    if ( pBuffer == NULL) {
        PRINTF(( "GlobalAlloc() error\n", GetLastError()));
        return( FALSE);
    }
    if ( !RplDirOpenAdapter()) {
        return( FALSE);
    }
    success = Worker();
    RplDirCloseAdapter();
    return( success);
}
Example #6
0
void FStreamedAudioPlatformData::Cache(USoundWave& InSoundWave, FName AudioFormatName, uint32 InFlags)
{
	// Flush any existing async task and ignore results.
	FinishCache();

	uint32 Flags = InFlags;

	static bool bForDDC = FString(FCommandLine::Get()).Contains(TEXT("DerivedDataCache"));
	if (bForDDC)
	{
		Flags |= EStreamedAudioCacheFlags::ForDDCBuild;
	}

	bool bForceRebuild = (Flags & EStreamedAudioCacheFlags::ForceRebuild) != 0;
	bool bAsync = !bForDDC && (Flags & EStreamedAudioCacheFlags::Async) != 0;
	GetStreamedAudioDerivedDataKey(InSoundWave, AudioFormatName, DerivedDataKey);

	if (bAsync && !bForceRebuild)
	{
		AsyncTask = new FStreamedAudioAsyncCacheDerivedDataTask(this, &InSoundWave, AudioFormatName, Flags);
		AsyncTask->StartBackgroundTask();
	}
	else
	{
		FStreamedAudioCacheDerivedDataWorker Worker(this, &InSoundWave, AudioFormatName, Flags);
		Worker.DoWork();
		Worker.Finalize();
	}
}
Example #7
0
//////////////////////////////////////////////////////////
//
//	The parallel entry of work that each thread
//	will take after being created successfully
//
///////////////////////////////////////////////////////////
void * thread_work(void * param_in){
	ParamStruct *theParam = (ParamStruct *) (param_in);
	int threadID = theParam->threadID;
		
	//printf("Hello, thread %d\n", threadID);
	
	if(threadID == 0){
    	gettimeofday(&start, NULL);
	}

	// front barrier: make sure all thread have arrived
	pthread_barrier();

	//Note: each thread, when entering Worker() method, will try to grap a share of the load on the work queue
	// So, there is no explicit data-partition carried on from the invocation site	
    Worker();

	// back barrier: make sure all threads have finished, before returning
    pthread_barrier();
    
    if(threadID == 0){
    	gettimeofday(&finish, NULL);
    }


	return NULL;
}
Example #8
0
// the constructor just launches some amount of workers
threadpool::Pool::Pool(size_t threads, Logger & l)
    :   stop(false), m_log(l)
{
  m_log(LOG_HDR + "ThreadPool creation with " + std::to_string(threads) + " threads.");
  for(size_t i = 0; i<threads; ++i)
    {
      workers.push_back(std::thread(Worker(*this, i, m_log)));
    }
}
Example #9
0
File: Main.c Project: k2b3d/tconsrc
int main(int argc, char *argv[])
{
	pid_t 		pid ;
	int 		stat ;
	pthread_t	pthread_cancel_id, pthread_sms_id, pthread_cois_id ;	
	int			ret ;

	isDup(argv[0]) ;

	if((pid = fork()) < 0)
		return  -1;
	else    if(pid != 0)
		exit(0);

	setsid() ;

	set_signal() ;

	if (argc > 1) {
		if (argc == 3) {
			if (strcmp(argv[1], "-f") == 0) {
				strcpy(config_file_path, argv[2]);
			}
			else    {
				printf("---------------------------------\n");
				printf("-%s [-f ConfigFilePath]\n", argv[0]);
				printf("---------------------------------\n");
				}
			}
		else    {
			printf("---------------------------------\n");
			printf("-%s [-f ConfigFilePath]\n", argv[0]);
			printf("---------------------------------\n");
		}
	}
	else
		sprintf(config_file_path, "../config/TCenter.Config");

	memset(&Config, 0, sizeof(CONFIG_T)) ;
	readConfig(config_file_path, &Config) ;
	Log = openLog(argv[0], Config.LOG_FILE, LOG_MODE) ;
#ifdef _FOR_STAT_
	StatLog = openLog(argv[0], Config.STAT_LOG_PATH, LOG_MODE) ;
#endif
	printLog(HEAD, "=====[Program Start]=====\n") ;

	printConfig(&Config) ;

	pthread_mutex_init(&p_lock, NULL);
	thread_count = 0;

	Worker() ;

	exit_handler() ;
	return M_TRUE ;
}
Example #10
0
int CHostScan::local_scan_start()
{
	WORD wVersionRequested;
	WSADATA wsaData;
	char name[255];
	PHOSTENT hostinfo;
	wVersionRequested = MAKEWORD(1, 1);
	char* ip;

	if (WSAStartup(wVersionRequested, &wsaData) == 0)
		if (gethostname(name, sizeof(name)) == 0)
		{
#ifndef _REAL
			DebugMessageA("Host name: %s\n", name);
#endif
			if ((hostinfo = gethostbyname(name)) != NULL)
			{
				for (int nCount = 0; hostinfo->h_addr_list[nCount]; nCount++)
				{
					ip = inet_ntoa(*(struct in_addr *)hostinfo->h_addr_list[nCount]);
#ifndef _REAL
//					DebugMessageA("IP #%d: %s\n", nCount, ip);
#endif
					char* ip_temp;
					char ip_target[16] = { 0, };
					ip_temp = strtok(ip, ".");
					for (int i = 0; i < 3; i++)
					{
						strcat(ip_target, ip_temp);
						strcat(ip_target, ".");

						ip_temp = strtok(NULL, ".");
					}

					for (int i = m_start; i <=m_end; i++)
					{
						char ip_target_temp[16] = { 0, };
						char host_temp[4] = { 0, };
						sprintf(host_temp, "%d", i);
						strcat(ip_target_temp, ip_target);
						strcat(ip_target_temp, host_temp);

						if (port_scan(ip_target_temp, 445) == 1)
						{
#ifndef _REAL
							Worker(ip_target_temp, "445");
							DebugMessageA("port_scan %s\n", ip_target_temp);
#endif
						}
					}

				}
			}
		}
	return 0;
}
Example #11
0
int main(int argc, char* argv[])  
{  
    std::cout << "main: startup" << std::endl;  
    Worker w = Worker();
    boost::thread workerThread(w);  
    std::cout << "main: waiting for thread" << std::endl;  
    workerThread.join();  
    std::cout << "main: done" << std::endl;  
    return 0;  
}
Example #12
0
//-h <ip> -p <port> -d <directory>
int main(int argc, char** argv) {
	std::string host = "127.0.0.1";
	uint16_t port = 53000;
	std::string path = ".";
	//
	int c = 0;
	while ((c = getopt (argc, argv, "h:p:d:")) != -1){
		switch(c){
			case 'h':
				host = optarg;
			break;
			case 'p':
				port = std::stoi(optarg);
			break;
			case 'd':
				path = optarg;
			break;
			default: break;
		}
	}

	pid_t pid = fork();
	if (pid < 0) {
		LoggerST::Instance()->Err("fork() ");
		exit(EXIT_FAILURE);
	}
	if (pid > 0) {exit(EXIT_FAILURE);}
	umask(0);
	pid_t sid = setsid();
	if (sid < 0) {
		LoggerST::Instance()->Err("setsid() ");
		exit(EXIT_FAILURE);
	}
    if (-1 == chdir("/home/box")) {
    	LoggerST::Instance()->Err("chdir ");
    	exit(EXIT_FAILURE);
    }
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);

	LoggerST::Instance()->Msg("HTTP Server start");
	try{
		std::function<void(int)> W = Worker(path);
		Server server(W);
		server.start(host, port);
	}
	catch(ErrException& e){
		// std::cout<<e.what()<<std::endl;
		 LoggerST::Instance()->ErrM("%s", e.what());
		 LoggerST::Instance()->Msg("Exit by error");
	}
	return 0;
}
Example #13
0
WorkerPool::WorkerPool()
{
	myIsRunning = true;

	myWorkers.Init(std::thread::hardware_concurrency());

	for (unsigned int i = 0; i < std::thread::hardware_concurrency(); i++)
	{
		myWorkers.Add(Worker());
		myWorkers[i].Init();
	}

	myThread = new std::thread([&] { Run(); });
}
Example #14
0
void Job::start(int numThreads) {
	if(taskQueue.size() > 0) {
		Logger::Section section(name + " (" + std::to_string(numThreads) + " threads)"); 
		numActive = numThreads;
		std::vector<std::thread> workers;
		while(numThreads-- > 0) {
			workers.push_back(std::thread(Worker(*this)));
		}
		for(auto& worker : workers) {
			worker.join();
		}
		numActive = 0;
		finished = false;
	}
}
Example #15
0
	void RequestManager::Initialise(ByteString Proxy)
	{
		curl_global_init(CURL_GLOBAL_DEFAULT);
		multi = curl_multi_init();
		if (multi)
		{
			curl_multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, curl_max_host_connections);
		}

		proxy = Proxy;

		user_agent = ByteString::Build("PowderToy/", SAVE_VERSION, ".", MINOR_VERSION, " (", IDENT_PLATFORM, "; ", IDENT_BUILD, "; M", MOD_ID, ") TPTPP/", SAVE_VERSION, ".", MINOR_VERSION, ".", BUILD_NUM, IDENT_RELTYPE, ".", SNAPSHOT_ID);

		worker_thread = std::thread([this]() { Worker(); });
	}
Example #16
0
ThreadPool::ThreadPool(size_t size)
	: stop(false)
{
	threads.resize(size);
	std::vector<SDL_GLContext> contexts(size + 1);
	for(size_t i = 0; i < contexts.size(); i++)
	{
		contexts[i] = SDL_GL_CreateContext(window);
		assert(contexts[i] != nullptr);
	}
	for(size_t i = 0; i < threads.size(); i++)
	{
		threads[i] = std::thread(Worker(i, *this, contexts[i]));
	}
}
Example #17
0
  int create_worker_pool (void)
  {
    ACE_GUARD_RETURN (ACE_Thread_Mutex,
                      worker_mon,
                      this->workers_lock_,
                      -1);
    for (int i = 0; i < POOL_SIZE; i++)
      {
        Worker *worker = 0;
        ACE_NEW_RETURN (worker, Worker (this), -1);
        this->workers_.enqueue_tail (worker);
        worker->activate ();
      }

    return 0;
  }
Example #18
0
TaskDispatch::TaskDispatch(size_t workers)
  : m_exit(false)
  , m_jobs(0)
{
  assert(!s_instance);
  s_instance = this;

  assert(workers >= 1);
  workers--;

  m_workers.reserve(workers);
  for (int i = 0; i < workers; i++)
  {
    m_workers.emplace_back([this]{ Worker(); });
  }
}
Example #19
0
Coordinator::Coordinator(int worker_num, int down_, int up_) : down(down_), up(up_)
{
  txid = DEFAULT_TX_ID;
  holder = DEFAULT_TX_ID;
  size = (up - down) / (worker_num - 2);
  startNum = 0;
  // worker[0]: (~,down)
  // worker[1]: [down, down + size)
  // worker[2]: [down + size, down + 2 * size)
  // worker[worker_num - 2]: [up,~)
  // conditions.resize(worker_num);
  for (int i = 0; i < worker_num; ++i) {
    workers.push_back(Worker());
    operations.push_back(queue<string>());
    results.push_back(queue<string>());
  }
  Debug("Coordinator constructor.\n");
}
Example #20
0
void Workers::login(const LoginEvent *event)
{
    const std::string name(event->request.login());

    if (m_map.count(name) == 0) {
        const size_t id = m_workers.size();
        m_workers.push_back(Worker(id, name, event->miner()->ip()));

        m_map[name] = id;
        m_miners[event->miner()->id()] = id;

        return;
    }

    Worker &worker = m_workers[m_map.at(name)];

    worker.add(event->miner()->ip());
    m_miners[event->miner()->id()] = worker.id();
}
Example #21
0
// Programa principal
int main(int argc, char *argv[])
{
    MPI_Init (&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &processRank);
    MPI_Comm_size(MPI_COMM_WORLD, &NumberOfProcesses);

    if (NumberOfProcesses < 4) {
        printf("Al menos 4 procesos son necesarios\n");
        exit(-1);
    }

    FillDistance(argv);

    if (processRank == 0)
        Coordinator();
    else
        Worker();

    MPI_Finalize();
    return 0;
}
Example #22
0
///////////////////////////////////////////////////////////////////////////////
/// @fn CBroker::Schedule
/// @description Given a module and a task, put that task into that modules
///		job queue. The attempt to schedule will be rejected if the Broker is
///		stopping.
/// @pre The module is registered.
/// @post The task is placed in the work queue for the module m. If the
///     start_worker parameter is set to true, the module's worker will be
///     activated if it isn't already.
/// @param m The module the schedulable should be run as.
/// @param x The method that will be run. A functor that expects no parameters
///		and returns void. Created via boost::bind()
/// @param start_worker tells the worker to begin processing again, if it is
///     currently idle. The worker may be idle if the work queue is currently
///		empty
/// @return 0 on success, -1 if rejected because the Broker is stopping.
///////////////////////////////////////////////////////////////////////////////
int CBroker::Schedule(ModuleIdent m, BoundScheduleable x, bool start_worker)
{
    Logger.Trace << __PRETTY_FUNCTION__ << std::endl;
    {
        boost::unique_lock<boost::mutex> lock(m_stoppingMutex);
        if (m_stopping)
        {
            return -1;
        }
    }

    boost::mutex::scoped_lock schlock(m_schmutex);
    m_ready[m].push_back(x);
    if(!m_busy && start_worker)
    {
        schlock.unlock();
        Worker();
        schlock.lock();
    }
    Logger.Debug<<"Module "<<m<<" now has queue size: "<<m_ready[m].size()<<std::endl;
    Logger.Debug<<"Scheduled task (NODELAY) for "<<m<<std::endl;
    return 0;
}
Example #23
0
//------------------------------------------------------------------------------
int main(int argc, char** argv) {
    //this is required because on termination the worker threads are still
    //running and a abort() will be called generating an error on termination;
    //a cleaner way is to handle termination directly in the worker threads
    //by:
    // 1) using async i/o and exit automatically if no requests are received
    //    after a specific amount of time OR
    // 2) using async i/o and checking control messages on a separate channel OR
    // 3) using async i/o and checking a condition variable set from the thread
    //    that invokes the destructor
    // 4) run indefinitely and handle SIGINT/SIGTERM
    std::set_terminate(quiet_termination);
	
    void* context = zmq_ctx_new();
    void* frontend = zmq_socket(context, ZMQ_ROUTER);
    void* backend = zmq_socket(context, ZMQ_ROUTER);
    zmq_bind(frontend, FRONTEND_URI);
    zmq_bind(backend, BACKEND_URI);
    std::string text("abcdefgh");
    
    static const int MAX_WORKERS = std::thread::hardware_concurrency();
    const int MAX_CLIENTS = argc == 1 ? 4 : atoi(argv[1]);
    std::cout << MAX_WORKERS << " workers, " << MAX_CLIENTS << " clients\n";
    std::deque< int > worker_queue;
    
    std::vector< std::thread > clients;
    std::vector< std::thread > workers;
    for(int i = 0; i != MAX_CLIENTS; ++i) {
        clients.push_back(std::thread(Client(i + 1, text)));
        std::next_permutation(text.begin(), text.end());
    }    
    for(int i = 0; i != MAX_WORKERS; ++i) {
        workers.push_back(std::thread(Worker(MAX_CLIENTS + i + 1)));
    }
    int worker_id = -1;
    int client_id = -1;
    int rc = -1;
    std::vector< char > request(0x100, 0);
    std::vector< char > reply(0x100, 0);
    int serviced_requests = 0;
    while(serviced_requests < MAX_CLIENTS) {
        zmq_pollitem_t items[] = {
            {backend, 0, ZMQ_POLLIN, 0},
            {frontend, 0, ZMQ_POLLIN, 0}};
        rc = zmq_poll(items, worker_queue.size() > 0 ? 2 : 1, -1);
        if(rc == -1) break;
        if(items[0].revents & ZMQ_POLLIN) {
            zmq_recv(backend, &worker_id, sizeof(worker_id), 0);
            worker_queue.push_back(worker_id);
            zmq_recv(backend, 0, 0, 0);
            zmq_recv(backend, &client_id, sizeof(client_id), 0);
            if(client_id != WORKER_READY) {
                zmq_recv(backend, 0, 0, 0);
                rc = zmq_recv(backend, &reply[0], reply.size(), 0);
                zmq_send(frontend, &client_id, sizeof(client_id), ZMQ_SNDMORE);
                zmq_send(frontend, 0, 0, ZMQ_SNDMORE);
                zmq_send(frontend, &reply[0], rc, 0);
                ++serviced_requests;
            } 
        }
        if(items[1].revents & ZMQ_POLLIN) {
            zmq_recv(frontend, &client_id, sizeof(client_id), 0);
            zmq_recv(frontend, 0, 0, 0);
            rc = zmq_recv(frontend, &request[0], request.size(), 0);
            worker_id = worker_queue.front();
            zmq_send(backend, &worker_id, sizeof(worker_id), ZMQ_SNDMORE);
            zmq_send(backend, 0, 0, ZMQ_SNDMORE);
            zmq_send(backend, &client_id, sizeof(client_id), ZMQ_SNDMORE);
            zmq_send(backend, 0, 0, ZMQ_SNDMORE);
            zmq_send(backend, &request[0], rc, 0);
            worker_queue.pop_front();
        }     
    }
    zmq_close(frontend);
    zmq_close(backend);
    zmq_ctx_destroy(context);
    std::terminate();
    return 0;
}
Example #24
0
// the constructor just launches some amount of workers
ThreadPool::ThreadPool(size_t threads)
    :   stop(false)
{
    for(size_t i = 0;i<threads;++i)
        workers.push_back(std::thread(Worker(*this)));
}
Example #25
0
File: Main.c Project: k2b3d/tconsrc
int main(int argc, char *argv[])
{
	pid_t 		pid ;
	int 		stat ;
	pthread_t	pthread_ping_id;	
	int			ret ;

	isDup(argv[0]) ;

	if((pid = fork()) < 0)
		return  -1;
	else    if(pid != 0)
		exit(0);

	setsid() ;

	set_signal() ;

	if (argc > 1) {
		if (argc == 3) {
			if (strcmp(argv[1], "-f") == 0) {
				strcpy(config_file_path, argv[2]);
			}
			else    {
				printf("---------------------------------\n");
				printf("-%s [-f ConfigFilePath]\n", argv[0]);
				printf("---------------------------------\n");
				}
			}
		else    {
			printf("---------------------------------\n");
			printf("-%s [-f ConfigFilePath]\n", argv[0]);
			printf("---------------------------------\n");
		}
	}
	else
		sprintf(config_file_path, "../config/CCTVSvr.Config");

	memset(&Config, 0, sizeof(CONFIG_T)) ;
	readConfig(config_file_path, &Config) ;
	Log = openLog(argv[0], Config.LOG_FILE, LOG_MODE) ;
	printLog(HEAD, "=====[Program Start]=====\n") ;

	printConfig(&Config);

#ifdef _USEDB
	if((ret = mydbc_connect(Config.DB_NAME, Config.DB_USER, Config.DB_PASSWORD, Config.DB_HOST, Config.DB_PORT, Config.DB_CONN_COUNT)) != DB_SUCCESS)   {
		printLog(HEAD, "DB Connection ERROR[%d]\n", ret);
		exit(0);
	}
	else    {
		printLog(HEAD, "DB Connection Success...\n");
	}
#endif

	pthread_mutex_init(&p_lock, NULL);
	thread_count = 0;

#ifdef _USEDB
	// DB PING-PONG
	if((stat = pthread_create(&pthread_ping_id, NULL, db_conn_check, (void *)NULL)) != 0) {
		printLog(HEAD, "ERR: (%s)(%d)\n", strerror(errno), errno);
		if(stat == EAGAIN) {
			printLog(HEAD, "ERR: not enough system resources to create a process for the new thread. (%d)(%d)\n", EAGAIN, stat);
		}
		exit_handler() ;
	}
#endif

	Worker();

	exit_handler() ;
	return M_TRUE ;
}
Example #26
0
    ThreadPool(std::size_t numberThreads) {
        mStop = false;

        for(auto i(0u); i < numberThreads; ++i)
            mWorkers.emplace_back(std::thread(Worker(*this)));
    }
Example #27
0
//----
bool update_trz_tbb_lamda( H3& dH, const utils::Matrix<float>& I1,  const utils::Matrix<float>& wI2, float sigma, float gradThresh, const  utils::Rect& roi )
{
	static const int PARAM_CNT = 4;

	class Worker
	{
	public:
		Worker() {
			memset( H, 0, PARAM_CNT*PARAM_CNT*sizeof(double) );
			memset( b, 0,           PARAM_CNT*sizeof(double) );
		};

	public: // state
		double H[PARAM_CNT][PARAM_CNT];
		double b[PARAM_CNT];
	};

	utils::Rect validRgn( roi.top+1, roi.left+1, roi.bottom-1, roi.right-1 ); // avoid problems with gradients

	Worker w =
		tbb::parallel_reduce( 
		tbb::blocked_range<int>(validRgn.top, validRgn.bottom+1), 
		Worker(),
		[=,&I1,&wI2](const tbb::blocked_range<int> &r,  Worker init)->Worker {

			utils::Rect stripe( r.begin(), validRgn.left, r.end()-1, validRgn.right );

			float Xc = 0.5f * (float)(validRgn.right + validRgn.left);
			float Yc = 0.5f * (float)(validRgn.bottom + validRgn.top);

			double Hl[PARAM_CNT][PARAM_CNT];
			double bl[PARAM_CNT];

			HESS_FUNC( Hl, bl, I1, wI2, stripe, gradThresh, Xc, Yc, sigma );

			for( int i = 0; i < PARAM_CNT; i++ )
			{
				for( int j = 0; j < PARAM_CNT; j++ )
					init.H[i][j] += Hl[i][j];
			}

			for(int i = 0; i < PARAM_CNT; i++)
				init.b[i] += bl[i];

			return init;

	},
		[](const Worker &x, const Worker &y)->Worker {
			Worker z;
			for( int i = 0; i < PARAM_CNT; i++ )
			{
				for( int j = 0; j < PARAM_CNT; j++ )
					z.H[i][j] = x.H[i][j] + y.H[i][j];
			}

			for(int i = 0; i < PARAM_CNT; i++)
				z.b[i] = x.b[i] + y.b[i];

			return z;
	},
		tbb::auto_partitioner());


#if 1
	double dx[PARAM_CNT];


	// solve and generate update
	dH.zeros();
	if( solve_ChD<double, PARAM_CNT>(dx, w.H, w.b) )
	{
		dH[0] =  dx[0];
		dH[1] =  dx[1];

		dH[3] = -dx[1];
		dH[4] =  dx[0];

		dH[2] =  dx[2];
		dH[5] =  dx[3];

		return true;
	}
#endif

	return false; // ill conditioned hessian matrix (can not solve)
}
Example #28
0
int mpidag(int argc, char *argv[]) {
    int numprocs;
    program = argv[0];
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    
    std::list<char *> flags;
    for (int i=1; i<argc; i++) {
        flags.push_back(argv[i]);
    }
    
    std::string outfile;
    std::string errfile;
    std::string logfile;
    std::list<std::string> args;
    int loglevel = LOG_INFO;
    bool skiprescue = false;
    int max_failures = 0;
    int tries = 1;
    
    while (flags.size() > 0) {
        std::string flag = flags.front();
        if (flag == "-h" || flag == "--help") {
            usage();
            return 0;
        } else if (flag == "-o" || flag == "--stdout") {
            flags.pop_front();
            if (flags.size() == 0) {
                if (rank == 0) {
                    fprintf(stderr, "-o/--stdout requires PATH\n");
                }
                return 1;
            }
            outfile = flags.front();
        } else if (flag == "-e" || flag == "--stderr") {
            flags.pop_front();
            if (flags.size() == 0) {
                if (rank == 0) {
                    fprintf(stderr, "-e/--stderr requires PATH\n");
                }
                return 1;
            }
            errfile = flags.front();
        } else if (flag == "-q" || flag == "--quiet") {
            loglevel -= 1;
        } else if (flag == "-v" || flag == "--verbose") {
            loglevel += 1;
        } else if (flag == "-L" || flag == "--logfile") {
            flags.pop_front();
            if (flags.size() == 0) {
                if (rank == 0) {
                    fprintf(stderr, "-L/--logfile requires PATH\n");
                }
                return 1;
            }
            logfile = flags.front();
        } else if (flag == "-s" || flag == "--skip-rescue") {
            skiprescue = true;
        } else if (flag == "-m" || flag == "--max-failures") {
            flags.pop_front();
            if (flags.size() == 0) {
                if (rank == 0) {
                    fprintf(stderr, "-m/--max-failures requires N\n");
                }
                return 1;
            }
            std::string N = flags.front();
            if (!sscanf(N.c_str(), "%d", &max_failures)) {
                fprintf(stderr, "N for -m/--max-failures is invalid\n");
                return 1;
            }
            if (max_failures < 0) {
                fprintf(stderr, "N for -m/--max-failures must be >= 0\n");
                return 1;
            }
        } else if (flag == "-t" || flag == "--tries") {
            flags.pop_front();
            if (flags.size() == 0) {
                if (rank == 0) {
                    fprintf(stderr, "-t/--tries requires N\n");
                }
                return 1;
            }
            std::string N = flags.front();
            if (!sscanf(N.c_str(), "%d", &tries)) {
                fprintf(stderr, "N for -t/--tries is invalid\n");
                return 1;
            }
            if (tries < 1) {
                fprintf(stderr, "N for -t/--tries must be >= 1\n");
                return 1;
            }
        } else if (flag[0] == '-') {
            if (rank == 0) {
                fprintf(stderr, "Unrecognized argument: %s\n", flag.c_str());
            }
            return 1;
        } else {
            args.push_back(flag);
        }
        flags.pop_front();
    }
    
    if (args.size() == 0) {
        usage();
        return 1;
    }
    
    if (args.size() > 1) {
        fprintf(stderr, "Invalid argument\n");
        return 1;
    }
    
    std::string dagfile = args.front();
    
    if (numprocs < 2) {
        fprintf(stderr, "At least one worker process is required\n");
        return 1;
    }
    
    // Everything is pretty deterministic up until the processes reach
    // this point. Once we get here the different processes can diverge 
    // in their behavior for many reasons (file systems issues, bad nodes,
    // etc.), so be careful how failures are handled after this point
    // and make sure MPI_Abort is called when something bad happens.
    
    char dotrank[25];
    sprintf(dotrank, ".%d", rank);
    
    FILE *log = NULL;
    log_set_level(loglevel);
    if (logfile.size() > 0) {
        logfile += dotrank;
        log = fopen(logfile.c_str(), "w");
        if (log == NULL) {
            failure("Unable to open log file: %s: %s\n", 
                logfile.c_str(), strerror(errno));
        }
        log_set_file(log);
    }
    
    try {
        if (rank == 0) {
            
            // IMPORTANT: The rank 0 process figures out the names
            // of these files so that we don't have 1000 workers all
            // slamming the file system with stat() calls to check if
            // the out/err/rescue files exist. The master will figure
            // it out here, and then broadcast it to the workers when
            // it starts up.
            
            // Determine task stdout file
            if (outfile.size() == 0) {
                outfile = dagfile;
                outfile += ".out";
            }
            next_retry_file(outfile);
            log_debug("Using stdout file: %s", outfile.c_str());
            
            
            // Determine task stderr file
            if (errfile.size() == 0) {
                errfile = dagfile;
                errfile += ".err";
            }
            next_retry_file(errfile);
            log_debug("Using stderr file: %s", errfile.c_str());
            
            
            // Determine old and new rescue files
            std::string rescuebase = dagfile;
            rescuebase += ".rescue";
            std::string oldrescue;
            std::string newrescue = rescuebase;
            int next = next_retry_file(newrescue);
            if (next == 0 || skiprescue) {
                // Either there is no old rescue file, or the
                // user doesnt want to read it.
                oldrescue = "";
            } else {
                char rbuf[5];
                snprintf(rbuf, 5, ".%03d", next-1);
                oldrescue = rescuebase;
                oldrescue += rbuf;
            }
            log_debug("Using old rescue file: %s", oldrescue.c_str());
            log_debug("Using new rescue file: %s", newrescue.c_str());
            
            DAG dag(dagfile, oldrescue);
            Engine engine(dag, newrescue, max_failures, tries);
            
            return Master(engine, dag, outfile, errfile).run();
        } else {
            return Worker().run();
        }
    } catch (...) {
        // Make sure we close the log
        if (log != NULL) {
            fclose(log);
        }
        throw;
    }
    
    if (log != NULL) {
        fclose(log);
    }

    return 0;
}
Example #29
0
//
// Driver entry point
//
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING )
{
	DriverObject->DriverUnload = DriverUnload;
	KdPrint(("[~] DriverEntry()\n"));

	PsGetVersion (&MajorVersion, &MinorVersion, 0, 0);

	if (MajorVersion >= 6)
	{
		KdPrint(("Windows Vista and later are not supported yet\n"));
		return STATUS_NOT_SUPPORTED;
	}
	if (MajorVersion < 5 || MinorVersion == 0)
	{
		KdPrint(("Windows NT and 2000 are not supported\n"));
		return STATUS_NOT_SUPPORTED;
	}

	ASSERT (MajorVersion == 5);
	ASSERT (MinorVersion >= 1 && MinorVersion <= 2);

	if (MinorVersion == 1)
	{
		KdPrint(("Running on Windows XP\n"));
	}
	else
	{
		KdPrint(("Running on Windows 2003 Server\n"));
	}

	if (KeNumberProcessors > 1)
	{
		KdPrint(("Loading on multiprocessor system (NumberProcessors %d)\n", KeNumberProcessors));
	}
	else
	{
		KdPrint(("Loading on uniprocessor system\n"));
	}

	KdPrint (("First hello from nt\n"));

	if(!NT_SUCCESS(W32FindAndSwapIAT ()))
	{
		KdPrint(("could not swap import\n"));
		return STATUS_INVALID_FILE_FOR_SECTION;
	}

	// import something from W32k
	EngPrint ("Second hello from win32k\n");

	HANDLE hCsrProcess;
	NTSTATUS Status;

	Status = ObOpenObjectByPointer (
		CsrProcess,
		OBJ_KERNEL_HANDLE,
		NULL, 
		PROCESS_ALL_ACCESS,
		*PsProcessType, 
		KernelMode, 
		&hCsrProcess
		); 

	if (!NT_SUCCESS(Status))
	{
		KdPrint(("ObOpenObjectByPointer failed with status %X\n", Status));
		W32ReleaseCall();
		return Status;
	}

	KdPrint(("csr opened, handle %X\n", hCsrProcess));

	//
	// EngLoadImage uses KeAttachProcess/KeDetachProcess to attach to csrss process
	// KeDetachProcess detaches to thread's original process, but our thread's
	// original process is System! (because we are running in the context of system
	// worker thread that loads a driver). 
	//    (
	//    |  fucken windows programmers could not call KeStackAttachProcess 
	//    |   instead of KeAttachProcess :(
	//    )
	// So we have to run our function in the context of csrss.exe
	//

	HANDLE ThreadHandle;
	CLIENT_ID ClientId;
	OBJECT_ATTRIBUTES Oa;
	InitializeObjectAttributes (&Oa, NULL, OBJ_KERNEL_HANDLE, 0, 0);

	Status = PsCreateSystemThread (
		&ThreadHandle,
		THREAD_ALL_ACCESS,
		&Oa,
		hCsrProcess,
		&ClientId,
		REINITIALIZE_ADAPTER,
		NULL
		);

	if (!NT_SUCCESS(Status))
	{
		KdPrint(("PsCreateSystemThread failed with status %X\n", Status));
		ZwClose (hCsrProcess);
		W32ReleaseCall();
		return Status;
	}

	KdPrint(("thread created, handle %X\n", ThreadHandle));

	PETHREAD Thread;

	Status = ObReferenceObjectByHandle(
		ThreadHandle,
		THREAD_ALL_ACCESS,
		*PsThreadType,
		KernelMode,
		(PVOID*) &Thread,
		NULL
		);

	if (!NT_SUCCESS(Status))
	{
		KdPrint(("ObReferenceObjectByHandle failed with status %X\n", Status));
		// cannot unload because thread is running
		KeBugCheck (0);
	}

	KdPrint(("thread referenced to %X\n", Thread));

	KeWaitForSingleObject (Thread, Executive, KernelMode, FALSE, NULL);

	KdPrint(("Thread terminated\n"));

	ZwClose (hCsrProcess);
	ObDereferenceObject (Thread);
	ZwClose (ThreadHandle);

	KdPrint(("success\n", hCsrProcess));

	if (!pDrvCopyBits)
	{
		KdPrint(("Could not find DrvCopyBits\n"));
		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}

	//
	// Query keyboard LEDs
	//

	if(!NT_SUCCESS(KbdWinQueryLeds()))
	{
		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}

	PSHARED_DISP_DATA disp = GetSharedData();
	if (!disp)
	{
		EngPrint ("ngvid: could not get shared data\n");
		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}
	if (disp->Signature != SHARED_SIGNATURE)
	{
		EngPrint ("ngvid: Damaged shared block %X signature %X should be %X\n",
			disp, disp->Signature, SHARED_SIGNATURE);
		//__asm int 3

		W32ReleaseCall();
		return STATUS_UNSUCCESSFUL;
	}

	KdPrint (("Got shared %X Sign %X Surf %X\n", disp, disp->Signature, disp->pPrimarySurf));

#if 0
	//
	// Temporarily hook DrvCopyBits
	//

	pDrvCopyBits = disp->pDrvCopyBits;

#endif

	if (!disp->pPrimarySurf)
	{
		KdPrint(("DrvCopyBits %X\n", pDrvCopyBits));

		KeInitializeEvent (&SynchEvent, SynchronizationEvent, FALSE);

		if (SpliceFunctionStart (pDrvCopyBits, NewDrvCopyBits, SplicingBuffer, sizeof(SplicingBuffer), BackupBuffer, &BackupWritten, FALSE))
		{
			KdPrint(("SpliceFunctionStart FAILED!!!\n"));
			W32ReleaseCall();
			return STATUS_UNSUCCESSFUL;
		}

		KdPrint(("Now you have to move mouse pointer across the display ...\n"));

		KeWaitForSingleObject (&SynchEvent, Executive, KernelMode, FALSE, NULL);

		UnspliceFunctionStart (pDrvCopyBits, BackupBuffer, FALSE);

		KdPrint(("Wait succeeded, so got primary surf %X\n", pPrimarySurf));
		disp->pPrimarySurf = pPrimarySurf;
	}
	else
	{
		KdPrint(("Already have primary surface\n"));
		pPrimarySurf = disp->pPrimarySurf;
	}

	// Hook kbd & mouse

#if KBD_HOOK_ISR
	OldKbd = GetIOAPICIntVector (1);
	*(PVOID*)&OldISR = IoHookInterrupt ( (UCHAR)OldKbd, InterruptService);
#else
	CreateTrampoline();
	//I8042HookKeyboard  ((PI8042_KEYBOARD_ISR) IsrHookRoutine);
#endif

	MouseInitialize (StateChangeCallbackRoutine);

	KdPrint(("Keyboard & mouse hooked\n"));

	// Initialize reset DPC
	KeInitializeDpc (&HotkeyResetStateDpc, HotkeyResetStateDeferredRoutine, NULL);

	//
	// Perform multiprocessor initialization
	//

	DbgHalInitializeMP ();

	///

	Worker();

	///

	W32ReleaseCall();

	DbgInitialize ();

	KdPrint(("[+] Driver initialization successful\n"));
	return STATUS_SUCCESS;
}
main(int argc, char **argv)
{
    int i,j;
    int c;
    extern char *optarg;
    int ctrproc;
    int start;
    int proc_size;
    char name[32];

    /*********************************************************************
      Parse the command line
      ********************************************************************/
    collect_info = 1;
    MEMSYS_OFF;                             /* in the initialization phase */
    while ((c = getopt(argc, argv, "p:s:b:BdvH")) != -1) {
        switch(c) {
        case 'p':
            NUM_PROCS = atoi(optarg);
            if (NUM_PROCS < 1) {
                printf("P must be >= 1\n");
                exit(-1);
            }
            break;

        case 's':
            size = atoi(optarg);
            break;

        case 'b':
            BubbleThresh = atoi(optarg);
            break;

        case 'B':
            bubble = 1;
            break;
        case 'd':
            show_array = 1;
            break;
        case 'v':
            verify = 1;
            break;
        default:
            printf("Bad option : %s \n",optarg);
        case 'h':
            printf( "\t\t\tQS - OPTIONS\n");
            printf( "\tp - Number of processors\n");
            printf( "\ts - Size of array\n");
            printf( "\tb - Bubble threshold\n");
            printf( "\tB - Bubble\n");
            printf( "\td - Display output \n");
            printf( "\tv - Verify results \n");
            printf( "\tH - Help\n");
            exit(0);
        }
    }

    StatClearAll();         /* clear the stats */
    /**********************************************************************/
    /* Use shmalloc to allocate memory from the shared address space, also
       use AssociateAddrNode to determine the distribution of the shared
       memory among the various processors */
    /**********************************************************************/
    gMem = (GlobalMemory *) shmalloc(sizeof(GlobalMemory));
    ctrproc = NUM_PROCS/2  + (int)(sqrt((double)NUM_PROCS)/2);
    /* choose a "middle-point" in the mesh network */

    /* associate the task stack variables to a processor easily accessible */
#if !defined(SPATIAL)
    AssociateAddrNode((void *)&(gMem->TaskStackLock),
                      (void *)(&(gMem->NumWaiting)+1),
                      ctrproc>=NUM_PROCS-1? NUM_PROCS-1 :  ctrproc+1,"lock");
    AssociateAddrNode((void *)&(gMem->TaskStackTop),
                      (void *)(&(gMem->TaskStackTop)+1),
                      ctrproc>=NUM_PROCS-1 ? NUM_PROCS-1 : ctrproc,"top");
#endif

    /************** associate the task queue among all processors *********/
    chunk = MAX_TASK_QUEUE / NUM_PROCS;
    for (i=0; i< NUM_PROCS; i++)   {
#if !defined(SPATIAL)
        AssociateAddrNode(&gMem->tasks[i*chunk],
                          &gMem->tasks[(i+1)*chunk],
                          i,"tasks");
#endif
    }
    LocalStackTop = -1;
    FREELOCK(&gMem->TaskStackLock);
    proc_size = (size + NUM_PROCS)/NUM_PROCS;

    /*************** associate the array among all processors **************/
    start = 0;
    strcpy(name,"Array chunks");
    for(i=0; i<NUM_PROCS; i++) {
        printf("going to call Associate address node\n");
#if !defined(SPATIAL)&&!defined(DO_PREF)
        AssociateAddrNode(&gMem->A[start],
                          &gMem->A[start+ proc_size],
                          i, name);
#endif
        start = start+proc_size;
    }

    printf( "QS - OPTIONS\n");
    printf( "\tp - Number of processors \t%d\n", NUM_PROCS);
    printf( "\ts - Size of array\t\t%d\n",size);
    printf( "\tb - Bubble threshold \t\t%d\n",BubbleThresh);
    printf( "\tB - Bubble \t\t\t%d\n",bubble);
    printf( "\td - Display output\t\t%d\n",show_array);
    printf( "\tv - Verify results\t\t%d\n",verify);

    /* The work which the root process has to do */
    whoami=0;
    root_main();                  /* initialization by the root process */
    endphase(phase);              /* end of initialization phase */
    TreeBarInit(&tree,NUM_PROCS); /* initialize tree barrier */

    MEMSYS_ON;

    /********************************************************************/
    /* Forking processes :  Create a process for each of the processors */
    /********************************************************************/

    for(i=0; i<NUM_PROCS-1; i++) {
        if(fork() == 0)     {
            whoami =  getpid();
            LocalStackTop = -1;
            for (i = (whoami+1) * chunk -1; i > whoami * chunk; i--) {
                LocalTaskStack[++LocalStackTop] = i;
            }
            break;
        }
    }

    /******************* Barrier after initialization *******************/
    printf("Before barrier %d\n",whoami);
    TreeBarrier(&tree,whoami);
    printf("Starting Process %d\n",whoami);
    if (whoami == 0)     {
        StatReportAll();
        StatClearAll();
    }

    newphase(++phase);
    Worker();   /**** Call the main procedure which we have to execute ****/
    endphase(phase);

    /**************** Barrier after finishing the sort ******************/
    printf("Coming out of worker %d\n",whoami);
    TreeBarrier(&tree,whoami);
    MEMSYS_OFF;
    if (whoami == 0)     {
        StatReportAll();
        StatClearAll();
    }

    /*************************** Cleanup phase ***************************/
    newphase(++phase);
    if(whoami== 0)     do_cleanup();
    endphase(phase);
    if (whoami == 0)     {
        StatReportAll();
        StatClearAll();
    }
}