NCBI C++ ToolKit
blast_async_format.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: blast_async_format.cpp 76972 2017-03-16 12:03:45Z madden $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Tom Madden
27  *
28  * File Description:
29  * Class to print results
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
35 
36 
37 // Global mutex
39 
40 
43 USING_SCOPE(blast);
44 
46 {
47 }
48 
49 void
51  vector<SFormatResultValues> results)
52 {
53  if (m_Done == true)
54  NCBI_THROW(CException, eUnknown, "QueueResults called after Finalize");
55  if (m_ResultsMap.find(batchNumber) != m_ResultsMap.end())
56  {
57  string message = "Duplicate batchNumber entered: " + NStr::NumericToString(batchNumber);
58  NCBI_THROW(CException, eUnknown, "message");
59  }
61  m_ResultsMap.insert(std::pair<int, vector<SFormatResultValues>>(batchNumber, results));
63  m_Semaphore.Post();
64 }
65 
66 
67 void
69 {
71  m_Done=true;
73  m_Semaphore.Post();
74 }
75 
76 void
78 {
79  if(m_Done == false)
80  Finalize();
81 
82  CThread::Join();
83 }
84 
85 
87 {
88  const int kVecSize=5000; // Large array so we should not wrap around.
89  vector<vector<SFormatResultValues>> results_v;
90  results_v.resize(kVecSize);
91  int currNum=0;
92  int lastNum=0;
93  while (1)
94  {
95  m_Semaphore.Wait();
97  for(std::map<int, vector<SFormatResultValues>>::iterator itr=m_ResultsMap.begin(); itr != m_ResultsMap.end(); itr++)
98  {
99  if (itr->first < currNum)
100  continue;
101  else if (itr->first > currNum)
102  break;
103 
104  results_v[currNum%kVecSize].swap(itr->second);
105  currNum++;
106  }
108 
109  for (int index=lastNum; index<currNum; ++index)
110  {
111  for(vector<SFormatResultValues>::iterator vecitr=results_v[index%kVecSize].begin();
112  vecitr != results_v[index%kVecSize].end(); vecitr++)
113  {
114  ITERATE(CSearchResultSet, result, *((*vecitr).blastResults))
115  (*vecitr).formatter->PrintOneResultSet(**result, (*vecitr).qVec);
116  }
117  results_v[index%kVecSize].clear();
118  }
119  lastNum=currNum;
120  if (m_Done == true) // All worker threads done.
121  {
122  if (m_ResultsMap.size() != currNum)
123  { // More results that have been loaded but not printed.
124  m_Semaphore.Post();
125  continue;
126  }
127  else
128  break;
129  }
130  }
131  return (void*) NULL;
132 }
133 
USING_SCOPE(objects)
CFastMutex blastProcessGuard
USING_NCBI_SCOPE
void Finalize()
Close queue for printing. No calls to QueueResults allowed after this.
std::map< int, vector< SFormatResultValues > > m_ResultsMap
virtual void * Main(void)
Derived (user-created) class must provide a real thread function.
void Join()
Calls Finalize (if not already called) then CThread::Join(); Should only be called if QueueResults wi...
void QueueResults(int batchNumber, vector< SFormatResultValues > results)
Queue results for printing.
virtual ~CBlastAsyncFormatThread(void)
CFastMutex –.
Definition: ncbimtx.hpp:667
Search Results for All Queries.
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
#define NULL
Definition: ncbistd.hpp:225
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
void Lock(void)
Acquire mutex for the current thread with no nesting checks.
void Wait(void)
Wait on semaphore.
Definition: ncbimtx.cpp:1787
void Post(unsigned int count=1)
Increment the semaphore by "count".
Definition: ncbimtx.cpp:1971
void Join(void **exit_data=0)
Wait for the thread termination.
Definition: ncbithr.cpp:863
void Unlock(void)
Release mutex with no owner or nesting checks.
else result
Definition: token2.c:20
Modified on Fri Feb 23 11:49:45 2024 by modify_doxy.py rev. 669887