我在哪里可以在我的C ++代码中使用OpenMP

时间:2016-04-09 21:42:52

标签: c++ multithreading parallel-processing openmp

我正在编写一个C ++代码来计算代码覆盖率,我想通过使函数并行工作来最小化整个运行时间来使用OpenMP来帮助增强我的代码,这样我就可以减少运行时间。 有人可以告诉我如何以及在何处使用OpenMP?

int _tmain(int argc, _TCHAR* argv[])
{



    std::clock_t start;
    start = std::clock();

    char inputFilename[] = "Test-Case-3.cs";   // Test Case File
    char outputFilename[] = "Result.txt"; // Result File

    int totalNumberOfLines = 0;
    int numberOfBranches = 0;
    int statementsCovered = 0;
    float statementCoveragePercentage = 0;
    double overallRuntime = 0;

    ifstream inFile;  // object for reading from a file
    ofstream outFile; // object for writing to a file

    inFile.open(inputFilename, ios::in);
    if (!inFile) {
        cerr << "Can't open input file " << inputFilename << endl;
        exit(1);
    }



    totalNumberOfLines = NoOfLines(inFile);

    inFile.clear();   // reset
    inFile.seekg(0, ios::beg);

    numberOfBranches = NoOfBranches(inFile);
    inFile.close();

    statementsCovered = totalNumberOfLines - numberOfBranches;

    statementCoveragePercentage = (float)statementsCovered * 100/ totalNumberOfLines;

    outFile.open(outputFilename, ios::out);

    if (!outFile) {
        cerr << "Can't open output file " << outputFilename << endl;
        exit(1);
    }

    outFile << "Total Number of Lines" << " : " << totalNumberOfLines << endl;
    outFile << "Number of Branches" << " : " << numberOfBranches << endl;
    outFile << "Statements Covered" << " : " << statementsCovered << endl;
    outFile << "Statement Coverage Percentage" << " : " << statementCoveragePercentage <<"%"<< endl;

    overallRuntime = (std::clock() - start) / (double)CLOCKS_PER_SEC;


    outFile << "Overall Runtime" << " : " << overallRuntime << " Seconds"<< endl;

    outFile.close();

}  

我想通过允许多个线程并行工作以更快地计算数量来最小化计算分支数量所花费的时间?如何编辑代码,以便我可以使用open mp,在这里你可以找到我的函数:bool is_only_ascii_whitespace(const std :: string&amp; str) {

auto it = str.begin();
do {
    if (it == str.end()) return true;
} while (*it >= 0 && *it <= 0x7f && std::isspace(*(it++)));
// one of these conditions will be optimized away by the compiler,
// which one depends on whether char is signed or not
return false;

}

//功能1 int NoOfLines(ifstream&amp; inFile) {

//char line[1000];
string line;
int lines = 0;

while (!inFile.eof()) {

    getline(inFile, line);
    //cout << line << endl;

    if ((line.find("//") == std::string::npos))  // Remove Comments
    {
        if (!is_only_ascii_whitespace(line)) // Remove Blank
        {
            lines++;
        }
    } 
    //cout << line << "~" <<endl;
        }    
return lines;

}

//功能2 int NoOfBranches(ifstream&amp; inFile) {

//char line[1000];
string line;
int branches = 0;

while (!inFile.eof()) {

    getline(inFile, line);



    if ((line.find("if") != std::string::npos) || (line.find("else") != std::string::npos))
    {
        branches++;
    }


}   

    return branches;
    } 

0 个答案:

没有答案