Friday, November 13, 2015

Vedic Mathematics method to find square of 2-digit number is used in a distributed programming. Use shared memory and distributed (multi-CPU) programming to complete the task.

PROGRAM:

// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>

// includes, project
#include <helper_functions.h>
#include <helper_cuda.h>
#include <iostream>
#include <cuda.h>
using namespace std;

__global__ void mul(int *d, int n)
{
  __shared__ int s[4];
  int t = threadIdx.x;

  s[t] = d[2*t]*d[2*t+1];
  __syncthreads();
  d[2*t]=s[t];
}

__global__ void add(int *d, int n)
{
  __shared__ int s[4];
  int t = threadIdx.x;


  s[t]= d[2*t]*d[2*t+1];
    __syncthreads();
    d[8]=s[0]+s[1]+s[2]+s[3];
}


int main(void)
{
  const int n = 8;
  int ori[n], ans[n], d[n],ans1;

  int no,x,y;
  cout <<"Enter your number" << endl;
  cin>>no;

  x=(no/10)*10;
  y=no%10;
  cout<<"x:"<<x<<" y:"<<y<<endl;
  ori[0]=ori[1]=ori[2]=ori[4]=x;
  ori[3]=ori[5]=ori[6]=ori[7]=y;


  int *d_d,*a_a;
  cudaMalloc(&d_d, n * sizeof(int));

  // run version with static shared memory
  cudaMemcpy(d_d, ori, n*sizeof(int), cudaMemcpyHostToDevice);
  mul<<<1,n/2>>>(d_d, n/2);
  cudaMemcpy(d, d_d, n*sizeof(int), cudaMemcpyDeviceToHost);
  cudaFree(d_d);
  ans1=d[0]+d[2]+d[4]+d[6];

  cout<<"The Square is:"<<ans1;

return 0;
}

#OUTPUT:

Enter your number
23
x:20 y:3
The Square is:529

No comments:

Post a Comment

Perform a suitable assignment using Xen Hypervisor or equivalent open source to configure it. Give necessary GUI.

 To install kvm on Fedora:  yum install kvm  yum install virt-manager libvirt libvirt-python python-virtinst  su -c "yum install @v...