Saturday, June 7, 2014

Extracting the content of the webpage using BeautifulSoup and Mechanize in python

There arises several condition to extract the content of the page and display in our application. In such case we can use the BeautifulSoup and Mechanize python package.

The python code to extract the price of gold and silver form the website http://www.fenegosida.org/ is shown here


''''Reap gold price from http://www.fenegosida.org/

<h1> tag contains the prices. These h1 resides in following IDS + "-content"

Sample data
{'tejabi-1tola': u'53450', 'hallmark-1tola': u'53700', 'hallmark-10gms': u'46040', 'silver-1tola': u'860', 'tejabi-10gms': u'45825', 'silver-10gms': u'737.50'}
"""

URL = "http://www.fenegosida.org/"
IDS=["hallmark","tejabi","silver"]

import sys
from BeautifulSoup import BeautifulSoup
from mechanize import Browser

if len(sys.argv) > 1 and sys.argv[1] == "-sample":
    print "{'tejabi-1tola_new': u'53450', 'hallmark-1tola': u'53700', 'hallmark-10gms': u'46040', 'silver-1tola': u'860', 'tejabi-10gms': u'45825', 'silver-10gms': u'737.50'}"
    sys.exit(0)

br = Browser()
br.addheaders = [
    ('user-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3',),
    ('accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',),
    ]
page = br.open(URL)
soup = BeautifulSoup(page.read())
price = {}

for id in IDS:
    hallmark = soup.findAll('div',{'id':"{0}-content".format(id)})
    
    a = hallmark[0].h1.text
    b = hallmark[1].h1.text

    if float(a) > float(b):
        a, b = b, a

    price['{0}-10gms'.format(id)] = a
    price['{0}-1tola'.format(id)] = b

print price

sys.exit(0)

first cuda program for beginners

before starting the code directly, we need to understand the basic terms and theoretical aspects of cuda programming. First thing is we need a device popularly called GPU for cuda programming and GPU must be Nvidia gpu not any others like ATI or anyone else except Nvidia’s GPU.
Then we need to have basic concept of two terms
1)      Host
2)      Device
These two terms are frequently used in the documentation and in the resources found in the internet.
1)      Host refers to CPU where sequential execution of programs occurs.
2)      Device refers to Nvidia GPU card.



Basic concepts of Gpu Programming
Gpu programming is parallel programming where we create large number of threads and each threads executes the program concurrently at the same time thus parallism occurs.And the block of code which is executed by each thread is called kernel function.

Now let us learn how the  cuda code is written.
1)      first variables for the host and device are initialized.
2)      Memory is allocated in host side.
3)      Values are assigned in the memory at host side.
4)      Memory is allocated in the devices.
5)      Values in the host side are copied to device memory as gpu can only access the gpu memory.
6)      Then the parallel operations are done in the gpu using kernel function.
7)       Then the final data are again copied back to host memory as in order to display the data we need to display it through cpu memory or host memory.
8)       Finally free host aswell as device memory.
Basic cuda program
Firstprogram.cu
#include <stdio.h>
#include <cuda.h>

// Kernel that executes on the CUDA device
__global__ void sum_array(float *a, int N)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx<N) a[idx] = a[idx] + a[idx];
}

// main routine that executes on the host
int main(void)
{
  float *array_h, *array_d;  // Pointer to host & device arrays
  const int N = 10;  // Number of elements in arrays
  size_t size = N * sizeof(float);
  array_h = (float *)malloc(size);        // Allocate array on host
  cudaMalloc((void **) &array_d, size);   // Allocate array on device
  // Initialize host array and copy it to CUDA device
  for (int i=0; i<N; i++) array_h[i] = (float)i;
  cudaMemcpy(array_d, array_h, size, cudaMemcpyHostToDevice);
  



  
  sum_array <<< 3,4 >>> (array_d, N);//kernel call
  // save the result from device in the device memory and copy it in host array
  cudaMemcpy(array_h, array_d, sizeof(float)*N, cudaMemcpyDeviceToHost);
  // Print results
  for (int i=0; i<N; i++) printf("%d %f\n", i, array_h[i]);
  // Cleanup
  free(array_h); cudaFree(array_d);
}


Here __global__ void sum_array(float *a, int N) is a kernel function. This kernel function runs simultaneously at the same time for all the threads which causes parralism. During the call of kernel function <<,>> is used which indicates the call to kernel function and << ,>> has << number of block,number of threads >>.


Monday, June 2, 2014

Sourcecode of Newton's forward and backward interpolation using C

newton's forward and backward interpolation

#include<stdio.h>
#include<math.h>
int main()

{

    float x[10],y[15][15];
    int n,i,j;
//no. of items
    printf("Enter n : ");
    scanf("%d",&n);
    printf("X\tY\n");
    printf("enter the value of x then y\n");
    for(i = 0;i<n;i++){
            scanf("%f %f",&x[i],&y[i][0]);

    }


    printf("the entered value are\n");

     printf("x\t\ty\n");
     for(i = 0;i<n;i++){
            printf("\n");
            printf("%f\t%f",x[i],y[i][0]);

    }


    //forward difference table

    for(j=1;j<n;j++)
        for(i=0;i<(n-j);i++)
            y[i][j] = y[i+1][j-1] - y[i][j-1];
    printf("\n***********Forward Difference Table ***********\n");
//here is the  Forward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<(n-i);j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
    // here is thebackward difference table
    for(j=1;j<n;j++)
//for j = 0 initially input is taken so we start from j=1
        for(i=n-1;i>(j-1);i--)
            y[i][j] = y[i][j-1] - y[i-1][j-1];
    printf("\n***********Backward Difference Table ***********\n");
//here is the Backward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<=i;j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
return 0;

}