Question

Write an MPI program that implements multiplication of a vector by a scalar and dot product....

Write an MPI program that implements multiplication of a vector by a scalar and dot product. The user should enter two vectors and a scalar, all of which are read in by process 0 and distributed among the processes. The results are calculated and collected onto process 0, which prints them. You can assume that n, the order of the vectors, is evenly divisible by comm_sz.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Objective : Vector_Vector Multiplication (Using Block Partition)

Input : Read files (vdata1.inp) for Vector_A and
(vdata2.inp) for Vector_B

Output : Result of Vector-Vector Multiplication on processor 0

Necessary Condition : Number of Processes should be less than
or equal to 8. Vector size for Vectors A and
B should be properly striped. that is Vector
size should be properly divisible by
Number of processes used.

**********************************************************************
*/

#include <stdio.h>
#include <mpi.h>
#include <stdlib.h>

#define Epsilon 1.0E-10
main(int argc, char** argv)
{

int Numprocs, MyRank;
int VectorSize, VectorSize_A, ScatterSize, VectorSize_B;
int index, iproc;
int Root = 0, ValidInput = 1, ValidOutput = 1;
float *Mybuffer_A, *Mybuffer_B, MyFinalVector, FinalAnswer;
float CheckResultVector;
FILE *fp;
int VectorA_FileStatus = 1, VectorB_FileStatus = 1;
float *Vector_A, *Vector_B;


/* ........MPI Initialisation .......*/

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &MyRank);
MPI_Comm_size(MPI_COMM_WORLD, &Numprocs);

if(MyRank == Root) {

/*.......Read the Vector A Input file ......*/
if((fp = fopen ("./data/vdata1.inp", "r")) == NULL) {
VectorA_FileStatus = 0;
}

if(VectorA_FileStatus != 0) {
fscanf(fp, "%d\n", &VectorSize_A);

/* .......Allocate memory and read data for vector A .....*/
Vector_A = (float *) malloc(VectorSize_A*sizeof(float));

/* .......Read data for matrix .....*/
for(index=0; index< VectorSize_A; index++) {
fscanf(fp, "%f", &Vector_A[index]);
}
fclose(fp);
}

/*.......Read the Vector B Input file ......*/

if((fp = fopen ("./data/vdata2.inp", "r")) == NULL) {
VectorB_FileStatus = 0;
}

if(VectorB_FileStatus != 0) {
fscanf(fp, "%d\n", &VectorSize_B);
  
/* .......Allocate memory and read data for vector B .....*/
Vector_B = (float *)malloc(VectorSize_B*sizeof(float));
for(index=0; index< VectorSize_B; index++) {
fscanf(fp, "%f", &Vector_B[index]);
}
fclose(fp);
}

if(VectorSize_A != VectorSize_B)
ValidInput = 0;

}/* MyRank == 0 */

MPI_Barrier(MPI_COMM_WORLD);

MPI_Bcast(&VectorA_FileStatus, 1, MPI_INT, Root, MPI_COMM_WORLD);
if(VectorA_FileStatus == 0) {
if(MyRank == Root) printf("Can't open input file for Vector A\n");
MPI_Finalize();
exit(-1);
}

MPI_Bcast(&VectorB_FileStatus, 1, MPI_INT, Root, MPI_COMM_WORLD);
if(VectorB_FileStatus == 0) {
if(MyRank == Root) printf("Can't open input file for Vector B\n");
MPI_Finalize();
exit(-1);
}

MPI_Bcast(&ValidInput, 1, MPI_INT, Root, MPI_COMM_WORLD);
if(ValidInput == 0) {
if(MyRank == Root) printf("Size of both vectors is not equal ....\n");
MPI_Finalize();
exit(-1);
}

VectorSize = VectorSize_A;
MPI_Bcast(&VectorSize, 1, MPI_INT, Root, MPI_COMM_WORLD);

if(VectorSize < Numprocs) {
MPI_Finalize();
if(MyRank == 0)
printf("VectorSize should be more than No of Processors ..... \n");
exit(0);
}   

if(VectorSize % Numprocs != 0) {
MPI_Finalize();
if(MyRank == 0) {
printf("VectorSize Can not be Striped Evenly ..... \n");
}
exit(0);
}   

/* Scatter vector A and B */

ScatterSize = VectorSize / Numprocs;
Mybuffer_A = (float *)malloc(ScatterSize * sizeof(float));
MPI_Scatter( Vector_A, ScatterSize, MPI_FLOAT,
Mybuffer_A, ScatterSize, MPI_FLOAT,
Root, MPI_COMM_WORLD);

Mybuffer_B = (float *)malloc(ScatterSize * sizeof(float));
MPI_Scatter( Vector_B, ScatterSize, MPI_FLOAT,Mybuffer_B,
ScatterSize, MPI_FLOAT,
Root, MPI_COMM_WORLD);

/* Calculate partial sum */

MyFinalVector = 0.0;
for(index = 0 ; index < ScatterSize ; index++)
MyFinalVector += (Mybuffer_A[index] * Mybuffer_B[index]);

/* Collective computation : Final answer on process 0 */
MPI_Reduce(&MyFinalVector, &FinalAnswer, 1, MPI_FLOAT, MPI_SUM, Root, MPI_COMM_WORLD);

if(MyRank == 0){
CheckResultVector = 0.0;

for(index = 0 ; index < VectorSize ; index++)
CheckResultVector += (Vector_A[index] * Vector_B[index]);

if(fabs((double)(FinalAnswer-CheckResultVector)) > Epsilon){
printf("Error %d\n",index);
ValidOutput = 0;
}
if(ValidOutput)
printf("FinalResult = %f\n", FinalAnswer);
else
printf("Result may be wrong\n");

free(Vector_A);
free(Vector_B);
}

free(Mybuffer_A);
free(Mybuffer_B);
MPI_Finalize();
}

Add a comment
Know the answer?
Add Answer to:
Write an MPI program that implements multiplication of a vector by a scalar and dot product....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Simulation: Write a MIPS program which computes the vector dot product. Vector dot product involv...

    Simulation: Write a MIPS program which computes the vector dot product. Vector dot product involves calculations of two vectors. Let A and B be two vectors of length n. Their dot product is defined as: Dot Product-2.0 A(i): B(i) Where the result is stored in memory location DOTPROD. The first elements of each vector, A(0) and B(0), are stored at memory locations A_vec and B_vec, with the remaining elements in the following word locations Results: Put your MIPS code here...

  • in c++ Write a program which can find the dot product of two vectors of the...

    in c++ Write a program which can find the dot product of two vectors of the same length ?. The user will enter the length ?. Use the length to control how many times you loop. The result is a scalar value and not a vector. If the dot product is zero, then the two vectors are perpendicular.

  • matlab progam A commonly used matrix operation in linea alpebrais matrix matrix multiplication. Write a script...

    matlab progam A commonly used matrix operation in linea alpebrais matrix matrix multiplication. Write a script that reads a matrix M from the that this is different from the clement wise Squaring of the matrix ( M 2). To do this, follow these steps income che of them (M2) Note a. Read a 2x2 matrix from the user and store it in M. Make sure you let the user know what to input b. Print the dimensions of the matrix,...

  • Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations....

    Python REALLY NEED HELP !!!!!!!!! [10pts] Write the class Vector that supports the basic vector operations. Such operations are addition (+) and subtraction (-) of vectors of the same length, dot product (*) and multiplication (*) of a vector by a scalar. All methods must return (not print) the result. Your class should also support the rich comparison for equality (==) - You must use the special methods for those 4 operators in order to override their behavior - You...

  • Please and thank you 2) (5 pts) Arrays & Vectors: write a program which prompts the...

    Please and thank you 2) (5 pts) Arrays & Vectors: write a program which prompts the user to enter numbers (terminated with a non-numeric), reads them simultaneously into an array of doubles and into a vector of doubles, then prints A) the array elements in the original order B) the vector elements in reverse order C) the average of the vector elements D) the largest of the vector elements Example program output, user input shown underlined Enter a list of...

  • You are to write a program that implements a Reverse Polish Notation Calculator in C using...

    You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files. Link to the files to start and have a general view of the program: https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0 Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed...

  • The first issue: write a program in C #, which does the following: 1- Read two...

    The first issue: write a program in C #, which does the following: 1- Read two string strings str1 and str2 entered by the user so that each one is composed from at least five characters. 2- Transferring the first two letters of the str1 to the end of the str2, then writing each of them. 3- Delete the first and last characters of the str2 string, then write the new string that results from Deletion process. 4- Moving two...

  • Objective: Write a program that implements the Game of Life cellular automata system invented by John...

    Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...

  • Write a program named text_indexing.c that does the following: Reads text and stores it as one...

    Write a program named text_indexing.c that does the following: Reads text and stores it as one string called text. You can read from a file or from the user. (In my implementation, I read only one paragraph (up to new line) from the user. With this same code, I am able to read data from a file by using input redirection (executable < filename) when I run the program. See sample runs below). You can assume that the text will...

  • Roman Roulette: Write this program in C++ Background The historian H relates how, in the conflict...

    Roman Roulette: Write this program in C++ Background The historian H relates how, in the conflict of 67 C.E., the Romans took the town of Jotapata which he was commanding. Escaping, He found himself trapped in a cave with 40 companions. The Romans discovered his whereabouts and invited him to surrender, but his companions refused to allow him to do so. He, therefore, suggested that they kill each other, one by one, the order to be decided by lot. Tradition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT