]> git.donarmstrong.com Git - mothur.git/blobdiff - preclustercommand.cpp
pre.cluster unable to spawn necessary processes adjustment.
[mothur.git] / preclustercommand.cpp
index def53255b1c124c2d40ae76120723f78f47a7bec..10ed82571150e9d61e26f62b12508a9e5f967701 100644 (file)
@@ -363,7 +363,8 @@ int PreClusterCommand::createProcessesGroups(string newFName, string newNName, s
                vector<int> processIDS;
                int process = 1;
                int num = 0;
-               
+               bool recalc = false;
+        
                //sanity check
                if (groups.size() < processors) { processors = groups.size(); }
                
@@ -401,12 +402,57 @@ int PreClusterCommand::createProcessesGroups(string newFName, string newNName, s
                 outTemp.close();
                 
                                exit(0);
-                       }else { 
-                               m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine(); 
-                               for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
-                               exit(0);
+                       }else {
+                m->mothurOut("[ERROR]: unable to spawn the number of processes you requested, reducing number to " + toString(process) + "\n"); processors = process;
+                for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                recalc = true;
+                               break;
                        }
                }
+        
+        if (recalc) {
+            lines.clear();
+            num = 0;
+            processIDS.resize(0);
+            process = 1;
+            
+            int remainingPairs = groups.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, (startIndex+numPairs))); //startIndex, endIndex
+                startIndex = startIndex + numPairs;
+                remainingPairs = remainingPairs - numPairs;
+            }
+            
+            while (process != processors) {
+                pid_t pid = fork();
+                
+                if (pid > 0) {
+                    processIDS.push_back(pid);  //create map from line number to pid so you can append files in correct order later
+                    process++;
+                }else if (pid == 0){
+                    outputNames.clear();
+                    num = driverGroups(newFName + m->mothurGetpid(process) + ".temp", newNName + m->mothurGetpid(process) + ".temp", newMFile, lines[process].start, lines[process].end, groups);
+                    
+                    string tempFile = m->mothurGetpid(process) + ".outputNames.temp";
+                    ofstream outTemp;
+                    m->openOutputFile(tempFile, outTemp);
+                    
+                    outTemp << outputNames.size();
+                    for (int i = 0; i < outputNames.size(); i++) { outTemp << outputNames[i] << endl; }
+                    outTemp.close();
+                    
+                    exit(0);
+                }else {
+                    m->mothurOut("[ERROR]: unable to spawn the necessary processes."); m->mothurOutEndLine();
+                    for (int i = 0; i < processIDS.size(); i++) { kill (processIDS[i], SIGINT); }
+                    exit(0);
+                }
+            }
+        }
+
                
                //do my part
                num = driverGroups(newFName, newNName, newMFile, lines[0].start, lines[0].end, groups);