Ignore:
Timestamp:
Mar 10, 2016, 4:10:02 PM (3 years ago)
Author:
nmedfort
Message:

Some fixes for threading and kernel builder.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r4967 r4968  
    55 */
    66
    7 #include <string>
    8 #include <iostream>
    9 #include <fstream>
    107#include <cstdio>
    118
    12 
    13 #include <llvm/Support/SourceMgr.h>
    149#include <llvm/Support/CommandLine.h>
    15 #include <llvm/Support/Debug.h>
    16 #include <llvm/Support/TargetSelect.h>
    17 #include <llvm/Support/Host.h>
    18 
    19 #include <re/re_re.h>
    20 #include <re/parsefailure.h>
     10#include <re/re_alt.h>
    2111#include <re/re_parser.h>
    22 #include <re/re_any.h>
    23 #include <re/re_alt.h>
    24 
    2512#include <grep_engine.h>
     13#include <fstream>
     14#include <string>
    2615
    2716#include <boost/uuid/sha1.hpp>
     
    161150    grepEngine.grepCodeGen(module_name, re_ast);
    162151
    163 
    164152    initResult(inputFiles, inputFiles.size());
    165 
    166     if(Threads == 1){
     153    if (Threads <= 1) {
    167154        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
    168155            grepEngine.doGrep(inputFiles[i]);
    169156        }       
    170     }
    171     else if (Threads > 1) {
     157    } else if (Threads > 1) {
     158        const unsigned numOfThreads = Threads; // <- convert the command line value into an integer to allow stack allocation
     159        pthread_t threads[numOfThreads];
    172160
    173         pthread_t threads[100];
    174         int rc;
    175         long t;
    176         void *status;
    177 
    178         for(t=0; t<Threads; t++){
    179           rc = pthread_create(&threads[t], NULL, DoGrep, (void *)t);
    180           if (rc){
    181              printf("ERROR; return code from pthread_create() is %d\n", rc);
    182              exit(-1);
    183           }
     161        for(unsigned long i = 0; i < numOfThreads; ++i){
     162            const int rc = pthread_create(&threads[i], NULL, DoGrep, (void *)i);
     163            if (rc) {
     164                throw std::runtime_error("Failed to create thread: code " + std::to_string(rc));
     165            }
    184166        }
    185167
    186         for(t=0; t<Threads; t++) {
    187             rc = pthread_join(threads[t], &status);
     168        for(unsigned i = 0; i < numOfThreads; ++i) {
     169            void * status = nullptr;
     170            const int rc = pthread_join(threads[i], &status);
    188171            if (rc) {
    189                 printf("ERROR; return code from pthread_join() is %d\n", rc);
    190                 exit(-1);
     172                throw std::runtime_error("Failed to join thread: code " + std::to_string(rc));
    191173            }
    192174        }
    193175    }
    194     else {
    195         std::cerr << "Invalid number of threads :" << Threads << std::endl;
    196     }
    197 
    198176    PrintResult();   
    199177   
Note: See TracChangeset for help on using the changeset viewer.