Exemplo n.º 1
0
Parallel::Parallel(int threadCount, ParallelFactory* factory)
{
    // Cache the number of threads
    _threadCount = threadCount;

    // Remember the ID of the current thread
    _parentThread = std::this_thread::get_id();
    factory->addThreadIndex(_parentThread, 0);

    // Initialize shared data members and launch threads in a critical section
    {
        std::unique_lock<std::mutex> lock(_mutex);

        // Initialize shared data members
        _target = nullptr;
        _assigner = nullptr;
        _limit = 0;
        _active.assign(threadCount, true);
        _exception = nullptr;
        _terminate = false;
        _next = 0;

        // Create the extra parallel threads with one-based index (parent thread has index zero)
        for (int index = 1; index < threadCount; index++)
        {
            _threads.push_back(std::thread(&Parallel::run, this, index));
            factory->addThreadIndex(_threads.back().get_id(), index);
        }
    }

    // Wait until all parallel threads are ready
    waitForThreads();
}
Exemplo n.º 2
0
void stopThreads() {
	threadStop = 1;
	if(threads!= NULL) {
		waitForThreads();
	}
	threadStop = 0;
}
Exemplo n.º 3
0
int main(int argc, char* argv[]) {
	init();
	atexit(freeMem);
	if(argc <= 1) { // rendern zu OpenGL
		fileMode = 0;
		glutInit(&argc, argv);
		glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
		glutInitWindowSize(633, 641);
		glutCreateWindow("Mandelbrot");	
		initGL();
		glutKeyboardFunc(keyboard);
		glutMouseFunc(mouse);
		glutMotionFunc(mousemove);
		glutDisplayFunc(render);
		glutIdleFunc(repaint);
		glutReshapeFunc(reshape);
		glutMainLoop();
	} else { // rendern zur Datei
		fileMode = 1;
		sizeX = 1024;
		sizeY = 1024;
		maxIterations=500;
		//		loadPosition();
		startThreads();
		waitForThreads();
		writeTGA();
	}
	return EXIT_SUCCESS;	
}
Exemplo n.º 4
0
int
main(int argc, char *argv[])
{

    processArgs(argc, argv);

    cout << " NumIters " << NumIters << ","
         << " NumEntries " <<  NumEntries << endl;

    if (LockType == FallbackLock::MUTEX_LOCK) {
        GLock = new MutexFallbackLock(PmxLock);
		cout << "Allocated Pthread Mutex lock" << endl;
    } else if (LockType == FallbackLock::SPIN_LOCK) {
        GLock = new SpinFallbackLock(PspLock);
		cout << "Allocated Pthread Spin lock" << endl;
    } else if (LockType == FallbackLock::HLE_LOCK) {
        GLock = new HLELock();
		cout << "Allocated HLE Spin lock" << endl;
    } else if (LockType == FallbackLock::CUSTOM_LOCK) {
        GLock = new CustomSpinLock();
		cout << "Allocated Custom Spin lock" << endl;
    } else {
        cerr << "Error: invalid lock type specified, exiting..." << LockType;
    }

    SharedTable = new KaLib::HashTable<int>(
        NumEntries * NumThreads, GLock);
    for (int ec = NumThreads * NumEntries; ec > 0; --ec) {
        int v = rand();

       	SharedTable->insert(ec, v);
    }

    ThreadWorker_t worker = executeThreadLoopSimple;
	Timer coreTimer("Workers");

	coreTimer.Start();

    createWorkerThreads(NumThreads, worker);
    waitForThreads(NumThreads);

	coreTimer.Stop();

	Timer checker("VerifyHashTable");
	checker.Start();

    checkTable(*SharedTable, NumThreads, NumEntries);

	checker.Stop();

    delete SharedTable;

	coreTimer.PrintElapsedTime("RESULT: Filling up the hash table finished in ");

    checker.PrintElapsedTime("Verification of Hash table done in ");

    return 0;
}
Exemplo n.º 5
0
int main(void) {
	srand(time(NULL));

	createQueues();

	setupThreads();

	createThreads(cpuThread, cpuThreads, NUMBER_OF_CPU_THREADS);
	createThreads(ioThread, ioThreads, NUMBER_OF_IO_THREADS);
	createThreads(submissionThread, submissionThreads, NUMBER_OF_SUBMISSION_THREADS);

	waitForThreads(cpuThreads);
	waitForThreads(ioThreads);
	waitForThreads(submissionThreads);

	printf("All threads complete.\n");

	return EXIT_SUCCESS;
}
Exemplo n.º 6
0
/**************************************************
 * Main                                           *
 **************************************************/
int main(int argc, const char* argv[]) {
    parseArgs(argc, argv);
    
//    Print("main thread starting the test..."); // FOR DEBUG
    
    InitLocks();
    
    if (!createThreads()) { // returns true if all threads were created successfully
        ErrorExit(RES_CREATE_FAILED);
    }
    
    waitForThreads();
    waitOrExit();
    
    Print("main thread is calling return from main()"); // FOR DEBUG
    return RES_SUCCESS;
}
Exemplo n.º 7
0
bool myMasterFunc()
{
    releaseThreads();

    // do my chunk of work
    work[0].work_func( 0 );

    waitForThreads();

    bool ret = true;

    for(int n = 0; n < nWorkers; n++)
    {
        if( work[n].ret == false ) ret = false;
    }

    return( ret );
}
Exemplo n.º 8
0
void shutdownThreads()
{
#ifdef MEM_MULTICORE
    int n;

    for( n = 1; n < nWorkers; n++ )
    {
        work[n].exit = true;
        work[n].work_func = NULL;
    }

    releaseThreads();
    waitForThreads();

    for(int n = 1; n < nWorkers; n++)
    {
        WaitForSingleObject( tn[n], INFINITE );
    }
#endif
}
Exemplo n.º 9
0
void Parallel::call(ParallelTarget* target, ProcessAssigner* assigner)
{
    // Verify that we're being called from our parent thread
    if (std::this_thread::get_id() != _parentThread)
        throw FATALERROR("Parallel call not invoked from thread that constructed this object");

    // Initialize shared data members and activate threads in a critical section
    {
        std::unique_lock<std::mutex> lock(_mutex);

        // Copy the arguments so they can be used from any of the threads
        _target = target;
        _assigner = assigner;
        _limit = assigner->nvalues();

        // Initialize the number of active threads (i.e. not waiting for new work)
        if (assigner->parallel()) _active.assign(_threadCount, true);

        // Clear the exception pointer
        _exception = 0;

        // Initialize the loop variable
        _next = 0;

        // Wake all parallel threads, if multithreading is allowed
        if (assigner->parallel()) _conditionExtra.notify_all();
    }

    // Do some work ourselves as well
    doWork();

    // Wait until all parallel threads are done
    if (assigner->parallel()) waitForThreads();

    // Check for and process the exception, if any
    if (_exception)
    {
        throw *_exception;  // throw by value (the memory for the heap-allocated exception is leaked)
    }
}
Exemplo n.º 10
0
void launchThreads()
{
#ifdef MEM_MULTICORE
    int *nArg = (int*)malloc(sizeof(int) *(nWorkers + 1));
    int n;
    for( n = 1; n < nWorkers; n++ )
    {
        nArg[n] = n;
        work[n].exit = false;
        work[n].work_func = NULL;

        tn[n] = (HANDLE) _beginthreadex( NULL, 0, myThreadFunc, (void *) (&nArg[n]), 0, &tid[n] );
    }

    // XXX if this isn't here subsequent barrier roundtrips
    // are more expensive. Why?
    releaseThreads();
    waitForThreads();
    if(nArg)
        free(nArg);
#endif
}
Exemplo n.º 11
0
Parallel::Parallel(int threadCount, ParallelFactory* factory)
    : _assigner(0)
{
    // remember the current thread
    _parentThread = QThread::currentThread();
    factory->addThreadIndex(_parentThread, 0);

    // initialize the number of active threads (i.e. not waiting for new work) other than the current thread
    _terminate = false;
    _active = threadCount - 1;

    // create and start the extra parallel threads
    for (int index=1; index<threadCount; index++)
    {
        Thread* thread = new Thread(this);
        thread->start();
        _threads << thread;
        factory->addThreadIndex(thread, index);
    }

    // wait until all parallel threads are ready
    waitForThreads();
}
Exemplo n.º 12
0
 void LinearBarrierActive::wait (const size_t threadIndex, const size_t __threadCount)
 {
   waitForThreads(threadIndex,numThreads);
 }
Exemplo n.º 13
0
int main(int argc, char* argv[]){
	FILE *file;
	char *line=NULL;//size of line allocated by getline
	size_t length=0;//number of bytes to read (allocated by getline)
	ssize_t read;//number of characters read
	int j;	//counter for current threads
	char *buf;	//for path
	char *directory; //for path

	char prompt[30];
        strcpy(prompt,"Prompt");

        char *history[512]; //array of up to 512 previous commands
        int hIndex = 0; //index for next history entry

	/*Reading in files needs to be done one line at a time for both batch and interpreted
	 *so that every command on a line can be executed at once
	*/

	if(argc==2){	//batch mode
		//file is argv[1]
		if((file=fopen(argv[1],"r"))==NULL){
			perror(argv[1]);
			return 1;
		}
	}
	else if(argc==1){		//interactive mode
		//file is stdin
		printf("\nEnter commands separated by a semi-colon.\n");
		printf("To edit the prompt, type: editPrompt WhateverPromptYouWant\n");
		printf("To get a list of previously entered commands, type: displayHistory\n");
		printf("To look at your path, type: myPATH\n");  //Margarita
				printf("To exit this shell, type in 'quit'.\n\n");
		printf("%s> ",prompt);
		file=stdin;
	}
	else{	//incorrect number of arguments
		printf("\nIncorrect number of arguments.\n");
		printf("Run the program by itself, or with one file of commands.\n");
		printf("Ex. './shell' or './shell commands'\n\n");
		return 0;
	}


	//reads each line
	while((read=getline(&line,&length,file))!=-1){
		//Break up line and execute all commands using threading
		char *command;
		command=strtok(line,";");
		i = 0;


		//reads each command one at a time until null/newline
		while(command!=NULL && strcmp(command, "\n")!=0){

			//trimming whitespace around commands
			command = trimwhitespace(command);

			//checks for quit
			if(strcmp(command,"quit")==0 || strcmp(command,"quit\n")==0 || strcmp(command," quit")==0 ||
				strcmp(command,"exit")==0 || strcmp(command,"exit\n")==0 || strcmp(command," exit")==0){
				waitForThreads();
				printf("Exiting shell..\n");
				exit(0);
			}


			//customizable prompt
                        char * editPrompt =  strstr(command, (char *)"editPrompt "); //ptr to name of new prompt
                        if (editPrompt != NULL){
                                int k = 11;
                                //add or remove terminating characters in the following loop conditionals to specify syntax eg: ...!= '"' to terminate string at the end of quotations
                                while (editPrompt[k]!= '\0' && editPrompt[k] != ';' && editPrompt[k] != '\n' /*&& editPrompt[k] != ' '*/){
                                        prompt[k-11] = editPrompt[k];
                                        k++;
                                }
                                prompt[k-11] = '\0';
                                break;

                        }

                        //display command history
                        editPrompt = strstr(command, (char *)"displayHistory"); //lazy use of previous variable
                        if (editPrompt != NULL){
                                int k;
                                for(k = 0; k < hIndex; k++){
                                        printf(">%s\n",history[k]);
                                }
                                break;
                        }

						//display PATH
						editPrompt = strstr(command, (char *)"myPATH");
						if(editPrompt != NULL){
							buf = (char *)malloc(PATH_MAX+1);
							directory = getcwd(buf, PATH_MAX+1);
							if(buf != NULL){
								printf("Your path is: %s\n", directory);
								break;
							}
							else
							{ 	printf("Error finding path. Error num: %d\n", errno);
								break;
							}
						}

                        //adds command to history
                        history[hIndex] = (char *) malloc(strlen(command));
                        strcpy (history[hIndex++],command);




			//creates a thread
			pthread_create(&tid[i],NULL,function,command);

			//increment
			command=strtok(NULL,";");
			i++;
		}

		//waits for all the threads to finish
		waitForThreads();

		//prompts for more inputs if in interactive mode
		if(argc==1){
			printf("\n%s>",prompt);
		}
	}

	return 0;
}
Exemplo n.º 14
0
int
main(int argc, char *argv[])
{
    int c;
    ThreadWorker_t worker = NULL;

    while (1) {
        static struct option longOptions[] = {
            {"verbose",   no_argument,       &VerboseFlag, 1},
            {"iters",     required_argument, 0,            'i'},
            {"threads",   required_argument, 0,            't'},
            {"dumplevel", required_argument, 0,            'd'},
            {"numretries",required_argument, 0,            'r'},
            {"locktype",  required_argument, 0,            'l'},
            {"sharedmode",required_argument, 0,            's'},
            {0, 0, 0, 0}
        };
        int optionIndex = 0;
        c = getopt_long(argc, argv, "i:t:d:r:l:s:", longOptions, &optionIndex);

        if (c == -1)
            break;

        switch (c) {
            case 0:
                if (longOptions[optionIndex].flag != 0)
                    break;
                printf("option %s\n", longOptions[optionIndex].name);
                break;
            case 'i':
                NumIters = atoi(optarg);
                break;
            case 't':
                NumThreads = atoi(optarg);
                break;
            case 'd':
                DumpLevel = atoi(optarg);
                break;
            case 'r':
                MaxRetries = atoi(optarg);
                break;
            case 'l':
                LockType = atoi(optarg);
                break;
            case 's':
                SharedMode = atoi(optarg);
                break;
            default:
                abort();
        };
    }
    
    printf(" NumIters = %d \n", NumIters);


    if (LockType == FallbackLock::MUTEX_LOCK) {
        GLock = new MutexFallbackLock(PmxLock);
        cout << "Allocated Pthread Mutex lock" << endl;
    } else if (LockType == FallbackLock::SPIN_LOCK) {
        GLock = new SpinFallbackLock(PspLock);
        cout << "Allocated Pthread Spin lock" << endl;
    } else if (LockType == FallbackLock::HLE_LOCK) {
        GLock = new HLELock();
        cout << "Allocated HLE Spin lock" << endl;
    } else if (LockType == FallbackLock::CUSTOM_LOCK) {
        GLock = new CustomSpinLock();
        cout << "Allocated Custom Spin lock" << endl;
    } else {
        cerr << "Error: invalid lock type specified, exiting..." << LockType;
		exit(1);
    }

    /* SharedArray = (ULong*) malloc(((8+1) * (NumIters+1) + 1) * sizeof(ULong)); */
    int* rawPtr = (int*) malloc(64*1024*1024 * sizeof(ULong));

    SharedArray = (int*)((char*) rawPtr + (CACHE_LINE_BYTES -
		 	   ((ULong)rawPtr & 0x3f)));

    memset(SharedArray, 0, 64*1024*1024 * sizeof(int));

	if (SharedMode) {
    	worker = executeThreadLoopShared;
	} else {
    	worker = executeThreadLoopPrivate;
	}

    
	Timer coreTimer("Worker Threads");

	coreTimer.Start();
    createLoopThreads(NumThreads, worker);

    waitForThreads(NumThreads);
	coreTimer.Stop();


    printCheckSumOfArray(NumThreads, NumIters);

    coreTimer.PrintElapsedTime("Worker threads finished in ");
    cout << "Final Index = \n" << SharedIndex;

    if (VerboseFlag)
        printSharedArray(NumThreads, NumIters);

    return 0;
}
Exemplo n.º 15
0
int main (int argc, char *argv[])
{
   // Program description
   std::cerr << "map_back: context of significant kmers\n";

   // Do parsing and checking of command line params
   // If no input options, give quick usage rather than full help
   boost::program_options::variables_map vm;
   if (argc == 1)
   {
      std::cerr << "Usage: map_back -k seer_output.txt -r references.txt > mappings.txt\n\n"
         << "For full option details run map_back -h\n";
      return 0;
   }
   else if (parseCommandLine(argc, argv, vm))
   {
      return 1;
   }

   // Set number of threads
   size_t num_threads = vm["threads"].as<size_t>();
   if (num_threads < 1)
   {
      num_threads = 1;
   }

   // Read all sequences into memory as Fasta objects
   std::cerr << "Reading reference sequences into memory...\n";
   std::vector<Fasta> sequence_cache = readSequences(vm["references"].as<std::string>());

   // Loop through significant kmers
   std::cerr << "Now mapping significant kmers...\n";
   std::ifstream kmer_file(vm["kmers"].as<std::string>().c_str());
   if (!kmer_file)
   {
      throw std::runtime_error("Could not open kmer_file " + vm["kmers"].as<std::string>() + "\n");
   }
   else
   {
      // Set up list of threads, and mutex lock on std out
      std::mutex out_mtx;
      std::list<std::future<void>> threads;

      // Read the header
      std::string header;
      std::getline(kmer_file, header);
      int num_covar_fields = parseHeader(header);

      Significant_kmer sig_kmer(num_covar_fields);
      while(kmer_file)
      {
         kmer_file >> sig_kmer;

         // Check the read into sig_kmer hasn't reached end of file
         if (!kmer_file.eof())
         {
            assert(num_threads >= 1);

            std::cout << sig_kmer.sequence();

            // sig_kmer samples and sample cache are sorted in the same order, so
            // can go through linearly
            std::vector<std::string> search_names = sig_kmer.samples_found();
            std::vector<std::string>::iterator search_names_it = search_names.begin();

            for (std::vector<Fasta>::iterator all_names_it = sequence_cache.begin(); all_names_it != sequence_cache.end(); ++all_names_it)
            {
               // For each sample we know the kmer is in, print all matches to
               // the kmer
               if (all_names_it->get_name() == *search_names_it)
               {
                  // Thread each search (i.e. per sample per kmer)
                  if (num_threads > 1)
                  {
                     // Check if four searches are running. If so, wait for the
                     // next one to finish. Wait (for a max of 100ms) so this
                     // thread doesn't consume processing
                     waitForThreads(threads, num_threads - 1);

                     // Start a new thread asynchronously, at the back of the
                     // queue
                     threads.push_back(std::async(std::launch::async,
                              &Fasta::printMappings, *all_names_it, std::ref(std::cout), sig_kmer.sequence(), std::ref(out_mtx)));
                  }
                  else
                  {
                     all_names_it->printMappings(std::cout, sig_kmer.sequence(), out_mtx);
                  }

                  ++search_names_it;
                  if (search_names_it == search_names.end())
                  {
                     break;
                  }
               }
            }

            // Tab between every sample, line break after every kmer
            if (num_threads > 1)
            {
               waitForThreads(threads, 0);
            }

            std::cout << std::endl;
         }
      }

      std::cerr << "Done.\n";
   }

}