Question
The syntax within the terminal will be: ./mympi invertMatrix dimension n With n being a number between 1 and 16 in binary terms (1,2,4,8,16). Your
The syntax within the terminal will be:
./mympi invertMatrix dimension n With n being a number between 1 and 16 in binary terms (1,2,4,8,16). Your program must work with all of these inputs.
Program 1:
Program 1, named mympi will start program 2, named invertMatrix, n times. The second program should divide the task of finding the inverse of a matrix with a certain dimension. I.e. program 1 should spawn the program 2 n times using execv terminate clean without zombies.
Program 2:
This program that finds the inverse of square matrices (float datatype). The size of the matrix will can is specified over the command line. Keep in mind if you have a 2x2 matrix, its dimension is 4. Have the following matrices: A, Ainv and I, I1, I2. A is the square matrix populated with random numbers that you will find its inverse Ainv from. Make sure thats the case by writing a function for doing this, like randommatrix(A).
I is the identity matrix and can be used to control if A*Ainv = I. Please do so! A, Ainv and I,I2,I3 should be shared over the other processes and cleaned up accordingly at the end.
The program should calculate:
I1 = A*Ainv; I2 = Ainv*A; //check that I1 == I2 == I
In that order.
Have synch() functions between the calculation, before it and after it.
Print the result of the calculation.
And: Print the time taken from before the first calculation starts until the last one ends.
Needless to say, print only with one process.
In C Language. Sample Matrix
#include
#define MATRIX_DIMENSION_XY 10 //************************************************************************************************************************ // sets one element of the matrix void set_matrix_elem(float *M,int x,int y,float f) { M[x + y*MATRIX_DIMENSION_XY] = f; } //************************************************************************************************************************ // checks to see if both matrices are the same int quadratic_matrix_compare(float *A,float *B) { for(int a = 0;a //nullify the result matrix first for(int a = 0;a //synch algorithm. make sure, ALL processes get stuck here until all ARE here void synch(int par_id,int par_count,int *ready) { ready[par_id] = 1; int count = 0; while(count < par_count) { while(ready[count] == 0); count++; } ready[par_id] = 2; if (par_id == 0) { int count = 0; while(count < par_count) { while(ready[count] == 1); count++; } for (int i = 1; i int fd[4]; if(par_id==0) { //init the shared memory for A,B,C, ready. shm_open with C_CREAT here! then ftruncate! then mmap fd[0] = shm_open("A", O_RDWR | O_CREAT, 0777); fd[1] = shm_open("B", O_RDWR | O_CREAT, 0777); fd[2] = shm_open("C", O_RDWR | O_CREAT, 0777); fd[3] = shm_open("synch", O_RDWR | O_CREAT, 0777); ftruncate(fd[0], sizeof(float)*100); ftruncate(fd[1], sizeof(float)*100); ftruncate(fd[2], sizeof(float)*100); ftruncate(fd[3], sizeof(float)*100); A = (float*)mmap(0, sizeof(float)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd[0], 0); B = (float*)mmap(0, sizeof(float)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd[1], 0); C = (float*)mmap(0, sizeof(float)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd[2], 0); ready = (int*)mmap(0, sizeof(int)*(par_count+1), PROT_READ|PROT_WRITE, MAP_SHARED, fd[3], 0); for (int i = 0; i <= par_count; i++) { ready[i] = 0; } } else { //init the shared memory for A,B,C, ready. shm_open withOUT C_CREAT here! NO ftruncate! but yes to mmap sleep(2); //needed for initalizing synch fd[0] = shm_open("matrixA", O_RDWR, 0777); fd[1] = shm_open("matrixB", O_RDWR, 0777); fd[2] = shm_open("matrixC", O_RDWR, 0777); fd[3] = shm_open("synchobject", O_RDWR, 0777); A = (float*)mmap(0, sizeof(int)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd[0], 0); B = (float*)mmap(0, sizeof(int)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd[1], 0); C = (float*)mmap(0, sizeof(int)*100, PROT_READ|PROT_WRITE, MAP_SHARED, fd[2], 0); ready = (int*)mmap(0, sizeof(int)*(par_count+1), PROT_READ|PROT_WRITE, MAP_SHARED, fd[3], 0); ready[par_id] = 0; } synch(par_id,par_count,ready); if(par_id==0) { //initialize the matrices A and B for(int x = 0; x < 100; x++) { for(int y = 0; y < 100; y++) { set_matrix_elem(A, x, y, 1); set_matrix_elem(B, x, y, 1); } } } synch(par_id,par_count,ready); quadratic_matrix_multiplication_parallel(A, B, C, par_id, par_count); synch(par_id,par_count,ready); if(par_id==0) { stop = clock(); double timetaken = ((double)(stop - start))/CLOCKS_PER_SEC; printf(" Time taken to multiply: %f seconds", timetaken); quadratic_matrix_print(C); fflush(0); } synch(par_id, par_count, ready); close (fd[0]); close (fd[1]); close (fd[2]); close (fd[3]); shm_unlink("A"); shm_unlink("mB"); shm_unlink("C"); shm_unlink("synch"); //lets test the result: float M[MATRIX_DIMENSION_XY * MATRIX_DIMENSION_XY]; quadratic_matrix_multiplication(A, B, M); if (quadratic_matrix_compare(C, M)) { printf("full points! "); fflush(0); } else { printf("buuug! "); fflush(0); } return 0; } Program 2 Sample #include //calls program1 int main(int argc, char *argv[]) { char*args[4]; args[0] = malloc(100); args[1] = malloc(100); args[2] = malloc(100); args[3] = malloc(100); strcpy(args[0], argv[1]); strcpy(args[2], argv[2]); args[3] = NULL; for (int i = 0; i < atoi(argv[2]); i++) { if (fork() != 0) { sprintf(args[1], "%d", i); execv(argv[1], args); return 0; } else { wait(0); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started