ANNA UNIVERSITY
JAYA ENGINEERING COLLEGE
THIRUNINRAVUR-602024
(An ISO 9001:2000 Certified Institution)
Email :info@[Link] Website:[Link]
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING
PRACTICAL RECORD
CP4292: MULTICORE ARCHITECTURE AND
PROGRAMMING
NAME :
REGNO. :
YEAR :
SEM :
THIRUNINRAVUR-602024
(An ISO9001:2000 Certified Institution)
Email :info@[Link] Website:[Link]
BONAFIDE CERTIFICATE
This is to certify that this is a bonafide record of work done by
Mr./Ms. [Link].
Of M.E / Computer Science Engineering in CP4292 - MULTICORE ARCHITECTURE
AND PROGRAMMING in the II semester during March 2023 to June 2023
Head of the Department Staff-In-Charge
Submitted For Practical Examination Held On
Internal Examiner External Examiner
INDEX
SNO PARTICULARS PAGENO
1 Vision and Mission of Institute
2 Vision and Mission of Department
3 Program Educational Outcomes
4 Program Specific Outcomes
5 Program Outcomes
6 Course Out Comes
LISTOFEXPERIMENTS
PAGE POs
EX NAME OF
DATE COs &PS MARK SIGN
NO EXPERIMENT NO
Os
Write a simple Program to
1 demonstrate an Open MP Fork- 1
CO1 PO1
Join Parallelism
Create a program that computes a
simple matrix-vector
2 multiplication b=Ax, either in 3
C/C++. Use Open MP directives
CO2 PO2
to make it run in parallel.
Create a program that computes
the sum of all the elements in an
array A (C/C++) or a program that
3. 5
finds the largest number in an
array A. Use Open MP directives
CO2 PO2
to make it run in parallel.
Reading Data From Text Files,
Excel and the web and exploring
4 various commands for doing 8
PO3
descriptive analytics on the iris
CO1
dataset
Implement the All-Pairs Shortest-
Path Problem (Floyd's Algorithm)
5 Using Open MP 9
CO3 PO4
To find implement a program
Parallel Random Number
6 12
Generators using Monte Carlo CO4
PO5
Methods in Open MP
Write a Program to demonstrate
MPI-broadcast-and-collective-
7 15
communication in C CO4 PO6
Write a Program to demonstrate
MPI-broadcast-and-collective-
8 16
communication in C CO5 PO6
Write a Program to demonstrate
9 17
MPI-send-and-receive in C. CO5 PO7
Write a Program to demonstrate
10 20
MPI-send-and-receive in C. C05 PO7
VISION ANDMISSION OF THE INSTITUTE
Vision of the Institute
To achieve excellence in technical education through innovative teaching and multi disciplinary
research with professional is to serve the global society.
Mission of the Institute
Jaya Engineering College(JEC) will Endeavor
M1-To provide state of art teaching and learning for Engineering and Technology, Research and
Management studies.
M2-To provide quality education, self discipline and ethical values.
M3-To associate with R&D and industries to have connectivity with the society.
M4 –To impart knowledge to become empowered professionals in the field of Engineering and
Management
1. VISION AND MISSION OF THE DEPARTMENT
Vision of the department
To achieve Excellence in Computer Science and Engineering by providing quality technical
education to cater the need of industry and society through the search and development.
Mission of the department
The Computer Science and Engineering Department is committed to:
M1:Provide strong fundamentals and technical skills in Computer Science Engineering through
Effective teaching and learning methods.
M2:Impart high quality experiential learning to get expertise in modern software tools and to
procure the real-time requirements of the industry.
M3:Inculcate problem solving and team building skills and promote life long learning with a sense
of societal and ethical responsibilities.
2. PROGRAM EDUCATIONAL OBJECTIVES
PEO1: Apply their technical competence in computer science to solve real world problems, with
technical and people leadership.
PEO2: Conduct cutting edge research and develop solutions on problems of social relevance.
PEO3: Work in a business environment, exhibiting team skills, work ethics, adaptability and
lifelong learning.
3. PROGRAM SPECIFIC OUTCOMES
Engineering Graduates will be able to:
PSO1: Exhibit design and programming skills to build and automate business solutions
using cutting edge technologies
PSO2: Strong theoretical foundation leading to excellence and excitement towards research,
to provide elegant solutions to complex problems.
PSO3: Ability to work effectively with various engineering fields as a team to design, build
and develop system applications.
PROGRAM OUTCOMES
Engineering Graduates will be able to:
PO1 - Engineering Knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex
engineering problems.
PO2 - 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.
PO3-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.
PO4 - Conduct investigations of complex problems: Use research-based knowledge and research
methods including design no of experiments, analysis and interpretation of data and
synthesis of the information to provide valid conclusions.
PO5 - 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.
PO6 - 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.
PO7 - 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.
PO8-Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9- Individual and teamwork: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
PO10 - 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.
PO11 - Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply the set ones own work, as a member and
leader in a team, to manage projects and in multidisciplinary environments.
PO12- 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.
4. COURSE OUTCOME
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.
[Link].1 Write a simple Program to demonstrate an Open MP Fork-Join Parallelism.
Date:
1. Aim:
To write a simple Program to demonstrate an Open MP Fork-Join Parallelism
Program
*
Create a program that computes a simple matrix vector multiplication
b=Ax, either in fort ran or C/C++. Use Open MP directives to make
It runs in parallel.
This is the parallel version.
*/
#include <studio .h>
#include <oomph>
int main() {
float A[2][2] = {{1,2},{3,4}};
float b[] = {8,10};
float c[2];
int i,j;
// computes A*b
#pragma omp parallel for
for (i=0; i<2; i++) {
c[i]=0;
for (j=0;j<2;j++) {
c[i]=c[i]+A[i][j]*b[j];
}
}
// prints result
for (i=0; i<2; i++) {
printf("c[%i]=%f \n",i,c[i]);
}
return 0;
}
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 1
Output
C[0] = 28.000000
C[1] = 64.000000
RESULT
Thus the program successfully executed
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 2
[Link].2 Create a program that computes a simple matrix-vector multiplication
Date: b=Ax, either in C/C++. Use Open MP directives to make it run in
parallel.
Aim:
To create a program that computes a simple matrix- vector multiplication b=Ax, either in
C/C++.Use open MP directives to make it run in. parallel
Program
#include <iostream>
#include <vector>
#include <chrono>
#include <omp.h>
using namespace std;
const int N = 1000;
int main()
{
vector<vector<int>> A (N, vector<int>(N));
vector<vector<int>> B (N, vector<int>(N));
vector<vector<int>> C (N, vector<int>(N));
// Initialize matrices A and B with random values
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = rand() % 100;
B[i][j] = rand() % 100;
}
auto start_serial = chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int sum = 0;
for (int k = 0; k < N; k++) {
sum += A[i][k] * B[k][j];
}
C[i][j] = sum;
}
}
auto end_serial = chrono::high_resolution_clock::now();
auto duration_serial = chrono::duration_cast<chrono::milliseconds>(end_serial - start_serial);
// perform matrix multiplication in parallel using Open MP
auto start_parallel = chrono::high_resolution_clock::now();
#pragma omp parallel for
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 3
int sum = 0;
for (int k = 0; k < N; k++) {
sum += A[i][k] * B[k][j];
}
C[i][j] = sum;
}
}
Auto end_parallel = chrono::high_resolution_clock::now();
auto duration_parallel = chrono::duration_cast<chrono::milliseconds>(end_parallel - start_parallel);
// Display the time taken for each approach
cout << "Time taken for serial matrix multiplication: " << duration_serial.count() << " milliseconds" <<
endl;
cout << "Time taken for parallel matrix multiplication: " << duration_parallel.count() << " milliseconds"
<< endl;
return 0;
}
}
Output
Time taken for serial matrix
multiplication : 1979
milliseconds
Time taken for parallel matrix
multiplication: 544
milliseconds
Result:
Thus the program successfully executed.
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 4
Create a program that computes the sum of all the elements in an
[Link].3 array A (C/C++) or a program that finds the largest number in an
Date: array A. Use OpenMP directives to make it run in parallel.
Aim:
To Create a program that computes the sum of all the elements in an array A (C/C++) or a
program that finds the largest number in an array A. Use OpenMP directives to make it run in
parallel.
program
#include<stdio.h>
#include<omp.h>
/* Main Program */
main()
{
float *Array, *Check, serial_sum, sum, partialsum;
int array_size, i;
printf("Enter the size of the array\n");
scanf("%d", &array_size);
if (array_size <= 0) {
printf("Array Size Should Be Of Positive Value ");
exit(1);
}
/* Dynamic Memory Allocation */
Array = (float *) malloc(sizeof(float) * array_size);
Check = (float *) malloc(sizeof(float) * array_size);
/* Array Elements Initialization */
for (i = 0; i < array_size; i++) {
Array[i] = i * 5;
Check[i] = Array[i];
}
printf("The Array Elements Are \n");
for (i = 0; i < array_size; i++)
printf("Array[%d]=%f\n", i, Array[i]);
sum = 0.0;
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 5
partialsum = 0.0;
/* OpenMP Parallel For Directive And Critical Section */
#pragma omp parallel for shared(sum)
for (i = 0; i < array_size; i++) {
#pragma omp critical
sum = sum + Array[i];
serial_sum = 0.0;
/* Serail Calculation */
for (i = 0; i < array_size; i++)
serial_sum = serial_sum + Check[i];
if (serial_sum == sum)
printf("\nThe Serial And Parallel Sums Are Equal\n");
else {
printf("\nThe Serial And Parallel Sums Are UnEqual\n");
exit(1);
}
/* Freeing Memory */
free(Check);
free(Array);
printf("\nThe SumOfElements Of The Array Using OpenMP Directives Is %f\n", sum);
printf("\nThe SumOfElements Of The Array By Serial Calculation Is %f\n", serial_sum);
}
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 6
Output
Enter the size of the array 5
The Array Elements Are
Array[0]=0.000000
Array[1]=5.000000
Array[2]=10.000000
Array[3]=15.000000
Array[4]=20.000000
The Serial And Parallel Sums Are Equal
The SumOfElements Of The Array Using OpenMP Directives Is 50.000000
The SumOfElements Of The Array By Serial Calculation Is 50.000000
Result:
Thus the program successfully executed.
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 7
[Link].4 READING DATA FROM TEXT FILES, EXCEL AND THE WEB
Date: AND EXPLORING VARIOUS COMMANDS FOR DOING
DESCRIPTIVE ANALYTICS ON THEIR IS DATASET
Aim:
To find reading data from text files, excel and the web and exploring various commands for doing
descriptive analytics on the iris data set
Program:
// OpenMP program to print Hello World
// using C language
// OpenMP header
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
// Beginning of parallel region
#pragma omp parallel
{
printf("Hello World... from thread = %d\n",
omp_get_thread_num());
}
// Ending of parallel region
}
output
Hello World... from thread = 0
Hello World... from thread = 1
Hello World... from thread = 2
Hello World... from thread = 3
Hello World... from thread = 4
Hello World... from thread = 5
Hello World... from thread = 6
Hello World... from thread = 7
Result
Thus the program successfully executed.
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 8
[Link].5
Date: Implement the All-Pairs Shortest-Path Problem (Floyd's Algorithm)
Using OpenMP
Aim:
To write implement the All-Pairs Shortest-Path Problem (Floyd's Algorithm) Using OpenMP.
program
// C++ Program for Floyd Warshall Algorithm
#include <bits/stdc++.h>
using namespace std;
// Number of vertices in the graph
#define V 4
/* Define Infinite as a large enough
[Link] value will be used for
vertices not connected to each other */
#define INF 99999
// A function to print the solution matrix
void printSolution(int dist[][V]);
// Solves the all-pairs shortest path
// problem using Floyd Warshall algorithm
void floydWarshall(int dist[][V])
{
int i, j, k;
/* Add all vertices one by one to
the set of intermediate vertices.
---> Before start of an iteration,
we have shortest distances between all
pairs of vertices such that the
shortest distances consider only the
vertices in set {0, 1, 2, .. k-1} as
intermediate vertices.
----> After the end of an iteration,
vertex no. k is added to the set of
intermediate vertices and the set becomes {0, 1, 2, ..
k} */
for (k = 0; k < V; k++) {
// Pick all vertices as source one by one
for (i = 0; i < V; i++) {
// Pick all vertices as destination for the
// above picked source
for (j = 0; j < V; j++) {
// If vertex k is on the shortest path from
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 9
// i to j, then update the value of
// dist[i][j]
if (dist[i][j] > (dist[i][k] + dist[k][j])
&& (dist[k][j] != INF
&& dist[i][k] != INF))
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
// Print the shortest distance matrix
printSolution(dist);
}
/* A utility function to print solution */
void printSolution(int dist[][V])
{
cout << "The following matrix shows the shortest "
"distances"
" between every pair of vertices \n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (dist[i][j] == INF)
cout << "INF" << " ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}
// Driver's code
int main()
{
/* Let us create the following weighted graph
10
(0) ------ >(3)
| /|\
5| |
| |1
\|/ |
(1) ------ >(2)
3 */
int graph[V][V] = { { 0, 5, INF, 10 },
{ INF, 0, 3, INF },
{ INF, INF, 0, 1 },
{ INF, INF, INF, 0 } };
// Function call
floydWarshall(graph);
return 0;
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 10
}
OUTPUT:
The following matrix shows the shortest distances between every pair of vertices
0 5 8 9
INF 0 3 4
INF INF 0 1
INF INF INF 0
Time Complexity: O(V3)
Auxiliary Space: O(V2)
Result:
Thus the program successfully executed.
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 11
Implement a program Parallel Random Number Generators using
[Link].6
Monte Carlo Methods in OpenMP
Date:
Aim:
To find implement a program Parallel Random Number Generators using Monte Carlo Methods
in OpenMP
PROGRAM:
// C++ program for the above approach
#include <iostream>
using namespace std;
// Function to find estimated
// value of PI using Monte
// Carlo algorithm
void monteCarlo(int N, int K)
{
// Stores X and Y coordinates
// of a random point
double x, y;
// Stores distance of a random
// point from origin
double d;
// Stores number of points
// lying inside circle
int pCircle = 0;
// Stores number of points
// lying inside square
int pSquare = 0;
int i = 0;
// Parallel calculation of random
// points lying inside a circle
#pragma omp parallel firstprivate(x, y, d, i) reduction(+ : pCircle, pSquare) num_threads(K)
{
// Initializes random points
// with a seed
srand48((int)time(NULL));
for (i = 0; i < N; i++)
{
// Finds random X co-ordinate
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 12
x = (double)drand48();
// Finds random X co-ordinate
y = (double)drand48();
// Finds the square of distance
// of point (x, y) from origin
d = ((x * x) + (y * y));
// If d is less than or
// equal to 1
if (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));
// Prints the value in pi
cout << "Final Estimation of Pi = "<< pi;
}
// Driver Code
int main()
{
// Input
int N = 100000;
int K = 8;
// Function call
monteCarlo(N, K);
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 13
Output
Final Estimation of Pi = 3.14159
Result:
Thus the program successfully executed
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 14
[Link].7 Write a Program to demonstrate MPI-broadcast-and-collective-
Date: communication in C
Aim:
To Write a Program to demonstrate MPI-broadcast-and-collective-communication in C.
Program:
#include "mpi.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
int rank, nprocs;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&nprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Barrier(MPI_COMM_WORLD);
printf("Hello, world. I am %d of %d\n", rank, nprocs);fflush(stdout);
MPI_Finalize();
return 0;
}
Output
Standard Output
Compiling
Compilation is OK
Execution ...
Hello, world. I am 0 of 8
Hello, world. I am 1 of 8
Hello, world. I am 2 of 8
Hello, world. I am 3 of 8
Hello, world. I am 4 of 8
Hello, world. I am 5 of 8
Hello, world. I am 6 of 8
Hello, world. I am 7 of 8
Result:
Thus the program successfully executed.
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 15
[Link].8 Write a Program to demonstrate MPI-broadcast-and-collective-
Date: communication in C
Aim:
To find Write a Program to demonstrate MPI-broadcast-and-collective-communication in C
Program:
const int recvsize = 50;
int *sendbuf, recvbuf[recvsize];
int sendsize = nb_proc*recvsize;
sendbuf = new int[sendsize];
if (proc_id == 0)
Generate_data(sendbuf, sendsize);
MPI_Scatter(sendbuf, recvsize, MPI_INT, recvbuf, recvsize, MPI_INT, 0, MPI_COMM_WORLD);
for (i=0; i<nb_proc; i++)
Print_data(recvbuf, recvsize);
Example Using Gather
const int sendsize = 50;
int sendbuf[sendsize], *recvbuf;
int recvsize = nb_proc*sendsize;
if (proc_id == 0)
recvbuf = new int[recvsize];
for (i=0; i<nb_proc; i++)
Generate_data(sendbuf, sendsize);
MPI_Gather(sendbuf, sendsize, MPI_INT, recvbuf, sendsize, MPI_INT, 0, MPI_COMM_WORLD);
if (proc_id == 0)
Print_data(recvbuf, recvsize);
Output
Process 0 received: 0 1 2 3 ... 48 49
Process 1 received: 50 51 52 53 ... 98 99
Process 2 received: 100 101 102 103 ... 148 149
Process 3 received: 150 151 152 153 ... 198 199
Result:
Thus the program successfully executed
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 16
[Link].9
Date: Write a Program to demonstrate MPI-send-and-receive in C.
Aim:
To find Write a Program to demonstrate MPI-send-and-receive in C.
program:
const int recvsize = 50;
#include <stdio.h>
#include <mpi.h>
#define max_rows 100000
#define send_data_tag 2001
#define return_data_tag 2002
int array[max_rows];
int array2[max_rows];
main(int argc, char **argv)
{
long int sum, partial_sum;
MPI_Status status;
int my_id, root_process, ierr, i, num_rows, num_procs,
an_id, num_rows_to_receive, avg_rows_per_process,
sender, num_rows_received, start_row, end_row, num_rows_to_send;
/* Now replicte this process to create parallel processes.
* From this point on, every process executes a seperate copy
* of this program */
ierr = MPI_Init(&argc, &argv);
root_process = 0;
/* find out MY process ID, and how many processes were started. */
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &my_id);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &num_procs);
if(my_id == root_process) {
/* I must be the root process, so I will query the user
* to determine how many numbers to sum. */
printf("please enter the number of numbers to sum: ");
scanf("%i", &num_rows);
if(num_rows > max_rows) {
printf("Too many numbers.\n");
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 17
exit(1);
}
avg_rows_per_process = num_rows / num_procs;
/* initialize an array */
for(i = 0; i < num_rows; i++) {
array[i] = i + 1;
}
/* distribute a portion of the bector to each child process */
for(an_id = 1; an_id < num_procs; an_id++) {
start_row = an_id*avg_rows_per_process + 1;
end_row = (an_id + 1)*avg_rows_per_process;
if((num_rows - end_row) < avg_rows_per_process)
end_row = num_rows - 1;
num_rows_to_send = end_row - start_row + 1;
ierr = MPI_Send( &num_rows_to_send, 1 , MPI_INT,
an_id, send_data_tag, MPI_COMM_WORLD);
ierr = MPI_Send( &array[start_row], num_rows_to_send, MPI_INT,
an_id, send_data_tag, MPI_COMM_WORLD);
}
/* and calculate the sum of the values in the segment assigned
* to the root process */
sum = 0;
for(i = 0; i < avg_rows_per_process + 1; i++) {
sum += array[i];
}
printf("sum %i calculated by root process\n", sum);
/* and, finally, I collet the partial sums from the slave processes,
* print them, and add them to the grand sum, and print it */
for(an_id = 1; an_id < num_procs; an_id++) {
ierr = MPI_Recv( &partial_sum, 1, MPI_LONG, MPI_ANY_SOURCE,
return_data_tag, MPI_COMM_WORLD, &status);
sender = status.MPI_SOURCE;
printf("Partial sum %i returned from process %i\n", partial_sum, sender);
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 18
sum += partial_sum;
}
printf("The grand total is: %i\n", sum);
}
else {
/* I must be a slave process, so I must receive my array segment,
* storing it in a "local" array, array1. */
ierr = MPI_Recv( &num_rows_to_receive, 1, MPI_INT,
root_process, send_data_tag, MPI_COMM_WORLD, &status);
ierr = MPI_Recv( &array2, num_rows_to_receive, MPI_INT,
root_process, send_data_tag, MPI_COMM_WORLD, &status);
num_rows_received = num_rows_to_receive;
/* Calculate the sum of my portion of the array */
partial_sum = 0;
for(i = 0; i < num_rows_received; i++) {
partial_sum += array2[i];
}
/* and finally, send my partial sum to hte root process */
ierr = MPI_Send( &partial_sum, 1, MPI_LONG, root_process,
return_data_tag, MPI_COMM_WORLD);
}
ierr = MPI_Finalize();
}
Output
please enter the number of numbers to sum: 10
sum 55 calculated by root process
Partial sum 55 returned from process 1
The grand total is: 110
Result
Thus the program successfully executed
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 19
[Link].10
Date: Write a Program to demonstrate MPI-send-and-receive in C.
Aim:
To find Write a Program to demonstrate MPI-send-and-receive in C.
program:
const int recvsize = 50;
#include <mpi.h>
#include "stdio.h"
int main(int argc, char** argv)
{
// Initialize the MPI environment
MPI_Init(NULL, NULL);
// Get the rank of the process
int PID;
MPI_Comm_rank(MPI_COMM_WORLD, &PID);
// Get the number of processes
Int number_of_processes;
MPI_Comm_size (MPI_COMM_WORLD, &number_of_processes);
// Get the name of the processor
Char processor_name [MPI_MAX_PROCESSOR_NAME];
int name length;
MPI_Get_processor_name (processor_name, &name length);
// Print off a hello world message
Printf ("Hello MPI user: from process PID %d out of %d processes on machine %s\n", PID,
number_of_processes, processor_name);
// finalize the MPI environment
MPI_Finalize ();
return 0;
}
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 20
Output
Hello MPI user: from process PID 0 out of 4 processes on machine node1
Hello MPI user: from process PID 1 out of 4 processes on machine node2
Hello MPI user: from process PID 2 out of 4 processes on machine node1
Hello MPI user: from process PID 3 out of 4 processes on machine node2
Result
Thus the program successfully executed.
CP4292 –MULTICORE AECHITECTURE AND PROGRAMMING Page | 21