DataProcessorAlgorithm.cpp 9.65 KB
Newer Older
1
#include "MantidAPI/DataProcessorAlgorithm.h"
2
3
4
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidAPI/IEventWorkspace.h"
5
#include "MantidKernel/System.h"
6
7
8
#include "MantidAPI/FileFinder.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/PropertyManagerDataService.h"
9
#include "MantidKernel/FacilityInfo.h"
10
#include "MantidKernel/PropertyManager.h"
11
#include <stdexcept>
12
#include "Poco/Path.h"
13
14
15
#ifdef MPI_BUILD
#include <boost/mpi.hpp>
#endif
16
17
18
19
20
21
22
23
24
25
26
27
28

using namespace Mantid::Kernel;
using namespace Mantid::API;

namespace Mantid
{
namespace API
{


  //----------------------------------------------------------------------------------------------
  /** Constructor
   */
29
  DataProcessorAlgorithm::DataProcessorAlgorithm() : API::Algorithm()
30
31
32
  {
    m_loadAlg = "Load";
    m_accumulateAlg = "Plus";
33
    m_loadAlgFileProp = "Filename";
34
    m_useMPI = false;
35
    enableHistoryRecordingForChild(true);
36
37
38
39
40
41
42
43
44
  }
    
  //----------------------------------------------------------------------------------------------
  /** Destructor
   */
  DataProcessorAlgorithm::~DataProcessorAlgorithm()
  {
  }

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  //---------------------------------------------------------------------------------------------
  /** Create a Child Algorithm.  A call to this method creates a child algorithm object.
  *  Using this mechanism instead of creating daughter
  *  algorithms directly via the new operator is prefered since then
  *  the framework can take care of all of the necessary book-keeping.
  *
  *  Overrides the method of the same name in Algorithm to enable history tracking by default.
  *
  *  @param name ::           The concrete algorithm class of the Child Algorithm
  *  @param startProgress ::  The percentage progress value of the overall algorithm where this child algorithm starts
  *  @param endProgress ::    The percentage progress value of the overall algorithm where this child algorithm ends
  *  @param enableLogging ::  Set to false to disable logging from the child algorithm
  *  @param version ::        The version of the child algorithm to create. By default gives the latest version.
  *  @param recordHistory ::  Set whether to record history for children. By default this is set to false.
  *  @return shared pointer to the newly created algorithm object
  */
61
  boost::shared_ptr<Algorithm> DataProcessorAlgorithm::createChildAlgorithm(const std::string& name, const double startProgress,
62
      const double endProgress, const bool enableLogging, const int& version)
63
64
  {
    //call parent method to create the child algorithm
65
66
67
    auto alg = Algorithm::createChildAlgorithm(name, startProgress, endProgress, enableLogging, version);
    alg->enableHistoryRecordingForChild(this->isRecordingHistoryForChild());
    if(this->isRecordingHistoryForChild())
68
69
70
71
    {
      //pass pointer to the history object created in Algorithm to the child
      alg->trackAlgorithmHistory(m_history);
    }
72
    return alg;
73
74
  }

75
  void DataProcessorAlgorithm::setLoadAlg(const std::string &alg)
76
77
78
79
80
81
  {
    if (alg.empty())
      throw std::invalid_argument("Cannot set load algorithm to empty string");
    m_loadAlg = alg;
  }

82
83
84
85
86
87
88
89
90
  void DataProcessorAlgorithm::setLoadAlgFileProp(const std::string &filePropName)
  {
    if (filePropName.empty())
      {
        throw std::invalid_argument("Cannot set the load algorithm file property name");
      }
    m_loadAlgFileProp = filePropName;
  }

91
92
93
94
95
96
97
98
99
100
101
102
103
104
  void DataProcessorAlgorithm::setAccumAlg(const std::string &alg)
  {
    if (alg.empty())
      throw std::invalid_argument("Cannot set accumulate algorithm to empty string");
    m_accumulateAlg = alg;
  }

  ITableWorkspace_sptr DataProcessorAlgorithm::determineChunk()
  {
    throw std::runtime_error("DataProcessorAlgorithm::determineChunk is not implemented");
  }

  void DataProcessorAlgorithm::loadChunk()
  {
105

106
107
108
    throw std::runtime_error("DataProcessorAlgorithm::loadChunk is not implemented");
  }

109
110
111
112
113
  /**
   * Assemble the partial workspaces from all MPI processes
   * @param partialWSName :: Name of the workspace to assemble
   * @param outputWSName :: Name of the assembled workspace (available in main thread only)
   */
114
115
  Workspace_sptr DataProcessorAlgorithm::assemble(const std::string &partialWSName, const std::string &outputWSName)
  {
116
    std::string threadOutput = partialWSName;
117
118
#ifdef MPI_BUILD
    Workspace_sptr partialWS = AnalysisDataService::Instance().retrieve(partialWSName);
119
    IAlgorithm_sptr gatherAlg = createChildAlgorithm("GatherWorkspaces");
120
    gatherAlg->setLogging(true);
121
122
    gatherAlg->setAlwaysStoreInADS(true);
    gatherAlg->setProperty("InputWorkspace", partialWS);
123
    gatherAlg->setProperty("PreserveEvents", true);
124
125
    gatherAlg->setPropertyValue("OutputWorkspace", outputWSName);
    gatherAlg->execute();
126
127
128
129

    if (isMainThread()) threadOutput = outputWSName;
#else
    UNUSED_ARG(outputWSName)
130
#endif
131
    Workspace_sptr outputWS = AnalysisDataService::Instance().retrieve(threadOutput);
132
133
134
    return outputWS;
  }

135
136
137
138
139
140
  /**
   * Save a workspace as a nexus file, with check for which thread
   * we are executing in.
   * @param outputWSName :: Name of the workspace to save
   * @param outputFile :: Path to the Nexus file to save
   */
141
142
143
144
145
146
147
148
149
150
  void DataProcessorAlgorithm::saveNexus(const std::string &outputWSName,
      const std::string &outputFile)
  {
    bool saveOutput = true;
#ifdef MPI_BUILD
    if(boost::mpi::communicator().rank()>0) saveOutput = false;
#endif

    if (saveOutput && outputFile.size() > 0)
    {
151
      IAlgorithm_sptr saveAlg = createChildAlgorithm("SaveNexus");
152
153
154
155
156
157
      saveAlg->setPropertyValue("Filename", outputFile);
      saveAlg->setPropertyValue("InputWorkspace", outputWSName);
      saveAlg->execute();
    }
  }

158
  /// Return true if we are running on the main thread
159
160
  bool DataProcessorAlgorithm::isMainThread()
  {
161
    bool mainThread;
162
163
#ifdef MPI_BUILD
    mainThread = (boost::mpi::communicator().rank()==0);
164
165
#else
    mainThread = true;
166
167
168
169
#endif
    return mainThread;
  }

170
  /// Return the number of MPI processes running
171
172
173
174
175
176
177
178
  int DataProcessorAlgorithm::getNThreads()
  {
#ifdef MPI_BUILD
    return boost::mpi::communicator().size();
#else
    return 1;
#endif
  }
179
180
181
182

  /**
   * Determine what kind of input data we have and load it
   * @param inputData :: File path or workspace name
183
   * @param loadQuiet :: If true then the output is not stored in the ADS
184
   */
185
186
  Workspace_sptr DataProcessorAlgorithm::load(const std::string &inputData,
      const bool loadQuiet)
187
188
189
190
191
192
  {
    Workspace_sptr inputWS;

    // First, check whether we have the name of an existing workspace
    if (AnalysisDataService::Instance().doesExist(inputData))
    {
193
      inputWS = AnalysisDataService::Instance().retrieve(inputData);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    }
    else
    {
      std::string foundFile = FileFinder::Instance().getFullPath(inputData);
      if (foundFile.empty())
      {
        // Get facility extensions
        FacilityInfo facilityInfo = ConfigService::Instance().getFacility();
        const std::vector<std::string>  facilityExts = facilityInfo.extensions();
        foundFile = FileFinder::Instance().findRun(inputData, facilityExts);
      }

      if (!foundFile.empty())
      {
208
209
210
        Poco::Path p(foundFile);
        const std::string outputWSName = p.getBaseName();

211
        IAlgorithm_sptr loadAlg = createChildAlgorithm(m_loadAlg);
212
        loadAlg->setProperty(m_loadAlgFileProp, foundFile);
213
214
215
216
        if (!loadQuiet)
        {
          loadAlg->setAlwaysStoreInADS(true);
        }
217
218
219
220
221
222
223
224
225
226

        // Set up MPI if available
#ifdef MPI_BUILD
        // First, check whether the loader allows use to chunk the data
        if (loadAlg->existsProperty("ChunkNumber")
            && loadAlg->existsProperty("TotalChunks"))
        {
          m_useMPI = true;
          // The communicator containing all processes
          boost::mpi::communicator world;
227
228
229
230
          g_log.notice() << "Chunk/Total: " << world.rank()+1 << "/" << world.size() << std::endl;
          loadAlg->setPropertyValue("OutputWorkspace", outputWSName);
          loadAlg->setProperty("ChunkNumber", world.rank()+1);
          loadAlg->setProperty("TotalChunks", world.size());
231
        }
232
233
#else
        loadAlg->setPropertyValue("OutputWorkspace", outputWSName);
234
235
#endif
        loadAlg->execute();
236

237
238
239
240
241
242
243
244
        if (loadQuiet)
        {
          inputWS = loadAlg->getProperty("OutputWorkspace");
        }
        else
        {
          inputWS = AnalysisDataService::Instance().retrieve(outputWSName);
        }
245
      }
246
247
      else
        throw std::runtime_error("DataProcessorAlgorithm::load could process any data");
248
249
250
251
    }
    return inputWS;
  }

252
253
254
255
256
  /**
   * Get the property manager object of a given name from the property manager
   * data service, or create a new one.
   * @param propertyManager :: Name of the property manager to retrieve
   */
257
258
  boost::shared_ptr<PropertyManager>
  DataProcessorAlgorithm::getProcessProperties(const std::string &propertyManager)
259
  {
260
261
262
263
264
265
266
    boost::shared_ptr<PropertyManager> processProperties;
    if (PropertyManagerDataService::Instance().doesExist(propertyManager))
    {
      processProperties = PropertyManagerDataService::Instance().retrieve(propertyManager);
    }
    else
    {
Gigg, Martyn Anthony's avatar
Gigg, Martyn Anthony committed
267
      getLogger().notice() << "Could not find property manager" << std::endl;
268
269
270
271
      processProperties = boost::make_shared<PropertyManager>();
      PropertyManagerDataService::Instance().addOrReplace(propertyManager, processProperties);
    }
    return processProperties;
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  }

  std::vector<std::string> DataProcessorAlgorithm::splitInput(const std::string & input)
  {
    UNUSED_ARG(input);
    throw std::runtime_error("DataProcessorAlgorithm::splitInput is not implemented");
  }

  void DataProcessorAlgorithm::forwardProperties()
  {
    throw std::runtime_error("DataProcessorAlgorithm::forwardProperties is not implemented");
  }
} // namespace Mantid
} // namespace API