All HPC Programs
All HPC Programs
#include <iostream>
#include <vector>
#include <queue>
#include <omp.h>
public:
Graph(int V) : V(V), adj(V) {}
visited[startVertex] = true;
q.push(startVertex);
while (!q.empty()) {
int v = q.front();
q.pop();
cout << v << " ";
Output:
Practical 3
#include <iostream>
//#include <vector>
#include <omp.h>
#include <climits>
using namespace std;
void min_reduction(int arr[], int n) {
int min_value = INT_MAX;
#pragma omp parallel for reduction(min: min_value)
for (int i = 0; i < n; i++) {
if (arr[i] < min_value) {
min_value = arr[i];
}
}
cout << "Minimum value: " << min_value << endl;
}
int main() {
int *arr,n;
cout<<"\n enter total no of elements=>";
cin>>n;
arr=new int[n];
cout<<"\n enter elements=>";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
min_reduction(arr, n);
max_reduction(arr, n);
sum_reduction(arr, n);
average_reduction(arr, n);
}
Output
Practical 2
Bubble Sort
#include<iostream>
#include<stdlib.h>
#include<omp.h>
using namespace std;
int test;
test=a;
a=b;
b=test;
int main()
{
int *a,n;
cout<<"\n enter total no of elements=>";
cin>>n;
a=new int[n];
cout<<"\n enter elements=>";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
bubble(a,n);
return 0;
}
Output
Merge Sort
#include<iostream>
#include<stdlib.h>
#include<omp.h>
using namespace std;
merge(a,i,mid,mid+1,j);
}
while(i<=j1)
{
temp[k++]=a[i++];
}
while(j<=j2)
{
temp[k++]=a[j++];
}
for(i=i1,j=0;i<=j2;i++,j++)
{
a[i]=temp[j];
}
}
int main()
{
int *a,n,i;
cout<<"\n enter total no of elements=>";
cin>>n;
a= new int[n];
Output
Epoch 1: Train accuracy = 0.9773, Test accuracy = 0.9745
Epoch 2: Train accuracy = 0.9859, Test accuracy = 0.9835
Epoch 3: Train accuracy = 0.9887, Test accuracy = 0.9857
Epoch 4: Train accuracy = 0.9905, Test accuracy = 0.9876
Epoch 5: Train accuracy = 0.9919, Test accuracy = 0.9880
Practical 4
Vector Addition
import numpy as np
import pycuda.driver as cuda
Output
Enter the first vector: 1 2 3 4 5
Enter the second vector: 6 7 8 9 10
[ 7 9 11 13 15]
Matrix Multiplication
#include<stdio.h>
#include<cuda.h>
#define row1 2 /* Number of rows of first matrix */
#define col1 3 /* Number of columns of first matrix */
#define row2 3 /* Number of rows of second matrix */
#define col2 2 /* Number of columns of second matrix */
n[col2*y+x]=0;
for(k=0;k<col1;k++)
{
n[col2*y+x]=n[col2*y+x]+l[col1*y+k]*m[col2*k+x];
}
}
int main()
{
int a[row1][col1];
int b[row2][col2];
int c[row1][col2];
int *d,*e,*f;
int i,j;
cudaMalloc((void **)&d,row1*col1*sizeof(int));
cudaMalloc((void **)&e,row2*col2*sizeof(int));
cudaMalloc((void **)&f,row1*col2*sizeof(int));
cudaMemcpy(d,a,row1*col1*sizeof(int),cudaMemcpyHostToDevice);
cudaMemcpy(e,b,row2*col2*sizeof(int),cudaMemcpyHostToDevice);
dim3 grid(col2,row1);
/* Here we are defining two dimensional Grid(collection of blocks) structure. Syntax is
dim3 grid(no. of columns,no. of rows) */
matproduct<<<grid,1>>>(d,e,f);
cudaMemcpy(c,f,row1*col2*sizeof(int),cudaMemcpyDeviceToHost);
printf("\nProduct of two matrices:\n ");
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
cudaFree(d);
cudaFree(e);
cudaFree(f);
return 0;
}
Output
Enter elements of first matrix of size 2*3
123456
Database Connectivity:
import psycopg2
def run():
try:
# establishing the connection
conn = psycopg2.connect(database="root", user='root',
password='root',
host='127.0.0.1',
port='5432')
Output:
Source Code:
from multiprocessing.connection import Connection
import time,os
from multiprocessing import Pool, freeze_support
import psycopg2
def run():
try:
conn = psycopg2.connect(database="root", user='root',
password='root',
host='127.0.0.1',
port='5432')
cursor = conn.cursor()
cursor.execute('''SELECT * FROM root''')
records = cursor.fetchall()
return records
except:
print("Connection not established to the database")
return -1
if __name__=="__main__":
freeze_support()
print("Enter the number of times to run the above query")
n=int(input())
results = []
for _ in range(n):
res=pool.apply_async(run)
results.append(res)
res = [result.get() for result in results]
print(res)
pool.close()
pool.join()
Output: