Saturday, November 2, 2013

Introduction To OpenMP

INTRODUCTION

This manual documents the usage of libgomp, the GNU implementation of the Openmp. Application Programming Interface (API) for multi-platform shared-memory parallel programming in C/C++ and Fortran.


1.Enabling OpenMP

To activate the OpenMP extensions for C/C++ and Fortran, the compile-time flag -fopenmp must be specified. This enables the OpenMP directive #pragma omp in C/C++ and !$omp directives in free form, c$omp*$omp and !$omp directives in fixed form, !$ conditional compilation sentinels in free form and c$*$ and !$ sentinels in fixed form, for Fortran. The flag also arranges for automatic linking of the OpenMP runtime library .
A complete description of all OpenMP directives accepted may be found in the OpenMP Application Program Interface manual, version 3.1.


 2.Runtime Library Routines

The routines are structured in following three parts:

(i)Control threads, processors and the parallel environment.

 Some of the functions are:-
 opm_get_active_level- This function returns the nesting level for the active parallel blocks, which      enclosing the calling call.
C/C++
 Prototype:  int omp_get_active_level(void);

 omp_get_level- This function returns the nesting level for the parallel blocks, which enclose the      calling call. 
C/C++
 Prototype:  int omp_get_level(void); 

omp_get_max_threads- Return the maximum number of threads used for the current parallel region that does not use the clause num_threads.
C/C++
Prototype:int omp_get_max_threads(void);

omp_get_thread_num-Returns a unique thread identification number within the current team. In a sequential parts of the program, omp_get_thread_num always returns 0. In parallel regions the return value varies from 0 to omp_get_num_threads-1 inclusive. The return value of the master thread of a team is always 0. 
C/C++
Prototype:int omp_get_thread_num(void);

omp_in_parallel-This function returns true if currently running in parallel, false otherwise. Here, true and false represent their language-specific counterparts. 
C/C++
Prototype:int omp_in_parallel(void); 






(ii)Initialize, set, test, unset and destroy simple and nested locks.

Some of the functions are:-
omp_init_lock-Initialize a simple lock. After initialization, the lock is in an unlocked state. 
C/C++
 Prototypevoid omp_init_lock(omp_lock_t *lock);

omp_set_lock-Before setting a simple lock, the lock variable must be initialized by omp_init_lock. The calling thread is blocked until the lock is available. If the lock is already held by the current thread, a deadlock occurs. 
C/C++
 Prototypevoid omp_set_lock(omp_lock_t *lock);

omp_test_lock-Before setting a simple lock, the lock variable must be initialized by omp_init_lock. Contrary to omp_set_lockomp_test_lock does not block if the lock is not available. This function returns true upon success, false otherwise. Here, true and false represent their language-specific counterparts.
C/C++ 
 Prototypeint omp_test_lock(omp_lock_t *lock);

omp_unset_lock-A simple lock about to be unset must have been locked by omp_set_lock or omp_test_lock before. In addition, the lock must be held by the thread calling omp_unset_lock. Then, the lock becomes unlocked. If one or more threads attempted to set the lock before, one of them is chosen to, again, set the lock to itself.
C/C++
 Prototype:void omp_unset_lock(omp_lock_t *lock);

omp_destroy_lock-Destroy a simple lock. In order to be destroyed, a simple lock must be in the unlocked state. 
C/C++
 Prototypevoid omp_destroy_lock(omp_lock_t *lock); 


(iii)Portable, thread-based, wall clock timer.

Functions are:-
 omp_get_wtick-Gets the timer precision, i.e., the number of seconds between two successive clock ticks. 
C/C++
 Prototype:double omp_get_wtick(void);
 omp_get_wtime-Elapsed wall clock time in seconds. The time is measured per thread, no guarantee can be made that two distinct threads measure the same time. Time is measured from some "time in the past", which is an arbitrary time guaranteed not to change during the execution of the program.
C/C++
 Prototype:double omp_get_wtime(void);


3.Environment Variables

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.
OMP_DYNAMIC – Enable or disable the dynamic adjustment of the number of threads within a team. The value of this environment variable shall be TRUE or FALSE. If undefined, dynamic adjustment is disabled by default. 
C/C++:
Prototype:void omp_set_dynamic(int set);


 OMP_MAX_ACTIVE_LEVELS – Specifies the initial value for the maximum number of nested parallel regions. The value of this variable shall be a positive integer. If undefined, the number of active levels is unlimited. 
C/C++
Prototype: void omp_set_max_active_levels(int max_levels);

OMP_NESTED –Enable or disable nested parallel regions, i.e., whether team members are allowed to create new teams. The value of this environment variable shall be TRUE or FALSE. If undefined, nested parallel regions are disabled by default. 
C/C++:
Prototype:void omp_set_nested(int set); 

omp_set_num_threads-Specifies the number of threads used by default in subsequent parallel sections, if those do not specify a num_threads clause. The argument of omp_set_num_threads shall be a positive integer.





C/C++:
Prototype:void omp_set_num_threads(int n); 





Sunday, March 31, 2013

Linked List Implementation Of Stack


The linked list is generally pictured as a list of linear nodes that are tethered together somehow. In C/++, you generally have a structure which contains data and a pointer to the next structure container which contains data and a pointer to the next structure container... and so on. The linked list's main advantage is that it doesn't contain data in a contiguous way and rather, a flexible way. This allows for fast insertion and better overall iteration. The linked list is often even used as the basis for other containers (such as queue or stack containers).

Program:

#include<iostream>
#include<windows.h>

using namespace std;
struct Stack{
              int data;
              Stack *next;
           }*top,*temp;


void push(int);
int pop();

int main()
{
    system("color ec");
    int n;
    top=NULL;
    cout<<"\n\t\t\tPusing data...";
    Sleep(2500);
    push(10);
    push(20);
    push(30);
    push(40);
    push(50);
    cout<<"\n\n\n\t\t\tPoping data...";
    Sleep(2000);
    cout<<"\n\n";
    cout<<"\t\t\t\t"<<pop()<<endl;
    cout<<"\t\t\t\t"<<pop()<<endl;
    cout<<"\t\t\t\t"<<pop()<<endl;
    cout<<"\t\t\t\t"<<pop()<<endl;
    cout<<"\t\t\t\t"<<pop()<<endl;
    cout<<"\n\n";
    return 0;
}

void push(int a)
{
    temp=new Stack;
    if(temp==NULL)
    cout<<"Memory full!!!!!!";
    else
    {
        temp->data=a;
        temp->next=NULL;
        if(top==NULL)
        top=temp;
        else
        {
            temp->next=top;
            top=temp;
        }
    }
}


int pop()
{
    int b;
    if(top==NULL)
    return -1;
    else
    {
        temp=top;
        b=top->data;
        top=top->next;
        delete temp;
        return b;
    }
}



Output:



 

CPU Date And Time

In this post I'll tell how to store your cpu date and time in a text file . For this purpose we will be using "time.h" header file ... CPU time and date is nothing but the date you see when you log into into your OS....
This is helpful to keep log of activities on your C++ applications...

Program:

#include "iostream"
#include "iomanip.h"
#include "fstream.h"
using namespace std;

int main()
{
    system("color ce");
    cout<<setw(54)<<"*****-------------------*****\n";
    cout<<setw(54)<<"*****C.P.U DATE AND TIME*****\n";
    cout<<setw(54)<<"*****-------------------*****\n";
    cout<<"\n\n\n\t\t\t";
    ofstream fout;
    fout.open("cputime.txt");
    time_t t1;
    time(&t1);
    cout<<ctime(&t1);
    fout<<ctime(&t1);
    cout<<"\n\n\n\t\t\t\t\t";
    fout.close();
    return 0;
}


Output:



Character Pyramid

Character pyramid algoritm is generally based on its the ascii values of the characters.
The American Standard Code for Information Interchange (ASCII) is a  character-encoding  scheme originally based on the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text. Most modern character-encoding schemes are based on ASCII, though they support many additional characters.

Here is the complete ascii chart....

Program:
 
#include<iostream>
using namespace std;
int main()
{
    system("color 1e");
    int n,i,j;
    char ch;
    cout<<"\n\n\n";
    for(i=65;i<=69;++i)
    {
        cout<<"\t\t\t\t";
        for(j=65;j<=i;++j)
        {
            ch=j;
            cout<<ch;
        }
        cout<<endl;
    }
    cout<<"\n\n\n";
    return 0;
}
 
 
Output:
 
 

 

Array Implementation Of Stack

Stacks data structures refer to the lists stored and accessed in a special way, where LIFO (Last In First Out)
technique is followed. In stacks ,insertion and deletions (PUSH and POP) take place only at one end , called top.
Stack is similar to a stack of plates. The plates are inserted or removed only from the top of the stack.


Program:

#include<iostream>
#include<windows.h>

using namespace std;
#define MAX 100
using namespace std;
struct Stack{
               int arr[MAX];
               int top;
               public:
               Stack()
               {
                   top=-1;
               }
               void push(int n)
               {
                   if(top==MAX-1)
                   cout<<"Stack full!!!!!!";
                   else
                   arr[++top]=n;
               }
               int pop()
               {
                   if(top==-1)
                   return -1;
                   else
                   return arr[top--];
               }
               void display()
               {
                   for(int i=0;i<top;++i)
                   cout<<arr[i]<<"  ";
               }
}a[5];


int main()
{
    system("color e0");
    for(int i=0;i<=10;++i)
    {
        a[i].push(i);
    }
    cout<<"\n\n\t\tPushing array elements...\n\n";
    Sleep(3500);
    cout<<"\n\n\t\tPoping array elements...\n\n";
    Sleep(3000);
    for(int i=0;i<=10;++i)
    {
        cout<<"\t\t\t"<<a[i].pop()<<endl;
    }

    return 0;
}



Output:



 

Array Implementation Of Queue

The queue works on the principle of first in first out...FIFO.. , where insertion takes place at the "rear" end of the queues and deletions take place at the "front" end of the queues.
Queue is much and same as a line of people waiting for their turn to vote.First person will be the first to vote and a new person can join the queue , at the rear end of it....



Program:

#include<iostream>
#define MAX 100
using namespace std;

class Queue{
               int arr[MAX];
               int rear,front;
               public:
               Queue()
               {
                   front=rear=-1;
               }
               void insert(int num)
               {
                   if(rear==MAX-1)
                   cout<<"Queue full!!!!!!!";
                   if(rear==-1&&front==-1)
                   {
                       front=0;
                       arr[++rear]=num;
                   }
                   else
                   arr[++rear]=num;
               }
               int remove()
               {
                   int val;
                   if(front==-1)
                   val=-1;
                   else
                   val=arr[front++];
                   if(rear==front)
                   rear=-1;
                   return val;
               }
               void display()
               {
                   for(int i=front;i<=rear;++i)
                   cout<<arr[i]<<"   ";
               }
}a;

int main()
{
    system("cls");
    system("color ec");
    cout<<"\n\n\n\t\t\t";
    a.insert(10);
    a.insert(20);
    a.insert(30);
    a.insert(40);
    a.insert(50);

    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<a.remove()<<"  ";
    cout<<"\n\n\n\n\n\n";
    return 0;
}


Output:


 

Wednesday, March 20, 2013

Row Swap Of A Matrix

In row swap operation the rows of the matrix is swaped.............
i.e., the rows are interchanged................




Program:

#include<iostream>
#include<iomanip>
#include<windows.h>


using namespace std;




int main()
{
                            system("cls");
                            system("color a0");
                            cout<<setw(54)<<"*****---------------------*****\n";
                            cout<<setw(54)<<"*****SWAP ROWS OF A MATRIX*****\n";
                            cout<<setw(54)<<"*****---------------------*****\n";
                            cout<<"\n\n";
                            int a[100][100],i,j,m,n;
                            cout<<"\tEnter number of rows : ";
                            cin>>m;
                            cout<<"\tEnter number of columns : ";
                            cin>>n;
                            cout<<"\n\n\tEnter the matrix : \n\n";
                            for(i=0;i<m;++i)
                            {
                                for(j=0;j<n;++j)
                                {
                                    cout<<"\tEnter ["<<i<<"]["<<j<<"] : ";
                                    cin>>a[i][j];
                                }
                            }
                            cout<<"\n\n\t\t\tProcessing......";
                            Sleep(2200);
                            system("cls");
                            system("color f0");
                            cout<<"\n\n\n\t\t\tThe matrix is : \n\n";
                            for(i=0;i<m;++i)
                            {
                                cout<<"\t\t\t";
                                for(j=0;j<n;++j)
                                {
                                    cout<<a[i][j]<<" ";
                                }
                                cout<<endl;
                            }
                            for(i=0;i<m/2;++i)
                            {
                                int p=m-1;
                                for(j=0;j<n;++j)
                                {
                                    int temp=a[i][j];
                                    a[i][j]=a[p][j];
                                    a[p][j]=temp;
                                }
                                --p;
                            }
                            cout<<"\n\n\t\t\tThe swaped matrix is : \n\n";
                            for(i=0;i<m;++i)
                            {
                               cout<<"\t\t\t";
                               for(j=0;j<n;++j)
                               {
                                   cout<<a[i][j]<<" ";
                               }
                               cout<<endl;
                            }
                            return 0;
}



Output: