Tutorials:SingleTaskHOWTO

From ALPS
Jump to: navigation, search

Introduction

This tutorial describes how to write a single task application using parts of the ALPS library (e.g. the parameter library, the lattice library or the model library).

The ALPS Scheduler needs a worker class, a task class and a factory class. We will develop implementations of these classes to implement a version of the most famous program in the world: The hello world example.

Header

First wir have to load the ALPS::Scheduler library:


   #include <alps/scheduler.h>
   #include <alps/osiris/comm.h>
   #include <stdexcept>

The SingleWorker

In the worker class all the work is done. This is the only call you have to change, to write your own application. We inherit our worker class from the alps::scheduler::Worker class, a generic prototype of a worker class:


   class SingleWorker: public alps::scheduler::Worker {
           public:
           SingleWorker(const alps::ProcessList& where, const alps::Parameters& p, int node):
           alps::scheduler::Worker(where, p, node)
           {}
           void dostep() {
                   std::cout << parms["MESSAGE"] << std::endl << std::endl;
           }
           static void print_copyright(std::ostream& out ) {}
   };

In the dostep function all the Work is done. If you develop your own application, this function ist the point to call your applicaiton. You should also put your copyright in the print_copyright function.

You should notice the term parms["MESSAGE"]. This Message is passed from the parameterfile. The params member variable is a protected member of the alps::scheduler::Worker class and represents all parameter passt throu the paramter file.

The SingleTask

The Task class is only needed to call the Worker class:


   class SingleTask: public alps::scheduler::WorkerTask {
           public:
           SingleTask(const alps::ProcessList& where, const boost::filesystem::path& p):
           alps::scheduler::WorkerTask(where, p)
           { construct(); }
           SingleTask(const alps::ProcessList& where, const alps::Parameters& p):
           WorkerTask(where, p)
           { construct(); }
           void dostep () {
                   dynamic_cast<alps::scheduler::Worker&>(*theWorker).run();
                   finish();
           }
           std::string worker_tag() const { return "SingleTask"; }
           static void print_copyright(std::ostream& out ) {}
   };

The SingleFactory

The scheduler needs a factory to create instances of the worker and the task class. There is a generic class in ALPS to create factories:


   typedef alps::scheduler::BasicFactory<SingleTask, SingleWorker> SingleFactory;

The main program

We're nearly done. All components are available, only the main file is missing:


   int main(int argc, char** argv) {
           try {
                   return alps::scheduler::start(argc, argv, SingleFactory());
           } catch (std::exception& exc) {
                   std::cerr << exc.what() << "\n";
                   alps::comm_exit(true);
                   return -1;
           } catch (...) {
                   std::cerr << "Fatal Error: Unknown Exception!\n";
                   return -2;
           }
   }

Compile the program

We don't want to write a makefile for a single file application, so we can compile it by hand. If ALPS is installed in $HOME/ALPS and boost in $HOME/boost the follwoing command compiles our very exciting and sophisticated application:


   g++ -O3 -o helloworld.out \
   -I$HOME/boost \
   -I$HOME/ALPS/include \
   -L$HOME/ALPS/lib \
   -lalps \
   -lcomm-sgl \
   -lexpat \
   main.cpp

Run the program

To run the program we need a parameter file to pass the task parameter to the scheduler. Here we can pass our message to the world:


   {MESSAGE="Hello World";}

That's it. To create the task XML-files we can call the parameter2xml program


   parameter2xml params

and get a params.in.xml among some other files. Now we can call our application


   helloworld.out params.in.xml


$copy; 2007 by Lukas Gamper