Difference between revisions of "Tutorials:SingleTaskHOWTO"

From ALPS
Jump to: navigation, search
(The SingleWorker)
m (Run the program)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
==  Introduction  ==
 
==  Introduction  ==
  
This tutorial describes, how to write a single task application using parts of the ALPS libary (e.g the parameter libary, the lattice libary or the model libary).
+
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 implementaions of these classes to implement a version of the most famos programm in the world: The ''hello world'' example.
+
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  ==
 
==  Header  ==
Line 41: Line 41:
 
     class SingleTask: public alps::scheduler::WorkerTask {
 
     class SingleTask: public alps::scheduler::WorkerTask {
 
             public:
 
             public:
             SingleTask(const alps::ProcessList$amp; where, const boost::filesystem::path$amp; p):
+
             SingleTask(const alps::ProcessList& where, const boost::filesystem::path& p):
 
             alps::scheduler::WorkerTask(where, p)
 
             alps::scheduler::WorkerTask(where, p)
 
             { construct(); }
 
             { construct(); }
             SingleTask(const alps::ProcessList$amp; where, const alps::Parameters$amp; p):
+
             SingleTask(const alps::ProcessList& where, const alps::Parameters& p):
 
             WorkerTask(where, p)
 
             WorkerTask(where, p)
 
             { construct(); }
 
             { construct(); }
 
             void dostep () {
 
             void dostep () {
                     dynamic_cast<alps::scheduler::Worker$amp;>(*theWorker).run();
+
                     dynamic_cast<alps::scheduler::Worker&>(*theWorker).run();
 
                     finish();
 
                     finish();
 
             }
 
             }
 
             std::string worker_tag() const { return "SingleTask"; }
 
             std::string worker_tag() const { return "SingleTask"; }
             static void print_copyright(std::ostream$amp; out ) {}
+
             static void print_copyright(std::ostream& out ) {}
 
     };
 
     };
  
Line 64: Line 64:
 
==  The main program  ==
 
==  The main program  ==
  
We're nearly done. All components are avalable only the main file is missing:
+
We're nearly done. All components are available, only the main file is missing:
  
  
Line 70: Line 70:
 
             try {
 
             try {
 
                     return alps::scheduler::start(argc, argv, SingleFactory());
 
                     return alps::scheduler::start(argc, argv, SingleFactory());
             } catch (std::exception$amp; exc) {
+
             } catch (std::exception& exc) {
 
                     std::cerr << exc.what() << "\n";
 
                     std::cerr << exc.what() << "\n";
 
                     alps::comm_exit(true);
 
                     alps::comm_exit(true);
Line 82: Line 82:
 
==  Compile the program  ==
 
==  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 sofisticated application:
+
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:
  
  
Line 96: Line 96:
 
==  Run the program  ==
 
==  Run the program  ==
  
To run the program we need a parameterfile to pass the task parameter to the schedler. Here we can pass our message to the world:
+
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";}
 
     {MESSAGE="Hello World";}
  
Thats it. To create the task XML-files we can call the parameters2xml programm
+
That's it. To create the task XML-files we can call the parameter2xml program
  
  

Latest revision as of 00:21, 16 April 2008

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