DataProcessorAlgorithm.cpp 5.93 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
9
#include "MantidAPI/FileFinder.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/PropertyManagerDataService.h"
#include "MantidKernel/PropertyManager.h"
10
#include <stdexcept>
11
12
13
#ifdef MPI_BUILD
#include <boost/mpi.hpp>
#endif
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

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

namespace Mantid
{
namespace API
{


  //----------------------------------------------------------------------------------------------
  /** Constructor
   */
  DataProcessorAlgorithm::DataProcessorAlgorithm()
  {
    m_loadAlg = "Load";
    m_accumulateAlg = "Plus";
31
    m_useMPI = false;
32
33
34
35
36
37
38
39
40
41
  }
    
  //----------------------------------------------------------------------------------------------
  /** Destructor
   */
  DataProcessorAlgorithm::~DataProcessorAlgorithm()
  {
  }

  //----------------------------------------------------------------------------------------------
42
  void DataProcessorAlgorithm::setLoadAlg(const std::string &alg)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  {
    if (alg.empty())
      throw std::invalid_argument("Cannot set load algorithm to empty string");
    m_loadAlg = alg;
  }

  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()
  {
63

64
65
66
    throw std::runtime_error("DataProcessorAlgorithm::loadChunk is not implemented");
  }

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  Workspace_sptr DataProcessorAlgorithm::assemble(const std::string &partialWSName, const std::string &outputWSName)
  {
#ifdef MPI_BUILD
    Workspace_sptr partialWS = AnalysisDataService::Instance().retrieve(partialWSName);
    IAlgorithm_sptr gatherAlg = createSubAlgorithm("GatherWorkspaces");
    gatherAlg->setAlwaysStoreInADS(true);
    gatherAlg->setProperty("InputWorkspace", partialWS);
    gatherAlg->setPropertyValue("OutputWorkspace", outputWSName);
    gatherAlg->execute();
#endif
    Workspace_sptr outputWS = AnalysisDataService::Instance().retrieve(outputWSName);
    return outputWS;
  }

  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)
    {
      IAlgorithm_sptr saveAlg = createSubAlgorithm("SaveNexus");
      saveAlg->setPropertyValue("Filename", outputFile);
      saveAlg->setPropertyValue("InputWorkspace", outputWSName);
      saveAlg->execute();
    }
  }

99
100
101
102
103
  /// Determine what kind of input data we have and load it
  //TODO: Chunking, MPI, etc...
  Workspace_sptr DataProcessorAlgorithm::load(const std::string &inputData)
  {
    Workspace_sptr inputWS;
104
    std::string outputWSName = inputData;
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

    // First, check whether we have the name of an existing workspace
    if (AnalysisDataService::Instance().doesExist(inputData))
    {
      Workspace_sptr inputWS = AnalysisDataService::Instance().retrieve(inputData);
    }
    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())
      {
        IAlgorithm_sptr loadAlg = createSubAlgorithm(m_loadAlg);
        loadAlg->setProperty("Filename", foundFile);
        loadAlg->setAlwaysStoreInADS(true);

        // 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;
137
138
139
140
          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());
141
        }
142
143
#else
        loadAlg->setPropertyValue("OutputWorkspace", outputWSName);
144
145
#endif
        loadAlg->execute();
146
147

        Workspace_sptr inputMatrixWS = AnalysisDataService::Instance().retrieve(outputWSName);
148
149
150
151
152
153
154
155
156
      }
    }
    return inputWS;
  }

  /// Get the property manager object of a given name from the property manager
  /// data service, or create a new one.
  boost::shared_ptr<PropertyManager>
  DataProcessorAlgorithm::getProcessProperties(const std::string &propertyManager)
157
  {
158
159
160
161
162
163
164
165
166
167
168
169
    boost::shared_ptr<PropertyManager> processProperties;
    if (PropertyManagerDataService::Instance().doesExist(propertyManager))
    {
      processProperties = PropertyManagerDataService::Instance().retrieve(propertyManager);
    }
    else
    {
      g_log.notice() << "Could not find property manager" << std::endl;
      processProperties = boost::make_shared<PropertyManager>();
      PropertyManagerDataService::Instance().addOrReplace(propertyManager, processProperties);
    }
    return processProperties;
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  }

  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