CP4292-Multicore Lab
CP4292-Multicore Lab
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and
engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and research methods
including design of experiments, analysis and interpretation of data, and synthesis of the information to
provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal,
health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional
engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions in
societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in diverse
teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the engineering and
management principles and apply these to one’s own work, as a member and leader in a team, to manage
projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
PROGRAM SPECIFIC OBJECTIVES (PSOs)
To analyze, design and develop computing solutions by applying foundational concepts of
Computer Science and Engineering.
To apply software engineering principles and practices for developing quality software for
scientific and business applications.
To adapt to emerging Information and Communication Technologies (ICT) to innovate
ideas and solutions to existing/novel problems.
Course Outcomes
CO1: Describe multicore architectures and identify their characteristics and challenges.
CO2: Identify the issues in programming Parallel Processors.
CO3: : Write programs using OpenMP and MPI.
CO4: Design parallel programming solutions to common problems
CO5: Compare and contrast programming for serial processors and programming for parallel processors
MULTICORE ARCHITECTURE AND PROGRAMMING
AIM:
PROGRAM:
#include<stdio.h>
#include <omp.h> int
main(void)
{
printf("Before: total thread number is %d\n", omp_get_num_threads());
#pragmaomp parallel
{
printf("Thread id is %d\n",omp_get_thread_num());
}
printf("After: total thread number is %d\n", omp_get_num_threads());return 0;
}
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <omp.h>
int main() {
float A[2][2] = {{1,2},{3,4}};
float b[] = {8,10};
float c[2];
int i,j;
return 0;
}
OUTPUT:
ALGORITHM:
Step 1: Start
Step 2: Creation of a program for computing the sum of all the elementsan array.
Step 3: Input the array elements.Step
4: Process of addition.
Step 5: Print the resultant sum.Step
6: Stop.
PROGRAM:
#include<omp.h> #include
<bits/stdc++.h>
usingnamespace std;
intmain(){
vector<int>arr{3,1,2,5,4,0};
queue<int> data;
intarr_sum=accumulate(arr.begin(),arr.end(),0);
intarr_size=arr.size();
intnew_data_size, x, y;
for(inti=0;i<arr_size;i++){
data.push(arr[i]);
}
omp_set_num_threads(ceil(arr_size/2));
#pragmaomp parallel
{
#pragmaomp critical
{
new_data_size=data.size();
for(int j=1; j<new_data_size; j=j*2){x
=data.front();
data.pop();
y =data.front();
data.pop();
data.push(x+y);
}
}
OUTPUT:
Array of elements: 1 5 7 9 11
Sum: 33
Result:
Thus the program has been executed successfully.
4. Write a simple Program demonstrating Message-Passing logic usingOpenMP.
AIM:
To write a simple program demonstrating Message-Passing logic usingOpenMP.
ALGORITHM:
Step 1: Start
Step 2: Creation of simple program demonstratingmessage-
passing logic.
Step 3: The message creation for transformation across web.Step 4:
Input the message.
Step 5: Process and print the result.Step 6:
Stop
PROGRAM:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
//Define minimum function that will be used later on to calcualte minimumvalues between
two numbers
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))#endif
Output:
Matrix of all pair shortest
path.0 3 4 5 6 7 7
3021344
4201323
5110233
6332021
7423201
7433110
Result:
PROGRAM:
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int i = 0;
// If d is less than or
// equal to 1if
(d <= 1) {
// Increment pCircle by 1
pCircle++;
}
// Increment pSquare by 1
pSquare++;
}
}
// Stores the estimated value of PI
double pi = 4.0 * ((double)pCircle / (double)(pSquare));
// Driver Codeint
main()
{
// Input
int N = 100000;
int K = 8;
// Function call
monteCarlo(N, K);
OUTPUT:
Final Estimation of Pi =3.1320757
Result:
if(rank == 0) {
buf = 777;
MPI_Bcast(&buf, 1, MPI_INT, 0, MPI_COMM_WORLD);
}
else {
MPI_Recv(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
printf("rank %d receiving received %d\n", rank, buf);
}
MPI_Finalize();
return0;
}
OUTPUT:
AIM:
To write a program to demonstrate MPI-scatter-gather-and-all gather.
ALGORITHM:
Step 1: Start
Step 2: Get an array of random numbers as input. Step 3:
Compute the average of array of numbers. Step 4: Process
and print the result.
Step 5: Stop
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
#include <assert.h>
// Creates an array of random numbers. Each number has a value from 0 - 1float
*create_rand_nums(int num_elements) {
float *rand_nums = (float *)malloc(sizeof(float) * num_elements);
assert(rand_nums != NULL);
int i;
for (i = 0; i<num_elements; i++) { rand_nums[i] =
(rand() / (float)RAND_MAX);
}
return rand_nums;
}
MPI_Init(NULL, NULL);
// Scatter the random numbers from the root process to all processes in
// the MPI world
MPI_Scatter(rand_nums, num_elements_per_proc, MPI_FLOAT,
sub_rand_nums,
num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD);
// Gather all partial averages down to all the processes float *sub_avgs
= (float *)malloc(sizeof(float) * world_size);assert(sub_avgs != NULL);
MPI_Allgather(&sub_avg, 1, MPI_FLOAT, sub_avgs, 1, MPI_FLOAT,
MPI_COMM_WORLD);
// Clean up
if (world_rank == 0) {
free(rand_nums);
}
free(sub_avgs);
free(sub_rand_nums);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
OUTPUT:
AIM:
PROGRAM:
if (rank == 0)
{
array = malloc (10 * sizeof(int)); // Array of 10 elementsif(!array)
// error checking
{
MPI_Abort (MPI_COMM_WORLD,1);
}
MPI_Send(&array,10,MPI_INT,1,tag,MPI_COMM_WORLD);
}
if (rank == 1)
{
MPI_Recv (&array,10,MPI_INT,0,tag,MPI_COMM_WORLD,&status);
// more code here
}
MPI_Finalize();
OUTPUT:
>>> ./run.py send_recv
mpirun –n/2 ./send_recv
Process 1 received number -1 from process 0
Result:
Thus the program has been executed successfully.
10. Write a Program to demonstrate by performing-parallel-rank-with-MPI in C
AIM:
// Seed the random number generator to get different results each time
srand(time(NULL) * world_rank);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
}
OUTPUT:
>>> ./run.py random_rank Mpirun –n
4 ./random_rank 100
Rank for 0.242578 on process 0 – 0
Rank for 0.894732 on process 1 – 3
Rank for 0.789463 on process 2 – 2
Rank for 0.684195 on process 3 – 1
Result:
Thus the program has been executed successfully.