OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
DataConnectCreator.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  * This program was prepared by PSI.
7  * All rights in the program are reserved by PSI.
8  * Neither PSI nor the author(s)
9  * makes any warranty, express or implied, or assumes any liability or
10  * responsibility for the use of this software
11  *
12  * Visit www.amas.web.psi for more details
13  *
14  ***************************************************************************/
15 
16 // -*- C++ -*-
17 /***************************************************************************
18  *
19  * The IPPL Framework
20  *
21  *
22  * Visit http://people.web.psi.ch/adelmann/ for more details
23  *
24  ***************************************************************************/
25 
26 // include files
28 #include "DataSource/DataConnect.h"
30 #include "Utility/IpplInfo.h"
31 #include <cstring>
32 
33 
34 // static data for this file
35 static const int CONNECTMETHODS = 2; // includes "no connection" method
36 static const char *ConnectMethodList = "file, or none";
37 static const char *ConnectMethodNames[CONNECTMETHODS] = {"file", "none" };
38 static bool ConnectMethodSupported[CONNECTMETHODS] = {true, true};
39 
40 
41 // a global instance of DataConnectCreator ... when this is destroyed
42 // at the end of the program, it will clean up the default connection
43 // object, if necessary
44 static DataConnectCreator GlobalDataConnectCreatorInstance;
45 
46 
47 
49 // static member data for DataConnectCreator
52 int DataConnectCreator::DefaultMethod = CONNECTMETHODS - 1;
54 
55 
56 
58 // constructor: increment the instance count
60 
61  InstanceCount++;
62 }
63 
64 
66 // destructor: deccrement the instance count, and if it goes to zero,
67 // delete any static object if necessary
69 
70  if (--InstanceCount == 0)
71  if (DefaultConnection != 0)
72  delete DefaultConnection;
73 }
74 
75 
77 // return the name of the Nth method
79 
80  return CONNECTMETHODS;
81 }
82 
83 
85 // return the name of the Nth method
87 
88  if (n >= 0 && n < CONNECTMETHODS)
89  return ConnectMethodNames[n];
90  else
91  return 0;
92 }
93 
94 
96 // return a list of all the methods, as a single string
98 
99  return ConnectMethodList;
100 }
101 
102 
104 // check if the given connection method is supported
106 
107  return (known(cm) ? ConnectMethodSupported[cm] : false);
108 }
109 
110 
112 // check if the given connection method is supported
113 bool DataConnectCreator::supported(const char *nm) {
114 
115  return supported(libindex(nm));
116 }
117 
118 
120 // check if the given connection method is known at all
122 
123  return (cm >= 0 && cm < CONNECTMETHODS);
124 }
125 
126 
128 // check if the given connection method is known at all
129 bool DataConnectCreator::known(const char *nm) {
130  return known(libindex(nm));
131 }
132 
133 
135 // create a new connection. Arguments = type, name, direction, nodes
136 // If n <= 0, use the "default" number of nodes set earlier
137 DataConnect *DataConnectCreator::create(int cm, const char *nm, int n) {
138 
139  // initially, we have a null pointer for the connection. If everything
140  // checks out, we'll have a non-null pointer at the end. If we still
141  // have a null pointer at the end of this routine, something went wrong
142  // and we return NULL to indicate an error.
143  DataConnect *dataconn = 0;
144 
145  // figure out how many nodes the connection should use, if it cares
146  // at all about that.
147  int nodes = n;
148  if (n <= 0)
149  nodes = getDefaultNodes();
150 
151  if (cm == 0) {
152  // transfer the data to/from a file using some form of parallel I/O
153  dataconn = new FileDataConnect(nm, nodes);
154  } else if (cm == 1) {
155  // just make a dummy connect object, which does nothing
156  dataconn = new DataConnect(nm, getMethodName(cm), DataSource::OUTPUT, nodes);
157  }
158  // make sure we have something
159  if (dataconn == 0) {
160  ERRORMSG("DataConnectCreator: unknown connection method." << endl);
161  }
162  return dataconn;
163 }
164 
165 
167 // a final method for creating objects; this one provides a default name,
168 // and if the default connection object has already been created, this just
169 // returns that one.
171 
172 
173  if (DefaultConnection == 0)
175  return DefaultConnection;
176 }
177 
178 
180 // change the default connection method. Return success.
182 
183  if (supported(cm)) {
184  DefaultMethod = cm;
185  return true;
186  }
187  return false;
188 }
189 
190 
192 // return the index of the given named method, or (-1) if not found
193 int DataConnectCreator::libindex(const char *nm) {
194 
195  for (int i=0; i < CONNECTMETHODS; ++i) {
196  if (strcmp(nm, getMethodName(i)) == 0)
197  return i;
198  }
199 
200  // if here, it was not found
201  return (-1);
202 }
203 
204 
206 // change the default number of nodes to use for the connection
208  ConnectNodes = n;
209 }
210 
211 
213 // return the default number of nodes to use in a connection
215  return (ConnectNodes >= 0 && ConnectNodes <= Ippl::getNodes() ?
216  ConnectNodes :
217  Ippl::getNodes());
218 }
219 
220 
221 /***************************************************************************
222  * $RCSfile: DataConnectCreator.cpp,v $ $Author: adelmann $
223  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:24 $
224  * IPPL_VERSION_ID: $Id: DataConnectCreator.cpp,v 1.1.1.1 2003/01/23 07:40:24 adelmann Exp $
225  ***************************************************************************/
static int getNodes()
Definition: IpplInfo.cpp:773
static bool known(int)
static bool setDefaultMethod(int)
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
static const char * getMethodName(int)
static void setDefaultNodes(int)
static bool supported(int)
static DataConnect * create()
static const char * getAllMethodNames()
static int libindex(const char *)
static DataConnect * DefaultConnection
Inform & endl(Inform &inf)
Definition: Inform.cpp:42