X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=parsimony.cpp;h=1b08e8a3fe8470e5d34944722e186fdca7e0731b;hb=250e3b11b1c9c1e1ad458ab6c7e71ac2e67e11d9;hp=6a0485c133b2868670e49e5873186e5d10df474d;hpb=90708fe9701e3827e477c82fb3652539c3bf2a0d;p=mothur.git diff --git a/parsimony.cpp b/parsimony.cpp index 6a0485c..1b08e8a 100644 --- a/parsimony.cpp +++ b/parsimony.cpp @@ -54,31 +54,19 @@ EstOutput Parsimony::getValues(Tree* t, int p, string o) { namesOfGroupCombos.push_back(groups); } } - - #if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - if(processors == 1){ - data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), ct); - }else{ - lines.clear(); - int numPairs = namesOfGroupCombos.size(); - - int numPairsPerProcessor = numPairs / processors; - - for (int i = 0; i < processors; i++) { - int startPos = i * numPairsPerProcessor; - - if(i == processors - 1){ - numPairsPerProcessor = numPairs - i * numPairsPerProcessor; - } - - lines.push_back(linePair(startPos, numPairsPerProcessor)); - } - - data = createProcesses(t, namesOfGroupCombos, ct); - } - #else - data = driver(t, namesOfGroupCombos, 0, namesOfGroupCombos.size(), ct); - #endif + + lines.clear(); + int remainingPairs = namesOfGroupCombos.size(); + int startIndex = 0; + for (int remainingProcessors = processors; remainingProcessors > 0; remainingProcessors--) { + int numPairs = remainingPairs; //case for last processor + if (remainingProcessors != 1) { numPairs = ceil(remainingPairs / remainingProcessors); } + lines.push_back(linePair(startIndex, numPairs)); //startIndex, numPairs + startIndex = startIndex + numPairs; + remainingPairs = remainingPairs - numPairs; + } + + data = createProcesses(t, namesOfGroupCombos, ct); return data; @@ -92,12 +80,13 @@ EstOutput Parsimony::getValues(Tree* t, int p, string o) { EstOutput Parsimony::createProcesses(Tree* t, vector< vector > namesOfGroupCombos, CountTable* ct) { try { -#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) - int process = 1; + int process = 1; vector processIDS; EstOutput results; - + +#if defined (__APPLE__) || (__MACH__) || (linux) || (__linux) || (__linux__) || (__unix__) || (__unix) + //loop through and create all the processes you want while (process != processors) { int pid = fork(); @@ -160,9 +149,47 @@ EstOutput Parsimony::createProcesses(Tree* t, vector< vector > namesOfGr in.close(); m->mothurRemove(s); } +#else + //fill in functions + vector pDataArray; + DWORD dwThreadIdArray[processors-1]; + HANDLE hThreadArray[processors-1]; + vector cts; + vector trees; + + //Create processor worker threads. + for( int i=1; icopy(ct); + Tree* copyTree = new Tree(copyCount); + copyTree->getCopy(t); + + cts.push_back(copyCount); + trees.push_back(copyTree); + + parsData* temppars = new parsData(m, lines[i].start, lines[i].num, namesOfGroupCombos, copyTree, copyCount); + pDataArray.push_back(temppars); + processIDS.push_back(i); + + hThreadArray[i-1] = CreateThread(NULL, 0, MyParsimonyThreadFunction, pDataArray[i-1], 0, &dwThreadIdArray[i-1]); + } + + results = driver(t, namesOfGroupCombos, lines[0].start, lines[0].num, ct); + + //Wait until all threads have terminated. + WaitForMultipleObjects(processors-1, hThreadArray, TRUE, INFINITE); + + //Close all thread handles and free memory allocations. + for(int i=0; i < pDataArray.size(); i++){ + for (int j = 0; j < pDataArray[i]->results.size(); j++) { results.push_back(pDataArray[i]->results[j]); } + delete cts[i]; + delete trees[i]; + CloseHandle(hThreadArray[i]); + delete pDataArray[i]; + } - return results; #endif + return results; } catch(exception& e) { m->errorOut(e, "Parsimony", "createProcesses");