Question

In: Computer Science

Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...

Programming Language Required: C

Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in a text file.

Solutions

Expert Solution

1). ANSWER :

GIVENTHAT :

C - PROGRAM :

#include <bits/stdc++.h> 
using namespace std; 
int step_i = 0; 
  
void* multi(void* arg) 
{ 
    int core = step_i++; 
  
    // Each thread computes 1/4th of matrix multiplication 
    for (int i = core * ma1 / 4; i < (core + 1) * ma1 / 4; i++)  
        for (int j = 0; j < mb1; j++)  
            for (int k = 0; k < mb2; k++)  
                matC[i][j] += matA[i][k] * matB[k][j]; 
} 
  
// Driver Code 
int main() 
{
    fstream file;
    string word, filename;
    
    filename = "example.txt";
    file.open(filename.c_str());
    
    cout<<"Enter dimensions of the first matrix :";
    int ma1, ma2, mb1, mb2;
    cin>>ma1>>ma2;
    
    cout<<"Enter dimensions of the second matrix :";
    cin>>mb1>>mb2;
    
    int matA[ma1][ma2];
    int matB[mb1][mb2];
    
    
    if(ma2 != mb1)
    {
        cout<<"The matrices can't be multiplied.";
        return 0;
    }
    
    int matC[ma1][mb2];
    int MAX_THREAD = ma2;
    
    for(int i = 0; i<ma1; i++)
    {
        for(int j = 0; j<ma2; j++)
        {
            file>>word;
            matA[i][j] = word;
        }
    }
    
    for(int i = 0; i<mb1; i++)
    {
        for(int j = 0; j<mb2; j++)
        {
            file>>word;
            matB[i][j] = word;
        }
    }
  
    // Displaying matA 
    cout << endl 
         << "Matrix A" << endl; 
    for (int i = 0; i < ma1; i++) { 
        for (int j = 0; j < ma2; j++)  
            cout << matA[i][j] << " "; 
        cout << endl; 
    } 
  
    // Displaying matB 
    cout << endl 
         << "Matrix B" << endl; 
    for (int i = 0; i < mb1; i++) { 
        for (int j = 0; j < mb2; j++)  
            cout << matB[i][j] << " ";         
        cout << endl; 
    } 
  
    // declaring four threads 
    pthread_t threads[MAX_THREAD]; 
  
    // Creating four threads, each evaluating its own part 
    for (int i = 0; i < MAX_THREAD; i++) { 
        int* p; 
        pthread_create(&threads[i], NULL, multi, (void*)(p)); 
    } 
  
    // joining and waiting for all threads to complete 
    for (int i = 0; i < MAX_THREAD; i++)  
        pthread_join(threads[i], NULL);     
  
    // Displaying the result matrix 
    cout << endl 
         << "Multiplication of A and B" << endl; 
    for (int i = 0; i < ma1; i++) { 
        for (int j = 0; j < mb2; j++)  
            cout << matC[i][j] << " ";         
        cout << endl; 
    } 
    return 0; 
} 

Related Solutions

Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
Using c# programming language Write a program that mimics a lottery game. Have the user enter...
Using c# programming language Write a program that mimics a lottery game. Have the user enter 3 distinct numbers between 1 and 10 and match them with 3 distinct, randomly generated numbers between 1 and 10. If all the numbers match, then the user will earn $10, if 2 matches are recorded then the user will win $3, else the user will lose $5. Keep tab of the user earnings for, let say 5 rounds. The user will start with...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT