The Original CHomP Software
Classes | Typedefs | Enumerations | Functions
chomp::multiwork Namespace Reference

This namespace contains an interface to the MultiWork module which allows one to easily distribute a computational task over multiple processes and run concurrent computations. More...

Classes

class  mwBoxes
 This is a helper class for iterating all the boxes which share a given vertex. More...
 
class  mwCoordinator
 This class defines a generic coordinator task object for the multi-work distributed computations framework. More...
 
class  mwCorners
 This is a helper class for iterating all the corners of a given box. More...
 
class  mwData
 This class is used to convert data structures into a single sequence of bytes and to retrieve this data for the purpose of communication between a coordinator and workers. More...
 
class  mwIniProbes
 This is a helper class for producing an initial set of probes to test the interior of the requested area of parameters. More...
 
class  mwSubCoordinator
 This class defines a coordinator for the multi-work subdivision framework. More...
 
class  mwSubDataPack
 This is a helper class which defines a single data pack used in the communication between coordinator and workers in the multi-work subdivision framework. More...
 
class  mwSubWorker
 This class defines a worker for the multi-work subdivision framework. More...
 
class  mwTask
 This class defines a generic task object (coordinator or worker) for the multi-work distributed computations framework. More...
 
class  mwWorker
 This class defines a generic worker task object for the multi-work distributed computations framework. More...
 
class  mwWorkerData
 A helper class for storing data on a single worker. More...
 

Typedefs

typedef int(* fcompute) (const double *left, const double *right, int dim, int level)
 The type of a function which computes a value of interest for the given product of intervals. More...
 

Enumerations

enum  mwConstants { mwOk = 0 , mwError = -1 , mwNoData = -2 , mwReject = -3 }
 Various error codes used during the communication. More...
 
enum  mwIOconstants {
  mwNone = 0 , mwCanRead = 0x01 , mwCanWrite = 0x02 , mwTimeOut = -4 ,
  mwLost = -5
}
 Input/output flags and error codes used for network communication, mainly for the 'select' function to check which sockets are available for reading/writing. More...
 
enum  mwCodes {
  mwNoMsg = 0x0000 , mwInitMsg = 0x0001 , mwStdMsg = 0x0002 , mwKeepMsg = 0x0004 ,
  mwDontKeepMsg = 0x0008 , mwByeMsg = 0x0010 , mwRejectedMsg = 0x0001 , mwPortMsg = 0x0002
}
 Various message codes used for the communication between the coordinator and workers. More...
 

Functions

void swap (mwWorkerData &data1, mwWorkerData &data2)
 
void swap (mwData &x, mwData &y)
 
template<class type >
mwDataoperator<< (mwData &m, const type &x)
 
template<class type >
mwDataoperator>> (mwData &m, type &x)
 
mwDataoperator<< (mwData &m, const char *x)
 
mwDataoperator<< (mwData &m, const unsigned char *x)
 
mwDataoperator>> (mwData &m, char *x)
 
mwDataoperator>> (mwData &m, unsigned char *x)
 
int mwSendBytes (int fd, const char *buf, int len)
 Sends the given buffer to the given socket. More...
 
int mwRecvBytes (int fd, char *buf, int len)
 Receives the given amount of data from the given socket. More...
 
int mwConnect (const char *name, int port)
 Connects to the given computer at the given port. More...
 
int mwListen (int port, int queuesize)
 Begins listening at the given port. More...
 
int mwAccept (int fd, std::string &computer, int timeout=-1)
 Waits for and accepts a connection at the given socket. More...
 
int mwSelect (const int *workers, int nworkers, int listensocket, int *ioflags, int timeout)
 Determines IOflags for each of the workers and additionally the listensocket (the last flag). More...
 
void mwDisconnect (int fd)
 Disconnects the given socket. More...
 
template<class coord >
double mwSubdivPoint (const double &left, const double &right, coord part, coord full)
 Returns a subdivision point corresponding to the given fraction of the provided interval. More...
 
template<int dim, class coord >
int mwSubdivMain (int argc, char *argv[], const char *title, const char *helpinfo, int defaultPortNumber, int controlNumber, const double *paramLeft, const double *paramRight, int minSubdivLevel, fcompute compute)
 The main procedure for running the computations in the multiwork subdivision framework. More...
 

Detailed Description

This namespace contains an interface to the MultiWork module which allows one to easily distribute a computational task over multiple processes and run concurrent computations.

Typedef Documentation

◆ fcompute

typedef int(* chomp::multiwork::fcompute) (const double *left, const double *right, int dim, int level)

The type of a function which computes a value of interest for the given product of intervals.

The subdivision level is provided in case the computations on higher levels need to be done in a more thorough way.

Definition at line 102 of file mwsubdiv.h.

Enumeration Type Documentation

◆ mwCodes

Various message codes used for the communication between the coordinator and workers.

Enumerator
mwNoMsg 

Message: No special code information included.

mwInitMsg 

Message to the Worker: A piece of initialization data to be processed.

mwStdMsg 

Message to the Worker: A standard piece of data to be processed.

mwKeepMsg 

Message to the Worker: Keep running after having disconnected.

mwDontKeepMsg 

Message to the Worker: Don't keep running after disconnecting.

mwByeMsg 

Message to the Worker: Please, disconnect.

mwRejectedMsg 

Message to the Coordinator: The data has been rejected.

mwPortMsg 

Message to the Coordinator: I will listen at this port number.

Definition at line 56 of file mwtask.h.

57{
59 mwNoMsg = 0x0000,
60
63 mwInitMsg = 0x0001,
64
66 mwStdMsg = 0x0002,
67
69 mwKeepMsg = 0x0004,
70
72 mwDontKeepMsg = 0x0008,
73
75 mwByeMsg = 0x0010,
76
78 mwRejectedMsg = 0x0001,
79
81 mwPortMsg = 0x0002
82
83}; /* enum mwCodes */
@ mwKeepMsg
Message to the Worker: Keep running after having disconnected.
Definition: mwtask.h:69
@ mwStdMsg
Message to the Worker: A standard piece of data to be processed.
Definition: mwtask.h:66
@ mwInitMsg
Message to the Worker: A piece of initialization data to be processed.
Definition: mwtask.h:63
@ mwRejectedMsg
Message to the Coordinator: The data has been rejected.
Definition: mwtask.h:78
@ mwByeMsg
Message to the Worker: Please, disconnect.
Definition: mwtask.h:75
@ mwPortMsg
Message to the Coordinator: I will listen at this port number.
Definition: mwtask.h:81
@ mwNoMsg
Message: No special code information included.
Definition: mwtask.h:59
@ mwDontKeepMsg
Message to the Worker: Don't keep running after disconnecting.
Definition: mwtask.h:72

◆ mwConstants

Various error codes used during the communication.

The OK code must equal zero, and error codes must be negative.

Enumerator
mwOk 

Everything is fine.

mwError 

A serious error occurred.

mwNoData 

There is no data to be sent to workers, for example, because everything has been already sent.

mwReject 

The data has been rejected.

Definition at line 156 of file mwconfig.h.

157{
159 mwOk = 0,
160
162 mwError = -1,
163
166 mwNoData = -2,
167
169 mwReject = -3
170
171}; /* enum mwConstants */
@ mwNoData
There is no data to be sent to workers, for example, because everything has been already sent.
Definition: mwconfig.h:166
@ mwReject
The data has been rejected.
Definition: mwconfig.h:169
@ mwError
A serious error occurred.
Definition: mwconfig.h:162
@ mwOk
Everything is fine.
Definition: mwconfig.h:159

◆ mwIOconstants

Input/output flags and error codes used for network communication, mainly for the 'select' function to check which sockets are available for reading/writing.

Enumerator
mwNone 

No flag selected.

mwCanRead 

Reading possible.

mwCanWrite 

Writing possible.

mwTimeOut 

A connection time out has occurred.

mwLost 

The network connection has been lost.

Definition at line 51 of file mwlowlev.h.

52{
54 mwNone = 0,
55
57 mwCanRead = 0x01,
58
60 mwCanWrite = 0x02,
61
63 mwTimeOut = -4,
64
66 mwLost = -5
67
68}; /* enum mwIOconstants */
@ mwTimeOut
A connection time out has occurred.
Definition: mwlowlev.h:63
@ mwCanWrite
Writing possible.
Definition: mwlowlev.h:60
@ mwCanRead
Reading possible.
Definition: mwlowlev.h:57
@ mwNone
No flag selected.
Definition: mwlowlev.h:54
@ mwLost
The network connection has been lost.
Definition: mwlowlev.h:66

Function Documentation

◆ mwAccept()

int chomp::multiwork::mwAccept ( int  fd,
std::string &  computer,
int  timeout = -1 
)

Waits for and accepts a connection at the given socket.

Saves the computer name in the string provided. Returns the new socket number (non-negative), mwTimeOut or mwError.

Referenced by chomp::multiwork::mwCoordinator::RunLoop(), and chomp::multiwork::mwWorker::Work().

◆ mwConnect()

int chomp::multiwork::mwConnect ( const char *  name,
int  port 
)

Connects to the given computer at the given port.

Returns the socket number (non-negative) or mwError.

Referenced by chomp::multiwork::mwCoordinator::ConnectWorkers(), chomp::multiwork::mwTask::QuitWorkers(), and chomp::multiwork::mwWorker::Work().

◆ mwDisconnect()

void chomp::multiwork::mwDisconnect ( int  fd)

Disconnects the given socket.

If the number is negative, then this function call is ignored.

Referenced by chomp::multiwork::mwCoordinator::DisconnectAll(), chomp::multiwork::mwTask::QuitWorkers(), chomp::multiwork::mwCoordinator::RunLoop(), and chomp::multiwork::mwWorker::Work().

◆ mwListen()

int chomp::multiwork::mwListen ( int  port,
int  queuesize 
)

Begins listening at the given port.

Allows for the given queue length. Returns the socket number (non-negative) or mwError.

Referenced by chomp::multiwork::mwCoordinator::BeginListening(), and chomp::multiwork::mwWorker::Work().

◆ mwRecvBytes()

int chomp::multiwork::mwRecvBytes ( int  fd,
char *  buf,
int  len 
)

Receives the given amount of data from the given socket.

Returns mwOk, mwError or mwLost.

Referenced by chomp::multiwork::mwTask::RecvMessage().

◆ mwSelect()

int chomp::multiwork::mwSelect ( const int *  workers,
int  nworkers,
int  listensocket,
int *  ioflags,
int  timeout 
)

Determines IOflags for each of the workers and additionally the listensocket (the last flag).

The IOflags of interest are initially set. Returns mwOk, mwTimeOut or mwError.

Referenced by chomp::multiwork::mwCoordinator::RunLoop(), and chomp::multiwork::mwWorker::WorkOne().

◆ mwSendBytes()

int chomp::multiwork::mwSendBytes ( int  fd,
const char *  buf,
int  len 
)

Sends the given buffer to the given socket.

Returns mwOk, mwError or mwLost.

Referenced by chomp::multiwork::mwTask::SendMessage().

◆ mwSubdivMain()

template<int dim, class coord >
int chomp::multiwork::mwSubdivMain ( int  argc,
char *  argv[],
const char *  title,
const char *  helpinfo,
int  defaultPortNumber,
int  controlNumber,
const double *  paramLeft,
const double *  paramRight,
int  minSubdivLevel,
fcompute  compute 
)

The main procedure for running the computations in the multiwork subdivision framework.

Returns: 0 = Ok, -1 = Error, 1 = Help displayed, 2 = Wrong arguments.

Definition at line 1464 of file mwsubdiv.h.

1469{
1470 const char *arginfo = "\
1471Command line arguments (at least '-w' or '-m N' must be specified):\n\
1472-w [port] - run as a worker (by default the program runs as a coordinator),\n\
1473-c [port] - run as a coordinator only (don't process any data locally),\n\
1474-p port - set the port number for the multi-work communication,\n\
1475-k - keep workers waiting after the computations have been completed,\n\
1476computer:port - use this connection at start-up (can be repeated),\n\
1477-s FILE - save the workers' list to this file (default: mwsubdiv.txt),\n\
1478-r FILE - retrieve the workers' list from this file (def: mwsubdiv.txt),\n\
1479-f filename - results file (new results will be appended); coord only!\n\
1480--flush - flush the results file very frequently (slows down the program),\n\
1481-i N - set the initial subdivision level (some minimum is enforced),\n\
1482-m N - set the maximal subdivision level (default: inilevel + 2),\n\
1483-q - quit all the workers who are waiting (provide addresses or use -r),\n\
1484--quiet - do not display any messages on the standard output,\n\
1485--log filename - save the console output to the given file,\n\
1486--help - show this brief help information and exit.\n\
1487For more information ask the author at http://www.PawelPilarczyk.com/.";
1488
1489 // prepare user-configurable data
1490 char *retrieveworkers = 0;
1491 char *saveworkers = 0;
1492 const int maxaddr = 1024;
1493 char *addr [maxaddr];
1494 int naddr = 0;
1495 int portnum = -1;
1496 int workport = -1;
1497 int coordport = -1;
1498 bool keepworkers = false;
1499 bool quitworkers = false;
1500 char *filename = 0;
1501 int inilevel = 0;
1502 int maxlevel = 0;
1503 bool flushfile = false;
1504
1505 // interprete the command-line arguments
1506 arguments a;
1507 arg (a, 0, addr, naddr, maxaddr);
1508 arg (a, "r", retrieveworkers, "mwsubdiv.txt");
1509 arg (a, "s", saveworkers, "mwsubdiv.txt");
1510 arg (a, "f", filename);
1511 arg (a, "i", inilevel);
1512 arg (a, "m", maxlevel);
1513 arg (a, "w", workport, defaultPortNumber);
1514 arg (a, "p", portnum);
1515 arg (a, "c", coordport, defaultPortNumber);
1516 argswitch (a, "k", keepworkers, true);
1517 argswitch (a, "q", quitworkers, true);
1518 argswitch (a, "-flush", flushfile, true);
1519 arghelp (a);
1520
1521 argstreamprepare (a);
1522 int argresult = a. analyze (argc, argv);
1523 argstreamset ();
1524
1525 // show the program's main title
1526 if (argresult >= 0)
1527 sout << title << '\n';
1528
1529 // if something was incorrect, show an additional message and exit
1530 if (argresult < 0)
1531 {
1532 sout << "Call with '--help' for help.\n";
1533 return 2;
1534 }
1535
1536 // set the right port number and determine if to run as a worker
1537 // or as a coordinator, with or without local work
1538 int port = defaultPortNumber;
1539 bool localwork = (coordport < 0);
1540 if (coordport >= 0)
1541 port = coordport;
1542 bool worker = (workport >= 0);
1543 if (workport >= 0)
1544 port = workport;
1545 if (portnum >= 0)
1546 port = portnum;
1547
1548 // if no data packs are to be processed, don't run the program
1549 if (!maxlevel && !worker && !quitworkers)
1550 argresult = 1;
1551
1552 // if help requested, show help information
1553 if (argresult > 0)
1554 {
1555 sout << helpinfo << '\n' << arginfo << '\n';
1556 return 1;
1557 }
1558
1559 // try running the main function and catch an error message if thrown
1560 try
1561 {
1562 // set an appropriate program time message
1563 program_time = "Aborted after:";
1564 program_time = 1;
1565
1566 // quit all the workers from the list if requested to
1567 if (quitworkers)
1568 {
1569 // prepare a dummy coordinator class
1570 mwSubCoordinator<dim,coord> c;
1571
1572 // set up the parameters necessary for identification
1573 c. Port (port);
1574 c. ControlNumber (controlNumber);
1575
1576 // prepare a list of workers' addresses
1577 if (retrieveworkers)
1578 c. Load (retrieveworkers);
1579 for (int i = 0; i < naddr; ++ i)
1580 c. Add (addr [i]);
1581
1582 // quit all the workers which appear in the list
1583 sout << "Quitting workers... ";
1584 c. QuitWorkers ();
1585 sout << "Done.\n";
1586 }
1587
1588 // run as a worker if requested to
1589 else if (worker)
1590 {
1591 // prepare a worker object
1592 mwSubWorker<dim,coord> w (compute);
1593
1594 // set up various options of the worker object
1595 w. Port (port);
1596 w. ControlNumber (controlNumber);
1597 for (int i = 0; i < naddr; ++ i)
1598 w. Add (addr [i]);
1599
1600 // run the computations
1601 sout << "Running as a worker...\n";
1602 int result = w. Work ();
1603 if (result == mwOk)
1604 sout << "Work completed successfully.\n";
1605 else
1606 sout << "Could not work - probably "
1607 "an error occurred.\n";
1608 }
1609
1610 // run as a coordinator otherwise
1611 else
1612 {
1613 // make a correction to the subdivision level bounds
1614 if (inilevel <= minSubdivLevel)
1615 inilevel = minSubdivLevel;
1616 if (maxlevel <= inilevel)
1617 maxlevel = inilevel + 2;
1618
1619 // prepare a local worker and a coordinator
1620 mwSubWorker<dim,coord> w (compute);
1621 mwSubCoordinator<dim,coord> c (filename, inilevel,
1622 maxlevel, flushfile, paramLeft, paramRight);
1623
1624 // set up various options of the coordinator
1625 c. KeepWorkers (keepworkers);
1626 c. Port (port);
1627 c. ControlNumber (controlNumber);
1628 for (int i = 0; i < naddr; ++ i)
1629 c. Add (addr [i]);
1630 if (retrieveworkers)
1631 c. Load (retrieveworkers);
1632
1633 // run the computations
1634 sout << "Running as a coordinator...\n";
1635 int result = c. Coordinate (localwork ? &w : 0);
1636 if (result == mwOk)
1637 sout << "The task completed successfully.\n";
1638 else
1639 sout << "Could not coordinate - probably "
1640 "an error occurred.\n";
1641
1642 // save the connected workers if necessary
1643 if (saveworkers)
1644 c. SaveWorkers (saveworkers);
1645 }
1646
1647 // set an appropriate program time message
1648 program_time = "Total time used:";
1649
1650 // finalize
1651 return 0;
1652 }
1653 catch (const char *msg)
1654 {
1655 sout << "ERROR: " << msg << '\n';
1656 return -1;
1657 }
1658 catch (const std::exception &e)
1659 {
1660 sout << "ERROR: " << e. what () << '\n';
1661 return -1;
1662 }
1663 catch (...)
1664 {
1665 sout << "ABORT: An unknown error occurred.\n";
1666 return -1;
1667 }
1668} /* mwSubdivMain */
#define argstreamprepare(a)
This macrodefinition sets up command line arguments for the analysis of typical arguments related to ...
Definition: arg.h:838
#define argstreamset()
This macrodefinition sets up the streams defined in the module "textfile.h", based on the analyzed co...
Definition: arg.h:852
The objects of this class gather the expected command-line arguments and decode them.
Definition: arg.h:534
outputstream sout
A replacement for standard output stream, with optional logging and other features provided by the cl...
void arg(arguments &a, const char *name, type &value)
Adds a command line argument.
Definition: arg.h:604
timeused program_time
The external variable which measures the time used by the program from its start.
void argswitch(arguments &arg, const char *name, type &value, const type &defaultvalue)
Defines a command line argument which is a switch, that is, there is no value given for it in the com...
Definition: arg.h:745
void arghelp(arguments &a)
Adds the typical arguments which should make the program display help information.
Definition: arg.h:778

References chomp::homology::arg(), chomp::homology::arghelp(), argstreamprepare, argstreamset, chomp::homology::argswitch(), mwOk, chomp::homology::program_time, and chomp::homology::sout.

◆ mwSubdivPoint()

template<class coord >
double chomp::multiwork::mwSubdivPoint ( const double &  left,
const double &  right,
coord  part,
coord  full 
)
inline

Returns a subdivision point corresponding to the given fraction of the provided interval.

Should be called with part = 0, ..., full, to get a subdivision of the entire interval, including the endpoints.

Definition at line 202 of file mwsubdiv.h.

204{
205 // uncomment if using CAPD to switch the rounding mode to the nearest
206// round_nearest ();
207
208 // if this is a boundary subdivision point then return the bound
209 if (part == 0)
210 return left;
211 else if (part == full)
212 return right;
213
214 // divide 'part' and 'full' by their largest common divisor
215 // which is a power of two
216 while (!(part & 1) && !(full & 1))
217 {
218 part >>= 1;
219 full >>= 1;
220 }
221
222 // compute the corresponding subdivision point of the interval
223 double fraction = static_cast<double> (part) / full;
224 return (left + (right - left) * fraction);
225} /* mwSubdivPoint */

Referenced by chomp::multiwork::mwSubCoordinator< dim, coord >::Prepare().

◆ operator<<() [1/3]

mwData & chomp::multiwork::operator<< ( mwData m,
const char *  x 
)
inline

Definition at line 842 of file mwdata.h.

843{
844 return m. Append (x);
845} /* operator << */

◆ operator<<() [2/3]

template<class type >
mwData & chomp::multiwork::operator<< ( mwData m,
const type &  x 
)
inline

Definition at line 831 of file mwdata.h.

832{
833 return m. Append (x);
834} /* operator << */

◆ operator<<() [3/3]

mwData & chomp::multiwork::operator<< ( mwData m,
const unsigned char *  x 
)
inline

Definition at line 847 of file mwdata.h.

848{
849 return m. Append (x);
850} /* operator << */

◆ operator>>() [1/3]

mwData & chomp::multiwork::operator>> ( mwData m,
char *  x 
)
inline

Definition at line 852 of file mwdata.h.

853{
854 return m. Retrieve (x);
855} /* operator >> */

◆ operator>>() [2/3]

template<class type >
mwData & chomp::multiwork::operator>> ( mwData m,
type &  x 
)
inline

Definition at line 837 of file mwdata.h.

838{
839 return m. Retrieve (x);
840} /* operator >> */

◆ operator>>() [3/3]

mwData & chomp::multiwork::operator>> ( mwData m,
unsigned char *  x 
)
inline

Definition at line 857 of file mwdata.h.

858{
859 return m. Retrieve (x);
860} /* operator >> */

◆ swap() [1/2]

void chomp::multiwork::swap ( mwData x,
mwData y 
)
inline

Definition at line 267 of file mwdata.h.

268{
269 x. Swap (y);
270 return;
271} /* swap */

◆ swap() [2/2]

void chomp::multiwork::swap ( mwWorkerData data1,
mwWorkerData data2 
)
inline